OLD | NEW |
---|---|
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 | 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 | 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). | 16 // aligned to 16 bytes (i.e. has mask of 0xF). |
17 uint32_t grsltype_to_alignment_mask(GrSLType type) { | 17 uint32_t grsltype_to_alignment_mask(GrSLType type) { |
18 SkASSERT(GrSLTypeIsFloatType(type)); | 18 SkASSERT(GrSLTypeIsFloatType(type)); |
19 static const uint32_t kAlignments[kGrSLTypeCount] = { | 19 static const uint32_t kAlignments[kGrSLTypeCount] = { |
20 0x0, // kVoid_GrSLType, should never return this | 20 0x0, // kVoid_GrSLType, should never return this |
21 0x3, // kFloat_GrSLType | 21 0x3, // kFloat_GrSLType |
22 0x7, // kVec2f_GrSLType | 22 0x7, // kVec2f_GrSLType |
23 0xF, // kVec3f_GrSLType | 23 0xF, // kVec3f_GrSLType |
24 0xF, // kVec4f_GrSLType | 24 0xF, // kVec4f_GrSLType |
25 0xF, // kMat22f_GrSLType | |
egdaniel
2016/03/07 18:51:26
so this function technically returns things in std
egdaniel
2016/03/07 21:20:03
Can you just make this change 0x7
Chris Dalton
2016/03/07 22:17:12
Done.
| |
25 0xF, // kMat33f_GrSLType | 26 0xF, // kMat33f_GrSLType |
26 0xF, // kMat44f_GrSLType | 27 0xF, // kMat44f_GrSLType |
27 0x0, // Sampler2D_GrSLType, should never return this | 28 0x0, // Sampler2D_GrSLType, should never return this |
28 0x0, // SamplerExternal_GrSLType, should never return this | 29 0x0, // SamplerExternal_GrSLType, should never return this |
29 }; | 30 }; |
30 GR_STATIC_ASSERT(0 == kVoid_GrSLType); | 31 GR_STATIC_ASSERT(0 == kVoid_GrSLType); |
31 GR_STATIC_ASSERT(1 == kFloat_GrSLType); | 32 GR_STATIC_ASSERT(1 == kFloat_GrSLType); |
32 GR_STATIC_ASSERT(2 == kVec2f_GrSLType); | 33 GR_STATIC_ASSERT(2 == kVec2f_GrSLType); |
33 GR_STATIC_ASSERT(3 == kVec3f_GrSLType); | 34 GR_STATIC_ASSERT(3 == kVec3f_GrSLType); |
34 GR_STATIC_ASSERT(4 == kVec4f_GrSLType); | 35 GR_STATIC_ASSERT(4 == kVec4f_GrSLType); |
35 GR_STATIC_ASSERT(5 == kMat33f_GrSLType); | 36 GR_STATIC_ASSERT(5 == kMat22f_GrSLType); |
36 GR_STATIC_ASSERT(6 == kMat44f_GrSLType); | 37 GR_STATIC_ASSERT(6 == kMat33f_GrSLType); |
37 GR_STATIC_ASSERT(7 == kSampler2D_GrSLType); | 38 GR_STATIC_ASSERT(7 == kMat44f_GrSLType); |
38 GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType); | 39 GR_STATIC_ASSERT(8 == kSampler2D_GrSLType); |
40 GR_STATIC_ASSERT(9 == kSamplerExternal_GrSLType); | |
39 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kAlignments) == kGrSLTypeCount); | 41 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kAlignments) == kGrSLTypeCount); |
40 return kAlignments[type]; | 42 return kAlignments[type]; |
41 } | 43 } |
42 | 44 |
43 /** Returns the size in bytes taken up in vulkanbuffers for floating point GrSLT ypes. | 45 /** Returns the size in bytes taken up in vulkanbuffers for floating point GrSLT ypes. |
44 For non floating point type returns 0 */ | 46 For non floating point type returns 0 */ |
45 static inline uint32_t grsltype_to_vk_size(GrSLType type) { | 47 static inline uint32_t grsltype_to_vk_size(GrSLType type) { |
46 SkASSERT(GrSLTypeIsFloatType(type)); | 48 SkASSERT(GrSLTypeIsFloatType(type)); |
47 static const uint32_t kSizes[] = { | 49 static const uint32_t kSizes[] = { |
48 0, // kVoid_GrSLType | 50 0, // kVoid_GrSLType |
49 sizeof(float), // kFloat_GrSLType | 51 sizeof(float), // kFloat_GrSLType |
50 2 * sizeof(float), // kVec2f_GrSLType | 52 2 * sizeof(float), // kVec2f_GrSLType |
51 3 * sizeof(float), // kVec3f_GrSLType | 53 3 * sizeof(float), // kVec3f_GrSLType |
52 4 * sizeof(float), // kVec4f_GrSLType | 54 4 * sizeof(float), // kVec4f_GrSLType |
55 8 * sizeof(float), // kMat22f_GrSLType | |
egdaniel
2016/03/07 18:51:26
This function may need some reworking since we hav
egdaniel
2016/03/07 21:20:03
Just add a comment after Mat22f_GrSLType saying so
Chris Dalton
2016/03/07 22:17:12
Done.
| |
53 12 * sizeof(float), // kMat33f_GrSLType | 56 12 * sizeof(float), // kMat33f_GrSLType |
54 16 * sizeof(float), // kMat44f_GrSLType | 57 16 * sizeof(float), // kMat44f_GrSLType |
55 0, // kSampler2D_GrSLType | 58 0, // kSampler2D_GrSLType |
56 0, // kSamplerExternal_GrSLType | 59 0, // kSamplerExternal_GrSLType |
57 0, // kSampler2DRect_GrSLType | 60 0, // kSampler2DRect_GrSLType |
58 0, // kBool_GrSLType | 61 0, // kBool_GrSLType |
59 0, // kInt_GrSLType | 62 0, // kInt_GrSLType |
60 0, // kUint_GrSLType | 63 0, // kUint_GrSLType |
61 }; | 64 }; |
62 return kSizes[type]; | 65 return kSizes[type]; |
63 | 66 |
64 GR_STATIC_ASSERT(0 == kVoid_GrSLType); | 67 GR_STATIC_ASSERT(0 == kVoid_GrSLType); |
65 GR_STATIC_ASSERT(1 == kFloat_GrSLType); | 68 GR_STATIC_ASSERT(1 == kFloat_GrSLType); |
66 GR_STATIC_ASSERT(2 == kVec2f_GrSLType); | 69 GR_STATIC_ASSERT(2 == kVec2f_GrSLType); |
67 GR_STATIC_ASSERT(3 == kVec3f_GrSLType); | 70 GR_STATIC_ASSERT(3 == kVec3f_GrSLType); |
68 GR_STATIC_ASSERT(4 == kVec4f_GrSLType); | 71 GR_STATIC_ASSERT(4 == kVec4f_GrSLType); |
69 GR_STATIC_ASSERT(5 == kMat33f_GrSLType); | 72 GR_STATIC_ASSERT(5 == kMat22f_GrSLType); |
70 GR_STATIC_ASSERT(6 == kMat44f_GrSLType); | 73 GR_STATIC_ASSERT(6 == kMat33f_GrSLType); |
71 GR_STATIC_ASSERT(7 == kSampler2D_GrSLType); | 74 GR_STATIC_ASSERT(7 == kMat44f_GrSLType); |
72 GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType); | 75 GR_STATIC_ASSERT(8 == kSampler2D_GrSLType); |
73 GR_STATIC_ASSERT(9 == kSampler2DRect_GrSLType); | 76 GR_STATIC_ASSERT(9 == kSamplerExternal_GrSLType); |
74 GR_STATIC_ASSERT(10 == kBool_GrSLType); | 77 GR_STATIC_ASSERT(10 == kSampler2DRect_GrSLType); |
75 GR_STATIC_ASSERT(11 == kInt_GrSLType); | 78 GR_STATIC_ASSERT(11 == kBool_GrSLType); |
76 GR_STATIC_ASSERT(12 == kUint_GrSLType); | 79 GR_STATIC_ASSERT(12 == kInt_GrSLType); |
77 GR_STATIC_ASSERT(13 == kGrSLTypeCount); | 80 GR_STATIC_ASSERT(13 == kUint_GrSLType); |
81 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kSizes) == kGrSLTypeCount); | |
78 } | 82 } |
79 | 83 |
80 | 84 |
81 // Given the current offset into the ubo, calculate the offset for the uniform w e're trying to add | 85 // Given the current offset into the ubo, calculate the offset for the uniform w e're trying to add |
82 // taking into consideration all alignment requirements. The uniformOffset is se t to the offset for | 86 // taking into consideration all alignment requirements. The uniformOffset is se t to the offset for |
83 // the new uniform, and currentOffset is updated to be the offset to the end of the new uniform. | 87 // the new uniform, and currentOffset is updated to be the offset to the end of the new uniform. |
84 void get_ubo_aligned_offset(uint32_t* uniformOffset, | 88 void get_ubo_aligned_offset(uint32_t* uniformOffset, |
85 uint32_t* currentOffset, | 89 uint32_t* currentOffset, |
86 GrSLType type, | 90 GrSLType type, |
87 int arrayCount) { | 91 int arrayCount) { |
88 uint32_t alignmentMask = grsltype_to_alignment_mask(type); | 92 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. | 93 // We want to use the std140 layout here, so we must make arrays align to 16 bytes. |
90 if (arrayCount) { | 94 if (arrayCount) { |
egdaniel
2016/03/07 18:51:26
we could make this if (arrayCount || Mat22 == type
egdaniel
2016/03/07 21:20:03
Add assert in this function the type != Mat22
Chris Dalton
2016/03/07 22:17:12
Done.
| |
91 alignmentMask = 0xF; | 95 alignmentMask = 0xF; |
92 } | 96 } |
93 uint32_t offsetDiff = *currentOffset & alignmentMask; | 97 uint32_t offsetDiff = *currentOffset & alignmentMask; |
94 if (offsetDiff != 0) { | 98 if (offsetDiff != 0) { |
95 offsetDiff = alignmentMask - offsetDiff + 1; | 99 offsetDiff = alignmentMask - offsetDiff + 1; |
96 } | 100 } |
97 *uniformOffset = *currentOffset + offsetDiff; | 101 *uniformOffset = *currentOffset + offsetDiff; |
98 SkASSERT(sizeof(float) == 4); | 102 SkASSERT(sizeof(float) == 4); |
99 if (arrayCount) { | 103 if (arrayCount) { |
100 uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type)); | 104 uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type)); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 } | 193 } |
190 } | 194 } |
191 if (!uniformsString.isEmpty()) { | 195 if (!uniformsString.isEmpty()) { |
192 const char* stage = (visibility == kVertex_GrShaderFlag) ? "vertex" : "f ragment"; | 196 const char* stage = (visibility == kVertex_GrShaderFlag) ? "vertex" : "f ragment"; |
193 out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n", | 197 out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n", |
194 kUniformBufferDescSet, uniformBinding, stage); | 198 kUniformBufferDescSet, uniformBinding, stage); |
195 out->appendf("%s\n};\n", uniformsString.c_str()); | 199 out->appendf("%s\n};\n", uniformsString.c_str()); |
196 } | 200 } |
197 } | 201 } |
198 | 202 |
OLD | NEW |