Skip to content

Commit

Permalink
Use 'static const' for spec constants in HLSL
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rossy committed Oct 21, 2017
1 parent 129d8b5 commit 1f16f0d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
6 changes: 3 additions & 3 deletions reference/shaders-hlsl/asm/comp/storage-buffer-basic.asm.comp
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion reference/shaders-hlsl/comp/builtins.comp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
16 changes: 8 additions & 8 deletions reference/shaders-hlsl/frag/spec-constant.frag
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
5 changes: 3 additions & 2 deletions spirv_hlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 1f16f0d

Please sign in to comment.