Skip to content

gpu: BindGroupLayoutDesc has no Retained, so a layout nothing retained uses is not restored after device loss #2338

Backlog

Description

@houko

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 bool to BindGroupLayoutDesc, set l.retained = desc.Retained || d.retainResources in CreateBindGroupLayoutDesc, matching the other descriptors.
  • Keep the label on BindGroupLayout and pass it when the layout is recreated in restoreRetainedFrom.
  • Mention in docs/architecture/gpu.md which descriptors take Retained, 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 with BindGroupLayoutDesc{Retained: true} and no bind group or pipeline using it, call TriggerGPUDeviceLostWithReason then RestoreGPUDevice, and check that the layout is Valid() at epoch 2 and that CreateBindGroupDesc with it succeeds on the restored device.
  • A layout created without Retained (and without RetainResources) and not referenced by anything retained still stays invalid after restore.
  • A restored layout keeps its label.
  • docs/architecture/gpu.md lists BindGroupLayoutDesc.Retained with the other opt-in flags.

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