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
//
// Copyright 2010 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.
//
#include "libANGLE/Uniform.h"
#include "common/BinaryStream.h"
#include "libANGLE/ProgramLinkedResources.h"
#include <cstring>
namespace gl
{
LinkedUniform::LinkedUniform(GLenum typeIn,
GLenum precisionIn,
const std::vector<unsigned int> &arraySizesIn,
const int bindingIn,
const int offsetIn,
const int locationIn,
const int bufferIndexIn,
const sh::BlockMemberInfo &blockInfoIn)
{
// arrays are always flattened, which means at most 1D array
ASSERT(arraySizesIn.size() <= 1);
memset(this, 0, sizeof(*this));
SetBitField(pod.type, typeIn);
SetBitField(pod.precision, precisionIn);
pod.location = locationIn;
SetBitField(pod.binding, bindingIn);
SetBitField(pod.offset, offsetIn);
SetBitField(pod.bufferIndex, bufferIndexIn);
pod.outerArraySizeProduct = 1;
SetBitField(pod.arraySize, arraySizesIn.empty() ? 1u : arraySizesIn[0]);
SetBitField(pod.flagBits.isArray, !arraySizesIn.empty());
if (!(blockInfoIn == sh::kDefaultBlockMemberInfo))
{
pod.flagBits.isBlock = 1;
pod.flagBits.blockIsRowMajorMatrix = blockInfoIn.isRowMajorMatrix;
SetBitField(pod.blockOffset, blockInfoIn.offset);
SetBitField(pod.blockArrayStride, blockInfoIn.arrayStride);
SetBitField(pod.blockMatrixStride, blockInfoIn.matrixStride);
}
}
LinkedUniform::LinkedUniform(const UsedUniform &usedUniform)
{
ASSERT(!usedUniform.isArrayOfArrays());
ASSERT(!usedUniform.isStruct());
ASSERT(usedUniform.active);
ASSERT(usedUniform.blockInfo == sh::kDefaultBlockMemberInfo);
// Note: Ensure every data member is initialized.
pod.flagBitsAsUByte = 0;
SetBitField(pod.type, usedUniform.type);
SetBitField(pod.precision, usedUniform.precision);
SetBitField(pod.imageUnitFormat, usedUniform.imageUnitFormat);
pod.location = usedUniform.location;
pod.blockOffset = 0;
pod.blockArrayStride = 0;
pod.blockMatrixStride = 0;
SetBitField(pod.binding, usedUniform.binding);
SetBitField(pod.offset, usedUniform.offset);
SetBitField(pod.bufferIndex, usedUniform.bufferIndex);
SetBitField(pod.parentArrayIndex, usedUniform.parentArrayIndex());
SetBitField(pod.outerArraySizeProduct, ArraySizeProduct(usedUniform.outerArraySizes));
SetBitField(pod.outerArrayOffset, usedUniform.outerArrayOffset);
SetBitField(pod.arraySize, usedUniform.isArray() ? usedUniform.getArraySizeProduct() : 1u);
SetBitField(pod.flagBits.isArray, usedUniform.isArray());
pod.id = usedUniform.id;
pod.activeUseBits = usedUniform.activeVariable.activeShaders();
pod.ids = usedUniform.activeVariable.getIds();
SetBitField(pod.flagBits.isFragmentInOut, usedUniform.isFragmentInOut);
SetBitField(pod.flagBits.texelFetchStaticUse, usedUniform.texelFetchStaticUse);
ASSERT(!usedUniform.isArray() || pod.arraySize == usedUniform.getArraySizeProduct());
}
BufferVariable::BufferVariable()
{
memset(&pod, 0, sizeof(pod));
pod.bufferIndex = -1;
pod.blockInfo = sh::kDefaultBlockMemberInfo;
pod.topLevelArraySize = -1;
}
BufferVariable::BufferVariable(GLenum type,
GLenum precision,
const std::string &name,
const std::vector<unsigned int> &arraySizes,
const int bufferIndex,
int topLevelArraySize,
const sh::BlockMemberInfo &blockInfo)
: name(name)
{
memset(&pod, 0, sizeof(pod));
SetBitField(pod.type, type);
SetBitField(pod.precision, precision);
SetBitField(pod.bufferIndex, bufferIndex);
pod.blockInfo = blockInfo;
SetBitField(pod.topLevelArraySize, topLevelArraySize);
pod.isArray = !arraySizes.empty();
SetBitField(pod.basicTypeElementCount, arraySizes.empty() ? 1u : arraySizes.back());
}
AtomicCounterBuffer::AtomicCounterBuffer()
{
memset(&pod, 0, sizeof(pod));
}
void AtomicCounterBuffer::unionReferencesWith(const LinkedUniform &other)
{
pod.activeUseBits |= other.pod.activeUseBits;
for (const ShaderType shaderType : AllShaderTypes())
{
ASSERT(pod.ids[shaderType] == 0 || other.getId(shaderType) == 0 ||
pod.ids[shaderType] == other.getId(shaderType));
if (pod.ids[shaderType] == 0)
{
pod.ids[shaderType] = other.getId(shaderType);
}
}
}
InterfaceBlock::InterfaceBlock()
{
memset(&pod, 0, sizeof(pod));
}
InterfaceBlock::InterfaceBlock(const std::string &name,
const std::string &mappedName,
bool isArray,
bool isReadOnly,
unsigned int arrayElementIn,
unsigned int firstFieldArraySizeIn,
int binding)
: name(name), mappedName(mappedName)
{
memset(&pod, 0, sizeof(pod));
SetBitField(pod.isArray, isArray);
SetBitField(pod.isReadOnly, isReadOnly);
SetBitField(pod.inShaderBinding, binding);
pod.arrayElement = arrayElementIn;
pod.firstFieldArraySize = firstFieldArraySizeIn;
}
std::string InterfaceBlock::nameWithArrayIndex() const
{
std::stringstream fullNameStr;
fullNameStr << name;
if (pod.isArray)
{
fullNameStr << "[" << pod.arrayElement << "]";
}
return fullNameStr.str();
}
std::string InterfaceBlock::mappedNameWithArrayIndex() const
{
std::stringstream fullNameStr;
fullNameStr << mappedName;
if (pod.isArray)
{
fullNameStr << "[" << pod.arrayElement << "]";
}
return fullNameStr.str();
}
} // namespace gl