1 /*
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).
14 // 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).
grsltype_to_alignment_mask(GrSLType type)17 uint32_t grsltype_to_alignment_mask(GrSLType type) {
18 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;
31 case kVec2i_GrSLType:
32 return 0x7;
33 case kVec3i_GrSLType:
34 return 0xF;
35 case kVec4i_GrSLType:
36 return 0xF;
37 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:
48 case kITexture2DSampler_GrSLType:
49 case kTextureExternalSampler_GrSLType:
50 case kTexture2DRectSampler_GrSLType:
51 case kBufferSampler_GrSLType:
52 case kTexture2D_GrSLType:
53 case kSampler_GrSLType:
54 case kImageStorage2D_GrSLType:
55 case kIImageStorage2D_GrSLType:
56 break;
57 }
58 SkFAIL("Unexpected type");
59 return 0;
60 }
61
62 /** Returns the size in bytes taken up in vulkanbuffers for floating point GrSLTypes.
63 For non floating point type returns 0. Currently this reflects the std140 alignment
64 so a mat22 takes up 8 floats. */
grsltype_to_vk_size(GrSLType type)65 static inline uint32_t grsltype_to_vk_size(GrSLType type) {
66 switch(type) {
67 case kInt_GrSLType:
68 return sizeof(int32_t);
69 case kUint_GrSLType:
70 return sizeof(int32_t);
71 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);
79 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);
85 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);
92
93 // This query is only valid for certain types.
94 case kVoid_GrSLType:
95 case kBool_GrSLType:
96 case kTexture2DSampler_GrSLType:
97 case kITexture2DSampler_GrSLType:
98 case kTextureExternalSampler_GrSLType:
99 case kTexture2DRectSampler_GrSLType:
100 case kBufferSampler_GrSLType:
101 case kTexture2D_GrSLType:
102 case kSampler_GrSLType:
103 case kImageStorage2D_GrSLType:
104 case kIImageStorage2D_GrSLType:
105 break;
106 }
107 SkFAIL("Unexpected type");
108 return 0;
109 }
110
111
112 // 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.
get_ubo_aligned_offset(uint32_t * uniformOffset,uint32_t * currentOffset,GrSLType type,int arrayCount)115 void 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);
120 // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
121 if (arrayCount || type == kMat22f_GrSLType) {
122 alignmentMask = 0xF;
123 }
124 uint32_t offsetDiff = *currentOffset & alignmentMask;
125 if (offsetDiff != 0) {
126 offsetDiff = alignmentMask - offsetDiff + 1;
127 }
128 *uniformOffset = *currentOffset + offsetDiff;
129 SkASSERT(sizeof(float) == 4);
130 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 }
137 }
138
internalAddUniformArray(uint32_t visibility,GrSLType type,GrSLPrecision precision,const char * name,bool mangleName,int arrayCount,const char ** outName)139 GrGLSLUniformHandler::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));
148 // 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);
154 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
155 GrSLTypeIsFloatType(type);
156
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);
171 uni.fVisibility = visibility;
172 uni.fVariable.setPrecision(precision);
173 // 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.
175 uni.fVariable.setTypeModifier(GrShaderVar::kNone_TypeModifier);
176
177 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 }
185 get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount);
186
187 SkString layoutQualifier;
188 layoutQualifier.appendf("offset=%d", uni.fUBOffset);
189 uni.fVariable.addLayoutQualifier(layoutQualifier.c_str());
190
191 if (outName) {
192 *outName = uni.fVariable.c_str();
193 }
194
195 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
196 }
197
addSampler(uint32_t visibility,GrSwizzle swizzle,GrSLType type,GrSLPrecision precision,const char * name)198 GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(uint32_t visibility,
199 GrSwizzle swizzle,
200 GrSLType type,
201 GrSLPrecision precision,
202 const char* name) {
203 SkASSERT(name && strlen(name));
204 // 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);
208 SkString mangleName;
209 char prefix = 'u';
210 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
211
212 UniformInfo& info = fSamplers.push_back();
213 SkASSERT(GrSLTypeIsCombinedSamplerType(type));
214 info.fVariable.setType(type);
215 info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
216 info.fVariable.setPrecision(precision);
217 info.fVariable.setName(mangleName);
218 SkString layoutQualifier;
219 layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count() - 1);
220 info.fVariable.addLayoutQualifier(layoutQualifier.c_str());
221 info.fVisibility = visibility;
222 info.fUBOffset = 0;
223 fSamplerSwizzles.push_back(swizzle);
224 SkASSERT(fSamplerSwizzles.count() == fSamplers.count());
225 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
226 }
227
addTexelBuffer(uint32_t visibility,GrSLPrecision precision,const char * name)228 GrGLSLUniformHandler::TexelBufferHandle GrVkUniformHandler::addTexelBuffer(uint32_t visibility,
229 GrSLPrecision precision,
230 const char* name) {
231 SkASSERT(name && strlen(name));
232 SkDEBUGCODE(static const uint32_t kVisMask = kVertex_GrShaderFlag |
233 kGeometry_GrShaderFlag |
234 kFragment_GrShaderFlag);
235 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
appendUniformDecls(GrShaderFlags visibility,SkString * out) const254 void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
255 SkASSERT(kVertex_GrShaderFlag == visibility ||
256 kGeometry_GrShaderFlag == visibility ||
257 kFragment_GrShaderFlag == visibility);
258
259 for (int i = 0; i < fSamplers.count(); ++i) {
260 const UniformInfo& sampler = fSamplers[i];
261 SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType);
262 if (visibility == sampler.fVisibility) {
263 sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
264 out->append(";\n");
265 }
266 }
267
268 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
276 #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
302 SkString uniformsString;
303 for (int i = 0; i < fUniforms.count(); ++i) {
304 const UniformInfo& localUniform = fUniforms[i];
305 if (visibility & localUniform.fVisibility) {
306 if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
307 localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
308 uniformsString.append(";\n");
309 }
310 }
311 }
312
313 if (!uniformsString.isEmpty()) {
314 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 }
327 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 }
331 }
332