1 //
2 // Copyright 2010 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 #include "libANGLE/Uniform.h"
8 #include "common/BinaryStream.h"
9 #include "libANGLE/ProgramLinkedResources.h"
10
11 #include <cstring>
12
13 namespace gl
14 {
15
ActiveVariable()16 ActiveVariable::ActiveVariable()
17 {
18 std::fill(mIds.begin(), mIds.end(), 0);
19 }
20
~ActiveVariable()21 ActiveVariable::~ActiveVariable() {}
22
23 ActiveVariable::ActiveVariable(const ActiveVariable &rhs) = default;
24 ActiveVariable &ActiveVariable::operator=(const ActiveVariable &rhs) = default;
25
setActive(ShaderType shaderType,bool used,uint32_t id)26 void ActiveVariable::setActive(ShaderType shaderType, bool used, uint32_t id)
27 {
28 ASSERT(shaderType != ShaderType::InvalidEnum);
29 mActiveUseBits.set(shaderType, used);
30 mIds[shaderType] = id;
31 }
32
unionReferencesWith(const ActiveVariable & other)33 void ActiveVariable::unionReferencesWith(const ActiveVariable &other)
34 {
35 mActiveUseBits |= other.mActiveUseBits;
36 for (const ShaderType shaderType : AllShaderTypes())
37 {
38 ASSERT(mIds[shaderType] == 0 || other.mIds[shaderType] == 0 ||
39 mIds[shaderType] == other.mIds[shaderType]);
40 if (mIds[shaderType] == 0)
41 {
42 mIds[shaderType] = other.mIds[shaderType];
43 }
44 }
45 }
46
LinkedUniform()47 LinkedUniform::LinkedUniform()
48 {
49 mFixedSizeData.type = GL_NONE;
50 mFixedSizeData.precision = 0;
51 mFixedSizeData.flagBitsAsUInt = 0;
52 mFixedSizeData.location = -1;
53 mFixedSizeData.binding = -1;
54 mFixedSizeData.imageUnitFormat = GL_NONE;
55 mFixedSizeData.offset = -1;
56 mFixedSizeData.id = 0;
57 mFixedSizeData.flattenedOffsetInParentArrays = -1;
58 typeInfo = nullptr;
59 mFixedSizeData.bufferIndex = -1;
60 mFixedSizeData.blockInfo = sh::kDefaultBlockMemberInfo;
61 mFixedSizeData.outerArraySizeProduct = 1;
62 mFixedSizeData.outerArrayOffset = 0;
63 }
64
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)65 LinkedUniform::LinkedUniform(GLenum typeIn,
66 GLenum precisionIn,
67 const std::string &nameIn,
68 const std::vector<unsigned int> &arraySizesIn,
69 const int bindingIn,
70 const int offsetIn,
71 const int locationIn,
72 const int bufferIndexIn,
73 const sh::BlockMemberInfo &blockInfoIn)
74 {
75 mFixedSizeData.type = typeIn;
76 mFixedSizeData.precision = precisionIn;
77 mFixedSizeData.location = locationIn;
78 mFixedSizeData.binding = bindingIn;
79 mFixedSizeData.offset = offsetIn;
80 mFixedSizeData.bufferIndex = bufferIndexIn;
81 mFixedSizeData.blockInfo = blockInfoIn;
82 mFixedSizeData.flagBitsAsUInt = 0;
83 mFixedSizeData.id = 0;
84 mFixedSizeData.flattenedOffsetInParentArrays = -1;
85 mFixedSizeData.outerArraySizeProduct = 1;
86 mFixedSizeData.outerArrayOffset = 0;
87 mFixedSizeData.imageUnitFormat = GL_NONE;
88
89 name = nameIn;
90 typeInfo = &GetUniformTypeInfo(typeIn);
91 arraySizes = arraySizesIn;
92
93 ASSERT(!isArrayOfArrays());
94 ASSERT(!isArray() || !isStruct());
95 }
96
LinkedUniform(const LinkedUniform & other)97 LinkedUniform::LinkedUniform(const LinkedUniform &other)
98 {
99 *this = other;
100 }
101
LinkedUniform(const UsedUniform & usedUniform)102 LinkedUniform::LinkedUniform(const UsedUniform &usedUniform)
103 {
104 mFixedSizeData.type = usedUniform.type;
105 mFixedSizeData.precision = usedUniform.precision;
106
107 mFixedSizeData.flagBits.staticUse = usedUniform.staticUse;
108 mFixedSizeData.flagBits.active = usedUniform.active;
109 mFixedSizeData.flagBits.isStruct = usedUniform.isStruct();
110 mFixedSizeData.flagBits.rasterOrdered = usedUniform.rasterOrdered;
111 mFixedSizeData.flagBits.readonly = usedUniform.readonly;
112 mFixedSizeData.flagBits.writeonly = usedUniform.writeonly;
113 mFixedSizeData.flagBits.isFragmentInOut = usedUniform.isFragmentInOut;
114 mFixedSizeData.flagBits.texelFetchStaticUse = usedUniform.texelFetchStaticUse;
115
116 mFixedSizeData.flattenedOffsetInParentArrays = usedUniform.getFlattenedOffsetInParentArrays();
117 mFixedSizeData.location = usedUniform.location;
118 mFixedSizeData.binding = usedUniform.binding;
119 mFixedSizeData.imageUnitFormat = usedUniform.imageUnitFormat;
120 mFixedSizeData.offset = usedUniform.offset;
121 mFixedSizeData.id = usedUniform.id;
122 mFixedSizeData.bufferIndex = usedUniform.bufferIndex;
123 mFixedSizeData.blockInfo = usedUniform.blockInfo;
124 mFixedSizeData.outerArraySizeProduct = ArraySizeProduct(usedUniform.outerArraySizes);
125 mFixedSizeData.outerArrayOffset = usedUniform.outerArrayOffset;
126 mFixedSizeData.activeVariable = usedUniform.activeVariable;
127
128 name = usedUniform.name;
129 mappedName = usedUniform.mappedName;
130 arraySizes = usedUniform.arraySizes;
131 typeInfo = usedUniform.typeInfo;
132 }
133
operator =(const LinkedUniform & other)134 LinkedUniform &LinkedUniform::operator=(const LinkedUniform &other)
135 {
136 mFixedSizeData = other.mFixedSizeData;
137
138 name = other.name;
139 mappedName = other.mappedName;
140 arraySizes = other.arraySizes;
141 typeInfo = other.typeInfo;
142
143 return *this;
144 }
145
~LinkedUniform()146 LinkedUniform::~LinkedUniform() {}
147
save(BinaryOutputStream * stream) const148 void LinkedUniform::save(BinaryOutputStream *stream) const
149 {
150 // mFixedSizeData is a simple structure with fundamental data types, we can just do bulk save
151 // for performance.
152 stream->writeBytes(reinterpret_cast<const unsigned char *>(&mFixedSizeData),
153 sizeof(mFixedSizeData));
154
155 stream->writeString(name);
156 stream->writeString(mappedName);
157 stream->writeIntVector(arraySizes);
158 }
159
load(BinaryInputStream * stream)160 void LinkedUniform::load(BinaryInputStream *stream)
161 {
162 // mFixedSizeData is a simple structure with fundamental data types, we can just do bulk load
163 // for performance.
164 stream->readBytes(reinterpret_cast<unsigned char *>(&mFixedSizeData), sizeof(mFixedSizeData));
165
166 stream->readString(&name);
167 stream->readString(&mappedName);
168 stream->readIntVector<unsigned int>(&arraySizes);
169
170 typeInfo = &GetUniformTypeInfo(getType());
171 }
172
BufferVariable()173 BufferVariable::BufferVariable()
174 : bufferIndex(-1), blockInfo(sh::kDefaultBlockMemberInfo), topLevelArraySize(-1)
175 {}
176
BufferVariable(GLenum typeIn,GLenum precisionIn,const std::string & nameIn,const std::vector<unsigned int> & arraySizesIn,const int bufferIndexIn,const sh::BlockMemberInfo & blockInfoIn)177 BufferVariable::BufferVariable(GLenum typeIn,
178 GLenum precisionIn,
179 const std::string &nameIn,
180 const std::vector<unsigned int> &arraySizesIn,
181 const int bufferIndexIn,
182 const sh::BlockMemberInfo &blockInfoIn)
183 : bufferIndex(bufferIndexIn), blockInfo(blockInfoIn), topLevelArraySize(-1)
184 {
185 type = typeIn;
186 precision = precisionIn;
187 name = nameIn;
188 arraySizes = arraySizesIn;
189 }
190
~BufferVariable()191 BufferVariable::~BufferVariable() {}
192
ShaderVariableBuffer()193 ShaderVariableBuffer::ShaderVariableBuffer() : binding(0), dataSize(0) {}
194
195 ShaderVariableBuffer::ShaderVariableBuffer(const ShaderVariableBuffer &other) = default;
196
~ShaderVariableBuffer()197 ShaderVariableBuffer::~ShaderVariableBuffer() {}
198
numActiveVariables() const199 int ShaderVariableBuffer::numActiveVariables() const
200 {
201 return static_cast<int>(memberIndexes.size());
202 }
203
InterfaceBlock()204 InterfaceBlock::InterfaceBlock() : isArray(false), isReadOnly(false), arrayElement(0) {}
205
InterfaceBlock(const std::string & nameIn,const std::string & mappedNameIn,bool isArrayIn,bool isReadOnlyIn,unsigned int arrayElementIn,unsigned int firstFieldArraySizeIn,int bindingIn)206 InterfaceBlock::InterfaceBlock(const std::string &nameIn,
207 const std::string &mappedNameIn,
208 bool isArrayIn,
209 bool isReadOnlyIn,
210 unsigned int arrayElementIn,
211 unsigned int firstFieldArraySizeIn,
212 int bindingIn)
213 : name(nameIn),
214 mappedName(mappedNameIn),
215 isArray(isArrayIn),
216 isReadOnly(isReadOnlyIn),
217 arrayElement(arrayElementIn),
218 firstFieldArraySize(firstFieldArraySizeIn)
219 {
220 binding = bindingIn;
221 }
222
223 InterfaceBlock::InterfaceBlock(const InterfaceBlock &other) = default;
224
nameWithArrayIndex() const225 std::string InterfaceBlock::nameWithArrayIndex() const
226 {
227 std::stringstream fullNameStr;
228 fullNameStr << name;
229 if (isArray)
230 {
231 fullNameStr << "[" << arrayElement << "]";
232 }
233
234 return fullNameStr.str();
235 }
236
mappedNameWithArrayIndex() const237 std::string InterfaceBlock::mappedNameWithArrayIndex() const
238 {
239 std::stringstream fullNameStr;
240 fullNameStr << mappedName;
241 if (isArray)
242 {
243 fullNameStr << "[" << arrayElement << "]";
244 }
245
246 return fullNameStr.str();
247 }
248 } // namespace gl
249