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