Hash :
7b14dd73
Author :
Date :
2025-08-19T15:57:34
WGSL: RewriteMixedTypeMathExprs RewriteMixedTypeMathExprs: Some mixed-type arithmetic is legal in GLSL but not WGSL. Generate code to perform the arithmetic as specified in GLSL. Example: uvec2 x; uint y; x &= y; Is transformed into: x &= uvec(y); Also, mat2 x; int y; x += y; Is transformed into: x += mat2(float(y), float(y), float(y), float(y)) Bug: angleproject:42267100 Change-Id: I4a0ec1d9806b3331b4b1feff6fbe7c0f212f8120 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6862843 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Matthew Denton <mpdenton@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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
//
// Copyright 2017 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.
//
// IntermNode_util.cpp: High-level utilities for creating AST nodes and node hierarchies. Mostly
// meant to be used in AST transforms.
#include "compiler/translator/util.h"
#ifdef UNSAFE_BUFFERS_BUILD
# pragma allow_unsafe_buffers
#endif
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/FunctionLookup.h"
#include "compiler/translator/Name.h"
#include "compiler/translator/Symbol.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/SymbolUniqueId.h"
#include "compiler/translator/Types.h"
namespace sh
{
namespace
{
const TFunction *LookUpBuiltInFunction(const char *name,
const TIntermSequence *arguments,
const TSymbolTable &symbolTable,
int shaderVersion)
{
const ImmutableString &mangledName = TFunctionLookup::GetMangledName(name, *arguments);
const TSymbol *symbol = symbolTable.findBuiltIn(mangledName, shaderVersion);
if (symbol)
{
ASSERT(symbol->isFunction());
return static_cast<const TFunction *>(symbol);
}
return nullptr;
}
} // anonymous namespace
TIntermFunctionPrototype *CreateInternalFunctionPrototypeNode(const TFunction &func)
{
return new TIntermFunctionPrototype(&func);
}
TIntermFunctionDefinition *CreateInternalFunctionDefinitionNode(const TFunction &func,
TIntermBlock *functionBody)
{
return new TIntermFunctionDefinition(new TIntermFunctionPrototype(&func), functionBody);
}
TIntermTyped *CreateZeroNode(const TType &type)
{
TType constType(type);
constType.setQualifier(EvqConst);
// Make sure as a constructor, the type does not inherit qualifiers that are otherwise specified
// on interface blocks and varyings.
constType.setInvariant(false);
constType.setPrecise(false);
constType.setInterpolant(false);
constType.setMemoryQualifier(TMemoryQualifier::Create());
constType.setLayoutQualifier(TLayoutQualifier::Create());
constType.setInterfaceBlock(nullptr);
if (!type.isArray() && type.getBasicType() != EbtStruct)
{
size_t size = constType.getObjectSize();
TConstantUnion *u = new TConstantUnion[size];
for (size_t i = 0; i < size; ++i)
{
switch (type.getBasicType())
{
case EbtFloat:
u[i].setFConst(0.0f);
break;
case EbtInt:
u[i].setIConst(0);
break;
case EbtUInt:
u[i].setUConst(0u);
break;
case EbtBool:
u[i].setBConst(false);
break;
default:
// CreateZeroNode is called by ParseContext that keeps parsing even when an
// error occurs, so it is possible for CreateZeroNode to be called with
// non-basic types. This happens only on error condition but CreateZeroNode
// needs to return a value with the correct type to continue the type check.
// That's why we handle non-basic type by setting whatever value, we just need
// the type to be right.
u[i].setIConst(42);
break;
}
}
TIntermConstantUnion *node = new TIntermConstantUnion(u, constType);
return node;
}
TIntermSequence arguments;
if (type.isArray())
{
TType elementType(type);
elementType.toArrayElementType();
size_t arraySize = type.getOutermostArraySize();
for (size_t i = 0; i < arraySize; ++i)
{
arguments.push_back(CreateZeroNode(elementType));
}
}
else
{
ASSERT(type.getBasicType() == EbtStruct);
const TStructure *structure = type.getStruct();
for (const auto &field : structure->fields())
{
arguments.push_back(CreateZeroNode(*field->type()));
}
}
return TIntermAggregate::CreateConstructor(constType, &arguments);
}
TIntermConstantUnion *CreateFloatNode(float value, TPrecision precision)
{
TConstantUnion *u = new TConstantUnion[1];
u[0].setFConst(value);
TType type(EbtFloat, precision, EvqConst, 1);
return new TIntermConstantUnion(u, type);
}
TIntermConstantUnion *CreateVecNode(const float values[],
unsigned int vecSize,
TPrecision precision)
{
TConstantUnion *u = new TConstantUnion[vecSize];
for (unsigned int channel = 0; channel < vecSize; ++channel)
{
u[channel].setFConst(values[channel]);
}
TType type(EbtFloat, precision, EvqConst, static_cast<uint8_t>(vecSize));
return new TIntermConstantUnion(u, type);
}
TIntermConstantUnion *CreateUVecNode(const unsigned int values[],
unsigned int vecSize,
TPrecision precision)
{
TConstantUnion *u = new TConstantUnion[vecSize];
for (unsigned int channel = 0; channel < vecSize; ++channel)
{
u[channel].setUConst(values[channel]);
}
TType type(EbtUInt, precision, EvqConst, static_cast<uint8_t>(vecSize));
return new TIntermConstantUnion(u, type);
}
TIntermConstantUnion *CreateIndexNode(int index)
{
TConstantUnion *u = new TConstantUnion[1];
u[0].setIConst(index);
TType type(EbtInt, EbpHigh, EvqConst, 1);
return new TIntermConstantUnion(u, type);
}
TIntermConstantUnion *CreateUIntNode(unsigned int value)
{
TConstantUnion *u = new TConstantUnion[1];
u[0].setUConst(value);
TType type(EbtUInt, EbpHigh, EvqConst, 1);
return new TIntermConstantUnion(u, type);
}
TIntermConstantUnion *CreateBoolNode(bool value)
{
TConstantUnion *u = new TConstantUnion[1];
u[0].setBConst(value);
TType type(EbtBool, EbpUndefined, EvqConst, 1);
return new TIntermConstantUnion(u, type);
}
TVariable *CreateTempVariable(TSymbolTable *symbolTable, const TType *type)
{
ASSERT(symbolTable != nullptr);
return new TVariable(symbolTable, kEmptyImmutableString, type, SymbolType::AngleInternal);
}
TVariable *CreateTempVariable(TSymbolTable *symbolTable, const TType *type, TQualifier qualifier)
{
ASSERT(symbolTable != nullptr);
if (type->getQualifier() != qualifier || type->getInterfaceBlock() != nullptr)
{
TType *newType = new TType(*type);
newType->setQualifier(qualifier);
newType->setInterfaceBlock(nullptr);
type = newType;
}
return new TVariable(symbolTable, kEmptyImmutableString, type, SymbolType::AngleInternal);
}
TIntermSymbol *CreateTempSymbolNode(const TVariable *tempVariable)
{
ASSERT(tempVariable->symbolType() == SymbolType::AngleInternal);
ASSERT(tempVariable->getType().getQualifier() == EvqTemporary ||
tempVariable->getType().getQualifier() == EvqConst ||
tempVariable->getType().getQualifier() == EvqGlobal);
return new TIntermSymbol(tempVariable);
}
TIntermDeclaration *CreateTempDeclarationNode(const TVariable *tempVariable)
{
TIntermDeclaration *tempDeclaration = new TIntermDeclaration();
tempDeclaration->appendDeclarator(CreateTempSymbolNode(tempVariable));
return tempDeclaration;
}
TIntermDeclaration *CreateTempInitDeclarationNode(const TVariable *tempVariable,
TIntermTyped *initializer)
{
ASSERT(initializer != nullptr);
TIntermSymbol *tempSymbol = CreateTempSymbolNode(tempVariable);
TIntermDeclaration *tempDeclaration = new TIntermDeclaration();
TIntermBinary *tempInit = new TIntermBinary(EOpInitialize, tempSymbol, initializer);
tempDeclaration->appendDeclarator(tempInit);
return tempDeclaration;
}
TIntermBinary *CreateTempAssignmentNode(const TVariable *tempVariable, TIntermTyped *rightNode)
{
ASSERT(rightNode != nullptr);
TIntermSymbol *tempSymbol = CreateTempSymbolNode(tempVariable);
return new TIntermBinary(EOpAssign, tempSymbol, rightNode);
}
TVariable *DeclareTempVariable(TSymbolTable *symbolTable,
const TType *type,
TQualifier qualifier,
TIntermDeclaration **declarationOut)
{
TVariable *variable = CreateTempVariable(symbolTable, type, qualifier);
*declarationOut = CreateTempDeclarationNode(variable);
return variable;
}
TVariable *DeclareTempVariable(TSymbolTable *symbolTable,
TIntermTyped *initializer,
TQualifier qualifier,
TIntermDeclaration **declarationOut)
{
TVariable *variable =
CreateTempVariable(symbolTable, new TType(initializer->getType()), qualifier);
*declarationOut = CreateTempInitDeclarationNode(variable, initializer);
return variable;
}
std::pair<const TVariable *, const TVariable *> DeclareStructure(
TIntermBlock *root,
TSymbolTable *symbolTable,
TFieldList *fieldList,
TQualifier qualifier,
const TMemoryQualifier &memoryQualifier,
uint32_t arraySize,
const ImmutableString &structTypeName,
const ImmutableString *structInstanceName)
{
TStructure *structure =
new TStructure(symbolTable, structTypeName, fieldList, SymbolType::AngleInternal);
auto makeStructureType = [&](bool isStructSpecifier) {
TType *structureType = new TType(structure, isStructSpecifier);
structureType->setQualifier(qualifier);
structureType->setMemoryQualifier(memoryQualifier);
if (arraySize > 0)
{
structureType->makeArray(arraySize);
}
return structureType;
};
TIntermSequence insertSequence;
TVariable *typeVar = new TVariable(symbolTable, kEmptyImmutableString, makeStructureType(true),
SymbolType::Empty);
insertSequence.push_back(new TIntermDeclaration{typeVar});
TVariable *instanceVar = nullptr;
if (structInstanceName)
{
instanceVar = new TVariable(symbolTable, *structInstanceName, makeStructureType(false),
SymbolType::AngleInternal);
insertSequence.push_back(new TIntermDeclaration{instanceVar});
}
size_t firstFunctionIndex = FindFirstFunctionDefinitionIndex(root);
root->insertChildNodes(firstFunctionIndex, insertSequence);
return {typeVar, instanceVar};
}
TInterfaceBlock *DeclareInterfaceBlock(TSymbolTable *symbolTable,
TFieldList *fieldList,
const TLayoutQualifier &layoutQualifier,
const ImmutableString &blockTypeName)
{
// Define an interface block.
TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
symbolTable, blockTypeName, fieldList, layoutQualifier, SymbolType::AngleInternal);
return interfaceBlock;
}
const TVariable *DeclareInterfaceBlockVariable(TIntermBlock *root,
TSymbolTable *symbolTable,
TQualifier qualifier,
const TInterfaceBlock *interfaceBlock,
const TLayoutQualifier &layoutQualifier,
const TMemoryQualifier &memoryQualifier,
const uint32_t arraySize,
const ImmutableString &blockVariableName)
{
TType *interfaceBlockType = new TType(interfaceBlock, qualifier, layoutQualifier);
interfaceBlockType->setMemoryQualifier(memoryQualifier);
if (arraySize > 0)
{
interfaceBlockType->makeArray(arraySize);
}
TIntermDeclaration *interfaceBlockDecl = new TIntermDeclaration;
TVariable *interfaceBlockVar =
new TVariable(symbolTable, blockVariableName, interfaceBlockType,
blockVariableName.empty() ? SymbolType::Empty : SymbolType::AngleInternal);
TIntermSymbol *interfaceBlockDeclarator = new TIntermSymbol(interfaceBlockVar);
interfaceBlockDecl->appendDeclarator(interfaceBlockDeclarator);
// Insert the declarations before the first function.
TIntermSequence insertSequence;
insertSequence.push_back(interfaceBlockDecl);
size_t firstFunctionIndex = FindFirstFunctionDefinitionIndex(root);
root->insertChildNodes(firstFunctionIndex, insertSequence);
return interfaceBlockVar;
}
const TVariable &CreateStructTypeVariable(TSymbolTable &symbolTable, const TStructure &structure)
{
TType *type = new TType(&structure, true);
TVariable *var = new TVariable(&symbolTable, ImmutableString(""), type, SymbolType::Empty);
return *var;
}
const TVariable &CreateInstanceVariable(TSymbolTable &symbolTable,
const TStructure &structure,
const Name &name,
TQualifier qualifier,
const angle::Span<const unsigned int> *arraySizes)
{
TType *type = new TType(&structure, false);
type->setQualifier(qualifier);
if (arraySizes)
{
type->makeArrays(*arraySizes);
}
TVariable *var = new TVariable(&symbolTable, name.rawName(), type, name.symbolType());
return *var;
}
TIntermBinary &AccessField(const TVariable &structInstanceVar, const Name &name)
{
return AccessField(*new TIntermSymbol(&structInstanceVar), name);
}
TIntermBinary &AccessField(TIntermTyped &object, const Name &name)
{
const TStructure *structure = object.getType().getStruct();
ASSERT(structure);
const TFieldList &fieldList = structure->fields();
for (int i = 0; i < static_cast<int>(fieldList.size()); ++i)
{
TField *current = fieldList[i];
if (Name(*current) == name)
{
return AccessFieldByIndex(object, i);
}
}
UNREACHABLE();
return AccessFieldByIndex(object, -1);
}
TIntermBinary &AccessFieldByIndex(TIntermTyped &object, int index)
{
const TType &type = object.getType();
ASSERT(!type.isArray());
const TStructure *structure = type.getStruct();
ASSERT(structure);
ASSERT(0 <= index);
ASSERT(static_cast<size_t>(index) < structure->fields().size());
return *new TIntermBinary(
TOperator::EOpIndexDirectStruct, &object,
new TIntermConstantUnion(new TConstantUnion(index), *new TType(TBasicType::EbtInt)));
}
TIntermBinary *AccessFieldOfNamedInterfaceBlock(const TVariable *object, int index)
{
ASSERT(object->getType().getInterfaceBlock());
const TFieldList &fieldList = object->getType().getInterfaceBlock()->fields();
ASSERT(0 <= index);
ASSERT(static_cast<size_t>(index) < fieldList.size());
ASSERT(object->symbolType() != SymbolType::Empty);
return new TIntermBinary(TOperator::EOpIndexDirectInterfaceBlock, new TIntermSymbol(object),
CreateIndexNode(index));
}
TIntermBlock *EnsureBlock(TIntermNode *node)
{
if (node == nullptr)
return nullptr;
TIntermBlock *blockNode = node->getAsBlock();
if (blockNode != nullptr)
{
return blockNode;
}
blockNode = new TIntermBlock();
blockNode->setLine(node->getLine());
blockNode->appendStatement(node);
return blockNode;
}
TIntermBlock *EnsureLoopBodyBlock(TIntermNode *node)
{
if (node == nullptr)
{
return new TIntermBlock();
}
return EnsureBlock(node);
}
TIntermSymbol *ReferenceGlobalVariable(const ImmutableString &name, const TSymbolTable &symbolTable)
{
const TSymbol *symbol = symbolTable.findGlobal(name);
ASSERT(symbol && symbol->isVariable());
return new TIntermSymbol(static_cast<const TVariable *>(symbol));
}
TIntermSymbol *ReferenceBuiltInVariable(const ImmutableString &name,
const TSymbolTable &symbolTable,
int shaderVersion)
{
const TVariable *var =
static_cast<const TVariable *>(symbolTable.findBuiltIn(name, shaderVersion));
ASSERT(var);
return new TIntermSymbol(var);
}
TIntermTyped *CreateBuiltInFunctionCallNode(const char *name,
TIntermSequence *arguments,
const TSymbolTable &symbolTable,
int shaderVersion)
{
const TFunction *fn = LookUpBuiltInFunction(name, arguments, symbolTable, shaderVersion);
ASSERT(fn);
TOperator op = fn->getBuiltInOp();
if (BuiltInGroup::IsMath(op) && arguments->size() == 1)
{
return new TIntermUnary(op, arguments->at(0)->getAsTyped(), fn);
}
return TIntermAggregate::CreateBuiltInFunctionCall(*fn, arguments);
}
TIntermTyped *CreateBuiltInFunctionCallNode(const char *name,
const std::initializer_list<TIntermNode *> &arguments,
const TSymbolTable &symbolTable,
int shaderVersion)
{
TIntermSequence argSequence(arguments);
return CreateBuiltInFunctionCallNode(name, &argSequence, symbolTable, shaderVersion);
}
TIntermTyped *CreateBuiltInUnaryFunctionCallNode(const char *name,
TIntermTyped *argument,
const TSymbolTable &symbolTable,
int shaderVersion)
{
return CreateBuiltInFunctionCallNode(name, {argument}, symbolTable, shaderVersion);
}
// Returns true if a block ends in a branch (break, continue, return, etc). This is only correct
// after PruneNoOps, because it expects empty blocks after a branch to have been already pruned,
// i.e. a block can only end in a branch if its last statement is a branch or is a block ending in
// branch.
bool EndsInBranch(TIntermBlock *block)
{
while (block != nullptr)
{
// Get the last statement of the block.
TIntermSequence &statements = *block->getSequence();
if (statements.empty())
{
return false;
}
TIntermNode *lastStatement = statements.back();
// If it's a branch itself, we have the answer.
if (lastStatement->getAsBranchNode())
{
return true;
}
// Otherwise, see if it's a block that ends in a branch
block = lastStatement->getAsBlock();
}
return false;
}
TIntermNode *CastScalar(const TType &type, TIntermTyped *scalar)
{
const TBasicType basicType = type.getBasicType();
if (scalar->getType().getBasicType() == basicType)
{
return scalar;
}
TType castDestType(basicType, type.getPrecision());
return TIntermAggregate::CreateConstructor(castDestType, {scalar});
}
} // namespace sh