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/GrTexturePriv.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 kHalf_GrSLType: // fall through
52         case kFloat_GrSLType:
53             return 0x3;
54         case kHalf2_GrSLType: // fall through
55         case kFloat2_GrSLType:
56             return 0x7;
57         case kHalf3_GrSLType: // fall through
58         case kFloat3_GrSLType:
59             return 0xF;
60         case kHalf4_GrSLType: // fall through
61         case kFloat4_GrSLType:
62             return 0xF;
63         case kUint2_GrSLType:
64             return 0x7;
65         case kInt2_GrSLType:
66             return 0x7;
67         case kInt3_GrSLType:
68             return 0xF;
69         case kInt4_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 kTexture2DSampler_GrSLType:
85         case kTextureExternalSampler_GrSLType:
86         case kTexture2DRectSampler_GrSLType:
87         case kSampler_GrSLType:
88         case kTexture2D_GrSLType:
89             break;
90     }
91     SK_ABORT("Unexpected type");
92 }
93 
94 /** Returns the size in bytes taken up in vulkanbuffers for GrSLTypes. */
grsltype_to_vk_size(GrSLType type)95 static inline uint32_t grsltype_to_vk_size(GrSLType type) {
96     switch(type) {
97         case kByte_GrSLType:
98             return sizeof(int8_t);
99         case kByte2_GrSLType:
100             return 2 * sizeof(int8_t);
101         case kByte3_GrSLType:
102             return 3 * sizeof(int8_t);
103         case kByte4_GrSLType:
104             return 4 * sizeof(int8_t);
105         case kUByte_GrSLType:
106             return sizeof(uint8_t);
107         case kUByte2_GrSLType:
108             return 2 * sizeof(uint8_t);
109         case kUByte3_GrSLType:
110             return 3 * sizeof(uint8_t);
111         case kUByte4_GrSLType:
112             return 4 * sizeof(uint8_t);
113         case kShort_GrSLType:
114             return sizeof(int16_t);
115         case kShort2_GrSLType:
116             return 2 * sizeof(int16_t);
117         case kShort3_GrSLType:
118             return 3 * sizeof(int16_t);
119         case kShort4_GrSLType:
120             return 4 * sizeof(int16_t);
121         case kUShort_GrSLType:
122             return sizeof(uint16_t);
123         case kUShort2_GrSLType:
124             return 2 * sizeof(uint16_t);
125         case kUShort3_GrSLType:
126             return 3 * sizeof(uint16_t);
127         case kUShort4_GrSLType:
128             return 4 * sizeof(uint16_t);
129         case kInt_GrSLType:
130             return sizeof(int32_t);
131         case kUint_GrSLType:
132             return sizeof(int32_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 kUint2_GrSLType:
146             return 2 * sizeof(uint32_t);
147         case kInt2_GrSLType:
148             return 2 * sizeof(int32_t);
149         case kInt3_GrSLType:
150             return 3 * sizeof(int32_t);
151         case kInt4_GrSLType:
152             return 4 * sizeof(int32_t);
153         case kHalf2x2_GrSLType: // fall through
154         case kFloat2x2_GrSLType:
155             //TODO: this will be 4 * szof(float) on std430.
156             return 8 * sizeof(float);
157         case kHalf3x3_GrSLType: // fall through
158         case kFloat3x3_GrSLType:
159             return 12 * sizeof(float);
160         case kHalf4x4_GrSLType: // fall through
161         case kFloat4x4_GrSLType:
162             return 16 * sizeof(float);
163 
164         // This query is only valid for certain types.
165         case kVoid_GrSLType:
166         case kBool_GrSLType:
167         case kTexture2DSampler_GrSLType:
168         case kTextureExternalSampler_GrSLType:
169         case kTexture2DRectSampler_GrSLType:
170         case kSampler_GrSLType:
171         case kTexture2D_GrSLType:
172             break;
173     }
174     SK_ABORT("Unexpected type");
175 }
176 
177 
178 // Given the current offset into the ubo, calculate the offset for the uniform we're trying to add
179 // taking into consideration all alignment requirements. The uniformOffset is set to the offset for
180 // the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
get_ubo_aligned_offset(uint32_t * currentOffset,GrSLType type,int arrayCount)181 static uint32_t get_ubo_aligned_offset(uint32_t* currentOffset,
182                                    GrSLType type,
183                                    int arrayCount) {
184     uint32_t alignmentMask = grsltype_to_alignment_mask(type);
185     // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
186     if (arrayCount || type == kFloat2x2_GrSLType) {
187         alignmentMask = 0xF;
188     }
189     uint32_t offsetDiff = *currentOffset & alignmentMask;
190     if (offsetDiff != 0) {
191         offsetDiff = alignmentMask - offsetDiff + 1;
192     }
193     int32_t uniformOffset = *currentOffset + offsetDiff;
194     SkASSERT(sizeof(float) == 4);
195     if (arrayCount) {
196         uint32_t elementSize = std::max<uint32_t>(16, grsltype_to_vk_size(type));
197         SkASSERT(0 == (elementSize & 0xF));
198         *currentOffset = uniformOffset + elementSize * arrayCount;
199     } else {
200         *currentOffset = uniformOffset + grsltype_to_vk_size(type);
201     }
202     return uniformOffset;
203 }
204 
~GrVkUniformHandler()205 GrVkUniformHandler::~GrVkUniformHandler() {
206     for (UniformInfo& sampler : fSamplers.items()) {
207         if (sampler.fImmutableSampler) {
208             sampler.fImmutableSampler->unref();
209             sampler.fImmutableSampler = nullptr;
210         }
211     }
212 }
213 
internalAddUniformArray(uint32_t visibility,GrSLType type,const char * name,bool mangleName,int arrayCount,const char ** outName)214 GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
215                                                                             uint32_t visibility,
216                                                                             GrSLType type,
217                                                                             const char* name,
218                                                                             bool mangleName,
219                                                                             int arrayCount,
220                                                                             const char** outName) {
221     SkASSERT(name && strlen(name));
222     SkASSERT(GrSLTypeIsFloatType(type));
223 
224     // TODO this is a bit hacky, lets think of a better way.  Basically we need to be able to use
225     // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
226     // exactly what name it wants to use for the uniform view matrix.  If we prefix anythings, then
227     // the names will mismatch.  I think the correct solution is to have all GPs which need the
228     // uniform view matrix, they should upload the view matrix in their setData along with regular
229     // uniforms.
230     SkString resolvedName;
231     char prefix = 'u';
232     if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
233         prefix = '\0';
234     }
235     fProgramBuilder->nameVariable(&resolvedName, prefix, name, mangleName);
236 
237     uint32_t offset = get_ubo_aligned_offset(&fCurrentUBOOffset, type, arrayCount);
238     SkString layoutQualifier;
239     layoutQualifier.appendf("offset=%d", offset);
240 
241     UniformInfo& uni = fUniforms.push_back(GrVkUniformHandler::UniformInfo{
242         GrShaderVar{std::move(resolvedName), type, GrShaderVar::TypeModifier::None, arrayCount,
243                     std::move(layoutQualifier), SkString()},
244         visibility, offset, nullptr
245     });
246 
247     if (outName) {
248         *outName = uni.fVariable.c_str();
249     }
250 
251     return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
252 }
253 
addSampler(const GrBackendFormat & backendFormat,GrSamplerState state,const GrSwizzle & swizzle,const char * name,const GrShaderCaps * shaderCaps)254 GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(
255         const GrBackendFormat& backendFormat, GrSamplerState state, const GrSwizzle& swizzle,
256         const char* name, const GrShaderCaps* shaderCaps) {
257     SkASSERT(name && strlen(name));
258 
259     SkString mangleName;
260     char prefix = 'u';
261     fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
262 
263     SkString layoutQualifier;
264     layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count());
265 
266     UniformInfo& info = fSamplers.push_back(GrVkUniformHandler::UniformInfo{
267         GrShaderVar{std::move(mangleName),
268                     GrSLCombinedSamplerTypeForTextureType(backendFormat.textureType()),
269                     GrShaderVar::TypeModifier::Uniform, GrShaderVar::kNonArray,
270                     std::move(layoutQualifier), SkString()},
271         kFragment_GrShaderFlag, 0, nullptr
272     });
273 
274     // Check if we are dealing with an external texture and store the needed information if so.
275     auto ycbcrInfo = backendFormat.getVkYcbcrConversionInfo();
276     if (ycbcrInfo && ycbcrInfo->isValid()) {
277         GrVkGpu* gpu = static_cast<GrVkPipelineStateBuilder*>(fProgramBuilder)->gpu();
278         info.fImmutableSampler = gpu->resourceProvider().findOrCreateCompatibleSampler(
279                 state, *ycbcrInfo);
280         SkASSERT(info.fImmutableSampler);
281     }
282 
283     SkASSERT(shaderCaps->textureSwizzleAppliedInShader());
284     fSamplerSwizzles.push_back(swizzle);
285     SkASSERT(fSamplerSwizzles.count() == fSamplers.count());
286     return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
287 }
288 
appendUniformDecls(GrShaderFlags visibility,SkString * out) const289 void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
290     for (const UniformInfo& sampler : fSamplers.items()) {
291         SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType ||
292                  sampler.fVariable.getType() == kTextureExternalSampler_GrSLType);
293         if (visibility == sampler.fVisibility) {
294             sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
295             out->append(";\n");
296         }
297     }
298 
299 #ifdef SK_DEBUG
300     bool firstOffsetCheck = false;
301     for (const UniformInfo& localUniform : fUniforms.items()) {
302         if (!firstOffsetCheck) {
303             // Check to make sure we are starting our offset at 0 so the offset qualifier we
304             // set on each variable in the uniform block is valid.
305             SkASSERT(0 == localUniform.fUBOffset);
306             firstOffsetCheck = true;
307         }
308     }
309 #endif
310 
311     SkString uniformsString;
312     for (const UniformInfo& localUniform : fUniforms.items()) {
313         if (visibility & localUniform.fVisibility) {
314             if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
315                 localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
316                 uniformsString.append(";\n");
317             }
318         }
319     }
320 
321     if (!uniformsString.isEmpty()) {
322         out->appendf("layout (set=%d, binding=%d) uniform uniformBuffer\n{\n",
323                      kUniformBufferDescSet, kUniformBinding);
324         out->appendf("%s\n};\n", uniformsString.c_str());
325     }
326 }
327 
getRTHeightOffset() const328 uint32_t GrVkUniformHandler::getRTHeightOffset() const {
329     uint32_t currentOffset = fCurrentUBOOffset;
330     return get_ubo_aligned_offset(&currentOffset, kFloat_GrSLType, 0);
331 }
332