blob: f0f0fff7a60440dbcd78e566a5daa1d8759bbaab [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).
Greg Daniel164a9f02016-02-22 09:56:40 -050017uint32_t grsltype_to_alignment_mask(GrSLType type) {
Brian Salomonfa26e662016-11-14 11:27:00 -050018 switch(type) {
19 case kInt_GrSLType:
20 return 0x3;
21 case kUint_GrSLType:
22 return 0x3;
23 case kFloat_GrSLType:
24 return 0x3;
25 case kVec2f_GrSLType:
26 return 0x7;
27 case kVec3f_GrSLType:
28 return 0xF;
29 case kVec4f_GrSLType:
30 return 0xF;
31 case kMat22f_GrSLType:
32 return 0x7;
33 case kMat33f_GrSLType:
34 return 0xF;
35 case kMat44f_GrSLType:
36 return 0xF;
37
38 // This query is only valid for certain types.
39 case kVoid_GrSLType:
40 case kBool_GrSLType:
41 case kTexture2DSampler_GrSLType:
Brian Salomona8f00022016-11-16 12:55:57 -050042 case kITexture2DSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050043 case kTextureExternalSampler_GrSLType:
44 case kTexture2DRectSampler_GrSLType:
csmartdalton22458032016-11-16 11:28:16 -070045 case kBufferSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050046 case kTexture2D_GrSLType:
47 case kSampler_GrSLType:
Brian Salomonf9f45122016-11-29 11:59:17 -050048 case kImageStorage2D_GrSLType:
49 case kIImageStorage2D_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050050 break;
51 }
52 SkFAIL("Unexpected type");
53 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -050054}
55
egdaniel4ee1cda2016-02-26 08:18:49 -080056/** Returns the size in bytes taken up in vulkanbuffers for floating point GrSLTypes.
egdaniel75d2bfc2016-07-07 08:04:08 -070057 For non floating point type returns 0. Currently this reflects the std140 alignment
58 so a mat22 takes up 8 floats. */
egdaniel4ee1cda2016-02-26 08:18:49 -080059static inline uint32_t grsltype_to_vk_size(GrSLType type) {
Brian Salomonfa26e662016-11-14 11:27:00 -050060 switch(type) {
61 case kInt_GrSLType:
62 return 4;
63 case kUint_GrSLType:
64 return 4;
65 case kFloat_GrSLType:
66 return sizeof(float);
67 case kVec2f_GrSLType:
68 return 2 * sizeof(float);
69 case kVec3f_GrSLType:
70 return 3 * sizeof(float);
71 case kVec4f_GrSLType:
72 return 4 * sizeof(float);
73 case kMat22f_GrSLType:
74 //TODO: this will be 4 * szof(float) on std430.
75 return 8 * sizeof(float);
76 case kMat33f_GrSLType:
77 return 12 * sizeof(float);
78 case kMat44f_GrSLType:
79 return 16 * sizeof(float);
egdaniel4ee1cda2016-02-26 08:18:49 -080080
Brian Salomonfa26e662016-11-14 11:27:00 -050081 // This query is only valid for certain types.
82 case kVoid_GrSLType:
83 case kBool_GrSLType:
84 case kTexture2DSampler_GrSLType:
Brian Salomona8f00022016-11-16 12:55:57 -050085 case kITexture2DSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050086 case kTextureExternalSampler_GrSLType:
87 case kTexture2DRectSampler_GrSLType:
csmartdalton22458032016-11-16 11:28:16 -070088 case kBufferSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050089 case kTexture2D_GrSLType:
90 case kSampler_GrSLType:
Brian Salomonf9f45122016-11-29 11:59:17 -050091 case kImageStorage2D_GrSLType:
92 case kIImageStorage2D_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050093 break;
94 }
95 SkFAIL("Unexpected type");
96 return 0;
egdaniel4ee1cda2016-02-26 08:18:49 -080097}
98
99
Greg Daniel164a9f02016-02-22 09:56:40 -0500100// Given the current offset into the ubo, calculate the offset for the uniform we're trying to add
101// taking into consideration all alignment requirements. The uniformOffset is set to the offset for
102// the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
103void get_ubo_aligned_offset(uint32_t* uniformOffset,
104 uint32_t* currentOffset,
105 GrSLType type,
106 int arrayCount) {
107 uint32_t alignmentMask = grsltype_to_alignment_mask(type);
egdaniel4ee1cda2016-02-26 08:18:49 -0800108 // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
egdaniel75d2bfc2016-07-07 08:04:08 -0700109 if (arrayCount || type == kMat22f_GrSLType) {
egdaniel4ee1cda2016-02-26 08:18:49 -0800110 alignmentMask = 0xF;
111 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500112 uint32_t offsetDiff = *currentOffset & alignmentMask;
113 if (offsetDiff != 0) {
114 offsetDiff = alignmentMask - offsetDiff + 1;
115 }
116 *uniformOffset = *currentOffset + offsetDiff;
117 SkASSERT(sizeof(float) == 4);
egdaniel4ee1cda2016-02-26 08:18:49 -0800118 if (arrayCount) {
119 uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type));
120 SkASSERT(0 == (elementSize & 0xF));
121 *currentOffset = *uniformOffset + elementSize * arrayCount;
122 } else {
123 *currentOffset = *uniformOffset + grsltype_to_vk_size(type);
124 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500125}
126
127GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
128 uint32_t visibility,
129 GrSLType type,
130 GrSLPrecision precision,
131 const char* name,
132 bool mangleName,
133 int arrayCount,
134 const char** outName) {
135 SkASSERT(name && strlen(name));
136 SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_GrShaderFlag|kFragment_GrShaderFlag);
137 SkASSERT(0 == (~kVisibilityMask & visibility));
138 SkASSERT(0 != visibility);
139 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
egdaniel09aa1fc2016-04-20 07:09:46 -0700140 GrSLTypeIsFloatType(type);
Greg Daniel164a9f02016-02-22 09:56:40 -0500141
142 UniformInfo& uni = fUniforms.push_back();
143 uni.fVariable.setType(type);
144 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
145 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
146 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
147 // the names will mismatch. I think the correct solution is to have all GPs which need the
148 // uniform view matrix, they should upload the view matrix in their setData along with regular
149 // uniforms.
150 char prefix = 'u';
151 if ('u' == name[0]) {
152 prefix = '\0';
153 }
154 fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
155 uni.fVariable.setArrayCount(arrayCount);
156 // For now asserting the the visibility is either only vertex or only fragment
157 SkASSERT(kVertex_GrShaderFlag == visibility || kFragment_GrShaderFlag == visibility);
158 uni.fVisibility = visibility;
159 uni.fVariable.setPrecision(precision);
egdaniel09aa1fc2016-04-20 07:09:46 -0700160 // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus
161 // we set the modifier to none for all uniforms declared inside the block.
Brian Salomon99938a82016-11-21 13:41:08 -0500162 uni.fVariable.setTypeModifier(GrShaderVar::kNone_TypeModifier);
Greg Daniel164a9f02016-02-22 09:56:40 -0500163
egdaniel09aa1fc2016-04-20 07:09:46 -0700164 uint32_t* currentOffset = kVertex_GrShaderFlag == visibility ? &fCurrentVertexUBOOffset
Brian Salomonf9f45122016-11-29 11:59:17 -0500165 : &fCurrentFragmentUBOOffset;
egdaniel09aa1fc2016-04-20 07:09:46 -0700166 get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500167
Greg Danieldbd44c72017-01-24 15:12:12 -0500168 SkString layoutQualifier;
169 layoutQualifier.appendf("offset=%d", uni.fUBOffset);
170 uni.fVariable.addLayoutQualifier(layoutQualifier.c_str());
171
egdaniel09aa1fc2016-04-20 07:09:46 -0700172 if (outName) {
173 *outName = uni.fVariable.c_str();
Greg Daniel164a9f02016-02-22 09:56:40 -0500174 }
175
176 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
177}
178
Brian Salomon101b8442016-11-18 11:58:54 -0500179GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(uint32_t visibility,
180 GrSwizzle swizzle,
181 GrSLType type,
182 GrSLPrecision precision,
183 const char* name) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700184 SkASSERT(name && strlen(name));
185 SkDEBUGCODE(static const uint32_t kVisMask = kVertex_GrShaderFlag | kFragment_GrShaderFlag);
186 SkASSERT(0 == (~kVisMask & visibility));
187 SkASSERT(0 != visibility);
188 SkString mangleName;
189 char prefix = 'u';
190 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
Brian Salomon101b8442016-11-18 11:58:54 -0500191
192 UniformInfo& info = fSamplers.push_back();
193 SkASSERT(GrSLTypeIsCombinedSamplerType(type));
194 info.fVariable.setType(type);
Brian Salomon99938a82016-11-21 13:41:08 -0500195 info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
Brian Salomon101b8442016-11-18 11:58:54 -0500196 info.fVariable.setPrecision(precision);
197 info.fVariable.setName(mangleName);
198 SkString layoutQualifier;
199 layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count() - 1);
Brian Salomon60397682016-11-22 15:06:46 -0500200 info.fVariable.addLayoutQualifier(layoutQualifier.c_str());
Brian Salomon101b8442016-11-18 11:58:54 -0500201 info.fVisibility = visibility;
202 info.fUBOffset = 0;
203 fSamplerSwizzles.push_back(swizzle);
204 SkASSERT(fSamplerSwizzles.count() == fSamplers.count());
egdaniel09aa1fc2016-04-20 07:09:46 -0700205 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
206}
207
Greg Daniel164a9f02016-02-22 09:56:40 -0500208void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdanielb8002482016-04-19 15:24:29 -0700209 SkASSERT(kVertex_GrShaderFlag == visibility || kFragment_GrShaderFlag == visibility);
egdaniel09aa1fc2016-04-20 07:09:46 -0700210
211 for (int i = 0; i < fSamplers.count(); ++i) {
Brian Salomon101b8442016-11-18 11:58:54 -0500212 const UniformInfo& sampler = fSamplers[i];
213 SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType);
214 if (visibility == sampler.fVisibility) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500215 sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
egdaniel09aa1fc2016-04-20 07:09:46 -0700216 out->append(";\n");
217 }
218 }
219
Greg Danieldbd44c72017-01-24 15:12:12 -0500220 SkDEBUGCODE(bool firstOffsetCheck = false);
egdaniel09aa1fc2016-04-20 07:09:46 -0700221 SkString uniformsString;
Greg Daniel164a9f02016-02-22 09:56:40 -0500222 for (int i = 0; i < fUniforms.count(); ++i) {
223 const UniformInfo& localUniform = fUniforms[i];
224 if (visibility == localUniform.fVisibility) {
225 if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
Greg Danieldbd44c72017-01-24 15:12:12 -0500226#ifdef SK_DEBUG
227 if (!firstOffsetCheck) {
228 // Check to make sure we are starting our offset at 0 so the offset qualifier we
229 // set on each variable in the uniform block is valid.
230 SkASSERT(0 == localUniform.fUBOffset);
231 firstOffsetCheck = true;
232 }
233#endif
Brian Salomon94efbf52016-11-29 13:43:05 -0500234 localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
Greg Daniel164a9f02016-02-22 09:56:40 -0500235 uniformsString.append(";\n");
Greg Daniel164a9f02016-02-22 09:56:40 -0500236 }
237 }
238 }
239 if (!uniformsString.isEmpty()) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700240 uint32_t uniformBinding = (visibility == kVertex_GrShaderFlag) ? kVertexBinding
241 : kFragBinding;
Greg Daniel164a9f02016-02-22 09:56:40 -0500242 const char* stage = (visibility == kVertex_GrShaderFlag) ? "vertex" : "fragment";
243 out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n",
244 kUniformBufferDescSet, uniformBinding, stage);
245 out->appendf("%s\n};\n", uniformsString.c_str());
246 }
egdaniel4ee1cda2016-02-26 08:18:49 -0800247}