Sitelet https://github.com/microsoft/DirectXShaderCompiler/pull/8947
Skip to content

[SPIR-V] Add support for RWTexture2DMS and RWTexture2DMSArray - #8947

Open
Diego Novillo (dnovillo) wants to merge 1 commit into
microsoft:mainfrom
dnovillo:fix-5244
Open

Diego Novillo (dnovillo) wants to merge 1 commit into
microsoft:mainfrom
dnovillo:fix-5244

Conversation

@dnovillo

Copy link
Copy Markdown
Collaborator

RWTexture2DMS<T> and RWTexture2DMSArray<T> do not compile when targeting SPIR-V. LowerTypeVisitor::lowerResourceType lists neither name, so the type falls through to generic struct lowering.

SPIR-V expresses a writable multisampled image as OpTypeImage with MS of 1 and Sampled of 2, gated by StorageImageMultisample. Every access to one carries a sample index, and both HLSL access forms have to supply it.

This change:

  1. Lowers RWTexture2DMS and RWTexture2DMSArray to a multisampled storage image, and infers StorageImageMultisample, plus ImageMSArray for the arrayed variant, from the lowered type rather than from the HLSL name. Dim::SubpassData is excluded, since the validator's requirement does not reach it and StorageImageMultisample maps to an optional Vulkan device feature.
  2. Adds isRWTextureMS(QualType) to AstTypeProbe and applies it at each emitter dispatch point that has to tell a sample index from a mip level, and adds the two names to the resource lists behind -fspv-reflect and the RelaxedPrecision decoration.
  3. Gives SpirvBuilder::createImageWrite a sample parameter and composes the image operands mask from it, so OpImageWrite carries Sample.
  4. Carries an explicit sample index on <obj>.sample[idx][coord], and sample 0 on the plain <obj>[coord] subscript. Sample 0 is what the DXIL path emits for the same source, per tools/clang/test/HLSLFileCheck/hlsl/objects/RWTexture/rwtexture2dms.hlsl.

Tests:

  • type.rwtexture2dms.hlsl and type.rwtexture2dmsarray.hlsl: the lowered OpTypeImage operands and the capabilities. Separate files because capabilities are module-global.
  • op.rwtexture2dms.access.hlsl: every access form, scalar and vector sampled types, and both GetDimensions overloads.
  • op.rwtexture2dms.ps.hlsl: the shader from the issue. The one test without -fcgl, so capability trimming and validation cover its output.
  • decoration.user-type.hlsl: the two -fspv-reflect strings.
  • vk.subpass-input.hlsl: that a SubpassInputMS does not acquire StorageImageMultisample.

Fixes #5244

Lower both types to a multisampled storage image and carry the sample
index through every read, write and query path.
Copilot AI balanced review requested due to automatic review settings September 22, 2026 18:45
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Two expression-evaluation bugs can produce incorrect coordinates, samples, and side effects.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Adds SPIR-V support for RWTexture2DMS and RWTexture2DMSArray.

Changes:

  • Lowers writable multisampled textures to SPIR-V storage images with required capabilities.
  • Supports sample-indexed reads, writes, queries, reflection, and sample-zero subscripting.
  • Adds documentation and regression tests.

Required fixes:

  • Cache coordinate and sample expressions during component/swizzle read-modify-write operations to avoid duplicate evaluation.
  • Evaluate sample expressions before coordinate expressions on writes, matching source order and read behavior.
File Description
tools/​clang/​test/​CodeGenSPIRV/​vk.subpass-input.hlsl Guards subpass-input capability behavior.
tools/​clang/​test/​CodeGenSPIRV/​type.rwtexture2dmsarray.hlsl Tests arrayed type lowering and capabilities.
tools/​clang/​test/​CodeGenSPIRV/​type.rwtexture2dms.hlsl Tests non-arrayed type lowering.
tools/​clang/​test/​CodeGenSPIRV/​op.rwtexture2dms.ps.hlsl Tests validated pixel-shader output.
tools/​clang/​test/​CodeGenSPIRV/​op.rwtexture2dms.access.hlsl Tests access forms and dimension queries.
tools/​clang/​test/​CodeGenSPIRV/​decoration.user-type.hlsl Tests reflection metadata.
tools/​clang/​lib/​SPIRV/​SpirvEmitter.cpp Implements multisampled access; contains the expression-evaluation issues above.
tools/​clang/​lib/​SPIRV/​SpirvBuilder.cpp Adds sample operands to image writes.
tools/​clang/​lib/​SPIRV/​LowerTypeVisitor.cpp Lowers writable multisampled texture types.
tools/​clang/​lib/​SPIRV/​DeclResultIdMapper.cpp Classifies the resources as UAVs.
tools/​clang/​lib/​SPIRV/​CapabilityVisitor.cpp Infers required SPIR-V capabilities.
tools/​clang/​lib/​SPIRV/​AstTypeProbe.cpp Recognizes writable multisampled textures.
tools/​clang/​include/​clang/​SPIRV/​SpirvBuilder.h Updates the image-write API.
tools/​clang/​include/​clang/​SPIRV/​AstTypeProbe.h Declares the new type probe.
docs/​SPIR-V.rst Documents resource lowering and operations.
docs/​ReleaseNotes.md Announces the new support.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 8480 to +8489
auto *loc = doExpr(indexExpr, range);
const QualType imageType = baseExpr->getType();
// A plain <object>[coord] write to RWTexture2DMS(Array) targets sample 0,
// matching the DXIL path.
SpirvInstruction *sample = nullptr;
if (isMSWrite)
sample = doExpr(sampleExpr, range);
else if (isRWTextureMS(imageType))
sample = spvBuilder.getConstantInt(astContext.UnsignedIntTy,
llvm::APInt(32, 0));

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

[SPIR-V][SM6.7] Add support for RWTexture2DMS in the SPIR-V backend

2 participants