Hash :
b7582680
Author :
Date :
2025-02-24T18:04:32
Reland: GL: Allow untranslated shaders to pass through on GLES Add an EGL extension EGL_ANGLE_create_context_passthrough_shaders which uses the NULL translator and passes the original shader to the driver. The parser is still used for shader reflection. Always enable the null compiler backend. It has almost no binary size cost and is now potentially used when the null ANGLE backend is not enabled. Bug: angleproject:398857482 Change-Id: Id528189ccbbacb1c444eacb151baadfda9fcc04b Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6488609 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: 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 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
//
// 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.
//
// mtl_msl_utils.h: Utilities to manipulate MSL.
//
#import <Foundation/Foundation.h>
#include <variant>
#include "common/string_utils.h"
#include "common/utilities.h"
#include "compiler/translator/Name.h"
#include "compiler/translator/msl/TranslatorMSL.h"
#include "libANGLE/renderer/metal/ContextMtl.h"
#include "libANGLE/renderer/metal/ShaderMtl.h"
#include "libANGLE/renderer/metal/mtl_msl_utils.h"
namespace rx
{
namespace
{
constexpr char kXfbBindingsMarker[] = "@@XFB-Bindings@@";
constexpr char kXfbOutMarker[] = "ANGLE_@@XFB-OUT@@";
constexpr char kUserDefinedNamePrefix[] = "_u"; // Defined in GLSLANG/ShaderLang.h
constexpr char kAttribBindingsMarker[] = "@@Attrib-Bindings@@\n";
std::string GetXfbBufferNameMtl(const uint32_t bufferIndex)
{
return "xfbBuffer" + Str(bufferIndex);
}
// Name format needs to match sh::Name.
struct UserDefinedNameExpr
{
std::string name;
};
std::ostream &operator<<(std::ostream &stream, const UserDefinedNameExpr &expr)
{
return stream << kUserDefinedNamePrefix << expr.name;
}
struct UserDefinedNameComponentExpr
{
UserDefinedNameExpr name;
const int component;
};
std::ostream &operator<<(std::ostream &stream, const UserDefinedNameComponentExpr &expr)
{
return stream << expr.name << '[' << expr.component << ']';
}
struct InternalNameExpr
{
std::string name;
};
std::ostream &operator<<(std::ostream &stream, const InternalNameExpr &expr)
{
return stream << sh::kAngleInternalPrefix << '_' << expr.name;
}
struct InternalNameComponentExpr
{
InternalNameExpr name;
const int component;
};
std::ostream &operator<<(std::ostream &stream, const InternalNameComponentExpr &expr)
{
return stream << expr.name << '_' << expr.component;
}
// ModifyStructs phase forwarded a single-component user-defined name or created a new AngleInternal
// field name to support multi-component fields as multiple single-component fields.
std::variant<UserDefinedNameExpr, InternalNameComponentExpr>
ResolveModifiedAttributeName(const std::string &name, int registerIndex, int registerCount)
{
if (registerCount < 2)
{
return UserDefinedNameExpr{name};
}
return InternalNameComponentExpr{InternalNameExpr{name}, registerIndex};
}
std::variant<UserDefinedNameExpr, InternalNameComponentExpr>
ResolveModifiedOutputName(const std::string &name, int component, int componentCount)
{
if (componentCount == 0)
{
return UserDefinedNameExpr{name};
}
return InternalNameComponentExpr{InternalNameExpr{name}, component};
}
// Accessing unmodified structs uses user-defined name, business as usual.
std::variant<UserDefinedNameExpr, UserDefinedNameComponentExpr>
ResolveUserDefinedName(const std::string &name, int component, int componentCount)
{
if (componentCount == 0)
{
return UserDefinedNameExpr{name};
}
return UserDefinedNameComponentExpr{{name}, component};
}
template <class T>
struct ApplyOStream
{
const T &value;
};
template <class T>
ApplyOStream(T) -> ApplyOStream<T>;
template <class T>
std::ostream &operator<<(std::ostream &stream, ApplyOStream<T> sv)
{
stream << sv.value;
return stream;
}
template <class... Ts>
std::ostream &operator<<(std::ostream &stream, ApplyOStream<std::variant<Ts...>> sv)
{
std::visit([&stream](auto &&v) { stream << ApplyOStream{v}; }, sv.value);
return stream;
}
} // namespace
namespace mtl
{
void TranslatedShaderInfo::reset()
{
metalShaderSource = nullptr;
metalLibrary = nil;
hasUBOArgumentBuffer = false;
hasIsnanOrIsinf = false;
hasInvariant = false;
for (mtl::SamplerBinding &binding : actualSamplerBindings)
{
binding.textureBinding = mtl::kMaxShaderSamplers;
binding.samplerBinding = 0;
}
for (int &rwTextureBinding : actualImageBindings)
{
rwTextureBinding = -1;
}
for (uint32_t &binding : actualUBOBindings)
{
binding = mtl::kMaxShaderBuffers;
}
for (uint32_t &binding : actualXFBBindings)
{
binding = mtl::kMaxShaderBuffers;
}
}
// Original mapping of front end from sampler name to multiple sampler slots (in form of
// slot:count pair)
using OriginalSamplerBindingMap =
std::unordered_map<std::string, std::vector<std::pair<uint32_t, uint32_t>>>;
bool MappedSamplerNameNeedsUserDefinedPrefix(const std::string &originalName)
{
return originalName.find('.') == std::string::npos;
}
static std::string MSLGetMappedSamplerName(const std::string &originalName)
{
std::string samplerName = originalName;
// Samplers in structs are extracted.
std::replace(samplerName.begin(), samplerName.end(), '.', '_');
// Remove array elements
auto out = samplerName.begin();
for (auto in = samplerName.begin(); in != samplerName.end(); in++)
{
if (*in == '[')
{
while (*in != ']')
{
in++;
ASSERT(in != samplerName.end());
}
}
else
{
*out++ = *in;
}
}
samplerName.erase(out, samplerName.end());
if (MappedSamplerNameNeedsUserDefinedPrefix(originalName))
{
samplerName = kUserDefinedNamePrefix + samplerName;
}
return samplerName;
}
void MSLGetShaderSource(const gl::ProgramState &programState,
const gl::ProgramLinkedResources &resources,
gl::ShaderMap<std::string> *shaderSourcesOut)
{
for (const gl::ShaderType shaderType : gl::AllShaderTypes())
{
const gl::SharedCompiledShaderState &glShader = programState.getAttachedShader(shaderType);
(*shaderSourcesOut)[shaderType] = glShader ? glShader->translatedSource : "";
}
}
void GetAssignedSamplerBindings(const sh::TranslatorMetalReflection *reflection,
const OriginalSamplerBindingMap &originalBindings,
std::unordered_set<std::string> &structSamplers,
std::array<SamplerBinding, mtl::kMaxGLSamplerBindings> *bindings)
{
for (auto &sampler : reflection->getSamplerBindings())
{
const std::string &name = sampler.first;
const uint32_t actualSamplerSlot = (uint32_t)reflection->getSamplerBinding(name);
const uint32_t actualTextureSlot = (uint32_t)reflection->getTextureBinding(name);
// Assign sequential index for subsequent array elements
const bool structSampler = structSamplers.find(name) != structSamplers.end();
const std::string mappedName =
structSampler ? name : MSLGetMappedSamplerName(kUserDefinedNamePrefix + name);
auto original = originalBindings.find(mappedName);
if (original != originalBindings.end())
{
const std::vector<std::pair<uint32_t, uint32_t>> &resOrignalBindings =
originalBindings.at(mappedName);
uint32_t currentTextureSlot = actualTextureSlot;
uint32_t currentSamplerSlot = actualSamplerSlot;
for (const std::pair<uint32_t, uint32_t> &originalBindingRange : resOrignalBindings)
{
SamplerBinding &actualBinding = bindings->at(originalBindingRange.first);
actualBinding.textureBinding = currentTextureSlot;
actualBinding.samplerBinding = currentSamplerSlot;
currentTextureSlot += originalBindingRange.second;
currentSamplerSlot += originalBindingRange.second;
}
}
}
}
std::string UpdateAliasedShaderAttributes(std::string shaderSourceIn,
const gl::ProgramExecutable &executable)
{
// Cache max number of components for each attribute location
std::array<uint8_t, gl::MAX_VERTEX_ATTRIBS> maxComponents{};
for (auto &attribute : executable.getProgramInputs())
{
const int location = attribute.getLocation();
const int registers = gl::VariableRegisterCount(attribute.getType());
const uint8_t components = gl::VariableColumnCount(attribute.getType());
for (int i = 0; i < registers; ++i)
{
ASSERT(location + i < static_cast<int>(maxComponents.size()));
maxComponents[location + i] = std::max(maxComponents[location + i], components);
}
}
// Define aliased names pointing to real attributes with swizzles as needed
std::ostringstream stream;
for (auto &attribute : executable.getProgramInputs())
{
const int location = attribute.getLocation();
const int registers = gl::VariableRegisterCount(attribute.getType());
const uint8_t components = gl::VariableColumnCount(attribute.getType());
for (int i = 0; i < registers; i++)
{
stream << "#define ANGLE_ALIASED_"
<< ApplyOStream{ResolveModifiedAttributeName(attribute.name, i, registers)}
<< " ANGLE_modified.ANGLE_ATTRIBUTE_" << (location + i);
if (components != maxComponents[location + i])
{
ASSERT(components < maxComponents[location + i]);
switch (components)
{
case 1:
stream << ".x";
break;
case 2:
stream << ".xy";
break;
case 3:
stream << ".xyz";
break;
}
}
stream << "\n";
}
}
// Declare actual MSL attributes
for (size_t i : executable.getActiveAttribLocationsMask())
{
stream << " float";
if (maxComponents[i] > 1)
{
stream << static_cast<int>(maxComponents[i]);
}
stream << " ANGLE_ATTRIBUTE_" << i << "[[attribute(" << i << ")]];\n";
}
std::string outputSource = shaderSourceIn;
size_t markerFound = outputSource.find(kAttribBindingsMarker);
ASSERT(markerFound != std::string::npos);
outputSource.replace(markerFound, angle::ConstStrLen(kAttribBindingsMarker), stream.str());
return outputSource;
}
std::string updateShaderAttributes(std::string shaderSourceIn,
const gl::ProgramExecutable &executable)
{
// Build string to attrib map.
const auto &programAttributes = executable.getProgramInputs();
std::ostringstream stream;
std::unordered_map<std::string, uint32_t> attributeBindings;
for (auto &attribute : programAttributes)
{
const int registers = gl::VariableRegisterCount(attribute.getType());
for (int i = 0; i < registers; i++)
{
stream.str("");
stream << ' '
<< ApplyOStream{ResolveModifiedAttributeName(attribute.name, i, registers)}
<< sh::kUnassignedAttributeString;
attributeBindings.insert({stream.str(), i + attribute.getLocation()});
}
}
// Rewrite attributes
std::string outputSource = shaderSourceIn;
for (auto it = attributeBindings.begin(); it != attributeBindings.end(); ++it)
{
std::size_t attribFound = outputSource.find(it->first);
if (attribFound != std::string::npos)
{
stream.str("");
stream << "[[attribute(" << it->second << ")]]";
outputSource = outputSource.replace(
attribFound + it->first.length() -
angle::ConstStrLen(sh::kUnassignedAttributeString),
angle::ConstStrLen(sh::kUnassignedAttributeString), stream.str());
}
}
return outputSource;
}
std::string UpdateFragmentShaderOutputs(std::string shaderSourceIn,
const gl::ProgramExecutable &executable,
bool defineAlpha0)
{
std::ostringstream stream;
std::string outputSource = shaderSourceIn;
const auto &outputVariables = executable.getOutputVariables();
// For alpha-to-coverage emulation, a reference to the alpha channel
// of color output 0 is needed. For ESSL 1.00, it is gl_FragColor or
// gl_FragData[0]; for ESSL 3.xx, it is a user-defined output.
std::string alphaOutputName;
auto assignLocations = [&](const std::vector<gl::VariableLocation> &locations, bool secondary) {
for (auto &outputLocation : locations)
{
if (!outputLocation.used())
{
continue;
}
const int index = outputLocation.arrayIndex;
const gl::ProgramOutput &outputVar = outputVariables[outputLocation.index];
ASSERT(outputVar.pod.location >= 0);
const int location = outputVar.pod.location + index;
const int arraySize = outputVar.getOutermostArraySize();
stream.str("");
stream << ApplyOStream{ResolveModifiedOutputName(outputVar.name, index, arraySize)}
<< " [[" << sh::kUnassignedFragmentOutputString;
const std::string placeholder(stream.str());
size_t outputFound = outputSource.find(placeholder);
if (outputFound != std::string::npos)
{
stream.str("");
stream << "color(" << location << (secondary ? "), index(1)" : ")");
outputSource = outputSource.replace(
outputFound + placeholder.length() -
angle::ConstStrLen(sh::kUnassignedFragmentOutputString),
angle::ConstStrLen(sh::kUnassignedFragmentOutputString), stream.str());
}
if (defineAlpha0 && location == 0 && !secondary && outputVar.pod.type == GL_FLOAT_VEC4)
{
ASSERT(index == 0);
ASSERT(alphaOutputName.empty());
std::ostringstream nameStream;
nameStream << "ANGLE_fragmentOut."
<< ApplyOStream{ResolveUserDefinedName(outputVar.name, index, arraySize)}
<< ".a";
alphaOutputName = nameStream.str();
}
}
};
assignLocations(executable.getOutputLocations(), false);
assignLocations(executable.getSecondaryOutputLocations(), true);
if (defineAlpha0)
{
// Locations are empty for ESSL 1.00 shaders, try built-in outputs
if (alphaOutputName.empty())
{
for (auto &v : outputVariables)
{
if (v.name == "gl_FragColor")
{
alphaOutputName = "ANGLE_fragmentOut.gl_FragColor.a";
break;
}
else if (v.name == "gl_FragData")
{
alphaOutputName = "ANGLE_fragmentOut.ANGLE_gl_FragData_0.a";
break;
}
}
}
// Set a value used for alpha-to-coverage emulation
const std::string alphaPlaceholder("#define ANGLE_ALPHA0");
size_t alphaFound = outputSource.find(alphaPlaceholder);
ASSERT(alphaFound != std::string::npos);
std::ostringstream alphaStream;
alphaStream << alphaPlaceholder << " ";
alphaStream << (alphaOutputName.empty() ? "1.0" : alphaOutputName);
outputSource =
outputSource.replace(alphaFound, alphaPlaceholder.length(), alphaStream.str());
}
return outputSource;
}
std::string SubstituteTransformFeedbackMarkers(const std::string &originalSource,
const std::string &xfbBindings,
const std::string &xfbOut)
{
const size_t xfbBindingsMarkerStart = originalSource.find(kXfbBindingsMarker);
bool hasBindingsMarker = xfbBindingsMarkerStart != std::string::npos;
const size_t xfbBindingsMarkerEnd =
xfbBindingsMarkerStart + angle::ConstStrLen(kXfbBindingsMarker);
const size_t xfbOutMarkerStart = originalSource.find(kXfbOutMarker, xfbBindingsMarkerStart);
bool hasOutMarker = xfbOutMarkerStart != std::string::npos;
const size_t xfbOutMarkerEnd = xfbOutMarkerStart + angle::ConstStrLen(kXfbOutMarker);
// The shader is the following form:
//
// ..part1..
// @@ XFB-BINDINGS @@
// ..part2..
// @@ XFB-OUT @@;
// ..part3..
//
// Construct the string by concatenating these five pieces, replacing the markers with the given
// values.
std::string result;
if (hasBindingsMarker && hasOutMarker)
{
result.append(&originalSource[0], &originalSource[xfbBindingsMarkerStart]);
result.append(xfbBindings);
result.append(&originalSource[xfbBindingsMarkerEnd], &originalSource[xfbOutMarkerStart]);
result.append(xfbOut);
result.append(&originalSource[xfbOutMarkerEnd], &originalSource[originalSource.size()]);
return result;
}
return originalSource;
}
std::string GenerateTransformFeedbackVaryingOutput(const gl::TransformFeedbackVarying &varying,
const gl::UniformTypeInfo &info,
size_t strideBytes,
size_t offset,
const std::string &bufferIndex)
{
std::ostringstream result;
ASSERT(strideBytes % 4 == 0);
size_t stride = strideBytes / 4;
const size_t arrayIndexStart = varying.arrayIndex == GL_INVALID_INDEX ? 0 : varying.arrayIndex;
const size_t arrayIndexEnd = arrayIndexStart + varying.size();
for (size_t arrayIndex = arrayIndexStart; arrayIndex < arrayIndexEnd; ++arrayIndex)
{
for (int col = 0; col < info.columnCount; ++col)
{
for (int row = 0; row < info.rowCount; ++row)
{
result << " ";
result << "ANGLE_" << "xfbBuffer" << bufferIndex << "[" << "ANGLE_"
<< std::string(sh::kUniformsVar) << ".ANGLE_xfbBufferOffsets[" << bufferIndex
<< "] + (ANGLE_vertexIDMetal + (ANGLE_instanceIdMod - ANGLE_baseInstance) * "
<< "ANGLE_" << std::string(sh::kUniformsVar)
<< ".ANGLE_xfbVerticesPerInstance) * " << stride << " + " << offset
<< "] = " << "as_type<float>" << "(" << "ANGLE_vertexOut.";
if (!varying.isBuiltIn())
{
result << kUserDefinedNamePrefix;
}
result << varying.name;
if (varying.isArray())
{
result << "[" << arrayIndex << "]";
}
if (info.columnCount > 1)
{
result << "[" << col << "]";
}
if (info.rowCount > 1)
{
result << "[" << row << "]";
}
result << ");\n";
++offset;
}
}
}
return result.str();
}
void GenerateTransformFeedbackEmulationOutputs(
const gl::ProgramExecutable &executable,
std::string *vertexShader,
std::array<uint32_t, kMaxShaderXFBs> *xfbBindingRemapOut)
{
const std::vector<gl::TransformFeedbackVarying> &varyings =
executable.getLinkedTransformFeedbackVaryings();
const std::vector<GLsizei> &bufferStrides = executable.getTransformFeedbackStrides();
const bool isInterleaved =
executable.getTransformFeedbackBufferMode() == GL_INTERLEAVED_ATTRIBS;
const size_t bufferCount = isInterleaved ? 1 : varyings.size();
std::vector<std::string> xfbIndices(bufferCount);
std::string xfbBindings;
for (uint32_t bufferIndex = 0; bufferIndex < bufferCount; ++bufferIndex)
{
const std::string xfbBinding = Str(0);
xfbIndices[bufferIndex] = Str(bufferIndex);
std::string bufferName = GetXfbBufferNameMtl(bufferIndex);
xfbBindings += ", ";
// TODO: offset from last used buffer binding from front end
// XFB buffer is allocated slot starting from last discrete Metal buffer slot.
uint32_t bindingPoint = kMaxShaderBuffers - 1 - bufferIndex;
xfbBindingRemapOut->at(bufferIndex) = bindingPoint;
xfbBindings +=
"device float* ANGLE_" + bufferName + " [[buffer(" + Str(bindingPoint) + ")]]";
}
std::string xfbOut = "#if TRANSFORM_FEEDBACK_ENABLED\n {\n";
size_t outputOffset = 0;
for (size_t varyingIndex = 0; varyingIndex < varyings.size(); ++varyingIndex)
{
const size_t bufferIndex = isInterleaved ? 0 : varyingIndex;
const gl::TransformFeedbackVarying &varying = varyings[varyingIndex];
// For every varying, output to the respective buffer packed. If interleaved, the output is
// always to the same buffer, but at different offsets.
const gl::UniformTypeInfo &info = gl::GetUniformTypeInfo(varying.type);
xfbOut += GenerateTransformFeedbackVaryingOutput(varying, info, bufferStrides[bufferIndex],
outputOffset, xfbIndices[bufferIndex]);
if (isInterleaved)
{
outputOffset += info.columnCount * info.rowCount * varying.size();
}
}
xfbOut += " }\n#endif\n";
*vertexShader = SubstituteTransformFeedbackMarkers(*vertexShader, xfbBindings, xfbOut);
}
angle::Result MTLGetMSL(const angle::FeaturesMtl &features,
const gl::ProgramExecutable &executable,
const gl::ShaderMap<std::string> &shaderSources,
const gl::ShaderMap<SharedCompiledShaderStateMtl> &shadersState,
gl::ShaderMap<TranslatedShaderInfo> *mslShaderInfoOut)
{
// Retrieve original uniform buffer bindings generated by front end. We will need to do a remap.
std::unordered_map<std::string, uint32_t> uboOriginalBindings;
const std::vector<gl::InterfaceBlock> &blocks = executable.getUniformBlocks();
for (uint32_t bufferIdx = 0; bufferIdx < blocks.size(); ++bufferIdx)
{
const gl::InterfaceBlock &block = blocks[bufferIdx];
if (!uboOriginalBindings.count(block.name))
{
uboOriginalBindings[block.name] = bufferIdx;
}
}
// Retrieve original sampler bindings produced by front end.
OriginalSamplerBindingMap originalSamplerBindings;
const std::vector<gl::SamplerBinding> &samplerBindings = executable.getSamplerBindings();
std::unordered_set<std::string> structSamplers = {};
for (uint32_t textureIndex = 0; textureIndex < samplerBindings.size(); ++textureIndex)
{
const gl::SamplerBinding &samplerBinding = samplerBindings[textureIndex];
uint32_t uniformIndex = executable.getUniformIndexFromSamplerIndex(textureIndex);
const std::string &uniformName = executable.getUniformNames()[uniformIndex];
const std::string &uniformMappedName = executable.getUniformMappedNames()[uniformIndex];
bool isSamplerInStruct = uniformName.find('.') != std::string::npos;
std::string mappedSamplerName = isSamplerInStruct
? MSLGetMappedSamplerName(uniformName)
: MSLGetMappedSamplerName(uniformMappedName);
// These need to be prefixed later seperately
if (isSamplerInStruct)
structSamplers.insert(mappedSamplerName);
originalSamplerBindings[mappedSamplerName].push_back(
{textureIndex, static_cast<uint32_t>(samplerBinding.textureUnitsCount)});
}
for (gl::ShaderType type : {gl::ShaderType::Vertex, gl::ShaderType::Fragment})
{
std::string source;
if (type == gl::ShaderType::Vertex)
{
source =
shadersState[gl::ShaderType::Vertex]->translatorMetalReflection.hasAttributeAliasing
? UpdateAliasedShaderAttributes(shaderSources[type], executable)
: updateShaderAttributes(shaderSources[type], executable);
// Write transform feedback output code.
if (!source.empty())
{
if (executable.getLinkedTransformFeedbackVaryings().empty())
{
source = SubstituteTransformFeedbackMarkers(source, "", "");
}
else
{
GenerateTransformFeedbackEmulationOutputs(
executable, &source, &(*mslShaderInfoOut)[type].actualXFBBindings);
}
}
}
else
{
ASSERT(type == gl::ShaderType::Fragment);
const bool defineAlpha0 = features.emulateAlphaToCoverage.enabled ||
features.generateShareableShaders.enabled;
source = UpdateFragmentShaderOutputs(shaderSources[type], executable, defineAlpha0);
}
(*mslShaderInfoOut)[type].metalShaderSource =
std::make_shared<const std::string>(std::move(source));
const sh::TranslatorMetalReflection *reflection =
&shadersState[type]->translatorMetalReflection;
if (reflection->hasUBOs)
{
(*mslShaderInfoOut)[type].hasUBOArgumentBuffer = true;
for (auto &uboBinding : reflection->getUniformBufferBindings())
{
const std::string &uboName = uboBinding.first;
const sh::UBOBindingInfo &bindInfo = uboBinding.second;
const uint32_t uboBindIndex = static_cast<uint32_t>(bindInfo.bindIndex);
const uint32_t uboArraySize = static_cast<uint32_t>(bindInfo.arraySize);
const uint32_t originalBinding = uboOriginalBindings.at(uboName);
uint32_t currentSlot = static_cast<uint>(uboBindIndex);
for (uint32_t i = 0; i < uboArraySize; ++i)
{
// Use consecutive slot for member in array
(*mslShaderInfoOut)[type].actualUBOBindings[originalBinding + i] =
currentSlot + i;
}
}
}
// Retrieve automatic texture slot assignments
if (originalSamplerBindings.size() > 0)
{
GetAssignedSamplerBindings(reflection, originalSamplerBindings, structSamplers,
&mslShaderInfoOut->at(type).actualSamplerBindings);
}
for (uint32_t i = 0; i < kMaxShaderImages; ++i)
{
mslShaderInfoOut->at(type).actualImageBindings[i] = reflection->getRWTextureBinding(i);
}
(*mslShaderInfoOut)[type].hasIsnanOrIsinf = reflection->hasIsnanOrIsinf;
(*mslShaderInfoOut)[type].hasInvariant = reflection->hasInvariance;
}
return angle::Result::Continue;
}
uint MslGetShaderShadowCompareMode(GLenum mode, GLenum func)
{
// See SpirvToMslCompiler::emit_header()
if (mode == GL_NONE)
{
return 0;
}
else
{
switch (func)
{
case GL_LESS:
return 1;
case GL_LEQUAL:
return 2;
case GL_GREATER:
return 3;
case GL_GEQUAL:
return 4;
case GL_NEVER:
return 5;
case GL_ALWAYS:
return 6;
case GL_EQUAL:
return 7;
case GL_NOTEQUAL:
return 8;
default:
UNREACHABLE();
return 1;
}
}
}
} // namespace mtl
} // namespace rx