blob: d92d8d2fd618b6fd3afd2fbca773fe6bc92b435b [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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).
egdaniel4ee1cda2016-02-26 08:18:49 -080014// 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).
Chris Dalton51ebd662017-10-24 16:10:48 -060017// These are designated in the Vulkan spec, section 14.5.4 "Offset and Stride Assignment".
18// https://www.khronos.org/registry/vulkan/specs/1.0-wsi_extensions/html/vkspec.html#interfaces-resources-layout
Greg Daniel164a9f02016-02-22 09:56:40 -050019uint32_t grsltype_to_alignment_mask(GrSLType type) {
Brian Salomonfa26e662016-11-14 11:27:00 -050020 switch(type) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040021 case kShort_GrSLType: // fall through
Chris Dalton51ebd662017-10-24 16:10:48 -060022 case kUShort_GrSLType:
23 return 0x1;
24 case kShort2_GrSLType: // fall through
25 case kUShort2_GrSLType:
Chris Dalton6dd0d8a2017-10-24 19:53:13 +000026 return 0x3;
Chris Dalton51ebd662017-10-24 16:10:48 -060027 case kShort3_GrSLType: // fall through
28 case kShort4_GrSLType:
29 case kUShort3_GrSLType:
30 case kUShort4_GrSLType:
31 return 0x7;
32 case kInt_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050033 case kUint_GrSLType:
34 return 0x3;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040035 case kHalf_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -040036 case kFloat_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050037 return 0x3;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040038 case kHalf2_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -040039 case kFloat2_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050040 return 0x7;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040041 case kHalf3_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -040042 case kFloat3_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050043 return 0xF;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040044 case kHalf4_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -040045 case kFloat4_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050046 return 0xF;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040047 case kUint2_GrSLType:
Chris Dalton51ebd662017-10-24 16:10:48 -060048 return 0x7;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040049 case kInt2_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -050050 return 0x7;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040051 case kInt3_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -050052 return 0xF;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040053 case kInt4_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -050054 return 0xF;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040055 case kHalf2x2_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -040056 case kFloat2x2_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050057 return 0x7;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040058 case kHalf3x3_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -040059 case kFloat3x3_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050060 return 0xF;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040061 case kHalf4x4_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -040062 case kFloat4x4_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050063 return 0xF;
64
65 // This query is only valid for certain types.
66 case kVoid_GrSLType:
67 case kBool_GrSLType:
68 case kTexture2DSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050069 case kTextureExternalSampler_GrSLType:
70 case kTexture2DRectSampler_GrSLType:
csmartdalton22458032016-11-16 11:28:16 -070071 case kBufferSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050072 case kTexture2D_GrSLType:
73 case kSampler_GrSLType:
74 break;
75 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040076 SK_ABORT("Unexpected type");
Brian Salomonfa26e662016-11-14 11:27:00 -050077 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -050078}
79
Chris Dalton51ebd662017-10-24 16:10:48 -060080/** Returns the size in bytes taken up in vulkanbuffers for GrSLTypes. */
egdaniel4ee1cda2016-02-26 08:18:49 -080081static inline uint32_t grsltype_to_vk_size(GrSLType type) {
Brian Salomonfa26e662016-11-14 11:27:00 -050082 switch(type) {
Chris Dalton51ebd662017-10-24 16:10:48 -060083 case kShort_GrSLType:
84 return sizeof(int16_t);
85 case kShort2_GrSLType:
86 return 2 * sizeof(int16_t);
87 case kShort3_GrSLType:
88 return 3 * sizeof(int16_t);
89 case kShort4_GrSLType:
90 return 4 * sizeof(int16_t);
91 case kUShort_GrSLType:
92 return sizeof(uint16_t);
93 case kUShort2_GrSLType:
94 return 2 * sizeof(uint16_t);
95 case kUShort3_GrSLType:
96 return 3 * sizeof(uint16_t);
97 case kUShort4_GrSLType:
98 return 4 * sizeof(uint16_t);
Brian Salomonfa26e662016-11-14 11:27:00 -050099 case kInt_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500100 return sizeof(int32_t);
Brian Salomonfa26e662016-11-14 11:27:00 -0500101 case kUint_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500102 return sizeof(int32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400103 case kHalf_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400104 case kFloat_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500105 return sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400106 case kHalf2_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400107 case kFloat2_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500108 return 2 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400109 case kHalf3_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400110 case kFloat3_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500111 return 3 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400112 case kHalf4_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400113 case kFloat4_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500114 return 4 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400115 case kUint2_GrSLType:
Chris Dalton51ebd662017-10-24 16:10:48 -0600116 return 2 * sizeof(uint32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400117 case kInt2_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500118 return 2 * sizeof(int32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400119 case kInt3_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500120 return 3 * sizeof(int32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400121 case kInt4_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500122 return 4 * sizeof(int32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400123 case kHalf2x2_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400124 case kFloat2x2_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500125 //TODO: this will be 4 * szof(float) on std430.
126 return 8 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400127 case kHalf3x3_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400128 case kFloat3x3_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500129 return 12 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400130 case kHalf4x4_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400131 case kFloat4x4_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500132 return 16 * sizeof(float);
egdaniel4ee1cda2016-02-26 08:18:49 -0800133
Brian Salomonfa26e662016-11-14 11:27:00 -0500134 // This query is only valid for certain types.
135 case kVoid_GrSLType:
136 case kBool_GrSLType:
137 case kTexture2DSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500138 case kTextureExternalSampler_GrSLType:
139 case kTexture2DRectSampler_GrSLType:
csmartdalton22458032016-11-16 11:28:16 -0700140 case kBufferSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500141 case kTexture2D_GrSLType:
142 case kSampler_GrSLType:
143 break;
144 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400145 SK_ABORT("Unexpected type");
Brian Salomonfa26e662016-11-14 11:27:00 -0500146 return 0;
egdaniel4ee1cda2016-02-26 08:18:49 -0800147}
148
149
Greg Daniel164a9f02016-02-22 09:56:40 -0500150// Given the current offset into the ubo, calculate the offset for the uniform we're trying to add
151// taking into consideration all alignment requirements. The uniformOffset is set to the offset for
152// the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
153void get_ubo_aligned_offset(uint32_t* uniformOffset,
154 uint32_t* currentOffset,
155 GrSLType type,
156 int arrayCount) {
157 uint32_t alignmentMask = grsltype_to_alignment_mask(type);
egdaniel4ee1cda2016-02-26 08:18:49 -0800158 // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400159 if (arrayCount || type == kFloat2x2_GrSLType) {
egdaniel4ee1cda2016-02-26 08:18:49 -0800160 alignmentMask = 0xF;
161 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500162 uint32_t offsetDiff = *currentOffset & alignmentMask;
163 if (offsetDiff != 0) {
164 offsetDiff = alignmentMask - offsetDiff + 1;
165 }
166 *uniformOffset = *currentOffset + offsetDiff;
167 SkASSERT(sizeof(float) == 4);
egdaniel4ee1cda2016-02-26 08:18:49 -0800168 if (arrayCount) {
169 uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type));
170 SkASSERT(0 == (elementSize & 0xF));
171 *currentOffset = *uniformOffset + elementSize * arrayCount;
172 } else {
173 *currentOffset = *uniformOffset + grsltype_to_vk_size(type);
174 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500175}
176
177GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
178 uint32_t visibility,
179 GrSLType type,
180 GrSLPrecision precision,
181 const char* name,
182 bool mangleName,
183 int arrayCount,
184 const char** outName) {
185 SkASSERT(name && strlen(name));
Greg Daniel18f96022017-05-04 15:09:03 -0400186 // For now asserting the the visibility is either geometry types (vertex, tesselation, geometry,
187 // etc.) or only fragment.
188 SkASSERT(kVertex_GrShaderFlag == visibility ||
189 kGeometry_GrShaderFlag == visibility ||
190 (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == visibility ||
191 kFragment_GrShaderFlag == visibility);
Greg Daniel164a9f02016-02-22 09:56:40 -0500192 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
egdaniel09aa1fc2016-04-20 07:09:46 -0700193 GrSLTypeIsFloatType(type);
Greg Daniel164a9f02016-02-22 09:56:40 -0500194
195 UniformInfo& uni = fUniforms.push_back();
196 uni.fVariable.setType(type);
197 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
198 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
199 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
200 // the names will mismatch. I think the correct solution is to have all GPs which need the
201 // uniform view matrix, they should upload the view matrix in their setData along with regular
202 // uniforms.
203 char prefix = 'u';
Robert Phillipsfe8da172018-01-24 14:52:02 +0000204 if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500205 prefix = '\0';
206 }
207 fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
208 uni.fVariable.setArrayCount(arrayCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500209 uni.fVisibility = visibility;
210 uni.fVariable.setPrecision(precision);
egdaniel09aa1fc2016-04-20 07:09:46 -0700211 // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus
212 // we set the modifier to none for all uniforms declared inside the block.
Brian Salomon99938a82016-11-21 13:41:08 -0500213 uni.fVariable.setTypeModifier(GrShaderVar::kNone_TypeModifier);
Greg Daniel164a9f02016-02-22 09:56:40 -0500214
Greg Daniel18f96022017-05-04 15:09:03 -0400215 uint32_t* currentOffset;
216 uint32_t geomStages = kVertex_GrShaderFlag | kGeometry_GrShaderFlag;
217 if (geomStages & visibility) {
218 currentOffset = &fCurrentGeometryUBOOffset;
219 } else {
220 SkASSERT(kFragment_GrShaderFlag == visibility);
221 currentOffset = &fCurrentFragmentUBOOffset;
222 }
egdaniel09aa1fc2016-04-20 07:09:46 -0700223 get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500224
Greg Danieldbd44c72017-01-24 15:12:12 -0500225 SkString layoutQualifier;
226 layoutQualifier.appendf("offset=%d", uni.fUBOffset);
227 uni.fVariable.addLayoutQualifier(layoutQualifier.c_str());
228
egdaniel09aa1fc2016-04-20 07:09:46 -0700229 if (outName) {
230 *outName = uni.fVariable.c_str();
Greg Daniel164a9f02016-02-22 09:56:40 -0500231 }
232
233 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
234}
235
Brian Salomon101b8442016-11-18 11:58:54 -0500236GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(uint32_t visibility,
237 GrSwizzle swizzle,
238 GrSLType type,
239 GrSLPrecision precision,
240 const char* name) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700241 SkASSERT(name && strlen(name));
Greg Daniel18f96022017-05-04 15:09:03 -0400242 // For now asserting the the visibility is either only vertex, geometry, or fragment
243 SkASSERT(kVertex_GrShaderFlag == visibility ||
244 kFragment_GrShaderFlag == visibility ||
245 kGeometry_GrShaderFlag == visibility);
egdaniel09aa1fc2016-04-20 07:09:46 -0700246 SkString mangleName;
247 char prefix = 'u';
248 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
Brian Salomon101b8442016-11-18 11:58:54 -0500249
250 UniformInfo& info = fSamplers.push_back();
251 SkASSERT(GrSLTypeIsCombinedSamplerType(type));
252 info.fVariable.setType(type);
Brian Salomon99938a82016-11-21 13:41:08 -0500253 info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
Brian Salomon101b8442016-11-18 11:58:54 -0500254 info.fVariable.setPrecision(precision);
255 info.fVariable.setName(mangleName);
256 SkString layoutQualifier;
257 layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count() - 1);
Brian Salomon60397682016-11-22 15:06:46 -0500258 info.fVariable.addLayoutQualifier(layoutQualifier.c_str());
Brian Salomon101b8442016-11-18 11:58:54 -0500259 info.fVisibility = visibility;
260 info.fUBOffset = 0;
261 fSamplerSwizzles.push_back(swizzle);
262 SkASSERT(fSamplerSwizzles.count() == fSamplers.count());
egdaniel09aa1fc2016-04-20 07:09:46 -0700263 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
264}
265
Greg Daniel164a9f02016-02-22 09:56:40 -0500266void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
Greg Daniel18f96022017-05-04 15:09:03 -0400267 SkASSERT(kVertex_GrShaderFlag == visibility ||
268 kGeometry_GrShaderFlag == visibility ||
269 kFragment_GrShaderFlag == visibility);
egdaniel09aa1fc2016-04-20 07:09:46 -0700270
271 for (int i = 0; i < fSamplers.count(); ++i) {
Brian Salomon101b8442016-11-18 11:58:54 -0500272 const UniformInfo& sampler = fSamplers[i];
273 SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType);
274 if (visibility == sampler.fVisibility) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500275 sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
egdaniel09aa1fc2016-04-20 07:09:46 -0700276 out->append(";\n");
277 }
278 }
279
Greg Danielaa352de2017-07-17 09:05:16 -0400280#ifdef SK_DEBUG
281 bool firstGeomOffsetCheck = false;
282 bool firstFragOffsetCheck = false;
283 for (int i = 0; i < fUniforms.count(); ++i) {
284 const UniformInfo& localUniform = fUniforms[i];
285 if (kVertex_GrShaderFlag == localUniform.fVisibility ||
286 kGeometry_GrShaderFlag == localUniform.fVisibility ||
287 (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == localUniform.fVisibility) {
288 if (!firstGeomOffsetCheck) {
289 // Check to make sure we are starting our offset at 0 so the offset qualifier we
290 // set on each variable in the uniform block is valid.
291 SkASSERT(0 == localUniform.fUBOffset);
292 firstGeomOffsetCheck = true;
293 }
294 } else {
295 SkASSERT(kFragment_GrShaderFlag == localUniform.fVisibility);
296 if (!firstFragOffsetCheck) {
297 // Check to make sure we are starting our offset at 0 so the offset qualifier we
298 // set on each variable in the uniform block is valid.
299 SkASSERT(0 == localUniform.fUBOffset);
300 firstFragOffsetCheck = true;
301 }
302 }
303 }
304#endif
305
egdaniel09aa1fc2016-04-20 07:09:46 -0700306 SkString uniformsString;
Greg Daniel164a9f02016-02-22 09:56:40 -0500307 for (int i = 0; i < fUniforms.count(); ++i) {
308 const UniformInfo& localUniform = fUniforms[i];
Greg Daniel18f96022017-05-04 15:09:03 -0400309 if (visibility & localUniform.fVisibility) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500310 if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500311 localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
Greg Daniel164a9f02016-02-22 09:56:40 -0500312 uniformsString.append(";\n");
Greg Daniel164a9f02016-02-22 09:56:40 -0500313 }
314 }
315 }
Greg Danielaa352de2017-07-17 09:05:16 -0400316
Greg Daniel164a9f02016-02-22 09:56:40 -0500317 if (!uniformsString.isEmpty()) {
Greg Daniel18f96022017-05-04 15:09:03 -0400318 uint32_t uniformBinding;
319 const char* stage;
320 if (kVertex_GrShaderFlag == visibility) {
321 uniformBinding = kGeometryBinding;
322 stage = "vertex";
323 } else if (kGeometry_GrShaderFlag == visibility) {
324 uniformBinding = kGeometryBinding;
325 stage = "geometry";
326 } else {
327 SkASSERT(kFragment_GrShaderFlag == visibility);
328 uniformBinding = kFragBinding;
329 stage = "fragment";
330 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500331 out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n",
332 kUniformBufferDescSet, uniformBinding, stage);
333 out->appendf("%s\n};\n", uniformsString.c_str());
334 }
egdaniel4ee1cda2016-02-26 08:18:49 -0800335}