Hash :
be5e767e
Author :
Date :
2023-02-01T11:03:26
dump_shader_source: Also dump program links. When the dump_shader_source is enabled, dump a small file for each program link containing which shaders were attached to the program. This allows us to build perf tests knowing which shaders can link together. Bug: angleproject:7760, chromium:1385510 Change-Id: I0713e9f04cc157c3a3eeeec6d597db27b08575d1 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4214011 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Cody Northrop <cnorthrop@google.com> 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 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
//
// Copyright 2002 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.
//
// Shader.cpp: Implements the gl::Shader class and its derived classes
// VertexShader and FragmentShader. Implements GL shader objects and related
// functionality. [OpenGL ES 2.0.24] section 2.10 page 24 and section 3.8 page 84.
#include "libANGLE/Shader.h"
#include <functional>
#include <sstream>
#include "GLSLANG/ShaderLang.h"
#include "common/angle_version_info.h"
#include "common/string_utils.h"
#include "common/system_utils.h"
#include "common/utilities.h"
#include "libANGLE/Caps.h"
#include "libANGLE/Compiler.h"
#include "libANGLE/Constants.h"
#include "libANGLE/Context.h"
#include "libANGLE/Display.h"
#include "libANGLE/MemoryShaderCache.h"
#include "libANGLE/Program.h"
#include "libANGLE/ResourceManager.h"
#include "libANGLE/renderer/GLImplFactory.h"
#include "libANGLE/renderer/ShaderImpl.h"
#include "platform/FrontendFeatures_autogen.h"
namespace gl
{
namespace
{
constexpr uint32_t kShaderCacheIdentifier = 0x12345678;
// Environment variable (and associated Android property) for the path to read and write shader
// dumps
constexpr char kShaderDumpPathVarName[] = "ANGLE_SHADER_DUMP_PATH";
constexpr char kEShaderDumpPathPropertyName[] = "debug.angle.shader_dump_path";
size_t ComputeShaderHash(const std::string &mergedSource)
{
return std::hash<std::string>{}(mergedSource);
}
std::string GetShaderDumpFilePath(size_t shaderHash)
{
std::stringstream path;
std::string shaderDumpDir = GetShaderDumpFileDirectory();
if (!shaderDumpDir.empty())
{
path << shaderDumpDir << "/";
}
path << shaderHash << ".essl";
return path.str();
}
} // anonymous namespace
const char *GetShaderTypeString(ShaderType type)
{
switch (type)
{
case ShaderType::Vertex:
return "VERTEX";
case ShaderType::Fragment:
return "FRAGMENT";
case ShaderType::Compute:
return "COMPUTE";
case ShaderType::Geometry:
return "GEOMETRY";
case ShaderType::TessControl:
return "TESS_CONTROL";
case ShaderType::TessEvaluation:
return "TESS_EVALUATION";
default:
UNREACHABLE();
return "";
}
}
std::string GetShaderDumpFileDirectory()
{
// Check the environment variable for the path to save and read shader dump files.
std::string environmentVariableDumpDir =
angle::GetAndSetEnvironmentVarOrUnCachedAndroidProperty(kShaderDumpPathVarName,
kEShaderDumpPathPropertyName);
if (!environmentVariableDumpDir.empty() && environmentVariableDumpDir.compare("0") != 0)
{
return environmentVariableDumpDir;
}
// Fall back to the temp dir. If that doesn't exist, use the current working directory.
return angle::GetTempDirectory().valueOr("");
}
std::string GetShaderDumpFileName(size_t shaderHash)
{
std::stringstream name;
name << shaderHash << ".essl";
return name.str();
}
class [[nodiscard]] ScopedExit final : angle::NonCopyable
{
public:
ScopedExit(std::function<void()> exit) : mExit(exit) {}
~ScopedExit() { mExit(); }
private:
std::function<void()> mExit;
};
struct Shader::CompilingState
{
std::shared_ptr<rx::WaitableCompileEvent> compileEvent;
ShCompilerInstance shCompilerInstance;
};
ShaderState::ShaderState(ShaderType shaderType) : mCompiledShaderState(shaderType) {}
ShaderState::~ShaderState() {}
Shader::Shader(ShaderProgramManager *manager,
rx::GLImplFactory *implFactory,
const gl::Limitations &rendererLimitations,
ShaderType type,
ShaderProgramID handle)
: mState(type),
mImplementation(implFactory->createShader(mState)),
mRendererLimitations(rendererLimitations),
mHandle(handle),
mType(type),
mRefCount(0),
mDeleteStatus(false),
mResourceManager(manager),
mCurrentMaxComputeWorkGroupInvocations(0u),
mMaxComputeSharedMemory(0u)
{
ASSERT(mImplementation);
}
void Shader::onDestroy(const gl::Context *context)
{
resolveCompile(context);
mImplementation->destroy();
mBoundCompiler.set(context, nullptr);
mImplementation.reset(nullptr);
delete this;
}
Shader::~Shader()
{
ASSERT(!mImplementation);
}
angle::Result Shader::setLabel(const Context *context, const std::string &label)
{
mState.mLabel = label;
if (mImplementation)
{
return mImplementation->onLabelUpdate(context);
}
return angle::Result::Continue;
}
const std::string &Shader::getLabel() const
{
return mState.mLabel;
}
ShaderProgramID Shader::getHandle() const
{
return mHandle;
}
void Shader::setSource(const Context *context,
GLsizei count,
const char *const *string,
const GLint *length)
{
std::ostringstream stream;
for (int i = 0; i < count; i++)
{
if (length == nullptr || length[i] < 0)
{
stream.write(string[i], strlen(string[i]));
}
else
{
stream.write(string[i], length[i]);
}
}
std::string source = stream.str();
// Compute the hash based on the original source before any substitutions
size_t sourceHash = ComputeShaderHash(source);
const angle::FrontendFeatures &frontendFeatures = context->getFrontendFeatures();
bool substitutedShader = false;
if (frontendFeatures.enableShaderSubstitution.enabled)
{
std::string subsitutionShaderPath = GetShaderDumpFilePath(sourceHash);
std::string substituteShader;
if (angle::ReadFileToString(subsitutionShaderPath, &substituteShader))
{
source = std::move(substituteShader);
substitutedShader = true;
INFO() << "Shader substitute found, loading from " << subsitutionShaderPath;
}
}
// Only dump shaders that have not been previously substituted. It would write the same data
// back to the file.
if (frontendFeatures.dumpShaderSource.enabled && !substitutedShader)
{
std::string dumpFile = GetShaderDumpFilePath(sourceHash);
writeFile(dumpFile.c_str(), source.c_str(), source.length());
INFO() << "Dumped shader source: " << dumpFile;
}
mState.mSource = std::move(source);
mState.mSourceHash = sourceHash;
}
int Shader::getInfoLogLength(const Context *context)
{
resolveCompile(context);
if (mInfoLog.empty())
{
return 0;
}
return (static_cast<int>(mInfoLog.length()) + 1);
}
void Shader::getInfoLog(const Context *context, GLsizei bufSize, GLsizei *length, char *infoLog)
{
resolveCompile(context);
int index = 0;
if (bufSize > 0)
{
index = std::min(bufSize - 1, static_cast<GLsizei>(mInfoLog.length()));
memcpy(infoLog, mInfoLog.c_str(), index);
infoLog[index] = '\0';
}
if (length)
{
*length = index;
}
}
int Shader::getSourceLength() const
{
return mState.mSource.empty() ? 0 : (static_cast<int>(mState.mSource.length()) + 1);
}
int Shader::getTranslatedSourceLength(const Context *context)
{
resolveCompile(context);
if (mState.getTranslatedSource().empty())
{
return 0;
}
return (static_cast<int>(mState.getTranslatedSource().length()) + 1);
}
int Shader::getTranslatedSourceWithDebugInfoLength(const Context *context)
{
resolveCompile(context);
const std::string &debugInfo = mImplementation->getDebugInfo();
if (debugInfo.empty())
{
return 0;
}
return (static_cast<int>(debugInfo.length()) + 1);
}
// static
void Shader::GetSourceImpl(const std::string &source,
GLsizei bufSize,
GLsizei *length,
char *buffer)
{
int index = 0;
if (bufSize > 0)
{
index = std::min(bufSize - 1, static_cast<GLsizei>(source.length()));
memcpy(buffer, source.c_str(), index);
buffer[index] = '\0';
}
if (length)
{
*length = index;
}
}
void Shader::getSource(GLsizei bufSize, GLsizei *length, char *buffer) const
{
GetSourceImpl(mState.mSource, bufSize, length, buffer);
}
void Shader::getTranslatedSource(const Context *context,
GLsizei bufSize,
GLsizei *length,
char *buffer)
{
GetSourceImpl(getTranslatedSource(context), bufSize, length, buffer);
}
const std::string &Shader::getTranslatedSource(const Context *context)
{
resolveCompile(context);
return mState.getTranslatedSource();
}
const sh::BinaryBlob &Shader::getCompiledBinary(const Context *context)
{
resolveCompile(context);
return mState.getCompiledBinary();
}
size_t Shader::getSourceHash() const
{
return mState.mSourceHash;
}
void Shader::getTranslatedSourceWithDebugInfo(const Context *context,
GLsizei bufSize,
GLsizei *length,
char *buffer)
{
resolveCompile(context);
const std::string &debugInfo = mImplementation->getDebugInfo();
GetSourceImpl(debugInfo, bufSize, length, buffer);
}
void Shader::compile(const Context *context)
{
resolveCompile(context);
mState.mCompiledShaderState.translatedSource.clear();
mState.mCompiledShaderState.compiledBinary.clear();
mInfoLog.clear();
mState.mCompiledShaderState.shaderVersion = 100;
mState.mCompiledShaderState.inputVaryings.clear();
mState.mCompiledShaderState.outputVaryings.clear();
mState.mCompiledShaderState.uniforms.clear();
mState.mCompiledShaderState.uniformBlocks.clear();
mState.mCompiledShaderState.shaderStorageBlocks.clear();
mState.mCompiledShaderState.activeAttributes.clear();
mState.mCompiledShaderState.activeOutputVariables.clear();
mState.mCompiledShaderState.numViews = -1;
mState.mCompiledShaderState.geometryShaderInputPrimitiveType.reset();
mState.mCompiledShaderState.geometryShaderOutputPrimitiveType.reset();
mState.mCompiledShaderState.geometryShaderMaxVertices.reset();
mState.mCompiledShaderState.geometryShaderInvocations = 1;
mState.mCompiledShaderState.tessControlShaderVertices = 0;
mState.mCompiledShaderState.tessGenMode = 0;
mState.mCompiledShaderState.tessGenSpacing = 0;
mState.mCompiledShaderState.tessGenVertexOrder = 0;
mState.mCompiledShaderState.tessGenPointMode = 0;
mState.mCompiledShaderState.advancedBlendEquations.reset();
mState.mCompiledShaderState.hasClipDistance = false;
mState.mCompiledShaderState.hasDiscard = false;
mState.mCompiledShaderState.enablesPerSampleShading = false;
mState.mCompiledShaderState.specConstUsageBits.reset();
mCurrentMaxComputeWorkGroupInvocations =
static_cast<GLuint>(context->getCaps().maxComputeWorkGroupInvocations);
mMaxComputeSharedMemory = context->getCaps().maxComputeSharedMemorySize;
ShCompileOptions options = {};
options.objectCode = true;
options.variables = true;
options.emulateGLDrawID = true;
// Add default options to WebGL shaders to prevent unexpected behavior during
// compilation.
if (context->isWebGL())
{
options.initGLPosition = true;
options.limitCallStackDepth = true;
options.limitExpressionComplexity = true;
options.enforcePackingRestrictions = true;
options.initSharedVariables = true;
}
else
{
// Per https://github.com/KhronosGroup/WebGL/pull/3278 gl_BaseVertex/gl_BaseInstance are
// removed from WebGL
options.emulateGLBaseVertexBaseInstance = true;
}
// Some targets (e.g. D3D11 Feature Level 9_3 and below) do not support non-constant loop
// indexes in fragment shaders. Shader compilation will fail. To provide a better error
// message we can instruct the compiler to pre-validate.
if (mRendererLimitations.shadersRequireIndexedLoopValidation)
{
options.validateLoopIndexing = true;
}
if (context->getFrontendFeatures().scalarizeVecAndMatConstructorArgs.enabled)
{
options.scalarizeVecAndMatConstructorArgs = true;
}
if (context->getFrontendFeatures().forceInitShaderVariables.enabled)
{
options.initOutputVariables = true;
options.initializeUninitializedLocals = true;
}
mBoundCompiler.set(context, context->getCompiler());
ASSERT(mBoundCompiler.get());
ShCompilerInstance compilerInstance = mBoundCompiler->getInstance(mType);
ShHandle compilerHandle = compilerInstance.getHandle();
ASSERT(compilerHandle);
// Find a shader in Blob Cache
setShaderKey(context, options, compilerInstance.getShaderOutputType(),
compilerInstance.getBuiltInResources());
ASSERT(!mShaderHash.empty());
MemoryShaderCache *shaderCache = context->getMemoryShaderCache();
if (shaderCache)
{
angle::Result cacheResult =
shaderCache->getShader(context, this, options, compilerInstance, mShaderHash);
if (cacheResult == angle::Result::Continue)
{
compilerInstance.destroy();
return;
}
}
// Cache load failed, fall through normal compiling.
mState.mCompileStatus = CompileStatus::COMPILE_REQUESTED;
mCompilingState.reset(new CompilingState());
mCompilingState->shCompilerInstance = std::move(compilerInstance);
mCompilingState->compileEvent =
mImplementation->compile(context, &(mCompilingState->shCompilerInstance), &options);
}
void Shader::resolveCompile(const Context *context)
{
if (!mState.compilePending())
{
return;
}
ASSERT(mCompilingState.get());
mCompilingState->compileEvent->wait();
mInfoLog += mCompilingState->compileEvent->getInfoLog();
ScopedExit exit([this]() {
mBoundCompiler->putInstance(std::move(mCompilingState->shCompilerInstance));
mCompilingState->compileEvent.reset();
mCompilingState.reset();
});
ShHandle compilerHandle = mCompilingState->shCompilerInstance.getHandle();
if (!mCompilingState->compileEvent->getResult())
{
mInfoLog += sh::GetInfoLog(compilerHandle);
INFO() << std::endl << mInfoLog;
mState.mCompileStatus = CompileStatus::NOT_COMPILED;
return;
}
const ShShaderOutput outputType = mCompilingState->shCompilerInstance.getShaderOutputType();
bool isBinaryOutput = outputType == SH_SPIRV_VULKAN_OUTPUT;
mState.mCompiledShaderState.buildCompiledShaderState(compilerHandle, isBinaryOutput);
#if !defined(NDEBUG)
if (outputType != SH_SPIRV_VULKAN_OUTPUT)
{
// Prefix translated shader with commented out un-translated shader.
// Useful in diagnostics tools which capture the shader source.
std::ostringstream shaderStream;
shaderStream << "// GLSL\n";
shaderStream << "//\n";
std::istringstream inputSourceStream(mState.mSource);
std::string line;
while (std::getline(inputSourceStream, line))
{
// Remove null characters from the source line
line.erase(std::remove(line.begin(), line.end(), '\0'), line.end());
shaderStream << "// " << line;
// glslang complains if a comment ends with backslash
if (!line.empty() && line.back() == '\\')
{
shaderStream << "\\";
}
shaderStream << std::endl;
}
shaderStream << "\n\n";
shaderStream << mState.mCompiledShaderState.translatedSource;
mState.mCompiledShaderState.translatedSource = shaderStream.str();
}
#endif // !defined(NDEBUG)
// Validation checks for compute shaders
if (mState.mCompiledShaderState.shaderType == ShaderType::Compute &&
mState.mCompiledShaderState.localSize.isDeclared())
{
angle::CheckedNumeric<uint32_t> checked_local_size_product(
mState.mCompiledShaderState.localSize[0]);
checked_local_size_product *= mState.mCompiledShaderState.localSize[1];
checked_local_size_product *= mState.mCompiledShaderState.localSize[2];
if (!checked_local_size_product.IsValid())
{
WARN() << std::endl
<< "Integer overflow when computing the product of local_size_x, "
<< "local_size_y and local_size_z.";
mState.mCompileStatus = CompileStatus::NOT_COMPILED;
return;
}
if (checked_local_size_product.ValueOrDie() > mCurrentMaxComputeWorkGroupInvocations)
{
WARN() << std::endl
<< "The total number of invocations within a work group exceeds "
<< "MAX_COMPUTE_WORK_GROUP_INVOCATIONS.";
mState.mCompileStatus = CompileStatus::NOT_COMPILED;
return;
}
}
unsigned int sharedMemSize = sh::GetShaderSharedMemorySize(compilerHandle);
if (sharedMemSize > mMaxComputeSharedMemory)
{
WARN() << std::endl << "Exceeded maximum shared memory size";
mState.mCompileStatus = CompileStatus::NOT_COMPILED;
return;
}
ASSERT(!mState.mCompiledShaderState.translatedSource.empty() ||
!mState.mCompiledShaderState.compiledBinary.empty());
bool success = mCompilingState->compileEvent->postTranslate(&mInfoLog);
mState.mCompileStatus = success ? CompileStatus::COMPILED : CompileStatus::NOT_COMPILED;
MemoryShaderCache *shaderCache = context->getMemoryShaderCache();
if (success && shaderCache)
{
// Save to the shader cache.
if (shaderCache->putShader(context, mShaderHash, this) != angle::Result::Continue)
{
ANGLE_PERF_WARNING(context->getState().getDebug(), GL_DEBUG_SEVERITY_LOW,
"Failed to save compiled shader to memory shader cache.");
}
}
}
void Shader::addRef()
{
mRefCount++;
}
void Shader::release(const Context *context)
{
mRefCount--;
if (mRefCount == 0 && mDeleteStatus)
{
mResourceManager->deleteShader(context, mHandle);
}
}
unsigned int Shader::getRefCount() const
{
return mRefCount;
}
bool Shader::isFlaggedForDeletion() const
{
return mDeleteStatus;
}
void Shader::flagForDeletion()
{
mDeleteStatus = true;
}
bool Shader::isCompiled(const Context *context)
{
resolveCompile(context);
return mState.mCompileStatus == CompileStatus::COMPILED;
}
bool Shader::isCompleted()
{
return (!mState.compilePending() || mCompilingState->compileEvent->isReady());
}
int Shader::getShaderVersion(const Context *context)
{
resolveCompile(context);
return mState.mCompiledShaderState.shaderVersion;
}
const std::vector<sh::ShaderVariable> &Shader::getInputVaryings(const Context *context)
{
resolveCompile(context);
return mState.getInputVaryings();
}
const std::vector<sh::ShaderVariable> &Shader::getOutputVaryings(const Context *context)
{
resolveCompile(context);
return mState.getOutputVaryings();
}
const std::vector<sh::ShaderVariable> &Shader::getUniforms(const Context *context)
{
resolveCompile(context);
return mState.getUniforms();
}
const std::vector<sh::InterfaceBlock> &Shader::getUniformBlocks(const Context *context)
{
resolveCompile(context);
return mState.getUniformBlocks();
}
const std::vector<sh::InterfaceBlock> &Shader::getShaderStorageBlocks(const Context *context)
{
resolveCompile(context);
return mState.getShaderStorageBlocks();
}
const std::vector<sh::ShaderVariable> &Shader::getActiveAttributes(const Context *context)
{
resolveCompile(context);
return mState.getActiveAttributes();
}
const std::vector<sh::ShaderVariable> &Shader::getAllAttributes(const Context *context)
{
resolveCompile(context);
return mState.getAllAttributes();
}
const std::vector<sh::ShaderVariable> &Shader::getActiveOutputVariables(const Context *context)
{
resolveCompile(context);
return mState.getActiveOutputVariables();
}
std::string Shader::getTransformFeedbackVaryingMappedName(const Context *context,
const std::string &tfVaryingName)
{
ASSERT(mState.getShaderType() != ShaderType::Fragment &&
mState.getShaderType() != ShaderType::Compute);
const auto &varyings = getOutputVaryings(context);
auto bracketPos = tfVaryingName.find("[");
if (bracketPos != std::string::npos)
{
auto tfVaryingBaseName = tfVaryingName.substr(0, bracketPos);
for (const auto &varying : varyings)
{
if (varying.name == tfVaryingBaseName)
{
std::string mappedNameWithArrayIndex =
varying.mappedName + tfVaryingName.substr(bracketPos);
return mappedNameWithArrayIndex;
}
}
}
else
{
for (const auto &varying : varyings)
{
if (varying.name == tfVaryingName)
{
return varying.mappedName;
}
else if (varying.isStruct())
{
GLuint fieldIndex = 0;
const auto *field = varying.findField(tfVaryingName, &fieldIndex);
if (field == nullptr)
{
continue;
}
ASSERT(field != nullptr && !field->isStruct() &&
(!field->isArray() || varying.isShaderIOBlock));
std::string mappedName;
// If it's an I/O block without an instance name, don't include the block name.
if (!varying.isShaderIOBlock || !varying.name.empty())
{
mappedName = varying.isShaderIOBlock ? varying.mappedStructOrBlockName
: varying.mappedName;
mappedName += '.';
}
return mappedName + field->mappedName;
}
}
}
UNREACHABLE();
return std::string();
}
const sh::WorkGroupSize &Shader::getWorkGroupSize(const Context *context)
{
resolveCompile(context);
return mState.mCompiledShaderState.localSize;
}
int Shader::getNumViews(const Context *context)
{
resolveCompile(context);
return mState.mCompiledShaderState.numViews;
}
Optional<PrimitiveMode> Shader::getGeometryShaderInputPrimitiveType(const Context *context)
{
resolveCompile(context);
return mState.mCompiledShaderState.geometryShaderInputPrimitiveType;
}
Optional<PrimitiveMode> Shader::getGeometryShaderOutputPrimitiveType(const Context *context)
{
resolveCompile(context);
return mState.mCompiledShaderState.geometryShaderOutputPrimitiveType;
}
int Shader::getGeometryShaderInvocations(const Context *context)
{
resolveCompile(context);
return mState.mCompiledShaderState.geometryShaderInvocations;
}
Optional<GLint> Shader::getGeometryShaderMaxVertices(const Context *context)
{
resolveCompile(context);
return mState.mCompiledShaderState.geometryShaderMaxVertices;
}
int Shader::getTessControlShaderVertices(const Context *context)
{
resolveCompile(context);
return mState.mCompiledShaderState.tessControlShaderVertices;
}
GLenum Shader::getTessGenMode(const Context *context)
{
resolveCompile(context);
return mState.mCompiledShaderState.tessGenMode;
}
GLenum Shader::getTessGenSpacing(const Context *context)
{
resolveCompile(context);
return mState.mCompiledShaderState.tessGenSpacing;
}
GLenum Shader::getTessGenVertexOrder(const Context *context)
{
resolveCompile(context);
return mState.mCompiledShaderState.tessGenVertexOrder;
}
GLenum Shader::getTessGenPointMode(const Context *context)
{
resolveCompile(context);
return mState.mCompiledShaderState.tessGenPointMode;
}
angle::Result Shader::serialize(const Context *context, angle::MemoryBuffer *binaryOut) const
{
BinaryOutputStream stream;
stream.writeInt(kShaderCacheIdentifier);
mState.mCompiledShaderState.serialize(stream);
ASSERT(binaryOut);
if (!binaryOut->resize(stream.length()))
{
std::stringstream sstream;
sstream << "Failed to allocate enough memory to serialize a shader. (" << stream.length()
<< " bytes )";
ANGLE_PERF_WARNING(context->getState().getDebug(), GL_DEBUG_SEVERITY_LOW,
sstream.str().c_str());
return angle::Result::Incomplete;
}
memcpy(binaryOut->data(), stream.data(), stream.length());
return angle::Result::Continue;
}
angle::Result Shader::deserialize(BinaryInputStream &stream)
{
mState.mCompiledShaderState.deserialize(stream);
if (stream.error())
{
// Error while deserializing binary stream
return angle::Result::Stop;
}
return angle::Result::Continue;
}
angle::Result Shader::loadBinary(const Context *context, const void *binary, GLsizei length)
{
return loadBinaryImpl(context, binary, length, false);
}
angle::Result Shader::loadShaderBinary(const Context *context, const void *binary, GLsizei length)
{
return loadBinaryImpl(context, binary, length, true);
}
angle::Result Shader::loadBinaryImpl(const Context *context,
const void *binary,
GLsizei length,
bool generatedWithOfflineCompiler)
{
BinaryInputStream stream(binary, length);
// Shader binaries generated with offline compiler have additional fields
if (generatedWithOfflineCompiler)
{
// Load binary from a glShaderBinary call.
// Validation layer should have already verified that the shader program version and shader
// type match
std::vector<uint8_t> commitString(angle::GetANGLEShaderProgramVersionHashSize(), 0);
stream.readBytes(commitString.data(), commitString.size());
ASSERT(memcmp(commitString.data(), angle::GetANGLEShaderProgramVersion(),
commitString.size()) == 0);
gl::ShaderType shaderType;
stream.readEnum(&shaderType);
ASSERT(mType == shaderType);
// Get fields needed to generate the key for memory caches.
ShShaderOutput outputType;
stream.readEnum<ShShaderOutput>(&outputType);
// Get the shader's source string.
mState.mSource = stream.readString();
// In the absence of element-by-element serialize/deserialize functions, read
// ShCompileOptions and ShBuiltInResources as raw binary blobs.
ShCompileOptions compileOptions;
stream.readBytes(reinterpret_cast<uint8_t *>(&compileOptions), sizeof(ShCompileOptions));
ShBuiltInResources resources;
stream.readBytes(reinterpret_cast<uint8_t *>(&resources), sizeof(ShBuiltInResources));
setShaderKey(context, compileOptions, outputType, resources);
}
else
{
// Load binary from shader cache.
if (stream.readInt<uint32_t>() != kShaderCacheIdentifier)
{
return angle::Result::Stop;
}
}
ANGLE_TRY(deserialize(stream));
// Only successfully-compiled shaders are serialized. If deserialization is successful, we can
// assume the CompileStatus.
mState.mCompileStatus = CompileStatus::COMPILED;
return angle::Result::Continue;
}
void Shader::setShaderKey(const Context *context,
const ShCompileOptions &compileOptions,
const ShShaderOutput &outputType,
const ShBuiltInResources &resources)
{
// Compute shader key.
BinaryOutputStream hashStream;
// Start with the shader type and source.
hashStream.writeEnum(mType);
hashStream.writeString(mState.getSource());
// Include the shader program version hash.
hashStream.writeString(angle::GetANGLEShaderProgramVersion());
hashStream.writeEnum(Compiler::SelectShaderSpec(context->getState()));
hashStream.writeEnum(outputType);
hashStream.writeBytes(reinterpret_cast<const uint8_t *>(&compileOptions),
sizeof(compileOptions));
// Include the ShBuiltInResources, which represent the extensions and constants used by the
// shader.
hashStream.writeBytes(reinterpret_cast<const uint8_t *>(&resources), sizeof(resources));
// Call the secure SHA hashing function.
const std::vector<uint8_t> &shaderKey = hashStream.getData();
mShaderHash = {0};
angle::base::SHA1HashBytes(shaderKey.data(), shaderKey.size(), mShaderHash.data());
}
} // namespace gl