blob: dcfefbfffdeaa7750387f1284c492e2c242fed55 [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:
45 case kTextureBufferSampler_GrSLType:
46 case kTexture2D_GrSLType:
47 case kSampler_GrSLType:
48 break;
49 }
50 SkFAIL("Unexpected type");
51 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -050052}
53
egdaniel4ee1cda2016-02-26 08:18:49 -080054/** Returns the size in bytes taken up in vulkanbuffers for floating point GrSLTypes.
egdaniel75d2bfc2016-07-07 08:04:08 -070055 For non floating point type returns 0. Currently this reflects the std140 alignment
56 so a mat22 takes up 8 floats. */
egdaniel4ee1cda2016-02-26 08:18:49 -080057static inline uint32_t grsltype_to_vk_size(GrSLType type) {
Brian Salomonfa26e662016-11-14 11:27:00 -050058 switch(type) {
59 case kInt_GrSLType:
60 return 4;
61 case kUint_GrSLType:
62 return 4;
63 case kFloat_GrSLType:
64 return sizeof(float);
65 case kVec2f_GrSLType:
66 return 2 * sizeof(float);
67 case kVec3f_GrSLType:
68 return 3 * sizeof(float);
69 case kVec4f_GrSLType:
70 return 4 * sizeof(float);
71 case kMat22f_GrSLType:
72 //TODO: this will be 4 * szof(float) on std430.
73 return 8 * sizeof(float);
74 case kMat33f_GrSLType:
75 return 12 * sizeof(float);
76 case kMat44f_GrSLType:
77 return 16 * sizeof(float);
egdaniel4ee1cda2016-02-26 08:18:49 -080078
Brian Salomonfa26e662016-11-14 11:27:00 -050079 // This query is only valid for certain types.
80 case kVoid_GrSLType:
81 case kBool_GrSLType:
82 case kTexture2DSampler_GrSLType:
Brian Salomona8f00022016-11-16 12:55:57 -050083 case kITexture2DSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050084 case kTextureExternalSampler_GrSLType:
85 case kTexture2DRectSampler_GrSLType:
86 case kTextureBufferSampler_GrSLType:
87 case kTexture2D_GrSLType:
88 case kSampler_GrSLType:
89 break;
90 }
91 SkFAIL("Unexpected type");
92 return 0;
egdaniel4ee1cda2016-02-26 08:18:49 -080093}
94
95
Greg Daniel164a9f02016-02-22 09:56:40 -050096// Given the current offset into the ubo, calculate the offset for the uniform we're trying to add
97// taking into consideration all alignment requirements. The uniformOffset is set to the offset for
98// the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
99void get_ubo_aligned_offset(uint32_t* uniformOffset,
100 uint32_t* currentOffset,
101 GrSLType type,
102 int arrayCount) {
103 uint32_t alignmentMask = grsltype_to_alignment_mask(type);
egdaniel4ee1cda2016-02-26 08:18:49 -0800104 // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
egdaniel75d2bfc2016-07-07 08:04:08 -0700105 if (arrayCount || type == kMat22f_GrSLType) {
egdaniel4ee1cda2016-02-26 08:18:49 -0800106 alignmentMask = 0xF;
107 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500108 uint32_t offsetDiff = *currentOffset & alignmentMask;
109 if (offsetDiff != 0) {
110 offsetDiff = alignmentMask - offsetDiff + 1;
111 }
112 *uniformOffset = *currentOffset + offsetDiff;
113 SkASSERT(sizeof(float) == 4);
egdaniel4ee1cda2016-02-26 08:18:49 -0800114 if (arrayCount) {
115 uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type));
116 SkASSERT(0 == (elementSize & 0xF));
117 *currentOffset = *uniformOffset + elementSize * arrayCount;
118 } else {
119 *currentOffset = *uniformOffset + grsltype_to_vk_size(type);
120 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500121}
122
123GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
124 uint32_t visibility,
125 GrSLType type,
126 GrSLPrecision precision,
127 const char* name,
128 bool mangleName,
129 int arrayCount,
130 const char** outName) {
131 SkASSERT(name && strlen(name));
132 SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_GrShaderFlag|kFragment_GrShaderFlag);
133 SkASSERT(0 == (~kVisibilityMask & visibility));
134 SkASSERT(0 != visibility);
135 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
egdaniel09aa1fc2016-04-20 07:09:46 -0700136 GrSLTypeIsFloatType(type);
Greg Daniel164a9f02016-02-22 09:56:40 -0500137
138 UniformInfo& uni = fUniforms.push_back();
139 uni.fVariable.setType(type);
140 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
141 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
142 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
143 // the names will mismatch. I think the correct solution is to have all GPs which need the
144 // uniform view matrix, they should upload the view matrix in their setData along with regular
145 // uniforms.
146 char prefix = 'u';
147 if ('u' == name[0]) {
148 prefix = '\0';
149 }
150 fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
151 uni.fVariable.setArrayCount(arrayCount);
152 // For now asserting the the visibility is either only vertex or only fragment
153 SkASSERT(kVertex_GrShaderFlag == visibility || kFragment_GrShaderFlag == visibility);
154 uni.fVisibility = visibility;
155 uni.fVariable.setPrecision(precision);
egdaniel09aa1fc2016-04-20 07:09:46 -0700156 // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus
157 // we set the modifier to none for all uniforms declared inside the block.
158 uni.fVariable.setTypeModifier(GrGLSLShaderVar::kNone_TypeModifier);
Greg Daniel164a9f02016-02-22 09:56:40 -0500159
egdaniel09aa1fc2016-04-20 07:09:46 -0700160 uint32_t* currentOffset = kVertex_GrShaderFlag == visibility ? &fCurrentVertexUBOOffset
161 : &fCurrentFragmentUBOOffset;
162 get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500163
egdaniel09aa1fc2016-04-20 07:09:46 -0700164 if (outName) {
165 *outName = uni.fVariable.c_str();
Greg Daniel164a9f02016-02-22 09:56:40 -0500166 }
167
168 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
169}
170
egdaniel09aa1fc2016-04-20 07:09:46 -0700171GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::internalAddSampler(uint32_t visibility,
172 GrPixelConfig config,
173 GrSLType type,
174 GrSLPrecision precision,
175 const char* name) {
176 SkASSERT(name && strlen(name));
177 SkDEBUGCODE(static const uint32_t kVisMask = kVertex_GrShaderFlag | kFragment_GrShaderFlag);
178 SkASSERT(0 == (~kVisMask & visibility));
179 SkASSERT(0 != visibility);
180 SkString mangleName;
181 char prefix = 'u';
182 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
183 fSamplers.emplace_back(visibility, config, type, precision, mangleName.c_str(),
184 (uint32_t)fSamplers.count(), kSamplerDescSet);
185 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
186}
187
Greg Daniel164a9f02016-02-22 09:56:40 -0500188void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdanielb8002482016-04-19 15:24:29 -0700189 SkASSERT(kVertex_GrShaderFlag == visibility || kFragment_GrShaderFlag == visibility);
egdaniel09aa1fc2016-04-20 07:09:46 -0700190
191 for (int i = 0; i < fSamplers.count(); ++i) {
192 const GrVkGLSLSampler& sampler = fSamplers[i];
egdaniel990dbc82016-07-13 14:09:30 -0700193 SkASSERT(sampler.type() == kTexture2DSampler_GrSLType);
egdaniel09aa1fc2016-04-20 07:09:46 -0700194 if (visibility == sampler.visibility()) {
195 sampler.fShaderVar.appendDecl(fProgramBuilder->glslCaps(), out);
196 out->append(";\n");
197 }
198 }
199
200 SkString uniformsString;
Greg Daniel164a9f02016-02-22 09:56:40 -0500201 for (int i = 0; i < fUniforms.count(); ++i) {
202 const UniformInfo& localUniform = fUniforms[i];
203 if (visibility == localUniform.fVisibility) {
204 if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500205 localUniform.fVariable.appendDecl(fProgramBuilder->glslCaps(), &uniformsString);
206 uniformsString.append(";\n");
Greg Daniel164a9f02016-02-22 09:56:40 -0500207 }
208 }
209 }
210 if (!uniformsString.isEmpty()) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700211 uint32_t uniformBinding = (visibility == kVertex_GrShaderFlag) ? kVertexBinding
212 : kFragBinding;
Greg Daniel164a9f02016-02-22 09:56:40 -0500213 const char* stage = (visibility == kVertex_GrShaderFlag) ? "vertex" : "fragment";
214 out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n",
215 kUniformBufferDescSet, uniformBinding, stage);
216 out->appendf("%s\n};\n", uniformsString.c_str());
217 }
egdaniel4ee1cda2016-02-26 08:18:49 -0800218}