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 * uniformOffset,uint32_t * currentOffset,GrSLType type,int arrayCount)181 static void get_ubo_aligned_offset(uint32_t* uniformOffset,
182 uint32_t* currentOffset,
183 GrSLType type,
184 int arrayCount) {
185 uint32_t alignmentMask = grsltype_to_alignment_mask(type);
186 // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
187 if (arrayCount || type == kFloat2x2_GrSLType) {
188 alignmentMask = 0xF;
189 }
190 uint32_t offsetDiff = *currentOffset & alignmentMask;
191 if (offsetDiff != 0) {
192 offsetDiff = alignmentMask - offsetDiff + 1;
193 }
194 *uniformOffset = *currentOffset + offsetDiff;
195 SkASSERT(sizeof(float) == 4);
196 if (arrayCount) {
197 uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type));
198 SkASSERT(0 == (elementSize & 0xF));
199 *currentOffset = *uniformOffset + elementSize * arrayCount;
200 } else {
201 *currentOffset = *uniformOffset + grsltype_to_vk_size(type);
202 }
203 }
204
~GrVkUniformHandler()205 GrVkUniformHandler::~GrVkUniformHandler() {
206 GrVkGpu* gpu = static_cast<GrVkPipelineStateBuilder*>(fProgramBuilder)->gpu();
207 for (decltype(fSamplers)::Iter iter(&fSamplers); iter.next();) {
208 if (iter->fImmutableSampler) {
209 iter->fImmutableSampler->unref(gpu);
210 iter->fImmutableSampler = nullptr;
211 }
212 }
213 }
214
internalAddUniformArray(uint32_t visibility,GrSLType type,const char * name,bool mangleName,int arrayCount,const char ** outName)215 GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
216 uint32_t visibility,
217 GrSLType type,
218 const char* name,
219 bool mangleName,
220 int arrayCount,
221 const char** outName) {
222 SkASSERT(name && strlen(name));
223 SkASSERT(GrSLTypeIsFloatType(type));
224
225 UniformInfo& uni = fUniforms.push_back();
226 uni.fVariable.setType(type);
227 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
228 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
229 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
230 // the names will mismatch. I think the correct solution is to have all GPs which need the
231 // uniform view matrix, they should upload the view matrix in their setData along with regular
232 // uniforms.
233 char prefix = 'u';
234 if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
235 prefix = '\0';
236 }
237 fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
238 uni.fVariable.setArrayCount(arrayCount);
239 uni.fVisibility = visibility;
240 // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus
241 // we set the modifier to none for all uniforms declared inside the block.
242 uni.fVariable.setTypeModifier(GrShaderVar::kNone_TypeModifier);
243
244 get_ubo_aligned_offset(&uni.fUBOffset, &fCurrentUBOOffset, type, arrayCount);
245
246 SkString layoutQualifier;
247 layoutQualifier.appendf("offset=%d", uni.fUBOffset);
248 uni.fVariable.addLayoutQualifier(layoutQualifier.c_str());
249
250 if (outName) {
251 *outName = uni.fVariable.c_str();
252 }
253
254 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
255 }
256
addSampler(const GrTextureProxy * texture,const GrSamplerState & state,const GrSwizzle & swizzle,const char * name,const GrShaderCaps * shaderCaps)257 GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(const GrTextureProxy* texture,
258 const GrSamplerState& state,
259 const GrSwizzle& swizzle,
260 const char* name,
261 const GrShaderCaps* shaderCaps) {
262 SkASSERT(name && strlen(name));
263 SkString mangleName;
264 char prefix = 'u';
265 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
266
267 GrTextureType type = texture->textureType();
268
269 UniformInfo& info = fSamplers.push_back();
270 info.fVariable.setType(GrSLCombinedSamplerTypeForTextureType(type));
271 info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
272 info.fVariable.setName(mangleName);
273 SkString layoutQualifier;
274 layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count() - 1);
275 info.fVariable.addLayoutQualifier(layoutQualifier.c_str());
276 info.fVisibility = kFragment_GrShaderFlag;
277 info.fUBOffset = 0;
278
279 // Check if we are dealing with an external texture and store the needed information if so.
280 auto ycbcrInfo = texture->backendFormat().getVkYcbcrConversionInfo();
281 if (ycbcrInfo && ycbcrInfo->isValid()) {
282 GrVkGpu* gpu = static_cast<GrVkPipelineStateBuilder*>(fProgramBuilder)->gpu();
283 info.fImmutableSampler = gpu->resourceProvider().findOrCreateCompatibleSampler(
284 state, *ycbcrInfo);
285 SkASSERT(info.fImmutableSampler);
286 }
287
288 SkASSERT(shaderCaps->textureSwizzleAppliedInShader());
289 fSamplerSwizzles.push_back(swizzle);
290 SkASSERT(fSamplerSwizzles.count() == fSamplers.count());
291 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
292 }
293
appendUniformDecls(GrShaderFlags visibility,SkString * out) const294 void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
295 for (int i = 0; i < fSamplers.count(); ++i) {
296 const UniformInfo& sampler = fSamplers[i];
297 SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType ||
298 sampler.fVariable.getType() == kTextureExternalSampler_GrSLType);
299 if (visibility == sampler.fVisibility) {
300 sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
301 out->append(";\n");
302 }
303 }
304
305 #ifdef SK_DEBUG
306 bool firstOffsetCheck = false;
307 for (int i = 0; i < fUniforms.count(); ++i) {
308 const UniformInfo& localUniform = fUniforms[i];
309 if (!firstOffsetCheck) {
310 // Check to make sure we are starting our offset at 0 so the offset qualifier we
311 // set on each variable in the uniform block is valid.
312 SkASSERT(0 == localUniform.fUBOffset);
313 firstOffsetCheck = true;
314 }
315 }
316 #endif
317
318 SkString uniformsString;
319 for (int i = 0; i < fUniforms.count(); ++i) {
320 const UniformInfo& localUniform = fUniforms[i];
321 if (visibility & localUniform.fVisibility) {
322 if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
323 localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
324 uniformsString.append(";\n");
325 }
326 }
327 }
328
329 if (!uniformsString.isEmpty()) {
330 out->appendf("layout (set=%d, binding=%d) uniform uniformBuffer\n{\n",
331 kUniformBufferDescSet, kUniformBinding);
332 out->appendf("%s\n};\n", uniformsString.c_str());
333 }
334 }
335
getRTHeightOffset() const336 uint32_t GrVkUniformHandler::getRTHeightOffset() const {
337 uint32_t result;
338 uint32_t currentOffset = fCurrentUBOOffset;
339 get_ubo_aligned_offset(&result, ¤tOffset, kFloat_GrSLType, 0);
340 return result;
341 }
342