From a7785ea1ff5b10bfc2d8ca77fdad5929562897b7 Mon Sep 17 00:00:00 2001 From: jimihem <149994911+jimihem@users.noreply.github.com> Date: Wed, 13 Dec 2023 07:22:04 +0800 Subject: [PATCH] Support GL_EXT_draw_instanced extension. --- Test/GL_EXT_draw_instanced.vert | 18 ++ Test/baseResults/130.vert.out | 11 +- .../GL_EXT_draw_instanced.vert.out | 173 ++++++++++++++++++ glslang/MachineIndependent/Initialize.cpp | 5 +- glslang/MachineIndependent/Intermediate.cpp | 5 +- glslang/MachineIndependent/Versions.cpp | 1 + glslang/MachineIndependent/Versions.h | 1 + gtests/AST.FromFile.cpp | 1 + 8 files changed, 207 insertions(+), 8 deletions(-) create mode 100644 Test/GL_EXT_draw_instanced.vert create mode 100644 Test/baseResults/GL_EXT_draw_instanced.vert.out diff --git a/Test/GL_EXT_draw_instanced.vert b/Test/GL_EXT_draw_instanced.vert new file mode 100644 index 00000000..601e3d7f --- /dev/null +++ b/Test/GL_EXT_draw_instanced.vert @@ -0,0 +1,18 @@ +#version 120 +#extension GL_EXT_draw_instanced : require +#define ID gl_InstanceID + +uniform mat4 gtf_ModelViewProjectionMatrix; +uniform vec3 instanceOffsets[3]; +uniform vec4 va[gl_MaxVertexAttribs]; +vec4 color; + +void main (void) +{ + vec4 vertex = vec4(va[0].xy / 3.0, va[0].zw) + vec4(instanceOffsets[ID], 1.0); + color = vec4(0, 0, 0, 0); + for (int i = 1; i < gl_MaxVertexAttribs; i++) + color += va[i]; + gl_Position = gtf_ModelViewProjectionMatrix * vertex; + gl_PointSize = 1.0; +} \ No newline at end of file diff --git a/Test/baseResults/130.vert.out b/Test/baseResults/130.vert.out index e38043c6..ee7fddef 100644 --- a/Test/baseResults/130.vert.out +++ b/Test/baseResults/130.vert.out @@ -1,9 +1,8 @@ 130.vert -ERROR: 0:59: 'gl_InstanceID' : undeclared identifier -ERROR: 0:59: '=' : cannot convert from ' temp float' to ' temp int' +ERROR: 0:59: 'gl_InstanceID' : required extension not requested: GL_EXT_draw_instanced ERROR: 0:61: 'texelFetch' : no matching overloaded function found ERROR: 0:61: 'assign' : cannot convert from ' const float' to ' temp int' -ERROR: 4 compilation errors. No code generated. +ERROR: 3 compilation errors. No code generated. Shader version: 130 @@ -120,7 +119,11 @@ ERROR: node is still EOpNull! 0:46 0.300000 0:57 Function Definition: foo88( ( global void) 0:57 Function Parameters: -0:? Sequence +0:59 Sequence +0:59 Sequence +0:59 move second child to first child ( temp int) +0:59 'id' ( temp int) +0:59 'gl_InstanceID' ( gl_InstanceId int InstanceId) 0:61 'id' ( temp int) 0:63 'gl_ClipVertex' ( gl_ClipVertex 4-component vector of float ClipVertex) 0:64 'gl_Color' ( in 4-component vector of float Color) diff --git a/Test/baseResults/GL_EXT_draw_instanced.vert.out b/Test/baseResults/GL_EXT_draw_instanced.vert.out new file mode 100644 index 00000000..cce01a17 --- /dev/null +++ b/Test/baseResults/GL_EXT_draw_instanced.vert.out @@ -0,0 +1,173 @@ +GL_EXT_draw_instanced.vert +Shader version: 120 +Requested GL_EXT_draw_instanced +0:? Sequence +0:10 Function Definition: main( ( global void) +0:10 Function Parameters: +0:12 Sequence +0:12 Sequence +0:12 move second child to first child ( temp 4-component vector of float) +0:12 'vertex' ( temp 4-component vector of float) +0:12 add ( temp 4-component vector of float) +0:12 Construct vec4 ( temp 4-component vector of float) +0:12 divide ( temp 2-component vector of float) +0:12 vector swizzle ( temp 2-component vector of float) +0:12 direct index ( temp 4-component vector of float) +0:12 'va' ( uniform 64-element array of 4-component vector of float) +0:12 Constant: +0:12 0 (const int) +0:12 Sequence +0:12 Constant: +0:12 0 (const int) +0:12 Constant: +0:12 1 (const int) +0:12 Constant: +0:12 3.000000 +0:12 vector swizzle ( temp 2-component vector of float) +0:12 direct index ( temp 4-component vector of float) +0:12 'va' ( uniform 64-element array of 4-component vector of float) +0:12 Constant: +0:12 0 (const int) +0:12 Sequence +0:12 Constant: +0:12 2 (const int) +0:12 Constant: +0:12 3 (const int) +0:12 Construct vec4 ( temp 4-component vector of float) +0:12 indirect index ( temp 3-component vector of float) +0:12 'instanceOffsets' ( uniform 3-element array of 3-component vector of float) +0:12 'gl_InstanceID' ( gl_InstanceId int InstanceId) +0:12 Constant: +0:12 1.000000 +0:13 move second child to first child ( temp 4-component vector of float) +0:13 'color' ( global 4-component vector of float) +0:13 Constant: +0:13 0.000000 +0:13 0.000000 +0:13 0.000000 +0:13 0.000000 +0:14 Sequence +0:14 Sequence +0:14 move second child to first child ( temp int) +0:14 'i' ( temp int) +0:14 Constant: +0:14 1 (const int) +0:14 Loop with condition tested first +0:14 Loop Condition +0:14 Compare Less Than ( temp bool) +0:14 'i' ( temp int) +0:14 Constant: +0:14 64 (const int) +0:14 Loop Body +0:15 add second child into first child ( temp 4-component vector of float) +0:15 'color' ( global 4-component vector of float) +0:15 indirect index ( temp 4-component vector of float) +0:15 'va' ( uniform 64-element array of 4-component vector of float) +0:15 'i' ( temp int) +0:14 Loop Terminal Expression +0:14 Post-Increment ( temp int) +0:14 'i' ( temp int) +0:16 move second child to first child ( temp 4-component vector of float) +0:16 'gl_Position' ( gl_Position 4-component vector of float Position) +0:16 matrix-times-vector ( temp 4-component vector of float) +0:16 'gtf_ModelViewProjectionMatrix' ( uniform 4X4 matrix of float) +0:16 'vertex' ( temp 4-component vector of float) +0:17 move second child to first child ( temp float) +0:17 'gl_PointSize' ( gl_PointSize float PointSize) +0:17 Constant: +0:17 1.000000 +0:? Linker Objects +0:? 'gtf_ModelViewProjectionMatrix' ( uniform 4X4 matrix of float) +0:? 'instanceOffsets' ( uniform 3-element array of 3-component vector of float) +0:? 'va' ( uniform 64-element array of 4-component vector of float) +0:? 'color' ( global 4-component vector of float) +0:? 'gl_InstanceID' ( gl_InstanceId int InstanceId) + + +Linked vertex stage: + + +Shader version: 120 +Requested GL_EXT_draw_instanced +0:? Sequence +0:10 Function Definition: main( ( global void) +0:10 Function Parameters: +0:12 Sequence +0:12 Sequence +0:12 move second child to first child ( temp 4-component vector of float) +0:12 'vertex' ( temp 4-component vector of float) +0:12 add ( temp 4-component vector of float) +0:12 Construct vec4 ( temp 4-component vector of float) +0:12 divide ( temp 2-component vector of float) +0:12 vector swizzle ( temp 2-component vector of float) +0:12 direct index ( temp 4-component vector of float) +0:12 'va' ( uniform 64-element array of 4-component vector of float) +0:12 Constant: +0:12 0 (const int) +0:12 Sequence +0:12 Constant: +0:12 0 (const int) +0:12 Constant: +0:12 1 (const int) +0:12 Constant: +0:12 3.000000 +0:12 vector swizzle ( temp 2-component vector of float) +0:12 direct index ( temp 4-component vector of float) +0:12 'va' ( uniform 64-element array of 4-component vector of float) +0:12 Constant: +0:12 0 (const int) +0:12 Sequence +0:12 Constant: +0:12 2 (const int) +0:12 Constant: +0:12 3 (const int) +0:12 Construct vec4 ( temp 4-component vector of float) +0:12 indirect index ( temp 3-component vector of float) +0:12 'instanceOffsets' ( uniform 3-element array of 3-component vector of float) +0:12 'gl_InstanceID' ( gl_InstanceId int InstanceId) +0:12 Constant: +0:12 1.000000 +0:13 move second child to first child ( temp 4-component vector of float) +0:13 'color' ( global 4-component vector of float) +0:13 Constant: +0:13 0.000000 +0:13 0.000000 +0:13 0.000000 +0:13 0.000000 +0:14 Sequence +0:14 Sequence +0:14 move second child to first child ( temp int) +0:14 'i' ( temp int) +0:14 Constant: +0:14 1 (const int) +0:14 Loop with condition tested first +0:14 Loop Condition +0:14 Compare Less Than ( temp bool) +0:14 'i' ( temp int) +0:14 Constant: +0:14 64 (const int) +0:14 Loop Body +0:15 add second child into first child ( temp 4-component vector of float) +0:15 'color' ( global 4-component vector of float) +0:15 indirect index ( temp 4-component vector of float) +0:15 'va' ( uniform 64-element array of 4-component vector of float) +0:15 'i' ( temp int) +0:14 Loop Terminal Expression +0:14 Post-Increment ( temp int) +0:14 'i' ( temp int) +0:16 move second child to first child ( temp 4-component vector of float) +0:16 'gl_Position' ( gl_Position 4-component vector of float Position) +0:16 matrix-times-vector ( temp 4-component vector of float) +0:16 'gtf_ModelViewProjectionMatrix' ( uniform 4X4 matrix of float) +0:16 'vertex' ( temp 4-component vector of float) +0:17 move second child to first child ( temp float) +0:17 'gl_PointSize' ( gl_PointSize float PointSize) +0:17 Constant: +0:17 1.000000 +0:? Linker Objects +0:? 'gtf_ModelViewProjectionMatrix' ( uniform 4X4 matrix of float) +0:? 'instanceOffsets' ( uniform 3-element array of 3-component vector of float) +0:? 'va' ( uniform 64-element array of 4-component vector of float) +0:? 'color' ( global 4-component vector of float) +0:? 'gl_InstanceID' ( gl_InstanceId int InstanceId) + diff --git a/glslang/MachineIndependent/Initialize.cpp b/glslang/MachineIndependent/Initialize.cpp index b3ca656f..af333f3f 100755 --- a/glslang/MachineIndependent/Initialize.cpp +++ b/glslang/MachineIndependent/Initialize.cpp @@ -5256,7 +5256,7 @@ void TBuiltIns::initialize(int version, EProfile profile, const SpvVersion& spvV stageBuiltins[EShLangVertex].append( "int gl_VertexID;" // needs qualifier fixed later ); - if (version >= 140 && spvVersion.vulkan == 0) + if (spvVersion.vulkan == 0) stageBuiltins[EShLangVertex].append( "int gl_InstanceID;" // needs qualifier fixed later ); @@ -5311,6 +5311,7 @@ void TBuiltIns::initialize(int version, EProfile profile, const SpvVersion& spvV stageBuiltins[EShLangVertex].append( "highp vec4 gl_Position;" // needs qualifier fixed later "mediump float gl_PointSize;" // needs qualifier fixed later + "highp int gl_InstanceID;" // needs qualifier fixed later ); } else { if (spvVersion.vulkan == 0 || spvVersion.vulkanRelaxed) @@ -7861,6 +7862,8 @@ void TBuiltIns::identifyBuiltIns(int version, EProfile profile, const SpvVersion if (spvVersion.vulkan == 0) { SpecialQualifier("gl_VertexID", EvqVertexId, EbvVertexId, symbolTable); SpecialQualifier("gl_InstanceID", EvqInstanceId, EbvInstanceId, symbolTable); + if (version < 140) + symbolTable.setVariableExtensions("gl_InstanceID", 1, &E_GL_EXT_draw_instanced); } if (spvVersion.vulkan > 0 && spvVersion.vulkanRelaxed) { diff --git a/glslang/MachineIndependent/Intermediate.cpp b/glslang/MachineIndependent/Intermediate.cpp index 45deed70..ae62f196 100644 --- a/glslang/MachineIndependent/Intermediate.cpp +++ b/glslang/MachineIndependent/Intermediate.cpp @@ -2855,10 +2855,9 @@ void TIntermediate::addSymbolLinkageNodes(TIntermAggregate*& linkage, EShLanguag //} if (language == EShLangVertex) { - // the names won't be found in the symbol table unless the versions are right, - // so version logic does not need to be repeated here addSymbolLinkageNode(linkage, symbolTable, "gl_VertexID"); - addSymbolLinkageNode(linkage, symbolTable, "gl_InstanceID"); + if ((version < 140 && requestedExtensions.find(E_GL_EXT_draw_instanced) != requestedExtensions.end()) || version >= 140) + addSymbolLinkageNode(linkage, symbolTable, "gl_InstanceID"); } // Add a child to the root node for the linker objects diff --git a/glslang/MachineIndependent/Versions.cpp b/glslang/MachineIndependent/Versions.cpp index ce9899ed..694246cb 100644 --- a/glslang/MachineIndependent/Versions.cpp +++ b/glslang/MachineIndependent/Versions.cpp @@ -359,6 +359,7 @@ void TParseVersions::initializeExtensionBehavior() extensionBehavior[E_GL_EXT_ray_tracing_position_fetch] = EBhDisable; extensionBehavior[E_GL_EXT_shader_tile_image] = EBhDisable; extensionBehavior[E_GL_EXT_texture_shadow_lod] = EBhDisable; + extensionBehavior[E_GL_EXT_draw_instanced] = EBhDisable; extensionBehavior[E_GL_EXT_texture_array] = EBhDisable; // OVR extensions diff --git a/glslang/MachineIndependent/Versions.h b/glslang/MachineIndependent/Versions.h index b8f5300b..475cb893 100755 --- a/glslang/MachineIndependent/Versions.h +++ b/glslang/MachineIndependent/Versions.h @@ -215,6 +215,7 @@ const char* const E_GL_EXT_spirv_intrinsics = "GL_EXT_spirv_intr const char* const E_GL_EXT_fragment_shader_barycentric = "GL_EXT_fragment_shader_barycentric"; const char* const E_GL_EXT_mesh_shader = "GL_EXT_mesh_shader"; const char* const E_GL_EXT_opacity_micromap = "GL_EXT_opacity_micromap"; +const char* const E_GL_EXT_draw_instanced = "GL_EXT_draw_instanced"; const char* const E_GL_EXT_texture_array = "GL_EXT_texture_array"; // Arrays of extensions for the above viewportEXTs duplications diff --git a/gtests/AST.FromFile.cpp b/gtests/AST.FromFile.cpp index a6546ca6..621d4fe0 100644 --- a/gtests/AST.FromFile.cpp +++ b/gtests/AST.FromFile.cpp @@ -301,6 +301,7 @@ INSTANTIATE_TEST_SUITE_P( "coord_conventions.frag", "gl_FragCoord.frag", "glsl.interpOp.error.frag", + "GL_EXT_draw_instanced.vert", "overflow_underflow_toinf_0.frag", "GL_EXT_texture_array.frag", })),