Commit 83498433 by Olli Etuaho Committed by Commit Bot

Put global init code inside main() instead in a function

Otherwise we may run into a name conflict issue if the shader code is run through the translator twice, since neither user-defined function names or internal function names are currently being prefixed in GLSL output. This could be fixed by prefixing user-defined variables in GLSL output, but this solution is much simpler. BUG=angleproject:1966 TEST=angle_end2end_tests, WebGL conformance tests Change-Id: I15c8de5a0a5e596fafe9f55e8d370345c790d5cc Reviewed-on: https://chromium-review.googlesource.com/509688 Commit-Queue: Olli Etuaho <oetuaho@nvidia.com> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org>
parent 538c7bcf
...@@ -3,11 +3,11 @@ ...@@ -3,11 +3,11 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// DeferGlobalInitializers is an AST traverser that moves global initializers into a function, and // DeferGlobalInitializers is an AST traverser that moves global initializers into a block in the
// adds a function call to that function in the beginning of main(). // beginning of main(). This enables initialization of globals with uniforms or non-constant
// This enables initialization of globals with uniforms or non-constant globals, as allowed by // globals, as allowed by the WebGL spec. Some initializers referencing non-constants may need to be
// the WebGL spec. Some initializers referencing non-constants may need to be unfolded into if // unfolded into if statements in HLSL - this kind of steps should be done after
// statements in HLSL - this kind of steps should be done after DeferGlobalInitializers is run. // DeferGlobalInitializers is run.
// //
#include "compiler/translator/DeferGlobalInitializers.h" #include "compiler/translator/DeferGlobalInitializers.h"
...@@ -80,34 +80,17 @@ void GetDeferredInitializers(TIntermDeclaration *declaration, ...@@ -80,34 +80,17 @@ void GetDeferredInitializers(TIntermDeclaration *declaration,
} }
} }
void InsertInitFunction(TIntermBlock *root, TIntermSequence *deferredInitializers) void InsertInitCodeToMain(TIntermBlock *root, TIntermSequence *deferredInitializers)
{ {
TSymbolUniqueId initFunctionId; // Insert init code as a block to the beginning of the main() function.
const char *functionName = "initializeGlobals"; TIntermBlock *initGlobalsBlock = new TIntermBlock();
initGlobalsBlock->getSequence()->swap(*deferredInitializers);
// Add function prototype to the beginning of the shader
TIntermFunctionPrototype *functionPrototypeNode =
TIntermTraverser::CreateInternalFunctionPrototypeNode(TType(EbtVoid), functionName,
initFunctionId);
root->getSequence()->insert(root->getSequence()->begin(), functionPrototypeNode);
// Add function definition to the end of the shader
TIntermBlock *functionBodyNode = new TIntermBlock();
functionBodyNode->getSequence()->swap(*deferredInitializers);
TIntermFunctionDefinition *functionDefinition =
TIntermTraverser::CreateInternalFunctionDefinitionNode(TType(EbtVoid), functionName,
functionBodyNode, initFunctionId);
root->getSequence()->push_back(functionDefinition);
// Insert call into main function
TIntermFunctionDefinition *main = FindMain(root); TIntermFunctionDefinition *main = FindMain(root);
ASSERT(main != nullptr); ASSERT(main != nullptr);
TIntermAggregate *functionCallNode = TIntermTraverser::CreateInternalFunctionCallNode(
TType(EbtVoid), functionName, initFunctionId, nullptr);
TIntermBlock *mainBody = main->getBody(); TIntermBlock *mainBody = main->getBody();
ASSERT(mainBody != nullptr); ASSERT(mainBody != nullptr);
mainBody->getSequence()->insert(mainBody->getSequence()->begin(), functionCallNode); mainBody->getSequence()->insert(mainBody->getSequence()->begin(), initGlobalsBlock);
} }
} // namespace } // namespace
...@@ -130,7 +113,7 @@ void DeferGlobalInitializers(TIntermBlock *root) ...@@ -130,7 +113,7 @@ void DeferGlobalInitializers(TIntermBlock *root)
// Add the function with initialization and the call to that. // Add the function with initialization and the call to that.
if (!deferredInitializers->empty()) if (!deferredInitializers->empty())
{ {
InsertInitFunction(root, deferredInitializers); InsertInitCodeToMain(root, deferredInitializers);
} }
} }
......
...@@ -3,11 +3,11 @@ ...@@ -3,11 +3,11 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// DeferGlobalInitializers is an AST traverser that moves global initializers into a function, and // DeferGlobalInitializers is an AST traverser that moves global initializers into a block in the
// adds a function call to that function in the beginning of main(). // beginning of main(). This enables initialization of globals with uniforms or non-constant
// This enables initialization of globals with uniforms or non-constant globals, as allowed by // globals, as allowed by the WebGL spec. Some initializers referencing non-constants may need to be
// the WebGL spec. Some initializers referencing non-constants may need to be unfolded into if // unfolded into if statements in HLSL - this kind of steps should be done after
// statements in HLSL - this kind of steps should be done after DeferGlobalInitializers is run. // DeferGlobalInitializers is run.
// //
#ifndef COMPILER_TRANSLATOR_DEFERGLOBALINITIALIZERS_H_ #ifndef COMPILER_TRANSLATOR_DEFERGLOBALINITIALIZERS_H_
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment