• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/gpu/vk/GrVkUniformHandler.h"
9 
10 #include "src/gpu/GrTexture.h"
11 #include "src/gpu/glsl/GrGLSLProgramBuilder.h"
12 #include "src/gpu/vk/GrVkGpu.h"
13 #include "src/gpu/vk/GrVkPipelineStateBuilder.h"
14 #include "src/gpu/vk/GrVkTexture.h"
15 
16 // To determine whether a current offset is aligned, we can just 'and' the lowest bits with the
17 // alignment mask. A value of 0 means aligned, any other value is how many bytes past alignment we
18 // are. This works since all alignments are powers of 2. The mask is always (alignment - 1).
19 // This alignment mask will give correct alignments for using the std430 block layout. If you want
20 // the std140 alignment, you can use this, but then make sure if you have an array type it is
21 // aligned to 16 bytes (i.e. has mask of 0xF).
22 // These are designated in the Vulkan spec, section 14.5.4 "Offset and Stride Assignment".
23 // https://www.khronos.org/registry/vulkan/specs/1.0-wsi_extensions/html/vkspec.html#interfaces-resources-layout
grsltype_to_alignment_mask(GrSLType type)24 static uint32_t grsltype_to_alignment_mask(GrSLType type) {
25     switch(type) {
26         case kByte_GrSLType: // fall through
27         case kUByte_GrSLType:
28             return 0x0;
29         case kByte2_GrSLType: // fall through
30         case kUByte2_GrSLType:
31             return 0x1;
32         case kByte3_GrSLType: // fall through
33         case kByte4_GrSLType:
34         case kUByte3_GrSLType:
35         case kUByte4_GrSLType:
36             return 0x3;
37         case kShort_GrSLType: // fall through
38         case kUShort_GrSLType:
39             return 0x1;
40         case kShort2_GrSLType: // fall through
41         case kUShort2_GrSLType:
42             return 0x3;
43         case kShort3_GrSLType: // fall through
44         case kShort4_GrSLType:
45         case kUShort3_GrSLType:
46         case kUShort4_GrSLType:
47             return 0x7;
48         case kInt_GrSLType:
49         case kUint_GrSLType:
50             return 0x3;
51         case kInt2_GrSLType:
52         case kUint2_GrSLType:
53             return 0x7;
54         case kInt3_GrSLType:
55         case kUint3_GrSLType:
56         case kInt4_GrSLType:
57         case kUint4_GrSLType:
58             return 0xF;
59         case kHalf_GrSLType: // fall through
60         case kFloat_GrSLType:
61             return 0x3;
62         case kHalf2_GrSLType: // fall through
63         case kFloat2_GrSLType:
64             return 0x7;
65         case kHalf3_GrSLType: // fall through
66         case kFloat3_GrSLType:
67             return 0xF;
68         case kHalf4_GrSLType: // fall through
69         case kFloat4_GrSLType:
70             return 0xF;
71         case kHalf2x2_GrSLType: // fall through
72         case kFloat2x2_GrSLType:
73             return 0x7;
74         case kHalf3x3_GrSLType: // fall through
75         case kFloat3x3_GrSLType:
76             return 0xF;
77         case kHalf4x4_GrSLType: // fall through
78         case kFloat4x4_GrSLType:
79             return 0xF;
80 
81         // This query is only valid for certain types.
82         case kVoid_GrSLType:
83         case kBool_GrSLType:
84         case kBool2_GrSLType:
85         case kBool3_GrSLType:
86         case kBool4_GrSLType:
87         case kTexture2DSampler_GrSLType:
88         case kTextureExternalSampler_GrSLType:
89         case kTexture2DRectSampler_GrSLType:
90         case kSampler_GrSLType:
91         case kTexture2D_GrSLType:
92         case kInput_GrSLType:
93             break;
94     }
95     SK_ABORT("Unexpected type");
96 }
97 
98 /** Returns the size in bytes taken up in vulkanbuffers for GrSLTypes. */
grsltype_to_vk_size(GrSLType type,int layout)99 static inline uint32_t grsltype_to_vk_size(GrSLType type, int layout) {
100     switch(type) {
101         case kByte_GrSLType:
102             return sizeof(int8_t);
103         case kByte2_GrSLType:
104             return 2 * sizeof(int8_t);
105         case kByte3_GrSLType:
106             return 3 * sizeof(int8_t);
107         case kByte4_GrSLType:
108             return 4 * sizeof(int8_t);
109         case kUByte_GrSLType:
110             return sizeof(uint8_t);
111         case kUByte2_GrSLType:
112             return 2 * sizeof(uint8_t);
113         case kUByte3_GrSLType:
114             return 3 * sizeof(uint8_t);
115         case kUByte4_GrSLType:
116             return 4 * sizeof(uint8_t);
117         case kShort_GrSLType:
118             return sizeof(int16_t);
119         case kShort2_GrSLType:
120             return 2 * sizeof(int16_t);
121         case kShort3_GrSLType:
122             return 3 * sizeof(int16_t);
123         case kShort4_GrSLType:
124             return 4 * sizeof(int16_t);
125         case kUShort_GrSLType:
126             return sizeof(uint16_t);
127         case kUShort2_GrSLType:
128             return 2 * sizeof(uint16_t);
129         case kUShort3_GrSLType:
130             return 3 * sizeof(uint16_t);
131         case kUShort4_GrSLType:
132             return 4 * sizeof(uint16_t);
133         case kHalf_GrSLType: // fall through
134         case kFloat_GrSLType:
135             return sizeof(float);
136         case kHalf2_GrSLType: // fall through
137         case kFloat2_GrSLType:
138             return 2 * sizeof(float);
139         case kHalf3_GrSLType: // fall through
140         case kFloat3_GrSLType:
141             return 3 * sizeof(float);
142         case kHalf4_GrSLType: // fall through
143         case kFloat4_GrSLType:
144             return 4 * sizeof(float);
145         case kInt_GrSLType: // fall through
146         case kUint_GrSLType:
147             return sizeof(int32_t);
148         case kInt2_GrSLType: // fall through
149         case kUint2_GrSLType:
150             return 2 * sizeof(int32_t);
151         case kInt3_GrSLType: // fall through
152         case kUint3_GrSLType:
153             return 3 * sizeof(int32_t);
154         case kInt4_GrSLType: // fall through
155         case kUint4_GrSLType:
156             return 4 * sizeof(int32_t);
157         case kHalf2x2_GrSLType: // fall through
158         case kFloat2x2_GrSLType:
159             if (layout == GrVkUniformHandler::kStd430Layout) {
160                 return 4 * sizeof(float);
161             } else {
162                 return 8 * sizeof(float);
163             }
164         case kHalf3x3_GrSLType: // fall through
165         case kFloat3x3_GrSLType:
166             return 12 * sizeof(float);
167         case kHalf4x4_GrSLType: // fall through
168         case kFloat4x4_GrSLType:
169             return 16 * sizeof(float);
170 
171         // This query is only valid for certain types.
172         case kVoid_GrSLType:
173         case kBool_GrSLType:
174         case kBool2_GrSLType:
175         case kBool3_GrSLType:
176         case kBool4_GrSLType:
177         case kTexture2DSampler_GrSLType:
178         case kTextureExternalSampler_GrSLType:
179         case kTexture2DRectSampler_GrSLType:
180         case kSampler_GrSLType:
181         case kTexture2D_GrSLType:
182         case kInput_GrSLType:
183             break;
184     }
185     SK_ABORT("Unexpected type");
186 }
187 
188 // Given the current offset into the ubo data, calculate the offset for the uniform we're trying to
189 // add taking into consideration all alignment requirements. The uniformOffset is set to the offset
190 // for the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
get_aligned_offset(uint32_t * currentOffset,GrSLType type,int arrayCount,int layout)191 static uint32_t get_aligned_offset(uint32_t* currentOffset,
192                                    GrSLType type,
193                                    int arrayCount,
194                                    int layout) {
195     uint32_t alignmentMask = grsltype_to_alignment_mask(type);
196     // For std140 layout we must make arrays align to 16 bytes.
197     if (layout == GrVkUniformHandler::kStd140Layout && (arrayCount || type == kFloat2x2_GrSLType)) {
198         alignmentMask = 0xF;
199     }
200     uint32_t offsetDiff = *currentOffset & alignmentMask;
201     if (offsetDiff != 0) {
202         offsetDiff = alignmentMask - offsetDiff + 1;
203     }
204     int32_t uniformOffset = *currentOffset + offsetDiff;
205     SkASSERT(sizeof(float) == 4);
206     if (arrayCount) {
207         // TODO: this shouldn't be necessary for std430
208         uint32_t elementSize = std::max<uint32_t>(16, grsltype_to_vk_size(type, layout));
209         SkASSERT(0 == (elementSize & 0xF));
210         *currentOffset = uniformOffset + elementSize * arrayCount;
211     } else {
212         *currentOffset = uniformOffset + grsltype_to_vk_size(type, layout);
213     }
214     return uniformOffset;
215 }
216 
~GrVkUniformHandler()217 GrVkUniformHandler::~GrVkUniformHandler() {
218     for (VkUniformInfo& sampler : fSamplers.items()) {
219         if (sampler.fImmutableSampler) {
220             sampler.fImmutableSampler->unref();
221             sampler.fImmutableSampler = nullptr;
222         }
223     }
224 }
225 
internalAddUniformArray(const GrFragmentProcessor * owner,uint32_t visibility,GrSLType type,const char * name,bool mangleName,int arrayCount,const char ** outName)226 GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
227                                                                    const GrFragmentProcessor* owner,
228                                                                    uint32_t visibility,
229                                                                    GrSLType type,
230                                                                    const char* name,
231                                                                    bool mangleName,
232                                                                    int arrayCount,
233                                                                    const char** outName) {
234     SkASSERT(name && strlen(name));
235     SkASSERT(GrSLTypeCanBeUniformValue(type));
236 
237     // TODO this is a bit hacky, lets think of a better way.  Basically we need to be able to use
238     // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
239     // exactly what name it wants to use for the uniform view matrix.  If we prefix anythings, then
240     // the names will mismatch.  I think the correct solution is to have all GPs which need the
241     // uniform view matrix, they should upload the view matrix in their setData along with regular
242     // uniforms.
243     char prefix = 'u';
244     if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
245         prefix = '\0';
246     }
247     SkString resolvedName = fProgramBuilder->nameVariable(prefix, name, mangleName);
248 
249     uint32_t offsets[kLayoutCount];
250     for (int layout = 0; layout < kLayoutCount; ++layout) {
251         offsets[layout] = get_aligned_offset(&fCurrentOffsets[layout], type, arrayCount, layout);
252     }
253 
254     VkUniformInfo& uni = fUniforms.push_back(VkUniformInfo{
255         {
256             GrShaderVar{std::move(resolvedName), type, GrShaderVar::TypeModifier::None, arrayCount},
257             visibility, owner, SkString(name)
258         },
259         {offsets[0], offsets[1]}, nullptr
260     });
261 
262     if (outName) {
263         *outName = uni.fVariable.c_str();
264     }
265 
266     return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
267 }
268 
addSampler(const GrBackendFormat & backendFormat,GrSamplerState state,const GrSwizzle & swizzle,const char * name,const GrShaderCaps * shaderCaps)269 GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(
270         const GrBackendFormat& backendFormat, GrSamplerState state, const GrSwizzle& swizzle,
271         const char* name, const GrShaderCaps* shaderCaps) {
272     SkASSERT(name && strlen(name));
273 
274     const char prefix = 'u';
275     SkString mangleName = fProgramBuilder->nameVariable(prefix, name, /*mangle=*/true);
276 
277     SkString layoutQualifier;
278     layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count());
279 
280     VkUniformInfo& info = fSamplers.push_back(VkUniformInfo{
281         {
282             GrShaderVar{std::move(mangleName),
283                         GrSLCombinedSamplerTypeForTextureType(backendFormat.textureType()),
284                         GrShaderVar::TypeModifier::Uniform, GrShaderVar::kNonArray,
285                         std::move(layoutQualifier), SkString()},
286             kFragment_GrShaderFlag, nullptr, SkString(name)
287         },
288         {0, 0}, nullptr
289     });
290 
291     // Check if we are dealing with an external texture and store the needed information if so.
292     auto ycbcrInfo = backendFormat.getVkYcbcrConversionInfo();
293     if (ycbcrInfo && ycbcrInfo->isValid()) {
294         GrVkGpu* gpu = static_cast<GrVkPipelineStateBuilder*>(fProgramBuilder)->gpu();
295         info.fImmutableSampler = gpu->resourceProvider().findOrCreateCompatibleSampler(
296                 state, *ycbcrInfo);
297         if (!info.fImmutableSampler) {
298             return {};
299         }
300     }
301 
302     fSamplerSwizzles.push_back(swizzle);
303     SkASSERT(fSamplerSwizzles.count() == fSamplers.count());
304     return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
305 }
306 
addInputSampler(const GrSwizzle & swizzle,const char * name)307 GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addInputSampler(const GrSwizzle& swizzle,
308                                                                         const char* name) {
309     SkASSERT(name && strlen(name));
310     SkASSERT(fInputUniform.fVariable.getType() == kVoid_GrSLType);
311 
312     const char prefix = 'u';
313     SkString mangleName = fProgramBuilder->nameVariable(prefix, name, /*mangle=*/true);
314 
315     SkString layoutQualifier;
316     layoutQualifier.appendf("input_attachment_index=%d, set=%d, binding=%d",
317                             kDstInputAttachmentIndex, kInputDescSet, kInputBinding);
318 
319     fInputUniform = {
320             GrShaderVar{std::move(mangleName), kInput_GrSLType, GrShaderVar::TypeModifier::Uniform,
321                         GrShaderVar::kNonArray, std::move(layoutQualifier), SkString()},
322             kFragment_GrShaderFlag, nullptr, SkString(name)};
323     fInputSwizzle = swizzle;
324     return GrGLSLUniformHandler::SamplerHandle(0);
325 }
326 
appendUniformDecls(GrShaderFlags visibility,SkString * out) const327 void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
328     for (const VkUniformInfo& sampler : fSamplers.items()) {
329         SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType ||
330                  sampler.fVariable.getType() == kTextureExternalSampler_GrSLType);
331         if (visibility == sampler.fVisibility) {
332             sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
333             out->append(";\n");
334         }
335     }
336     if (fInputUniform.fVariable.getType() == kInput_GrSLType) {
337         if (visibility == fInputUniform.fVisibility) {
338             SkASSERT(visibility == kFragment_GrShaderFlag);
339             fInputUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
340             out->append(";\n");
341         }
342     }
343 
344 #ifdef SK_DEBUG
345     bool firstOffsetCheck = false;
346     for (const VkUniformInfo& localUniform : fUniforms.items()) {
347         if (!firstOffsetCheck) {
348             // Check to make sure we are starting our offset at 0 so the offset qualifier we
349             // set on each variable in the uniform block is valid.
350             SkASSERT(0 == localUniform.fOffsets[kStd140Layout] &&
351                      0 == localUniform.fOffsets[kStd430Layout]);
352             firstOffsetCheck = true;
353         }
354     }
355 #endif
356 
357     // At this point we determine whether we'll be using push constants based on the
358     // uniforms set so far. Later checks will use the internal bool we set here to
359     // keep things consistent.
360     this->determineIfUsePushConstants();
361     SkString uniformsString;
362     for (const VkUniformInfo& localUniform : fUniforms.items()) {
363         if (visibility & localUniform.fVisibility) {
364             if (GrSLTypeCanBeUniformValue(localUniform.fVariable.getType())) {
365                 Layout layout = fUsePushConstants ? kStd430Layout : kStd140Layout;
366                 uniformsString.appendf("layout(offset=%d) ", localUniform.fOffsets[layout]);
367                 localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
368                 uniformsString.append(";\n");
369             }
370         }
371     }
372 
373     if (!uniformsString.isEmpty()) {
374         if (fUsePushConstants) {
375             out->append("layout (push_constant) ");
376         } else {
377             out->appendf("layout (set=%d, binding=%d) ",
378                          kUniformBufferDescSet, kUniformBinding);
379         }
380         out->append("uniform uniformBuffer\n{\n");
381         out->appendf("%s\n};\n", uniformsString.c_str());
382     }
383 }
384 
getRTHeightOffset() const385 uint32_t GrVkUniformHandler::getRTHeightOffset() const {
386     Layout layout = fUsePushConstants ? kStd430Layout : kStd140Layout;
387     uint32_t currentOffset = fCurrentOffsets[layout];
388     return get_aligned_offset(&currentOffset, kFloat_GrSLType, 0, layout);
389 }
390 
determineIfUsePushConstants() const391 void GrVkUniformHandler::determineIfUsePushConstants() const {
392     // If flipY is enabled we may be adding the RTHeight uniform during compilation.
393     // We won't know that for sure until then but we need to make this determination now,
394     // so assume we will need it.
395     uint32_t pad = fFlipY ? sizeof(float) : 0;
396     fUsePushConstants = fCurrentOffsets[kStd430Layout] > 0 &&
397             fCurrentOffsets[kStd430Layout] + pad <= fProgramBuilder->caps()->maxPushConstantsSize();
398 }
399