Skip to content

Commit

Permalink
spirv: Support initializers on uniforms
Browse files Browse the repository at this point in the history
If a uniform has an initializer it will now be given as the optional
initializer operand to the OpVariable instruction.

Fixes: KhronosGroup#1259

Signed-off-by: Neil Roberts <nroberts@igalia.com> (the code)
Signed-off-by: Alejandro Piñeiro <apinheiro@igalia.com> (the tests)
Signed-off-by: Arcady Goldmints-Orlov <agoldmints@igalia.com>
  • Loading branch information
bpeel authored and Arcady Goldmints-Orlov committed Apr 24, 2020
1 parent 02c70ad commit e11a52e
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
13 changes: 12 additions & 1 deletion SPIRV/GlslangToSpv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3524,7 +3524,18 @@ spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol*
if (glslang::IsAnonymous(name))
name = "";

return builder.createVariable(storageClass, spvType, name);
spv::Id initializer = spv::NoResult;

if (node->getType().getQualifier().storage == glslang::EvqUniform &&
!node->getConstArray().empty()) {
int nextConst = 0;
initializer = createSpvConstantFromConstUnionArray(node->getType(),
node->getConstArray(),
nextConst,
false /* specConst */);
}

return builder.createVariable(storageClass, spvType, name, initializer);
}

// Return type Id of the sampled type.
Expand Down
33 changes: 33 additions & 0 deletions Test/baseResults/spv.uniformInitializer.frag.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
spv.uniformInitializer.frag
// Module Version 10000
// Generated by (magic number): 80008
// Id's are bound by 16

Capability Shader
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint Fragment 4 "main" 9
ExecutionMode 4 OriginLowerLeft
Source GLSL 450
Name 4 "main"
Name 9 "color"
Name 14 "in_color"
Decorate 9(color) Location 0
Decorate 14(in_color) Location 0
2: TypeVoid
3: TypeFunction 2
6: TypeFloat 32
7: TypeVector 6(float) 4
8: TypePointer Output 7(fvec4)
9(color): 8(ptr) Variable Output
10: 6(float) Constant 0
11: 6(float) Constant 1065353216
12: 7(fvec4) ConstantComposite 10 11 10 11
13: TypePointer UniformConstant 7(fvec4)
14(in_color): 13(ptr) Variable UniformConstant 12
4(main): 2 Function None 3
5: Label
15: 7(fvec4) Load 14(in_color)
Store 9(color) 15
Return
FunctionEnd
10 changes: 10 additions & 0 deletions Test/spv.uniformInitializer.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#version 450

layout (location = 0) out vec4 color;

layout (location = 0) uniform vec4 in_color = vec4(0.0, 1.0, 0.0, 1.0);

void main()
{
color = in_color;
}
14 changes: 14 additions & 0 deletions Test/spv.uniformInitializerSpecConstant.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 450

layout (location = 0) out vec4 color;

layout (constant_id = 1) const float opacity = 0.5;

layout (location = 0) uniform vec3 in_color = vec3(1.0, 0.5, 0);

layout (location = 4) uniform float foo = opacity;

void main()
{
color = vec4(in_color, foo);
}
19 changes: 19 additions & 0 deletions Test/spv.uniformInitializerStruct.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 450

layout (location = 0) out vec4 color;

layout (location = 0) uniform struct {
float r;
float g;
float b;
} parts[2] = { { 1.0, 1.0, 1.0}, { 0.0, 1.0, 0.0 } };

void main() {
color = vec4(0.0, 0.0, 0.0, 1.0);

for (int i = 0; i < 2; i++) {
color.r += parts[i].r;
color.g += parts[i].g;
color.b += parts[i].b;
}
}
3 changes: 3 additions & 0 deletions gtests/Spv.FromFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,9 @@ INSTANTIATE_TEST_CASE_P(
"spv.specConst.vert",
"spv.specTexture.frag",
"spv.OVR_multiview.vert",
"spv.uniformInitializer.frag",
"spv.uniformInitializerSpecConstant.frag",
"spv.uniformInitializerStruct.frag",
"spv.xfbOffsetOnBlockMembersAssignment.vert",
"spv.xfbOffsetOnStructMembersAssignment.vert",
"spv.xfbOverlapOffsetCheckWithBlockAndMember.vert",
Expand Down

0 comments on commit e11a52e

Please sign in to comment.