Hash :
aead8edf
Author :
Date :
2019-02-13T13:55:09
Mute worker context creation warnings This moves the warnings to InfoLog. Bug: chromium:931294 Change-Id: I1627aa63bdda6f92fc89b8921eb260302ba9063f Reviewed-on: https://chromium-review.googlesource.com/c/1469721 Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Jie A Chen <jie.a.chen@intel.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
//
// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// 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/utilities.h"
#include "libANGLE/Caps.h"
#include "libANGLE/Compiler.h"
#include "libANGLE/Constants.h"
#include "libANGLE/Context.h"
#include "libANGLE/ResourceManager.h"
#include "libANGLE/WorkerThread.h"
#include "libANGLE/renderer/GLImplFactory.h"
#include "libANGLE/renderer/ShaderImpl.h"
namespace gl
{
namespace
{
template <typename VarT>
std::vector<VarT> GetActiveShaderVariables(const std::vector<VarT> *variableList)
{
ASSERT(variableList);
std::vector<VarT> result;
for (size_t varIndex = 0; varIndex < variableList->size(); varIndex++)
{
const VarT &var = variableList->at(varIndex);
if (var.active)
{
result.push_back(var);
}
}
return result;
}
template <typename VarT>
const std::vector<VarT> &GetShaderVariables(const std::vector<VarT> *variableList)
{
ASSERT(variableList);
return *variableList;
}
} // anonymous namespace
// true if varying x has a higher priority in packing than y
bool CompareShaderVar(const sh::ShaderVariable &x, const sh::ShaderVariable &y)
{
if (x.type == y.type)
{
return x.getArraySizeProduct() > y.getArraySizeProduct();
}
// Special case for handling structs: we sort these to the end of the list
if (x.type == GL_NONE)
{
return false;
}
if (y.type == GL_NONE)
{
return true;
}
return gl::VariableSortOrder(x.type) < gl::VariableSortOrder(y.type);
}
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";
default:
UNREACHABLE();
return "";
}
}
class ScopedExit final : angle::NonCopyable
{
public:
ScopedExit(std::function<void()> exit) : mExit(exit) {}
~ScopedExit() { mExit(); }
private:
std::function<void()> mExit;
};
using CompileImplFunctor = std::function<void(const std::string &, std::string &)>;
class CompileTask : public angle::Closure
{
public:
CompileTask(ShHandle handle,
std::string &&sourcePath,
std::string &&source,
ShCompileOptions options,
CompileImplFunctor &&functor)
: mHandle(handle),
mSourcePath(sourcePath),
mSource(source),
mOptions(options),
mCompileImplFunctor(functor),
mResult(false)
{}
void operator()() override
{
std::vector<const char *> srcStrings;
if (!mSourcePath.empty())
{
srcStrings.push_back(mSourcePath.c_str());
}
srcStrings.push_back(mSource.c_str());
mResult = sh::Compile(mHandle, &srcStrings[0], srcStrings.size(), mOptions);
if (mResult)
{
mCompileImplFunctor(sh::GetObjectCode(mHandle), mInfoLog);
}
}
bool getResult() { return mResult; }
const std::string &getInfoLog() { return mInfoLog; }
private:
ShHandle mHandle;
std::string mSourcePath;
std::string mSource;
ShCompileOptions mOptions;
CompileImplFunctor mCompileImplFunctor;
bool mResult;
std::string mInfoLog;
};
ShaderState::ShaderState(ShaderType shaderType)
: mLabel(),
mShaderType(shaderType),
mShaderVersion(100),
mNumViews(-1),
mGeometryShaderInvocations(1),
mCompileStatus(CompileStatus::NOT_COMPILED)
{
mLocalSize.fill(-1);
}
ShaderState::~ShaderState() {}
Shader::Shader(ShaderProgramManager *manager,
rx::GLImplFactory *implFactory,
const gl::Limitations &rendererLimitations,
ShaderType type,
GLuint handle)
: mState(type),
mImplementation(implFactory->createShader(mState)),
mRendererLimitations(rendererLimitations),
mHandle(handle),
mType(type),
mRefCount(0),
mDeleteStatus(false),
mResourceManager(manager),
mCurrentMaxComputeWorkGroupInvocations(0u)
{
ASSERT(mImplementation);
}
void Shader::onDestroy(const gl::Context *context)
{
resolveCompile();
mImplementation->destroy();
mBoundCompiler.set(context, nullptr);
mImplementation.reset(nullptr);
delete this;
}
Shader::~Shader()
{
ASSERT(!mImplementation);
}
void Shader::setLabel(const Context *context, const std::string &label)
{
mState.mLabel = label;
}
const std::string &Shader::getLabel() const
{
return mState.mLabel;
}
GLuint Shader::getHandle() const
{
return mHandle;
}
void Shader::setSource(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]);
}
}
mState.mSource = stream.str();
}
int Shader::getInfoLogLength()
{
resolveCompile();
if (mInfoLog.empty())
{
return 0;
}
return (static_cast<int>(mInfoLog.length()) + 1);
}
void Shader::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog)
{
resolveCompile();
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()
{
resolveCompile();
if (mState.mTranslatedSource.empty())
{
return 0;
}
return (static_cast<int>(mState.mTranslatedSource.length()) + 1);
}
int Shader::getTranslatedSourceWithDebugInfoLength()
{
resolveCompile();
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(GLsizei bufSize, GLsizei *length, char *buffer)
{
GetSourceImpl(getTranslatedSource(), bufSize, length, buffer);
}
const std::string &Shader::getTranslatedSource()
{
resolveCompile();
return mState.mTranslatedSource;
}
void Shader::getTranslatedSourceWithDebugInfo(GLsizei bufSize, GLsizei *length, char *buffer)
{
resolveCompile();
const std::string &debugInfo = mImplementation->getDebugInfo();
GetSourceImpl(debugInfo, bufSize, length, buffer);
}
void Shader::compile(const Context *context)
{
resolveCompile();
mState.mTranslatedSource.clear();
mInfoLog.clear();
mState.mShaderVersion = 100;
mState.mInputVaryings.clear();
mState.mOutputVaryings.clear();
mState.mUniforms.clear();
mState.mUniformBlocks.clear();
mState.mShaderStorageBlocks.clear();
mState.mActiveAttributes.clear();
mState.mActiveOutputVariables.clear();
mState.mNumViews = -1;
mState.mGeometryShaderInputPrimitiveType.reset();
mState.mGeometryShaderOutputPrimitiveType.reset();
mState.mGeometryShaderMaxVertices.reset();
mState.mGeometryShaderInvocations = 1;
mState.mCompileStatus = CompileStatus::COMPILE_REQUESTED;
mBoundCompiler.set(context, context->getCompiler());
// Cache the compile source and options for compilation. Must be done now, since the source
// can change before the link call or another call that resolves the compile.
std::stringstream sourceStream;
std::string sourcePath;
ShCompileOptions options =
mImplementation->prepareSourceAndReturnOptions(context, &sourceStream, &sourcePath);
options |= (SH_OBJECT_CODE | SH_VARIABLES | SH_EMULATE_GL_DRAW_ID);
auto source = sourceStream.str();
// Add default options to WebGL shaders to prevent unexpected behavior during compilation.
if (context->getExtensions().webglCompatibility)
{
options |= SH_INIT_GL_POSITION;
options |= SH_LIMIT_CALL_STACK_DEPTH;
options |= SH_LIMIT_EXPRESSION_COMPLEXITY;
options |= SH_ENFORCE_PACKING_RESTRICTIONS;
options |= SH_INIT_SHARED_VARIABLES;
}
// Some targets (eg 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 |= SH_VALIDATE_LOOP_INDEXING;
}
mCurrentMaxComputeWorkGroupInvocations = context->getCaps().maxComputeWorkGroupInvocations;
ASSERT(mBoundCompiler.get());
mShCompilerInstance = mBoundCompiler->getInstance(mState.mShaderType);
ShHandle compilerHandle = mShCompilerInstance.getHandle();
ASSERT(compilerHandle);
mCompilerResourcesString = mShCompilerInstance.getBuiltinResourcesString();
mWorkerPool = context->getWorkerThreadPool();
std::function<void(const std::string &, std::string &)> compileImplFunctor;
if (mWorkerPool->isAsync())
{
compileImplFunctor = [this](const std::string &source, std::string &infoLog) {
mImplementation->compileAsync(source, infoLog);
};
}
else
{
compileImplFunctor = [](const std::string &source, std::string &infoLog) {};
}
mCompileTask =
std::make_shared<CompileTask>(compilerHandle, std::move(sourcePath), std::move(source),
options, std::move(compileImplFunctor));
mCompileEvent = mWorkerPool->postWorkerTask(mCompileTask);
}
void Shader::resolveCompile()
{
if (!mState.compilePending())
{
return;
}
ASSERT(mCompileEvent.get());
ASSERT(mCompileTask.get());
mCompileEvent->wait();
mCompileEvent.reset();
mWorkerPool.reset();
bool compiled = mCompileTask->getResult();
mInfoLog += mCompileTask->getInfoLog();
mCompileTask.reset();
ScopedExit exit([this]() { mBoundCompiler->putInstance(std::move(mShCompilerInstance)); });
ShHandle compilerHandle = mShCompilerInstance.getHandle();
if (!compiled)
{
mInfoLog += sh::GetInfoLog(compilerHandle);
WARN() << std::endl << mInfoLog;
mState.mCompileStatus = CompileStatus::NOT_COMPILED;
return;
}
mState.mTranslatedSource = sh::GetObjectCode(compilerHandle);
#if !defined(NDEBUG)
// 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 << std::endl;
}
shaderStream << "\n\n";
shaderStream << mState.mTranslatedSource;
mState.mTranslatedSource = shaderStream.str();
#endif // !defined(NDEBUG)
// Gather the shader information
mState.mShaderVersion = sh::GetShaderVersion(compilerHandle);
mState.mUniforms = GetShaderVariables(sh::GetUniforms(compilerHandle));
mState.mUniformBlocks = GetShaderVariables(sh::GetUniformBlocks(compilerHandle));
mState.mShaderStorageBlocks = GetShaderVariables(sh::GetShaderStorageBlocks(compilerHandle));
switch (mState.mShaderType)
{
case ShaderType::Compute:
{
mState.mLocalSize = sh::GetComputeShaderLocalGroupSize(compilerHandle);
if (mState.mLocalSize.isDeclared())
{
angle::CheckedNumeric<uint32_t> checked_local_size_product(mState.mLocalSize[0]);
checked_local_size_product *= mState.mLocalSize[1];
checked_local_size_product *= mState.mLocalSize[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;
}
}
break;
}
case ShaderType::Vertex:
{
{
mState.mOutputVaryings = GetShaderVariables(sh::GetOutputVaryings(compilerHandle));
mState.mAllAttributes = GetShaderVariables(sh::GetAttributes(compilerHandle));
mState.mActiveAttributes = GetActiveShaderVariables(&mState.mAllAttributes);
mState.mNumViews = sh::GetVertexShaderNumViews(compilerHandle);
}
break;
}
case ShaderType::Fragment:
{
mState.mInputVaryings = GetShaderVariables(sh::GetInputVaryings(compilerHandle));
// TODO(jmadill): Figure out why we only sort in the FS, and if we need to.
std::sort(mState.mInputVaryings.begin(), mState.mInputVaryings.end(), CompareShaderVar);
mState.mActiveOutputVariables =
GetActiveShaderVariables(sh::GetOutputVariables(compilerHandle));
break;
}
case ShaderType::Geometry:
{
mState.mInputVaryings = GetShaderVariables(sh::GetInputVaryings(compilerHandle));
mState.mOutputVaryings = GetShaderVariables(sh::GetOutputVaryings(compilerHandle));
if (sh::HasValidGeometryShaderInputPrimitiveType(compilerHandle))
{
mState.mGeometryShaderInputPrimitiveType = FromGLenum<PrimitiveMode>(
sh::GetGeometryShaderInputPrimitiveType(compilerHandle));
}
if (sh::HasValidGeometryShaderOutputPrimitiveType(compilerHandle))
{
mState.mGeometryShaderOutputPrimitiveType = FromGLenum<PrimitiveMode>(
sh::GetGeometryShaderOutputPrimitiveType(compilerHandle));
}
if (sh::HasValidGeometryShaderMaxVertices(compilerHandle))
{
mState.mGeometryShaderMaxVertices =
sh::GetGeometryShaderMaxVertices(compilerHandle);
}
mState.mGeometryShaderInvocations = sh::GetGeometryShaderInvocations(compilerHandle);
break;
}
default:
UNREACHABLE();
}
ASSERT(!mState.mTranslatedSource.empty());
bool success = mImplementation->postTranslateCompile(&mShCompilerInstance, &mInfoLog);
mState.mCompileStatus = success ? CompileStatus::COMPILED : CompileStatus::NOT_COMPILED;
}
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()
{
resolveCompile();
return mState.mCompileStatus == CompileStatus::COMPILED;
}
bool Shader::isCompleted()
{
return (!mState.compilePending() || mCompileEvent->isReady());
}
int Shader::getShaderVersion()
{
resolveCompile();
return mState.mShaderVersion;
}
const std::vector<sh::Varying> &Shader::getInputVaryings()
{
resolveCompile();
return mState.getInputVaryings();
}
const std::vector<sh::Varying> &Shader::getOutputVaryings()
{
resolveCompile();
return mState.getOutputVaryings();
}
const std::vector<sh::Uniform> &Shader::getUniforms()
{
resolveCompile();
return mState.getUniforms();
}
const std::vector<sh::InterfaceBlock> &Shader::getUniformBlocks()
{
resolveCompile();
return mState.getUniformBlocks();
}
const std::vector<sh::InterfaceBlock> &Shader::getShaderStorageBlocks()
{
resolveCompile();
return mState.getShaderStorageBlocks();
}
const std::vector<sh::Attribute> &Shader::getActiveAttributes()
{
resolveCompile();
return mState.getActiveAttributes();
}
const std::vector<sh::Attribute> &Shader::getAllAttributes()
{
resolveCompile();
return mState.getAllAttributes();
}
const std::vector<sh::OutputVariable> &Shader::getActiveOutputVariables()
{
resolveCompile();
return mState.getActiveOutputVariables();
}
std::string Shader::getTransformFeedbackVaryingMappedName(const std::string &tfVaryingName)
{
// TODO(jiawei.shao@intel.com): support transform feedback on geometry shader.
ASSERT(mState.getShaderType() == ShaderType::Vertex);
const auto &varyings = getOutputVaryings();
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())
{
const auto *field = FindShaderVarField(varying, tfVaryingName);
ASSERT(field != nullptr && !field->isStruct() && !field->isArray());
return varying.mappedName + "." + field->mappedName;
}
}
}
UNREACHABLE();
return std::string();
}
const sh::WorkGroupSize &Shader::getWorkGroupSize()
{
resolveCompile();
return mState.mLocalSize;
}
int Shader::getNumViews()
{
resolveCompile();
return mState.mNumViews;
}
Optional<PrimitiveMode> Shader::getGeometryShaderInputPrimitiveType()
{
resolveCompile();
return mState.mGeometryShaderInputPrimitiveType;
}
Optional<PrimitiveMode> Shader::getGeometryShaderOutputPrimitiveType()
{
resolveCompile();
return mState.mGeometryShaderOutputPrimitiveType;
}
int Shader::getGeometryShaderInvocations()
{
resolveCompile();
return mState.mGeometryShaderInvocations;
}
Optional<GLint> Shader::getGeometryShaderMaxVertices()
{
resolveCompile();
return mState.mGeometryShaderMaxVertices;
}
const std::string &Shader::getCompilerResourcesString() const
{
return mCompilerResourcesString;
}
} // namespace gl