Hash :
28e7adca
Author :
Date :
2022-12-09T00:00:00
GL: Implement clip distance state emulation Pass the current set of enabled clip distances to vertex shaders via an internal uniform and dynamically set disabled elements to zero. Bug: angleproject:7880 Change-Id: I709d31dc7ca0606decf49adf674460a941837683 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4094314 Commit-Queue: Alexey Knyazev <lexa.knyazev@gmail.com> Reviewed-by: Shahbaz Youssefi <syoussefi@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
//
// 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.
//
// ShaderGL.cpp: Implements the class methods for ShaderGL.
#include "libANGLE/renderer/gl/ShaderGL.h"
#include "common/debug.h"
#include "libANGLE/Compiler.h"
#include "libANGLE/Context.h"
#include "libANGLE/renderer/ContextImpl.h"
#include "libANGLE/renderer/gl/FunctionsGL.h"
#include "libANGLE/renderer/gl/RendererGL.h"
#include "libANGLE/trace.h"
#include "platform/FeaturesGL_autogen.h"
#include <iostream>
namespace rx
{
using CompileAndCheckShaderInWorkerFunctor = std::function<bool(const char *source)>;
class TranslateTaskGL : public angle::Closure
{
public:
TranslateTaskGL(ShHandle handle,
const ShCompileOptions &options,
const std::string &source,
CompileAndCheckShaderInWorkerFunctor &&compileAndCheckShaderInWorkerFunctor)
: mHandle(handle),
mOptions(options),
mSource(source),
mCompileAndCheckShaderInWorkerFunctor(std::move(compileAndCheckShaderInWorkerFunctor)),
mResult(false),
mWorkerAvailable(true)
{}
void operator()() override
{
ANGLE_TRACE_EVENT1("gpu.angle", "TranslateTaskGL::run", "source", mSource);
const char *source = mSource.c_str();
mResult = sh::Compile(mHandle, &source, 1, mOptions);
if (mResult)
{
mWorkerAvailable =
mCompileAndCheckShaderInWorkerFunctor(sh::GetObjectCode(mHandle).c_str());
}
}
bool getResult() { return mResult; }
bool workerAvailable() { return mWorkerAvailable; }
ShHandle getHandle() { return mHandle; }
private:
ShHandle mHandle;
ShCompileOptions mOptions;
std::string mSource;
CompileAndCheckShaderInWorkerFunctor mCompileAndCheckShaderInWorkerFunctor;
bool mResult;
bool mWorkerAvailable;
};
using PostTranslateFunctor = std::function<bool(std::string *infoLog)>;
using CompileAndCheckShaderFunctor = std::function<void(const char *source)>;
class WaitableCompileEventWorkerContext final : public WaitableCompileEvent
{
public:
WaitableCompileEventWorkerContext(std::shared_ptr<angle::WaitableEvent> waitableEvent,
CompileAndCheckShaderFunctor &&compileAndCheckShaderFunctor,
PostTranslateFunctor &&postTranslateFunctor,
std::shared_ptr<TranslateTaskGL> translateTask)
: WaitableCompileEvent(waitableEvent),
mCompileAndCheckShaderFunctor(std::move(compileAndCheckShaderFunctor)),
mPostTranslateFunctor(std::move(postTranslateFunctor)),
mTranslateTask(translateTask)
{}
bool getResult() override { return mTranslateTask->getResult(); }
bool postTranslate(std::string *infoLog) override
{
if (!mTranslateTask->workerAvailable())
{
ShHandle handle = mTranslateTask->getHandle();
mCompileAndCheckShaderFunctor(sh::GetObjectCode(handle).c_str());
}
return mPostTranslateFunctor(infoLog);
}
private:
CompileAndCheckShaderFunctor mCompileAndCheckShaderFunctor;
PostTranslateFunctor mPostTranslateFunctor;
std::shared_ptr<TranslateTaskGL> mTranslateTask;
};
using PeekCompletionFunctor = std::function<bool()>;
using CheckShaderFunctor = std::function<void()>;
class WaitableCompileEventNativeParallel final : public WaitableCompileEvent
{
public:
WaitableCompileEventNativeParallel(PostTranslateFunctor &&postTranslateFunctor,
bool result,
CheckShaderFunctor &&checkShaderFunctor,
PeekCompletionFunctor &&peekCompletionFunctor)
: WaitableCompileEvent(std::shared_ptr<angle::WaitableEventDone>()),
mPostTranslateFunctor(std::move(postTranslateFunctor)),
mResult(result),
mCheckShaderFunctor(std::move(checkShaderFunctor)),
mPeekCompletionFunctor(std::move(peekCompletionFunctor))
{}
void wait() override { mCheckShaderFunctor(); }
bool isReady() override { return mPeekCompletionFunctor(); }
bool getResult() override { return mResult; }
bool postTranslate(std::string *infoLog) override { return mPostTranslateFunctor(infoLog); }
private:
PostTranslateFunctor mPostTranslateFunctor;
bool mResult;
CheckShaderFunctor mCheckShaderFunctor;
PeekCompletionFunctor mPeekCompletionFunctor;
};
class WaitableCompileEventDone final : public WaitableCompileEvent
{
public:
WaitableCompileEventDone(PostTranslateFunctor &&postTranslateFunctor, bool result)
: WaitableCompileEvent(std::make_shared<angle::WaitableEventDone>()),
mPostTranslateFunctor(std::move(postTranslateFunctor)),
mResult(result)
{}
bool getResult() override { return mResult; }
bool postTranslate(std::string *infoLog) override { return mPostTranslateFunctor(infoLog); }
private:
PostTranslateFunctor mPostTranslateFunctor;
bool mResult;
};
ShaderGL::ShaderGL(const gl::ShaderState &data,
GLuint shaderID,
MultiviewImplementationTypeGL multiviewImplementationType,
const std::shared_ptr<RendererGL> &renderer)
: ShaderImpl(data),
mShaderID(shaderID),
mMultiviewImplementationType(multiviewImplementationType),
mRenderer(renderer),
mCompileStatus(GL_FALSE)
{}
ShaderGL::~ShaderGL()
{
ASSERT(mShaderID == 0);
}
void ShaderGL::destroy()
{
mRenderer->getFunctions()->deleteShader(mShaderID);
mShaderID = 0;
}
void ShaderGL::compileAndCheckShader(const char *source)
{
compileShader(source);
checkShader();
}
void ShaderGL::compileShader(const char *source)
{
const FunctionsGL *functions = mRenderer->getFunctions();
functions->shaderSource(mShaderID, 1, &source, nullptr);
functions->compileShader(mShaderID);
}
void ShaderGL::checkShader()
{
const FunctionsGL *functions = mRenderer->getFunctions();
// Check for compile errors from the native driver
mCompileStatus = GL_FALSE;
functions->getShaderiv(mShaderID, GL_COMPILE_STATUS, &mCompileStatus);
if (mCompileStatus == GL_FALSE)
{
// Compilation failed, put the error into the info log
GLint infoLogLength = 0;
functions->getShaderiv(mShaderID, 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);
functions->getShaderInfoLog(mShaderID, infoLogLength, nullptr, &buf[0]);
mInfoLog += &buf[0];
WARN() << std::endl << mInfoLog;
}
else
{
WARN() << std::endl << "Shader compilation failed with no info log.";
}
}
}
bool ShaderGL::peekCompletion()
{
const FunctionsGL *functions = mRenderer->getFunctions();
GLint status = GL_FALSE;
functions->getShaderiv(mShaderID, GL_COMPLETION_STATUS, &status);
return status == GL_TRUE;
}
bool ShaderGL::compileAndCheckShaderInWorker(const char *source)
{
std::string workerInfoLog;
ScopedWorkerContextGL worker(mRenderer.get(), &workerInfoLog);
if (worker())
{
compileAndCheckShader(source);
return true;
}
else
{
#if !defined(NDEBUG)
mInfoLog += "bindWorkerContext failed.\n" + workerInfoLog;
#endif
return false;
}
}
std::shared_ptr<WaitableCompileEvent> ShaderGL::compile(const gl::Context *context,
gl::ShCompilerInstance *compilerInstance,
ShCompileOptions *options)
{
mInfoLog.clear();
options->initGLPosition = true;
bool isWebGL = context->isWebGL();
if (isWebGL && mState.getShaderType() != gl::ShaderType::Compute)
{
options->initOutputVariables = true;
}
if (isWebGL && !context->getState().getEnableFeature(GL_TEXTURE_RECTANGLE_ANGLE))
{
options->disableARBTextureRectangle = true;
}
const angle::FeaturesGL &features = GetFeaturesGL(context);
if (features.initFragmentOutputVariables.enabled)
{
options->initFragmentOutputVariables = true;
}
if (features.doWhileGLSLCausesGPUHang.enabled)
{
options->rewriteDoWhileLoops = true;
}
if (features.emulateAbsIntFunction.enabled)
{
options->emulateAbsIntFunction = true;
}
if (features.addAndTrueToLoopCondition.enabled)
{
options->addAndTrueToLoopCondition = true;
}
if (features.emulateIsnanFloat.enabled)
{
options->emulateIsnanFloatFunction = true;
}
if (features.emulateAtan2Float.enabled)
{
options->emulateAtan2FloatFunction = true;
}
if (features.useUnusedBlocksWithStandardOrSharedLayout.enabled)
{
options->useUnusedStandardSharedBlocks = true;
}
if (features.removeInvariantAndCentroidForESSL3.enabled)
{
options->removeInvariantAndCentroidForESSL3 = true;
}
if (features.rewriteFloatUnaryMinusOperator.enabled)
{
options->rewriteFloatUnaryMinusOperator = true;
}
if (!features.dontInitializeUninitializedLocals.enabled)
{
options->initializeUninitializedLocals = true;
}
if (features.clampPointSize.enabled)
{
options->clampPointSize = true;
}
if (features.dontUseLoopsToInitializeVariables.enabled)
{
options->dontUseLoopsToInitializeVariables = true;
}
if (features.clampFragDepth.enabled)
{
options->clampFragDepth = true;
}
if (features.rewriteRepeatedAssignToSwizzled.enabled)
{
options->rewriteRepeatedAssignToSwizzled = true;
}
if (mMultiviewImplementationType == MultiviewImplementationTypeGL::NV_VIEWPORT_ARRAY2)
{
options->initializeBuiltinsForInstancedMultiview = true;
options->selectViewInNvGLSLVertexShader = true;
}
if (features.clampArrayAccess.enabled || isWebGL)
{
options->clampIndirectArrayBounds = true;
}
if (features.vertexIDDoesNotIncludeBaseVertex.enabled)
{
options->addBaseVertexToVertexID = true;
}
if (features.unfoldShortCircuits.enabled)
{
options->unfoldShortCircuit = true;
}
if (features.removeDynamicIndexingOfSwizzledVector.enabled)
{
options->removeDynamicIndexingOfSwizzledVector = true;
}
if (features.preAddTexelFetchOffsets.enabled)
{
options->rewriteTexelFetchOffsetToTexelFetch = true;
}
if (features.regenerateStructNames.enabled)
{
options->regenerateStructNames = true;
}
if (features.rewriteRowMajorMatrices.enabled)
{
options->rewriteRowMajorMatrices = true;
}
if (features.passHighpToPackUnormSnormBuiltins.enabled)
{
options->passHighpToPackUnormSnormBuiltins = true;
}
if (features.emulateClipDistanceState.enabled)
{
options->emulateClipDistanceState = true;
}
if (mRenderer->getNativeExtensions().shaderPixelLocalStorageANGLE)
{
options->pls = mRenderer->getNativePixelLocalStorageOptions();
}
auto workerThreadPool = context->getShaderCompileThreadPool();
const std::string &source = mState.getSource();
auto postTranslateFunctor = [this](std::string *infoLog) {
if (mCompileStatus == GL_FALSE)
{
*infoLog = mInfoLog;
return false;
}
return true;
};
if (mRenderer->hasNativeParallelCompile())
{
ShHandle handle = compilerInstance->getHandle();
const char *str = source.c_str();
bool result = sh::Compile(handle, &str, 1, *options);
if (result)
{
compileShader(sh::GetObjectCode(handle).c_str());
auto checkShaderFunctor = [this]() { checkShader(); };
auto peekCompletionFunctor = [this]() { return peekCompletion(); };
return std::make_shared<WaitableCompileEventNativeParallel>(
std::move(postTranslateFunctor), result, std::move(checkShaderFunctor),
std::move(peekCompletionFunctor));
}
else
{
return std::make_shared<WaitableCompileEventDone>([](std::string *) { return true; },
result);
}
}
else if (workerThreadPool->isAsync())
{
auto compileAndCheckShaderInWorkerFunctor = [this](const char *source) {
return compileAndCheckShaderInWorker(source);
};
auto translateTask =
std::make_shared<TranslateTaskGL>(compilerInstance->getHandle(), *options, source,
std::move(compileAndCheckShaderInWorkerFunctor));
auto compileAndCheckShaderFunctor = [this](const char *source) {
compileAndCheckShader(source);
};
return std::make_shared<WaitableCompileEventWorkerContext>(
workerThreadPool->postWorkerTask(translateTask),
std::move(compileAndCheckShaderFunctor), std::move(postTranslateFunctor),
translateTask);
}
else
{
ShHandle handle = compilerInstance->getHandle();
const char *str = source.c_str();
bool result = sh::Compile(handle, &str, 1, *options);
if (result)
{
compileAndCheckShader(sh::GetObjectCode(handle).c_str());
}
return std::make_shared<WaitableCompileEventDone>(std::move(postTranslateFunctor), result);
}
}
std::string ShaderGL::getDebugInfo() const
{
return mState.getTranslatedSource();
}
GLuint ShaderGL::getShaderID() const
{
return mShaderID;
}
} // namespace rx