Skip to content

gpu: SetPipeline does not check the pipeline's colour target formats against the pass attachments #2341

Backlog

Description

@houko

Context

RenderPass.SetPipeline (gpu/device.go:2692) only checks that the pipeline is non-nil, ready, and from the encoder's device and epoch. It never compares the pipeline's PipelineDesc.Targets[i].Format (gpu/gpu.go:650, kept on the pipeline as Pipeline.desc, gpu/device.go:760) or its SampleCount and DepthStencil.Format with the attachments the pass was begun with in Encoder.BeginRenderPass (gpu/device.go:2304). Draw* (gpu/device.go:2769 onward) only checks ended, invalid and pipeValid, so a draw with a mismatched pipeline is recorded.

The software device accepts the mismatch silently. When it rasterizes, it takes each colour target's format from the attachment (internal/driver/gpu_software.go:1037-1104) and quantizes the fragment output to that format (internal/driver/gpu_software.go:2007-2035). It reads only Blend and WriteMask from the pipeline's targets, never Format. The headless nsgotest GPU surface (nsgotest/gpu.go, nsgotest/surface_gpu.go) has no such check either.

docs/architecture/gpu.md:410 says the headless recorder runs "attachment format match against pipeline Targets/DepthStencil" on every Set* and Draw* call and logs a structured record. That check is not implemented.

Why it matters

On real backends the same mismatch is an error or undefined behaviour. WebGPU rejects a draw whose pipeline's colour target formats, sample count or depth format differ from the pass's attachments, and invalidates the command buffer at submit. Metal's validation layer asserts on the same mismatch, and without validation Metal's behaviour is undefined. On D3D11 and GL the result depends on the driver. Because the software device renders the frame anyway, go tool nsgo test passes, and the bug only appears on a device, often as an asynchronous View.OnError far from the SetPipeline that caused it.

A game layer built on NSGo (nsgo-dev/game) hit this with post-processing and offscreen passes. A pipeline built for the swapchain (Targets[0].Format left FormatUndefined, which CreatePipeline resolves to Device.SurfaceFormat(), gpu/device.go:1527-1529) was reused for an FormatRGBA16Float render target. All of its tests passed on the software device.

Repro

On a software device (gpu.NewSoftwareRenderer, or go tool nsgo test):

  1. Create a texture with Device.CreateTexture using Format: gpu.FormatRGBA16Float and Usage: gpu.TextureRenderAttachment.
  2. Create a pipeline with Device.CreatePipeline whose Targets is []gpu.ColorTargetState{{Format: gpu.FormatRGBA8Unorm}}.
  3. enc := dev.BeginEncoder(), pass := enc.BeginRenderPass(gpu.RenderPassDesc{ColorAttachments: []gpu.ColorAttachment{{Texture: tex}}}), pass.SetPipeline(pipe), pass.Draw(3, 0), pass.End(), then submit.

Nothing is rejected or logged, and the triangle is written to the float texture. The same sequence on WebGPU fails validation.

Proposal

In RenderPass.SetPipeline, compare the pipeline with the pass: the number of colour targets against the colour attachments, each Targets[i].Format (after the FormatUndefined → surface format default) against the attachment's texture format (or the swapchain format for a nil Texture), SampleCount against the attachments' sample count, and DepthStencil.Format against the depth attachment, where a backend requires it. On a mismatch, clear pipeValid so later draws are skipped, as for a stale pipeline, and under nsgodev / nsgotest log a structured record naming the pass, the pipeline and the formats. The software device then behaves like the strictest backend, and tests catch the bug.

If some backends tolerate a depth-format mismatch on purpose (the Apple backend builds a Metal pipeline state per depth format, docs/architecture/gpu.md:436), state in the docs which fields are checked so the rule is the same on every backend.

Acceptance

  • SetPipeline with a pipeline whose colour target formats, colour target count or sample count do not match the pass's attachments records no draws, on the software device and on every native backend.
  • Under nsgotest, the mismatch appears as a structured log record, and a test covers an offscreen texture, the swapchain attachment and an MSAA attachment.
  • docs/architecture/gpu.md and the SetPipeline doc comment describe the check, matching what the code does.

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