diff options
| -rw-r--r-- | source/slang/diagnostic-defs.h | 2 | ||||
| -rw-r--r-- | source/slang/parameter-binding.cpp | 27 | ||||
| -rw-r--r-- | tests/compute/global-type-param1.slang | 7 | ||||
| -rw-r--r-- | tests/compute/global-type-param2.slang | 2 | ||||
| -rw-r--r-- | tests/diagnostics/global-uniform.slang | 13 | ||||
| -rw-r--r-- | tests/diagnostics/global-uniform.slang.expected | 8 | ||||
| -rw-r--r-- | tests/reflection/global-type-params.slang | 4 | ||||
| -rw-r--r-- | tests/reflection/global-type-params.slang.expected | 52 | ||||
| -rw-r--r-- | tests/reflection/global-uniforms.hlsl | 1 |
9 files changed, 84 insertions, 32 deletions
diff --git a/source/slang/diagnostic-defs.h b/source/slang/diagnostic-defs.h index 25e17f0c4..96c1366cd 100644 --- a/source/slang/diagnostic-defs.h +++ b/source/slang/diagnostic-defs.h @@ -375,6 +375,8 @@ DIAGNOSTIC(39015, Error, wholeSpaceParameterRequiresZeroBinding, "shader paramet DIAGNOSTIC(39013, Error, dontExpectOutParametersForStage, "the '$0' stage does not support `out` or `inout` entry point parameters") DIAGNOSTIC(39014, Error, dontExpectInParametersForStage, "the '$0' stage does not support `in` entry point parameters") +DIAGNOSTIC(39016, Error, globalUniformsNotSupported, "'$0' is implicitly a global uniform shader parameter, which is currently unsupported by Slang. If a uniform parameter is intended, use a constant buffer or parameter block. If a global is intended, use the 'static' modifier.") + // // 4xxxx - IL code generation. // diff --git a/source/slang/parameter-binding.cpp b/source/slang/parameter-binding.cpp index e48b1d5bd..b8df39ade 100644 --- a/source/slang/parameter-binding.cpp +++ b/source/slang/parameter-binding.cpp @@ -2622,6 +2622,29 @@ static void collectParameters( } } + /// Emit a diagnostic about a uniform parameter at global scope. +void diagnoseGlobalUniform( + SharedParameterBindingContext* sharedContext, + VarDeclBase* varDecl) +{ + // It is entirely possible for Slang to support uniform parameters at the global scope, + // by bundling them into an implicit constant buffer, and indeed the layout algorithm + // implemented in this file computes a layout *as if* the Slang compiler does just that. + // + // The missing link is the downstream IR and code generation steps, where we would need + // to collect all of the global-scope uniforms into a common `struct` type and then + // create a new constant buffer parameter over that type. + // + // For now it is easier to simply ban this case, since most shader authors have + // switched to modern HLSL/GLSL style with `cbuffer` or `uniform` block declarations. + // + // TODO: In the long run it may be best to require *all* global-scope shader parameters + // to be marked with a keyword (e.g., `uniform`) so that ordinary global variable syntax can be + // used safely. + // + getSink(sharedContext)->diagnose(varDecl, Diagnostics::globalUniformsNotSupported, varDecl->getName()); +} + void generateParameterBindings( TargetRequest* targetReq) { @@ -2678,7 +2701,7 @@ void generateParameterBindings( if( firstVarLayout->typeLayout->FindResourceInfo(LayoutResourceKind::Uniform) ) { needDefaultConstantBuffer = true; - break; + diagnoseGlobalUniform(&sharedContext, firstVarLayout->varDecl); } } @@ -3016,6 +3039,8 @@ RefPtr<ProgramLayout> specializeProgramLayout( fieldInfo); newVarLayout->findOrAddResourceInfo(LayoutResourceKind::Uniform)->index = uniformOffset.getFiniteValue(); anyUniforms = true; + + diagnoseGlobalUniform(&sharedContext, varLayout->varDecl); } structLayout->fields[varId] = newVarLayout; varLayoutMapping[varLayout] = newVarLayout; diff --git a/tests/compute/global-type-param1.slang b/tests/compute/global-type-param1.slang index 08e548b81..e16ffa9da 100644 --- a/tests/compute/global-type-param1.slang +++ b/tests/compute/global-type-param1.slang @@ -28,9 +28,12 @@ struct Impl : IBase __generic_param TImpl : IBase; -TImpl impl; +ParameterBlock<TImpl> impl; -float base0; // = 0.5 +cbuffer C +{ + float base0; // = 0.5 +} Texture2D tex1; // = 0.0 SamplerState sampler; diff --git a/tests/compute/global-type-param2.slang b/tests/compute/global-type-param2.slang index 51b586cf7..f29d01407 100644 --- a/tests/compute/global-type-param2.slang +++ b/tests/compute/global-type-param2.slang @@ -40,7 +40,7 @@ struct Impl : IBase __generic_param TImpl : IBase; -TImpl impl; +ParameterBlock<TImpl> impl; // at binding c0: cbuffer existingBuffer diff --git a/tests/diagnostics/global-uniform.slang b/tests/diagnostics/global-uniform.slang new file mode 100644 index 000000000..a3f17e536 --- /dev/null +++ b/tests/diagnostics/global-uniform.slang @@ -0,0 +1,13 @@ +// global-uniform.slang +//TEST:SIMPLE:-target hlsl + + +// Any attempt to declare a global variable that actually declares a +// global uniform should be diagnosed as unsupported. + +uniform float a; + +const uint4 b = uint4(0,1,2,3); + +struct C { float x; int y; }; +C c; diff --git a/tests/diagnostics/global-uniform.slang.expected b/tests/diagnostics/global-uniform.slang.expected new file mode 100644 index 000000000..a77144c4f --- /dev/null +++ b/tests/diagnostics/global-uniform.slang.expected @@ -0,0 +1,8 @@ +result code = -1 +standard error = { +tests/diagnostics/global-uniform.slang(8): error 39016: 'a' is implicitly a global uniform shader parameter, which is currently unsupported by Slang. If a uniform parameter is intended, use a constant buffer or parameter block. If a global is intended, use the 'static' modifier. +tests/diagnostics/global-uniform.slang(10): error 39016: 'b' is implicitly a global uniform shader parameter, which is currently unsupported by Slang. If a uniform parameter is intended, use a constant buffer or parameter block. If a global is intended, use the 'static' modifier. +tests/diagnostics/global-uniform.slang(13): error 39016: 'c' is implicitly a global uniform shader parameter, which is currently unsupported by Slang. If a uniform parameter is intended, use a constant buffer or parameter block. If a global is intended, use the 'static' modifier. +} +standard output = { +} diff --git a/tests/reflection/global-type-params.slang b/tests/reflection/global-type-params.slang index bfeb7fb2e..74961b7cc 100644 --- a/tests/reflection/global-type-params.slang +++ b/tests/reflection/global-type-params.slang @@ -3,7 +3,6 @@ // Confirm that we handle global generic parameters -float4 u; interface IBase {}; @@ -24,10 +23,11 @@ SamplerState s; cbuffer CB { + float4 u; float4 v; + float4 w; } -float4 w; float4 main() : SV_Target { diff --git a/tests/reflection/global-type-params.slang.expected b/tests/reflection/global-type-params.slang.expected index 308738b55..8f6ba2838 100644 --- a/tests/reflection/global-type-params.slang.expected +++ b/tests/reflection/global-type-params.slang.expected @@ -5,18 +5,6 @@ standard output = { { "parameters": [ { - "name": "u", - "binding": {"kind": "uniform", "offset": 0, "size": 16}, - "type": { - "kind": "vector", - "elementCount": 4, - "elementType": { - "kind": "scalar", - "scalarType": "float32" - } - } - }, - { "name": "arg", "binding": {"kind": "generic", "index": 0}, "type": { @@ -65,14 +53,14 @@ standard output = { }, { "name": "CB", - "binding": {"kind": "constantBuffer", "index": 1}, + "binding": {"kind": "constantBuffer", "index": 0}, "type": { "kind": "constantBuffer", "elementType": { "kind": "struct", "fields": [ { - "name": "v", + "name": "u", "type": { "kind": "vector", "elementCount": 4, @@ -82,22 +70,34 @@ standard output = { } }, "binding": {"kind": "uniform", "offset": 0, "size": 16} + }, + { + "name": "v", + "type": { + "kind": "vector", + "elementCount": 4, + "elementType": { + "kind": "scalar", + "scalarType": "float32" + } + }, + "binding": {"kind": "uniform", "offset": 16, "size": 16} + }, + { + "name": "w", + "type": { + "kind": "vector", + "elementCount": 4, + "elementType": { + "kind": "scalar", + "scalarType": "float32" + } + }, + "binding": {"kind": "uniform", "offset": 32, "size": 16} } ] } } - }, - { - "name": "w", - "binding": {"kind": "uniform", "offset": 16, "size": 16}, - "type": { - "kind": "vector", - "elementCount": 4, - "elementType": { - "kind": "scalar", - "scalarType": "float32" - } - } } ], "entryPoints": [ diff --git a/tests/reflection/global-uniforms.hlsl b/tests/reflection/global-uniforms.hlsl index 884042cfa..6d4992db2 100644 --- a/tests/reflection/global-uniforms.hlsl +++ b/tests/reflection/global-uniforms.hlsl @@ -1,3 +1,4 @@ +//TEST_IGNORE_FILE //TEST:REFLECTION:-profile ps_4_0 -target hlsl // Confirm that we handle uniforms at global scope |
