diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2018-11-30 17:52:52 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-11-30 17:52:52 -0800 |
| commit | 3d60cc0ca818556b78f38a59eb5521044b8a6b71 (patch) | |
| tree | 469cca95d07b578380a3961a8ffae18b8d9a4cd1 /source | |
| parent | 7f2d2c9a248a9413e236826a4c6473a6fc104714 (diff) | |
Add support for Vulkan raytraicng "shader record" (#735)
The syntax for this is a placeholder for now, since we will probably want to migrate to whatever gets decided on for dxc. To declare that some data should be part of the "shader record" use `layout(shaderRecordNV)` to mirror the GLSL raytracing extension:
```hlsl
layout(shaderRecordNV)
cbuffer MyShaderRecord
{
float4 someColor;
uint someValue;
}
```
The intention (not enforced) is that an application would map `MyShaderRecord` to "root constants" in the "local root signature" when compiling for DXR, while the output code in GLSL will always map to the shader record in Vulkan raytracing:
```glsl
layout(shaderRecordNV)
buffer MyShaderRecord
{
float4 someColor;
uint someValue;
};
```
This change does *not* support declaring a global value of `struct` type with `layout(shaderRecordNV)` (or a `ParameterBlock` with the modifiers, although that would be a nice-to-have feature) and it does *not* support having the contents of the shader record be mutable (even if GLSL/Vulkan allows it). Those can/should be added in future changes.
In terms of implementation, this closely mirrors the way that `layout(push_constant)` buffers were being handled, where the data inside the `ConstantBuffer<X>` (the value of type `X`) gets laid out using ordinary rules (and consuming ordinary `UNIFORM` storage, while the buffer itself is given a different layout resource to reflect that fact that it does not consume a VK `binding` any more, but a different conceptual resource.
Note: an alternative design here (that might actually be preferrable) would be to have both push-constant and shader-record buffers be handled as alternative aliases for `ConstantBuffer` (or maybe `ParameterBlock`) so that you have, e.g.:
```hlsl
PushConstantBuffer<X> myPushConstants;
ShaderRecord<Y> myShaderRecord;
```
This alternative design avoids API-specific decorations on the declarations, and reflects the intent of the programmer very directly, even when they are compiling for a target like D3D that doesn't reflect these choices at the IL level (it could still be exposed through the Slang reflection API).
Diffstat (limited to 'source')
| -rw-r--r-- | source/slang/emit.cpp | 17 | ||||
| -rw-r--r-- | source/slang/modifier-defs.h | 2 | ||||
| -rw-r--r-- | source/slang/parameter-binding.cpp | 7 | ||||
| -rw-r--r-- | source/slang/parser.cpp | 1 | ||||
| -rw-r--r-- | source/slang/type-layout.cpp | 29 | ||||
| -rw-r--r-- | source/slang/type-layout.h | 2 |
6 files changed, 54 insertions, 4 deletions
diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 187ef29bc..fb9968232 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -1753,14 +1753,14 @@ struct EmitVisitor emitHLSLParameterGroupFieldLayoutSemantics(&chain); } - void emitGLSLLayoutQualifier( + bool emitGLSLLayoutQualifier( LayoutResourceKind kind, EmitVarChain* chain) { if(!chain) - return; + return false; if(!chain->varLayout->FindResourceInfo(kind)) - return; + return false; UInt index = getBindingOffset(chain, kind); UInt space = getBindingSpace(chain, kind); @@ -1828,8 +1828,12 @@ struct EmitVisitor case LayoutResourceKind::PushConstantBuffer: Emit("layout(push_constant)\n"); break; + case LayoutResourceKind::ShaderRecord: + Emit("layout(shaderRecordNV)\n"); + break; } + return true; } void emitGLSLLayoutQualifiers( @@ -5821,8 +5825,13 @@ struct EmitVisitor emitGLSLLayoutQualifier(LayoutResourceKind::DescriptorTableSlot, &containerChain); emitGLSLLayoutQualifier(LayoutResourceKind::PushConstantBuffer, &containerChain); + bool isShaderRecord = emitGLSLLayoutQualifier(LayoutResourceKind::ShaderRecord, &containerChain); - if(as<IRGLSLShaderStorageBufferType>(type)) + if( isShaderRecord ) + { + emit("buffer "); + } + else if(as<IRGLSLShaderStorageBufferType>(type)) { emit("layout(std430) buffer "); } diff --git a/source/slang/modifier-defs.h b/source/slang/modifier-defs.h index e8b3f0774..2276f37f8 100644 --- a/source/slang/modifier-defs.h +++ b/source/slang/modifier-defs.h @@ -136,6 +136,8 @@ SIMPLE_SYNTAX_CLASS(GLSLLocalSizeXLayoutModifier, GLSLLocalSizeLayoutModifier SIMPLE_SYNTAX_CLASS(GLSLLocalSizeYLayoutModifier, GLSLLocalSizeLayoutModifier) SIMPLE_SYNTAX_CLASS(GLSLLocalSizeZLayoutModifier, GLSLLocalSizeLayoutModifier) +SIMPLE_SYNTAX_CLASS(ShaderRecordNVLayoutModifier, GLSLParsedLayoutModifier) + // A catch-all for single-keyword modifiers SIMPLE_SYNTAX_CLASS(SimpleModifier, Modifier) diff --git a/source/slang/parameter-binding.cpp b/source/slang/parameter-binding.cpp index 58891a956..622474116 100644 --- a/source/slang/parameter-binding.cpp +++ b/source/slang/parameter-binding.cpp @@ -1184,6 +1184,13 @@ getTypeLayoutForGlobalShaderParameter_HLSL( auto rules = layoutContext.getRulesFamily(); auto type = varDecl->getType(); + if( varDecl->HasModifier<ShaderRecordNVLayoutModifier>() && type->As<ConstantBufferType>() ) + { + return CreateTypeLayout( + layoutContext.with(rules->getShaderRecordConstantBufferRules()), + type); + } + // We want to check for a constant-buffer type with a `push_constant` layout // qualifier before we move on to anything else. if (varDecl->HasModifier<PushConstantAttribute>() && type->As<ConstantBufferType>()) diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp index 9ccd7b962..2e0ca5df9 100644 --- a/source/slang/parser.cpp +++ b/source/slang/parser.cpp @@ -4562,6 +4562,7 @@ namespace Slang CASE(local_size_x, GLSLLocalSizeXLayoutModifier); CASE(local_size_y, GLSLLocalSizeYLayoutModifier); CASE(local_size_z, GLSLLocalSizeZLayoutModifier); + CASE(shaderRecordNV, ShaderRecordNVLayoutModifier); #undef CASE else diff --git a/source/slang/type-layout.cpp b/source/slang/type-layout.cpp index 4e9a05b53..8fc48fe4f 100644 --- a/source/slang/type-layout.cpp +++ b/source/slang/type-layout.cpp @@ -350,6 +350,17 @@ struct GLSLPushConstantBufferObjectLayoutRulesImpl : GLSLObjectLayoutRulesImpl }; GLSLPushConstantBufferObjectLayoutRulesImpl kGLSLPushConstantBufferObjectLayoutRulesImpl_; +struct GLSLShaderRecordConstantBufferObjectLayoutRulesImpl : GLSLObjectLayoutRulesImpl +{ + virtual SimpleLayoutInfo GetObjectLayout(ShaderParameterKind /*kind*/) override + { + // Special-case the layout for a constant-buffer, because we don't + // want it to allocate a descriptor-table slot + return SimpleLayoutInfo(LayoutResourceKind::ShaderRecord, 1); + } +}; +GLSLShaderRecordConstantBufferObjectLayoutRulesImpl kGLSLShaderRecordConstantBufferObjectLayoutRulesImpl_; + struct HLSLObjectLayoutRulesImpl : ObjectLayoutRulesImpl { virtual SimpleLayoutInfo GetObjectLayout(ShaderParameterKind kind) override @@ -439,6 +450,8 @@ struct GLSLLayoutRulesFamilyImpl : LayoutRulesFamilyImpl LayoutRulesImpl* getRayPayloadParameterRules() override; LayoutRulesImpl* getCallablePayloadParameterRules() override; LayoutRulesImpl* getHitAttributesParameterRules() override; + + LayoutRulesImpl* getShaderRecordConstantBufferRules() override; }; struct HLSLLayoutRulesFamilyImpl : LayoutRulesFamilyImpl @@ -455,6 +468,8 @@ struct HLSLLayoutRulesFamilyImpl : LayoutRulesFamilyImpl LayoutRulesImpl* getRayPayloadParameterRules() override; LayoutRulesImpl* getCallablePayloadParameterRules() override; LayoutRulesImpl* getHitAttributesParameterRules() override; + + LayoutRulesImpl* getShaderRecordConstantBufferRules() override; }; GLSLLayoutRulesFamilyImpl kGLSLLayoutRulesFamilyImpl; @@ -475,6 +490,10 @@ LayoutRulesImpl kGLSLPushConstantLayoutRulesImpl_ = { &kGLSLLayoutRulesFamilyImpl, &kStd430LayoutRulesImpl, &kGLSLPushConstantBufferObjectLayoutRulesImpl_, }; +LayoutRulesImpl kGLSLShaderRecordLayoutRulesImpl_ = { + &kGLSLLayoutRulesFamilyImpl, &kStd430LayoutRulesImpl, &kGLSLShaderRecordConstantBufferObjectLayoutRulesImpl_, +}; + LayoutRulesImpl kGLSLVaryingInputLayoutRulesImpl_ = { &kGLSLLayoutRulesFamilyImpl, &kGLSLVaryingInputLayoutRulesImpl, &kGLSLObjectLayoutRulesImpl, }; @@ -547,6 +566,11 @@ LayoutRulesImpl* GLSLLayoutRulesFamilyImpl::getPushConstantBufferRules() return &kGLSLPushConstantLayoutRulesImpl_; } +LayoutRulesImpl* GLSLLayoutRulesFamilyImpl::getShaderRecordConstantBufferRules() +{ + return &kGLSLShaderRecordLayoutRulesImpl_; +} + LayoutRulesImpl* GLSLLayoutRulesFamilyImpl::getTextureBufferRules() { return nullptr; @@ -606,6 +630,11 @@ LayoutRulesImpl* HLSLLayoutRulesFamilyImpl::getPushConstantBufferRules() return &kHLSLConstantBufferLayoutRulesImpl_; } +LayoutRulesImpl* HLSLLayoutRulesFamilyImpl::getShaderRecordConstantBufferRules() +{ + return &kHLSLConstantBufferLayoutRulesImpl_; +} + LayoutRulesImpl* HLSLLayoutRulesFamilyImpl::getTextureBufferRules() { return nullptr; diff --git a/source/slang/type-layout.h b/source/slang/type-layout.h index 32ee41784..fa874cb80 100644 --- a/source/slang/type-layout.h +++ b/source/slang/type-layout.h @@ -738,6 +738,8 @@ struct LayoutRulesFamilyImpl virtual LayoutRulesImpl* getRayPayloadParameterRules() = 0; virtual LayoutRulesImpl* getCallablePayloadParameterRules() = 0; virtual LayoutRulesImpl* getHitAttributesParameterRules()= 0; + + virtual LayoutRulesImpl* getShaderRecordConstantBufferRules() = 0; }; struct TypeLayoutContext |
