blob: f1b62b0974053fdb9ca458081da78204d7d59ad3 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/vk/GrVkUniformHandler.h"
Greg Daniel9a51a862018-11-30 10:18:14 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrTexturePriv.h"
11#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
12#include "src/gpu/vk/GrVkGpu.h"
13#include "src/gpu/vk/GrVkPipelineStateBuilder.h"
14#include "src/gpu/vk/GrVkTexture.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050015
16// To determine whether a current offset is aligned, we can just 'and' the lowest bits with the
17// alignment mask. A value of 0 means aligned, any other value is how many bytes past alignment we
18// are. This works since all alignments are powers of 2. The mask is always (alignment - 1).
egdaniel4ee1cda2016-02-26 08:18:49 -080019// This alignment mask will give correct alignments for using the std430 block layout. If you want
20// the std140 alignment, you can use this, but then make sure if you have an array type it is
21// aligned to 16 bytes (i.e. has mask of 0xF).
Chris Dalton51ebd662017-10-24 16:10:48 -060022// These are designated in the Vulkan spec, section 14.5.4 "Offset and Stride Assignment".
23// https://www.khronos.org/registry/vulkan/specs/1.0-wsi_extensions/html/vkspec.html#interfaces-resources-layout
Mike Kleindc735592018-10-11 14:26:30 -040024static uint32_t grsltype_to_alignment_mask(GrSLType type) {
Brian Salomonfa26e662016-11-14 11:27:00 -050025 switch(type) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -040026 case kByte_GrSLType: // fall through
27 case kUByte_GrSLType:
28 return 0x0;
29 case kByte2_GrSLType: // fall through
30 case kUByte2_GrSLType:
31 return 0x1;
32 case kByte3_GrSLType: // fall through
33 case kByte4_GrSLType:
34 case kUByte3_GrSLType:
35 case kUByte4_GrSLType:
36 return 0x3;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040037 case kShort_GrSLType: // fall through
Chris Dalton51ebd662017-10-24 16:10:48 -060038 case kUShort_GrSLType:
39 return 0x1;
40 case kShort2_GrSLType: // fall through
41 case kUShort2_GrSLType:
Chris Dalton6dd0d8a2017-10-24 19:53:13 +000042 return 0x3;
Chris Dalton51ebd662017-10-24 16:10:48 -060043 case kShort3_GrSLType: // fall through
44 case kShort4_GrSLType:
45 case kUShort3_GrSLType:
46 case kUShort4_GrSLType:
47 return 0x7;
48 case kInt_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050049 case kUint_GrSLType:
50 return 0x3;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040051 case kHalf_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -040052 case kFloat_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050053 return 0x3;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040054 case kHalf2_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -040055 case kFloat2_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050056 return 0x7;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040057 case kHalf3_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -040058 case kFloat3_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050059 return 0xF;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040060 case kHalf4_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -040061 case kFloat4_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050062 return 0xF;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040063 case kUint2_GrSLType:
Chris Dalton51ebd662017-10-24 16:10:48 -060064 return 0x7;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040065 case kInt2_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -050066 return 0x7;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040067 case kInt3_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -050068 return 0xF;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040069 case kInt4_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -050070 return 0xF;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040071 case kHalf2x2_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -040072 case kFloat2x2_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050073 return 0x7;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040074 case kHalf3x3_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -040075 case kFloat3x3_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050076 return 0xF;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040077 case kHalf4x4_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -040078 case kFloat4x4_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050079 return 0xF;
80
81 // This query is only valid for certain types.
82 case kVoid_GrSLType:
83 case kBool_GrSLType:
84 case kTexture2DSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050085 case kTextureExternalSampler_GrSLType:
86 case kTexture2DRectSampler_GrSLType:
Stephen Whiteff5d7a22019-07-26 17:42:06 -040087 case kSampler_GrSLType:
88 case kTexture2D_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050089 break;
90 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040091 SK_ABORT("Unexpected type");
Greg Daniel164a9f02016-02-22 09:56:40 -050092}
93
Chris Dalton51ebd662017-10-24 16:10:48 -060094/** Returns the size in bytes taken up in vulkanbuffers for GrSLTypes. */
egdaniel4ee1cda2016-02-26 08:18:49 -080095static inline uint32_t grsltype_to_vk_size(GrSLType type) {
Brian Salomonfa26e662016-11-14 11:27:00 -050096 switch(type) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -040097 case kByte_GrSLType:
98 return sizeof(int8_t);
99 case kByte2_GrSLType:
100 return 2 * sizeof(int8_t);
101 case kByte3_GrSLType:
102 return 3 * sizeof(int8_t);
103 case kByte4_GrSLType:
104 return 4 * sizeof(int8_t);
105 case kUByte_GrSLType:
106 return sizeof(uint8_t);
107 case kUByte2_GrSLType:
108 return 2 * sizeof(uint8_t);
109 case kUByte3_GrSLType:
110 return 3 * sizeof(uint8_t);
111 case kUByte4_GrSLType:
112 return 4 * sizeof(uint8_t);
Chris Dalton51ebd662017-10-24 16:10:48 -0600113 case kShort_GrSLType:
114 return sizeof(int16_t);
115 case kShort2_GrSLType:
116 return 2 * sizeof(int16_t);
117 case kShort3_GrSLType:
118 return 3 * sizeof(int16_t);
119 case kShort4_GrSLType:
120 return 4 * sizeof(int16_t);
121 case kUShort_GrSLType:
122 return sizeof(uint16_t);
123 case kUShort2_GrSLType:
124 return 2 * sizeof(uint16_t);
125 case kUShort3_GrSLType:
126 return 3 * sizeof(uint16_t);
127 case kUShort4_GrSLType:
128 return 4 * sizeof(uint16_t);
Brian Salomonfa26e662016-11-14 11:27:00 -0500129 case kInt_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500130 return sizeof(int32_t);
Brian Salomonfa26e662016-11-14 11:27:00 -0500131 case kUint_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500132 return sizeof(int32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400133 case kHalf_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400134 case kFloat_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500135 return sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400136 case kHalf2_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400137 case kFloat2_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500138 return 2 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400139 case kHalf3_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400140 case kFloat3_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500141 return 3 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400142 case kHalf4_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400143 case kFloat4_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500144 return 4 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400145 case kUint2_GrSLType:
Chris Dalton51ebd662017-10-24 16:10:48 -0600146 return 2 * sizeof(uint32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400147 case kInt2_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500148 return 2 * sizeof(int32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400149 case kInt3_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500150 return 3 * sizeof(int32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400151 case kInt4_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500152 return 4 * sizeof(int32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400153 case kHalf2x2_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400154 case kFloat2x2_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500155 //TODO: this will be 4 * szof(float) on std430.
156 return 8 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400157 case kHalf3x3_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400158 case kFloat3x3_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500159 return 12 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400160 case kHalf4x4_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400161 case kFloat4x4_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500162 return 16 * sizeof(float);
egdaniel4ee1cda2016-02-26 08:18:49 -0800163
Brian Salomonfa26e662016-11-14 11:27:00 -0500164 // This query is only valid for certain types.
165 case kVoid_GrSLType:
166 case kBool_GrSLType:
167 case kTexture2DSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500168 case kTextureExternalSampler_GrSLType:
169 case kTexture2DRectSampler_GrSLType:
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400170 case kSampler_GrSLType:
171 case kTexture2D_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500172 break;
173 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400174 SK_ABORT("Unexpected type");
egdaniel4ee1cda2016-02-26 08:18:49 -0800175}
176
177
Greg Daniel164a9f02016-02-22 09:56:40 -0500178// Given the current offset into the ubo, calculate the offset for the uniform we're trying to add
179// taking into consideration all alignment requirements. The uniformOffset is set to the offset for
180// the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
Mike Kleindc735592018-10-11 14:26:30 -0400181static void get_ubo_aligned_offset(uint32_t* uniformOffset,
182 uint32_t* currentOffset,
183 GrSLType type,
184 int arrayCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500185 uint32_t alignmentMask = grsltype_to_alignment_mask(type);
egdaniel4ee1cda2016-02-26 08:18:49 -0800186 // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400187 if (arrayCount || type == kFloat2x2_GrSLType) {
egdaniel4ee1cda2016-02-26 08:18:49 -0800188 alignmentMask = 0xF;
189 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500190 uint32_t offsetDiff = *currentOffset & alignmentMask;
191 if (offsetDiff != 0) {
192 offsetDiff = alignmentMask - offsetDiff + 1;
193 }
194 *uniformOffset = *currentOffset + offsetDiff;
195 SkASSERT(sizeof(float) == 4);
egdaniel4ee1cda2016-02-26 08:18:49 -0800196 if (arrayCount) {
197 uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type));
198 SkASSERT(0 == (elementSize & 0xF));
199 *currentOffset = *uniformOffset + elementSize * arrayCount;
200 } else {
201 *currentOffset = *uniformOffset + grsltype_to_vk_size(type);
202 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500203}
204
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700205GrVkUniformHandler::~GrVkUniformHandler() {
206 GrVkGpu* gpu = static_cast<GrVkPipelineStateBuilder*>(fProgramBuilder)->gpu();
207 for (decltype(fSamplers)::Iter iter(&fSamplers); iter.next();) {
208 if (iter->fImmutableSampler) {
209 iter->fImmutableSampler->unref(gpu);
210 iter->fImmutableSampler = nullptr;
211 }
212 }
213}
214
Greg Daniel164a9f02016-02-22 09:56:40 -0500215GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
216 uint32_t visibility,
217 GrSLType type,
Greg Daniel164a9f02016-02-22 09:56:40 -0500218 const char* name,
219 bool mangleName,
220 int arrayCount,
221 const char** outName) {
222 SkASSERT(name && strlen(name));
Greg Daniel18f96022017-05-04 15:09:03 -0400223 // For now asserting the the visibility is either geometry types (vertex, tesselation, geometry,
224 // etc.) or only fragment.
225 SkASSERT(kVertex_GrShaderFlag == visibility ||
226 kGeometry_GrShaderFlag == visibility ||
227 (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == visibility ||
228 kFragment_GrShaderFlag == visibility);
egdaniel09aa1fc2016-04-20 07:09:46 -0700229 GrSLTypeIsFloatType(type);
Greg Daniel164a9f02016-02-22 09:56:40 -0500230
231 UniformInfo& uni = fUniforms.push_back();
232 uni.fVariable.setType(type);
233 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
234 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
235 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
236 // the names will mismatch. I think the correct solution is to have all GPs which need the
237 // uniform view matrix, they should upload the view matrix in their setData along with regular
238 // uniforms.
239 char prefix = 'u';
Robert Phillipsfe8da172018-01-24 14:52:02 +0000240 if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500241 prefix = '\0';
242 }
243 fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
244 uni.fVariable.setArrayCount(arrayCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500245 uni.fVisibility = visibility;
egdaniel09aa1fc2016-04-20 07:09:46 -0700246 // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus
247 // we set the modifier to none for all uniforms declared inside the block.
Brian Salomon99938a82016-11-21 13:41:08 -0500248 uni.fVariable.setTypeModifier(GrShaderVar::kNone_TypeModifier);
Greg Daniel164a9f02016-02-22 09:56:40 -0500249
Greg Daniel18f96022017-05-04 15:09:03 -0400250 uint32_t* currentOffset;
251 uint32_t geomStages = kVertex_GrShaderFlag | kGeometry_GrShaderFlag;
252 if (geomStages & visibility) {
253 currentOffset = &fCurrentGeometryUBOOffset;
254 } else {
255 SkASSERT(kFragment_GrShaderFlag == visibility);
256 currentOffset = &fCurrentFragmentUBOOffset;
257 }
egdaniel09aa1fc2016-04-20 07:09:46 -0700258 get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500259
Greg Danieldbd44c72017-01-24 15:12:12 -0500260 SkString layoutQualifier;
261 layoutQualifier.appendf("offset=%d", uni.fUBOffset);
262 uni.fVariable.addLayoutQualifier(layoutQualifier.c_str());
263
egdaniel09aa1fc2016-04-20 07:09:46 -0700264 if (outName) {
265 *outName = uni.fVariable.c_str();
Greg Daniel164a9f02016-02-22 09:56:40 -0500266 }
267
268 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
269}
270
Greg Daniel9a51a862018-11-30 10:18:14 -0500271GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(const GrTexture* texture,
272 const GrSamplerState& state,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400273 const GrSwizzle& swizzle,
Greg Daniel9a51a862018-11-30 10:18:14 -0500274 const char* name,
275 const GrShaderCaps* shaderCaps) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700276 SkASSERT(name && strlen(name));
egdaniel09aa1fc2016-04-20 07:09:46 -0700277 SkString mangleName;
278 char prefix = 'u';
279 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
Brian Salomon101b8442016-11-18 11:58:54 -0500280
Greg Daniel9a51a862018-11-30 10:18:14 -0500281 GrTextureType type = texture->texturePriv().textureType();
282
Brian Salomon101b8442016-11-18 11:58:54 -0500283 UniformInfo& info = fSamplers.push_back();
Brian Salomon60dd8c72018-07-30 10:24:13 -0400284 info.fVariable.setType(GrSLCombinedSamplerTypeForTextureType(type));
Brian Salomon99938a82016-11-21 13:41:08 -0500285 info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
Brian Salomon101b8442016-11-18 11:58:54 -0500286 info.fVariable.setName(mangleName);
287 SkString layoutQualifier;
288 layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count() - 1);
Brian Salomon60397682016-11-22 15:06:46 -0500289 info.fVariable.addLayoutQualifier(layoutQualifier.c_str());
Greg Daniel0f70be82018-10-08 17:35:08 +0000290 info.fVisibility = kFragment_GrShaderFlag;
Brian Salomon101b8442016-11-18 11:58:54 -0500291 info.fUBOffset = 0;
Greg Daniel9a51a862018-11-30 10:18:14 -0500292
Sergey Ulanov2739fd22019-08-11 22:46:33 -0700293 // Check if we are dealing with an external texture and store the needed information if so.
Greg Daniel9a51a862018-11-30 10:18:14 -0500294 const GrVkTexture* vkTexture = static_cast<const GrVkTexture*>(texture);
295 if (vkTexture->ycbcrConversionInfo().isValid()) {
Greg Daniel9a51a862018-11-30 10:18:14 -0500296 GrVkGpu* gpu = static_cast<GrVkPipelineStateBuilder*>(fProgramBuilder)->gpu();
297 info.fImmutableSampler = gpu->resourceProvider().findOrCreateCompatibleSampler(
298 state, vkTexture->ycbcrConversionInfo());
299 SkASSERT(info.fImmutableSampler);
300 }
301
Brian Salomon68ba1172019-06-05 11:15:08 -0400302 SkASSERT(shaderCaps->textureSwizzleAppliedInShader());
Brian Salomon101b8442016-11-18 11:58:54 -0500303 fSamplerSwizzles.push_back(swizzle);
304 SkASSERT(fSamplerSwizzles.count() == fSamplers.count());
egdaniel09aa1fc2016-04-20 07:09:46 -0700305 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
306}
307
Greg Daniel164a9f02016-02-22 09:56:40 -0500308void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
Greg Daniel18f96022017-05-04 15:09:03 -0400309 SkASSERT(kVertex_GrShaderFlag == visibility ||
310 kGeometry_GrShaderFlag == visibility ||
311 kFragment_GrShaderFlag == visibility);
egdaniel09aa1fc2016-04-20 07:09:46 -0700312
313 for (int i = 0; i < fSamplers.count(); ++i) {
Brian Salomon101b8442016-11-18 11:58:54 -0500314 const UniformInfo& sampler = fSamplers[i];
315 SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType);
316 if (visibility == sampler.fVisibility) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500317 sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
egdaniel09aa1fc2016-04-20 07:09:46 -0700318 out->append(";\n");
319 }
320 }
321
Greg Danielaa352de2017-07-17 09:05:16 -0400322#ifdef SK_DEBUG
323 bool firstGeomOffsetCheck = false;
324 bool firstFragOffsetCheck = false;
325 for (int i = 0; i < fUniforms.count(); ++i) {
326 const UniformInfo& localUniform = fUniforms[i];
327 if (kVertex_GrShaderFlag == localUniform.fVisibility ||
328 kGeometry_GrShaderFlag == localUniform.fVisibility ||
329 (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == localUniform.fVisibility) {
330 if (!firstGeomOffsetCheck) {
331 // Check to make sure we are starting our offset at 0 so the offset qualifier we
332 // set on each variable in the uniform block is valid.
333 SkASSERT(0 == localUniform.fUBOffset);
334 firstGeomOffsetCheck = true;
335 }
336 } else {
337 SkASSERT(kFragment_GrShaderFlag == localUniform.fVisibility);
338 if (!firstFragOffsetCheck) {
339 // Check to make sure we are starting our offset at 0 so the offset qualifier we
340 // set on each variable in the uniform block is valid.
341 SkASSERT(0 == localUniform.fUBOffset);
342 firstFragOffsetCheck = true;
343 }
344 }
345 }
346#endif
347
egdaniel09aa1fc2016-04-20 07:09:46 -0700348 SkString uniformsString;
Greg Daniel164a9f02016-02-22 09:56:40 -0500349 for (int i = 0; i < fUniforms.count(); ++i) {
350 const UniformInfo& localUniform = fUniforms[i];
Greg Daniel18f96022017-05-04 15:09:03 -0400351 if (visibility & localUniform.fVisibility) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500352 if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500353 localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
Greg Daniel164a9f02016-02-22 09:56:40 -0500354 uniformsString.append(";\n");
Greg Daniel164a9f02016-02-22 09:56:40 -0500355 }
356 }
357 }
Greg Danielaa352de2017-07-17 09:05:16 -0400358
Greg Daniel164a9f02016-02-22 09:56:40 -0500359 if (!uniformsString.isEmpty()) {
Greg Daniel18f96022017-05-04 15:09:03 -0400360 uint32_t uniformBinding;
361 const char* stage;
362 if (kVertex_GrShaderFlag == visibility) {
363 uniformBinding = kGeometryBinding;
364 stage = "vertex";
365 } else if (kGeometry_GrShaderFlag == visibility) {
366 uniformBinding = kGeometryBinding;
367 stage = "geometry";
368 } else {
369 SkASSERT(kFragment_GrShaderFlag == visibility);
370 uniformBinding = kFragBinding;
371 stage = "fragment";
372 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500373 out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n",
374 kUniformBufferDescSet, uniformBinding, stage);
375 out->appendf("%s\n};\n", uniformsString.c_str());
376 }
egdaniel4ee1cda2016-02-26 08:18:49 -0800377}