blob: 69842d085206f214ce689356a799057ae37bc3a5 [file] [log] [blame]
Jim Van Verthbe259dc2020-05-19 11:40:31 -04001/*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "src/gpu/GrUniformDataManager.h"
9
10#include "src/gpu/GrShaderVar.h"
11
Jim Van Verthabf4d502021-02-10 14:39:25 -050012// ensure that these types are the sizes the uniform data is expecting
13static_assert(sizeof(int32_t) == 4);
14static_assert(sizeof(float) == 4);
15
Jim Van Verth14a9b082021-09-20 14:09:52 +000016GrUniformDataManager::GrUniformDataManager(uint32_t uniformCount, uint32_t uniformSize)
17 : fUniformSize(uniformSize)
18 , fUniformsDirty(false) {
Jim Van Verthbe259dc2020-05-19 11:40:31 -040019 fUniformData.reset(uniformSize);
20 fUniforms.push_back_n(uniformCount);
Jim Van Verth14a9b082021-09-20 14:09:52 +000021 // subclasses fill in the uniforms in their constructor
Jim Van Verthbe259dc2020-05-19 11:40:31 -040022}
23
24void* GrUniformDataManager::getBufferPtrAndMarkDirty(const Uniform& uni) const {
25 fUniformsDirty = true;
26 return static_cast<char*>(fUniformData.get())+uni.fOffset;
27}
28
29void GrUniformDataManager::set1i(UniformHandle u, int32_t i) const {
30 const Uniform& uni = fUniforms[u.toIndex()];
31 SkASSERT(uni.fType == kInt_GrSLType || uni.fType == kShort_GrSLType);
32 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
33 void* buffer = this->getBufferPtrAndMarkDirty(uni);
34 memcpy(buffer, &i, sizeof(int32_t));
35}
36
37void GrUniformDataManager::set1iv(UniformHandle u,
38 int arrayCount,
39 const int32_t v[]) const {
40 const Uniform& uni = fUniforms[u.toIndex()];
41 SkASSERT(uni.fType == kInt_GrSLType || uni.fType == kShort_GrSLType);
42 SkASSERT(arrayCount > 0);
43 SkASSERT(arrayCount <= uni.fArrayCount ||
44 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
45
46 void* buffer = this->getBufferPtrAndMarkDirty(uni);
Jim Van Verthbe259dc2020-05-19 11:40:31 -040047 for (int i = 0; i < arrayCount; ++i) {
48 const int32_t* curVec = &v[i];
49 memcpy(buffer, curVec, sizeof(int32_t));
50 buffer = static_cast<char*>(buffer) + 4*sizeof(int32_t);
51 }
52}
53
54void GrUniformDataManager::set1f(UniformHandle u, float v0) const {
55 const Uniform& uni = fUniforms[u.toIndex()];
56 SkASSERT(uni.fType == kFloat_GrSLType || uni.fType == kHalf_GrSLType);
57 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
58 void* buffer = this->getBufferPtrAndMarkDirty(uni);
Jim Van Verthbe259dc2020-05-19 11:40:31 -040059 memcpy(buffer, &v0, sizeof(float));
60}
61
62void GrUniformDataManager::set1fv(UniformHandle u,
63 int arrayCount,
64 const float v[]) const {
65 const Uniform& uni = fUniforms[u.toIndex()];
66 SkASSERT(uni.fType == kFloat_GrSLType || uni.fType == kHalf_GrSLType);
67 SkASSERT(arrayCount > 0);
68 SkASSERT(arrayCount <= uni.fArrayCount ||
69 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
70
71 void* buffer = this->getBufferPtrAndMarkDirty(uni);
Jim Van Verthbe259dc2020-05-19 11:40:31 -040072 for (int i = 0; i < arrayCount; ++i) {
73 const float* curVec = &v[i];
74 memcpy(buffer, curVec, sizeof(float));
75 buffer = static_cast<char*>(buffer) + 4*sizeof(float);
76 }
77}
78
79void GrUniformDataManager::set2i(UniformHandle u, int32_t i0, int32_t i1) const {
80 const Uniform& uni = fUniforms[u.toIndex()];
81 SkASSERT(uni.fType == kInt2_GrSLType || uni.fType == kShort2_GrSLType);
82 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
83 void* buffer = this->getBufferPtrAndMarkDirty(uni);
84 int32_t v[2] = { i0, i1 };
85 memcpy(buffer, v, 2 * sizeof(int32_t));
86}
87
88void GrUniformDataManager::set2iv(UniformHandle u,
89 int arrayCount,
90 const int32_t v[]) const {
91 const Uniform& uni = fUniforms[u.toIndex()];
92 SkASSERT(uni.fType == kInt2_GrSLType || uni.fType == kShort2_GrSLType);
93 SkASSERT(arrayCount > 0);
94 SkASSERT(arrayCount <= uni.fArrayCount ||
95 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
96
97 void* buffer = this->getBufferPtrAndMarkDirty(uni);
Jim Van Verthbe259dc2020-05-19 11:40:31 -040098 for (int i = 0; i < arrayCount; ++i) {
99 const int32_t* curVec = &v[2 * i];
100 memcpy(buffer, curVec, 2 * sizeof(int32_t));
101 buffer = static_cast<char*>(buffer) + 4*sizeof(int32_t);
102 }
103}
104
105void GrUniformDataManager::set2f(UniformHandle u, float v0, float v1) const {
106 const Uniform& uni = fUniforms[u.toIndex()];
107 SkASSERT(uni.fType == kFloat2_GrSLType || uni.fType == kHalf2_GrSLType);
108 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
109 void* buffer = this->getBufferPtrAndMarkDirty(uni);
Jim Van Verthbe259dc2020-05-19 11:40:31 -0400110 float v[2] = { v0, v1 };
111 memcpy(buffer, v, 2 * sizeof(float));
112}
113
114void GrUniformDataManager::set2fv(UniformHandle u,
115 int arrayCount,
116 const float v[]) const {
117 const Uniform& uni = fUniforms[u.toIndex()];
118 SkASSERT(uni.fType == kFloat2_GrSLType || uni.fType == kHalf2_GrSLType);
119 SkASSERT(arrayCount > 0);
120 SkASSERT(arrayCount <= uni.fArrayCount ||
121 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
122
123 void* buffer = this->getBufferPtrAndMarkDirty(uni);
Jim Van Verthbe259dc2020-05-19 11:40:31 -0400124 for (int i = 0; i < arrayCount; ++i) {
125 const float* curVec = &v[2 * i];
126 memcpy(buffer, curVec, 2 * sizeof(float));
127 buffer = static_cast<char*>(buffer) + 4*sizeof(float);
128 }
129}
130
131void GrUniformDataManager::set3i(UniformHandle u,
132 int32_t i0,
133 int32_t i1,
134 int32_t i2) const {
135 const Uniform& uni = fUniforms[u.toIndex()];
136 SkASSERT(uni.fType == kInt3_GrSLType || uni.fType == kShort3_GrSLType);
137 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
138 void* buffer = this->getBufferPtrAndMarkDirty(uni);
139 int32_t v[3] = { i0, i1, i2 };
140 memcpy(buffer, v, 3 * sizeof(int32_t));
141}
142
143void GrUniformDataManager::set3iv(UniformHandle u,
144 int arrayCount,
145 const int32_t v[]) const {
146 const Uniform& uni = fUniforms[u.toIndex()];
147 SkASSERT(uni.fType == kInt3_GrSLType || uni.fType == kShort3_GrSLType);
148 SkASSERT(arrayCount > 0);
149 SkASSERT(arrayCount <= uni.fArrayCount ||
150 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
151
152 void* buffer = this->getBufferPtrAndMarkDirty(uni);
Jim Van Verthbe259dc2020-05-19 11:40:31 -0400153 for (int i = 0; i < arrayCount; ++i) {
154 const int32_t* curVec = &v[3 * i];
155 memcpy(buffer, curVec, 3 * sizeof(int32_t));
156 buffer = static_cast<char*>(buffer) + 4*sizeof(int32_t);
157 }
158}
159
160void GrUniformDataManager::set3f(UniformHandle u, float v0, float v1, float v2) const {
161 const Uniform& uni = fUniforms[u.toIndex()];
162 SkASSERT(uni.fType == kFloat3_GrSLType || uni.fType == kHalf3_GrSLType);
163 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
164 void* buffer = this->getBufferPtrAndMarkDirty(uni);
Jim Van Verthbe259dc2020-05-19 11:40:31 -0400165 float v[3] = { v0, v1, v2 };
166 memcpy(buffer, v, 3 * sizeof(float));
167}
168
169void GrUniformDataManager::set3fv(UniformHandle u,
170 int arrayCount,
171 const float v[]) const {
172 const Uniform& uni = fUniforms[u.toIndex()];
173 SkASSERT(uni.fType == kFloat3_GrSLType || uni.fType == kHalf3_GrSLType);
174 SkASSERT(arrayCount > 0);
175 SkASSERT(arrayCount <= uni.fArrayCount ||
176 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
177
178 void* buffer = this->getBufferPtrAndMarkDirty(uni);
Jim Van Verthbe259dc2020-05-19 11:40:31 -0400179 for (int i = 0; i < arrayCount; ++i) {
180 const float* curVec = &v[3 * i];
181 memcpy(buffer, curVec, 3 * sizeof(float));
182 buffer = static_cast<char*>(buffer) + 4*sizeof(float);
183 }
184}
185
186void GrUniformDataManager::set4i(UniformHandle u,
187 int32_t i0,
188 int32_t i1,
189 int32_t i2,
190 int32_t i3) const {
191 const Uniform& uni = fUniforms[u.toIndex()];
192 SkASSERT(uni.fType == kInt4_GrSLType || uni.fType == kShort4_GrSLType);
193 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
194 void* buffer = this->getBufferPtrAndMarkDirty(uni);
195 int32_t v[4] = { i0, i1, i2, i3 };
196 memcpy(buffer, v, 4 * sizeof(int32_t));
197}
198
199void GrUniformDataManager::set4iv(UniformHandle u,
200 int arrayCount,
201 const int32_t v[]) const {
202 const Uniform& uni = fUniforms[u.toIndex()];
203 SkASSERT(uni.fType == kInt4_GrSLType || uni.fType == kShort4_GrSLType);
204 SkASSERT(arrayCount > 0);
205 SkASSERT(arrayCount <= uni.fArrayCount ||
206 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
207
208 void* buffer = this->getBufferPtrAndMarkDirty(uni);
Jim Van Verthabf4d502021-02-10 14:39:25 -0500209 memcpy(buffer, v, arrayCount * 4 * sizeof(int32_t));
Jim Van Verthbe259dc2020-05-19 11:40:31 -0400210}
211
212void GrUniformDataManager::set4f(UniformHandle u,
213 float v0,
214 float v1,
215 float v2,
216 float v3) const {
217 const Uniform& uni = fUniforms[u.toIndex()];
218 SkASSERT(uni.fType == kFloat4_GrSLType || uni.fType == kHalf4_GrSLType);
219 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
220 void* buffer = this->getBufferPtrAndMarkDirty(uni);
Jim Van Verthbe259dc2020-05-19 11:40:31 -0400221 float v[4] = { v0, v1, v2, v3 };
222 memcpy(buffer, v, 4 * sizeof(float));
223}
224
225void GrUniformDataManager::set4fv(UniformHandle u,
226 int arrayCount,
227 const float v[]) const {
228 const Uniform& uni = fUniforms[u.toIndex()];
229 SkASSERT(uni.fType == kFloat4_GrSLType || uni.fType == kHalf4_GrSLType);
230 SkASSERT(arrayCount > 0);
231 SkASSERT(arrayCount <= uni.fArrayCount ||
232 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
233
234 void* buffer = this->getBufferPtrAndMarkDirty(uni);
Jim Van Verthbe259dc2020-05-19 11:40:31 -0400235 memcpy(buffer, v, arrayCount * 4 * sizeof(float));
236}
237
238void GrUniformDataManager::setMatrix2f(UniformHandle u, const float matrix[]) const {
239 this->setMatrices<2>(u, 1, matrix);
240}
241
Jim Van Verthabf4d502021-02-10 14:39:25 -0500242void GrUniformDataManager::setMatrix2fv(UniformHandle u, int arrayCount, const float m[]) const {
Jim Van Verthbe259dc2020-05-19 11:40:31 -0400243 this->setMatrices<2>(u, arrayCount, m);
244}
245
246void GrUniformDataManager::setMatrix3f(UniformHandle u, const float matrix[]) const {
247 this->setMatrices<3>(u, 1, matrix);
248}
249
Jim Van Verthabf4d502021-02-10 14:39:25 -0500250void GrUniformDataManager::setMatrix3fv(UniformHandle u, int arrayCount, const float m[]) const {
Jim Van Verthbe259dc2020-05-19 11:40:31 -0400251 this->setMatrices<3>(u, arrayCount, m);
252}
253
254void GrUniformDataManager::setMatrix4f(UniformHandle u, const float matrix[]) const {
255 this->setMatrices<4>(u, 1, matrix);
256}
257
Jim Van Verthabf4d502021-02-10 14:39:25 -0500258void GrUniformDataManager::setMatrix4fv(UniformHandle u, int arrayCount, const float m[]) const {
Jim Van Verthbe259dc2020-05-19 11:40:31 -0400259 this->setMatrices<4>(u, arrayCount, m);
260}
261
262template<int N> struct set_uniform_matrix;
263
264template<int N> inline void GrUniformDataManager::setMatrices(UniformHandle u,
Jim Van Verthabf4d502021-02-10 14:39:25 -0500265 int arrayCount,
266 const float matrices[]) const {
Jim Van Verthbe259dc2020-05-19 11:40:31 -0400267 const Uniform& uni = fUniforms[u.toIndex()];
268 SkASSERT(uni.fType == kFloat2x2_GrSLType + (N - 2) ||
269 uni.fType == kHalf2x2_GrSLType + (N - 2));
270 SkASSERT(arrayCount > 0);
271 SkASSERT(arrayCount <= uni.fArrayCount ||
272 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
273
274 void* buffer = fUniformData.get();
275 fUniformsDirty = true;
276
277 set_uniform_matrix<N>::set(buffer, uni.fOffset, arrayCount, matrices);
278}
279
280template<int N> struct set_uniform_matrix {
281 inline static void set(void* buffer, int uniformOffset, int count, const float matrices[]) {
Jim Van Verthbe259dc2020-05-19 11:40:31 -0400282 buffer = static_cast<char*>(buffer) + uniformOffset;
283 for (int i = 0; i < count; ++i) {
284 const float* matrix = &matrices[N * N * i];
285 for (int j = 0; j < N; ++j) {
286 memcpy(buffer, &matrix[j * N], N * sizeof(float));
287 buffer = static_cast<char*>(buffer) + 4 * sizeof(float);
288 }
289 }
290 }
291};
292
293template<> struct set_uniform_matrix<4> {
294 inline static void set(void* buffer, int uniformOffset, int count, const float matrices[]) {
Jim Van Verthbe259dc2020-05-19 11:40:31 -0400295 buffer = static_cast<char*>(buffer) + uniformOffset;
296 memcpy(buffer, matrices, count * 16 * sizeof(float));
297 }
298};
299