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)); |
bsalomon | 134af6b | 2016-04-12 07:46:21 -0700 | [diff] [blame] | 19 | static const uint32_t kAlignmentMask[] = { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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 |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 28 | 0x0, // kTexture2DSampler_GrSLType, should never return this |
| 29 | 0x0, // kTexture2DISampler_GrSLType, should never return this |
| 30 | 0x0, // kTextureExternalSampler_GrSLType, should never return this |
| 31 | 0x0, // kTexture2DSamplerRect_GrSLType, should never return this |
| 32 | 0x0, // ktextureBufferSampler_GrSLType, should never return this |
bsalomon | 134af6b | 2016-04-12 07:46:21 -0700 | [diff] [blame] | 33 | 0x0, // kBool_GrSLType |
| 34 | 0x7, // kInt_GrSLType |
| 35 | 0x7, // kUint_GrSLType |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 36 | 0x0, // Texture2D_GrSLType, should never return this |
| 37 | 0x0, // Sampler_GrSLType, should never return this |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 38 | }; |
| 39 | GR_STATIC_ASSERT(0 == kVoid_GrSLType); |
| 40 | GR_STATIC_ASSERT(1 == kFloat_GrSLType); |
| 41 | GR_STATIC_ASSERT(2 == kVec2f_GrSLType); |
| 42 | GR_STATIC_ASSERT(3 == kVec3f_GrSLType); |
| 43 | GR_STATIC_ASSERT(4 == kVec4f_GrSLType); |
cdalton | 8d988b3 | 2016-03-07 15:39:09 -0800 | [diff] [blame] | 44 | GR_STATIC_ASSERT(5 == kMat22f_GrSLType); |
| 45 | GR_STATIC_ASSERT(6 == kMat33f_GrSLType); |
| 46 | GR_STATIC_ASSERT(7 == kMat44f_GrSLType); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 47 | GR_STATIC_ASSERT(8 == kTexture2DSampler_GrSLType); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 48 | GR_STATIC_ASSERT(9 == kTexture2DISampler_GrSLType); |
| 49 | GR_STATIC_ASSERT(10 == kTextureExternalSampler_GrSLType); |
| 50 | GR_STATIC_ASSERT(11 == kTexture2DRectSampler_GrSLType); |
| 51 | GR_STATIC_ASSERT(12 == kTextureBufferSampler_GrSLType); |
| 52 | GR_STATIC_ASSERT(13 == kBool_GrSLType); |
| 53 | GR_STATIC_ASSERT(14 == kInt_GrSLType); |
| 54 | GR_STATIC_ASSERT(15 == kUint_GrSLType); |
| 55 | GR_STATIC_ASSERT(16 == kTexture2D_GrSLType); |
| 56 | GR_STATIC_ASSERT(17 == kSampler_GrSLType); |
bsalomon | 134af6b | 2016-04-12 07:46:21 -0700 | [diff] [blame] | 57 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kAlignmentMask) == kGrSLTypeCount); |
| 58 | return kAlignmentMask[type]; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 59 | } |
| 60 | |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 61 | /** Returns the size in bytes taken up in vulkanbuffers for floating point GrSLTypes. |
egdaniel | 75d2bfc | 2016-07-07 08:04:08 -0700 | [diff] [blame] | 62 | For non floating point type returns 0. Currently this reflects the std140 alignment |
| 63 | so a mat22 takes up 8 floats. */ |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 64 | static inline uint32_t grsltype_to_vk_size(GrSLType type) { |
| 65 | SkASSERT(GrSLTypeIsFloatType(type)); |
| 66 | static const uint32_t kSizes[] = { |
| 67 | 0, // kVoid_GrSLType |
| 68 | sizeof(float), // kFloat_GrSLType |
| 69 | 2 * sizeof(float), // kVec2f_GrSLType |
| 70 | 3 * sizeof(float), // kVec3f_GrSLType |
| 71 | 4 * sizeof(float), // kVec4f_GrSLType |
cdalton | 8d988b3 | 2016-03-07 15:39:09 -0800 | [diff] [blame] | 72 | 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] | 73 | 12 * sizeof(float), // kMat33f_GrSLType |
| 74 | 16 * sizeof(float), // kMat44f_GrSLType |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 75 | 0, // kTexture2DSampler_GrSLType |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 76 | 0, // kTexture2DISampler_GrSLType |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 77 | 0, // kTextureExternalSampler_GrSLType |
| 78 | 0, // kTexture2DRectSampler_GrSLType |
| 79 | 0, // kTextureBufferSampler_GrSLType |
bsalomon | 134af6b | 2016-04-12 07:46:21 -0700 | [diff] [blame] | 80 | 1, // kBool_GrSLType |
| 81 | 4, // kInt_GrSLType |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 82 | 4, // kUint_GrSLType |
| 83 | 0, // kTexture2D_GrSLType |
| 84 | 0, // kSampler_GrSLType |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 85 | }; |
| 86 | return kSizes[type]; |
| 87 | |
| 88 | GR_STATIC_ASSERT(0 == kVoid_GrSLType); |
| 89 | GR_STATIC_ASSERT(1 == kFloat_GrSLType); |
| 90 | GR_STATIC_ASSERT(2 == kVec2f_GrSLType); |
| 91 | GR_STATIC_ASSERT(3 == kVec3f_GrSLType); |
| 92 | GR_STATIC_ASSERT(4 == kVec4f_GrSLType); |
cdalton | 8d988b3 | 2016-03-07 15:39:09 -0800 | [diff] [blame] | 93 | GR_STATIC_ASSERT(5 == kMat22f_GrSLType); |
| 94 | GR_STATIC_ASSERT(6 == kMat33f_GrSLType); |
| 95 | GR_STATIC_ASSERT(7 == kMat44f_GrSLType); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 96 | GR_STATIC_ASSERT(8 == kTexture2DSampler_GrSLType); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 97 | GR_STATIC_ASSERT(9 == kTexture2DISampler_GrSLType); |
| 98 | GR_STATIC_ASSERT(10 == kTextureExternalSampler_GrSLType); |
| 99 | GR_STATIC_ASSERT(11 == kTexture2DRectSampler_GrSLType); |
| 100 | GR_STATIC_ASSERT(12 == kTextureBufferSampler_GrSLType); |
| 101 | GR_STATIC_ASSERT(13 == kBool_GrSLType); |
| 102 | GR_STATIC_ASSERT(14 == kInt_GrSLType); |
| 103 | GR_STATIC_ASSERT(15 == kUint_GrSLType); |
| 104 | GR_STATIC_ASSERT(16 == kTexture2D_GrSLType); |
| 105 | GR_STATIC_ASSERT(17 == kSampler_GrSLType); |
cdalton | 8d988b3 | 2016-03-07 15:39:09 -0800 | [diff] [blame] | 106 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kSizes) == kGrSLTypeCount); |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 110 | // Given the current offset into the ubo, calculate the offset for the uniform we're trying to add |
| 111 | // taking into consideration all alignment requirements. The uniformOffset is set to the offset for |
| 112 | // the new uniform, and currentOffset is updated to be the offset to the end of the new uniform. |
| 113 | void get_ubo_aligned_offset(uint32_t* uniformOffset, |
| 114 | uint32_t* currentOffset, |
| 115 | GrSLType type, |
| 116 | int arrayCount) { |
| 117 | uint32_t alignmentMask = grsltype_to_alignment_mask(type); |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 118 | // We want to use the std140 layout here, so we must make arrays align to 16 bytes. |
egdaniel | 75d2bfc | 2016-07-07 08:04:08 -0700 | [diff] [blame] | 119 | if (arrayCount || type == kMat22f_GrSLType) { |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 120 | alignmentMask = 0xF; |
| 121 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 122 | uint32_t offsetDiff = *currentOffset & alignmentMask; |
| 123 | if (offsetDiff != 0) { |
| 124 | offsetDiff = alignmentMask - offsetDiff + 1; |
| 125 | } |
| 126 | *uniformOffset = *currentOffset + offsetDiff; |
| 127 | SkASSERT(sizeof(float) == 4); |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 128 | if (arrayCount) { |
| 129 | uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type)); |
| 130 | SkASSERT(0 == (elementSize & 0xF)); |
| 131 | *currentOffset = *uniformOffset + elementSize * arrayCount; |
| 132 | } else { |
| 133 | *currentOffset = *uniformOffset + grsltype_to_vk_size(type); |
| 134 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray( |
| 138 | uint32_t visibility, |
| 139 | GrSLType type, |
| 140 | GrSLPrecision precision, |
| 141 | const char* name, |
| 142 | bool mangleName, |
| 143 | int arrayCount, |
| 144 | const char** outName) { |
| 145 | SkASSERT(name && strlen(name)); |
| 146 | SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_GrShaderFlag|kFragment_GrShaderFlag); |
| 147 | SkASSERT(0 == (~kVisibilityMask & visibility)); |
| 148 | SkASSERT(0 != visibility); |
| 149 | SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 150 | GrSLTypeIsFloatType(type); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 151 | |
| 152 | UniformInfo& uni = fUniforms.push_back(); |
| 153 | uni.fVariable.setType(type); |
| 154 | // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use |
| 155 | // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB |
| 156 | // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then |
| 157 | // the names will mismatch. I think the correct solution is to have all GPs which need the |
| 158 | // uniform view matrix, they should upload the view matrix in their setData along with regular |
| 159 | // uniforms. |
| 160 | char prefix = 'u'; |
| 161 | if ('u' == name[0]) { |
| 162 | prefix = '\0'; |
| 163 | } |
| 164 | fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName); |
| 165 | uni.fVariable.setArrayCount(arrayCount); |
| 166 | // For now asserting the the visibility is either only vertex or only fragment |
| 167 | SkASSERT(kVertex_GrShaderFlag == visibility || kFragment_GrShaderFlag == visibility); |
| 168 | uni.fVisibility = visibility; |
| 169 | uni.fVariable.setPrecision(precision); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 170 | // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus |
| 171 | // we set the modifier to none for all uniforms declared inside the block. |
| 172 | uni.fVariable.setTypeModifier(GrGLSLShaderVar::kNone_TypeModifier); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 173 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 174 | uint32_t* currentOffset = kVertex_GrShaderFlag == visibility ? &fCurrentVertexUBOOffset |
| 175 | : &fCurrentFragmentUBOOffset; |
| 176 | get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 177 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 178 | if (outName) { |
| 179 | *outName = uni.fVariable.c_str(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1); |
| 183 | } |
| 184 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 185 | GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::internalAddSampler(uint32_t visibility, |
| 186 | GrPixelConfig config, |
| 187 | GrSLType type, |
| 188 | GrSLPrecision precision, |
| 189 | const char* name) { |
| 190 | SkASSERT(name && strlen(name)); |
| 191 | SkDEBUGCODE(static const uint32_t kVisMask = kVertex_GrShaderFlag | kFragment_GrShaderFlag); |
| 192 | SkASSERT(0 == (~kVisMask & visibility)); |
| 193 | SkASSERT(0 != visibility); |
| 194 | SkString mangleName; |
| 195 | char prefix = 'u'; |
| 196 | fProgramBuilder->nameVariable(&mangleName, prefix, name, true); |
| 197 | fSamplers.emplace_back(visibility, config, type, precision, mangleName.c_str(), |
| 198 | (uint32_t)fSamplers.count(), kSamplerDescSet); |
| 199 | return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1); |
| 200 | } |
| 201 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 202 | void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const { |
egdaniel | b800248 | 2016-04-19 15:24:29 -0700 | [diff] [blame] | 203 | SkASSERT(kVertex_GrShaderFlag == visibility || kFragment_GrShaderFlag == visibility); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 204 | |
| 205 | for (int i = 0; i < fSamplers.count(); ++i) { |
| 206 | const GrVkGLSLSampler& sampler = fSamplers[i]; |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 207 | SkASSERT(sampler.type() == kTexture2DSampler_GrSLType); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 208 | if (visibility == sampler.visibility()) { |
| 209 | sampler.fShaderVar.appendDecl(fProgramBuilder->glslCaps(), out); |
| 210 | out->append(";\n"); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | SkString uniformsString; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 215 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 216 | const UniformInfo& localUniform = fUniforms[i]; |
| 217 | if (visibility == localUniform.fVisibility) { |
| 218 | if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 219 | localUniform.fVariable.appendDecl(fProgramBuilder->glslCaps(), &uniformsString); |
| 220 | uniformsString.append(";\n"); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | } |
| 224 | if (!uniformsString.isEmpty()) { |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 225 | uint32_t uniformBinding = (visibility == kVertex_GrShaderFlag) ? kVertexBinding |
| 226 | : kFragBinding; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 227 | const char* stage = (visibility == kVertex_GrShaderFlag) ? "vertex" : "fragment"; |
| 228 | out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n", |
| 229 | kUniformBufferDescSet, uniformBinding, stage); |
| 230 | out->appendf("%s\n};\n", uniformsString.c_str()); |
| 231 | } |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 232 | } |