From 801c209fa0a2878c0675b7808154de93bc42f5fd Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 10 Mar 2021 13:05:41 +0200 Subject: [PATCH] Count EmitVertex as referencing all active outputs --- source/glsl/optimize.cpp | 7 ++ .../glsl/conditional_multiple_emitvertex.glsl | 76 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 tests/glsl/conditional_multiple_emitvertex.glsl diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 080bfd7c..441a73a0 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -728,6 +728,13 @@ void UnusedVariableRemover::visit(FunctionCall &call) /* Treat function calls as having side effects so expression statements consisting of nothing but a function call won't be optimized away. */ r_side_effects = true; + + if(stage->type==Stage::GEOMETRY && call.name=="EmitVertex") + { + for(map::const_iterator i=variables.begin(); i!=variables.end(); ++i) + if(i->second.output) + referenced(i->first, call); + } } void UnusedVariableRemover::record_assignment(const Assignment::Target &target, Node &node) diff --git a/tests/glsl/conditional_multiple_emitvertex.glsl b/tests/glsl/conditional_multiple_emitvertex.glsl new file mode 100644 index 00000000..09bf25d1 --- /dev/null +++ b/tests/glsl/conditional_multiple_emitvertex.glsl @@ -0,0 +1,76 @@ +uniform bool flag; +uniform sampler2D tex; + +#pragma MSP stage(vertex) +layout(location=0) in vec4 position; +void main() +{ + out vec2 texcoord = position.xy*0.5+0.5; + gl_Position = position; +} + +#pragma MSP stage(geometry) +layout(triangles) in; +layout(triangles, max_vertices=3) out; +void main() +{ + passthrough[0]; + if(flag) + EmitVertex(); + passthrough[1]; + if(flag) + EmitVertex(); + passthrough[2]; + if(flag) + EmitVertex(); +} + +#pragma MSP stage(fragment) +layout(location=0) out vec4 frag_color; +void main() +{ + frag_color = texture(tex, texcoord); +} + +/* Expected output: vertex +layout(location=0) in vec4 position; +out vec2 texcoord; +void main() +{ + texcoord = position.xy*0.5+0.5; + gl_Position = position; +} +*/ + +/* Expected output: geometry +uniform bool flag; +layout(triangles) in; +layout(triangles, max_vertices=3) out; +in vec2 texcoord[]; +out vec2 _gs_out_texcoord; +void main() +{ + gl_Position = gl_in[0].gl_Position; + _gs_out_texcoord = texcoord[0]; + if(flag) + EmitVertex(); + gl_Position = gl_in[1].gl_Position; + _gs_out_texcoord = texcoord[1]; + if(flag) + EmitVertex(); + gl_Position = gl_in[2].gl_Position; + _gs_out_texcoord = texcoord[2]; + if(flag) + EmitVertex(); +} +*/ + +/* Expected output: fragment +uniform sampler2D tex; +layout(location=0) out vec4 frag_color; +in vec2 _gs_out_texcoord; +void main() +{ + frag_color = texture(tex, _gs_out_texcoord); +} +*/ -- 2.45.2