diff --git a/source/slang/slang-emit-spirv.cpp b/source/slang/slang-emit-spirv.cpp index 9aec497d895..f53066e91a3 100644 --- a/source/slang/slang-emit-spirv.cpp +++ b/source/slang/slang-emit-spirv.cpp @@ -3073,9 +3073,10 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex } if (format == SpvImageFormatUnknown && sampled == ImageOpConstants::readWriteImage) { - // TODO: It may not be necessary to have both of these - // depending on if we read or write - requireSPIRVCapability(SpvCapabilityStorageImageReadWithoutFormat); + // Write-only textures (WTexture*) only need the write capability. + // Read-write and rasterizer-ordered textures need both. + if (inst->getAccess() != SLANG_RESOURCE_ACCESS_WRITE) + requireSPIRVCapability(SpvCapabilityStorageImageReadWithoutFormat); requireSPIRVCapability(SpvCapabilityStorageImageWriteWithoutFormat); } diff --git a/tests/spirv/read-write-image-capability.slang b/tests/spirv/read-write-image-capability.slang new file mode 100644 index 00000000000..ae9f29c244e --- /dev/null +++ b/tests/spirv/read-write-image-capability.slang @@ -0,0 +1,18 @@ +//TEST:SIMPLE(filecheck=CHECK):-target spirv -emit-spirv-directly + +// When using a read-write texture (RWTexture2D) without an explicit format, +// both StorageImageReadWithoutFormat and StorageImageWriteWithoutFormat +// should be required. +// Companion to write-only-image-capability.slang. + +// CHECK-DAG: OpCapability StorageImageReadWithoutFormat +// CHECK-DAG: OpCapability StorageImageWriteWithoutFormat + +RWTexture2D rwTexture; + +[shader("compute")] +[numthreads(1, 1, 1)] +void computeMain() +{ + rwTexture[int2(0, 0)] = rwTexture[int2(1, 1)]; +} diff --git a/tests/spirv/write-only-image-capability.slang b/tests/spirv/write-only-image-capability.slang new file mode 100644 index 00000000000..4a2048d19c0 --- /dev/null +++ b/tests/spirv/write-only-image-capability.slang @@ -0,0 +1,20 @@ +//TEST:SIMPLE(filecheck=CHECK):-target spirv -emit-spirv-directly + +// When using a write-only texture (WTexture2D) without an explicit format, +// only StorageImageWriteWithoutFormat should be required, not +// StorageImageReadWithoutFormat. +// See: https://github.com/shader-slang/slang/issues/9997 + +// CHECK-NOT: OpCapability StorageImageReadWithoutFormat +// CHECK: OpCapability StorageImageWriteWithoutFormat +// CHECK: OpDecorate {{.*}} NonReadable +// CHECK-NOT: OpCapability StorageImageReadWithoutFormat + +WTexture2D writeOnlyTexture; + +[shader("compute")] +[numthreads(1, 1, 1)] +void computeMain() +{ + writeOnlyTexture.Store(int2(0, 0), float4(0, 0, 0, 1)); +}