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). |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 17 | // These are designated in the Vulkan spec, section 14.5.4 "Offset and Stride Assignment". |
| 18 | // https://www.khronos.org/registry/vulkan/specs/1.0-wsi_extensions/html/vkspec.html#interfaces-resources-layout |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 19 | uint32_t grsltype_to_alignment_mask(GrSLType type) { |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 20 | switch(type) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 21 | case kShort_GrSLType: // fall through |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 22 | case kUShort_GrSLType: |
| 23 | return 0x1; |
| 24 | case kShort2_GrSLType: // fall through |
| 25 | case kUShort2_GrSLType: |
Chris Dalton | 6dd0d8a | 2017-10-24 19:53:13 +0000 | [diff] [blame] | 26 | return 0x3; |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 27 | case kShort3_GrSLType: // fall through |
| 28 | case kShort4_GrSLType: |
| 29 | case kUShort3_GrSLType: |
| 30 | case kUShort4_GrSLType: |
| 31 | return 0x7; |
| 32 | case kInt_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 33 | case kUint_GrSLType: |
| 34 | return 0x3; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 35 | case kHalf_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 36 | case kFloat_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 37 | return 0x3; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 38 | case kHalf2_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 39 | case kFloat2_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 40 | return 0x7; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 41 | case kHalf3_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 42 | case kFloat3_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 43 | return 0xF; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 44 | case kHalf4_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 45 | case kFloat4_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 46 | return 0xF; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 47 | case kUint2_GrSLType: |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 48 | return 0x7; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 49 | case kInt2_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 50 | return 0x7; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 51 | case kInt3_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 52 | return 0xF; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 53 | case kInt4_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 54 | return 0xF; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 55 | case kHalf2x2_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 56 | case kFloat2x2_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 57 | return 0x7; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 58 | case kHalf3x3_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 59 | case kFloat3x3_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 60 | return 0xF; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 61 | case kHalf4x4_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 62 | case kFloat4x4_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 63 | return 0xF; |
| 64 | |
| 65 | // This query is only valid for certain types. |
| 66 | case kVoid_GrSLType: |
| 67 | case kBool_GrSLType: |
| 68 | case kTexture2DSampler_GrSLType: |
Brian Salomon | a8f0002 | 2016-11-16 12:55:57 -0500 | [diff] [blame] | 69 | case kITexture2DSampler_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 70 | case kTextureExternalSampler_GrSLType: |
| 71 | case kTexture2DRectSampler_GrSLType: |
csmartdalton | 2245803 | 2016-11-16 11:28:16 -0700 | [diff] [blame] | 72 | case kBufferSampler_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 73 | case kTexture2D_GrSLType: |
| 74 | case kSampler_GrSLType: |
| 75 | break; |
| 76 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 77 | SK_ABORT("Unexpected type"); |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 78 | return 0; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 81 | /** Returns the size in bytes taken up in vulkanbuffers for GrSLTypes. */ |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 82 | static inline uint32_t grsltype_to_vk_size(GrSLType type) { |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 83 | switch(type) { |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 84 | case kShort_GrSLType: |
| 85 | return sizeof(int16_t); |
| 86 | case kShort2_GrSLType: |
| 87 | return 2 * sizeof(int16_t); |
| 88 | case kShort3_GrSLType: |
| 89 | return 3 * sizeof(int16_t); |
| 90 | case kShort4_GrSLType: |
| 91 | return 4 * sizeof(int16_t); |
| 92 | case kUShort_GrSLType: |
| 93 | return sizeof(uint16_t); |
| 94 | case kUShort2_GrSLType: |
| 95 | return 2 * sizeof(uint16_t); |
| 96 | case kUShort3_GrSLType: |
| 97 | return 3 * sizeof(uint16_t); |
| 98 | case kUShort4_GrSLType: |
| 99 | return 4 * sizeof(uint16_t); |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 100 | case kInt_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 101 | return sizeof(int32_t); |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 102 | case kUint_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 103 | return sizeof(int32_t); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 104 | case kHalf_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 105 | case kFloat_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 106 | return sizeof(float); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 107 | case kHalf2_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 108 | case kFloat2_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 109 | return 2 * sizeof(float); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 110 | case kHalf3_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 111 | case kFloat3_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 112 | return 3 * sizeof(float); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 113 | case kHalf4_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 114 | case kFloat4_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 115 | return 4 * sizeof(float); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 116 | case kUint2_GrSLType: |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 117 | return 2 * sizeof(uint32_t); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 118 | case kInt2_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 119 | return 2 * sizeof(int32_t); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 120 | case kInt3_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 121 | return 3 * sizeof(int32_t); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 122 | case kInt4_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 123 | return 4 * sizeof(int32_t); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 124 | case kHalf2x2_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 125 | case kFloat2x2_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 126 | //TODO: this will be 4 * szof(float) on std430. |
| 127 | return 8 * sizeof(float); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 128 | case kHalf3x3_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 129 | case kFloat3x3_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 130 | return 12 * sizeof(float); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 131 | case kHalf4x4_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 132 | case kFloat4x4_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 133 | return 16 * sizeof(float); |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 134 | |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 135 | // This query is only valid for certain types. |
| 136 | case kVoid_GrSLType: |
| 137 | case kBool_GrSLType: |
| 138 | case kTexture2DSampler_GrSLType: |
Brian Salomon | a8f0002 | 2016-11-16 12:55:57 -0500 | [diff] [blame] | 139 | case kITexture2DSampler_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 140 | case kTextureExternalSampler_GrSLType: |
| 141 | case kTexture2DRectSampler_GrSLType: |
csmartdalton | 2245803 | 2016-11-16 11:28:16 -0700 | [diff] [blame] | 142 | case kBufferSampler_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 143 | case kTexture2D_GrSLType: |
| 144 | case kSampler_GrSLType: |
| 145 | break; |
| 146 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 147 | SK_ABORT("Unexpected type"); |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 148 | return 0; |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 152 | // Given the current offset into the ubo, calculate the offset for the uniform we're trying to add |
| 153 | // taking into consideration all alignment requirements. The uniformOffset is set to the offset for |
| 154 | // the new uniform, and currentOffset is updated to be the offset to the end of the new uniform. |
| 155 | void get_ubo_aligned_offset(uint32_t* uniformOffset, |
| 156 | uint32_t* currentOffset, |
| 157 | GrSLType type, |
| 158 | int arrayCount) { |
| 159 | uint32_t alignmentMask = grsltype_to_alignment_mask(type); |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 160 | // We want to use the std140 layout here, so we must make arrays align to 16 bytes. |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 161 | if (arrayCount || type == kFloat2x2_GrSLType) { |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 162 | alignmentMask = 0xF; |
| 163 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 164 | uint32_t offsetDiff = *currentOffset & alignmentMask; |
| 165 | if (offsetDiff != 0) { |
| 166 | offsetDiff = alignmentMask - offsetDiff + 1; |
| 167 | } |
| 168 | *uniformOffset = *currentOffset + offsetDiff; |
| 169 | SkASSERT(sizeof(float) == 4); |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 170 | if (arrayCount) { |
| 171 | uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type)); |
| 172 | SkASSERT(0 == (elementSize & 0xF)); |
| 173 | *currentOffset = *uniformOffset + elementSize * arrayCount; |
| 174 | } else { |
| 175 | *currentOffset = *uniformOffset + grsltype_to_vk_size(type); |
| 176 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray( |
| 180 | uint32_t visibility, |
| 181 | GrSLType type, |
| 182 | GrSLPrecision precision, |
| 183 | const char* name, |
| 184 | bool mangleName, |
| 185 | int arrayCount, |
| 186 | const char** outName) { |
| 187 | SkASSERT(name && strlen(name)); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 188 | // For now asserting the the visibility is either geometry types (vertex, tesselation, geometry, |
| 189 | // etc.) or only fragment. |
| 190 | SkASSERT(kVertex_GrShaderFlag == visibility || |
| 191 | kGeometry_GrShaderFlag == visibility || |
| 192 | (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == visibility || |
| 193 | kFragment_GrShaderFlag == visibility); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 194 | SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 195 | GrSLTypeIsFloatType(type); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 196 | |
| 197 | UniformInfo& uni = fUniforms.push_back(); |
| 198 | uni.fVariable.setType(type); |
| 199 | // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use |
| 200 | // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB |
| 201 | // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then |
| 202 | // the names will mismatch. I think the correct solution is to have all GPs which need the |
| 203 | // uniform view matrix, they should upload the view matrix in their setData along with regular |
| 204 | // uniforms. |
| 205 | char prefix = 'u'; |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame^] | 206 | if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 207 | prefix = '\0'; |
| 208 | } |
| 209 | fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName); |
| 210 | uni.fVariable.setArrayCount(arrayCount); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 211 | uni.fVisibility = visibility; |
| 212 | uni.fVariable.setPrecision(precision); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 213 | // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus |
| 214 | // 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] | 215 | uni.fVariable.setTypeModifier(GrShaderVar::kNone_TypeModifier); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 216 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 217 | uint32_t* currentOffset; |
| 218 | uint32_t geomStages = kVertex_GrShaderFlag | kGeometry_GrShaderFlag; |
| 219 | if (geomStages & visibility) { |
| 220 | currentOffset = &fCurrentGeometryUBOOffset; |
| 221 | } else { |
| 222 | SkASSERT(kFragment_GrShaderFlag == visibility); |
| 223 | currentOffset = &fCurrentFragmentUBOOffset; |
| 224 | } |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 225 | get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 226 | |
Greg Daniel | dbd44c7 | 2017-01-24 15:12:12 -0500 | [diff] [blame] | 227 | SkString layoutQualifier; |
| 228 | layoutQualifier.appendf("offset=%d", uni.fUBOffset); |
| 229 | uni.fVariable.addLayoutQualifier(layoutQualifier.c_str()); |
| 230 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 231 | if (outName) { |
| 232 | *outName = uni.fVariable.c_str(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1); |
| 236 | } |
| 237 | |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 238 | GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(uint32_t visibility, |
| 239 | GrSwizzle swizzle, |
| 240 | GrSLType type, |
| 241 | GrSLPrecision precision, |
| 242 | const char* name) { |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 243 | SkASSERT(name && strlen(name)); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 244 | // For now asserting the the visibility is either only vertex, geometry, or fragment |
| 245 | SkASSERT(kVertex_GrShaderFlag == visibility || |
| 246 | kFragment_GrShaderFlag == visibility || |
| 247 | kGeometry_GrShaderFlag == visibility); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 248 | SkString mangleName; |
| 249 | char prefix = 'u'; |
| 250 | fProgramBuilder->nameVariable(&mangleName, prefix, name, true); |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 251 | |
| 252 | UniformInfo& info = fSamplers.push_back(); |
| 253 | SkASSERT(GrSLTypeIsCombinedSamplerType(type)); |
| 254 | info.fVariable.setType(type); |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 255 | info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier); |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 256 | info.fVariable.setPrecision(precision); |
| 257 | info.fVariable.setName(mangleName); |
| 258 | SkString layoutQualifier; |
| 259 | layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count() - 1); |
Brian Salomon | 6039768 | 2016-11-22 15:06:46 -0500 | [diff] [blame] | 260 | info.fVariable.addLayoutQualifier(layoutQualifier.c_str()); |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 261 | info.fVisibility = visibility; |
| 262 | info.fUBOffset = 0; |
| 263 | fSamplerSwizzles.push_back(swizzle); |
| 264 | SkASSERT(fSamplerSwizzles.count() == fSamplers.count()); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 265 | return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1); |
| 266 | } |
| 267 | |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 268 | GrGLSLUniformHandler::TexelBufferHandle GrVkUniformHandler::addTexelBuffer(uint32_t visibility, |
| 269 | GrSLPrecision precision, |
| 270 | const char* name) { |
| 271 | SkASSERT(name && strlen(name)); |
Greg Daniel | aa352de | 2017-07-17 09:05:16 -0400 | [diff] [blame] | 272 | SkDEBUGCODE(static const uint32_t kVisMask = kVertex_GrShaderFlag | |
| 273 | kGeometry_GrShaderFlag | |
| 274 | kFragment_GrShaderFlag); |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 275 | SkASSERT(0 == (~kVisMask & visibility)); |
| 276 | SkASSERT(0 != visibility); |
| 277 | SkString mangleName; |
| 278 | char prefix = 'u'; |
| 279 | fProgramBuilder->nameVariable(&mangleName, prefix, name, true); |
| 280 | |
| 281 | UniformInfo& info = fTexelBuffers.push_back(); |
| 282 | info.fVariable.setType(kBufferSampler_GrSLType); |
| 283 | info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier); |
| 284 | info.fVariable.setPrecision(precision); |
| 285 | info.fVariable.setName(mangleName); |
| 286 | SkString layoutQualifier; |
| 287 | layoutQualifier.appendf("set=%d, binding=%d", kTexelBufferDescSet, fTexelBuffers.count()- 1); |
| 288 | info.fVariable.addLayoutQualifier(layoutQualifier.c_str()); |
| 289 | info.fVisibility = visibility; |
| 290 | info.fUBOffset = 0; |
| 291 | return GrGLSLUniformHandler::TexelBufferHandle(fTexelBuffers.count() - 1); |
| 292 | } |
| 293 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 294 | void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 295 | SkASSERT(kVertex_GrShaderFlag == visibility || |
| 296 | kGeometry_GrShaderFlag == visibility || |
| 297 | kFragment_GrShaderFlag == visibility); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 298 | |
| 299 | for (int i = 0; i < fSamplers.count(); ++i) { |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 300 | const UniformInfo& sampler = fSamplers[i]; |
| 301 | SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType); |
| 302 | if (visibility == sampler.fVisibility) { |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 303 | sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 304 | out->append(";\n"); |
| 305 | } |
| 306 | } |
| 307 | |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 308 | for (int i = 0; i < fTexelBuffers.count(); ++i) { |
| 309 | const UniformInfo& texelBuffer = fTexelBuffers[i]; |
| 310 | if (visibility == texelBuffer.fVisibility) { |
| 311 | texelBuffer.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out); |
| 312 | out->append(";\n"); |
| 313 | } |
| 314 | } |
| 315 | |
Greg Daniel | aa352de | 2017-07-17 09:05:16 -0400 | [diff] [blame] | 316 | #ifdef SK_DEBUG |
| 317 | bool firstGeomOffsetCheck = false; |
| 318 | bool firstFragOffsetCheck = false; |
| 319 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 320 | const UniformInfo& localUniform = fUniforms[i]; |
| 321 | if (kVertex_GrShaderFlag == localUniform.fVisibility || |
| 322 | kGeometry_GrShaderFlag == localUniform.fVisibility || |
| 323 | (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == localUniform.fVisibility) { |
| 324 | if (!firstGeomOffsetCheck) { |
| 325 | // Check to make sure we are starting our offset at 0 so the offset qualifier we |
| 326 | // set on each variable in the uniform block is valid. |
| 327 | SkASSERT(0 == localUniform.fUBOffset); |
| 328 | firstGeomOffsetCheck = true; |
| 329 | } |
| 330 | } else { |
| 331 | SkASSERT(kFragment_GrShaderFlag == localUniform.fVisibility); |
| 332 | if (!firstFragOffsetCheck) { |
| 333 | // Check to make sure we are starting our offset at 0 so the offset qualifier we |
| 334 | // set on each variable in the uniform block is valid. |
| 335 | SkASSERT(0 == localUniform.fUBOffset); |
| 336 | firstFragOffsetCheck = true; |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | #endif |
| 341 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 342 | SkString uniformsString; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 343 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 344 | const UniformInfo& localUniform = fUniforms[i]; |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 345 | if (visibility & localUniform.fVisibility) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 346 | if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) { |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 347 | localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 348 | uniformsString.append(";\n"); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | } |
Greg Daniel | aa352de | 2017-07-17 09:05:16 -0400 | [diff] [blame] | 352 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 353 | if (!uniformsString.isEmpty()) { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 354 | uint32_t uniformBinding; |
| 355 | const char* stage; |
| 356 | if (kVertex_GrShaderFlag == visibility) { |
| 357 | uniformBinding = kGeometryBinding; |
| 358 | stage = "vertex"; |
| 359 | } else if (kGeometry_GrShaderFlag == visibility) { |
| 360 | uniformBinding = kGeometryBinding; |
| 361 | stage = "geometry"; |
| 362 | } else { |
| 363 | SkASSERT(kFragment_GrShaderFlag == visibility); |
| 364 | uniformBinding = kFragBinding; |
| 365 | stage = "fragment"; |
| 366 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 367 | out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n", |
| 368 | kUniformBufferDescSet, uniformBinding, stage); |
| 369 | out->appendf("%s\n};\n", uniformsString.c_str()); |
| 370 | } |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 371 | } |