blob: cbef9261779e7be25ed20ad15068c87148ec19bf [file] [log] [blame]
egdaniel42701e92016-02-26 08:02:55 -08001/*
2* Copyright 2016 Google Inc.
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/vk/GrVkPipelineStateDataManager.h"
egdaniel42701e92016-02-26 08:02:55 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/vk/GrVkGpu.h"
11#include "src/gpu/vk/GrVkUniformBuffer.h"
egdaniel42701e92016-02-26 08:02:55 -080012
egdaniel22281c12016-03-23 13:49:40 -070013GrVkPipelineStateDataManager::GrVkPipelineStateDataManager(const UniformInfoArray& uniforms,
Ethan Nicholas0be34802019-08-15 12:36:58 -040014 uint32_t uniformSize)
15 : fUniformSize(uniformSize)
16 , fUniformsDirty(false) {
17 fUniformData.reset(uniformSize);
egdaniel42701e92016-02-26 08:02:55 -080018 int count = uniforms.count();
19 fUniforms.push_back_n(count);
20 // We must add uniforms in same order is the UniformInfoArray so that UniformHandles already
21 // owned by other objects will still match up here.
22 for (int i = 0; i < count; i++) {
23 Uniform& uniform = fUniforms[i];
24 const GrVkUniformHandler::UniformInfo uniformInfo = uniforms[i];
Brian Salomon99938a82016-11-21 13:41:08 -050025 SkASSERT(GrShaderVar::kNonArray == uniformInfo.fVariable.getArrayCount() ||
egdaniel42701e92016-02-26 08:02:55 -080026 uniformInfo.fVariable.getArrayCount() > 0);
27 SkDEBUGCODE(
28 uniform.fArrayCount = uniformInfo.fVariable.getArrayCount();
29 uniform.fType = uniformInfo.fVariable.getType();
Brian Salomon23356442018-11-30 15:33:19 -050030 )
Greg Daniel18f96022017-05-04 15:09:03 -040031
egdaniel42701e92016-02-26 08:02:55 -080032 uniform.fOffset = uniformInfo.fUBOffset;
egdaniel42701e92016-02-26 08:02:55 -080033 }
34}
35
egdaniel22281c12016-03-23 13:49:40 -070036void* GrVkPipelineStateDataManager::getBufferPtrAndMarkDirty(const Uniform& uni) const {
Ethan Nicholas0be34802019-08-15 12:36:58 -040037 fUniformsDirty = true;
38 return static_cast<char*>(fUniformData.get())+uni.fOffset;
jvanverth910114a2016-03-08 12:09:27 -080039}
40
fmenozzi497e9e22016-06-21 09:42:12 -070041void GrVkPipelineStateDataManager::set1i(UniformHandle u, int32_t i) const {
42 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholasf7b88202017-09-18 14:10:39 -040043 SkASSERT(uni.fType == kInt_GrSLType || uni.fType == kShort_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -050044 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
fmenozzi497e9e22016-06-21 09:42:12 -070045 void* buffer = this->getBufferPtrAndMarkDirty(uni);
46 memcpy(buffer, &i, sizeof(int32_t));
47}
48
fmenozzi35a98c72016-07-20 08:26:12 -070049void GrVkPipelineStateDataManager::set1iv(UniformHandle u,
50 int arrayCount,
51 const int32_t v[]) const {
52 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholasf7b88202017-09-18 14:10:39 -040053 SkASSERT(uni.fType == kInt_GrSLType || uni.fType == kShort_GrSLType);
fmenozzi35a98c72016-07-20 08:26:12 -070054 SkASSERT(arrayCount > 0);
55 SkASSERT(arrayCount <= uni.fArrayCount ||
Brian Salomon99938a82016-11-21 13:41:08 -050056 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
fmenozzi35a98c72016-07-20 08:26:12 -070057
58 void* buffer = this->getBufferPtrAndMarkDirty(uni);
59 SkASSERT(sizeof(int32_t) == 4);
60 for (int i = 0; i < arrayCount; ++i) {
61 const int32_t* curVec = &v[i];
62 memcpy(buffer, curVec, sizeof(int32_t));
63 buffer = static_cast<char*>(buffer) + 4*sizeof(int32_t);
64 }
65}
66
egdaniel22281c12016-03-23 13:49:40 -070067void GrVkPipelineStateDataManager::set1f(UniformHandle u, float v0) const {
egdaniel42701e92016-02-26 08:02:55 -080068 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -040069 SkASSERT(uni.fType == kFloat_GrSLType || uni.fType == kHalf_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -050070 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
jvanverth910114a2016-03-08 12:09:27 -080071 void* buffer = this->getBufferPtrAndMarkDirty(uni);
egdaniel42701e92016-02-26 08:02:55 -080072 SkASSERT(sizeof(float) == 4);
73 memcpy(buffer, &v0, sizeof(float));
74}
75
egdaniel22281c12016-03-23 13:49:40 -070076void GrVkPipelineStateDataManager::set1fv(UniformHandle u,
77 int arrayCount,
78 const float v[]) const {
egdaniel42701e92016-02-26 08:02:55 -080079 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -040080 SkASSERT(uni.fType == kFloat_GrSLType || uni.fType == kHalf_GrSLType);
egdaniel42701e92016-02-26 08:02:55 -080081 SkASSERT(arrayCount > 0);
82 SkASSERT(arrayCount <= uni.fArrayCount ||
Brian Salomon99938a82016-11-21 13:41:08 -050083 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
egdaniel42701e92016-02-26 08:02:55 -080084
jvanverth910114a2016-03-08 12:09:27 -080085 void* buffer = this->getBufferPtrAndMarkDirty(uni);
egdaniel42701e92016-02-26 08:02:55 -080086 SkASSERT(sizeof(float) == 4);
egdaniel4ee1cda2016-02-26 08:18:49 -080087 for (int i = 0; i < arrayCount; ++i) {
88 const float* curVec = &v[i];
89 memcpy(buffer, curVec, sizeof(float));
90 buffer = static_cast<char*>(buffer) + 4*sizeof(float);
91 }
egdaniel42701e92016-02-26 08:02:55 -080092}
93
Michael Ludwig779ed022018-08-28 17:20:07 -040094void GrVkPipelineStateDataManager::set2i(UniformHandle u, int32_t i0, int32_t i1) const {
95 const Uniform& uni = fUniforms[u.toIndex()];
96 SkASSERT(uni.fType == kInt2_GrSLType || uni.fType == kShort2_GrSLType);
97 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
98 void* buffer = this->getBufferPtrAndMarkDirty(uni);
99 int32_t v[2] = { i0, i1 };
100 memcpy(buffer, v, 2 * sizeof(int32_t));
101}
102
103void GrVkPipelineStateDataManager::set2iv(UniformHandle u,
104 int arrayCount,
105 const int32_t v[]) const {
106 const Uniform& uni = fUniforms[u.toIndex()];
107 SkASSERT(uni.fType == kInt2_GrSLType || uni.fType == kShort2_GrSLType);
108 SkASSERT(arrayCount > 0);
109 SkASSERT(arrayCount <= uni.fArrayCount ||
110 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
111
112 void* buffer = this->getBufferPtrAndMarkDirty(uni);
113 SkASSERT(sizeof(int32_t) == 4);
114 for (int i = 0; i < arrayCount; ++i) {
115 const int32_t* curVec = &v[2 * i];
116 memcpy(buffer, curVec, 2 * sizeof(int32_t));
117 buffer = static_cast<char*>(buffer) + 4*sizeof(int32_t);
118 }
119}
120
egdaniel22281c12016-03-23 13:49:40 -0700121void GrVkPipelineStateDataManager::set2f(UniformHandle u, float v0, float v1) const {
egdaniel42701e92016-02-26 08:02:55 -0800122 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400123 SkASSERT(uni.fType == kFloat2_GrSLType || uni.fType == kHalf2_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -0500124 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
jvanverth910114a2016-03-08 12:09:27 -0800125 void* buffer = this->getBufferPtrAndMarkDirty(uni);
egdaniel42701e92016-02-26 08:02:55 -0800126 SkASSERT(sizeof(float) == 4);
127 float v[2] = { v0, v1 };
128 memcpy(buffer, v, 2 * sizeof(float));
129}
130
egdaniel22281c12016-03-23 13:49:40 -0700131void GrVkPipelineStateDataManager::set2fv(UniformHandle u,
132 int arrayCount,
133 const float v[]) const {
egdaniel42701e92016-02-26 08:02:55 -0800134 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400135 SkASSERT(uni.fType == kFloat2_GrSLType || uni.fType == kHalf2_GrSLType);
egdaniel42701e92016-02-26 08:02:55 -0800136 SkASSERT(arrayCount > 0);
137 SkASSERT(arrayCount <= uni.fArrayCount ||
Brian Salomon99938a82016-11-21 13:41:08 -0500138 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
egdaniel42701e92016-02-26 08:02:55 -0800139
jvanverth910114a2016-03-08 12:09:27 -0800140 void* buffer = this->getBufferPtrAndMarkDirty(uni);
egdaniel42701e92016-02-26 08:02:55 -0800141 SkASSERT(sizeof(float) == 4);
egdaniel4ee1cda2016-02-26 08:18:49 -0800142 for (int i = 0; i < arrayCount; ++i) {
143 const float* curVec = &v[2 * i];
144 memcpy(buffer, curVec, 2 * sizeof(float));
145 buffer = static_cast<char*>(buffer) + 4*sizeof(float);
146 }
egdaniel42701e92016-02-26 08:02:55 -0800147}
148
Michael Ludwig779ed022018-08-28 17:20:07 -0400149void GrVkPipelineStateDataManager::set3i(UniformHandle u,
150 int32_t i0,
151 int32_t i1,
152 int32_t i2) const {
153 const Uniform& uni = fUniforms[u.toIndex()];
154 SkASSERT(uni.fType == kInt3_GrSLType || uni.fType == kShort3_GrSLType);
155 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
156 void* buffer = this->getBufferPtrAndMarkDirty(uni);
157 int32_t v[3] = { i0, i1, i2 };
158 memcpy(buffer, v, 3 * sizeof(int32_t));
159}
160
161void GrVkPipelineStateDataManager::set3iv(UniformHandle u,
162 int arrayCount,
163 const int32_t v[]) const {
164 const Uniform& uni = fUniforms[u.toIndex()];
165 SkASSERT(uni.fType == kInt3_GrSLType || uni.fType == kShort3_GrSLType);
166 SkASSERT(arrayCount > 0);
167 SkASSERT(arrayCount <= uni.fArrayCount ||
168 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
169
170 void* buffer = this->getBufferPtrAndMarkDirty(uni);
171 SkASSERT(sizeof(int32_t) == 4);
172 for (int i = 0; i < arrayCount; ++i) {
173 const int32_t* curVec = &v[3 * i];
174 memcpy(buffer, curVec, 3 * sizeof(int32_t));
175 buffer = static_cast<char*>(buffer) + 4*sizeof(int32_t);
176 }
177}
178
egdaniel22281c12016-03-23 13:49:40 -0700179void GrVkPipelineStateDataManager::set3f(UniformHandle u, float v0, float v1, float v2) const {
egdaniel42701e92016-02-26 08:02:55 -0800180 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400181 SkASSERT(uni.fType == kFloat3_GrSLType || uni.fType == kHalf3_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -0500182 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
jvanverth910114a2016-03-08 12:09:27 -0800183 void* buffer = this->getBufferPtrAndMarkDirty(uni);
egdaniel42701e92016-02-26 08:02:55 -0800184 SkASSERT(sizeof(float) == 4);
185 float v[3] = { v0, v1, v2 };
186 memcpy(buffer, v, 3 * sizeof(float));
187}
188
egdaniel22281c12016-03-23 13:49:40 -0700189void GrVkPipelineStateDataManager::set3fv(UniformHandle u,
190 int arrayCount,
191 const float v[]) const {
egdaniel42701e92016-02-26 08:02:55 -0800192 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400193 SkASSERT(uni.fType == kFloat3_GrSLType || uni.fType == kHalf3_GrSLType);
egdaniel42701e92016-02-26 08:02:55 -0800194 SkASSERT(arrayCount > 0);
195 SkASSERT(arrayCount <= uni.fArrayCount ||
Brian Salomon99938a82016-11-21 13:41:08 -0500196 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
egdaniel42701e92016-02-26 08:02:55 -0800197
jvanverth910114a2016-03-08 12:09:27 -0800198 void* buffer = this->getBufferPtrAndMarkDirty(uni);
egdaniel42701e92016-02-26 08:02:55 -0800199 SkASSERT(sizeof(float) == 4);
egdaniel4ee1cda2016-02-26 08:18:49 -0800200 for (int i = 0; i < arrayCount; ++i) {
201 const float* curVec = &v[3 * i];
202 memcpy(buffer, curVec, 3 * sizeof(float));
203 buffer = static_cast<char*>(buffer) + 4*sizeof(float);
204 }
egdaniel42701e92016-02-26 08:02:55 -0800205}
206
Michael Ludwig779ed022018-08-28 17:20:07 -0400207void GrVkPipelineStateDataManager::set4i(UniformHandle u,
208 int32_t i0,
209 int32_t i1,
210 int32_t i2,
211 int32_t i3) const {
212 const Uniform& uni = fUniforms[u.toIndex()];
213 SkASSERT(uni.fType == kInt4_GrSLType || uni.fType == kShort4_GrSLType);
214 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
215 void* buffer = this->getBufferPtrAndMarkDirty(uni);
216 int32_t v[4] = { i0, i1, i2, i3 };
217 memcpy(buffer, v, 4 * sizeof(int32_t));
218}
219
220void GrVkPipelineStateDataManager::set4iv(UniformHandle u,
221 int arrayCount,
222 const int32_t v[]) const {
223 const Uniform& uni = fUniforms[u.toIndex()];
224 SkASSERT(uni.fType == kInt4_GrSLType || uni.fType == kShort4_GrSLType);
225 SkASSERT(arrayCount > 0);
226 SkASSERT(arrayCount <= uni.fArrayCount ||
227 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
228
229 void* buffer = this->getBufferPtrAndMarkDirty(uni);
230 SkASSERT(sizeof(int32_t) == 4);
231 for (int i = 0; i < arrayCount; ++i) {
232 const int32_t* curVec = &v[4 * i];
233 memcpy(buffer, curVec, 4 * sizeof(int32_t));
234 buffer = static_cast<char*>(buffer) + 4*sizeof(int32_t);
235 }
236}
237
egdaniel22281c12016-03-23 13:49:40 -0700238void GrVkPipelineStateDataManager::set4f(UniformHandle u,
239 float v0,
240 float v1,
241 float v2,
242 float v3) const {
egdaniel42701e92016-02-26 08:02:55 -0800243 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400244 SkASSERT(uni.fType == kFloat4_GrSLType || uni.fType == kHalf4_GrSLType);
Brian Salomon99938a82016-11-21 13:41:08 -0500245 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
jvanverth910114a2016-03-08 12:09:27 -0800246 void* buffer = this->getBufferPtrAndMarkDirty(uni);
egdaniel42701e92016-02-26 08:02:55 -0800247 SkASSERT(sizeof(float) == 4);
248 float v[4] = { v0, v1, v2, v3 };
249 memcpy(buffer, v, 4 * sizeof(float));
250}
251
egdaniel22281c12016-03-23 13:49:40 -0700252void GrVkPipelineStateDataManager::set4fv(UniformHandle u,
253 int arrayCount,
254 const float v[]) const {
egdaniel42701e92016-02-26 08:02:55 -0800255 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400256 SkASSERT(uni.fType == kFloat4_GrSLType || uni.fType == kHalf4_GrSLType);
egdaniel42701e92016-02-26 08:02:55 -0800257 SkASSERT(arrayCount > 0);
258 SkASSERT(arrayCount <= uni.fArrayCount ||
Brian Salomon99938a82016-11-21 13:41:08 -0500259 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
egdaniel42701e92016-02-26 08:02:55 -0800260
jvanverth910114a2016-03-08 12:09:27 -0800261 void* buffer = this->getBufferPtrAndMarkDirty(uni);
egdaniel42701e92016-02-26 08:02:55 -0800262 SkASSERT(sizeof(float) == 4);
263 memcpy(buffer, v, arrayCount * 4 * sizeof(float));
264}
265
egdaniel22281c12016-03-23 13:49:40 -0700266void GrVkPipelineStateDataManager::setMatrix2f(UniformHandle u, const float matrix[]) const {
cdalton8d988b32016-03-07 15:39:09 -0800267 this->setMatrices<2>(u, 1, matrix);
268}
269
egdaniel22281c12016-03-23 13:49:40 -0700270void GrVkPipelineStateDataManager::setMatrix2fv(UniformHandle u,
271 int arrayCount,
272 const float m[]) const {
cdalton8d988b32016-03-07 15:39:09 -0800273 this->setMatrices<2>(u, arrayCount, m);
274}
275
egdaniel22281c12016-03-23 13:49:40 -0700276void GrVkPipelineStateDataManager::setMatrix3f(UniformHandle u, const float matrix[]) const {
cdalton8d988b32016-03-07 15:39:09 -0800277 this->setMatrices<3>(u, 1, matrix);
egdaniel42701e92016-02-26 08:02:55 -0800278}
279
egdaniel22281c12016-03-23 13:49:40 -0700280void GrVkPipelineStateDataManager::setMatrix3fv(UniformHandle u,
281 int arrayCount,
282 const float m[]) const {
cdalton8d988b32016-03-07 15:39:09 -0800283 this->setMatrices<3>(u, arrayCount, m);
egdaniel42701e92016-02-26 08:02:55 -0800284}
285
egdaniel22281c12016-03-23 13:49:40 -0700286void GrVkPipelineStateDataManager::setMatrix4f(UniformHandle u, const float matrix[]) const {
cdalton8d988b32016-03-07 15:39:09 -0800287 this->setMatrices<4>(u, 1, matrix);
egdaniel42701e92016-02-26 08:02:55 -0800288}
289
egdaniel22281c12016-03-23 13:49:40 -0700290void GrVkPipelineStateDataManager::setMatrix4fv(UniformHandle u,
291 int arrayCount,
292 const float m[]) const {
cdalton8d988b32016-03-07 15:39:09 -0800293 this->setMatrices<4>(u, arrayCount, m);
294}
295
296template<int N> struct set_uniform_matrix;
297
egdaniel22281c12016-03-23 13:49:40 -0700298template<int N> inline void GrVkPipelineStateDataManager::setMatrices(UniformHandle u,
299 int arrayCount,
300 const float matrices[]) const {
egdaniel42701e92016-02-26 08:02:55 -0800301 const Uniform& uni = fUniforms[u.toIndex()];
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400302 SkASSERT(uni.fType == kFloat2x2_GrSLType + (N - 2) ||
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400303 uni.fType == kHalf2x2_GrSLType + (N - 2));
egdaniel42701e92016-02-26 08:02:55 -0800304 SkASSERT(arrayCount > 0);
305 SkASSERT(arrayCount <= uni.fArrayCount ||
Brian Salomon99938a82016-11-21 13:41:08 -0500306 (1 == arrayCount && GrShaderVar::kNonArray == uni.fArrayCount));
egdaniel42701e92016-02-26 08:02:55 -0800307
Ethan Nicholas0be34802019-08-15 12:36:58 -0400308 void* buffer = fUniformData.get();
309 fUniformsDirty = true;
cdalton8d988b32016-03-07 15:39:09 -0800310
311 set_uniform_matrix<N>::set(buffer, uni.fOffset, arrayCount, matrices);
egdaniel42701e92016-02-26 08:02:55 -0800312}
313
cdalton8d988b32016-03-07 15:39:09 -0800314template<int N> struct set_uniform_matrix {
315 inline static void set(void* buffer, int uniformOffset, int count, const float matrices[]) {
316 GR_STATIC_ASSERT(sizeof(float) == 4);
317 buffer = static_cast<char*>(buffer) + uniformOffset;
318 for (int i = 0; i < count; ++i) {
319 const float* matrix = &matrices[N * N * i];
egdaniel75d2bfc2016-07-07 08:04:08 -0700320 for (int j = 0; j < N; ++j) {
321 memcpy(buffer, &matrix[j * N], N * sizeof(float));
322 buffer = static_cast<char*>(buffer) + 4 * sizeof(float);
323 }
cdalton8d988b32016-03-07 15:39:09 -0800324 }
325 }
326};
327
328template<> struct set_uniform_matrix<4> {
329 inline static void set(void* buffer, int uniformOffset, int count, const float matrices[]) {
330 GR_STATIC_ASSERT(sizeof(float) == 4);
331 buffer = static_cast<char*>(buffer) + uniformOffset;
332 memcpy(buffer, matrices, count * 16 * sizeof(float));
333 }
334};
335
jvanvertha584de92016-06-30 09:10:52 -0700336bool GrVkPipelineStateDataManager::uploadUniformBuffers(GrVkGpu* gpu,
Ethan Nicholas0be34802019-08-15 12:36:58 -0400337 GrVkUniformBuffer* buffer) const {
egdaniel7cbffda2016-04-08 13:27:53 -0700338 bool updatedBuffer = false;
Ethan Nicholas0be34802019-08-15 12:36:58 -0400339 if (buffer && fUniformsDirty) {
340 SkAssertResult(buffer->updateData(gpu, fUniformData.get(),
341 fUniformSize, &updatedBuffer));
342 fUniformsDirty = false;
egdaniel42701e92016-02-26 08:02:55 -0800343 }
Greg Daniel18f96022017-05-04 15:09:03 -0400344
egdaniel7cbffda2016-04-08 13:27:53 -0700345 return updatedBuffer;
egdaniel42701e92016-02-26 08:02:55 -0800346}