Skip to content

Commit

Permalink
Implement GL_EXT_terminate_invocation (KhronosGroup#2454)
Browse files Browse the repository at this point in the history
* Implement GL_EXT_terminate_invocation.

* terminateInvocation: declare the SPV extension

* Update test results for spirv-tools and bison version bumps

Co-authored-by: John Kessenich <cepheus@frii.com>
  • Loading branch information
critsec and johnkslang authored Nov 9, 2020
1 parent 383eaf3 commit 74e8f05
Show file tree
Hide file tree
Showing 26 changed files with 6,176 additions and 5,799 deletions.
2 changes: 2 additions & 0 deletions SPIRV/GLSL.ext.KHR.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ static const char* const E_SPV_KHR_non_semantic_info = "SPV_KHR_non_s
static const char* const E_SPV_KHR_ray_tracing = "SPV_KHR_ray_tracing";
static const char* const E_SPV_KHR_ray_query = "SPV_KHR_ray_query";
static const char* const E_SPV_KHR_fragment_shading_rate = "SPV_KHR_fragment_shading_rate";
static const char* const E_SPV_KHR_terminate_invocation = "SPV_KHR_terminate_invocation";

#endif // #ifndef GLSLextKHR_H
4 changes: 4 additions & 0 deletions SPIRV/GlslangToSpv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3458,6 +3458,10 @@ bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::T
case glslang::EOpKill:
builder.makeDiscard();
break;
case glslang::EOpTerminateInvocation:
builder.addExtension(spv::E_SPV_KHR_terminate_invocation);
builder.makeTerminateInvocation();
break;
case glslang::EOpBreak:
if (breakForLoop.top())
builder.createLoopExit();
Expand Down
7 changes: 7 additions & 0 deletions SPIRV/SpvBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,13 @@ void Builder::makeDiscard()
createAndSetNoPredecessorBlock("post-discard");
}

// Comments in header
void Builder::makeTerminateInvocation()
{
buildPoint->addInstruction(std::unique_ptr<Instruction>(new Instruction(OpTerminateInvocation)));
createAndSetNoPredecessorBlock("post-terminate-invocation");
}

