diff options
| author | Darren Wihandi <65404740+fairywreath@users.noreply.github.com> | 2025-04-27 14:31:34 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-27 14:31:34 -0600 |
| commit | 8f6c6e333c06ae1c3b9f00396563c14a2ae09b4d (patch) | |
| tree | ca752d05329a697f46d7e047d7e7c1c5de9fdb7e | |
| parent | a5efbb1b775afb2f6b29b37d39947c41744bb005 (diff) | |
Emit SPIRV NonReadable decoration for WTexture* (#6922)
| -rw-r--r-- | source/slang/slang-emit-glsl.cpp | 10 | ||||
| -rw-r--r-- | source/slang/slang-emit-spirv.cpp | 20 | ||||
| -rw-r--r-- | tests/wgsl/write-only-texture.slang | 12 |
3 files changed, 38 insertions, 4 deletions
diff --git a/source/slang/slang-emit-glsl.cpp b/source/slang/slang-emit-glsl.cpp index 848d9c08d..5e9fa7f70 100644 --- a/source/slang/slang-emit-glsl.cpp +++ b/source/slang/slang-emit-glsl.cpp @@ -1837,8 +1837,9 @@ bool GLSLSourceEmitter::tryEmitGlobalParamImpl(IRGlobalParam* varDecl, IRType* v void GLSLSourceEmitter::emitImageFormatModifierImpl(IRInst* varDecl, IRType* varType) { - // As a special case, if we are emitting a GLSL declaration - // for an HLSL `RWTexture*` then we need to emit a `format` layout qualifier. + // Special cases when emitting a GLSL declaration for HLSL/Slang `RWTexture* and `WTexture*`: + // - Emit a `format` layout qualifier. + // - Emit `writeonly` memory qualifier for `WTexture*`. if (auto resourceType = as<IRTextureType>(unwrapArray(varType))) { @@ -1855,6 +1856,11 @@ void GLSLSourceEmitter::emitImageFormatModifierImpl(IRInst* varDecl, IRType* var default: break; } + + if (resourceType->getAccess() == SLANG_RESOURCE_ACCESS_WRITE) + { + m_writer->emit("writeonly\n"); + } } } diff --git a/source/slang/slang-emit-spirv.cpp b/source/slang/slang-emit-spirv.cpp index d07d587e5..e4b5c2f4d 100644 --- a/source/slang/slang-emit-spirv.cpp +++ b/source/slang/slang-emit-spirv.cpp @@ -2767,6 +2767,7 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex param->getDataType(), storageClass); maybeEmitPointerDecoration(varInst, param); + maybeEmitWriteOnlyImageDecoration(varInst, param); if (layout) emitVarLayout(param, varInst, layout); emitDecorations(param, getID(varInst)); @@ -5686,6 +5687,25 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex maybeEmitPointerDecoration(varInst, inst->getDataType(), as<IRVar>(inst), inst->getOp()); } + void maybeEmitWriteOnlyImageDecoration(SpvInst* varInst, IRInst* inst) + { + auto ptrType = as<IRPtrTypeBase>(inst->getDataType()); + if (!ptrType) + return; + auto textureType = as<IRTextureType>(ptrType->getValueType()); + if (!textureType) + return; + + if (textureType->getAccess() == SLANG_RESOURCE_ACCESS_WRITE) + { + emitOpDecorate( + getSection(SpvLogicalSectionID::Annotations), + nullptr, + getID(varInst), + SpvDecorationNonReadable); + } + } + SpvInst* emitParam(SpvInstParent* parent, IRInst* inst) { auto paramSpvInst = emitOpFunctionParameter(parent, inst, inst->getFullType()); diff --git a/tests/wgsl/write-only-texture.slang b/tests/wgsl/write-only-texture.slang index 56034ce6a..7fc9d4266 100644 --- a/tests/wgsl/write-only-texture.slang +++ b/tests/wgsl/write-only-texture.slang @@ -1,14 +1,22 @@ //TEST:REFLECTION(filecheck=REFLECT):-stage compute -entry computeMain -target wgsl -no-codegen -//TEST:SIMPLE(filecheck=SPV): -target spirv +//TEST:SIMPLE(filecheck=SPV): -target spirv -emit-spirv-directly +//TEST:SIMPLE(filecheck=SPV): -target spirv -emit-spirv-via-glsl //TEST:SIMPLE(filecheck=CHECK): -target wgsl -entry computeMain -stage compute //TEST:SIMPLE(filecheck=CHECK-WGSL-SPV): -target wgsl-spirv-asm -entry computeMain -stage compute //TEST:SIMPLE(filecheck=HLSL): -target hlsl -entry computeMain -profile cs_6_0 //TEST:SIMPLE(filecheck=GLSL): -target glsl -entry computeMain -stage compute // CHECK: texture_storage_2d<rgba8unorm, write> + +// CHECK-WGSL-SPV: OpDecorate {{.*}} NonReadable // CHECK-WGSL-SPV: OpTypeImage %float 2D {{.}} 0 0 2 Rgba8 + +// SPV: OpDecorate {{.*}} NonReadable // SPV: OpTypeImage %float 2D {{.}} 0 0 2 Rgba8 + // HLSL: RWTexture2D + +// GLSL: writeonly // GLSL: image2D // REFLECT: "name": "writeOnlyTexture" @@ -21,4 +29,4 @@ WTexture2D writeOnlyTexture; void computeMain() { writeOnlyTexture.Store(int2(0,0), float4(0.2, 0.3, 0.4, 1.0)); -}
\ No newline at end of file +} |
