xref: /aosp_15_r20/external/angle/src/libANGLE/Uniform.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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 
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)16 LinkedUniform::LinkedUniform(GLenum typeIn,
17                              GLenum precisionIn,
18                              const std::vector<unsigned int> &arraySizesIn,
19                              const int bindingIn,
20                              const int offsetIn,
21                              const int locationIn,
22                              const int bufferIndexIn,
23                              const sh::BlockMemberInfo &blockInfoIn)
24 {
25     // arrays are always flattened, which means at most 1D array
26     ASSERT(arraySizesIn.size() <= 1);
27 
28     memset(this, 0, sizeof(*this));
29     pod.typeIndex = GetUniformTypeIndex(typeIn);
30     SetBitField(pod.precision, precisionIn);
31     pod.location = locationIn;
32     SetBitField(pod.binding, bindingIn);
33     SetBitField(pod.offset, offsetIn);
34     SetBitField(pod.bufferIndex, bufferIndexIn);
35     pod.outerArraySizeProduct = 1;
36     SetBitField(pod.arraySize, arraySizesIn.empty() ? 1u : arraySizesIn[0]);
37     SetBitField(pod.flagBits.isArray, !arraySizesIn.empty());
38     if (!(blockInfoIn == sh::kDefaultBlockMemberInfo))
39     {
40         pod.flagBits.isBlock               = 1;
41         pod.flagBits.blockIsRowMajorMatrix = blockInfoIn.isRowMajorMatrix;
42         SetBitField(pod.blockOffset, blockInfoIn.offset);
43         SetBitField(pod.blockArrayStride, blockInfoIn.arrayStride);
44         SetBitField(pod.blockMatrixStride, blockInfoIn.matrixStride);
45     }
46 }
47 
LinkedUniform(const UsedUniform & usedUniform)48 LinkedUniform::LinkedUniform(const UsedUniform &usedUniform)
49 {
50     ASSERT(!usedUniform.isArrayOfArrays());
51     ASSERT(!usedUniform.isStruct());
52     ASSERT(usedUniform.active);
53     ASSERT(usedUniform.blockInfo == sh::kDefaultBlockMemberInfo);
54 
55     // Note: Ensure every data member is initialized.
56     pod.flagBitsAsUByte = 0;
57     pod.typeIndex       = GetUniformTypeIndex(usedUniform.type);
58     SetBitField(pod.precision, usedUniform.precision);
59     SetBitField(pod.imageUnitFormat, usedUniform.imageUnitFormat);
60     pod.location          = usedUniform.location;
61     pod.blockOffset       = 0;
62     pod.blockArrayStride  = 0;
63     pod.blockMatrixStride = 0;
64     SetBitField(pod.binding, usedUniform.binding);
65     SetBitField(pod.offset, usedUniform.offset);
66 
67     SetBitField(pod.bufferIndex, usedUniform.bufferIndex);
68     SetBitField(pod.parentArrayIndex, usedUniform.parentArrayIndex());
69     SetBitField(pod.outerArraySizeProduct, ArraySizeProduct(usedUniform.outerArraySizes));
70     SetBitField(pod.outerArrayOffset, usedUniform.outerArrayOffset);
71     SetBitField(pod.arraySize, usedUniform.isArray() ? usedUniform.getArraySizeProduct() : 1u);
72     SetBitField(pod.flagBits.isArray, usedUniform.isArray());
73 
74     pod.id            = usedUniform.id;
75     pod.activeUseBits = usedUniform.activeVariable.activeShaders();
76     pod.ids           = usedUniform.activeVariable.getIds();
77 
78     SetBitField(pod.flagBits.isFragmentInOut, usedUniform.isFragmentInOut);
79     SetBitField(pod.flagBits.texelFetchStaticUse, usedUniform.texelFetchStaticUse);
80     ASSERT(!usedUniform.isArray() || pod.arraySize == usedUniform.getArraySizeProduct());
81 }
82 
BufferVariable()83 BufferVariable::BufferVariable()
84 {
85     memset(&pod, 0, sizeof(pod));
86     pod.bufferIndex       = -1;
87     pod.blockInfo         = sh::kDefaultBlockMemberInfo;
88     pod.topLevelArraySize = -1;
89 }
90 
BufferVariable(GLenum type,GLenum precision,const std::string & name,const std::vector<unsigned int> & arraySizes,const int bufferIndex,int topLevelArraySize,const sh::BlockMemberInfo & blockInfo)91 BufferVariable::BufferVariable(GLenum type,
92                                GLenum precision,
93                                const std::string &name,
94                                const std::vector<unsigned int> &arraySizes,
95                                const int bufferIndex,
96                                int topLevelArraySize,
97                                const sh::BlockMemberInfo &blockInfo)
98     : name(name)
99 {
100     memset(&pod, 0, sizeof(pod));
101     SetBitField(pod.type, type);
102     SetBitField(pod.precision, precision);
103     SetBitField(pod.bufferIndex, bufferIndex);
104     pod.blockInfo = blockInfo;
105     SetBitField(pod.topLevelArraySize, topLevelArraySize);
106     pod.isArray = !arraySizes.empty();
107     SetBitField(pod.basicTypeElementCount, arraySizes.empty() ? 1u : arraySizes.back());
108 }
109 
AtomicCounterBuffer()110 AtomicCounterBuffer::AtomicCounterBuffer()
111 {
112     memset(&pod, 0, sizeof(pod));
113 }
114 
unionReferencesWith(const LinkedUniform & other)115 void AtomicCounterBuffer::unionReferencesWith(const LinkedUniform &other)
116 {
117     pod.activeUseBits |= other.pod.activeUseBits;
118     for (const ShaderType shaderType : AllShaderTypes())
119     {
120         ASSERT(pod.ids[shaderType] == 0 || other.getId(shaderType) == 0 ||
121                pod.ids[shaderType] == other.getId(shaderType));
122         if (pod.ids[shaderType] == 0)
123         {
124             pod.ids[shaderType] = other.getId(shaderType);
125         }
126     }
127 }
128 
InterfaceBlock()129 InterfaceBlock::InterfaceBlock()
130 {
131     memset(&pod, 0, sizeof(pod));
132 }
133 
InterfaceBlock(const std::string & name,const std::string & mappedName,bool isArray,bool isReadOnly,unsigned int arrayElementIn,unsigned int firstFieldArraySizeIn,int binding)134 InterfaceBlock::InterfaceBlock(const std::string &name,
135                                const std::string &mappedName,
136                                bool isArray,
137                                bool isReadOnly,
138                                unsigned int arrayElementIn,
139                                unsigned int firstFieldArraySizeIn,
140                                int binding)
141     : name(name), mappedName(mappedName)
142 {
143     memset(&pod, 0, sizeof(pod));
144 
145     SetBitField(pod.isArray, isArray);
146     SetBitField(pod.isReadOnly, isReadOnly);
147     SetBitField(pod.inShaderBinding, binding);
148     pod.arrayElement        = arrayElementIn;
149     pod.firstFieldArraySize = firstFieldArraySizeIn;
150 }
151 
nameWithArrayIndex() const152 std::string InterfaceBlock::nameWithArrayIndex() const
153 {
154     std::stringstream fullNameStr;
155     fullNameStr << name;
156     if (pod.isArray)
157     {
158         fullNameStr << "[" << pod.arrayElement << "]";
159     }
160 
161     return fullNameStr.str();
162 }
163 
mappedNameWithArrayIndex() const164 std::string InterfaceBlock::mappedNameWithArrayIndex() const
165 {
166     std::stringstream fullNameStr;
167     fullNameStr << mappedName;
168     if (pod.isArray)
169     {
170         fullNameStr << "[" << pod.arrayElement << "]";
171     }
172 
173     return fullNameStr.str();
174 }
175 }  // namespace gl
176