blob: 7c11b9dc4682118d90494cc9f3c4f33a12bf6ef3 [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) {
18 SkASSERT(GrSLTypeIsFloatType(type));
bsalomon134af6b2016-04-12 07:46:21 -070019 static const uint32_t kAlignmentMask[] = {
Greg Daniel164a9f02016-02-22 09:56:40 -050020 0x0, // kVoid_GrSLType, should never return this
21 0x3, // kFloat_GrSLType
22 0x7, // kVec2f_GrSLType
23 0xF, // kVec3f_GrSLType
24 0xF, // kVec4f_GrSLType
cdalton8d988b32016-03-07 15:39:09 -080025 0x7, // kMat22f_GrSLType
Greg Daniel164a9f02016-02-22 09:56:40 -050026 0xF, // kMat33f_GrSLType
27 0xF, // kMat44f_GrSLType
Brian Salomonbf7b6202016-11-11 16:08:03 -050028 0x0, // kTexture2DSampler_GrSLType, should never return this
29 0x0, // kTexture2DISampler_GrSLType, should never return this
30 0x0, // kTextureExternalSampler_GrSLType, should never return this
31 0x0, // kTexture2DSamplerRect_GrSLType, should never return this
32 0x0, // ktextureBufferSampler_GrSLType, should never return this
bsalomon134af6b2016-04-12 07:46:21 -070033 0x0, // kBool_GrSLType
34 0x7, // kInt_GrSLType
35 0x7, // kUint_GrSLType
egdaniel990dbc82016-07-13 14:09:30 -070036 0x0, // Texture2D_GrSLType, should never return this
37 0x0, // Sampler_GrSLType, should never return this
Greg Daniel164a9f02016-02-22 09:56:40 -050038 };
39 GR_STATIC_ASSERT(0 == kVoid_GrSLType);
40 GR_STATIC_ASSERT(1 == kFloat_GrSLType);
41 GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
42 GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
43 GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
cdalton8d988b32016-03-07 15:39:09 -080044 GR_STATIC_ASSERT(5 == kMat22f_GrSLType);
45 GR_STATIC_ASSERT(6 == kMat33f_GrSLType);
46 GR_STATIC_ASSERT(7 == kMat44f_GrSLType);
egdaniel990dbc82016-07-13 14:09:30 -070047 GR_STATIC_ASSERT(8 == kTexture2DSampler_GrSLType);
Brian Salomonbf7b6202016-11-11 16:08:03 -050048 GR_STATIC_ASSERT(9 == kTexture2DISampler_GrSLType);
49 GR_STATIC_ASSERT(10 == kTextureExternalSampler_GrSLType);
50 GR_STATIC_ASSERT(11 == kTexture2DRectSampler_GrSLType);
51 GR_STATIC_ASSERT(12 == kTextureBufferSampler_GrSLType);
52 GR_STATIC_ASSERT(13 == kBool_GrSLType);
53 GR_STATIC_ASSERT(14 == kInt_GrSLType);
54 GR_STATIC_ASSERT(15 == kUint_GrSLType);
55 GR_STATIC_ASSERT(16 == kTexture2D_GrSLType);
56 GR_STATIC_ASSERT(17 == kSampler_GrSLType);
bsalomon134af6b2016-04-12 07:46:21 -070057 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kAlignmentMask) == kGrSLTypeCount);
58 return kAlignmentMask[type];
Greg Daniel164a9f02016-02-22 09:56:40 -050059}
60
egdaniel4ee1cda2016-02-26 08:18:49 -080061/** Returns the size in bytes taken up in vulkanbuffers for floating point GrSLTypes.
egdaniel75d2bfc2016-07-07 08:04:08 -070062 For non floating point type returns 0. Currently this reflects the std140 alignment
63 so a mat22 takes up 8 floats. */
egdaniel4ee1cda2016-02-26 08:18:49 -080064static inline uint32_t grsltype_to_vk_size(GrSLType type) {
65 SkASSERT(GrSLTypeIsFloatType(type));
66 static const uint32_t kSizes[] = {
67 0, // kVoid_GrSLType
68 sizeof(float), // kFloat_GrSLType
69 2 * sizeof(float), // kVec2f_GrSLType
70 3 * sizeof(float), // kVec3f_GrSLType
71 4 * sizeof(float), // kVec4f_GrSLType
cdalton8d988b32016-03-07 15:39:09 -080072 8 * sizeof(float), // kMat22f_GrSLType. TODO: this will be 4 * szof(float) on std430.
egdaniel4ee1cda2016-02-26 08:18:49 -080073 12 * sizeof(float), // kMat33f_GrSLType
74 16 * sizeof(float), // kMat44f_GrSLType
egdaniel990dbc82016-07-13 14:09:30 -070075 0, // kTexture2DSampler_GrSLType
Brian Salomonbf7b6202016-11-11 16:08:03 -050076 0, // kTexture2DISampler_GrSLType
egdaniel990dbc82016-07-13 14:09:30 -070077 0, // kTextureExternalSampler_GrSLType
78 0, // kTexture2DRectSampler_GrSLType
79 0, // kTextureBufferSampler_GrSLType
bsalomon134af6b2016-04-12 07:46:21 -070080 1, // kBool_GrSLType
81 4, // kInt_GrSLType
egdaniel990dbc82016-07-13 14:09:30 -070082 4, // kUint_GrSLType
83 0, // kTexture2D_GrSLType
84 0, // kSampler_GrSLType
egdaniel4ee1cda2016-02-26 08:18:49 -080085 };
86 return kSizes[type];
87
88 GR_STATIC_ASSERT(0 == kVoid_GrSLType);
89 GR_STATIC_ASSERT(1 == kFloat_GrSLType);
90 GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
91 GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
92 GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
cdalton8d988b32016-03-07 15:39:09 -080093 GR_STATIC_ASSERT(5 == kMat22f_GrSLType);
94 GR_STATIC_ASSERT(6 == kMat33f_GrSLType);
95 GR_STATIC_ASSERT(7 == kMat44f_GrSLType);
egdaniel990dbc82016-07-13 14:09:30 -070096 GR_STATIC_ASSERT(8 == kTexture2DSampler_GrSLType);
Brian Salomonbf7b6202016-11-11 16:08:03 -050097 GR_STATIC_ASSERT(9 == kTexture2DISampler_GrSLType);
98 GR_STATIC_ASSERT(10 == kTextureExternalSampler_GrSLType);
99 GR_STATIC_ASSERT(11 == kTexture2DRectSampler_GrSLType);
100 GR_STATIC_ASSERT(12 == kTextureBufferSampler_GrSLType);
101 GR_STATIC_ASSERT(13 == kBool_GrSLType);
102 GR_STATIC_ASSERT(14 == kInt_GrSLType);
103 GR_STATIC_ASSERT(15 == kUint_GrSLType);
104 GR_STATIC_ASSERT(16 == kTexture2D_GrSLType);
105 GR_STATIC_ASSERT(17 == kSampler_GrSLType);
cdalton8d988b32016-03-07 15:39:09 -0800106 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kSizes) == kGrSLTypeCount);
egdaniel4ee1cda2016-02-26 08:18:49 -0800107}
108
109
Greg Daniel164a9f02016-02-22 09:56:40 -0500110// Given the current offset into the ubo, calculate the offset for the uniform we're trying to add
111// taking into consideration all alignment requirements. The uniformOffset is set to the offset for
112// the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
113void get_ubo_aligned_offset(uint32_t* uniformOffset,
114 uint32_t* currentOffset,
115 GrSLType type,
116 int arrayCount) {
117 uint32_t alignmentMask = grsltype_to_alignment_mask(type);
egdaniel4ee1cda2016-02-26 08:18:49 -0800118 // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
egdaniel75d2bfc2016-07-07 08:04:08 -0700119 if (arrayCount || type == kMat22f_GrSLType) {
egdaniel4ee1cda2016-02-26 08:18:49 -0800120 alignmentMask = 0xF;
121 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500122 uint32_t offsetDiff = *currentOffset & alignmentMask;
123 if (offsetDiff != 0) {
124 offsetDiff = alignmentMask - offsetDiff + 1;
125 }
126 *uniformOffset = *currentOffset + offsetDiff;
127 SkASSERT(sizeof(float) == 4);
egdaniel4ee1cda2016-02-26 08:18:49 -0800128 if (arrayCount) {
129 uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type));
130 SkASSERT(0 == (elementSize & 0xF));
131 *currentOffset = *uniformOffset + elementSize * arrayCount;
132 } else {
133 *currentOffset = *uniformOffset + grsltype_to_vk_size(type);
134 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500135}
136
137GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
138 uint32_t visibility,
139 GrSLType type,
140 GrSLPrecision precision,
141 const char* name,
142 bool mangleName,
143 int arrayCount,
144 const char** outName) {
145 SkASSERT(name && strlen(name));
146 SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_GrShaderFlag|kFragment_GrShaderFlag);
147 SkASSERT(0 == (~kVisibilityMask & visibility));
148 SkASSERT(0 != visibility);
149 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
egdaniel09aa1fc2016-04-20 07:09:46 -0700150 GrSLTypeIsFloatType(type);
Greg Daniel164a9f02016-02-22 09:56:40 -0500151
152 UniformInfo& uni = fUniforms.push_back();
153 uni.fVariable.setType(type);
154 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
155 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
156 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
157 // the names will mismatch. I think the correct solution is to have all GPs which need the
158 // uniform view matrix, they should upload the view matrix in their setData along with regular
159 // uniforms.
160 char prefix = 'u';
161 if ('u' == name[0]) {
162 prefix = '\0';
163 }
164 fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
165 uni.fVariable.setArrayCount(arrayCount);
166 // For now asserting the the visibility is either only vertex or only fragment
167 SkASSERT(kVertex_GrShaderFlag == visibility || kFragment_GrShaderFlag == visibility);
168 uni.fVisibility = visibility;
169 uni.fVariable.setPrecision(precision);
egdaniel09aa1fc2016-04-20 07:09:46 -0700170 // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus
171 // we set the modifier to none for all uniforms declared inside the block.
172 uni.fVariable.setTypeModifier(GrGLSLShaderVar::kNone_TypeModifier);
Greg Daniel164a9f02016-02-22 09:56:40 -0500173
egdaniel09aa1fc2016-04-20 07:09:46 -0700174 uint32_t* currentOffset = kVertex_GrShaderFlag == visibility ? &fCurrentVertexUBOOffset
175 : &fCurrentFragmentUBOOffset;
176 get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500177
egdaniel09aa1fc2016-04-20 07:09:46 -0700178 if (outName) {
179 *outName = uni.fVariable.c_str();
Greg Daniel164a9f02016-02-22 09:56:40 -0500180 }
181
182 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
183}
184
egdaniel09aa1fc2016-04-20 07:09:46 -0700185GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::internalAddSampler(uint32_t visibility,
186 GrPixelConfig config,
187 GrSLType type,
188 GrSLPrecision precision,
189 const char* name) {
190 SkASSERT(name && strlen(name));
191 SkDEBUGCODE(static const uint32_t kVisMask = kVertex_GrShaderFlag | kFragment_GrShaderFlag);
192 SkASSERT(0 == (~kVisMask & visibility));
193 SkASSERT(0 != visibility);
194 SkString mangleName;
195 char prefix = 'u';
196 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
197 fSamplers.emplace_back(visibility, config, type, precision, mangleName.c_str(),
198 (uint32_t)fSamplers.count(), kSamplerDescSet);
199 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
200}
201
Greg Daniel164a9f02016-02-22 09:56:40 -0500202void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdanielb8002482016-04-19 15:24:29 -0700203 SkASSERT(kVertex_GrShaderFlag == visibility || kFragment_GrShaderFlag == visibility);
egdaniel09aa1fc2016-04-20 07:09:46 -0700204
205 for (int i = 0; i < fSamplers.count(); ++i) {
206 const GrVkGLSLSampler& sampler = fSamplers[i];
egdaniel990dbc82016-07-13 14:09:30 -0700207 SkASSERT(sampler.type() == kTexture2DSampler_GrSLType);
egdaniel09aa1fc2016-04-20 07:09:46 -0700208 if (visibility == sampler.visibility()) {
209 sampler.fShaderVar.appendDecl(fProgramBuilder->glslCaps(), out);
210 out->append(";\n");
211 }
212 }
213
214 SkString uniformsString;
Greg Daniel164a9f02016-02-22 09:56:40 -0500215 for (int i = 0; i < fUniforms.count(); ++i) {
216 const UniformInfo& localUniform = fUniforms[i];
217 if (visibility == localUniform.fVisibility) {
218 if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500219 localUniform.fVariable.appendDecl(fProgramBuilder->glslCaps(), &uniformsString);
220 uniformsString.append(";\n");
Greg Daniel164a9f02016-02-22 09:56:40 -0500221 }
222 }
223 }
224 if (!uniformsString.isEmpty()) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700225 uint32_t uniformBinding = (visibility == kVertex_GrShaderFlag) ? kVertexBinding
226 : kFragBinding;
Greg Daniel164a9f02016-02-22 09:56:40 -0500227 const char* stage = (visibility == kVertex_GrShaderFlag) ? "vertex" : "fragment";
228 out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n",
229 kUniformBufferDescSet, uniformBinding, stage);
230 out->appendf("%s\n};\n", uniformsString.c_str());
231 }
egdaniel4ee1cda2016-02-26 08:18:49 -0800232}