Hash :
b0e15ee4
Author :
Date :
2021-12-28T20:37:33
Decide GL_KHR_parallel_shader_compile in backends GL_KHR_parallel_shader_compile was previously being enabled unconditionally in the front end. However, some backends (Vulkan) perform worse with parallel shader compilation. This CL moves the decision of enabling GL_KHR_parallel_shader_compile to the backends. To support single-threaded shader compilation without affecting the generic worker thread pool, Context::mSingleThreadPool is added to own the single-threaded WorkerThreadPool and can be returned by the new function Context::getShaderCompileThreadPool(). Otherwise, if the extension is enabled, the (renamed) Context::mMultiThreadPool is returned. Bug: angleproject:6748 Change-Id: Ic8d3a183f397608f3002a05480deb976dfe44792 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3360337 Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Tim Van Patten <timvp@google.com>
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 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
//
// Copyright 2015 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.
//
// ProgramGL.cpp: Implements the class methods for ProgramGL.
#include "libANGLE/renderer/gl/ProgramGL.h"
#include "common/angleutils.h"
#include "common/bitset_utils.h"
#include "common/debug.h"
#include "common/string_utils.h"
#include "common/utilities.h"
#include "libANGLE/Context.h"
#include "libANGLE/ProgramLinkedResources.h"
#include "libANGLE/Uniform.h"
#include "libANGLE/WorkerThread.h"
#include "libANGLE/queryconversions.h"
#include "libANGLE/renderer/gl/ContextGL.h"
#include "libANGLE/renderer/gl/FunctionsGL.h"
#include "libANGLE/renderer/gl/RendererGL.h"
#include "libANGLE/renderer/gl/ShaderGL.h"
#include "libANGLE/renderer/gl/StateManagerGL.h"
#include "libANGLE/trace.h"
#include "platform/FeaturesGL.h"
#include "platform/PlatformMethods.h"
namespace rx
{
ProgramGL::ProgramGL(const gl::ProgramState &data,
const FunctionsGL *functions,
const angle::FeaturesGL &features,
StateManagerGL *stateManager,
const std::shared_ptr<RendererGL> &renderer)
: ProgramImpl(data),
mFunctions(functions),
mFeatures(features),
mStateManager(stateManager),
mMultiviewBaseViewLayerIndexUniformLocation(-1),
mProgramID(0),
mRenderer(renderer),
mLinkedInParallel(false)
{
ASSERT(mFunctions);
ASSERT(mStateManager);
mProgramID = mFunctions->createProgram();
}
ProgramGL::~ProgramGL()
{
mFunctions->deleteProgram(mProgramID);
mProgramID = 0;
}
std::unique_ptr<LinkEvent> ProgramGL::load(const gl::Context *context,
gl::BinaryInputStream *stream,
gl::InfoLog &infoLog)
{
ANGLE_TRACE_EVENT0("gpu.angle", "ProgramGL::load");
preLink();
// Read the binary format, size and blob
GLenum binaryFormat = stream->readInt<GLenum>();
GLint binaryLength = stream->readInt<GLint>();
const uint8_t *binary = stream->data() + stream->offset();
stream->skip(binaryLength);
// Load the binary
mFunctions->programBinary(mProgramID, binaryFormat, binary, binaryLength);
// Verify that the program linked
if (!checkLinkStatus(infoLog))
{
return std::make_unique<LinkEventDone>(angle::Result::Incomplete);
}
postLink();
reapplyUBOBindingsIfNeeded(context);
return std::make_unique<LinkEventDone>(angle::Result::Continue);
}
void ProgramGL::save(const gl::Context *context, gl::BinaryOutputStream *stream)
{
GLint binaryLength = 0;
mFunctions->getProgramiv(mProgramID, GL_PROGRAM_BINARY_LENGTH, &binaryLength);
std::vector<uint8_t> binary(std::max(binaryLength, 1));
GLenum binaryFormat = GL_NONE;
mFunctions->getProgramBinary(mProgramID, binaryLength, &binaryLength, &binaryFormat,
binary.data());
stream->writeInt(binaryFormat);
stream->writeInt(binaryLength);
stream->writeBytes(binary.data(), binaryLength);
reapplyUBOBindingsIfNeeded(context);
}
void ProgramGL::reapplyUBOBindingsIfNeeded(const gl::Context *context)
{
// Re-apply UBO bindings to work around driver bugs.
const angle::FeaturesGL &features = GetImplAs<ContextGL>(context)->getFeaturesGL();
if (features.reapplyUBOBindingsAfterUsingBinaryProgram.enabled)
{
const auto &blocks = mState.getUniformBlocks();
for (size_t blockIndex : mState.getActiveUniformBlockBindingsMask())
{
setUniformBlockBinding(static_cast<GLuint>(blockIndex), blocks[blockIndex].binding);
}
}
}
void ProgramGL::setBinaryRetrievableHint(bool retrievable)
{
// glProgramParameteri isn't always available on ES backends.
if (mFunctions->programParameteri)
{
mFunctions->programParameteri(mProgramID, GL_PROGRAM_BINARY_RETRIEVABLE_HINT,
retrievable ? GL_TRUE : GL_FALSE);
}
}
void ProgramGL::setSeparable(bool separable)
{
mFunctions->programParameteri(mProgramID, GL_PROGRAM_SEPARABLE, separable ? GL_TRUE : GL_FALSE);
}
using LinkImplFunctor = std::function<bool(std::string &)>;
class ProgramGL::LinkTask final : public angle::Closure
{
public:
LinkTask(LinkImplFunctor &&functor) : mLinkImplFunctor(functor), mFallbackToMainContext(false)
{}
void operator()() override
{
ANGLE_TRACE_EVENT0("gpu.angle", "ProgramGL::LinkTask::run");
mFallbackToMainContext = mLinkImplFunctor(mInfoLog);
}
bool fallbackToMainContext() { return mFallbackToMainContext; }
const std::string &getInfoLog() { return mInfoLog; }
private:
LinkImplFunctor mLinkImplFunctor;
bool mFallbackToMainContext;
std::string mInfoLog;
};
using PostLinkImplFunctor = std::function<angle::Result(bool, const std::string &)>;
// The event for a parallelized linking using the native driver extension.
class ProgramGL::LinkEventNativeParallel final : public LinkEvent
{
public:
LinkEventNativeParallel(PostLinkImplFunctor &&functor,
const FunctionsGL *functions,
GLuint programID)
: mPostLinkImplFunctor(functor), mFunctions(functions), mProgramID(programID)
{}
angle::Result wait(const gl::Context *context) override
{
ANGLE_TRACE_EVENT0("gpu.angle", "ProgramGL::LinkEventNativeParallel::wait");
GLint linkStatus = GL_FALSE;
mFunctions->getProgramiv(mProgramID, GL_LINK_STATUS, &linkStatus);
if (linkStatus == GL_TRUE)
{
return mPostLinkImplFunctor(false, std::string());
}
return angle::Result::Incomplete;
}
bool isLinking() override
{
GLint completionStatus = GL_FALSE;
mFunctions->getProgramiv(mProgramID, GL_COMPLETION_STATUS, &completionStatus);
return completionStatus == GL_FALSE;
}
private:
PostLinkImplFunctor mPostLinkImplFunctor;
const FunctionsGL *mFunctions;
GLuint mProgramID;
};
// The event for a parallelized linking using the worker thread pool.
class ProgramGL::LinkEventGL final : public LinkEvent
{
public:
LinkEventGL(std::shared_ptr<angle::WorkerThreadPool> workerPool,
std::shared_ptr<ProgramGL::LinkTask> linkTask,
PostLinkImplFunctor &&functor)
: mLinkTask(linkTask),
mWaitableEvent(std::shared_ptr<angle::WaitableEvent>(
angle::WorkerThreadPool::PostWorkerTask(workerPool, mLinkTask))),
mPostLinkImplFunctor(functor)
{}
angle::Result wait(const gl::Context *context) override
{
ANGLE_TRACE_EVENT0("gpu.angle", "ProgramGL::LinkEventGL::wait");
mWaitableEvent->wait();
return mPostLinkImplFunctor(mLinkTask->fallbackToMainContext(), mLinkTask->getInfoLog());
}
bool isLinking() override { return !mWaitableEvent->isReady(); }
private:
std::shared_ptr<ProgramGL::LinkTask> mLinkTask;
std::shared_ptr<angle::WaitableEvent> mWaitableEvent;
PostLinkImplFunctor mPostLinkImplFunctor;
};
std::unique_ptr<LinkEvent> ProgramGL::link(const gl::Context *context,
const gl::ProgramLinkedResources &resources,
gl::InfoLog &infoLog,
const gl::ProgramMergedVaryings & /*mergedVaryings*/)
{
ANGLE_TRACE_EVENT0("gpu.angle", "ProgramGL::link");
preLink();
if (mState.getAttachedShader(gl::ShaderType::Compute))
{
const ShaderGL *computeShaderGL =
GetImplAs<ShaderGL>(mState.getAttachedShader(gl::ShaderType::Compute));
mFunctions->attachShader(mProgramID, computeShaderGL->getShaderID());
}
else
{
// Set the transform feedback state
std::vector<std::string> transformFeedbackVaryingMappedNames;
for (const auto &tfVarying : mState.getTransformFeedbackVaryingNames())
{
gl::ShaderType tfShaderType =
mState.getExecutable().hasLinkedShaderStage(gl::ShaderType::Geometry)
? gl::ShaderType::Geometry
: gl::ShaderType::Vertex;
std::string tfVaryingMappedName =
mState.getAttachedShader(tfShaderType)
->getTransformFeedbackVaryingMappedName(tfVarying);
transformFeedbackVaryingMappedNames.push_back(tfVaryingMappedName);
}
if (transformFeedbackVaryingMappedNames.empty())
{
if (mFunctions->transformFeedbackVaryings)
{
mFunctions->transformFeedbackVaryings(mProgramID, 0, nullptr,
mState.getTransformFeedbackBufferMode());
}
}
else
{
ASSERT(mFunctions->transformFeedbackVaryings);
std::vector<const GLchar *> transformFeedbackVaryings;
for (const auto &varying : transformFeedbackVaryingMappedNames)
{
transformFeedbackVaryings.push_back(varying.c_str());
}
mFunctions->transformFeedbackVaryings(
mProgramID, static_cast<GLsizei>(transformFeedbackVaryingMappedNames.size()),
&transformFeedbackVaryings[0], mState.getTransformFeedbackBufferMode());
}
for (const gl::ShaderType shaderType : gl::kAllGraphicsShaderTypes)
{
const ShaderGL *shaderGL =
rx::SafeGetImplAs<ShaderGL, gl::Shader>(mState.getAttachedShader(shaderType));
if (shaderGL)
{
mFunctions->attachShader(mProgramID, shaderGL->getShaderID());
}
}
// Bind attribute locations to match the GL layer.
for (const sh::ShaderVariable &attribute : mState.getProgramInputs())
{
if (!attribute.active || attribute.isBuiltIn())
{
continue;
}
mFunctions->bindAttribLocation(mProgramID, attribute.location,
attribute.mappedName.c_str());
}
// Bind the secondary fragment color outputs defined in EXT_blend_func_extended. We only use
// the API to bind fragment output locations in case EXT_blend_func_extended is enabled.
// Otherwise shader-assigned locations will work.
if (context->getExtensions().blendFuncExtendedEXT)
{
gl::Shader *fragmentShader = mState.getAttachedShader(gl::ShaderType::Fragment);
if (fragmentShader && fragmentShader->getShaderVersion() == 100)
{
// TODO(http://anglebug.com/2833): The bind done below is only valid in case the
// compiler transforms the shader outputs to the angle/webgl prefixed ones. If we
// added support for running EXT_blend_func_extended on top of GLES, some changes
// would be required:
// - If we're backed by GLES 2.0, we shouldn't do the bind because it's not needed.
// - If we're backed by GLES 3.0+, it's a bit unclear what should happen. Currently
// the compiler doesn't support transforming GLSL ES 1.00 shaders to GLSL ES 3.00
// shaders in general, but support for that might be required. Or we might be
// able to skip the bind in case the compiler outputs GLSL ES 1.00.
const auto &shaderOutputs =
mState.getAttachedShader(gl::ShaderType::Fragment)->getActiveOutputVariables();
for (const auto &output : shaderOutputs)
{
// TODO(http://anglebug.com/1085) This could be cleaner if the transformed names
// would be set correctly in ShaderVariable::mappedName. This would require some
// refactoring in the translator. Adding a mapped name dictionary for builtins
// into the symbol table would be one fairly clean way to do it.
if (output.name == "gl_SecondaryFragColorEXT")
{
mFunctions->bindFragDataLocationIndexed(mProgramID, 0, 0,
"webgl_FragColor");
mFunctions->bindFragDataLocationIndexed(mProgramID, 0, 1,
"webgl_SecondaryFragColor");
}
else if (output.name == "gl_SecondaryFragDataEXT")
{
// Basically we should have a loop here going over the output
// array binding "webgl_FragData[i]" and "webgl_SecondaryFragData[i]" array
// indices to the correct color buffers and color indices.
// However I'm not sure if this construct is legal or not, neither ARB or
// EXT version of the spec mention this. They only mention that
// automatically assigned array locations for ESSL 3.00 output arrays need
// to have contiguous locations.
//
// In practice it seems that binding array members works on some drivers and
// fails on others. One option could be to modify the shader translator to
// expand the arrays into individual output variables instead of using an
// array.
//
// For now we're going to have a limitation of assuming that
// GL_MAX_DUAL_SOURCE_DRAW_BUFFERS is *always* 1 and then only bind the
// basename of the variable ignoring any indices. This appears to work
// uniformly.
ASSERT(output.isArray() && output.getOutermostArraySize() == 1);
mFunctions->bindFragDataLocationIndexed(mProgramID, 0, 0, "webgl_FragData");
mFunctions->bindFragDataLocationIndexed(mProgramID, 0, 1,
"webgl_SecondaryFragData");
}
}
}
else
{
// ESSL 3.00 and up.
const auto &outputLocations = mState.getOutputLocations();
const auto &secondaryOutputLocations = mState.getSecondaryOutputLocations();
for (size_t outputLocationIndex = 0u; outputLocationIndex < outputLocations.size();
++outputLocationIndex)
{
const gl::VariableLocation &outputLocation =
outputLocations[outputLocationIndex];
if (outputLocation.arrayIndex == 0 && outputLocation.used() &&
!outputLocation.ignored)
{
const sh::ShaderVariable &outputVar =
mState.getOutputVariables()[outputLocation.index];
if (outputVar.location == -1 || outputVar.index == -1)
{
// We only need to assign the location and index via the API in case the
// variable doesn't have a shader-assigned location and index. If a
// variable doesn't have its location set in the shader it doesn't have
// the index set either.
ASSERT(outputVar.index == -1);
mFunctions->bindFragDataLocationIndexed(
mProgramID, static_cast<int>(outputLocationIndex), 0,
outputVar.mappedName.c_str());
}
}
}
for (size_t outputLocationIndex = 0u;
outputLocationIndex < secondaryOutputLocations.size(); ++outputLocationIndex)
{
const gl::VariableLocation &outputLocation =
secondaryOutputLocations[outputLocationIndex];
if (outputLocation.arrayIndex == 0 && outputLocation.used() &&
!outputLocation.ignored)
{
const sh::ShaderVariable &outputVar =
mState.getOutputVariables()[outputLocation.index];
if (outputVar.location == -1 || outputVar.index == -1)
{
// We only need to assign the location and index via the API in case the
// variable doesn't have a shader-assigned location and index. If a
// variable doesn't have its location set in the shader it doesn't have
// the index set either.
ASSERT(outputVar.index == -1);
mFunctions->bindFragDataLocationIndexed(
mProgramID, static_cast<int>(outputLocationIndex), 1,
outputVar.mappedName.c_str());
}
}
}
}
}
}
auto workerPool = context->getShaderCompileThreadPool();
auto linkTask = std::make_shared<LinkTask>([this](std::string &infoLog) {
std::string workerInfoLog;
ScopedWorkerContextGL worker(mRenderer.get(), &workerInfoLog);
if (!worker())
{
#if !defined(NDEBUG)
infoLog += "bindWorkerContext failed.\n" + workerInfoLog;
#endif
// Fallback to the main context.
return true;
}
mFunctions->linkProgram(mProgramID);
// Make sure the driver actually does the link job.
GLint linkStatus = GL_FALSE;
mFunctions->getProgramiv(mProgramID, GL_LINK_STATUS, &linkStatus);
return false;
});
auto postLinkImplTask = [this, &infoLog, &resources](bool fallbackToMainContext,
const std::string &workerInfoLog) {
infoLog << workerInfoLog;
if (fallbackToMainContext)
{
mFunctions->linkProgram(mProgramID);
}
if (mState.getAttachedShader(gl::ShaderType::Compute))
{
const ShaderGL *computeShaderGL =
GetImplAs<ShaderGL>(mState.getAttachedShader(gl::ShaderType::Compute));
mFunctions->detachShader(mProgramID, computeShaderGL->getShaderID());
}
else
{
for (const gl::ShaderType shaderType : gl::kAllGraphicsShaderTypes)
{
const ShaderGL *shaderGL =
rx::SafeGetImplAs<ShaderGL>(mState.getAttachedShader(shaderType));
if (shaderGL)
{
mFunctions->detachShader(mProgramID, shaderGL->getShaderID());
}
}
}
// Verify the link
if (!checkLinkStatus(infoLog))
{
return angle::Result::Incomplete;
}
if (mFeatures.alwaysCallUseProgramAfterLink.enabled)
{
mStateManager->forceUseProgram(mProgramID);
}
linkResources(resources);
postLink();
return angle::Result::Continue;
};
if (mRenderer->hasNativeParallelCompile())
{
mFunctions->linkProgram(mProgramID);
return std::make_unique<LinkEventNativeParallel>(postLinkImplTask, mFunctions, mProgramID);
}
else if (workerPool->isAsync() &&
(!mFeatures.dontRelinkProgramsInParallel.enabled || !mLinkedInParallel))
{
mLinkedInParallel = true;
return std::make_unique<LinkEventGL>(workerPool, linkTask, postLinkImplTask);
}
else
{
return std::make_unique<LinkEventDone>(postLinkImplTask(true, std::string()));
}
}
GLboolean ProgramGL::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
{
// TODO(jmadill): implement validate
return true;
}
void ProgramGL::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
{
if (mFunctions->programUniform1fv != nullptr)
{
mFunctions->programUniform1fv(mProgramID, uniLoc(location), count, v);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniform1fv(uniLoc(location), count, v);
}
}
void ProgramGL::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
{
if (mFunctions->programUniform2fv != nullptr)
{
mFunctions->programUniform2fv(mProgramID, uniLoc(location), count, v);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniform2fv(uniLoc(location), count, v);
}
}
void ProgramGL::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
{
if (mFunctions->programUniform3fv != nullptr)
{
mFunctions->programUniform3fv(mProgramID, uniLoc(location), count, v);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniform3fv(uniLoc(location), count, v);
}
}
void ProgramGL::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
{
if (mFunctions->programUniform4fv != nullptr)
{
mFunctions->programUniform4fv(mProgramID, uniLoc(location), count, v);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniform4fv(uniLoc(location), count, v);
}
}
void ProgramGL::setUniform1iv(GLint location, GLsizei count, const GLint *v)
{
if (mFunctions->programUniform1iv != nullptr)
{
mFunctions->programUniform1iv(mProgramID, uniLoc(location), count, v);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniform1iv(uniLoc(location), count, v);
}
}
void ProgramGL::setUniform2iv(GLint location, GLsizei count, const GLint *v)
{
if (mFunctions->programUniform2iv != nullptr)
{
mFunctions->programUniform2iv(mProgramID, uniLoc(location), count, v);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniform2iv(uniLoc(location), count, v);
}
}
void ProgramGL::setUniform3iv(GLint location, GLsizei count, const GLint *v)
{
if (mFunctions->programUniform3iv != nullptr)
{
mFunctions->programUniform3iv(mProgramID, uniLoc(location), count, v);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniform3iv(uniLoc(location), count, v);
}
}
void ProgramGL::setUniform4iv(GLint location, GLsizei count, const GLint *v)
{
if (mFunctions->programUniform4iv != nullptr)
{
mFunctions->programUniform4iv(mProgramID, uniLoc(location), count, v);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniform4iv(uniLoc(location), count, v);
}
}
void ProgramGL::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
{
if (mFunctions->programUniform1uiv != nullptr)
{
mFunctions->programUniform1uiv(mProgramID, uniLoc(location), count, v);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniform1uiv(uniLoc(location), count, v);
}
}
void ProgramGL::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
{
if (mFunctions->programUniform2uiv != nullptr)
{
mFunctions->programUniform2uiv(mProgramID, uniLoc(location), count, v);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniform2uiv(uniLoc(location), count, v);
}
}
void ProgramGL::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
{
if (mFunctions->programUniform3uiv != nullptr)
{
mFunctions->programUniform3uiv(mProgramID, uniLoc(location), count, v);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniform3uiv(uniLoc(location), count, v);
}
}
void ProgramGL::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
{
if (mFunctions->programUniform4uiv != nullptr)
{
mFunctions->programUniform4uiv(mProgramID, uniLoc(location), count, v);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniform4uiv(uniLoc(location), count, v);
}
}
void ProgramGL::setUniformMatrix2fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
if (mFunctions->programUniformMatrix2fv != nullptr)
{
mFunctions->programUniformMatrix2fv(mProgramID, uniLoc(location), count, transpose, value);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniformMatrix2fv(uniLoc(location), count, transpose, value);
}
}
void ProgramGL::setUniformMatrix3fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
if (mFunctions->programUniformMatrix3fv != nullptr)
{
mFunctions->programUniformMatrix3fv(mProgramID, uniLoc(location), count, transpose, value);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniformMatrix3fv(uniLoc(location), count, transpose, value);
}
}
void ProgramGL::setUniformMatrix4fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
if (mFunctions->programUniformMatrix4fv != nullptr)
{
mFunctions->programUniformMatrix4fv(mProgramID, uniLoc(location), count, transpose, value);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniformMatrix4fv(uniLoc(location), count, transpose, value);
}
}
void ProgramGL::setUniformMatrix2x3fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
if (mFunctions->programUniformMatrix2x3fv != nullptr)
{
mFunctions->programUniformMatrix2x3fv(mProgramID, uniLoc(location), count, transpose,
value);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniformMatrix2x3fv(uniLoc(location), count, transpose, value);
}
}
void ProgramGL::setUniformMatrix3x2fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
if (mFunctions->programUniformMatrix3x2fv != nullptr)
{
mFunctions->programUniformMatrix3x2fv(mProgramID, uniLoc(location), count, transpose,
value);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniformMatrix3x2fv(uniLoc(location), count, transpose, value);
}
}
void ProgramGL::setUniformMatrix2x4fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
if (mFunctions->programUniformMatrix2x4fv != nullptr)
{
mFunctions->programUniformMatrix2x4fv(mProgramID, uniLoc(location), count, transpose,
value);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniformMatrix2x4fv(uniLoc(location), count, transpose, value);
}
}
void ProgramGL::setUniformMatrix4x2fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
if (mFunctions->programUniformMatrix4x2fv != nullptr)
{
mFunctions->programUniformMatrix4x2fv(mProgramID, uniLoc(location), count, transpose,
value);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniformMatrix4x2fv(uniLoc(location), count, transpose, value);
}
}
void ProgramGL::setUniformMatrix3x4fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
if (mFunctions->programUniformMatrix3x4fv != nullptr)
{
mFunctions->programUniformMatrix3x4fv(mProgramID, uniLoc(location), count, transpose,
value);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniformMatrix3x4fv(uniLoc(location), count, transpose, value);
}
}
void ProgramGL::setUniformMatrix4x3fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
if (mFunctions->programUniformMatrix4x3fv != nullptr)
{
mFunctions->programUniformMatrix4x3fv(mProgramID, uniLoc(location), count, transpose,
value);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniformMatrix4x3fv(uniLoc(location), count, transpose, value);
}
}
void ProgramGL::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
{
// Lazy init
if (mUniformBlockRealLocationMap.empty())
{
mUniformBlockRealLocationMap.reserve(mState.getUniformBlocks().size());
for (const gl::InterfaceBlock &uniformBlock : mState.getUniformBlocks())
{
const std::string &mappedNameWithIndex = uniformBlock.mappedNameWithArrayIndex();
GLuint blockIndex =
mFunctions->getUniformBlockIndex(mProgramID, mappedNameWithIndex.c_str());
mUniformBlockRealLocationMap.push_back(blockIndex);
}
}
GLuint realBlockIndex = mUniformBlockRealLocationMap[uniformBlockIndex];
if (realBlockIndex != GL_INVALID_INDEX)
{
mFunctions->uniformBlockBinding(mProgramID, realBlockIndex, uniformBlockBinding);
}
}
bool ProgramGL::getUniformBlockSize(const std::string & /* blockName */,
const std::string &blockMappedName,
size_t *sizeOut) const
{
ASSERT(mProgramID != 0u);
GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, blockMappedName.c_str());
if (blockIndex == GL_INVALID_INDEX)
{
*sizeOut = 0;
return false;
}
GLint dataSize = 0;
mFunctions->getActiveUniformBlockiv(mProgramID, blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE,
&dataSize);
*sizeOut = static_cast<size_t>(dataSize);
return true;
}
bool ProgramGL::getUniformBlockMemberInfo(const std::string & /* memberUniformName */,
const std::string &memberUniformMappedName,
sh::BlockMemberInfo *memberInfoOut) const
{
GLuint uniformIndex;
const GLchar *memberNameGLStr = memberUniformMappedName.c_str();
mFunctions->getUniformIndices(mProgramID, 1, &memberNameGLStr, &uniformIndex);
if (uniformIndex == GL_INVALID_INDEX)
{
*memberInfoOut = sh::kDefaultBlockMemberInfo;
return false;
}
mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_OFFSET,
&memberInfoOut->offset);
mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_ARRAY_STRIDE,
&memberInfoOut->arrayStride);
mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_MATRIX_STRIDE,
&memberInfoOut->matrixStride);
// TODO(jmadill): possibly determine this at the gl::Program level.
GLint isRowMajorMatrix = 0;
mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_IS_ROW_MAJOR,
&isRowMajorMatrix);
memberInfoOut->isRowMajorMatrix = gl::ConvertToBool(isRowMajorMatrix);
return true;
}
bool ProgramGL::getShaderStorageBlockMemberInfo(const std::string & /* memberName */,
const std::string &memberUniformMappedName,
sh::BlockMemberInfo *memberInfoOut) const
{
const GLchar *memberNameGLStr = memberUniformMappedName.c_str();
GLuint index =
mFunctions->getProgramResourceIndex(mProgramID, GL_BUFFER_VARIABLE, memberNameGLStr);
if (index == GL_INVALID_INDEX)
{
*memberInfoOut = sh::kDefaultBlockMemberInfo;
return false;
}
constexpr int kPropCount = 5;
std::array<GLenum, kPropCount> props = {
{GL_ARRAY_STRIDE, GL_IS_ROW_MAJOR, GL_MATRIX_STRIDE, GL_OFFSET, GL_TOP_LEVEL_ARRAY_STRIDE}};
std::array<GLint, kPropCount> params;
GLsizei length;
mFunctions->getProgramResourceiv(mProgramID, GL_BUFFER_VARIABLE, index, kPropCount,
props.data(), kPropCount, &length, params.data());
ASSERT(kPropCount == length);
memberInfoOut->arrayStride = params[0];
memberInfoOut->isRowMajorMatrix = params[1] != 0;
memberInfoOut->matrixStride = params[2];
memberInfoOut->offset = params[3];
memberInfoOut->topLevelArrayStride = params[4];
return true;
}
bool ProgramGL::getShaderStorageBlockSize(const std::string &name,
const std::string &mappedName,
size_t *sizeOut) const
{
const GLchar *nameGLStr = mappedName.c_str();
GLuint index =
mFunctions->getProgramResourceIndex(mProgramID, GL_SHADER_STORAGE_BLOCK, nameGLStr);
if (index == GL_INVALID_INDEX)
{
*sizeOut = 0;
return false;
}
GLenum prop = GL_BUFFER_DATA_SIZE;
GLsizei length = 0;
GLint dataSize = 0;
mFunctions->getProgramResourceiv(mProgramID, GL_SHADER_STORAGE_BLOCK, index, 1, &prop, 1,
&length, &dataSize);
*sizeOut = static_cast<size_t>(dataSize);
return true;
}
void ProgramGL::getAtomicCounterBufferSizeMap(std::map<int, unsigned int> *sizeMapOut) const
{
if (mFunctions->getProgramInterfaceiv == nullptr)
{
return;
}
int resourceCount = 0;
mFunctions->getProgramInterfaceiv(mProgramID, GL_ATOMIC_COUNTER_BUFFER, GL_ACTIVE_RESOURCES,
&resourceCount);
for (int index = 0; index < resourceCount; index++)
{
constexpr int kPropCount = 2;
std::array<GLenum, kPropCount> props = {{GL_BUFFER_BINDING, GL_BUFFER_DATA_SIZE}};
std::array<GLint, kPropCount> params;
GLsizei length;
mFunctions->getProgramResourceiv(mProgramID, GL_ATOMIC_COUNTER_BUFFER, index, kPropCount,
props.data(), kPropCount, &length, params.data());
ASSERT(kPropCount == length);
int bufferBinding = params[0];
unsigned int bufferDataSize = params[1];
sizeMapOut->insert(std::pair<int, unsigned int>(bufferBinding, bufferDataSize));
}
}
void ProgramGL::preLink()
{
// Reset the program state
mUniformRealLocationMap.clear();
mUniformBlockRealLocationMap.clear();
mMultiviewBaseViewLayerIndexUniformLocation = -1;
}
bool ProgramGL::checkLinkStatus(gl::InfoLog &infoLog)
{
GLint linkStatus = GL_FALSE;
mFunctions->getProgramiv(mProgramID, GL_LINK_STATUS, &linkStatus);
if (linkStatus == GL_FALSE)
{
// Linking or program binary loading failed, put the error into the info log.
GLint infoLogLength = 0;
mFunctions->getProgramiv(mProgramID, GL_INFO_LOG_LENGTH, &infoLogLength);
// Info log length includes the null terminator, so 1 means that the info log is an empty
// string.
if (infoLogLength > 1)
{
std::vector<char> buf(infoLogLength);
mFunctions->getProgramInfoLog(mProgramID, infoLogLength, nullptr, &buf[0]);
infoLog << buf.data();
WARN() << "Program link or binary loading failed: " << buf.data();
}
else
{
WARN() << "Program link or binary loading failed with no info log.";
}
// This may happen under normal circumstances if we're loading program binaries and the
// driver or hardware has changed.
ASSERT(mProgramID != 0);
return false;
}
return true;
}
void ProgramGL::postLink()
{
// Query the uniform information
ASSERT(mUniformRealLocationMap.empty());
const auto &uniformLocations = mState.getUniformLocations();
const auto &uniforms = mState.getUniforms();
mUniformRealLocationMap.resize(uniformLocations.size(), GL_INVALID_INDEX);
for (size_t uniformLocation = 0; uniformLocation < uniformLocations.size(); uniformLocation++)
{
const auto &entry = uniformLocations[uniformLocation];
if (!entry.used())
{
continue;
}
// From the GLES 3.0.5 spec:
// "Locations for sequential array indices are not required to be sequential."
const gl::LinkedUniform &uniform = uniforms[entry.index];
std::stringstream fullNameStr;
if (uniform.isArray())
{
ASSERT(angle::EndsWith(uniform.mappedName, "[0]"));
fullNameStr << uniform.mappedName.substr(0, uniform.mappedName.length() - 3);
fullNameStr << "[" << entry.arrayIndex << "]";
}
else
{
fullNameStr << uniform.mappedName;
}
const std::string &fullName = fullNameStr.str();
GLint realLocation = mFunctions->getUniformLocation(mProgramID, fullName.c_str());
mUniformRealLocationMap[uniformLocation] = realLocation;
}
if (mState.usesMultiview())
{
mMultiviewBaseViewLayerIndexUniformLocation =
mFunctions->getUniformLocation(mProgramID, "multiviewBaseViewLayerIndex");
ASSERT(mMultiviewBaseViewLayerIndexUniformLocation != -1);
}
}
void ProgramGL::enableSideBySideRenderingPath() const
{
ASSERT(mState.usesMultiview());
ASSERT(mMultiviewBaseViewLayerIndexUniformLocation != -1);
ASSERT(mFunctions->programUniform1i != nullptr);
mFunctions->programUniform1i(mProgramID, mMultiviewBaseViewLayerIndexUniformLocation, -1);
}
void ProgramGL::enableLayeredRenderingPath(int baseViewIndex) const
{
ASSERT(mState.usesMultiview());
ASSERT(mMultiviewBaseViewLayerIndexUniformLocation != -1);
ASSERT(mFunctions->programUniform1i != nullptr);
mFunctions->programUniform1i(mProgramID, mMultiviewBaseViewLayerIndexUniformLocation,
baseViewIndex);
}
void ProgramGL::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
{
mFunctions->getUniformfv(mProgramID, uniLoc(location), params);
}
void ProgramGL::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
{
mFunctions->getUniformiv(mProgramID, uniLoc(location), params);
}
void ProgramGL::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
{
mFunctions->getUniformuiv(mProgramID, uniLoc(location), params);
}
void ProgramGL::markUnusedUniformLocations(std::vector<gl::VariableLocation> *uniformLocations,
std::vector<gl::SamplerBinding> *samplerBindings,
std::vector<gl::ImageBinding> *imageBindings)
{
GLint maxLocation = static_cast<GLint>(uniformLocations->size());
for (GLint location = 0; location < maxLocation; ++location)
{
if (uniLoc(location) == -1)
{
auto &locationRef = (*uniformLocations)[location];
if (mState.isSamplerUniformIndex(locationRef.index))
{
GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationRef.index);
gl::SamplerBinding &samplerBinding = (*samplerBindings)[samplerIndex];
if (locationRef.arrayIndex < samplerBinding.boundTextureUnits.size())
{
// Crop unused sampler bindings in the sampler array.
samplerBinding.boundTextureUnits.resize(locationRef.arrayIndex);
}
}
else if (mState.isImageUniformIndex(locationRef.index))
{
GLuint imageIndex = mState.getImageIndexFromUniformIndex(locationRef.index);
gl::ImageBinding &imageBinding = (*imageBindings)[imageIndex];
if (locationRef.arrayIndex < imageBinding.boundImageUnits.size())
{
// Crop unused image bindings in the image array.
imageBinding.boundImageUnits.resize(locationRef.arrayIndex);
}
}
// If the location has been previously bound by a glBindUniformLocation call, it should
// be marked as ignored. Otherwise it's unused.
if (mState.getUniformLocationBindings().getBindingByLocation(location) != -1)
{
locationRef.markIgnored();
}
else
{
locationRef.markUnused();
}
}
}
}
void ProgramGL::linkResources(const gl::ProgramLinkedResources &resources)
{
// Gather interface block info.
auto getUniformBlockSize = [this](const std::string &name, const std::string &mappedName,
size_t *sizeOut) {
return this->getUniformBlockSize(name, mappedName, sizeOut);
};
auto getUniformBlockMemberInfo = [this](const std::string &name, const std::string &mappedName,
sh::BlockMemberInfo *infoOut) {
return this->getUniformBlockMemberInfo(name, mappedName, infoOut);
};
resources.uniformBlockLinker.linkBlocks(getUniformBlockSize, getUniformBlockMemberInfo);
auto getShaderStorageBlockSize = [this](const std::string &name, const std::string &mappedName,
size_t *sizeOut) {
return this->getShaderStorageBlockSize(name, mappedName, sizeOut);
};
auto getShaderStorageBlockMemberInfo = [this](const std::string &name,
const std::string &mappedName,
sh::BlockMemberInfo *infoOut) {
return this->getShaderStorageBlockMemberInfo(name, mappedName, infoOut);
};
resources.shaderStorageBlockLinker.linkBlocks(getShaderStorageBlockSize,
getShaderStorageBlockMemberInfo);
// Gather atomic counter buffer info.
std::map<int, unsigned int> sizeMap;
getAtomicCounterBufferSizeMap(&sizeMap);
resources.atomicCounterBufferLinker.link(sizeMap);
}
angle::Result ProgramGL::syncState(const gl::Context *context,
const gl::Program::DirtyBits &dirtyBits)
{
for (size_t dirtyBit : dirtyBits)
{
ASSERT(dirtyBit <= gl::Program::DIRTY_BIT_UNIFORM_BLOCK_BINDING_MAX);
GLuint binding = static_cast<GLuint>(dirtyBit);
setUniformBlockBinding(binding, mState.getUniformBlockBinding(binding));
}
return angle::Result::Continue;
}
} // namespace rx