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