Branch
Hash :
074eec2f
Author :
Date :
2025-10-02T15:31:19
Translator: remove angle_BaseVertex/Instance from built-ins These aren't built-ins, they are just used for emulation. Instead of listing them in the built-in variables, they are now created as uniforms during emulation. This change also gives gl_BaseVertex/Instance their own qualifiers so they aren't considered uniforms. The emulation pass is also optimized to do fewer passes. Bug: angleproject:349994211 Change-Id: I44fd345fd4a2dc4a001eb294952a984ae710d606 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/7004388 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
//
// Copyright 2019 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// EmulateGLDrawID is an AST traverser to convert the gl_DrawID builtin
// to a uniform int
//
// EmulateGLBaseVertex is an AST traverser to convert the gl_BaseVertex builtin
// to a uniform int
//
// EmulateGLBaseInstance is an AST traverser to convert the gl_BaseInstance builtin
// to a uniform int
//
#include "compiler/translator/tree_ops/EmulateMultiDrawShaderBuiltins.h"
#include "angle_gl.h"
#include "compiler/translator/StaticType.h"
#include "compiler/translator/Symbol.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/tree_util/BuiltIn.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "compiler/translator/tree_util/ReplaceVariable.h"
#include "compiler/translator/util.h"
namespace sh
{
namespace
{
constexpr const ImmutableString kEmulatedGLDrawIDName("angle_DrawID");
class FindGLDrawIDTraverser : public TIntermTraverser
{
public:
FindGLDrawIDTraverser() : TIntermTraverser(true, false, false), mVariable(nullptr) {}
const TVariable *getGLDrawIDBuiltinVariable() { return mVariable; }
protected:
void visitSymbol(TIntermSymbol *node) override
{
if (node->getQualifier() == EvqDrawID)
{
mVariable = &node->variable();
}
}
private:
const TVariable *mVariable;
};
class AddBaseVertexToGLVertexIDTraverser : public TIntermTraverser
{
public:
AddBaseVertexToGLVertexIDTraverser() : TIntermTraverser(true, false, false) {}
protected:
void visitSymbol(TIntermSymbol *node) override
{
if (&node->variable() == BuiltInVariable::gl_VertexID())
{
TIntermSymbol *baseVertexRef = new TIntermSymbol(BuiltInVariable::gl_BaseVertex());
TIntermBinary *addBaseVertex = new TIntermBinary(EOpAdd, node, baseVertexRef);
queueReplacement(addBaseVertex, OriginalNode::BECOMES_CHILD);
}
}
};
constexpr const ImmutableString kEmulatedGLBaseVertexName("angle_BaseVertex");
constexpr const ImmutableString kEmulatedGLBaseInstanceName("angle_BaseInstance");
class FindGLBaseVertexBaseInstanceTraverser : public TIntermTraverser
{
public:
FindGLBaseVertexBaseInstanceTraverser()
: TIntermTraverser(true, false, false),
mBaseVertexVariable(nullptr),
mBaseInstanceVariable(nullptr)
{}
const TVariable *getGLBaseVertexBuiltinVariable() { return mBaseVertexVariable; }
const TVariable *getGLBaseInstanceBuiltinVariable() { return mBaseInstanceVariable; }
protected:
void visitSymbol(TIntermSymbol *node) override
{
switch (node->getQualifier())
{
case EvqBaseVertex:
mBaseVertexVariable = &node->variable();
break;
case EvqBaseInstance:
mBaseInstanceVariable = &node->variable();
break;
default:
break;
}
}
private:
const TVariable *mBaseVertexVariable;
const TVariable *mBaseInstanceVariable;
};
bool EmulateBuiltIn(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable,
const TVariable *builtInVariable,
const TType *type,
const ImmutableString &name,
std::vector<sh::ShaderVariable> *uniforms)
{
const TVariable *emulatedVar =
new TVariable(symbolTable, name, type, SymbolType::AngleInternal);
const TIntermSymbol *emulatedSymbol = new TIntermSymbol(emulatedVar);
// AngleInternal variables don't get collected
ShaderVariable uniform;
uniform.name = name.data();
uniform.mappedName = name.data();
uniform.type = GLVariableType(*type);
uniform.precision = GLVariablePrecision(*type);
uniform.staticUse = symbolTable->isStaticallyUsed(*builtInVariable);
uniform.active = true;
uniform.binding = type->getLayoutQualifier().binding;
uniform.location = type->getLayoutQualifier().location;
uniform.offset = type->getLayoutQualifier().offset;
uniform.rasterOrdered = type->getLayoutQualifier().rasterOrdered;
uniform.readonly = type->getMemoryQualifier().readonly;
uniform.writeonly = type->getMemoryQualifier().writeonly;
uniforms->push_back(uniform);
DeclareGlobalVariable(root, emulatedVar);
return ReplaceVariableWithTyped(compiler, root, builtInVariable, emulatedSymbol);
}
} // namespace
bool EmulateGLDrawID(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable,
std::vector<sh::ShaderVariable> *uniforms)
{
FindGLDrawIDTraverser traverser;
root->traverse(&traverser);
const TVariable *builtInVariable = traverser.getGLDrawIDBuiltinVariable();
if (builtInVariable)
{
const TType *type = StaticType::Get<EbtInt, EbpHigh, EvqUniform, 1, 1>();
if (!EmulateBuiltIn(compiler, root, symbolTable, builtInVariable, type,
kEmulatedGLDrawIDName, uniforms))
{
return false;
}
}
return true;
}
bool EmulateGLBaseVertexBaseInstance(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable,
std::vector<sh::ShaderVariable> *uniforms,
bool addBaseVertexToVertexID)
{
if (addBaseVertexToVertexID)
{
// This is a workaround for Mac AMD GPU
// Replace gl_VertexID with (gl_VertexID + gl_BaseVertex)
AddBaseVertexToGLVertexIDTraverser traverserVertexID;
root->traverse(&traverserVertexID);
if (!traverserVertexID.updateTree(compiler, root))
{
return false;
}
}
FindGLBaseVertexBaseInstanceTraverser traverser;
root->traverse(&traverser);
const TVariable *builtInVariableBaseVertex = traverser.getGLBaseVertexBuiltinVariable();
const TVariable *builtInVariableBaseInstance = traverser.getGLBaseInstanceBuiltinVariable();
if (builtInVariableBaseVertex)
{
const TType *type = StaticType::Get<EbtInt, EbpHigh, EvqUniform, 1, 1>();
if (!EmulateBuiltIn(compiler, root, symbolTable, builtInVariableBaseVertex, type,
kEmulatedGLBaseVertexName, uniforms))
{
return false;
}
}
if (builtInVariableBaseInstance)
{
const TType *type = StaticType::Get<EbtInt, EbpHigh, EvqUniform, 1, 1>();
if (!EmulateBuiltIn(compiler, root, symbolTable, builtInVariableBaseInstance, type,
kEmulatedGLBaseInstanceName, uniforms))
{
return false;
}
}
// DeclareGlobalVariable prepends to the declarations, but the uniforms are appended. So if
// both base vertex and instance variables are added, the order doesn't match. Fix that here.
if (builtInVariableBaseVertex && builtInVariableBaseInstance)
{
const size_t count = uniforms->size();
ASSERT(count >= 2);
std::swap((*uniforms)[count - 1], (*uniforms)[count - 2]);
}
return true;
}
} // namespace sh