Skip to content

gpu: Encoder has no Device() accessor, so helpers that take only an encoder cannot detect a restored device #2345

Backlog

Description

@houko

Context

gpu.Frame exposes the device it belongs to (func (f *Frame) Device() *Device, gpu/device.go:3086 on main), but gpu.Encoder (gpu/device.go:2276-2283), RenderPass and ComputePass do not. The encoder keeps the device in an unexported dev field and checks every resource against it: RenderPass.SetBindGroup (gpu/device.go:2708-2719) returns without recording anything when group.dev != p.enc.dev || group.epoch != p.enc.epoch, and SetVertexBuffer (:2721), SetIndexBuffer (:2734) and ComputePass.SetBindGroup (:2421) do the same.

After a device or surface restore, gpu.View builds a new *Device and passes it to OnReady / OnDeviceRestored (gpu/view.go:215-244), moving retained resources onto it with restoreRetainedFrom (gpu/device.go:1788).

Why it matters

Rendering helpers in a library naturally take the encoder they record into (func (r *Renderer) Draw(enc *gpu.Encoder, ...)) and lazily create or look up their own bind groups, buffers and pipelines. Without Encoder.Device() such a helper cannot tell which device the encoder records for, so it cannot notice that the device changed after a restore and rebuild its cached resources for the new one. Its draws then silently record nothing, because the cached bind groups belong to the old device; the drop is intended for stale handles (docs/architecture/gpu.md, "Device-loss recovery"), but here the helper has no way to avoid it.

A game layer built on NSGo (nsgo-dev/game) works around it by passing the *gpu.Device next to the encoder in every render call, which every caller has to thread through and keep consistent.

Proposal

Add func (e *Encoder) Device() *Device, mirroring Frame.Device, returning the device the encoder was begun on (nil for a nil encoder). Optionally add the same accessor to RenderPass and ComputePass.

Acceptance

  • dev.BeginEncoder().Device() == dev, and a nil encoder returns nil.
  • After app.TriggerGPUDeviceLost and app.RestoreGPUDevice in nsgotest, an encoder begun on the restored device reports the new device, distinct from the lost one.
  • docs/architecture/gpu.md lists the accessor next to the other Encoder methods.

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