Repository navigation
Description
Context
PipelineDesc.Layouts (gpu/gpu.go:667) and ComputePipelineDesc.Layouts (gpu/gpu.go:680) are indexed by @group(N), but every entry must be a live layout. Device.preparePipelineDesc rejects any nil entry with ErrInvalidDescriptor "pipeline BindGroupLayout is nil or invalid" (gpu/device.go:1506-1511), and Device.CreateComputePipeline does the same with "compute pipeline BindGroupLayout is nil or invalid" (gpu/device.go:2046-2051). So a pipeline whose shader uses @group(0) and @group(3) must still supply real layouts for groups 1 and 2.
WebGPU allows this: GPUPipelineLayoutDescriptor.bindGroupLayouts is a sequence<GPUBindGroupLayout?>, and a null entry is treated as an empty bind group layout, so a pipeline can leave unused group slots empty. Neither the doc comments nor docs/architecture/gpu.md (the PipelineDesc listing at line 207) say that gaps are refused or how an app should fill them. CreateBindGroupLayout (gpu/device.go:1251) appears to accept an empty entries slice, so a zero-entry layout may work as a filler, but that is not documented as the intended pattern, and it is not stated that every backend handles an empty group correctly.
Why it matters
Renderers commonly assign groups by role so that pipeline variants stay compatible: for example group 0 per frame (camera, lights), group 1 per material, group 2 optional effects (fog, IBL), group 3 per object or skinning. A variant without the optional features wants to keep the later groups at their fixed indices and leave the middle ones unused.
A game layer built on NSGo (nsgo-dev/game) hit this when adding GPU skinning. Because nil gaps are refused, the skin palette cannot sit at a fixed group; it is bound at the first free group of each pipeline (group 1 for the unlit pipeline, group 3 for lit PBR, group 2 for material pipelines). With only four groups guaranteed on WebGPU and WebGL 2, that makes skinning mutually exclusive with fog and IBL in the lit pipeline and with fog in the material pipeline, and shader code has to be generated with a different group index per variant. The alternative, creating dummy layouts and empty bind groups for every unused slot, is extra bookkeeping that WebGPU code ported to NSGo does not expect, and its behaviour on each backend is undocumented.
Proposal
Accept nil entries in PipelineDesc.Layouts and ComputePipelineDesc.Layouts with WebGPU semantics: a nil slot is an empty bind group layout.
- In
preparePipelineDescandCreateComputePipeline, keep refusing closed or foreign layouts, but map a nil entry to an empty layout (an internal, cached zero-entry layout per device, or a "no layout" ID that each backend understands). - Validate that the shader declares no bindings in a group whose layout is nil, returning
ErrInvalidDescriptornaming the group, so a mistake is still caught at creation time. - Make the backends (Metal, Direct3D 11, WebGPU, OpenGL / GLES / WebGL 2 slot flattening, software) treat such a slot as having no bindings; on WebGPU pass
nullor an empty layout tocreatePipelineLayout. - Do not require
SetBindGroupfor a nil slot before drawing or dispatching. - Document the rule on
PipelineDesc.Layouts,ComputePipelineDesc.Layoutsand indocs/architecture/gpu.md.
If nil slots are not wanted, the minimum is to document that a zero-entry BindGroupLayout is the supported filler, confirm it works on every backend, and say whether a bind group must be set for it before a draw.
Acceptance
CreatePipelineandCreateComputePipelinesucceed withLayouts: []*gpu.BindGroupLayout{frame, nil, nil, object}for a shader that uses only@group(0)and@group(3), and draws / dispatches using groups 0 and 3 run correctly on the software device undernsgotestand on each backend that supports the pipeline.- A shader that declares a binding in a group whose layout is nil is refused with
ErrInvalidDescriptornaming that group. - Closed or foreign-device layouts are still refused as today.
- The
Layoutsdoc comments anddocs/architecture/gpu.mddescribe nil slots.
Activity