diff options
| -rw-r--r-- | source/slang/slang-emit-hlsl.cpp | 4 | ||||
| -rw-r--r-- | source/slang/slang-emit-metal.cpp | 7 | ||||
| -rw-r--r-- | source/slang/slang-type-layout.cpp | 52 | ||||
| -rw-r--r-- | source/slang/slang-type-layout.h | 5 | ||||
| -rw-r--r-- | tests/hlsl/tbuffer.slang | 40 |
5 files changed, 86 insertions, 22 deletions
diff --git a/source/slang/slang-emit-hlsl.cpp b/source/slang/slang-emit-hlsl.cpp index 72b2a08b0..04f5bd643 100644 --- a/source/slang/slang-emit-hlsl.cpp +++ b/source/slang/slang-emit-hlsl.cpp @@ -191,8 +191,10 @@ void HLSLSourceEmitter::_emitHLSLParameterGroupFieldLayoutSemantics(IRVarLayout* void HLSLSourceEmitter::_emitHLSLParameterGroup(IRGlobalParam* varDecl, IRUniformParameterGroupType* type) { + LayoutResourceKind layoutResourceKind = LayoutResourceKind::ConstantBuffer; if (as<IRTextureBufferType>(type)) { + layoutResourceKind = LayoutResourceKind::ShaderResource; m_writer->emit("tbuffer "); } else @@ -218,7 +220,7 @@ void HLSLSourceEmitter::_emitHLSLParameterGroup(IRGlobalParam* varDecl, IRUnifor typeLayout = parameterGroupTypeLayout->getElementVarLayout()->getTypeLayout(); } - _emitHLSLRegisterSemantic(LayoutResourceKind::ConstantBuffer, &containerChain, varDecl); + _emitHLSLRegisterSemantic(layoutResourceKind, &containerChain, varDecl); auto elementType = type->getElementType(); if (shouldForceUnpackConstantBufferElements(type) || hasExplicitConstantBufferOffset(type)) diff --git a/source/slang/slang-emit-metal.cpp b/source/slang/slang-emit-metal.cpp index 07d5b9f6c..0a5506776 100644 --- a/source/slang/slang-emit-metal.cpp +++ b/source/slang/slang-emit-metal.cpp @@ -690,6 +690,13 @@ void MetalSourceEmitter::emitSimpleTypeImpl(IRType* type) _emitHLSLTextureType(texType); return; } + else if (as<IRTextureBufferType>(type)) + { + m_writer->emit("texture_buffer<"); + emitVal(type->getOperand(0), getInfo(EmitOp::General)); + m_writer->emit(">"); + return; + } else if (auto imageType = as<IRGLSLImageType>(type)) { _emitHLSLTextureType(imageType); diff --git a/source/slang/slang-type-layout.cpp b/source/slang/slang-type-layout.cpp index 1aeaceb72..22aa3f304 100644 --- a/source/slang/slang-type-layout.cpp +++ b/source/slang/slang-type-layout.cpp @@ -917,7 +917,7 @@ struct GLSLLayoutRulesFamilyImpl : LayoutRulesFamilyImpl virtual LayoutRulesImpl* getAnyValueRules() override; virtual LayoutRulesImpl* getConstantBufferRules(CompilerOptionSet& compilerOptions) override; virtual LayoutRulesImpl* getPushConstantBufferRules() override; - virtual LayoutRulesImpl* getTextureBufferRules() override; + virtual LayoutRulesImpl* getTextureBufferRules(CompilerOptionSet& compilerOptions) override; virtual LayoutRulesImpl* getVaryingInputRules() override; virtual LayoutRulesImpl* getVaryingOutputRules() override; virtual LayoutRulesImpl* getSpecializationConstantRules() override; @@ -938,7 +938,7 @@ struct HLSLLayoutRulesFamilyImpl : LayoutRulesFamilyImpl virtual LayoutRulesImpl* getAnyValueRules() override; virtual LayoutRulesImpl* getConstantBufferRules(CompilerOptionSet& compilerOptions) override; virtual LayoutRulesImpl* getPushConstantBufferRules() override; - virtual LayoutRulesImpl* getTextureBufferRules() override; + virtual LayoutRulesImpl* getTextureBufferRules(CompilerOptionSet& compilerOptions) override; virtual LayoutRulesImpl* getVaryingInputRules() override; virtual LayoutRulesImpl* getVaryingOutputRules() override; virtual LayoutRulesImpl* getSpecializationConstantRules() override; @@ -959,7 +959,7 @@ struct CPULayoutRulesFamilyImpl : LayoutRulesFamilyImpl virtual LayoutRulesImpl* getAnyValueRules() override; virtual LayoutRulesImpl* getConstantBufferRules(CompilerOptionSet& compilerOptions) override; virtual LayoutRulesImpl* getPushConstantBufferRules() override; - virtual LayoutRulesImpl* getTextureBufferRules() override; + virtual LayoutRulesImpl* getTextureBufferRules(CompilerOptionSet& compilerOptions) override; virtual LayoutRulesImpl* getVaryingInputRules() override; virtual LayoutRulesImpl* getVaryingOutputRules() override; virtual LayoutRulesImpl* getSpecializationConstantRules() override; @@ -979,7 +979,7 @@ struct CUDALayoutRulesFamilyImpl : LayoutRulesFamilyImpl virtual LayoutRulesImpl* getAnyValueRules() override; virtual LayoutRulesImpl* getConstantBufferRules(CompilerOptionSet& compilerOptions) override; virtual LayoutRulesImpl* getPushConstantBufferRules() override; - virtual LayoutRulesImpl* getTextureBufferRules() override; + virtual LayoutRulesImpl* getTextureBufferRules(CompilerOptionSet& compilerOptions) override; virtual LayoutRulesImpl* getVaryingInputRules() override; virtual LayoutRulesImpl* getVaryingOutputRules() override; virtual LayoutRulesImpl* getSpecializationConstantRules() override; @@ -999,7 +999,7 @@ struct MetalLayoutRulesFamilyImpl : LayoutRulesFamilyImpl virtual LayoutRulesImpl* getAnyValueRules() override; virtual LayoutRulesImpl* getConstantBufferRules(CompilerOptionSet& compilerOptions) override; virtual LayoutRulesImpl* getPushConstantBufferRules() override; - virtual LayoutRulesImpl* getTextureBufferRules() override; + virtual LayoutRulesImpl* getTextureBufferRules(CompilerOptionSet& compilerOptions) override; virtual LayoutRulesImpl* getVaryingInputRules() override; virtual LayoutRulesImpl* getVaryingOutputRules() override; virtual LayoutRulesImpl* getSpecializationConstantRules() override; @@ -1255,6 +1255,10 @@ LayoutRulesImpl kHLSLStructuredBufferLayoutRulesImpl_ = { &kHLSLLayoutRulesFamilyImpl, &kHLSLStructuredBufferLayoutRulesImpl, &kHLSLObjectLayoutRulesImpl, }; +LayoutRulesImpl kHLSLTextureBufferLayoutRulesImpl_ = { + &kHLSLLayoutRulesFamilyImpl, &kHLSLConstantBufferLayoutRulesImpl, &kHLSLObjectLayoutRulesImpl, +}; + LayoutRulesImpl kHLSLVaryingInputLayoutRulesImpl_ = { &kHLSLLayoutRulesFamilyImpl, &kHLSLVaryingInputLayoutRulesImpl, &kHLSLObjectLayoutRulesImpl, }; @@ -1306,9 +1310,12 @@ LayoutRulesImpl* GLSLLayoutRulesFamilyImpl::getShaderRecordConstantBufferRules() return &kGLSLShaderRecordLayoutRulesImpl_; } -LayoutRulesImpl* GLSLLayoutRulesFamilyImpl::getTextureBufferRules() +LayoutRulesImpl* GLSLLayoutRulesFamilyImpl::getTextureBufferRules(CompilerOptionSet& compilerOptions) { - return nullptr; + if (compilerOptions.shouldUseScalarLayout()) + return &kScalarLayoutRulesImpl_; + return &kStd430LayoutRulesImpl_; + } LayoutRulesImpl* GLSLLayoutRulesFamilyImpl::getVaryingInputRules() @@ -1389,9 +1396,9 @@ LayoutRulesImpl* HLSLLayoutRulesFamilyImpl::getStructuredBufferRules(CompilerOpt return &kHLSLStructuredBufferLayoutRulesImpl_; } -LayoutRulesImpl* HLSLLayoutRulesFamilyImpl::getTextureBufferRules() +LayoutRulesImpl* HLSLLayoutRulesFamilyImpl::getTextureBufferRules(CompilerOptionSet&) { - return nullptr; + return &kHLSLTextureBufferLayoutRulesImpl_; } LayoutRulesImpl* HLSLLayoutRulesFamilyImpl::getVaryingInputRules() @@ -1446,9 +1453,9 @@ LayoutRulesImpl* CPULayoutRulesFamilyImpl::getPushConstantBufferRules() return &kCPULayoutRulesImpl_; } -LayoutRulesImpl* CPULayoutRulesFamilyImpl::getTextureBufferRules() +LayoutRulesImpl* CPULayoutRulesFamilyImpl::getTextureBufferRules(CompilerOptionSet&) { - return nullptr; + return &kCPULayoutRulesImpl_; } LayoutRulesImpl* CPULayoutRulesFamilyImpl::getVaryingInputRules() @@ -1512,9 +1519,9 @@ LayoutRulesImpl* CUDALayoutRulesFamilyImpl::getPushConstantBufferRules() return &kCUDALayoutRulesImpl_; } -LayoutRulesImpl* CUDALayoutRulesFamilyImpl::getTextureBufferRules() +LayoutRulesImpl* CUDALayoutRulesFamilyImpl::getTextureBufferRules(CompilerOptionSet&) { - return nullptr; + return &kCUDALayoutRulesImpl_; } LayoutRulesImpl* CUDALayoutRulesFamilyImpl::getVaryingInputRules() @@ -1702,9 +1709,9 @@ LayoutRulesImpl* MetalLayoutRulesFamilyImpl::getStructuredBufferRules(CompilerOp return &kMetalStructuredBufferLayoutRulesImpl_; } -LayoutRulesImpl* MetalLayoutRulesFamilyImpl::getTextureBufferRules() +LayoutRulesImpl* MetalLayoutRulesFamilyImpl::getTextureBufferRules(CompilerOptionSet&) { - return nullptr; + return &kMetalConstantBufferLayoutRulesImpl_; } LayoutRulesImpl* MetalLayoutRulesFamilyImpl::getVaryingInputRules() @@ -2628,11 +2635,16 @@ static RefPtr<TypeLayout> _createParameterGroupTypeLayout( if( wantConstantBuffer ) { // If there is any ordinary data, then we'll need to - // allocate a constant buffer regiser/binding into - // the overall layout, to account for it. + // allocate a constant buffer or tbuffer (if we have a tbuffer parameter group type) + // register/binding the overall layout, to account for this. // - auto cbUsage = parameterGroupRules->GetObjectLayout(ShaderParameterKind::ConstantBuffer, context.objectLayoutOptions); - for (auto layoutInfo : cbUsage.layoutInfos) + ShaderParameterKind parameterKind = ShaderParameterKind::ConstantBuffer; + if (as<TextureBufferType>(parameterGroupType)) + { + parameterKind = ShaderParameterKind::TextureUniformBuffer; + } + auto bufferUsage = parameterGroupRules->GetObjectLayout(parameterKind, context.objectLayoutOptions); + for (auto layoutInfo : bufferUsage.layoutInfos) containerTypeLayout->addResourceUsage(layoutInfo.kind, layoutInfo.size); } @@ -3048,7 +3060,7 @@ LayoutRulesImpl* getParameterBufferElementTypeLayoutRules( } else if( as<TextureBufferType>(parameterGroupType) ) { - return rules->getLayoutRulesFamily()->getTextureBufferRules(); + return rules->getLayoutRulesFamily()->getTextureBufferRules(compilerOptions); } else if( as<GLSLInputParameterGroupType>(parameterGroupType) ) { diff --git a/source/slang/slang-type-layout.h b/source/slang/slang-type-layout.h index 6ca756e8c..f29a38435 100644 --- a/source/slang/slang-type-layout.h +++ b/source/slang/slang-type-layout.h @@ -1043,16 +1043,19 @@ struct LayoutRulesImpl UniformLayoutInfo BeginStructLayout() { + SLANG_ASSERT(simpleRules); return simpleRules->BeginStructLayout(); } LayoutSize AddStructField(UniformLayoutInfo* ioStructInfo, UniformLayoutInfo fieldInfo) { + SLANG_ASSERT(simpleRules); return simpleRules->AddStructField(ioStructInfo, fieldInfo); } void EndStructLayout(UniformLayoutInfo* ioStructInfo) { + SLANG_ASSERT(simpleRules); return simpleRules->EndStructLayout(ioStructInfo); } @@ -1080,7 +1083,7 @@ struct LayoutRulesFamilyImpl virtual LayoutRulesImpl* getAnyValueRules() = 0; virtual LayoutRulesImpl* getConstantBufferRules(CompilerOptionSet& compilerOptions) = 0; virtual LayoutRulesImpl* getPushConstantBufferRules() = 0; - virtual LayoutRulesImpl* getTextureBufferRules() = 0; + virtual LayoutRulesImpl* getTextureBufferRules(CompilerOptionSet& compilerOptions) = 0; virtual LayoutRulesImpl* getVaryingInputRules() = 0; virtual LayoutRulesImpl* getVaryingOutputRules() = 0; virtual LayoutRulesImpl* getSpecializationConstantRules()= 0; diff --git a/tests/hlsl/tbuffer.slang b/tests/hlsl/tbuffer.slang new file mode 100644 index 000000000..a1bebf2e1 --- /dev/null +++ b/tests/hlsl/tbuffer.slang @@ -0,0 +1,40 @@ +//TEST:SIMPLE(filecheck=HLSL): -target hlsl -stage compute -entry computeMain +//TEST:SIMPLE(filecheck=GLSL): -target glsl -stage compute -entry computeMain + +// Metal backend requires a new legalization pass to support emitting `TextureBufferType` +//DISABLE_TEST:SIMPLE(filecheck=METAL): -target metal -stage compute -entry computeMain +//DISABLE_TEST:SIMPLE(filecheck=METALLIB): -target metallib -stage compute -entry computeMain + +// SPIRV backend has no support for emitting `TextureBufferType` +//DISABLE_TEST:SIMPLE(filecheck=SPIRV): -target spirv -stage compute -entry computeMain + +tbuffer tbuf : register(t0) +{ + float4 tb_val1; +} + +tbuffer tbuf2 : register(t1) +{ + Texture2D<float4> texture2D; + float4 tb_val2; +} + + +// HLSL-DAG: t0 +// HLSL-DAG: t1 +// HLSL-DAG: t2 + +// GLSL-DAG: binding = 0 +// GLSL-DAG: binding = 1 +// GLSL-DAG: binding = 2 + +// METAL-DAG: [texture(0)] +// METAL-DAG: [texture(1)] +// METAL-DAG: [texture(2)] +// METALLIB: @computeMain + +RWStructuredBuffer<float4> outputBuffer; +[numthreads(1,1,1)] +float4 computeMain() { + outputBuffer[0] = tb_val1 + texture2D[0] + tb_val2; +} |
