Hash :
9b63700b
Author :
Date :
2023-07-11T10:25:40
Translator: Reorganize files This change makes the translator files more organized by: - Grouping files that are specific to a certain output under their own directory, - Moving transformations under tree_ops - Removing Direct from metal translation now that that SPIR-V Cross path is removed Bug: angleproject:6394 Change-Id: Iaf5bb8d5604b84748dece029821b1f77b2813967 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4678780 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 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 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
//
// Copyright 2014 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.
//
// StructureHLSL.cpp:
// HLSL translation of GLSL constructors and structures.
//
#include "compiler/translator/hlsl/StructureHLSL.h"
#include "common/utilities.h"
#include "compiler/translator/Types.h"
#include "compiler/translator/hlsl/OutputHLSL.h"
#include "compiler/translator/hlsl/UtilsHLSL.h"
#include "compiler/translator/util.h"
namespace sh
{
namespace
{
TString Define(const TStructure &structure,
bool useHLSLRowMajorPacking,
bool useStd140Packing,
bool forcePadding,
Std140PaddingHelper *padHelper)
{
const TFieldList &fields = structure.fields();
const bool isNameless = (structure.symbolType() == SymbolType::Empty);
const TString &structName = QualifiedStructNameString(structure, useHLSLRowMajorPacking,
useStd140Packing, forcePadding);
const TString declareString = (isNameless ? "struct" : "struct " + structName);
TString string;
string += declareString +
"\n"
"{\n";
size_t memberSize = fields.size();
for (const TField *field : fields)
{
memberSize--;
const TType &fieldType = *field->type();
if (!IsSampler(fieldType.getBasicType()))
{
const TStructure *fieldStruct = fieldType.getStruct();
const TString &fieldTypeString =
fieldStruct ? QualifiedStructNameString(*fieldStruct, useHLSLRowMajorPacking,
useStd140Packing, false)
: TypeString(fieldType);
if (padHelper)
{
string += padHelper->prePaddingString(
fieldType, (memberSize != fields.size() - 1) && forcePadding);
}
string += " " + fieldTypeString + " " + DecorateField(field->name(), structure) +
ArrayString(fieldType).data() + ";\n";
if (padHelper)
{
string += padHelper->postPaddingString(fieldType, useHLSLRowMajorPacking,
memberSize == 0, forcePadding);
}
}
}
// Nameless structs do not finish with a semicolon and newline, to leave room for an instance
// variable
string += (isNameless ? "} " : "};\n");
return string;
}
TString WriteParameterList(const std::vector<TType> ¶meters)
{
TString parameterList;
for (size_t parameter = 0u; parameter < parameters.size(); parameter++)
{
const TType ¶mType = parameters[parameter];
parameterList +=
TypeString(paramType) + " x" + str(parameter) + ArrayString(paramType).data();
if (parameter < parameters.size() - 1u)
{
parameterList += ", ";
}
}
return parameterList;
}
int GetElementPadding(int elementIndex, int alignment)
{
const int paddingOffset = elementIndex % alignment;
return paddingOffset != 0 ? (alignment - paddingOffset) : 0;
}
} // anonymous namespace
Std140PaddingHelper::Std140PaddingHelper(const std::map<TString, int> &structElementIndexes,
unsigned *uniqueCounter)
: mPaddingCounter(uniqueCounter), mElementIndex(0), mStructElementIndexes(&structElementIndexes)
{}
Std140PaddingHelper::Std140PaddingHelper(const Std140PaddingHelper &other)
: mPaddingCounter(other.mPaddingCounter),
mElementIndex(other.mElementIndex),
mStructElementIndexes(other.mStructElementIndexes)
{}
Std140PaddingHelper &Std140PaddingHelper::operator=(const Std140PaddingHelper &other)
{
mPaddingCounter = other.mPaddingCounter;
mElementIndex = other.mElementIndex;
mStructElementIndexes = other.mStructElementIndexes;
return *this;
}
TString Std140PaddingHelper::next()
{
unsigned value = (*mPaddingCounter)++;
return str(value);
}
int Std140PaddingHelper::prePadding(const TType &type, bool forcePadding)
{
if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
{
if (forcePadding)
{
// Add padding between the structure's members to follow the std140 rules manually.
const int forcePaddingCount = GetElementPadding(mElementIndex, 4);
mElementIndex = 0;
return forcePaddingCount;
}
else
{
// no padding needed, HLSL will align the field to a new register
mElementIndex = 0;
return 0;
}
}
const GLenum glType = GLVariableType(type);
const int numComponents = gl::VariableComponentCount(glType);
if (numComponents >= 4)
{
if (forcePadding)
{
// Add padding between the structure's members to follow the std140 rules manually.
const int forcePaddingCount = GetElementPadding(mElementIndex, 4);
mElementIndex = numComponents % 4;
return forcePaddingCount;
}
else
{
// no padding needed, HLSL will align the field to a new register
mElementIndex = 0;
return 0;
}
}
if (mElementIndex + numComponents > 4)
{
if (forcePadding)
{
// Add padding between the structure's members to follow the std140 rules manually.
const int forcePaddingCount = GetElementPadding(mElementIndex, 4);
mElementIndex = numComponents;
return forcePaddingCount;
}
else
{
// no padding needed, HLSL will align the field to a new register
mElementIndex = numComponents;
return 0;
}
}
const int alignment = numComponents == 3 ? 4 : numComponents;
const int paddingCount = GetElementPadding(mElementIndex, alignment);
mElementIndex += paddingCount;
mElementIndex += numComponents;
mElementIndex %= 4;
return paddingCount;
}
TString Std140PaddingHelper::prePaddingString(const TType &type, bool forcePadding)
{
int paddingCount = prePadding(type, forcePadding);
TString padding;
for (int paddingIndex = 0; paddingIndex < paddingCount; paddingIndex++)
{
padding += " float pad_" + next() + ";\n";
}
return padding;
}
TString Std140PaddingHelper::postPaddingString(const TType &type,
bool useHLSLRowMajorPacking,
bool isLastElement,
bool forcePadding)
{
if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
{
if (forcePadding)
{
const GLenum glType = GLVariableType(type);
const int numComponents = gl::VariableComponentCount(glType);
if (isLastElement || (numComponents >= 4))
{
// If this structure will be used as HLSL StructuredBuffer member's type, in
// order to follow the std140 rules, add padding at the end of the structure
// if necessary. Or if the current element straddles a vec4 boundary, add
// padding to round up the base offset of the next element to the base
// alignment of a vec4.
TString forcePaddingStr;
const int paddingCount = GetElementPadding(mElementIndex, 4);
for (int paddingIndex = 0; paddingIndex < paddingCount; paddingIndex++)
{
forcePaddingStr += " float pad_" + next() + ";\n";
}
mElementIndex = 0;
return forcePaddingStr;
}
}
return "";
}
int numComponents = 0;
const TStructure *structure = type.getStruct();
if (type.isMatrix())
{
// This method can also be called from structureString, which does not use layout
// qualifiers.
// Thus, use the method parameter for determining the matrix packing.
//
// Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
// wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
//
const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
const GLenum glType = GLVariableType(type);
numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
}
else if (structure)
{
const TString &structName =
QualifiedStructNameString(*structure, useHLSLRowMajorPacking, true, false);
numComponents = mStructElementIndexes->find(structName)->second;
if (numComponents == 0)
{
return "";
}
}
else
{
const GLenum glType = GLVariableType(type);
numComponents = gl::VariableComponentCount(glType);
}
TString padding;
for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
{
padding += " float pad_" + next() + ";\n";
}
return padding;
}
StructureHLSL::StructureHLSL() : mUniquePaddingCounter(0) {}
Std140PaddingHelper StructureHLSL::getPaddingHelper()
{
return Std140PaddingHelper(mStd140StructElementIndexes, &mUniquePaddingCounter);
}
TString StructureHLSL::defineQualified(const TStructure &structure,
bool useHLSLRowMajorPacking,
bool useStd140Packing,
bool forcePadding)
{
if (useStd140Packing)
{
Std140PaddingHelper padHelper = getPaddingHelper();
return Define(structure, useHLSLRowMajorPacking, useStd140Packing, forcePadding,
&padHelper);
}
else
{
return Define(structure, useHLSLRowMajorPacking, useStd140Packing, false, nullptr);
}
}
TString StructureHLSL::defineNameless(const TStructure &structure)
{
return Define(structure, false, false, false, nullptr);
}
StructureHLSL::DefinedStructs::iterator StructureHLSL::defineVariants(const TStructure &structure,
const TString &name)
{
ASSERT(mDefinedStructs.find(name) == mDefinedStructs.end());
for (const TField *field : structure.fields())
{
const TType *fieldType = field->type();
if (fieldType->getBasicType() == EbtStruct)
{
ensureStructDefined(*fieldType->getStruct());
}
}
DefinedStructs::iterator addedStruct =
mDefinedStructs.insert(std::make_pair(name, new TStructProperties())).first;
// Add element index
storeStd140ElementIndex(structure, false);
storeStd140ElementIndex(structure, true);
const TString &structString = defineQualified(structure, false, false, false);
ASSERT(std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) ==
mStructDeclarations.end());
// Add row-major packed struct for interface blocks
TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
defineQualified(structure, true, false, false) +
"#pragma pack_matrix(column_major)\n";
TString std140String = defineQualified(structure, false, true, false);
TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
defineQualified(structure, true, true, false) +
"#pragma pack_matrix(column_major)\n";
// Must force to pad the structure's elements for StructuredBuffer's element type, if qualifier
// of structure is std140.
TString std140ForcePaddingString = defineQualified(structure, false, true, true);
TString std140RowMajorForcePaddingString = "#pragma pack_matrix(row_major)\n" +
defineQualified(structure, true, true, true) +
"#pragma pack_matrix(column_major)\n";
mStructDeclarations.push_back(structString);
mStructDeclarations.push_back(rowMajorString);
mStructDeclarations.push_back(std140String);
mStructDeclarations.push_back(std140RowMajorString);
mStructDeclarations.push_back(std140ForcePaddingString);
mStructDeclarations.push_back(std140RowMajorForcePaddingString);
return addedStruct;
}
void StructureHLSL::ensureStructDefined(const TStructure &structure)
{
const TString name = StructNameString(structure);
if (name == "")
{
return; // Nameless structures are not defined
}
if (mDefinedStructs.find(name) == mDefinedStructs.end())
{
defineVariants(structure, name);
}
}
TString StructureHLSL::addStructConstructor(const TStructure &structure)
{
const TString name = StructNameString(structure);
if (name == "")
{
return TString(); // Nameless structures don't have constructors
}
auto definedStruct = mDefinedStructs.find(name);
if (definedStruct == mDefinedStructs.end())
{
definedStruct = defineVariants(structure, name);
}
const TString constructorFunctionName = TString(name) + "_ctor";
TString *constructor = &definedStruct->second->constructor;
if (!constructor->empty())
{
return constructorFunctionName; // Already added
}
*constructor += name + " " + constructorFunctionName + "(";
std::vector<TType> ctorParameters;
const TFieldList &fields = structure.fields();
for (const TField *field : fields)
{
const TType *fieldType = field->type();
if (!IsSampler(fieldType->getBasicType()))
{
ctorParameters.push_back(*fieldType);
}
}
// Structs that have sampler members should not have constructor calls, and otherwise structs
// are guaranteed to be non-empty by the grammar. Structs can't contain empty declarations
// either.
ASSERT(!ctorParameters.empty());
*constructor += WriteParameterList(ctorParameters);
*constructor +=
")\n"
"{\n"
" " +
name + " structure = { ";
for (size_t parameterIndex = 0u; parameterIndex < ctorParameters.size(); ++parameterIndex)
{
*constructor += "x" + str(parameterIndex);
if (parameterIndex < ctorParameters.size() - 1u)
{
*constructor += ", ";
}
}
*constructor +=
"};\n"
" return structure;\n"
"}\n";
return constructorFunctionName;
}
TString StructureHLSL::addBuiltInConstructor(const TType &type, const TIntermSequence *parameters)
{
ASSERT(!type.isArray());
ASSERT(type.getStruct() == nullptr);
ASSERT(parameters);
TType ctorType = type;
ctorType.setPrecision(EbpHigh);
ctorType.setQualifier(EvqTemporary);
const TString constructorFunctionName =
TString(type.getBuiltInTypeNameString()) + "_ctor" + DisambiguateFunctionName(parameters);
TString constructor = TypeString(ctorType) + " " + constructorFunctionName + "(";
std::vector<TType> ctorParameters;
for (auto parameter : *parameters)
{
const TType ¶mType = parameter->getAsTyped()->getType();
ASSERT(!paramType.isArray());
ctorParameters.push_back(paramType);
}
constructor += WriteParameterList(ctorParameters);
constructor +=
")\n"
"{\n"
" return " +
TypeString(ctorType) + "(";
if (ctorType.isMatrix() && ctorParameters.size() == 1)
{
uint8_t rows = ctorType.getRows();
uint8_t cols = ctorType.getCols();
const TType ¶meter = ctorParameters[0];
if (parameter.isScalar())
{
for (uint8_t col = 0; col < cols; col++)
{
for (uint8_t row = 0; row < rows; row++)
{
constructor += TString((row == col) ? "x0" : "0.0");
if (row < rows - 1 || col < cols - 1)
{
constructor += ", ";
}
}
}
}
else if (parameter.isMatrix())
{
for (uint8_t col = 0; col < cols; col++)
{
for (uint8_t row = 0; row < rows; row++)
{
if (row < parameter.getRows() && col < parameter.getCols())
{
constructor += TString("x0") + "[" + str(col) + "][" + str(row) + "]";
}
else
{
constructor += TString((row == col) ? "1.0" : "0.0");
}
if (row < rows - 1 || col < cols - 1)
{
constructor += ", ";
}
}
}
}
else
{
ASSERT(rows == 2 && cols == 2 && parameter.isVector() &&
parameter.getNominalSize() == 4);
constructor += "x0";
}
}
else
{
size_t remainingComponents = ctorType.getObjectSize();
size_t parameterIndex = 0;
while (remainingComponents > 0)
{
const TType ¶meter = ctorParameters[parameterIndex];
const size_t parameterSize = parameter.getObjectSize();
bool moreParameters = parameterIndex + 1 < ctorParameters.size();
constructor += "x" + str(parameterIndex);
if (parameter.isScalar())
{
remainingComponents -= parameter.getObjectSize();
}
else if (parameter.isVector())
{
if (remainingComponents == parameterSize || moreParameters)
{
ASSERT(parameterSize <= remainingComponents);
remainingComponents -= parameterSize;
}
else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
{
switch (remainingComponents)
{
case 1:
constructor += ".x";
break;
case 2:
constructor += ".xy";
break;
case 3:
constructor += ".xyz";
break;
case 4:
constructor += ".xyzw";
break;
default:
UNREACHABLE();
}
remainingComponents = 0;
}
else
UNREACHABLE();
}
else if (parameter.isMatrix())
{
uint8_t column = 0;
while (remainingComponents > 0 && column < parameter.getCols())
{
constructor += "[" + str(column) + "]";
if (remainingComponents < static_cast<size_t>(parameter.getRows()))
{
switch (remainingComponents)
{
case 1:
constructor += ".x";
break;
case 2:
constructor += ".xy";
break;
case 3:
constructor += ".xyz";
break;
default:
UNREACHABLE();
}
remainingComponents = 0;
}
else
{
remainingComponents -= parameter.getRows();
if (remainingComponents > 0)
{
constructor += ", x" + str(parameterIndex);
}
}
column++;
}
}
else
{
UNREACHABLE();
}
if (moreParameters)
{
parameterIndex++;
}
if (remainingComponents)
{
constructor += ", ";
}
}
}
constructor +=
");\n"
"}\n";
mBuiltInConstructors.insert(constructor);
return constructorFunctionName;
}
std::string StructureHLSL::structsHeader() const
{
TInfoSinkBase out;
for (auto &declaration : mStructDeclarations)
{
out << declaration;
}
for (auto &structure : mDefinedStructs)
{
out << structure.second->constructor;
}
for (auto &constructor : mBuiltInConstructors)
{
out << constructor;
}
return out.str();
}
void StructureHLSL::storeStd140ElementIndex(const TStructure &structure,
bool useHLSLRowMajorPacking)
{
Std140PaddingHelper padHelper = getPaddingHelper();
const TFieldList &fields = structure.fields();
for (const TField *field : fields)
{
padHelper.prePadding(*field->type(), false);
}
// Add remaining element index to the global map, for use with nested structs in standard
// layouts
const TString &structName =
QualifiedStructNameString(structure, useHLSLRowMajorPacking, true, false);
mStd140StructElementIndexes[structName] = padHelper.elementIndex();
}
} // namespace sh