Hash :
5217beb2
Author :
Date :
2023-08-15T13:43:12
Reland "Tightly pack LinkedUniform by using int16_t" This is a reland of commit 152cf62b38874238095a91307e4ea9bcdedf8f46 Original change's description: > Tightly pack LinkedUniform by using int16_t > > There is a check of vector size when we link uniforms and the maximum > vector size is 4096 due to we clamp the maxUniformBlockSize to 64KB. In > reality, if we exceeds this number, program link will take really long > time and then hit failure. So there is no real need to keep all the > variables in 32 bit integer. This CL changes to 16 bit integer. Further, > sh::BlockMemberInfo and ActiveVariable data members are embeded into > LinkedUniform struct as well so that the unused variables can be removed > and data can be tightly packed. This also makes LinkedUniform easier to > maintain as a simple struct with basic data types. With this change, > LinkedUniform size is reduced from 108 bytes down to 60 bytes, 48 bytes > reduction. Given some apps has 200-ish uniforms, this CL reduces 48 > bytes x 200 = ~9K memory just for uniforms per program (which goes > through hash compute and decompression and file reads). > > Bug: b/275102061 > Change-Id: I7fae20f5b75f3239305e2094a992e3040b8c8e4c > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4754133 > Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> > Commit-Queue: Charlie Lao <cclao@google.com> Bug: b/275102061 Change-Id: I1cdec9407e930608d3239a104dcbf77c8d8e2113 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4791661 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Charlie Lao <cclao@google.com>
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
//
// 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;
// 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 LinkedUniform &otherUniform);
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; }
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;
};
// 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();
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 LinkedUniform &other);
LinkedUniform(const UsedUniform &usedUniform);
~LinkedUniform();
bool isSampler() const { return GetUniformTypeInfo(type).isSampler; }
bool isImage() const { return GetUniformTypeInfo(type).isImageType; }
bool isAtomicCounter() const { return IsAtomicCounterType(type); }
bool isInDefaultBlock() const { return bufferIndex == -1; }
size_t getElementSize() const { return GetUniformTypeInfo(type).externalSize; }
GLint getElementComponents() const { return GetUniformTypeInfo(type).componentCount; }
bool isTexelFetchStaticUse() const { return flagBits.texelFetchStaticUse; }
bool isFragmentInOut() const { return flagBits.isFragmentInOut; }
bool isArray() const { return flagBits.isArray; }
uint16_t getBasicTypeElementCount() const
{
ASSERT(flagBits.isArray || arraySize == 1u);
return arraySize;
}
GLenum getType() const { return type; }
uint16_t getOuterArrayOffset() const { return outerArrayOffset; }
uint16_t getOuterArraySizeProduct() const { return outerArraySizeProduct; }
int16_t getBinding() const { return binding; }
int16_t getOffset() const { return offset; }
int getBufferIndex() const { return bufferIndex; }
int getLocation() const { return location; }
GLenum getImageUnitFormat() const { return imageUnitFormat; }
ShaderType getFirstActiveShaderType() const
{
return static_cast<ShaderType>(ScanForward(mActiveUseBits.bits()));
}
void setActive(ShaderType shaderType, bool used, uint32_t _id)
{
mActiveUseBits.set(shaderType, used);
mIds[shaderType] = id;
}
bool isActive(ShaderType shaderType) const { 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()); }
uint16_t type;
uint16_t precision;
int 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 mActiveUseBits;
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> mIds;
};
ANGLE_DISABLE_STRUCT_PADDING_WARNINGS
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 LinkedUniform &otherUniform)
{
activeVariable.unionReferencesWith(otherUniform);
}
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_