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