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
//
// 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.
//
#ifndef LIBANGLE_UNIFORM_H_
#define LIBANGLE_UNIFORM_H_
#include <string>
#include <vector>
#include "angle_gl.h"
#include "common/MemoryBuffer.h"
#include "common/debug.h"
#include "common/utilities.h"
#include "compiler/translator/blocklayout.h"
#include "libANGLE/angletypes.h"
namespace gl
{
class BinaryInputStream;
class BinaryOutputStream;
struct UniformTypeInfo;
struct UsedUniform;
struct LinkedUniform;
#define ACTIVE_VARIABLE_COMMON_INTERFACES \
void setActive(ShaderType shaderType, bool used, uint32_t id) \
{ \
ASSERT(shaderType != ShaderType::InvalidEnum); \
pod.activeUseBits.set(shaderType, used); \
pod.ids[shaderType] = id; \
} \
ShaderType getFirstActiveShaderType() const \
{ \
return pod.activeUseBits.first(); \
} \
bool isActive(ShaderType shaderType) const \
{ \
return pod.activeUseBits[shaderType]; \
} \
const ShaderMap<uint32_t> &getIds() const \
{ \
return pod.ids; \
} \
uint32_t getId(ShaderType shaderType) const \
{ \
return pod.ids[shaderType]; \
} \
ShaderBitSet activeShaders() const \
{ \
return pod.activeUseBits; \
} \
uint32_t activeShaderCount() const \
{ \
return static_cast<uint32_t>(pod.activeUseBits.count()); \
}
struct ActiveVariable
{
ActiveVariable() { memset(&pod, 0, sizeof(pod)); }
ACTIVE_VARIABLE_COMMON_INTERFACES
struct PODStruct
{
ShaderBitSet activeUseBits;
// The id of a linked variable in each shader stage. This id originates from
// sh::ShaderVariable::id or sh::InterfaceBlock::id
ShaderMap<uint32_t> ids;
} pod;
};
// Important: This struct must have basic data types only, so that we can initialize with memcpy. Do
// not put any std::vector or objects with virtual functions in it.
// Helper struct representing a single shader uniform. Most of this structure's data member and
// access functions mirrors ShaderVariable; See ShaderVars.h for more info.
ANGLE_ENABLE_STRUCT_PADDING_WARNINGS
struct LinkedUniform
{
LinkedUniform() = default;
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);
LinkedUniform(const UsedUniform &usedUniform);
bool isSampler() const { return GetUniformTypeInfo(pod.type).isSampler; }
bool isImage() const { return GetUniformTypeInfo(pod.type).isImageType; }
bool isAtomicCounter() const { return IsAtomicCounterType(pod.type); }
bool isInDefaultBlock() const { return pod.bufferIndex == -1; }
size_t getElementSize() const { return GetUniformTypeInfo(pod.type).externalSize; }
GLint getElementComponents() const { return GetUniformTypeInfo(pod.type).componentCount; }
bool isTexelFetchStaticUse() const { return pod.flagBits.texelFetchStaticUse; }
bool isFragmentInOut() const { return pod.flagBits.isFragmentInOut; }
bool isArray() const { return pod.flagBits.isArray; }
uint16_t getBasicTypeElementCount() const
{
ASSERT(pod.flagBits.isArray || pod.arraySize == 1u);
return pod.arraySize;
}
GLenum getType() const { return pod.type; }
uint16_t getOuterArrayOffset() const { return pod.outerArrayOffset; }
uint16_t getOuterArraySizeProduct() const { return pod.outerArraySizeProduct; }
int16_t getBinding() const { return pod.binding; }
int16_t getOffset() const { return pod.offset; }
int getBufferIndex() const { return pod.bufferIndex; }
int getLocation() const { return pod.location; }
GLenum getImageUnitFormat() const { return pod.imageUnitFormat; }
ACTIVE_VARIABLE_COMMON_INTERFACES
struct PODStruct
{
uint16_t type;
uint16_t precision;
int32_t location;
// These are from sh::struct BlockMemberInfo struct. See locklayout.h for detail.
uint16_t blockOffset;
uint16_t blockArrayStride;
uint16_t blockMatrixStride;
uint16_t imageUnitFormat;
// maxUniformVectorsCount is 4K due to we clamp maxUniformBlockSize to 64KB. All of these
// variable should be enough to pack into 16 bits to reduce the size of mUniforms.
int16_t binding;
int16_t bufferIndex;
int16_t offset;
uint16_t arraySize;
uint16_t outerArraySizeProduct;
uint16_t outerArrayOffset;
uint16_t parentArrayIndex;
union
{
struct
{
uint8_t isFragmentInOut : 1;
uint8_t texelFetchStaticUse : 1;
uint8_t isArray : 1;
uint8_t blockIsRowMajorMatrix : 1;
uint8_t isBlock : 1;
uint8_t padding : 3;
} flagBits;
uint8_t flagBitsAsUByte;
};
ShaderBitSet activeUseBits;
uint32_t id;
// The id of a linked variable in each shader stage. This id originates from
// sh::ShaderVariable::id or sh::InterfaceBlock::id
ShaderMap<uint32_t> ids;
} pod;
};
ANGLE_DISABLE_STRUCT_PADDING_WARNINGS
struct BufferVariable
{
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);
~BufferVariable() {}
bool isArray() const { return pod.isArray; }
uint32_t getBasicTypeElementCount() const { return pod.basicTypeElementCount; }
ACTIVE_VARIABLE_COMMON_INTERFACES
std::string name;
std::string mappedName;
ANGLE_ENABLE_STRUCT_PADDING_WARNINGS
struct PODStruct
{
uint16_t type;
uint16_t precision;
// 1 byte each
ShaderBitSet activeUseBits;
bool isArray;
int16_t bufferIndex;
// The id of a linked variable in each shader stage. This id originates from
// sh::ShaderVariable::id or sh::InterfaceBlock::id
ShaderMap<uint32_t> ids;
sh::BlockMemberInfo blockInfo;
int32_t topLevelArraySize;
uint32_t basicTypeElementCount;
} pod;
ANGLE_DISABLE_STRUCT_PADDING_WARNINGS
};
// Represents a single atomic counter buffer
struct AtomicCounterBuffer
{
AtomicCounterBuffer();
~AtomicCounterBuffer() {}
ACTIVE_VARIABLE_COMMON_INTERFACES
int numActiveVariables() const { return static_cast<int>(memberIndexes.size()); }
void unionReferencesWith(const LinkedUniform &otherUniform);
std::vector<unsigned int> memberIndexes;
ANGLE_ENABLE_STRUCT_PADDING_WARNINGS
struct PODStruct
{
// The id of a linked variable in each shader stage. This id originates from
// sh::ShaderVariable::id or sh::InterfaceBlock::id
ShaderMap<uint32_t> ids;
// The binding as specified by the shader.
int inShaderBinding;
unsigned int dataSize;
ShaderBitSet activeUseBits;
uint8_t pads[3];
} pod;
ANGLE_DISABLE_STRUCT_PADDING_WARNINGS
};
// Helper struct representing a single shader interface block
struct InterfaceBlock
{
InterfaceBlock();
InterfaceBlock(const std::string &nameIn,
const std::string &mappedNameIn,
bool isArrayIn,
bool isReadOnlyIn,
unsigned int arrayElementIn,
unsigned int firstFieldArraySizeIn,
int bindingIn);
std::string nameWithArrayIndex() const;
std::string mappedNameWithArrayIndex() const;
ACTIVE_VARIABLE_COMMON_INTERFACES
int numActiveVariables() const { return static_cast<int>(memberIndexes.size()); }
std::string name;
std::string mappedName;
std::vector<unsigned int> memberIndexes;
ANGLE_ENABLE_STRUCT_PADDING_WARNINGS
struct PODStruct
{
uint32_t arrayElement;
uint32_t firstFieldArraySize;
ShaderBitSet activeUseBits;
uint8_t isArray : 1;
// Only valid for SSBOs, specifies whether it has the readonly qualifier.
uint8_t isReadOnly : 1;
uint8_t padings : 6;
// The binding as specified by the shader (0 if unspecified, per spec)
int16_t inShaderBinding;
unsigned int dataSize;
// The id of a linked variable in each shader stage. This id originates from
// sh::ShaderVariable::id or sh::InterfaceBlock::id
ShaderMap<uint32_t> ids;
} pod;
ANGLE_DISABLE_STRUCT_PADDING_WARNINGS
};
#undef ACTIVE_VARIABLE_COMMON_INTERFACES
} // namespace gl
#endif // LIBANGLE_UNIFORM_H_