Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(740)

Side by Side Diff: src/gpu/vk/GrVkUniformHandler.cpp

Issue 1734403002: Fix uniform buffer layout in vulkan (Closed) Base URL: https://skia.googlesource.com/skia.git@addFileBack
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/gpu/vk/GrVkProgramDataManager.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "GrVkUniformHandler.h" 8 #include "GrVkUniformHandler.h"
9 #include "glsl/GrGLSLProgramBuilder.h" 9 #include "glsl/GrGLSLProgramBuilder.h"
10 10
11 // To determine whether a current offset is aligned, we can just 'and' the lowes t bits with the 11 // To determine whether a current offset is aligned, we can just 'and' the lowes t bits with the
12 // alignment mask. A value of 0 means aligned, any other value is how many bytes past alignment we 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 (ali gnment - 1). 13 // are. This works since all alignments are powers of 2. The mask is always (ali gnment - 1).
14 // This alignment mask will give correct alignments for using the std430 block l ayout. If you want
15 // the std140 alignment, you can use this, but then make sure if you have an arr ay type it is
16 // aligned to 16 bytes (i.e. has mask of 0xF).
14 uint32_t grsltype_to_alignment_mask(GrSLType type) { 17 uint32_t grsltype_to_alignment_mask(GrSLType type) {
15 SkASSERT(GrSLTypeIsFloatType(type)); 18 SkASSERT(GrSLTypeIsFloatType(type));
16 static const uint32_t kAlignments[kGrSLTypeCount] = { 19 static const uint32_t kAlignments[kGrSLTypeCount] = {
17 0x0, // kVoid_GrSLType, should never return this 20 0x0, // kVoid_GrSLType, should never return this
18 0x3, // kFloat_GrSLType 21 0x3, // kFloat_GrSLType
19 0x7, // kVec2f_GrSLType 22 0x7, // kVec2f_GrSLType
20 0xF, // kVec3f_GrSLType 23 0xF, // kVec3f_GrSLType
21 0xF, // kVec4f_GrSLType 24 0xF, // kVec4f_GrSLType
22 0xF, // kMat33f_GrSLType 25 0xF, // kMat33f_GrSLType
23 0xF, // kMat44f_GrSLType 26 0xF, // kMat44f_GrSLType
24 0x0, // Sampler2D_GrSLType, should never return this 27 0x0, // Sampler2D_GrSLType, should never return this
25 0x0, // SamplerExternal_GrSLType, should never return this 28 0x0, // SamplerExternal_GrSLType, should never return this
26 }; 29 };
27 GR_STATIC_ASSERT(0 == kVoid_GrSLType); 30 GR_STATIC_ASSERT(0 == kVoid_GrSLType);
28 GR_STATIC_ASSERT(1 == kFloat_GrSLType); 31 GR_STATIC_ASSERT(1 == kFloat_GrSLType);
29 GR_STATIC_ASSERT(2 == kVec2f_GrSLType); 32 GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
30 GR_STATIC_ASSERT(3 == kVec3f_GrSLType); 33 GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
31 GR_STATIC_ASSERT(4 == kVec4f_GrSLType); 34 GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
32 GR_STATIC_ASSERT(5 == kMat33f_GrSLType); 35 GR_STATIC_ASSERT(5 == kMat33f_GrSLType);
33 GR_STATIC_ASSERT(6 == kMat44f_GrSLType); 36 GR_STATIC_ASSERT(6 == kMat44f_GrSLType);
34 GR_STATIC_ASSERT(7 == kSampler2D_GrSLType); 37 GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
35 GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType); 38 GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType);
36 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kAlignments) == kGrSLTypeCount); 39 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kAlignments) == kGrSLTypeCount);
37 return kAlignments[type]; 40 return kAlignments[type];
38 } 41 }
39 42
43 /** Returns the size in bytes taken up in vulkanbuffers for floating point GrSLT ypes.
44 For non floating point type returns 0 */
45 static inline uint32_t grsltype_to_vk_size(GrSLType type) {
46 SkASSERT(GrSLTypeIsFloatType(type));
47 static const uint32_t kSizes[] = {
48 0, // kVoid_GrSLType
49 sizeof(float), // kFloat_GrSLType
50 2 * sizeof(float), // kVec2f_GrSLType
51 3 * sizeof(float), // kVec3f_GrSLType
52 4 * sizeof(float), // kVec4f_GrSLType
53 12 * sizeof(float), // kMat33f_GrSLType
54 16 * sizeof(float), // kMat44f_GrSLType
55 0, // kSampler2D_GrSLType
56 0, // kSamplerExternal_GrSLType
57 0, // kSampler2DRect_GrSLType
58 0, // kBool_GrSLType
59 0, // kInt_GrSLType
60 0, // kUint_GrSLType
61 };
62 return kSizes[type];
63
64 GR_STATIC_ASSERT(0 == kVoid_GrSLType);
65 GR_STATIC_ASSERT(1 == kFloat_GrSLType);
66 GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
67 GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
68 GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
69 GR_STATIC_ASSERT(5 == kMat33f_GrSLType);
70 GR_STATIC_ASSERT(6 == kMat44f_GrSLType);
71 GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
72 GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType);
73 GR_STATIC_ASSERT(9 == kSampler2DRect_GrSLType);
74 GR_STATIC_ASSERT(10 == kBool_GrSLType);
75 GR_STATIC_ASSERT(11 == kInt_GrSLType);
76 GR_STATIC_ASSERT(12 == kUint_GrSLType);
77 GR_STATIC_ASSERT(13 == kGrSLTypeCount);
78 }
79
80
40 // Given the current offset into the ubo, calculate the offset for the uniform w e're trying to add 81 // Given the current offset into the ubo, calculate the offset for the uniform w e're trying to add
41 // taking into consideration all alignment requirements. The uniformOffset is se t to the offset for 82 // taking into consideration all alignment requirements. The uniformOffset is se t to the offset for
42 // the new uniform, and currentOffset is updated to be the offset to the end of the new uniform. 83 // the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
43 void get_ubo_aligned_offset(uint32_t* uniformOffset, 84 void get_ubo_aligned_offset(uint32_t* uniformOffset,
44 uint32_t* currentOffset, 85 uint32_t* currentOffset,
45 GrSLType type, 86 GrSLType type,
46 int arrayCount) { 87 int arrayCount) {
47 uint32_t alignmentMask = grsltype_to_alignment_mask(type); 88 uint32_t alignmentMask = grsltype_to_alignment_mask(type);
89 // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
90 if (arrayCount) {
91 alignmentMask = 0xF;
92 }
48 uint32_t offsetDiff = *currentOffset & alignmentMask; 93 uint32_t offsetDiff = *currentOffset & alignmentMask;
49 if (offsetDiff != 0) { 94 if (offsetDiff != 0) {
50 offsetDiff = alignmentMask - offsetDiff + 1; 95 offsetDiff = alignmentMask - offsetDiff + 1;
51 } 96 }
52 *uniformOffset = *currentOffset + offsetDiff; 97 *uniformOffset = *currentOffset + offsetDiff;
53 SkASSERT(sizeof(float) == 4); 98 SkASSERT(sizeof(float) == 4);
54 // We use a 0 arrayCount to indicate it is not an array type but we still ne ed to count the one 99 if (arrayCount) {
55 // object. 100 uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type));
56 int count = arrayCount ? arrayCount : 1; 101 SkASSERT(0 == (elementSize & 0xF));
57 *currentOffset = *uniformOffset + count * (uint32_t)GrSLTypeSize(type); 102 *currentOffset = *uniformOffset + elementSize * arrayCount;
103 } else {
104 *currentOffset = *uniformOffset + grsltype_to_vk_size(type);
105 }
58 } 106 }
59 107
60 GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray( 108 GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
61 uint 32_t visibility, 109 uint 32_t visibility,
62 GrSL Type type, 110 GrSL Type type,
63 GrSL Precision precision, 111 GrSL Precision precision,
64 cons t char* name, 112 cons t char* name,
65 bool mangleName, 113 bool mangleName,
66 int arrayCount, 114 int arrayCount,
67 cons t char** outName) { 115 cons t char** outName) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 out->append(";\n"); 187 out->append(";\n");
140 } 188 }
141 } 189 }
142 } 190 }
143 if (!uniformsString.isEmpty()) { 191 if (!uniformsString.isEmpty()) {
144 const char* stage = (visibility == kVertex_GrShaderFlag) ? "vertex" : "f ragment"; 192 const char* stage = (visibility == kVertex_GrShaderFlag) ? "vertex" : "f ragment";
145 out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n", 193 out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n",
146 kUniformBufferDescSet, uniformBinding, stage); 194 kUniformBufferDescSet, uniformBinding, stage);
147 out->appendf("%s\n};\n", uniformsString.c_str()); 195 out->appendf("%s\n};\n", uniformsString.c_str());
148 } 196 }
149 } 197 }
198
OLDNEW
« no previous file with comments | « src/gpu/vk/GrVkProgramDataManager.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698