Hash :
22438ad8
Author :
Date :
2023-08-02T14:01:45
Embed ActiveVariable into BufferVariable and ShaderVariableBuffer This CL embeds ActiveVariable into BufferVariable and ShaderVariableBuffer struct instead of subclass. This allows us to remove the virtual function of ~ActiveVariable(), which means ActiveVariable is a simple struct with basic types and memcpy can be used for load/save. Thus, in this CL, I also moved activeVariables to LinkedUniform::mFixedSizeData structure and let memcpy handle the load/save. Bug: b/275102061 Change-Id: I8d21080cfdd72d4d22cef927d136ca428d9b12e4 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4722265 Reviewed-by: Roman Lavrov <romanl@google.com> Commit-Queue: Charlie Lao <cclao@google.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
//
// 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;
// Note: keep this struct memcpy-able: i.e, a simple struct with basic types only and no virtual
// functions. LinkedUniform relies on this so that it can use memcpy to initialize uniform for
// performance.
struct ActiveVariable
{
ActiveVariable();
ActiveVariable(const ActiveVariable &rhs);
~ActiveVariable();
ActiveVariable &operator=(const ActiveVariable &rhs);
ShaderType getFirstActiveShaderType() const
{
return static_cast<ShaderType>(ScanForward(mActiveUseBits.bits()));
}
void setActive(ShaderType shaderType, bool used, uint32_t id);
void unionReferencesWith(const ActiveVariable &other);
bool isActive(ShaderType shaderType) const
{
ASSERT(shaderType != ShaderType::InvalidEnum);
return mActiveUseBits[shaderType];
}
const ShaderMap<uint32_t> &getIds() const { return mIds; }
uint32_t getId(ShaderType shaderType) const { return mIds[shaderType]; }
ShaderBitSet activeShaders() const { return mActiveUseBits; }
GLuint activeShaderCount() const { return static_cast<GLuint>(mActiveUseBits.count()); }
private:
ShaderBitSet mActiveUseBits;
// The id of a linked variable in each shader stage. This id originates from
// sh::ShaderVariable::id or sh::InterfaceBlock::id
ShaderMap<uint32_t> mIds;
};
// 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.
struct LinkedUniform
{
LinkedUniform();
LinkedUniform(GLenum typeIn,
GLenum precisionIn,
const std::string &nameIn,
const std::vector<unsigned int> &arraySizesIn,
const int bindingIn,
const int offsetIn,
const int locationIn,
const int bufferIndexIn,
const sh::BlockMemberInfo &blockInfoIn);
LinkedUniform(const LinkedUniform &other);
LinkedUniform(const UsedUniform &usedUniform);
LinkedUniform &operator=(const LinkedUniform &other);
~LinkedUniform();
void setBlockInfo(int offset, int arrayStride, int matrixStride, bool isRowMajorMatrix)
{
mFixedSizeData.blockInfo.offset = offset;
mFixedSizeData.blockInfo.arrayStride = arrayStride;
mFixedSizeData.blockInfo.matrixStride = matrixStride;
mFixedSizeData.blockInfo.isRowMajorMatrix = isRowMajorMatrix;
}
void setBufferIndex(int bufferIndex) { mFixedSizeData.bufferIndex = bufferIndex; }
bool isSampler() const { return typeInfo->isSampler; }
bool isImage() const { return typeInfo->isImageType; }
bool isAtomicCounter() const { return IsAtomicCounterType(mFixedSizeData.type); }
bool isInDefaultBlock() const { return mFixedSizeData.bufferIndex == -1; }
size_t getElementSize() const { return typeInfo->externalSize; }
size_t getElementComponents() const { return typeInfo->componentCount; }
bool isStruct() const { return mFixedSizeData.flagBits.isStruct; }
bool isTexelFetchStaticUse() const { return mFixedSizeData.flagBits.texelFetchStaticUse; }
bool isFragmentInOut() const { return mFixedSizeData.flagBits.isFragmentInOut; }
bool isArrayOfArrays() const { return arraySizes.size() >= 2u; }
bool isArray() const { return !arraySizes.empty(); }
unsigned int getArraySizeProduct() const { return gl::ArraySizeProduct(arraySizes); }
unsigned int getOutermostArraySize() const { return isArray() ? arraySizes.back() : 0; }
unsigned int getBasicTypeElementCount() const
{
ASSERT(!isArrayOfArrays());
ASSERT(!isStruct() || !isArray());
if (isArray())
{
return getOutermostArraySize();
}
return 1u;
}
GLenum getType() const { return mFixedSizeData.type; }
unsigned int getExternalSize() const;
unsigned int getOuterArrayOffset() const { return mFixedSizeData.outerArrayOffset; }
unsigned int getOuterArraySizeProduct() const { return mFixedSizeData.outerArraySizeProduct; }
int getBinding() const { return mFixedSizeData.binding; }
int getOffset() const { return mFixedSizeData.offset; }
const sh::BlockMemberInfo &getBlockInfo() const { return mFixedSizeData.blockInfo; }
int getBufferIndex() const { return mFixedSizeData.bufferIndex; }
int getLocation() const { return mFixedSizeData.location; }
GLenum getImageUnitFormat() const { return mFixedSizeData.imageUnitFormat; }
bool findInfoByMappedName(const std::string &mappedFullName,
const sh::ShaderVariable **leafVar,
std::string *originalFullName) const;
bool isBuiltIn() const { return gl::IsBuiltInName(name); }
int parentArrayIndex() const
{
return hasParentArrayIndex() ? mFixedSizeData.flattenedOffsetInParentArrays : 0;
}
bool hasParentArrayIndex() const { return mFixedSizeData.flattenedOffsetInParentArrays != -1; }
bool isSameInterfaceBlockFieldAtLinkTime(const sh::ShaderVariable &other) const;
bool isSameVariableAtLinkTime(const sh::ShaderVariable &other,
bool matchPrecision,
bool matchName) const;
ShaderType getFirstActiveShaderType() const
{
return mFixedSizeData.activeVariable.getFirstActiveShaderType();
}
void setActive(ShaderType shaderType, bool used, uint32_t _id)
{
mFixedSizeData.activeVariable.setActive(shaderType, used, _id);
}
bool isActive(ShaderType shaderType) const
{
return mFixedSizeData.activeVariable.isActive(shaderType);
}
const ShaderMap<uint32_t> &getIds() const { return mFixedSizeData.activeVariable.getIds(); }
uint32_t getId(ShaderType shaderType) const
{
return mFixedSizeData.activeVariable.getId(shaderType);
}
ShaderBitSet activeShaders() const { return mFixedSizeData.activeVariable.activeShaders(); }
GLuint activeShaderCount() const { return mFixedSizeData.activeVariable.activeShaderCount(); }
const ActiveVariable &getActiveVariable() const { return mFixedSizeData.activeVariable; }
void save(BinaryOutputStream *stream) const;
void load(BinaryInputStream *stream);
std::string name;
// Only used by GL backend
std::string mappedName;
std::vector<unsigned int> arraySizes;
const UniformTypeInfo *typeInfo;
private:
// Important: The fixed size data structure with fundamental data types only, so that we can
// initialize with memcpy. Do not put any std::vector or objects with virtual functions in it.
struct
{
GLenum type;
GLenum precision;
int location;
int binding;
GLenum imageUnitFormat;
int offset;
uint32_t id;
int flattenedOffsetInParentArrays;
int bufferIndex;
sh::BlockMemberInfo blockInfo;
unsigned int outerArraySizeProduct;
unsigned int outerArrayOffset;
ActiveVariable activeVariable;
union
{
struct
{
uint32_t staticUse : 1;
uint32_t active : 1;
uint32_t isStruct : 1;
uint32_t rasterOrdered : 1;
uint32_t readonly : 1;
uint32_t writeonly : 1;
uint32_t isFragmentInOut : 1;
uint32_t texelFetchStaticUse : 1;
uint32_t padding : 24;
} flagBits;
uint32_t flagBitsAsUInt;
};
} mFixedSizeData;
};
struct BufferVariable : public sh::ShaderVariable
{
BufferVariable();
BufferVariable(GLenum type,
GLenum precision,
const std::string &name,
const std::vector<unsigned int> &arraySizes,
const int bufferIndex,
const sh::BlockMemberInfo &blockInfo);
~BufferVariable();
void setActive(ShaderType shaderType, bool used, uint32_t _id)
{
activeVariable.setActive(shaderType, used, _id);
}
bool isActive(ShaderType shaderType) const { return activeVariable.isActive(shaderType); }
uint32_t getId(ShaderType shaderType) const { return activeVariable.getId(shaderType); }
ShaderBitSet activeShaders() const { return activeVariable.activeShaders(); }
ActiveVariable activeVariable;
int bufferIndex;
sh::BlockMemberInfo blockInfo;
int topLevelArraySize;
};
// Parent struct for atomic counter, uniform block, and shader storage block buffer, which all
// contain a group of shader variables, and have a GL buffer backed.
struct ShaderVariableBuffer
{
ShaderVariableBuffer();
ShaderVariableBuffer(const ShaderVariableBuffer &other);
~ShaderVariableBuffer();
ShaderType getFirstActiveShaderType() const
{
return activeVariable.getFirstActiveShaderType();
}
void setActive(ShaderType shaderType, bool used, uint32_t _id)
{
activeVariable.setActive(shaderType, used, _id);
}
void unionReferencesWith(const ActiveVariable &other)
{
activeVariable.unionReferencesWith(other);
}
bool isActive(ShaderType shaderType) const { return activeVariable.isActive(shaderType); }
const ShaderMap<uint32_t> &getIds() const { return activeVariable.getIds(); }
uint32_t getId(ShaderType shaderType) const { return activeVariable.getId(shaderType); }
ShaderBitSet activeShaders() const { return activeVariable.activeShaders(); }
int numActiveVariables() const;
ActiveVariable activeVariable;
int binding;
unsigned int dataSize;
std::vector<unsigned int> memberIndexes;
};
using AtomicCounterBuffer = ShaderVariableBuffer;
// Helper struct representing a single shader interface block
struct InterfaceBlock : public ShaderVariableBuffer
{
InterfaceBlock();
InterfaceBlock(const std::string &nameIn,
const std::string &mappedNameIn,
bool isArrayIn,
bool isReadOnlyIn,
unsigned int arrayElementIn,
unsigned int firstFieldArraySizeIn,
int bindingIn);
InterfaceBlock(const InterfaceBlock &other);
std::string nameWithArrayIndex() const;
std::string mappedNameWithArrayIndex() const;
std::string name;
std::string mappedName;
bool isArray;
// Only valid for SSBOs, specifies whether it has the readonly qualifier.
bool isReadOnly;
unsigned int arrayElement;
unsigned int firstFieldArraySize;
};
} // namespace gl
#endif // LIBANGLE_UNIFORM_H_