Use 'static const' for spec constants in HLSL

If 'const' is used, the shader expects the variable to be backed by a
constant buffer. 'static const' is probably preferred for a value that
is initialized with a constant in the HLSL source code.

FXC also emits a warning for 'const' variables with initializers, since
'static const' was probably intended.
This commit is contained in:
James Ross-Gowan 2017-10-21 19:13:06 +11:00
parent 129d8b534f
commit 1f16f0d260
5 changed files with 18 additions and 17 deletions

View File

@ -1,6 +1,6 @@
const uint _5 = 9u;
const uint _6 = 4u;
const uint3 gl_WorkGroupSize = uint3(_5, 20u, _6);
static const uint _5 = 9u;
static const uint _6 = 4u;
static const uint3 gl_WorkGroupSize = uint3(_5, 20u, _6);
RWByteAddressBuffer _4 : register(u0);

View File

@ -1,6 +1,6 @@
const uint _3 = 1u;
const uint _4 = 3u;
const uint3 gl_WorkGroupSize = uint3(_3, 2u, _4);
static const uint _3 = 1u;
static const uint _4 = 3u;
static const uint3 gl_WorkGroupSize = uint3(_3, 2u, _4);
RWByteAddressBuffer _8 : register(u0);
RWByteAddressBuffer _9 : register(u1);

View File

@ -1,4 +1,4 @@
const uint3 gl_WorkGroupSize = uint3(8u, 4u, 2u);
static const uint3 gl_WorkGroupSize = uint3(8u, 4u, 2u);
static uint3 gl_WorkGroupID;
static uint3 gl_LocalInvocationID;

View File

@ -1,11 +1,11 @@
const float a = 1.0f;
const float b = 2.0f;
const int c = 3;
const int d = 4;
const uint e = 5u;
const uint f = 6u;
const bool g = false;
const bool h = true;
static const float a = 1.0f;
static const float b = 2.0f;
static const int c = 3;
static const int d = 4;
static const uint e = 5u;
static const uint f = 6u;
static const bool g = false;
static const bool h = true;
struct Foo
{

View File

@ -644,14 +644,15 @@ void CompilerHLSL::emit_specialization_constants()
auto &type = get<SPIRType>(c.constant_type);
auto name = to_name(c.self);
statement("const ", variable_decl(type, name), " = ", constant_expression(c), ";");
statement("static const ", variable_decl(type, name), " = ", constant_expression(c), ";");
emitted = true;
}
}
if (workgroup_size_id)
{
statement("const uint3 gl_WorkGroupSize = ", constant_expression(get<SPIRConstant>(workgroup_size_id)), ";");
statement("static const uint3 gl_WorkGroupSize = ",
constant_expression(get<SPIRConstant>(workgroup_size_id)), ";");
emitted = true;
}