Support dual-source blend outputs in Metal #8007
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds support for dual-source blend outputs in the Metal backend. Previously, when using
[[vk::location(0), vk::index(1)]]
attributes on fragment shader outputs, the Metal backend would incorrectly generate[[color(1)]]
instead of the correct[[color(0), index(1)]]
attribute.Problem
DX11, Vulkan, and Metal all support dual-source blend. In DX11, it's done by writing to
SV_Target0
andSV_Target1
. On Vulkan and Metal, this is done by writing to color 0 index 0 and color 0 index 1. Slang can already generate SPIR-V shaders that write color 0 index 1 by marking the output with[[vk::location(0), vk::index(1)]]
, but the Metal backend was ignoring the index information.For example, this Slang code:
Previously generated incorrect Metal output:
Solution
Modified the
SystemValueSemanticName::Target
case inLegalizeMetalEntryPointContext::getSystemValueInfo()
to:IRGLSLLocationDecoration
to extract the correct location value fromvk::location
attributesvk::index
valuecolor(location)
with optional, index(space)
when space != 0Now the same code correctly generates:
This enables proper dual-source blending support in Metal, allowing shaders to write to two different blend sources at the same color attachment location.
Fixes #8003.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.