You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature request: ignore shader writes to color attachments #2060
I would like to propose an ability to discard shader writes to certain color attachments if it is doable.
Use case from my codebase:
I have a G-Buffer with Colors, Positions, Normals. I want to render opaque objects first and write associated g-buffer data. These data will be later used for (SS)AO (only opaque objects get AO computed). In the second pass I want to render transparent data into another color attachment BUT ignore writes of shader to positions and normals.
In this case I could use one shader with one pipeline with the desired functionality.
You wouldn't use the same pipeline. You'd definitely need a different pipeline, which would have smaller number of color targets in it. But an investigation is needed to figure out if you can use the same shader. The question is, basically, whether or not the native APIs support this.
You could also use a different pipeline and set writeMask in GPUColorTargetState to 0 for the targets you want to ignore, then you don't need to switch to a new pass. I'm not sure if or how this would impact performance.
Trying to find out the relevant Vulkan spec bits. Maybe it's supported, but the spec isn't clear about it:
The pipeline creation rule:
VUID-VkGraphicsPipelineCreateInfo-attachmentCount-00746
If the pipeline is being created with fragment output interface state, and the subpass uses color attachments, the attachmentCount member of pColorBlendState must be equal to the colorAttachmentCount used to create subpass
The fragment output interface matching rule:
A fragment shader output variable identified with a Location decoration of i is directed to the color attachment indicated by pColorAttachments[i], after passing through the blending unit as described in Blending, if enabled. Locations are consumed as described in Location Assignment. The number of available fragment output locations is given by the maxFragmentOutputAttachments member of the VkPhysicalDeviceLimits structure.
The exact meaning of this is that pColorAttachments[i] (of the subpass) is used for each shader output, so it can't be smaller. But maybe the spec isn't precise?
But an investigation is needed to figure out if you can use the same shader. The question is, basically, whether or not the native APIs support this.
I don't see why -- presumably the browser's shader compiler could remove all writes to invalid attachments at pipeline compilation time? Or set the write mask to 0?
It's worth noting that D3D12 allows shaders to write to "unbound RTVs" (null RTVs), even with non-zero write masks. It's simply treated the same as a zero write mask.
I don't see why -- presumably the browser's shader compiler could remove all writes to invalid attachments at pipeline compilation time? Or set the write mask to 0?
this would be possible if all the shaders are compiled at shader module pipeline creation time, which isn't the case.
Otherwise I made a small Dawn test that has an FS writing to two locations but uses it in a pipeline with only a single color target, and it passes (with debug layers) on all of D3D12/Metal/Vulkan. Turns out we didn't have validation for that in Dawn either! (I checked that Dawn doesn't have a transform that removes the outputs too).
So I think we can just support ignoring FS outputs in WebGPU.
For completeness, I didn't find anything in the Metal docs that say that FS outputs are ignored, or not ignored.
For D3D12 it seems the behavior should be described here but it only mentions outputs being a subset of the render target, not the opposite. @RafaelCintron @jenatalie, do you know if D3D12 allows the set of FS outputs to be larger than the set of the render targets? (the debug device is happy)
Activity
kvark commentedon Aug 24, 2021
You wouldn't use the same pipeline. You'd definitely need a different pipeline, which would have smaller number of color targets in it. But an investigation is needed to figure out if you can use the same shader. The question is, basically, whether or not the native APIs support this.
jobtalle commentedon Aug 24, 2021
You could also use a different pipeline and set
writeMaskinGPUColorTargetStateto0for the targets you want to ignore, then you don't need to switch to a new pass. I'm not sure if or how this would impact performance.kvark commentedon Aug 24, 2021
Trying to find out the relevant Vulkan spec bits. Maybe it's supported, but the spec isn't clear about it:
The pipeline creation rule:
The fragment output interface matching rule:
The exact meaning of this is that
pColorAttachments[i](of the subpass) is used for each shader output, so it can't be smaller. But maybe the spec isn't precise?magcius commentedon Aug 25, 2021
I don't see why -- presumably the browser's shader compiler could remove all writes to invalid attachments at pipeline compilation time? Or set the write mask to 0?
It's worth noting that D3D12 allows shaders to write to "unbound RTVs" (null RTVs), even with non-zero write masks. It's simply treated the same as a zero write mask.
litherum commentedon Aug 25, 2021
See also: #2038
kvark commentedon Aug 25, 2021
@magcius
this would be possible if all the shaders are compiled at
shader modulepipeline creation time, which isn't the case.Kangz commentedon Feb 1, 2022
It is a warning in Vulkan's validation layers but not clear if the spec allows it.
Otherwise I made a small Dawn test that has an FS writing to two locations but uses it in a pipeline with only a single color target, and it passes (with debug layers) on all of D3D12/Metal/Vulkan. Turns out we didn't have validation for that in Dawn either! (I checked that Dawn doesn't have a transform that removes the outputs too).
So I think we can just support ignoring FS outputs in WebGPU.
Kangz commentedon Feb 1, 2022
krOoze (intentionally not
@ing them) pointed out that Vulkan always allows ignoring outputs of shader stages:15.1.3. Interface Matching:
Allow sparse color attachments and ignored FS outputs
Kangz commentedon Feb 7, 2022
For completeness, I didn't find anything in the Metal docs that say that FS outputs are ignored, or not ignored.
For D3D12 it seems the behavior should be described here but it only mentions outputs being a subset of the render target, not the opposite. @RafaelCintron @jenatalie, do you know if D3D12 allows the set of FS outputs to be larger than the set of the render targets? (the debug device is happy)
Allow sparse color attachments and ignored FS outputs
Allow sparse color attachments and ignored FS outputs (#2562)