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 kAlignmentMask[] = { |
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 0x7, // kMat22f_GrSLType | 25 0x7, // kMat22f_GrSLType |
26 0xF, // kMat33f_GrSLType | 26 0xF, // kMat33f_GrSLType |
27 0xF, // kMat44f_GrSLType | 27 0xF, // kMat44f_GrSLType |
28 0x0, // Sampler2D_GrSLType, should never return this | 28 0x0, // Sampler2D_GrSLType, should never return this |
29 0x0, // SamplerExternal_GrSLType, should never return this | 29 0x0, // SamplerExternal_GrSLType, should never return this |
| 30 0x0, // Sampler2DRect_GrSLType, should never return this |
| 31 0x0, // SamplerBuffer_GrSLType, should never return this |
| 32 0x0, // kBool_GrSLType |
| 33 0x7, // kInt_GrSLType |
| 34 0x7, // kUint_GrSLType |
30 }; | 35 }; |
31 GR_STATIC_ASSERT(0 == kVoid_GrSLType); | 36 GR_STATIC_ASSERT(0 == kVoid_GrSLType); |
32 GR_STATIC_ASSERT(1 == kFloat_GrSLType); | 37 GR_STATIC_ASSERT(1 == kFloat_GrSLType); |
33 GR_STATIC_ASSERT(2 == kVec2f_GrSLType); | 38 GR_STATIC_ASSERT(2 == kVec2f_GrSLType); |
34 GR_STATIC_ASSERT(3 == kVec3f_GrSLType); | 39 GR_STATIC_ASSERT(3 == kVec3f_GrSLType); |
35 GR_STATIC_ASSERT(4 == kVec4f_GrSLType); | 40 GR_STATIC_ASSERT(4 == kVec4f_GrSLType); |
36 GR_STATIC_ASSERT(5 == kMat22f_GrSLType); | 41 GR_STATIC_ASSERT(5 == kMat22f_GrSLType); |
37 GR_STATIC_ASSERT(6 == kMat33f_GrSLType); | 42 GR_STATIC_ASSERT(6 == kMat33f_GrSLType); |
38 GR_STATIC_ASSERT(7 == kMat44f_GrSLType); | 43 GR_STATIC_ASSERT(7 == kMat44f_GrSLType); |
39 GR_STATIC_ASSERT(8 == kSampler2D_GrSLType); | 44 GR_STATIC_ASSERT(8 == kSampler2D_GrSLType); |
40 GR_STATIC_ASSERT(9 == kSamplerExternal_GrSLType); | 45 GR_STATIC_ASSERT(9 == kSamplerExternal_GrSLType); |
41 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kAlignments) == kGrSLTypeCount); | 46 GR_STATIC_ASSERT(10 == kSampler2DRect_GrSLType); |
42 return kAlignments[type]; | 47 GR_STATIC_ASSERT(11 == kSamplerBuffer_GrSLType); |
| 48 GR_STATIC_ASSERT(12 == kBool_GrSLType); |
| 49 GR_STATIC_ASSERT(13 == kInt_GrSLType); |
| 50 GR_STATIC_ASSERT(14 == kUint_GrSLType); |
| 51 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kAlignmentMask) == kGrSLTypeCount); |
| 52 return kAlignmentMask[type]; |
43 } | 53 } |
44 | 54 |
45 /** Returns the size in bytes taken up in vulkanbuffers for floating point GrSLT
ypes. | 55 /** Returns the size in bytes taken up in vulkanbuffers for floating point GrSLT
ypes. |
46 For non floating point type returns 0 */ | 56 For non floating point type returns 0 */ |
47 static inline uint32_t grsltype_to_vk_size(GrSLType type) { | 57 static inline uint32_t grsltype_to_vk_size(GrSLType type) { |
48 SkASSERT(GrSLTypeIsFloatType(type)); | 58 SkASSERT(GrSLTypeIsFloatType(type)); |
49 SkASSERT(kMat22f_GrSLType != type); // TODO: handle mat2 differences between
std140 and std430. | 59 SkASSERT(kMat22f_GrSLType != type); // TODO: handle mat2 differences between
std140 and std430. |
50 static const uint32_t kSizes[] = { | 60 static const uint32_t kSizes[] = { |
51 0, // kVoid_GrSLType | 61 0, // kVoid_GrSLType |
52 sizeof(float), // kFloat_GrSLType | 62 sizeof(float), // kFloat_GrSLType |
53 2 * sizeof(float), // kVec2f_GrSLType | 63 2 * sizeof(float), // kVec2f_GrSLType |
54 3 * sizeof(float), // kVec3f_GrSLType | 64 3 * sizeof(float), // kVec3f_GrSLType |
55 4 * sizeof(float), // kVec4f_GrSLType | 65 4 * sizeof(float), // kVec4f_GrSLType |
56 8 * sizeof(float), // kMat22f_GrSLType. TODO: this will be 4 * sz
of(float) on std430. | 66 8 * sizeof(float), // kMat22f_GrSLType. TODO: this will be 4 * sz
of(float) on std430. |
57 12 * sizeof(float), // kMat33f_GrSLType | 67 12 * sizeof(float), // kMat33f_GrSLType |
58 16 * sizeof(float), // kMat44f_GrSLType | 68 16 * sizeof(float), // kMat44f_GrSLType |
59 0, // kSampler2D_GrSLType | 69 0, // kSampler2D_GrSLType |
60 0, // kSamplerExternal_GrSLType | 70 0, // kSamplerExternal_GrSLType |
61 0, // kSampler2DRect_GrSLType | 71 0, // kSampler2DRect_GrSLType |
62 0, // kBool_GrSLType | 72 0, // kSamplerBuffer_GrSLType |
63 0, // kInt_GrSLType | 73 1, // kBool_GrSLType |
64 0, // kUint_GrSLType | 74 4, // kInt_GrSLType |
| 75 4 // kUint_GrSLType |
65 }; | 76 }; |
66 return kSizes[type]; | 77 return kSizes[type]; |
67 | 78 |
68 GR_STATIC_ASSERT(0 == kVoid_GrSLType); | 79 GR_STATIC_ASSERT(0 == kVoid_GrSLType); |
69 GR_STATIC_ASSERT(1 == kFloat_GrSLType); | 80 GR_STATIC_ASSERT(1 == kFloat_GrSLType); |
70 GR_STATIC_ASSERT(2 == kVec2f_GrSLType); | 81 GR_STATIC_ASSERT(2 == kVec2f_GrSLType); |
71 GR_STATIC_ASSERT(3 == kVec3f_GrSLType); | 82 GR_STATIC_ASSERT(3 == kVec3f_GrSLType); |
72 GR_STATIC_ASSERT(4 == kVec4f_GrSLType); | 83 GR_STATIC_ASSERT(4 == kVec4f_GrSLType); |
73 GR_STATIC_ASSERT(5 == kMat22f_GrSLType); | 84 GR_STATIC_ASSERT(5 == kMat22f_GrSLType); |
74 GR_STATIC_ASSERT(6 == kMat33f_GrSLType); | 85 GR_STATIC_ASSERT(6 == kMat33f_GrSLType); |
75 GR_STATIC_ASSERT(7 == kMat44f_GrSLType); | 86 GR_STATIC_ASSERT(7 == kMat44f_GrSLType); |
76 GR_STATIC_ASSERT(8 == kSampler2D_GrSLType); | 87 GR_STATIC_ASSERT(8 == kSampler2D_GrSLType); |
77 GR_STATIC_ASSERT(9 == kSamplerExternal_GrSLType); | 88 GR_STATIC_ASSERT(9 == kSamplerExternal_GrSLType); |
78 GR_STATIC_ASSERT(10 == kSampler2DRect_GrSLType); | 89 GR_STATIC_ASSERT(10 == kSampler2DRect_GrSLType); |
79 GR_STATIC_ASSERT(11 == kBool_GrSLType); | 90 GR_STATIC_ASSERT(11 == kSamplerBuffer_GrSLType); |
80 GR_STATIC_ASSERT(12 == kInt_GrSLType); | 91 GR_STATIC_ASSERT(12 == kBool_GrSLType); |
81 GR_STATIC_ASSERT(13 == kUint_GrSLType); | 92 GR_STATIC_ASSERT(13 == kInt_GrSLType); |
| 93 GR_STATIC_ASSERT(14 == kUint_GrSLType); |
82 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kSizes) == kGrSLTypeCount); | 94 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kSizes) == kGrSLTypeCount); |
83 } | 95 } |
84 | 96 |
85 | 97 |
86 // Given the current offset into the ubo, calculate the offset for the uniform w
e're trying to add | 98 // Given the current offset into the ubo, calculate the offset for the uniform w
e're trying to add |
87 // taking into consideration all alignment requirements. The uniformOffset is se
t to the offset for | 99 // taking into consideration all alignment requirements. The uniformOffset is se
t to the offset for |
88 // the new uniform, and currentOffset is updated to be the offset to the end of
the new uniform. | 100 // the new uniform, and currentOffset is updated to be the offset to the end of
the new uniform. |
89 void get_ubo_aligned_offset(uint32_t* uniformOffset, | 101 void get_ubo_aligned_offset(uint32_t* uniformOffset, |
90 uint32_t* currentOffset, | 102 uint32_t* currentOffset, |
91 GrSLType type, | 103 GrSLType type, |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 } | 206 } |
195 } | 207 } |
196 } | 208 } |
197 if (!uniformsString.isEmpty()) { | 209 if (!uniformsString.isEmpty()) { |
198 const char* stage = (visibility == kVertex_GrShaderFlag) ? "vertex" : "f
ragment"; | 210 const char* stage = (visibility == kVertex_GrShaderFlag) ? "vertex" : "f
ragment"; |
199 out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n", | 211 out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n", |
200 kUniformBufferDescSet, uniformBinding, stage); | 212 kUniformBufferDescSet, uniformBinding, stage); |
201 out->appendf("%s\n};\n", uniformsString.c_str()); | 213 out->appendf("%s\n};\n", uniformsString.c_str()); |
202 } | 214 } |
203 } | 215 } |
OLD | NEW |