Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrVkUniformHandler.cpp
Go to the documentation of this file.
1/*
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
9
16
17// To determine whether a current offset is aligned, we can just 'and' the lowest bits with the
18// alignment mask. A value of 0 means aligned, any other value is how many bytes past alignment we
19// are. This works since all alignments are powers of 2. The mask is always (alignment - 1).
20// This alignment mask will give correct alignments for using the std430 block layout. If you want
21// the std140 alignment, you can use this, but then make sure if you have an array type it is
22// aligned to 16 bytes (i.e. has mask of 0xF).
23// These are designated in the Vulkan spec, section 14.5.4 "Offset and Stride Assignment".
24// https://www.khronos.org/registry/vulkan/specs/1.0-wsi_extensions/html/vkspec.html#interfaces-resources-layout
26 switch(type) {
27 case SkSLType::kShort: // fall through
29 return 0x1;
30 case SkSLType::kShort2: // fall through
32 return 0x3;
33 case SkSLType::kShort3: // fall through
37 return 0x7;
38 case SkSLType::kInt:
39 case SkSLType::kUInt:
40 return 0x3;
41 case SkSLType::kInt2:
43 return 0x7;
44 case SkSLType::kInt3:
46 case SkSLType::kInt4:
48 return 0xF;
49 case SkSLType::kHalf: // fall through
51 return 0x3;
52 case SkSLType::kHalf2: // fall through
54 return 0x7;
55 case SkSLType::kHalf3: // fall through
57 return 0xF;
58 case SkSLType::kHalf4: // fall through
60 return 0xF;
61 case SkSLType::kHalf2x2: // fall through
63 return 0x7;
64 case SkSLType::kHalf3x3: // fall through
66 return 0xF;
67 case SkSLType::kHalf4x4: // fall through
69 return 0xF;
70
71 // This query is only valid for certain types.
72 case SkSLType::kVoid:
73 case SkSLType::kBool:
83 break;
84 }
85 SK_ABORT("Unexpected type");
86}
87
88/** Returns the size in bytes taken up in vulkanbuffers for SkSLTypes. */
89static inline uint32_t sksltype_to_vk_size(SkSLType type, int layout) {
90 switch(type) {
92 return sizeof(int16_t);
94 return 2 * sizeof(int16_t);
96 return 3 * sizeof(int16_t);
98 return 4 * sizeof(int16_t);
100 return sizeof(uint16_t);
102 return 2 * sizeof(uint16_t);
104 return 3 * sizeof(uint16_t);
106 return 4 * sizeof(uint16_t);
107 case SkSLType::kHalf: // fall through
108 case SkSLType::kFloat:
109 return sizeof(float);
110 case SkSLType::kHalf2: // fall through
112 return 2 * sizeof(float);
113 case SkSLType::kHalf3: // fall through
115 return 3 * sizeof(float);
116 case SkSLType::kHalf4: // fall through
118 return 4 * sizeof(float);
119 case SkSLType::kInt: // fall through
120 case SkSLType::kUInt:
121 return sizeof(int32_t);
122 case SkSLType::kInt2: // fall through
123 case SkSLType::kUInt2:
124 return 2 * sizeof(int32_t);
125 case SkSLType::kInt3: // fall through
126 case SkSLType::kUInt3:
127 return 3 * sizeof(int32_t);
128 case SkSLType::kInt4: // fall through
129 case SkSLType::kUInt4:
130 return 4 * sizeof(int32_t);
131 case SkSLType::kHalf2x2: // fall through
133 if (layout == GrVkUniformHandler::kStd430Layout) {
134 return 4 * sizeof(float);
135 } else {
136 return 8 * sizeof(float);
137 }
138 case SkSLType::kHalf3x3: // fall through
140 return 12 * sizeof(float);
141 case SkSLType::kHalf4x4: // fall through
143 return 16 * sizeof(float);
144
145 // This query is only valid for certain types.
146 case SkSLType::kVoid:
147 case SkSLType::kBool:
148 case SkSLType::kBool2:
149 case SkSLType::kBool3:
150 case SkSLType::kBool4:
156 case SkSLType::kInput:
157 break;
158 }
159 SK_ABORT("Unexpected type");
160}
161
162// Given the current offset into the ubo data, calculate the offset for the uniform we're trying to
163// add taking into consideration all alignment requirements. The uniformOffset is set to the offset
164// for the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
165static uint32_t get_aligned_offset(uint32_t* currentOffset,
167 int arrayCount,
168 int layout) {
169 uint32_t alignmentMask = sksltype_to_alignment_mask(type);
170 // For std140 layout we must make arrays align to 16 bytes.
171 // TODO(skia:13380): make sure 2x3 and 3x2 matrices are handled properly once SkSLType adds
172 // support for non-square matrices
173 if (layout == GrVkUniformHandler::kStd140Layout &&
174 (arrayCount || type == SkSLType::kFloat2x2 || type == SkSLType::kHalf2x2)) {
175 alignmentMask = 0xF;
176 }
177 uint32_t offsetDiff = *currentOffset & alignmentMask;
178 if (offsetDiff != 0) {
179 offsetDiff = alignmentMask - offsetDiff + 1;
180 }
181 int32_t uniformOffset = *currentOffset + offsetDiff;
182 SkASSERT(sizeof(float) == 4);
183 if (arrayCount) {
184 // TODO: this shouldn't be necessary for std430
185 uint32_t elementSize = std::max<uint32_t>(16, sksltype_to_vk_size(type, layout));
186 SkASSERT(0 == (elementSize & 0xF));
187 *currentOffset = uniformOffset + elementSize * arrayCount;
188 } else {
189 *currentOffset = uniformOffset + sksltype_to_vk_size(type, layout);
190 }
191 return uniformOffset;
192}
193
195 for (VkUniformInfo& sampler : fSamplers.items()) {
196 if (sampler.fImmutableSampler) {
197 sampler.fImmutableSampler->unref();
198 sampler.fImmutableSampler = nullptr;
199 }
200 }
201}
202
204 const GrProcessor* owner,
205 uint32_t visibility,
207 const char* name,
208 bool mangleName,
209 int arrayCount,
210 const char** outName) {
211 SkASSERT(name && strlen(name));
213
214 // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
215 // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
216 // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
217 // the names will mismatch. I think the correct solution is to have all GPs which need the
218 // uniform view matrix, they should upload the view matrix in their setData along with regular
219 // uniforms.
220 char prefix = 'u';
221 if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
222 prefix = '\0';
223 }
224 SkString resolvedName = fProgramBuilder->nameVariable(prefix, name, mangleName);
225
226 VkUniformInfo tempInfo;
227 tempInfo.fVariable = GrShaderVar{std::move(resolvedName),
228 type,
230 arrayCount};
231
232 tempInfo.fVisibility = visibility;
233 tempInfo.fOwner = owner;
234 tempInfo.fRawName = SkString(name);
235
236 for (int layout = 0; layout < kLayoutCount; ++layout) {
237 tempInfo.fOffsets[layout] = get_aligned_offset(&fCurrentOffsets[layout],
238 type,
239 arrayCount,
240 layout);
241 }
242
243 fUniforms.push_back(tempInfo);
244
245 if (outName) {
246 *outName = fUniforms.back().fVariable.c_str();
247 }
248
249 return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
250}
251
252GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(
253 const GrBackendFormat& backendFormat,
255 const skgpu::Swizzle& swizzle,
256 const char* name,
257 const GrShaderCaps* shaderCaps) {
258 SkASSERT(name && strlen(name));
259
260 const char prefix = 'u';
261 SkString mangleName = fProgramBuilder->nameVariable(prefix, name, /*mangle=*/true);
262
263 SkString layoutQualifier;
264 layoutQualifier.appendf("vulkan, set=%d, binding=%d", kSamplerDescSet, fSamplers.count());
265
266 VkUniformInfo tempInfo;
267 tempInfo.fVariable =
268 GrShaderVar{std::move(mangleName),
272 std::move(layoutQualifier),
273 SkString()};
274
276 tempInfo.fOwner = nullptr;
277 tempInfo.fRawName = SkString(name);
278 tempInfo.fOffsets[0] = 0;
279 tempInfo.fOffsets[1] = 0;
280
281 fSamplers.push_back(tempInfo);
282
283 // Check if we are dealing with an external texture and store the needed information if so.
284 auto ycbcrInfo = GrBackendFormats::GetVkYcbcrConversionInfo(backendFormat);
285 if (ycbcrInfo && ycbcrInfo->isValid()) {
286 GrVkGpu* gpu = static_cast<GrVkPipelineStateBuilder*>(fProgramBuilder)->gpu();
288 *ycbcrInfo);
289 fSamplers.back().fImmutableSampler = sampler;
290 if (!sampler) {
291 return {};
292 }
293 }
294
295 fSamplerSwizzles.push_back(swizzle);
296 SkASSERT(fSamplerSwizzles.size() == fSamplers.count());
297 return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
298}
299
300GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addInputSampler(
301 const skgpu::Swizzle& swizzle, const char* name) {
302 SkASSERT(name && strlen(name));
303 SkASSERT(fInputUniform.fVariable.getType() == SkSLType::kVoid);
304
305 const char prefix = 'u';
306 SkString mangleName = fProgramBuilder->nameVariable(prefix, name, /*mangle=*/true);
307
308 auto layoutQualifier = SkStringPrintf("vulkan, input_attachment_index=%d, set=%d, binding=%d",
312
313 fInputUniform = {GrShaderVar{std::move(mangleName),
317 std::move(layoutQualifier),
318 SkString()},
320 nullptr,
321 SkString(name)};
322 fInputSwizzle = swizzle;
323 return GrGLSLUniformHandler::SamplerHandle(0);
324}
325
327 for (const VkUniformInfo& sampler : fSamplers.items()) {
328 SkASSERT(sampler.fVariable.getType() == SkSLType::kTexture2DSampler ||
329 sampler.fVariable.getType() == SkSLType::kTextureExternalSampler);
330 if (visibility == sampler.fVisibility) {
331 sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
332 out->append(";\n");
333 }
334 }
335 if (fInputUniform.fVariable.getType() == SkSLType::kInput) {
336 if (visibility == fInputUniform.fVisibility) {
337 SkASSERT(visibility == kFragment_GrShaderFlag);
338 fInputUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
339 out->append(";\n");
340 }
341 }
342
343#ifdef SK_DEBUG
344 bool firstOffsetCheck = false;
345 for (const VkUniformInfo& localUniform : fUniforms.items()) {
346 if (!firstOffsetCheck) {
347 // Check to make sure we are starting our offset at 0 so the offset qualifier we
348 // set on each variable in the uniform block is valid.
349 SkASSERT(0 == localUniform.fOffsets[kStd140Layout] &&
350 0 == localUniform.fOffsets[kStd430Layout]);
351 firstOffsetCheck = true;
352 }
353 }
354#endif
355
356 // At this point we determine whether we'll be using push constants based on the
357 // uniforms set so far. Later checks will use the internal bool we set here to
358 // keep things consistent.
359 this->determineIfUsePushConstants();
360 SkString uniformsString;
361 for (const VkUniformInfo& localUniform : fUniforms.items()) {
362 if (visibility & localUniform.fVisibility) {
363 if (SkSLTypeCanBeUniformValue(localUniform.fVariable.getType())) {
364 Layout layout = fUsePushConstants ? kStd430Layout : kStd140Layout;
365 uniformsString.appendf("layout(offset=%u) ", localUniform.fOffsets[layout]);
366 localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
367 uniformsString.append(";\n");
368 }
369 }
370 }
371
372 if (!uniformsString.isEmpty()) {
373 if (fUsePushConstants) {
374 out->append("layout (vulkan, push_constant) ");
375 } else {
376 out->appendf("layout (vulkan, set=%d, binding=%d) ",
378 }
379 out->append("uniform uniformBuffer\n{\n");
380 out->appendf("%s\n};\n", uniformsString.c_str());
381 }
382}
383
385 Layout layout = fUsePushConstants ? kStd430Layout : kStd140Layout;
386 uint32_t currentOffset = fCurrentOffsets[layout];
388}
389
390void GrVkUniformHandler::determineIfUsePushConstants() const {
391 // We may insert a uniform for flipping origin-sensitive language features (e.g. sk_FragCoord).
392 // We won't know that for sure until then but we need to make this determination now,
393 // so assume we will need it.
394 static constexpr uint32_t kPad = 2*sizeof(float);
395 fUsePushConstants =
396 fCurrentOffsets[kStd430Layout] > 0 &&
397 fCurrentOffsets[kStd430Layout] + kPad <= fProgramBuilder->caps()->maxPushConstantsSize();
398}
#define GR_NO_MANGLE_PREFIX
GrShaderFlags
@ kFragment_GrShaderFlag
static SkSLType SkSLCombinedSamplerTypeForTextureType(GrTextureType type)
Definition GrUtil.h:50
static uint32_t sksltype_to_vk_size(SkSLType type, int layout)
static uint32_t sksltype_to_alignment_mask(SkSLType type)
static uint32_t get_aligned_offset(uint32_t *currentOffset, SkSLType type, int arrayCount, int layout)
#define SK_ABORT(message,...)
Definition SkAssert.h:70
#define SkASSERT(cond)
Definition SkAssert.h:116
static constexpr bool SkSLTypeCanBeUniformValue(SkSLType type)
SkSLType
@ kTextureExternalSampler
@ kTexture2DSampler
@ kTexture2DRectSampler
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
constexpr int kPad
GrTextureType textureType() const
uint32_t maxPushConstantsSize() const
Definition GrCaps.h:238
SkString nameVariable(char prefix, const char *name, bool mangle=true)
virtual const GrCaps * caps() const =0
const GrShaderCaps * shaderCaps() const
GrGLSLProgramBuilder * fProgramBuilder
GrGLSLProgramDataManager::UniformHandle UniformHandle
SkSLType getType() const
Definition GrShaderVar.h:97
const char * c_str() const
Definition GrShaderVar.h:94
void appendDecl(const GrShaderCaps *, SkString *out) const
GrVkResourceProvider & resourceProvider()
Definition GrVkGpu.h:83
GrVkSampler * findOrCreateCompatibleSampler(GrSamplerState, const GrVkYcbcrConversionInfo &ycbcrInfo)
uint32_t currentOffset() const
static constexpr int kLayoutCount
SamplerHandle addInputSampler(const skgpu::Swizzle &swizzle, const char *name) override
SamplerHandle addSampler(const GrBackendFormat &, GrSamplerState, const skgpu::Swizzle &, const char *name, const GrShaderCaps *) override
uint32_t getRTFlipOffset() const
void appendUniformDecls(GrShaderFlags, SkString *) const override
UniformHandle internalAddUniformArray(const GrProcessor *owner, uint32_t visibility, SkSLType type, const char *name, bool mangleName, int arrayCount, const char **outName) override
bool isEmpty() const
Definition SkString.h:130
void append(const char text[])
Definition SkString.h:203
const char * c_str() const
Definition SkString.h:133
void void void appendf(const char format[],...) SK_PRINTF_LIKE(2
Definition SkString.cpp:550
int count() const
int size() const
Definition SkTArray.h:416
AtkStateType state
const char * name
Definition fuchsia.cc:50
SK_API const GrVkYcbcrConversionInfo * GetVkYcbcrConversionInfo(const GrBackendFormat &)