Skip to content

gpu: closing a resource leaves its cleanup on the scope (and its entry in the retained lists), so recreating render targets on resize grows them without bound #2339

Backlog

Description

@houko

Context

Every gpu.Device.Create* call registers a cleanup on the *core.Scope it is given, and closing the resource never takes that cleanup off again.

Device.bindCleanup (gpu/device.go:295-303 on main) is a plain owner.Cleanup(fn):

func (d *Device) bindCleanup(cx *core.Scope, fn func()) {
	owner := cx
	if owner == nil {
		owner = d.scope
	}
	if owner != nil {
		owner.Cleanup(fn)
	}
}

It is called once per resource: CreateBuffer (gpu/device.go:982), CreateTexture (:1093), CreateSampler (:1246), CreateBindGroupLayout (:1305), CreateBindGroup (:1446), the render pipelines (:1664, :1699), the compute pipeline (:2068) and the timestamp query (:895). CreateRenderTarget goes through CreateTexture for each attachment (:1149, :1163, :1189), so one render target adds up to three entries.

Scope.Cleanup / addCleanup (core/reactive.go:434-442) only append to o.cleanups, which is emptied only when the owner is disposed or its computation runs again (disposeOwned, core/reactive.go:400-412). Texture.Close (gpu/device.go:559-569), Buffer.Close and RenderTarget.Close (:620-634) mark the handle closed and enqueue the destroy op, but leave the closure on the scope. That closure holds the *Texture / *Buffer (descriptor, label, device pointer) until the scope goes away.

The same applies to the retained lists: with View.RetainResources or Desc.Retained, CreateBuffer / CreateTexture / CreateSampler / the pipelines append to d.retainedBuffers, d.retainedTextures and so on (gpu/device.go:980, :1091, ...), and Close does not remove the entry; restoreRetainedFrom (gpu/device.go:1788) just skips closed ones.

docs/architecture/gpu.md ("Disposal via *core.Scope") says each resource "implements Close() error for immediate release and also registers a cleanup on the *core.Scope passed to its constructor", which reads as if Close releases everything, so nothing warns about this.

Why it matters

The natural place to create size-dependent resources is a long-lived scope, the one that owns the gpu.View, and to recreate them from View.OnResize: offscreen render targets, depth buffers, post-processing chains, bloom mip chains, shadow maps whose size follows a quality setting. Each resize closes the old targets and creates new ones on that same scope, and each adds one to three closures that stay until the view unmounts. A window drag on desktop fires many resizes per second; a game that stays open for hours, or that recreates a few transient buffers per level or per frame on the view's scope, grows that scope's cleanup list (and, with retention on, the device's retained lists) without bound.

This is the same class of problem #198 fixed for Scope.AfterFunc. A game layer built on NSGo (nsgo-dev/game) currently works around it in its render-texture type by creating each target under cx.Child() and disposing that child scope on every resize and on close. Every app that resizes offscreen targets would have to rediscover that.

Proposal

Make Close drop what the constructor registered, as AfterFunc does since #198:

  • Give the gpu package a way to unregister. Either keep a per-scope set of live resources in gpu (register one cleanup per scope the first time it is used, keep the resources in a set, remove on Close, close whatever is left when the scope's cleanup runs), or add an unexported or core-internal removable cleanup (for example addCleanup returning a handle that Close can cancel) and use it from bindCleanup.
  • Have every Close (buffer, texture, sampler, bind group layout, bind group, pipelines, compute pipeline, timestamp query) remove its entry, and remove closed resources from the device's retained* lists as well.
  • State in docs/architecture/gpu.md that a closed resource keeps nothing on its scope or device, so recreating resources under a long-lived scope does not grow it.

Acceptance

  • Creating and closing a texture (or a render target, or a buffer) N times under one scope leaves that scope's cleanup count, and the device's retained lists, at a constant size; a test in gpu asserts it (for example via an internal counter or export_test.go).
  • Disposing the scope still closes every resource that was not closed, and closing a resource after its scope is disposed stays a no-op.
  • Retained resources that were not closed are still recreated by restoreRetainedFrom after a simulated device loss (app.TriggerGPUDeviceLost / app.RestoreGPUDevice).
  • docs/architecture/gpu.md describes the behaviour.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions