blob: 32210d645cac4bd9249d4bf776a124f20fa51e9c [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");
Brian Salomonfa26e662016-11-14 11:27:00 -050092 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -050093}
94
Chris Dalton51ebd662017-10-24 16:10:48 -060095/** Returns the size in bytes taken up in vulkanbuffers for GrSLTypes. */
egdaniel4ee1cda2016-02-26 08:18:49 -080096static inline uint32_t grsltype_to_vk_size(GrSLType type) {
Brian Salomonfa26e662016-11-14 11:27:00 -050097 switch(type) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -040098 case kByte_GrSLType:
99 return sizeof(int8_t);
100 case kByte2_GrSLType:
101 return 2 * sizeof(int8_t);
102 case kByte3_GrSLType:
103 return 3 * sizeof(int8_t);
104 case kByte4_GrSLType:
105 return 4 * sizeof(int8_t);
106 case kUByte_GrSLType:
107 return sizeof(uint8_t);
108 case kUByte2_GrSLType:
109 return 2 * sizeof(uint8_t);
110 case kUByte3_GrSLType:
111 return 3 * sizeof(uint8_t);
112 case kUByte4_GrSLType:
113 return 4 * sizeof(uint8_t);
Chris Dalton51ebd662017-10-24 16:10:48 -0600114 case kShort_GrSLType:
115 return sizeof(int16_t);
116 case kShort2_GrSLType:
117 return 2 * sizeof(int16_t);
118 case kShort3_GrSLType:
119 return 3 * sizeof(int16_t);
120 case kShort4_GrSLType:
121 return 4 * sizeof(int16_t);
122 case kUShort_GrSLType:
123 return sizeof(uint16_t);
124 case kUShort2_GrSLType:
125 return 2 * sizeof(uint16_t);
126 case kUShort3_GrSLType:
127 return 3 * sizeof(uint16_t);
128 case kUShort4_GrSLType:
129 return 4 * sizeof(uint16_t);
Brian Salomonfa26e662016-11-14 11:27:00 -0500130 case kInt_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500131 return sizeof(int32_t);
Brian Salomonfa26e662016-11-14 11:27:00 -0500132 case kUint_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500133 return sizeof(int32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400134 case kHalf_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400135 case kFloat_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500136 return sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400137 case kHalf2_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400138 case kFloat2_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500139 return 2 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400140 case kHalf3_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400141 case kFloat3_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500142 return 3 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400143 case kHalf4_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400144 case kFloat4_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500145 return 4 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400146 case kUint2_GrSLType:
Chris Dalton51ebd662017-10-24 16:10:48 -0600147 return 2 * sizeof(uint32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400148 case kInt2_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500149 return 2 * sizeof(int32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400150 case kInt3_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500151 return 3 * sizeof(int32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400152 case kInt4_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -0500153 return 4 * sizeof(int32_t);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400154 case kHalf2x2_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400155 case kFloat2x2_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500156 //TODO: this will be 4 * szof(float) on std430.
157 return 8 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400158 case kHalf3x3_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400159 case kFloat3x3_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500160 return 12 * sizeof(float);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400161 case kHalf4x4_GrSLType: // fall through
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400162 case kFloat4x4_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500163 return 16 * sizeof(float);
egdaniel4ee1cda2016-02-26 08:18:49 -0800164
Brian Salomonfa26e662016-11-14 11:27:00 -0500165 // This query is only valid for certain types.
166 case kVoid_GrSLType:
167 case kBool_GrSLType:
168 case kTexture2DSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500169 case kTextureExternalSampler_GrSLType:
170 case kTexture2DRectSampler_GrSLType:
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400171 case kSampler_GrSLType:
172 case kTexture2D_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -0500173 break;
174 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400175 SK_ABORT("Unexpected type");
Brian Salomonfa26e662016-11-14 11:27:00 -0500176 return 0;
egdaniel4ee1cda2016-02-26 08:18:49 -0800177}
178
179
Greg Daniel164a9f02016-02-22 09:56:40 -0500180// Given the current offset into the ubo, calculate the offset for the uniform we're trying to add
181// taking into consideration all alignment requirements. The uniformOffset is set to the offset for
182// 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 -0400183static void get_ubo_aligned_offset(uint32_t* uniformOffset,
184 uint32_t* currentOffset,
185 GrSLType type,
186 int arrayCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500187 uint32_t alignmentMask = grsltype_to_alignment_mask(type);
egdaniel4ee1cda2016-02-26 08:18:49 -0800188 // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400189 if (arrayCount || type == kFloat2x2_GrSLType) {
egdaniel4ee1cda2016-02-26 08:18:49 -0800190 alignmentMask = 0xF;
191 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500192 uint32_t offsetDiff = *currentOffset & alignmentMask;
193 if (offsetDiff != 0) {
194 offsetDiff = alignmentMask - offsetDiff + 1;
195 }
196 *uniformOffset = *currentOffset + offsetDiff;
197 SkASSERT(sizeof(float) == 4);
egdaniel4ee1cda2016-02-26 08:18:49 -0800198 if (arrayCount) {
199 uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type));
200 SkASSERT(0 == (elementSize & 0xF));
201 *currentOffset = *uniformOffset + elementSize * arrayCount;
202 } else {
203 *currentOffset = *uniformOffset + grsltype_to_vk_size(type);
204 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500205}
206
207GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
208 uint32_t visibility,
209 GrSLType type,
Greg Daniel164a9f02016-02-22 09:56:40 -0500210 const char* name,
211 bool mangleName,
212 int arrayCount,
213 const char** outName) {
214 SkASSERT(name && strlen(name));
Greg Daniel18f96022017-05-04 15:09:03 -0400215 // For now asserting the the visibility is either geometry types (vertex, tesselation, geometry,
216 // etc.) or only fragment.
217 SkASSERT(kVertex_GrShaderFlag == visibility ||
218 kGeometry_GrShaderFlag == visibility ||
219 (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == visibility ||
220 kFragment_GrShaderFlag == visibility);
egdaniel09aa1fc2016-04-20 07:09:46 -0700221 GrSLTypeIsFloatType(type);
Greg Daniel164a9f02016-02-22 09:56:40 -0500222
223 UniformInfo& uni = fUniforms.push_back();
224 uni.fVariable.setType(type);
225 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
226 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
227 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
228 // the names will mismatch. I think the correct solution is to have all GPs which need the
229 // uniform view matrix, they should upload the view matrix in their setData along with regular
230 // uniforms.
231 char prefix = 'u';
Robert Phillipsfe8da172018-01-24 14:52:02 +0000232 if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500233 prefix = '\0';
234 }
235 fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
236 uni.fVariable.setArrayCount(arrayCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500237 uni.fVisibility = visibility;
egdaniel09aa1fc2016-04-20 07:09:46 -0700238 // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus
239 // we set the modifier to none for all uniforms declared inside the block.
Brian Salomon99938a82016-11-21 13:41:08 -0500240 uni.fVariable.setTypeModifier(GrShaderVar::kNone_TypeModifier);
Greg Daniel164a9f02016-02-22 09:56:40 -0500241
Greg Daniel18f96022017-05-04 15:09:03 -0400242 uint32_t* currentOffset;
243 uint32_t geomStages = kVertex_GrShaderFlag | kGeometry_GrShaderFlag;
244 if (geomStages & visibility) {
245 currentOffset = &fCurrentGeometryUBOOffset;
246 } else {
247 SkASSERT(kFragment_GrShaderFlag == visibility);
248 currentOffset = &fCurrentFragmentUBOOffset;
249 }
egdaniel09aa1fc2016-04-20 07:09:46 -0700250 get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500251
Greg Danieldbd44c72017-01-24 15:12:12 -0500252 SkString layoutQualifier;
253 layoutQualifier.appendf("offset=%d", uni.fUBOffset);
254 uni.fVariable.addLayoutQualifier(layoutQualifier.c_str());
255
egdaniel09aa1fc2016-04-20 07:09:46 -0700256 if (outName) {
257 *outName = uni.fVariable.c_str();
Greg Daniel164a9f02016-02-22 09:56:40 -0500258 }
259
260 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
261}
262
Greg Daniel9a51a862018-11-30 10:18:14 -0500263GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(const GrTexture* texture,
264 const GrSamplerState& state,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400265 const GrSwizzle& swizzle,
Greg Daniel9a51a862018-11-30 10:18:14 -0500266 const char* name,
267 const GrShaderCaps* shaderCaps) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700268 SkASSERT(name && strlen(name));
egdaniel09aa1fc2016-04-20 07:09:46 -0700269 SkString mangleName;
270 char prefix = 'u';
271 fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
Brian Salomon101b8442016-11-18 11:58:54 -0500272
Greg Daniel9a51a862018-11-30 10:18:14 -0500273 GrTextureType type = texture->texturePriv().textureType();
274
Brian Salomon101b8442016-11-18 11:58:54 -0500275 UniformInfo& info = fSamplers.push_back();
Brian Salomon60dd8c72018-07-30 10:24:13 -0400276 info.fVariable.setType(GrSLCombinedSamplerTypeForTextureType(type));
Brian Salomon99938a82016-11-21 13:41:08 -0500277 info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
Brian Salomon101b8442016-11-18 11:58:54 -0500278 info.fVariable.setName(mangleName);
279 SkString layoutQualifier;
280 layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count() - 1);
Brian Salomon60397682016-11-22 15:06:46 -0500281 info.fVariable.addLayoutQualifier(layoutQualifier.c_str());
Greg Daniel0f70be82018-10-08 17:35:08 +0000282 info.fVisibility = kFragment_GrShaderFlag;
Brian Salomon101b8442016-11-18 11:58:54 -0500283 info.fUBOffset = 0;
Greg Daniel9a51a862018-11-30 10:18:14 -0500284
285 // Check if we are dealing with an external texture and store the needed information if so
286 const GrVkTexture* vkTexture = static_cast<const GrVkTexture*>(texture);
287 if (vkTexture->ycbcrConversionInfo().isValid()) {
288 SkASSERT(type == GrTextureType::kExternal);
289 GrVkGpu* gpu = static_cast<GrVkPipelineStateBuilder*>(fProgramBuilder)->gpu();
290 info.fImmutableSampler = gpu->resourceProvider().findOrCreateCompatibleSampler(
291 state, vkTexture->ycbcrConversionInfo());
292 SkASSERT(info.fImmutableSampler);
293 }
294
Brian Salomon68ba1172019-06-05 11:15:08 -0400295 SkASSERT(shaderCaps->textureSwizzleAppliedInShader());
Brian Salomon101b8442016-11-18 11:58:54 -0500296 fSamplerSwizzles.push_back(swizzle);
297 SkASSERT(fSamplerSwizzles.count() == fSamplers.count());
egdaniel09aa1fc2016-04-20 07:09:46 -0700298 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
299}
300
Greg Daniel164a9f02016-02-22 09:56:40 -0500301void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
Greg Daniel18f96022017-05-04 15:09:03 -0400302 SkASSERT(kVertex_GrShaderFlag == visibility ||
303 kGeometry_GrShaderFlag == visibility ||
304 kFragment_GrShaderFlag == visibility);
egdaniel09aa1fc2016-04-20 07:09:46 -0700305
306 for (int i = 0; i < fSamplers.count(); ++i) {
Brian Salomon101b8442016-11-18 11:58:54 -0500307 const UniformInfo& sampler = fSamplers[i];
308 SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType);
309 if (visibility == sampler.fVisibility) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500310 sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
egdaniel09aa1fc2016-04-20 07:09:46 -0700311 out->append(";\n");
312 }
313 }
314
Greg Danielaa352de2017-07-17 09:05:16 -0400315#ifdef SK_DEBUG
316 bool firstGeomOffsetCheck = false;
317 bool firstFragOffsetCheck = false;
318 for (int i = 0; i < fUniforms.count(); ++i) {
319 const UniformInfo& localUniform = fUniforms[i];
320 if (kVertex_GrShaderFlag == localUniform.fVisibility ||
321 kGeometry_GrShaderFlag == localUniform.fVisibility ||
322 (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == localUniform.fVisibility) {
323 if (!firstGeomOffsetCheck) {
324 // Check to make sure we are starting our offset at 0 so the offset qualifier we
325 // set on each variable in the uniform block is valid.
326 SkASSERT(0 == localUniform.fUBOffset);
327 firstGeomOffsetCheck = true;
328 }
329 } else {
330 SkASSERT(kFragment_GrShaderFlag == localUniform.fVisibility);
331 if (!firstFragOffsetCheck) {
332 // Check to make sure we are starting our offset at 0 so the offset qualifier we
333 // set on each variable in the uniform block is valid.
334 SkASSERT(0 == localUniform.fUBOffset);
335 firstFragOffsetCheck = true;
336 }
337 }
338 }
339#endif
340
egdaniel09aa1fc2016-04-20 07:09:46 -0700341 SkString uniformsString;
Greg Daniel164a9f02016-02-22 09:56:40 -0500342 for (int i = 0; i < fUniforms.count(); ++i) {
343 const UniformInfo& localUniform = fUniforms[i];
Greg Daniel18f96022017-05-04 15:09:03 -0400344 if (visibility & localUniform.fVisibility) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500345 if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500346 localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
Greg Daniel164a9f02016-02-22 09:56:40 -0500347 uniformsString.append(";\n");
Greg Daniel164a9f02016-02-22 09:56:40 -0500348 }
349 }
350 }
Greg Danielaa352de2017-07-17 09:05:16 -0400351
Greg Daniel164a9f02016-02-22 09:56:40 -0500352 if (!uniformsString.isEmpty()) {
Greg Daniel18f96022017-05-04 15:09:03 -0400353 uint32_t uniformBinding;
354 const char* stage;
355 if (kVertex_GrShaderFlag == visibility) {
356 uniformBinding = kGeometryBinding;
357 stage = "vertex";
358 } else if (kGeometry_GrShaderFlag == visibility) {
359 uniformBinding = kGeometryBinding;
360 stage = "geometry";
361 } else {
362 SkASSERT(kFragment_GrShaderFlag == visibility);
363 uniformBinding = kFragBinding;
364 stage = "fragment";
365 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500366 out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n",
367 kUniformBufferDescSet, uniformBinding, stage);
368 out->appendf("%s\n};\n", uniformsString.c_str());
369 }
egdaniel4ee1cda2016-02-26 08:18:49 -0800370}