diff options
| -rw-r--r-- | source/slang/hlsl.meta.slang | 34 | ||||
| -rw-r--r-- | source/slang/slang-emit-spirv.cpp | 15 | ||||
| -rw-r--r-- | tests/hlsl-intrinsic/const-buffer-pointer.slang | 13 |
3 files changed, 54 insertions, 8 deletions
diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang index 477e0cfaf..39ed13488 100644 --- a/source/slang/hlsl.meta.slang +++ b/source/slang/hlsl.meta.slang @@ -12546,7 +12546,37 @@ struct ConstBufferPointer { __glsl_version(450) __glsl_extension(GL_EXT_buffer_reference) - __target_intrinsic(glsl, "$0._data") [__NoSideEffect] - T get(); + T get() + { + __target_switch + { + case glsl: + __intrinsic_asm "$0._data"; + case spirv: + return spirv_asm { + result:$$T = OpLoad $this Aligned $Alignment; + }; + } + } + + __glsl_version(450) + __glsl_extension(GL_EXT_shader_explicit_arithmetic_types_int64) + __glsl_extension(GL_EXT_buffer_reference) + [__NoSideEffect] + [ForceInline] + bool isValid() + { + __target_switch + { + case glsl: + __intrinsic_asm "(uint64_t($0) != 0)"; + case spirv: + uint64_t zero = 0ULL; + return spirv_asm { + %ptrval:$$uint64_t = OpConvertPtrToU $this; + result:$$bool = OpINotEqual %ptrval $zero; + }; + } + } } diff --git a/source/slang/slang-emit-spirv.cpp b/source/slang/slang-emit-spirv.cpp index d071328c4..5939754e0 100644 --- a/source/slang/slang-emit-spirv.cpp +++ b/source/slang/slang-emit-spirv.cpp @@ -429,6 +429,8 @@ struct SPIRVEmitContext /// The next destination `<id>` to allocate. SpvWord m_nextID = 1; + SpvAddressingModel m_addressingMode = SpvAddressingModelLogical; + // We will store the logical sections of the SPIR-V module // in a single array so that we can easily look up a // section by its `SpvLogicalSectionID`. @@ -1200,7 +1202,7 @@ struct SPIRVEmitContext emitOpMemoryModel( getSection(SpvLogicalSectionID::MemoryModel), nullptr, - SpvAddressingModelLogical, + m_addressingMode, SpvMemoryModelGLSL450 ); } @@ -1394,6 +1396,14 @@ struct SPIRVEmitContext requireSPIRVCapability(SpvCapabilityShaderInvocationReorderNV); return emitOpTypeHitObject(inst); + case kIROp_HLSLConstBufferPointerType: + ensureExtensionDeclaration(UnownedStringSlice("SPV_KHR_variable_pointers")); + requireSPIRVCapability(SpvCapabilityVariablePointers); + ensureExtensionDeclaration(UnownedStringSlice("SPV_KHR_physical_storage_buffer")); + requireSPIRVCapability(SpvCapabilityPhysicalStorageBufferAddresses); + m_addressingMode = SpvAddressingModelPhysicalStorageBuffer64; + return emitOpTypePointer(inst, SpvStorageClassPhysicalStorageBuffer, inst->getOperand(0)); + case kIROp_FuncType: // > OpTypeFunction // @@ -4908,7 +4918,6 @@ SlangResult emitSPIRVFromIR( } #endif - context.emitFrontMatter(); for (auto inst : irModule->getGlobalInsts()) { if (as<IRDebugSource>(inst)) @@ -4925,6 +4934,8 @@ SlangResult emitSPIRVFromIR( { context.ensureInst(irEntryPoint); } + context.emitFrontMatter(); + context.emitPhysicalLayout(); spirvOut.addRange( diff --git a/tests/hlsl-intrinsic/const-buffer-pointer.slang b/tests/hlsl-intrinsic/const-buffer-pointer.slang index eddfc3be0..ec242fa07 100644 --- a/tests/hlsl-intrinsic/const-buffer-pointer.slang +++ b/tests/hlsl-intrinsic/const-buffer-pointer.slang @@ -1,8 +1,10 @@ //TEST:SIMPLE(filecheck=CHECK):-target glsl -profile glsl_450 -entry main -stage compute //TEST:SIMPLE(filecheck=SPV):-target spirv -profile glsl_450 -entry main -stage compute - +//TEST:SIMPLE(filecheck=SPV):-target spirv -profile glsl_450 -entry main -stage compute -emit-spirv-directly +// SPV: OpMemoryModel PhysicalStorageBuffer64 // SPV: OpEntryPoint GLCompute {{.*}} "main" {{.*}} - +// SPV: OpTypePointer PhysicalStorageBuffer +// SPV: OpINotEqual struct MyStruct { float4 position; @@ -34,6 +36,9 @@ RWStructuredBuffer<uint> outputBuffer; [numthreads(1,1,1)] void main(int3 tid: SV_DispatchThreadID) { - MyStruct s = gGlobals.pStruct.get(); - outputBuffer[tid.x] = uint(s.position.x); + if (gGlobals.pStruct.isValid()) + { + MyStruct s = gGlobals.pStruct.get(); + outputBuffer[tid.x] = uint(s.position.x); + } } |
