blob: f69c9d16f9e5ce46473a1cb999732839731d5562 [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;
csmartdaltonb37cb232017-02-08 14:56:27 -050031 case kVec2i_GrSLType:
32 return 0x7;
33 case kVec3i_GrSLType:
34 return 0xF;
35 case kVec4i_GrSLType:
36 return 0xF;
Brian Salomonfa26e662016-11-14 11:27:00 -050037 case kMat22f_GrSLType:
38 return 0x7;
39 case kMat33f_GrSLType:
40 return 0xF;
41 case kMat44f_GrSLType:
42 return 0xF;
43
44 // This query is only valid for certain types.
45 case kVoid_GrSLType:
46 case kBool_GrSLType:
47 case kTexture2DSampler_GrSLType:
Brian Salomona8f00022016-11-16 12:55:57 -050048 case kITexture2DSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050049 case kTextureExternalSampler_GrSLType:
50 case kTexture2DRectSampler_GrSLType:
csmartdalton22458032016-11-16 11:28:16 -070051 case kBufferSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050052 case kTexture2D_GrSLType:
53 case kSampler_GrSLType:
Brian Salomonf9f45122016-11-29 11:59:17 -050054 case kImageStorage2D_GrSLType:
55 case kIImageStorage2D_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050056 break;
57 }
58 SkFAIL("Unexpected type");
59 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -050060}
61
egdaniel4ee1cda2016-02-26 08:18:49 -080062/** Returns the size in bytes taken up in vulkanbuffers for floating point GrSLTypes.
egdaniel75d2bfc2016-07-07 08:04:08 -070063 For non floating point type returns 0. Currently this reflects the std140 alignment
64 so a mat22 takes up 8 floats. */
egdaniel4ee1cda2016-02-26 08:18:49 -080065static inline uint32_t grsltype_to_vk_size(GrSLType type) {
Brian Salomonfa26e662016-11-14 11:27:00 -050066 switch(type) {
67 case kInt_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -050068 return sizeof(int32_t);
Brian Salomonfa26e662016-11-14 11:27:00 -050069 case kUint_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -050070 return sizeof(int32_t);
Brian Salomonfa26e662016-11-14 11:27:00 -050071 case kFloat_GrSLType:
72 return sizeof(float);
73 case kVec2f_GrSLType:
74 return 2 * sizeof(float);
75 case kVec3f_GrSLType:
76 return 3 * sizeof(float);
77 case kVec4f_GrSLType:
78 return 4 * sizeof(float);
csmartdaltonb37cb232017-02-08 14:56:27 -050079 case kVec2i_GrSLType:
80 return 2 * sizeof(int32_t);
81 case kVec3i_GrSLType:
82 return 3 * sizeof(int32_t);
83 case kVec4i_GrSLType:
84 return 4 * sizeof(int32_t);
Brian Salomonfa26e662016-11-14 11:27:00 -050085 case kMat22f_GrSLType:
86 //TODO: this will be 4 * szof(float) on std430.
87 return 8 * sizeof(float);
88 case kMat33f_GrSLType:
89 return 12 * sizeof(float);
90 case kMat44f_GrSLType:
91 return 16 * sizeof(float);
egdaniel4ee1cda2016-02-26 08:18:49 -080092
Brian Salomonfa26e662016-11-14 11:27:00 -050093 // This query is only valid for certain types.
94 case kVoid_GrSLType:
95 case kBool_GrSLType:
96 case kTexture2DSampler_GrSLType:
Brian Salomona8f00022016-11-16 12:55:57 -050097 case kITexture2DSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050098 case kTextureExternalSampler_GrSLType:
99 case kTexture2DRectSampler_GrSLType:
csmartdalton22458032016-11-16 11:28:16 -0700100 case kBufferSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500101 case kTexture2D_GrSLType:
102 case kSampler_GrSLType:
Brian Salomonf9f45122016-11-29 11:59:17 -0500103 case kImageStorage2D_GrSLType:
104 case kIImageStorage2D_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500105 break;
106 }
107 SkFAIL("Unexpected type");
108 return 0;
egdaniel4ee1cda2016-02-26 08:18:49 -0800109}
110
111
Greg Daniel164a9f02016-02-22 09:56:40 -0500112// Given the current offset into the ubo, calculate the offset for the uniform we're trying to add
113// taking into consideration all alignment requirements. The uniformOffset is set to the offset for
114// the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
115void get_ubo_aligned_offset(uint32_t* uniformOffset,
116 uint32_t* currentOffset,
117 GrSLType type,
118 int arrayCount) {
119 uint32_t alignmentMask = grsltype_to_alignment_mask(type);
egdaniel4ee1cda2016-02-26 08:18:49 -0800120 // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
egdaniel75d2bfc2016-07-07 08:04:08 -0700121 if (arrayCount || type == kMat22f_GrSLType) {
egdaniel4ee1cda2016-02-26 08:18:49 -0800122 alignmentMask = 0xF;
123 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500124 uint32_t offsetDiff = *currentOffset & alignmentMask;
125 if (offsetDiff != 0) {
126 offsetDiff = alignmentMask - offsetDiff + 1;
127 }
128 *uniformOffset = *currentOffset + offsetDiff;
129 SkASSERT(sizeof(float) == 4);
egdaniel4ee1cda2016-02-26 08:18:49 -0800130 if (arrayCount) {
131 uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type));
132 SkASSERT(0 == (elementSize & 0xF));
133 *currentOffset = *uniformOffset + elementSize * arrayCount;
134 } else {
135 *currentOffset = *uniformOffset + grsltype_to_vk_size(type);
136 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500137}
138
139GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
140 uint32_t visibility,
141 GrSLType type,
142 GrSLPrecision precision,
143 const char* name,
144 bool mangleName,
145 int arrayCount,
146 const char** outName) {
147 SkASSERT(name && strlen(name));
148 SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_GrShaderFlag|kFragment_GrShaderFlag);
149 SkASSERT(0 == (~kVisibilityMask & visibility));
150 SkASSERT(0 != visibility);
151 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
egdaniel09aa1fc2016-04-20 07:09:46 -0700152 GrSLTypeIsFloatType(type);
Greg Daniel164a9f02016-02-22 09:56:40 -0500153
154 UniformInfo& uni = fUniforms.push_back();
155 uni.fVariable.setType(type);
156 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
157 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
158 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
159 // the names will mismatch. I think the correct solution is to have all GPs which need the
160 // uniform view matrix, they should upload the view matrix in their setData along with regular
161 // uniforms.
162 char prefix = 'u';
163 if ('u' == name[0]) {
164 prefix = '\0';
165 }
166 fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
167 uni.fVariable.setArrayCount(arrayCount);
168 // For now asserting the the visibility is either only vertex or only fragment
169 SkASSERT(kVertex_GrShaderFlag == visibility || kFragment_GrShaderFlag == visibility);
170 uni.fVisibility = visibility;
171 uni.fVariable.setPrecision(precision);
egdaniel09aa1fc2016-04-20 07:09:46 -0700172 // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus
173 // we set the modifier to none for all uniforms declared inside the block.
Brian Salomon99938a82016-11-21 13:41:08 -0500174 uni.fVariable.setTypeModifier(GrShaderVar::kNone_TypeModifier);
Greg Daniel164a9f02016-02-22 09:56:40 -0500175
egdaniel09aa1fc2016-04-20 07:09:46 -0700176 uint32_t* currentOffset = kVertex_GrShaderFlag == visibility ? &fCurrentVertexUBOOffset
Brian Salomonf9f45122016-11-29 11:59:17 -0500177 : &fCurrentFragmentUBOOffset;
egdaniel09aa1fc2016-04-20 07:09:46 -0700178 get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500179
Greg Danieldbd44c72017-01-24 15:12:12 -0500180 SkString layoutQualifier;
181 layoutQualifier.appendf("offset=%d", uni.fUBOffset);
182 uni.fVariable.addLayoutQualifier(layoutQualifier.c_str());
183
egdaniel09aa1fc2016-04-20 07:09:46 -0700184 if (outName) {
185 *outName = uni.fVariable.c_str();
Greg Daniel164a9f02016-02-22 09:56:40 -0500186 }
187
188 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
189}
190
Brian Salomon101b8442016-11-18 11:58:54 -0500191GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(uint32_t visibility,
192 GrSwizzle swizzle,
193 GrSLType type,
194 GrSLPrecision precision,
195 const char* name) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700196 SkASSERT(name && strlen(name));
197 SkDEBUGCODE(static const uint32_t kVisMask = kVertex_GrShaderFlag | kFragment_GrShaderFlag);
198 SkASSERT(0 == (~kVisMask & visibility));
199 SkASSERT(0 != visibility);
200 SkString mangleName;
201 char prefix = 'u';
202 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
Brian Salomon101b8442016-11-18 11:58:54 -0500203
204 UniformInfo& info = fSamplers.push_back();
205 SkASSERT(GrSLTypeIsCombinedSamplerType(type));
206 info.fVariable.setType(type);
Brian Salomon99938a82016-11-21 13:41:08 -0500207 info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
Brian Salomon101b8442016-11-18 11:58:54 -0500208 info.fVariable.setPrecision(precision);
209 info.fVariable.setName(mangleName);
210 SkString layoutQualifier;
211 layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count() - 1);
Brian Salomon60397682016-11-22 15:06:46 -0500212 info.fVariable.addLayoutQualifier(layoutQualifier.c_str());
Brian Salomon101b8442016-11-18 11:58:54 -0500213 info.fVisibility = visibility;
214 info.fUBOffset = 0;
215 fSamplerSwizzles.push_back(swizzle);
216 SkASSERT(fSamplerSwizzles.count() == fSamplers.count());
egdaniel09aa1fc2016-04-20 07:09:46 -0700217 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
218}
219
Greg Daniel164a9f02016-02-22 09:56:40 -0500220void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdanielb8002482016-04-19 15:24:29 -0700221 SkASSERT(kVertex_GrShaderFlag == visibility || kFragment_GrShaderFlag == visibility);
egdaniel09aa1fc2016-04-20 07:09:46 -0700222
223 for (int i = 0; i < fSamplers.count(); ++i) {
Brian Salomon101b8442016-11-18 11:58:54 -0500224 const UniformInfo& sampler = fSamplers[i];
225 SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType);
226 if (visibility == sampler.fVisibility) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500227 sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
egdaniel09aa1fc2016-04-20 07:09:46 -0700228 out->append(";\n");
229 }
230 }
231
Greg Danieldbd44c72017-01-24 15:12:12 -0500232 SkDEBUGCODE(bool firstOffsetCheck = false);
egdaniel09aa1fc2016-04-20 07:09:46 -0700233 SkString uniformsString;
Greg Daniel164a9f02016-02-22 09:56:40 -0500234 for (int i = 0; i < fUniforms.count(); ++i) {
235 const UniformInfo& localUniform = fUniforms[i];
236 if (visibility == localUniform.fVisibility) {
237 if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
Greg Danieldbd44c72017-01-24 15:12:12 -0500238#ifdef SK_DEBUG
239 if (!firstOffsetCheck) {
240 // Check to make sure we are starting our offset at 0 so the offset qualifier we
241 // set on each variable in the uniform block is valid.
242 SkASSERT(0 == localUniform.fUBOffset);
243 firstOffsetCheck = true;
244 }
245#endif
Brian Salomon94efbf52016-11-29 13:43:05 -0500246 localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
Greg Daniel164a9f02016-02-22 09:56:40 -0500247 uniformsString.append(";\n");
Greg Daniel164a9f02016-02-22 09:56:40 -0500248 }
249 }
250 }
251 if (!uniformsString.isEmpty()) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700252 uint32_t uniformBinding = (visibility == kVertex_GrShaderFlag) ? kVertexBinding
253 : kFragBinding;
Greg Daniel164a9f02016-02-22 09:56:40 -0500254 const char* stage = (visibility == kVertex_GrShaderFlag) ? "vertex" : "fragment";
255 out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n",
256 kUniformBufferDescSet, uniformBinding, stage);
257 out->appendf("%s\n};\n", uniformsString.c_str());
258 }
egdaniel4ee1cda2016-02-26 08:18:49 -0800259}