Clean up redundant initialization of gl_Position
In case gl_Position is statically used in the input shader, setting
the INIT_OUTPUT_VARIABLES flag will initialize gl_Position. Avoid
redundant initialization of gl_Position in this case.
Includes cleaning up memory management in InitOutputVariables_test:
all the pool-allocated variables will be freed at the end of each test
when the memory pool is cleared, so manual memory management is not
needed.
Also includes making the zero node check used in unit tests stricter
so that the tests are more reliable and moving it to
ShaderCompileTreeTest.h so that it can be reused in the future.
BUG=angleproject:2092
TEST=angle_unittests
Change-Id: I323a0a094afa6cea95c8a64e681d9fc485137423
Reviewed-on: https://chromium-review.googlesource.com/549418
Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
diff --git a/src/compiler/translator/Compiler.cpp b/src/compiler/translator/Compiler.cpp
index f29f02a..706bc40 100644
--- a/src/compiler/translator/Compiler.cpp
+++ b/src/compiler/translator/Compiler.cpp
@@ -226,6 +226,7 @@
TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
: variablesCollected(false),
+ mGLPositionInitialized(false),
shaderType(type),
shaderSpec(spec),
outputType(output),
@@ -431,12 +432,6 @@
DeclareAndInitBuiltinsForInstancedMultiview(root, getNumViews());
}
- // gl_Position is always written in compatibility output mode
- if (success && shaderType == GL_VERTEX_SHADER &&
- ((compileOptions & SH_INIT_GL_POSITION) ||
- (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
- initializeGLPosition(root);
-
// This pass might emit short circuits so keep it before the short circuit unfolding
if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
RewriteDoWhile(root, getTemporaryIndex());
@@ -487,6 +482,17 @@
}
}
+ // gl_Position is always written in compatibility output mode.
+ // It may have been already initialized among other output variables, in that case we don't
+ // need to initialize it twice.
+ if (success && shaderType == GL_VERTEX_SHADER && !mGLPositionInitialized &&
+ ((compileOptions & SH_INIT_GL_POSITION) ||
+ (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
+ {
+ initializeGLPosition(root);
+ mGLPositionInitialized = true;
+ }
+
// Removing invariant declarations must be done after collecting variables.
// Otherwise, built-in invariant declarations don't apply.
if (success && RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions))
@@ -727,6 +733,7 @@
varyings.clear();
interfaceBlocks.clear();
variablesCollected = false;
+ mGLPositionInitialized = false;
mNumViews = -1;
@@ -964,6 +971,11 @@
for (auto var : varyings)
{
list.push_back(var);
+ if (var.name == "gl_Position")
+ {
+ ASSERT(!mGLPositionInitialized);
+ mGLPositionInitialized = true;
+ }
}
}
else