Path: blob/main_old/src/compiler/translator/BuiltinsWorkaroundGLSL.cpp
1693 views
//1// Copyright 2019 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56#include "compiler/translator/BuiltinsWorkaroundGLSL.h"78#include "angle_gl.h"9#include "compiler/translator/Symbol.h"10#include "compiler/translator/SymbolTable.h"11#include "compiler/translator/tree_util/BuiltIn.h"1213namespace sh14{1516namespace17{18constexpr const ImmutableString kGlInstanceIDString("gl_InstanceID");19constexpr const ImmutableString kGlVertexIDString("gl_VertexID");2021class TBuiltinsWorkaroundGLSL : public TIntermTraverser22{23public:24TBuiltinsWorkaroundGLSL(TSymbolTable *symbolTable, ShCompileOptions options);2526void visitSymbol(TIntermSymbol *node) override;27bool visitDeclaration(Visit, TIntermDeclaration *node) override;2829private:30void ensureVersionIsAtLeast(int version);3132ShCompileOptions mCompileOptions;3334bool isBaseInstanceDeclared = false;35};3637TBuiltinsWorkaroundGLSL::TBuiltinsWorkaroundGLSL(TSymbolTable *symbolTable,38ShCompileOptions options)39: TIntermTraverser(true, false, false, symbolTable), mCompileOptions(options)40{}4142void TBuiltinsWorkaroundGLSL::visitSymbol(TIntermSymbol *node)43{44if (node->variable().symbolType() == SymbolType::BuiltIn)45{46if (node->getName() == kGlInstanceIDString)47{48TIntermSymbol *instanceIndexRef =49new TIntermSymbol(BuiltInVariable::gl_InstanceIndex());5051if (isBaseInstanceDeclared)52{53TIntermSymbol *baseInstanceRef =54new TIntermSymbol(BuiltInVariable::angle_BaseInstance());5556TIntermBinary *subBaseInstance =57new TIntermBinary(EOpSub, instanceIndexRef, baseInstanceRef);58queueReplacement(subBaseInstance, OriginalNode::IS_DROPPED);59}60else61{62queueReplacement(instanceIndexRef, OriginalNode::IS_DROPPED);63}64}65else if (node->getName() == kGlVertexIDString)66{67TIntermSymbol *vertexIndexRef = new TIntermSymbol(BuiltInVariable::gl_VertexIndex());68queueReplacement(vertexIndexRef, OriginalNode::IS_DROPPED);69}70}71}7273bool TBuiltinsWorkaroundGLSL::visitDeclaration(Visit, TIntermDeclaration *node)74{75const TIntermSequence &sequence = *(node->getSequence());76ASSERT(!sequence.empty());7778for (TIntermNode *variableNode : sequence)79{80TIntermSymbol *variable = variableNode->getAsSymbolNode();81if (variable && variable->variable().symbolType() == SymbolType::BuiltIn)82{83if (variable->getName() == "angle_BaseInstance")84{85isBaseInstanceDeclared = true;86}87}88}89return true;90}9192} // anonymous namespace9394ANGLE_NO_DISCARD bool ShaderBuiltinsWorkaround(TCompiler *compiler,95TIntermBlock *root,96TSymbolTable *symbolTable,97ShCompileOptions compileOptions)98{99TBuiltinsWorkaroundGLSL builtins(symbolTable, compileOptions);100root->traverse(&builtins);101if (!builtins.updateTree(compiler, root))102{103return false;104}105return true;106}107108} // namespace sh109110111