Repository navigation
Description
Context
BufferDesc, TextureDesc, RenderTargetDesc, SamplerDesc and PipelineDesc each have a Retained bool that opts one resource into automatic recreation after device loss (gpu/gpu.go:51, :282, :301, :349, :673). BindGroupLayoutDesc has no such field (gpu/gpu.go:395-398), and Device.CreateBindGroupLayoutDesc sets the layout's retained flag only from the view-wide switch, retained: d.retainResources (gpu/device.go:1302).
Device.restoreRetainedFrom then restores a layout only when it is retained or appears in neededLayouts (gpu/device.go:1931-1932). neededLayouts is built from the layouts of retained pipelines (gpu/device.go:1883-1892) and of bind groups that can be restored (gpu/device.go:1925-1928). So, unless ViewProps.RetainResources is set, a layout that no retained pipeline or restorable bind group refers to at the moment of the loss stays on the old epoch: Valid() is false after OnReady fires on the new device, and bind groups or pipelines created from it later fail.
docs/architecture/gpu.md ("Resource lifetime, errors, and device loss") does not say that layouts are excluded from the per-descriptor opt-in, and the ViewProps.RetainResources comment (gpu/view.go:47) presents the per-resource flags as the finer-grained alternative.
A smaller related detail: a restored layout is recreated without its label (gpu/device.go:1949 builds GPUBindGroupLayoutDesc{Entries: dEntries}, and BindGroupLayout does not keep the label, gpu/device.go:679-686), so debug tools show restored layouts unnamed while the original create passed debugLabel(desc.Label) (gpu/device.go:1295).
Why it matters
Libraries create layouts up front and bind groups lazily. A game layer built on NSGo (nsgo-dev/game) keeps a set of shared material layouts that are created once with the device and used whenever a material is first created. Everything else it owns is created with Retained: true, so it survives a device loss without touching ViewProps, which belongs to the app. The shared layouts are the exception: if no material of a kind exists when the device is lost, that kind's layout is not restored, and the next material of that kind fails to build. The game layer currently checks each layout's Valid() separately and documents that a layout comes back only while a retained material using it exists.
The only workarounds today are to require apps to set ViewProps.RetainResources (which retains every resource on the view, including per-frame ones) or to recreate the layouts in every OnReady, which defeats the point of the per-resource opt-in for everything that depends on them.
Proposal
- Add
Retained booltoBindGroupLayoutDesc, setl.retained = desc.Retained || d.retainResourcesinCreateBindGroupLayoutDesc, matching the other descriptors. - Keep the label on
BindGroupLayoutand pass it when the layout is recreated inrestoreRetainedFrom. - Mention in
docs/architecture/gpu.mdwhich descriptors takeRetained, and that a layout or bind group without it is restored only when a restored pipeline or bind group needs it.
Acceptance
- In a test like
TestGPUPerDescriptorRetainedOptIn(gpu/lifecycle_test.go:585): create a layout withBindGroupLayoutDesc{Retained: true}and no bind group or pipeline using it, callTriggerGPUDeviceLostWithReasonthenRestoreGPUDevice, and check that the layout isValid()at epoch 2 and thatCreateBindGroupDescwith it succeeds on the restored device. - A layout created without
Retained(and withoutRetainResources) and not referenced by anything retained still stays invalid after restore. - A restored layout keeps its label.
docs/architecture/gpu.mdlistsBindGroupLayoutDesc.Retainedwith the other opt-in flags.
Activity