// Comments in header
Id Builder::createVariable(Decoration precision, StorageClass storageClass, Id type, const char* name, Id initializer)
{
Expand Down
3 changes: 2 additions & 1 deletion SPIRV/SpvBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,9 @@ class Builder {
// Generate all the code needed to finish up a function.
void leaveFunction();

// Create a discard.
// Create a discard or terminate-invocation.
void makeDiscard();
void makeTerminateInvocation();

// Create a global or function local or IO variable.
Id createVariable(Decoration precision, StorageClass, Id type, const char* name = nullptr,
Expand Down
3 changes: 3 additions & 0 deletions SPIRV/doc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,8 @@ const char* OpcodeString(int op)
case 365: return "OpGroupNonUniformQuadBroadcast";
case 366: return "OpGroupNonUniformQuadSwap";

case OpTerminateInvocation: return "OpTerminateInvocation";

case 4421: return "OpSubgroupBallotKHR";
case 4422: return "OpSubgroupFirstInvocationKHR";
case 4428: return "OpSubgroupAllKHR";
Expand Down Expand Up @@ -1504,6 +1506,7 @@ void Parameterize()
InstructionDesc[OpBranchConditional].setResultAndType(false, false);
InstructionDesc[OpSwitch].setResultAndType(false, false);
InstructionDesc[OpKill].setResultAndType(false, false);
InstructionDesc[OpTerminateInvocation].setResultAndType(false, false);
InstructionDesc[OpReturn].setResultAndType(false, false);
InstructionDesc[OpReturnValue].setResultAndType(false, false);
InstructionDesc[OpUnreachable].setResultAndType(false, false);
Expand Down
1 change: 1 addition & 0 deletions SPIRV/spirv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,7 @@ inline void HasResultAndType(Op opcode, bool *hasResult, bool *hasResultType) {
case OpBranchConditional: *hasResult = false; *hasResultType = false; break;
case OpSwitch: *hasResult = false; *hasResultType = false; break;
case OpKill: *hasResult = false; *hasResultType = false; break;
case OpTerminateInvocation: *hasResult = false; *hasResultType = false; break;
case OpReturn: *hasResult = false; *hasResultType = false; break;
case OpReturnValue: *hasResult = false; *hasResultType = false; break;
case OpUnreachable: *hasResult = false; *hasResultType = false; break;
Expand Down
1 change: 1 addition & 0 deletions SPIRV/spvIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ class Block {
case OpBranchConditional:
case OpSwitch:
case OpKill:
case OpTerminateInvocation:
case OpReturn:
case OpReturnValue:
case OpUnreachable:
Expand Down
2 changes: 1 addition & 1 deletion Test/baseResults/cppDeepNest.frag.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cppDeepNest.frag
ERROR: 0:66: '#if/#ifdef/#ifndef' : maximum nesting depth exceeded
ERROR: 0:66: '' : missing #endif
ERROR: 0:66: '' : syntax error, unexpected $end
ERROR: 0:66: '' : syntax error, unexpected end of file
ERROR: 3 compilation errors. No code generated.


Expand Down
2 changes: 1 addition & 1 deletion Test/baseResults/numeral.frag.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ERROR: 0:88: '' : float literal needs a decimal point or exponent
ERROR: 0:98: '' : numeric literal too big
ERROR: 0:101: '' : numeric literal too big
ERROR: 0:104: '#' : preprocessor directive cannot be preceded by another token
ERROR: 0:104: '' : syntax error, unexpected $end, expecting COMMA or SEMICOLON
ERROR: 0:104: '' : syntax error, unexpected end of file, expecting COMMA or SEMICOLON
ERROR: 14 compilation errors. No code generated.


Expand Down
20 changes: 20 additions & 0 deletions Test/baseResults/spv.terminate.frag.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
spv.terminate.frag
// Module Version 10000
// Generated by (magic number): 8000a
// Id's are bound by 7

Capability Shader
Extension "SPV_KHR_terminate_invocation"
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint Fragment 4 "main"
ExecutionMode 4 OriginUpperLeft
Source GLSL 400
SourceExtension "GL_EXT_terminate_invocation"
Name 4 "main"
2: TypeVoid
3: TypeFunction 2
4(main): 2 Function None 3
5: Label
TerminateInvocation
FunctionEnd
24 changes: 24 additions & 0 deletions Test/baseResults/terminate.frag.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
terminate.frag
ERROR: 0:3: 'terminateInvocation' : undeclared identifier
ERROR: 0:9: '' : syntax error, unexpected TERMINATE_INVOCATION, expecting COMMA or SEMICOLON
ERROR: 2 compilation errors. No code generated.


Shader version: 400
Requested GL_EXT_terminate_invocation
ERROR: node is still EOpNull!
0:3 Function Definition: foo( ( global void)
0:3 Function Parameters:
0:3 Sequence
0:3 'terminateInvocation' ( temp float)
0:? Linker Objects


Linked fragment stage:


Shader version: 400
Requested GL_EXT_terminate_invocation
ERROR: node is still EOpNull!
0:? Linker Objects

36 changes: 36 additions & 0 deletions Test/baseResults/terminate.vert.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
terminate.vert
ERROR: 0:3: 'terminateInvocation' : undeclared identifier
ERROR: 0:9: 'terminateInvocation' : not supported in this stage: vertex
ERROR: 2 compilation errors. No code generated.


Shader version: 400
Requested GL_EXT_terminate_invocation
ERROR: node is still EOpNull!
0:3 Function Definition: foo( ( global void)
0:3 Function Parameters:
0:3 Sequence
0:3 'terminateInvocation' ( temp float)
0:7 Function Definition: main( ( global void)
0:7 Function Parameters:
0:9 Sequence
0:9 Branch: TerminateInvocation
0:? Linker Objects
0:? 'gl_VertexID' ( gl_VertexId int VertexId)
0:? 'gl_InstanceID' ( gl_InstanceId int InstanceId)


Linked vertex stage:


Shader version: 400
Requested GL_EXT_terminate_invocation
ERROR: node is still EOpNull!
0:7 Function Definition: main( ( global void)
0:7 Function Parameters:
0:9 Sequence
0:9 Branch: TerminateInvocation
0:? Linker Objects
0:? 'gl_VertexID' ( gl_VertexId int VertexId)
0:? 'gl_InstanceID' ( gl_InstanceId int InstanceId)

8 changes: 8 additions & 0 deletions Test/spv.terminate.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#version 400

#extension GL_EXT_terminate_invocation : enable

void main()
{
terminateInvocation;
}
10 changes: 10 additions & 0 deletions Test/terminate.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#version 400

void foo() { terminateInvocation; } // ERROR: identifier undeclared

#extension GL_EXT_terminate_invocation : enable

void main()
{
int terminateInvocation; // syntax ERROR
}
10 changes: 10 additions & 0 deletions Test/terminate.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#version 400

void foo() { terminateInvocation; } // ERROR: identifier undeclared

#extension GL_EXT_terminate_invocation : enable

void main()
{
terminateInvocation; // ERROR: wrong stage
}
5 changes: 3 additions & 2 deletions glslang/Include/intermediate.h
Original file line number Diff line number Diff line change
Expand Up @@ -628,13 +628,14 @@ enum TOperator {
// Branch
//

EOpKill, // Fragment only
EOpKill, // Fragment only
EOpTerminateInvocation, // Fragment only
EOpDemote, // Fragment only
EOpReturn,
EOpBreak,
EOpContinue,
EOpCase,
EOpDefault,
EOpDemote, // Fragment only

//
// Constructors
Expand Down
6 changes: 6 additions & 0 deletions glslang/MachineIndependent/Scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ void TScanContext::fillInKeywordMap()
(*KeywordMap)["if"] = IF;
(*KeywordMap)["else"] = ELSE;
(*KeywordMap)["discard"] = DISCARD;
(*KeywordMap)["terminateInvocation"] = TERMINATE_INVOCATION;
(*KeywordMap)["return"] = RETURN;
(*KeywordMap)["void"] = VOID;
(*KeywordMap)["bool"] = BOOL;
Expand Down Expand Up @@ -936,6 +937,11 @@ int TScanContext::tokenizeIdentifier()
case CASE:
return keyword;

case TERMINATE_INVOCATION:
if (!parseContext.extensionTurnedOn(E_GL_EXT_terminate_invocation))
return identifierOrType();
return keyword;

case BUFFER:
afterBuffer = true;
if ((parseContext.isEsProfile() && parseContext.version < 310) ||
Expand Down
8 changes: 7 additions & 1 deletion glslang/MachineIndependent/Versions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ void TParseVersions::initializeExtensionBehavior()
extensionBehavior[E_GL_EXT_shader_implicit_conversions] = EBhDisable;
extensionBehavior[E_GL_EXT_fragment_shading_rate] = EBhDisable;
extensionBehavior[E_GL_EXT_shader_image_int64] = EBhDisable;
extensionBehavior[E_GL_EXT_terminate_invocation] = EBhDisable;

// OVR extensions
extensionBehavior[E_GL_OVR_multiview] = EBhDisable;
Expand Down Expand Up @@ -411,7 +412,7 @@ void TParseVersions::getPreamble(std::string& preamble)
preamble += "#define GL_NV_shader_noperspective_interpolation 1\n";
}

} else {
} else { // !isEsProfile()
preamble =
"#define GL_FRAGMENT_PRECISION_HIGH 1\n"
"#define GL_ARB_texture_rectangle 1\n"
Expand Down Expand Up @@ -563,6 +564,11 @@ void TParseVersions::getPreamble(std::string& preamble)
"#define GL_GOOGLE_include_directive 1\n"
"#define GL_KHR_blend_equation_advanced 1\n"
;

// other general extensions
preamble +=
"#define GL_EXT_terminate_invocation 1\n"
;
#endif

// #define VULKAN XXXX
Expand Down
1 change: 1 addition & 0 deletions glslang/MachineIndependent/Versions.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ const char* const E_GL_EXT_shader_subgroup_extended_types_int8 = "GL_EXT_shad
const char* const E_GL_EXT_shader_subgroup_extended_types_int16 = "GL_EXT_shader_subgroup_extended_types_int16";
const char* const E_GL_EXT_shader_subgroup_extended_types_int64 = "GL_EXT_shader_subgroup_extended_types_int64";
const char* const E_GL_EXT_shader_subgroup_extended_types_float16 = "GL_EXT_shader_subgroup_extended_types_float16";
const char* const E_GL_EXT_terminate_invocation = "GL_EXT_terminate_invocation";

const char* const E_GL_EXT_shader_atomic_float = "GL_EXT_shader_atomic_float";

Expand Down
5 changes: 5 additions & 0 deletions glslang/MachineIndependent/glslang.m4
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ GLSLANG_WEB_EXCLUDE_OFF
%token <lex> CENTROID IN OUT INOUT
%token <lex> STRUCT VOID WHILE
%token <lex> BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
%token <lex> TERMINATE_INVOCATION
%token <lex> UNIFORM SHARED BUFFER
%token <lex> FLAT SMOOTH LAYOUT

Expand Down Expand Up @@ -3927,6 +3928,10 @@ jump_statement
parseContext.requireStage($1.loc, EShLangFragment, "discard");
$$ = parseContext.intermediate.addBranch(EOpKill, $1.loc);
}
| TERMINATE_INVOCATION SEMICOLON {
parseContext.requireStage($1.loc, EShLangFragment, "terminateInvocation");
$$ = parseContext.intermediate.addBranch(EOpTerminateInvocation, $1.loc);
}
;

// Grammar Note: No 'goto'. Gotos are not supported.
Expand Down
5 changes: 5 additions & 0 deletions glslang/MachineIndependent/glslang.y
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ extern int yylex(YYSTYPE*, TParseContext&);
%token <lex> CENTROID IN OUT INOUT
%token <lex> STRUCT VOID WHILE
%token <lex> BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
%token <lex> TERMINATE_INVOCATION
%token <lex> UNIFORM SHARED BUFFER
%token <lex> FLAT SMOOTH LAYOUT

Expand Down Expand Up @@ -3927,6 +3928,10 @@ jump_statement
parseContext.requireStage($1.loc, EShLangFragment, "discard");
$$ = parseContext.intermediate.addBranch(EOpKill, $1.loc);
}
| TERMINATE_INVOCATION SEMICOLON {
parseContext.requireStage($1.loc, EShLangFragment, "terminateInvocation");
$$ = parseContext.intermediate.addBranch(EOpTerminateInvocation, $1.loc);
}
;

// Grammar Note: No 'goto'. Gotos are not supported.
Expand Down
Loading

0 comments on commit 74e8f05

Please sign in to comment.