Repository navigation
Description
Context
Device.CreateBindGroupDesc appends every new bind group to d.retainedBindGroups (gpu/device.go:1445), whether or not the device retains resources (retained: d.retainResources at device.go:1443 only records the flag). Device.CreateBindGroupLayoutDesc does the same with d.retainedLayouts (device.go:1304).
BindGroup.Close (gpu/device.go:744-753) only sets closed and enqueues the destroy op; it does not remove the group from the list. The list is only ever rebuilt in restoreRetainedFrom, which skips closed groups when it copies them onto the new device (device.go:1894-1992). Without a device restore, the list is never pruned. Each entry keeps its BindGroup reachable, and with it the cloned entries slice and the Buffer, Texture and Sampler values it points to, even after all of them are closed.
Buffers, textures, samplers and pipelines are only added to their lists when they are retained (device.go:965-980, 1090-1091, 1243-1244, 1661-1662), and Buffer.Close / Texture.Close drop their shadow data (device.go:513, 564), so they leak much less. Bind groups and layouts are the only kinds added unconditionally.
Why it matters
Creating a bind group per draw or per frame and closing it afterwards is a normal pattern when the bound textures change often (sprite batches, materials whose texture set changes, post-processing chains that rebuild after a resize). On NSGo each of those groups stays in retainedBindGroups for the life of the device, so memory grows linearly with frames rendered, and the next device restore has to walk the whole history. A game layer built on NSGo (nsgo-dev/game) currently caches bind groups for its most recent texture/sampler combinations to keep the growth down, but it cannot remove it.
The scope cleanup registered at device.go:1446 adds a second, separate growth for long-lived scopes; that is the same pattern as for other resources through Device.bindCleanup (device.go:295), and fixing it is not required for this issue.
Proposal
Keep retainedBindGroups and retainedLayouts limited to open resources: either remove the entry in BindGroup.Close / BindGroupLayout.Close (an index or a set keyed by pointer keeps that O(1)), or compact the lists when the closed count passes a threshold, for example on Submit. Consider adding bind groups and layouts only when they can be restored, consistent with the other resource kinds.
Acceptance
- Creating and closing N bind groups on one device, without a device restore, leaves
retainedBindGroups(andretainedLayoutsfor layouts) at the size it had before. - Closed bind groups, and the closed buffers and textures they referenced, become unreachable from the
Device. - Device restore still recreates every open bind group and layout it does today.
- A test in
gpucovers the create/close loop and a restore after it.
Activity