Hash :
0c4d6446
Author :
Date :
2024-01-24T10:38:45
Rework uniform block <-> uniform buffer mapping
In GLES, the shader declares which buffer binding a block (uniform,
storage or atomic counter) is bound to. For example:
layout(binding = 1) uniform ubo0 { ... };
layout(binding = 2) uniform ubo1 { ... };
layout(binding = 1) uniform ubo2 { ... };
In the above, ubo0 and ubo2 use data from the buffer bound to index 2
(through glBindBufferRange), while ubo1 uses data from the buffer bound
to index 1. For uniform blocks in particular, omitting the binding
is allowed, in which case it is implicitly bound to buffer 0.
GLES allows uniform blocks (and only uniform blocks) to remap their
bindings through calls to glUniformBlockBinding. This means that the
mapping of uniform blocks in the program (ubo0, ubo1, ubo2) to the
buffer bindings is not constant. For storage blocks and atomic counter
buffers, this binding _is_ constant and is determined at link time.
At link time, the mapping of blocks to buffers is determined based on
values specified in the shaders. This info is stored was stored in
gl::InterfaceBlock::binding (for UBOs and SSBOs), and
gl::AtomicCounterBuffer::binding. For clarity, this change renames
these members to ...::inShaderBinding.
When glUniformBlockBinding is called, the mapping is updated. Prior to
this change, gl::InterfaceBlock::binding was directly updated, trumping
the mapping determined at link time. A bug here was that after a call
to glProgramBinary, GL expects the mappings to reset to their original
link-time values, but instead ANGLE restored the mappings to what was
configured at the time the binary was retrieved.
This change tracks the uniform block -> buffer binding mapping
separately from the link results so that the original values can be
restored during glProgramBinary. In the process, the support data
structures for tracking this mapping are moved to ProgramExecutable and
the algorithms are simplified. Program Pipeline Objects maintain this
mapping identically to Programs and no longer require a special and more
costly path when a buffer state changes.
This change prepares for but does not yet fix the more fundamental bug
that the dirty bits are tracked in the program executable instead of the
context state, which makes changes not propagate to all contexts
correctly.
Bug: angleproject:8493
Change-Id: Ib0999f49be24db06ebe9a4917d06b90af899611e
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5235883
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Charlie Lao <cclao@google.com>
Commit-Queue: 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 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
//
// Copyright 2023 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.
//
// ProgramExecutableGL.cpp: Implementation of ProgramExecutableGL.
#include "libANGLE/renderer/gl/ProgramExecutableGL.h"
#include "common/string_utils.h"
#include "libANGLE/Context.h"
#include "libANGLE/Program.h"
#include "libANGLE/Uniform.h"
#include "libANGLE/renderer/gl/ContextGL.h"
#include "libANGLE/renderer/gl/FunctionsGL.h"
#include "libANGLE/renderer/gl/RendererGL.h"
#include "libANGLE/renderer/gl/StateManagerGL.h"
namespace rx
{
ProgramExecutableGL::ProgramExecutableGL(const gl::ProgramExecutable *executable)
: ProgramExecutableImpl(executable),
mHasAppliedTransformFeedbackVaryings(false),
mClipDistanceEnabledUniformLocation(-1),
mClipOriginUniformLocation(-1),
mMultiviewBaseViewLayerIndexUniformLocation(-1),
mProgramID(0),
mFunctions(nullptr),
mStateManager(nullptr)
{}
ProgramExecutableGL::~ProgramExecutableGL() {}
void ProgramExecutableGL::destroy(const gl::Context *context) {}
void ProgramExecutableGL::reset()
{
mUniformRealLocationMap.clear();
mUniformBlockRealLocationMap.clear();
mClipDistanceEnabledUniformLocation = -1;
mClipOriginUniformLocation = -1;
mMultiviewBaseViewLayerIndexUniformLocation = -1;
}
void ProgramExecutableGL::postLink(const FunctionsGL *functions,
StateManagerGL *stateManager,
const angle::FeaturesGL &features,
GLuint programID)
{
// Cache the following so the executable is capable of making the appropriate GL calls without
// having to involve ProgramGL.
mProgramID = programID;
mFunctions = functions;
mStateManager = stateManager;
// Query the uniform information
ASSERT(mUniformRealLocationMap.empty());
const auto &uniformLocations = mExecutable->getUniformLocations();
const auto &uniforms = mExecutable->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];
const std::string &uniformMappedName = mExecutable->getUniformMappedNames()[entry.index];
std::stringstream fullNameStr;
if (uniform.isArray())
{
ASSERT(angle::EndsWith(uniformMappedName, "[0]"));
fullNameStr << uniformMappedName.substr(0, uniformMappedName.length() - 3);
fullNameStr << "[" << entry.arrayIndex << "]";
}
else
{
fullNameStr << uniformMappedName;
}
const std::string &fullName = fullNameStr.str();
GLint realLocation = functions->getUniformLocation(programID, fullName.c_str());
mUniformRealLocationMap[uniformLocation] = realLocation;
}
if (features.emulateClipDistanceState.enabled && mExecutable->hasClipDistance())
{
ASSERT(functions->standard == STANDARD_GL_ES);
mClipDistanceEnabledUniformLocation =
functions->getUniformLocation(programID, "angle_ClipDistanceEnabled");
ASSERT(mClipDistanceEnabledUniformLocation != -1);
}
if (features.emulateClipOrigin.enabled)
{
ASSERT(functions->standard == STANDARD_GL_ES);
mClipOriginUniformLocation = functions->getUniformLocation(programID, "angle_ClipOrigin");
}
if (mExecutable->usesMultiview())
{
mMultiviewBaseViewLayerIndexUniformLocation =
functions->getUniformLocation(programID, "multiviewBaseViewLayerIndex");
ASSERT(mMultiviewBaseViewLayerIndexUniformLocation != -1);
}
}
void ProgramExecutableGL::updateEnabledClipDistances(uint8_t enabledClipDistancesPacked) const
{
ASSERT(mExecutable->hasClipDistance());
ASSERT(mClipDistanceEnabledUniformLocation != -1);
ASSERT(mFunctions->programUniform1ui != nullptr);
mFunctions->programUniform1ui(mProgramID, mClipDistanceEnabledUniformLocation,
enabledClipDistancesPacked);
}
void ProgramExecutableGL::updateEmulatedClipOrigin(gl::ClipOrigin origin) const
{
if (mClipOriginUniformLocation == -1)
{
// A driver may optimize away the uniform when gl_Position.y is always zero.
return;
}
const float originValue = (origin == gl::ClipOrigin::LowerLeft) ? 1.0f : -1.0f;
if (mFunctions->programUniform1f != nullptr)
{
mFunctions->programUniform1f(mProgramID, mClipOriginUniformLocation, originValue);
}
else
{
mStateManager->useProgram(mProgramID);
mFunctions->uniform1f(mClipOriginUniformLocation, originValue);
}
}
void ProgramExecutableGL::enableLayeredRenderingPath(int baseViewIndex) const
{
ASSERT(mExecutable->usesMultiview());
ASSERT(mMultiviewBaseViewLayerIndexUniformLocation != -1);
ASSERT(mFunctions->programUniform1i != nullptr);
mFunctions->programUniform1i(mProgramID, mMultiviewBaseViewLayerIndexUniformLocation,
baseViewIndex);
}
void ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::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 ProgramExecutableGL::getUniformfv(const gl::Context *context,
GLint location,
GLfloat *params) const
{
mFunctions->getUniformfv(mProgramID, uniLoc(location), params);
}
void ProgramExecutableGL::getUniformiv(const gl::Context *context,
GLint location,
GLint *params) const
{
mFunctions->getUniformiv(mProgramID, uniLoc(location), params);
}
void ProgramExecutableGL::getUniformuiv(const gl::Context *context,
GLint location,
GLuint *params) const
{
mFunctions->getUniformuiv(mProgramID, uniLoc(location), params);
}
void ProgramExecutableGL::setUniformBlockBinding(GLuint uniformBlockIndex,
GLuint uniformBlockBinding)
{
// Lazy init
if (mUniformBlockRealLocationMap.empty())
{
mUniformBlockRealLocationMap.reserve(mExecutable->getUniformBlocks().size());
for (const gl::InterfaceBlock &uniformBlock : mExecutable->getUniformBlocks())
{
const std::string &mappedNameWithIndex = uniformBlock.mappedNameWithArrayIndex();
GLuint blockIndex =
mFunctions->getUniformBlockIndex(mProgramID, mappedNameWithIndex.c_str());
mUniformBlockRealLocationMap.push_back(blockIndex);
}
}
const GLuint realBlockIndex = mUniformBlockRealLocationMap[uniformBlockIndex];
if (realBlockIndex != GL_INVALID_INDEX)
{
mFunctions->uniformBlockBinding(mProgramID, realBlockIndex, uniformBlockBinding);
}
}
void ProgramExecutableGL::reapplyUBOBindings()
{
const std::vector<gl::InterfaceBlock> &blocks = mExecutable->getUniformBlocks();
for (size_t blockIndex = 0; blockIndex < blocks.size(); ++blockIndex)
{
if (blocks[blockIndex].activeShaders().any())
{
const GLuint index = static_cast<GLuint>(blockIndex);
setUniformBlockBinding(index, mExecutable->getUniformBlockBinding(index));
}
}
}
void ProgramExecutableGL::syncUniformBlockBindings()
{
for (size_t uniformBlockIndex : mDirtyUniformBlockBindings)
{
const GLuint index = static_cast<GLuint>(uniformBlockIndex);
setUniformBlockBinding(index, mExecutable->getUniformBlockBinding(index));
}
mDirtyUniformBlockBindings.reset();
}
} // namespace rx