Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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 | |
| 8 | #include "GrVkUniformHandler.h" |
| 9 | #include "glsl/GrGLSLProgramBuilder.h" |
| 10 | |
| 11 | // To determine whether a current offset is aligned, we can just 'and' the lowest bits with the |
| 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 (alignment - 1). |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 14 | // This alignment mask will give correct alignments for using the std430 block layout. If you want |
| 15 | // the std140 alignment, you can use this, but then make sure if you have an array type it is |
| 16 | // aligned to 16 bytes (i.e. has mask of 0xF). |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 17 | uint32_t grsltype_to_alignment_mask(GrSLType type) { |
| 18 | SkASSERT(GrSLTypeIsFloatType(type)); |
| 19 | static const uint32_t kAlignments[kGrSLTypeCount] = { |
| 20 | 0x0, // kVoid_GrSLType, should never return this |
| 21 | 0x3, // kFloat_GrSLType |
| 22 | 0x7, // kVec2f_GrSLType |
| 23 | 0xF, // kVec3f_GrSLType |
| 24 | 0xF, // kVec4f_GrSLType |
cdalton | 8d988b3 | 2016-03-07 15:39:09 -0800 | [diff] [blame^] | 25 | 0x7, // kMat22f_GrSLType |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 26 | 0xF, // kMat33f_GrSLType |
| 27 | 0xF, // kMat44f_GrSLType |
| 28 | 0x0, // Sampler2D_GrSLType, should never return this |
| 29 | 0x0, // SamplerExternal_GrSLType, should never return this |
| 30 | }; |
| 31 | GR_STATIC_ASSERT(0 == kVoid_GrSLType); |
| 32 | GR_STATIC_ASSERT(1 == kFloat_GrSLType); |
| 33 | GR_STATIC_ASSERT(2 == kVec2f_GrSLType); |
| 34 | GR_STATIC_ASSERT(3 == kVec3f_GrSLType); |
| 35 | GR_STATIC_ASSERT(4 == kVec4f_GrSLType); |
cdalton | 8d988b3 | 2016-03-07 15:39:09 -0800 | [diff] [blame^] | 36 | GR_STATIC_ASSERT(5 == kMat22f_GrSLType); |
| 37 | GR_STATIC_ASSERT(6 == kMat33f_GrSLType); |
| 38 | GR_STATIC_ASSERT(7 == kMat44f_GrSLType); |
| 39 | GR_STATIC_ASSERT(8 == kSampler2D_GrSLType); |
| 40 | GR_STATIC_ASSERT(9 == kSamplerExternal_GrSLType); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 41 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kAlignments) == kGrSLTypeCount); |
| 42 | return kAlignments[type]; |
| 43 | } |
| 44 | |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 45 | /** Returns the size in bytes taken up in vulkanbuffers for floating point GrSLTypes. |
| 46 | For non floating point type returns 0 */ |
| 47 | static inline uint32_t grsltype_to_vk_size(GrSLType type) { |
| 48 | SkASSERT(GrSLTypeIsFloatType(type)); |
cdalton | 8d988b3 | 2016-03-07 15:39:09 -0800 | [diff] [blame^] | 49 | SkASSERT(kMat22f_GrSLType != type); // TODO: handle mat2 differences between std140 and std430. |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 50 | static const uint32_t kSizes[] = { |
| 51 | 0, // kVoid_GrSLType |
| 52 | sizeof(float), // kFloat_GrSLType |
| 53 | 2 * sizeof(float), // kVec2f_GrSLType |
| 54 | 3 * sizeof(float), // kVec3f_GrSLType |
| 55 | 4 * sizeof(float), // kVec4f_GrSLType |
cdalton | 8d988b3 | 2016-03-07 15:39:09 -0800 | [diff] [blame^] | 56 | 8 * sizeof(float), // kMat22f_GrSLType. TODO: this will be 4 * szof(float) on std430. |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 57 | 12 * sizeof(float), // kMat33f_GrSLType |
| 58 | 16 * sizeof(float), // kMat44f_GrSLType |
| 59 | 0, // kSampler2D_GrSLType |
| 60 | 0, // kSamplerExternal_GrSLType |
| 61 | 0, // kSampler2DRect_GrSLType |
| 62 | 0, // kBool_GrSLType |
| 63 | 0, // kInt_GrSLType |
| 64 | 0, // kUint_GrSLType |
| 65 | }; |
| 66 | return kSizes[type]; |
| 67 | |
| 68 | GR_STATIC_ASSERT(0 == kVoid_GrSLType); |
| 69 | GR_STATIC_ASSERT(1 == kFloat_GrSLType); |
| 70 | GR_STATIC_ASSERT(2 == kVec2f_GrSLType); |
| 71 | GR_STATIC_ASSERT(3 == kVec3f_GrSLType); |
| 72 | GR_STATIC_ASSERT(4 == kVec4f_GrSLType); |
cdalton | 8d988b3 | 2016-03-07 15:39:09 -0800 | [diff] [blame^] | 73 | GR_STATIC_ASSERT(5 == kMat22f_GrSLType); |
| 74 | GR_STATIC_ASSERT(6 == kMat33f_GrSLType); |
| 75 | GR_STATIC_ASSERT(7 == kMat44f_GrSLType); |
| 76 | GR_STATIC_ASSERT(8 == kSampler2D_GrSLType); |
| 77 | GR_STATIC_ASSERT(9 == kSamplerExternal_GrSLType); |
| 78 | GR_STATIC_ASSERT(10 == kSampler2DRect_GrSLType); |
| 79 | GR_STATIC_ASSERT(11 == kBool_GrSLType); |
| 80 | GR_STATIC_ASSERT(12 == kInt_GrSLType); |
| 81 | GR_STATIC_ASSERT(13 == kUint_GrSLType); |
| 82 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kSizes) == kGrSLTypeCount); |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 86 | // Given the current offset into the ubo, calculate the offset for the uniform we're trying to add |
| 87 | // taking into consideration all alignment requirements. The uniformOffset is set to the offset for |
| 88 | // 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, |
| 90 | uint32_t* currentOffset, |
| 91 | GrSLType type, |
| 92 | int arrayCount) { |
| 93 | uint32_t alignmentMask = grsltype_to_alignment_mask(type); |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 94 | // We want to use the std140 layout here, so we must make arrays align to 16 bytes. |
cdalton | 8d988b3 | 2016-03-07 15:39:09 -0800 | [diff] [blame^] | 95 | SkASSERT(type != kMat22f_GrSLType); // TODO: support mat2. |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 96 | if (arrayCount) { |
| 97 | alignmentMask = 0xF; |
| 98 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 99 | uint32_t offsetDiff = *currentOffset & alignmentMask; |
| 100 | if (offsetDiff != 0) { |
| 101 | offsetDiff = alignmentMask - offsetDiff + 1; |
| 102 | } |
| 103 | *uniformOffset = *currentOffset + offsetDiff; |
| 104 | SkASSERT(sizeof(float) == 4); |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 105 | if (arrayCount) { |
| 106 | uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type)); |
| 107 | SkASSERT(0 == (elementSize & 0xF)); |
| 108 | *currentOffset = *uniformOffset + elementSize * arrayCount; |
| 109 | } else { |
| 110 | *currentOffset = *uniformOffset + grsltype_to_vk_size(type); |
| 111 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray( |
| 115 | uint32_t visibility, |
| 116 | GrSLType type, |
| 117 | GrSLPrecision precision, |
| 118 | const char* name, |
| 119 | bool mangleName, |
| 120 | int arrayCount, |
| 121 | const char** outName) { |
| 122 | SkASSERT(name && strlen(name)); |
| 123 | SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_GrShaderFlag|kFragment_GrShaderFlag); |
| 124 | SkASSERT(0 == (~kVisibilityMask & visibility)); |
| 125 | SkASSERT(0 != visibility); |
| 126 | SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); |
| 127 | |
| 128 | UniformInfo& uni = fUniforms.push_back(); |
| 129 | uni.fVariable.setType(type); |
| 130 | // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use |
| 131 | // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB |
| 132 | // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then |
| 133 | // the names will mismatch. I think the correct solution is to have all GPs which need the |
| 134 | // uniform view matrix, they should upload the view matrix in their setData along with regular |
| 135 | // uniforms. |
| 136 | char prefix = 'u'; |
| 137 | if ('u' == name[0]) { |
| 138 | prefix = '\0'; |
| 139 | } |
| 140 | fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName); |
| 141 | uni.fVariable.setArrayCount(arrayCount); |
| 142 | // For now asserting the the visibility is either only vertex or only fragment |
| 143 | SkASSERT(kVertex_GrShaderFlag == visibility || kFragment_GrShaderFlag == visibility); |
| 144 | uni.fVisibility = visibility; |
| 145 | uni.fVariable.setPrecision(precision); |
| 146 | if (GrSLTypeIsFloatType(type)) { |
| 147 | // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus |
| 148 | // we set the modifier to none for all uniforms declared inside the block. |
| 149 | uni.fVariable.setTypeModifier(GrGLSLShaderVar::kNone_TypeModifier); |
| 150 | |
| 151 | uint32_t* currentOffset = kVertex_GrShaderFlag == visibility ? &fCurrentVertexUBOOffset |
| 152 | : &fCurrentFragmentUBOOffset; |
| 153 | get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount); |
| 154 | uni.fSetNumber = kUniformBufferDescSet; |
| 155 | uni.fBinding = kVertex_GrShaderFlag == visibility ? kVertexBinding : kFragBinding; |
| 156 | |
| 157 | if (outName) { |
| 158 | *outName = uni.fVariable.c_str(); |
| 159 | } |
| 160 | } else { |
| 161 | SkASSERT(type == kSampler2D_GrSLType); |
| 162 | uni.fVariable.setTypeModifier(GrGLSLShaderVar::kUniform_TypeModifier); |
| 163 | |
| 164 | uni.fSetNumber = kSamplerDescSet; |
| 165 | uni.fBinding = fCurrentSamplerBinding++; |
| 166 | uni.fUBOffset = 0; // This value will be ignored, but initializing to avoid any errors. |
| 167 | SkString layoutQualifier; |
| 168 | layoutQualifier.appendf("set=%d, binding=%d", uni.fSetNumber, uni.fBinding); |
| 169 | uni.fVariable.setLayoutQualifier(layoutQualifier.c_str()); |
| 170 | } |
| 171 | |
| 172 | return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1); |
| 173 | } |
| 174 | |
| 175 | void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const { |
| 176 | SkTArray<UniformInfo*> uniformBufferUniform; |
| 177 | // Used to collect all the variables that will be place inside the uniform buffer |
| 178 | SkString uniformsString; |
| 179 | SkASSERT(kVertex_GrShaderFlag == visibility || kFragment_GrShaderFlag == visibility); |
| 180 | uint32_t uniformBinding = (visibility == kVertex_GrShaderFlag) ? kVertexBinding : kFragBinding; |
| 181 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 182 | const UniformInfo& localUniform = fUniforms[i]; |
| 183 | if (visibility == localUniform.fVisibility) { |
| 184 | if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) { |
| 185 | SkASSERT(uniformBinding == localUniform.fBinding); |
| 186 | SkASSERT(kUniformBufferDescSet == localUniform.fSetNumber); |
| 187 | localUniform.fVariable.appendDecl(fProgramBuilder->glslCaps(), &uniformsString); |
| 188 | uniformsString.append(";\n"); |
| 189 | } else { |
| 190 | SkASSERT(localUniform.fVariable.getType() == kSampler2D_GrSLType); |
| 191 | SkASSERT(kSamplerDescSet == localUniform.fSetNumber); |
| 192 | localUniform.fVariable.appendDecl(fProgramBuilder->glslCaps(), out); |
| 193 | out->append(";\n"); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | if (!uniformsString.isEmpty()) { |
| 198 | const char* stage = (visibility == kVertex_GrShaderFlag) ? "vertex" : "fragment"; |
| 199 | out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n", |
| 200 | kUniformBufferDescSet, uniformBinding, stage); |
| 201 | out->appendf("%s\n};\n", uniformsString.c_str()); |
| 202 | } |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 203 | } |
| 204 | |