blob: 55fd3d7f24de2170c10439b0b6dfdea9d55ecd5d [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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
8#include "GrVkUniformHandler.h"
9#include "glsl/GrGLSLProgramBuilder.h"
10
11// To determine whether a current offset is aligned, we can just 'and' the lowest bits with the
12// alignment mask. A value of 0 means aligned, any other value is how many bytes past alignment we
13// are. This works since all alignments are powers of 2. The mask is always (alignment - 1).
egdaniel4ee1cda2016-02-26 08:18:49 -080014// This alignment mask will give correct alignments for using the std430 block layout. If you want
15// the std140 alignment, you can use this, but then make sure if you have an array type it is
16// aligned to 16 bytes (i.e. has mask of 0xF).
Greg Daniel164a9f02016-02-22 09:56:40 -050017uint32_t grsltype_to_alignment_mask(GrSLType type) {
Brian Salomonfa26e662016-11-14 11:27:00 -050018 switch(type) {
19 case kInt_GrSLType:
20 return 0x3;
21 case kUint_GrSLType:
22 return 0x3;
23 case kFloat_GrSLType:
24 return 0x3;
25 case kVec2f_GrSLType:
26 return 0x7;
27 case kVec3f_GrSLType:
28 return 0xF;
29 case kVec4f_GrSLType:
30 return 0xF;
csmartdaltonb37cb232017-02-08 14:56:27 -050031 case kVec2i_GrSLType:
32 return 0x7;
33 case kVec3i_GrSLType:
34 return 0xF;
35 case kVec4i_GrSLType:
36 return 0xF;
Brian Salomonfa26e662016-11-14 11:27:00 -050037 case kMat22f_GrSLType:
38 return 0x7;
39 case kMat33f_GrSLType:
40 return 0xF;
41 case kMat44f_GrSLType:
42 return 0xF;
43
44 // This query is only valid for certain types.
45 case kVoid_GrSLType:
46 case kBool_GrSLType:
47 case kTexture2DSampler_GrSLType:
Brian Salomona8f00022016-11-16 12:55:57 -050048 case kITexture2DSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050049 case kTextureExternalSampler_GrSLType:
50 case kTexture2DRectSampler_GrSLType:
csmartdalton22458032016-11-16 11:28:16 -070051 case kBufferSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050052 case kTexture2D_GrSLType:
53 case kSampler_GrSLType:
Brian Salomonf9f45122016-11-29 11:59:17 -050054 case kImageStorage2D_GrSLType:
55 case kIImageStorage2D_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050056 break;
57 }
58 SkFAIL("Unexpected type");
59 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -050060}
61
egdaniel4ee1cda2016-02-26 08:18:49 -080062/** Returns the size in bytes taken up in vulkanbuffers for floating point GrSLTypes.
egdaniel75d2bfc2016-07-07 08:04:08 -070063 For non floating point type returns 0. Currently this reflects the std140 alignment
64 so a mat22 takes up 8 floats. */
egdaniel4ee1cda2016-02-26 08:18:49 -080065static inline uint32_t grsltype_to_vk_size(GrSLType type) {
Brian Salomonfa26e662016-11-14 11:27:00 -050066 switch(type) {
67 case kInt_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -050068 return sizeof(int32_t);
Brian Salomonfa26e662016-11-14 11:27:00 -050069 case kUint_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -050070 return sizeof(int32_t);
Brian Salomonfa26e662016-11-14 11:27:00 -050071 case kFloat_GrSLType:
72 return sizeof(float);
73 case kVec2f_GrSLType:
74 return 2 * sizeof(float);
75 case kVec3f_GrSLType:
76 return 3 * sizeof(float);
77 case kVec4f_GrSLType:
78 return 4 * sizeof(float);
csmartdaltonb37cb232017-02-08 14:56:27 -050079 case kVec2i_GrSLType:
80 return 2 * sizeof(int32_t);
81 case kVec3i_GrSLType:
82 return 3 * sizeof(int32_t);
83 case kVec4i_GrSLType:
84 return 4 * sizeof(int32_t);
Brian Salomonfa26e662016-11-14 11:27:00 -050085 case kMat22f_GrSLType:
86 //TODO: this will be 4 * szof(float) on std430.
87 return 8 * sizeof(float);
88 case kMat33f_GrSLType:
89 return 12 * sizeof(float);
90 case kMat44f_GrSLType:
91 return 16 * sizeof(float);
egdaniel4ee1cda2016-02-26 08:18:49 -080092
Brian Salomonfa26e662016-11-14 11:27:00 -050093 // This query is only valid for certain types.
94 case kVoid_GrSLType:
95 case kBool_GrSLType:
96 case kTexture2DSampler_GrSLType:
Brian Salomona8f00022016-11-16 12:55:57 -050097 case kITexture2DSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050098 case kTextureExternalSampler_GrSLType:
99 case kTexture2DRectSampler_GrSLType:
csmartdalton22458032016-11-16 11:28:16 -0700100 case kBufferSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500101 case kTexture2D_GrSLType:
102 case kSampler_GrSLType:
Brian Salomonf9f45122016-11-29 11:59:17 -0500103 case kImageStorage2D_GrSLType:
104 case kIImageStorage2D_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500105 break;
106 }
107 SkFAIL("Unexpected type");
108 return 0;
egdaniel4ee1cda2016-02-26 08:18:49 -0800109}
110
111
Greg Daniel164a9f02016-02-22 09:56:40 -0500112// Given the current offset into the ubo, calculate the offset for the uniform we're trying to add
113// taking into consideration all alignment requirements. The uniformOffset is set to the offset for
114// the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
115void get_ubo_aligned_offset(uint32_t* uniformOffset,
116 uint32_t* currentOffset,
117 GrSLType type,
118 int arrayCount) {
119 uint32_t alignmentMask = grsltype_to_alignment_mask(type);
egdaniel4ee1cda2016-02-26 08:18:49 -0800120 // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
egdaniel75d2bfc2016-07-07 08:04:08 -0700121 if (arrayCount || type == kMat22f_GrSLType) {
egdaniel4ee1cda2016-02-26 08:18:49 -0800122 alignmentMask = 0xF;
123 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500124 uint32_t offsetDiff = *currentOffset & alignmentMask;
125 if (offsetDiff != 0) {
126 offsetDiff = alignmentMask - offsetDiff + 1;
127 }
128 *uniformOffset = *currentOffset + offsetDiff;
129 SkASSERT(sizeof(float) == 4);
egdaniel4ee1cda2016-02-26 08:18:49 -0800130 if (arrayCount) {
131 uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type));
132 SkASSERT(0 == (elementSize & 0xF));
133 *currentOffset = *uniformOffset + elementSize * arrayCount;
134 } else {
135 *currentOffset = *uniformOffset + grsltype_to_vk_size(type);
136 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500137}
138
139GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
140 uint32_t visibility,
141 GrSLType type,
142 GrSLPrecision precision,
143 const char* name,
144 bool mangleName,
145 int arrayCount,
146 const char** outName) {
147 SkASSERT(name && strlen(name));
Greg Daniel18f96022017-05-04 15:09:03 -0400148 // For now asserting the the visibility is either geometry types (vertex, tesselation, geometry,
149 // etc.) or only fragment.
150 SkASSERT(kVertex_GrShaderFlag == visibility ||
151 kGeometry_GrShaderFlag == visibility ||
152 (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == visibility ||
153 kFragment_GrShaderFlag == visibility);
Greg Daniel164a9f02016-02-22 09:56:40 -0500154 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
egdaniel09aa1fc2016-04-20 07:09:46 -0700155 GrSLTypeIsFloatType(type);
Greg Daniel164a9f02016-02-22 09:56:40 -0500156
157 UniformInfo& uni = fUniforms.push_back();
158 uni.fVariable.setType(type);
159 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
160 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
161 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
162 // the names will mismatch. I think the correct solution is to have all GPs which need the
163 // uniform view matrix, they should upload the view matrix in their setData along with regular
164 // uniforms.
165 char prefix = 'u';
166 if ('u' == name[0]) {
167 prefix = '\0';
168 }
169 fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
170 uni.fVariable.setArrayCount(arrayCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500171 uni.fVisibility = visibility;
172 uni.fVariable.setPrecision(precision);
egdaniel09aa1fc2016-04-20 07:09:46 -0700173 // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus
174 // we set the modifier to none for all uniforms declared inside the block.
Brian Salomon99938a82016-11-21 13:41:08 -0500175 uni.fVariable.setTypeModifier(GrShaderVar::kNone_TypeModifier);
Greg Daniel164a9f02016-02-22 09:56:40 -0500176
Greg Daniel18f96022017-05-04 15:09:03 -0400177 uint32_t* currentOffset;
178 uint32_t geomStages = kVertex_GrShaderFlag | kGeometry_GrShaderFlag;
179 if (geomStages & visibility) {
180 currentOffset = &fCurrentGeometryUBOOffset;
181 } else {
182 SkASSERT(kFragment_GrShaderFlag == visibility);
183 currentOffset = &fCurrentFragmentUBOOffset;
184 }
egdaniel09aa1fc2016-04-20 07:09:46 -0700185 get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500186
Greg Danieldbd44c72017-01-24 15:12:12 -0500187 SkString layoutQualifier;
188 layoutQualifier.appendf("offset=%d", uni.fUBOffset);
189 uni.fVariable.addLayoutQualifier(layoutQualifier.c_str());
190
egdaniel09aa1fc2016-04-20 07:09:46 -0700191 if (outName) {
192 *outName = uni.fVariable.c_str();
Greg Daniel164a9f02016-02-22 09:56:40 -0500193 }
194
195 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
196}
197
Brian Salomon101b8442016-11-18 11:58:54 -0500198GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(uint32_t visibility,
199 GrSwizzle swizzle,
200 GrSLType type,
201 GrSLPrecision precision,
202 const char* name) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700203 SkASSERT(name && strlen(name));
Greg Daniel18f96022017-05-04 15:09:03 -0400204 // For now asserting the the visibility is either only vertex, geometry, or fragment
205 SkASSERT(kVertex_GrShaderFlag == visibility ||
206 kFragment_GrShaderFlag == visibility ||
207 kGeometry_GrShaderFlag == visibility);
egdaniel09aa1fc2016-04-20 07:09:46 -0700208 SkString mangleName;
209 char prefix = 'u';
210 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
Brian Salomon101b8442016-11-18 11:58:54 -0500211
212 UniformInfo& info = fSamplers.push_back();
213 SkASSERT(GrSLTypeIsCombinedSamplerType(type));
214 info.fVariable.setType(type);
Brian Salomon99938a82016-11-21 13:41:08 -0500215 info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
Brian Salomon101b8442016-11-18 11:58:54 -0500216 info.fVariable.setPrecision(precision);
217 info.fVariable.setName(mangleName);
218 SkString layoutQualifier;
219 layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count() - 1);
Brian Salomon60397682016-11-22 15:06:46 -0500220 info.fVariable.addLayoutQualifier(layoutQualifier.c_str());
Brian Salomon101b8442016-11-18 11:58:54 -0500221 info.fVisibility = visibility;
222 info.fUBOffset = 0;
223 fSamplerSwizzles.push_back(swizzle);
224 SkASSERT(fSamplerSwizzles.count() == fSamplers.count());
egdaniel09aa1fc2016-04-20 07:09:46 -0700225 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
226}
227
Greg Daniel31ec1442017-05-08 10:30:59 -0400228GrGLSLUniformHandler::TexelBufferHandle GrVkUniformHandler::addTexelBuffer(uint32_t visibility,
229 GrSLPrecision precision,
230 const char* name) {
231 SkASSERT(name && strlen(name));
Greg Danielaa352de2017-07-17 09:05:16 -0400232 SkDEBUGCODE(static const uint32_t kVisMask = kVertex_GrShaderFlag |
233 kGeometry_GrShaderFlag |
234 kFragment_GrShaderFlag);
Greg Daniel31ec1442017-05-08 10:30:59 -0400235 SkASSERT(0 == (~kVisMask & visibility));
236 SkASSERT(0 != visibility);
237 SkString mangleName;
238 char prefix = 'u';
239 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
240
241 UniformInfo& info = fTexelBuffers.push_back();
242 info.fVariable.setType(kBufferSampler_GrSLType);
243 info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
244 info.fVariable.setPrecision(precision);
245 info.fVariable.setName(mangleName);
246 SkString layoutQualifier;
247 layoutQualifier.appendf("set=%d, binding=%d", kTexelBufferDescSet, fTexelBuffers.count()- 1);
248 info.fVariable.addLayoutQualifier(layoutQualifier.c_str());
249 info.fVisibility = visibility;
250 info.fUBOffset = 0;
251 return GrGLSLUniformHandler::TexelBufferHandle(fTexelBuffers.count() - 1);
252}
253
Greg Daniel164a9f02016-02-22 09:56:40 -0500254void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
Greg Daniel18f96022017-05-04 15:09:03 -0400255 SkASSERT(kVertex_GrShaderFlag == visibility ||
256 kGeometry_GrShaderFlag == visibility ||
257 kFragment_GrShaderFlag == visibility);
egdaniel09aa1fc2016-04-20 07:09:46 -0700258
259 for (int i = 0; i < fSamplers.count(); ++i) {
Brian Salomon101b8442016-11-18 11:58:54 -0500260 const UniformInfo& sampler = fSamplers[i];
261 SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType);
262 if (visibility == sampler.fVisibility) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500263 sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
egdaniel09aa1fc2016-04-20 07:09:46 -0700264 out->append(";\n");
265 }
266 }
267
Greg Daniel31ec1442017-05-08 10:30:59 -0400268 for (int i = 0; i < fTexelBuffers.count(); ++i) {
269 const UniformInfo& texelBuffer = fTexelBuffers[i];
270 if (visibility == texelBuffer.fVisibility) {
271 texelBuffer.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
272 out->append(";\n");
273 }
274 }
275
Greg Danielaa352de2017-07-17 09:05:16 -0400276#ifdef SK_DEBUG
277 bool firstGeomOffsetCheck = false;
278 bool firstFragOffsetCheck = false;
279 for (int i = 0; i < fUniforms.count(); ++i) {
280 const UniformInfo& localUniform = fUniforms[i];
281 if (kVertex_GrShaderFlag == localUniform.fVisibility ||
282 kGeometry_GrShaderFlag == localUniform.fVisibility ||
283 (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == localUniform.fVisibility) {
284 if (!firstGeomOffsetCheck) {
285 // Check to make sure we are starting our offset at 0 so the offset qualifier we
286 // set on each variable in the uniform block is valid.
287 SkASSERT(0 == localUniform.fUBOffset);
288 firstGeomOffsetCheck = true;
289 }
290 } else {
291 SkASSERT(kFragment_GrShaderFlag == localUniform.fVisibility);
292 if (!firstFragOffsetCheck) {
293 // Check to make sure we are starting our offset at 0 so the offset qualifier we
294 // set on each variable in the uniform block is valid.
295 SkASSERT(0 == localUniform.fUBOffset);
296 firstFragOffsetCheck = true;
297 }
298 }
299 }
300#endif
301
egdaniel09aa1fc2016-04-20 07:09:46 -0700302 SkString uniformsString;
Greg Daniel164a9f02016-02-22 09:56:40 -0500303 for (int i = 0; i < fUniforms.count(); ++i) {
304 const UniformInfo& localUniform = fUniforms[i];
Greg Daniel18f96022017-05-04 15:09:03 -0400305 if (visibility & localUniform.fVisibility) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500306 if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500307 localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
Greg Daniel164a9f02016-02-22 09:56:40 -0500308 uniformsString.append(";\n");
Greg Daniel164a9f02016-02-22 09:56:40 -0500309 }
310 }
311 }
Greg Danielaa352de2017-07-17 09:05:16 -0400312
Greg Daniel164a9f02016-02-22 09:56:40 -0500313 if (!uniformsString.isEmpty()) {
Greg Daniel18f96022017-05-04 15:09:03 -0400314 uint32_t uniformBinding;
315 const char* stage;
316 if (kVertex_GrShaderFlag == visibility) {
317 uniformBinding = kGeometryBinding;
318 stage = "vertex";
319 } else if (kGeometry_GrShaderFlag == visibility) {
320 uniformBinding = kGeometryBinding;
321 stage = "geometry";
322 } else {
323 SkASSERT(kFragment_GrShaderFlag == visibility);
324 uniformBinding = kFragBinding;
325 stage = "fragment";
326 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500327 out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n",
328 kUniformBufferDescSet, uniformBinding, stage);
329 out->appendf("%s\n};\n", uniformsString.c_str());
330 }
egdaniel4ee1cda2016-02-26 08:18:49 -0800331}