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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/vk/GrVkUniformHandler.h" |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrTexturePriv.h" |
| 11 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 12 | #include "src/gpu/vk/GrVkGpu.h" |
| 13 | #include "src/gpu/vk/GrVkPipelineStateBuilder.h" |
| 14 | #include "src/gpu/vk/GrVkTexture.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 15 | |
| 16 | // To determine whether a current offset is aligned, we can just 'and' the lowest bits with the |
| 17 | // alignment mask. A value of 0 means aligned, any other value is how many bytes past alignment we |
| 18 | // 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] | 19 | // This alignment mask will give correct alignments for using the std430 block layout. If you want |
| 20 | // the std140 alignment, you can use this, but then make sure if you have an array type it is |
| 21 | // aligned to 16 bytes (i.e. has mask of 0xF). |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 22 | // These are designated in the Vulkan spec, section 14.5.4 "Offset and Stride Assignment". |
| 23 | // https://www.khronos.org/registry/vulkan/specs/1.0-wsi_extensions/html/vkspec.html#interfaces-resources-layout |
Mike Klein | dc73559 | 2018-10-11 14:26:30 -0400 | [diff] [blame] | 24 | static uint32_t grsltype_to_alignment_mask(GrSLType type) { |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 25 | switch(type) { |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 26 | case kByte_GrSLType: // fall through |
| 27 | case kUByte_GrSLType: |
| 28 | return 0x0; |
| 29 | case kByte2_GrSLType: // fall through |
| 30 | case kUByte2_GrSLType: |
| 31 | return 0x1; |
| 32 | case kByte3_GrSLType: // fall through |
| 33 | case kByte4_GrSLType: |
| 34 | case kUByte3_GrSLType: |
| 35 | case kUByte4_GrSLType: |
| 36 | return 0x3; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 37 | case kShort_GrSLType: // fall through |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 38 | case kUShort_GrSLType: |
| 39 | return 0x1; |
| 40 | case kShort2_GrSLType: // fall through |
| 41 | case kUShort2_GrSLType: |
Chris Dalton | 6dd0d8a | 2017-10-24 19:53:13 +0000 | [diff] [blame] | 42 | return 0x3; |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 43 | case kShort3_GrSLType: // fall through |
| 44 | case kShort4_GrSLType: |
| 45 | case kUShort3_GrSLType: |
| 46 | case kUShort4_GrSLType: |
| 47 | return 0x7; |
| 48 | case kInt_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 49 | case kUint_GrSLType: |
| 50 | return 0x3; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 51 | case kHalf_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 52 | case kFloat_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 53 | return 0x3; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 54 | case kHalf2_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 55 | case kFloat2_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 56 | return 0x7; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 57 | case kHalf3_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 58 | case kFloat3_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 59 | return 0xF; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 60 | case kHalf4_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 61 | case kFloat4_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 62 | return 0xF; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 63 | case kUint2_GrSLType: |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 64 | return 0x7; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 65 | case kInt2_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 66 | return 0x7; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 67 | case kInt3_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 68 | return 0xF; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 69 | case kInt4_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 70 | return 0xF; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 71 | case kHalf2x2_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 72 | case kFloat2x2_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 73 | return 0x7; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 74 | case kHalf3x3_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 75 | case kFloat3x3_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 76 | return 0xF; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 77 | case kHalf4x4_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 78 | case kFloat4x4_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 79 | return 0xF; |
| 80 | |
| 81 | // This query is only valid for certain types. |
| 82 | case kVoid_GrSLType: |
| 83 | case kBool_GrSLType: |
| 84 | case kTexture2DSampler_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 85 | case kTextureExternalSampler_GrSLType: |
| 86 | case kTexture2DRectSampler_GrSLType: |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 87 | case kSampler_GrSLType: |
| 88 | case kTexture2D_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 89 | break; |
| 90 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 91 | SK_ABORT("Unexpected type"); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 94 | /** Returns the size in bytes taken up in vulkanbuffers for GrSLTypes. */ |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 95 | static inline uint32_t grsltype_to_vk_size(GrSLType type) { |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 96 | switch(type) { |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 97 | case kByte_GrSLType: |
| 98 | return sizeof(int8_t); |
| 99 | case kByte2_GrSLType: |
| 100 | return 2 * sizeof(int8_t); |
| 101 | case kByte3_GrSLType: |
| 102 | return 3 * sizeof(int8_t); |
| 103 | case kByte4_GrSLType: |
| 104 | return 4 * sizeof(int8_t); |
| 105 | case kUByte_GrSLType: |
| 106 | return sizeof(uint8_t); |
| 107 | case kUByte2_GrSLType: |
| 108 | return 2 * sizeof(uint8_t); |
| 109 | case kUByte3_GrSLType: |
| 110 | return 3 * sizeof(uint8_t); |
| 111 | case kUByte4_GrSLType: |
| 112 | return 4 * sizeof(uint8_t); |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 113 | case kShort_GrSLType: |
| 114 | return sizeof(int16_t); |
| 115 | case kShort2_GrSLType: |
| 116 | return 2 * sizeof(int16_t); |
| 117 | case kShort3_GrSLType: |
| 118 | return 3 * sizeof(int16_t); |
| 119 | case kShort4_GrSLType: |
| 120 | return 4 * sizeof(int16_t); |
| 121 | case kUShort_GrSLType: |
| 122 | return sizeof(uint16_t); |
| 123 | case kUShort2_GrSLType: |
| 124 | return 2 * sizeof(uint16_t); |
| 125 | case kUShort3_GrSLType: |
| 126 | return 3 * sizeof(uint16_t); |
| 127 | case kUShort4_GrSLType: |
| 128 | return 4 * sizeof(uint16_t); |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 129 | case kInt_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 130 | return sizeof(int32_t); |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 131 | case kUint_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 132 | return sizeof(int32_t); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 133 | case kHalf_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 134 | case kFloat_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 135 | return sizeof(float); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 136 | case kHalf2_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 137 | case kFloat2_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 138 | return 2 * sizeof(float); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 139 | case kHalf3_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 140 | case kFloat3_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 141 | return 3 * sizeof(float); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 142 | case kHalf4_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 143 | case kFloat4_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 144 | return 4 * sizeof(float); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 145 | case kUint2_GrSLType: |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 146 | return 2 * sizeof(uint32_t); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 147 | case kInt2_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 148 | return 2 * sizeof(int32_t); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 149 | case kInt3_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 150 | return 3 * sizeof(int32_t); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 151 | case kInt4_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 152 | return 4 * sizeof(int32_t); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 153 | case kHalf2x2_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 154 | case kFloat2x2_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 155 | //TODO: this will be 4 * szof(float) on std430. |
| 156 | return 8 * sizeof(float); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 157 | case kHalf3x3_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 158 | case kFloat3x3_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 159 | return 12 * sizeof(float); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 160 | case kHalf4x4_GrSLType: // fall through |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 161 | case kFloat4x4_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 162 | return 16 * sizeof(float); |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 163 | |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 164 | // This query is only valid for certain types. |
| 165 | case kVoid_GrSLType: |
| 166 | case kBool_GrSLType: |
| 167 | case kTexture2DSampler_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 168 | case kTextureExternalSampler_GrSLType: |
| 169 | case kTexture2DRectSampler_GrSLType: |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 170 | case kSampler_GrSLType: |
| 171 | case kTexture2D_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 172 | break; |
| 173 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 174 | SK_ABORT("Unexpected type"); |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 178 | // Given the current offset into the ubo, calculate the offset for the uniform we're trying to add |
| 179 | // taking into consideration all alignment requirements. The uniformOffset is set to the offset for |
| 180 | // the new uniform, and currentOffset is updated to be the offset to the end of the new uniform. |
Mike Klein | dc73559 | 2018-10-11 14:26:30 -0400 | [diff] [blame] | 181 | static void get_ubo_aligned_offset(uint32_t* uniformOffset, |
| 182 | uint32_t* currentOffset, |
| 183 | GrSLType type, |
| 184 | int arrayCount) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 185 | uint32_t alignmentMask = grsltype_to_alignment_mask(type); |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 186 | // 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] | 187 | if (arrayCount || type == kFloat2x2_GrSLType) { |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 188 | alignmentMask = 0xF; |
| 189 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 190 | uint32_t offsetDiff = *currentOffset & alignmentMask; |
| 191 | if (offsetDiff != 0) { |
| 192 | offsetDiff = alignmentMask - offsetDiff + 1; |
| 193 | } |
| 194 | *uniformOffset = *currentOffset + offsetDiff; |
| 195 | SkASSERT(sizeof(float) == 4); |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 196 | if (arrayCount) { |
| 197 | uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type)); |
| 198 | SkASSERT(0 == (elementSize & 0xF)); |
| 199 | *currentOffset = *uniformOffset + elementSize * arrayCount; |
| 200 | } else { |
| 201 | *currentOffset = *uniformOffset + grsltype_to_vk_size(type); |
| 202 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Sergey Ulanov | 2739fd2 | 2019-08-11 22:46:33 -0700 | [diff] [blame] | 205 | GrVkUniformHandler::~GrVkUniformHandler() { |
| 206 | GrVkGpu* gpu = static_cast<GrVkPipelineStateBuilder*>(fProgramBuilder)->gpu(); |
| 207 | for (decltype(fSamplers)::Iter iter(&fSamplers); iter.next();) { |
| 208 | if (iter->fImmutableSampler) { |
| 209 | iter->fImmutableSampler->unref(gpu); |
| 210 | iter->fImmutableSampler = nullptr; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 215 | GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray( |
| 216 | uint32_t visibility, |
| 217 | GrSLType type, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 218 | const char* name, |
| 219 | bool mangleName, |
| 220 | int arrayCount, |
| 221 | const char** outName) { |
| 222 | SkASSERT(name && strlen(name)); |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 223 | SkASSERT(GrSLTypeIsFloatType(type)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 224 | |
| 225 | UniformInfo& uni = fUniforms.push_back(); |
| 226 | uni.fVariable.setType(type); |
| 227 | // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use |
| 228 | // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB |
| 229 | // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then |
| 230 | // the names will mismatch. I think the correct solution is to have all GPs which need the |
| 231 | // uniform view matrix, they should upload the view matrix in their setData along with regular |
| 232 | // uniforms. |
| 233 | char prefix = 'u'; |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 234 | 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] | 235 | prefix = '\0'; |
| 236 | } |
| 237 | fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName); |
| 238 | uni.fVariable.setArrayCount(arrayCount); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 239 | uni.fVisibility = visibility; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 240 | // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus |
| 241 | // 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] | 242 | uni.fVariable.setTypeModifier(GrShaderVar::kNone_TypeModifier); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 243 | |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 244 | get_ubo_aligned_offset(&uni.fUBOffset, &fCurrentUBOOffset, type, arrayCount); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 245 | |
Greg Daniel | dbd44c7 | 2017-01-24 15:12:12 -0500 | [diff] [blame] | 246 | SkString layoutQualifier; |
| 247 | layoutQualifier.appendf("offset=%d", uni.fUBOffset); |
| 248 | uni.fVariable.addLayoutQualifier(layoutQualifier.c_str()); |
| 249 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 250 | if (outName) { |
| 251 | *outName = uni.fVariable.c_str(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1); |
| 255 | } |
| 256 | |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 257 | GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(const GrTexture* texture, |
| 258 | const GrSamplerState& state, |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 259 | const GrSwizzle& swizzle, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 260 | const char* name, |
| 261 | const GrShaderCaps* shaderCaps) { |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 262 | SkASSERT(name && strlen(name)); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 263 | SkString mangleName; |
| 264 | char prefix = 'u'; |
| 265 | fProgramBuilder->nameVariable(&mangleName, prefix, name, true); |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 266 | |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 267 | GrTextureType type = texture->texturePriv().textureType(); |
| 268 | |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 269 | UniformInfo& info = fSamplers.push_back(); |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 270 | info.fVariable.setType(GrSLCombinedSamplerTypeForTextureType(type)); |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 271 | info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier); |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 272 | info.fVariable.setName(mangleName); |
| 273 | SkString layoutQualifier; |
| 274 | layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count() - 1); |
Brian Salomon | 6039768 | 2016-11-22 15:06:46 -0500 | [diff] [blame] | 275 | info.fVariable.addLayoutQualifier(layoutQualifier.c_str()); |
Greg Daniel | 0f70be8 | 2018-10-08 17:35:08 +0000 | [diff] [blame] | 276 | info.fVisibility = kFragment_GrShaderFlag; |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 277 | info.fUBOffset = 0; |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 278 | |
Sergey Ulanov | 2739fd2 | 2019-08-11 22:46:33 -0700 | [diff] [blame] | 279 | // Check if we are dealing with an external texture and store the needed information if so. |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 280 | const GrVkTexture* vkTexture = static_cast<const GrVkTexture*>(texture); |
| 281 | if (vkTexture->ycbcrConversionInfo().isValid()) { |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 282 | GrVkGpu* gpu = static_cast<GrVkPipelineStateBuilder*>(fProgramBuilder)->gpu(); |
| 283 | info.fImmutableSampler = gpu->resourceProvider().findOrCreateCompatibleSampler( |
| 284 | state, vkTexture->ycbcrConversionInfo()); |
| 285 | SkASSERT(info.fImmutableSampler); |
| 286 | } |
| 287 | |
Brian Salomon | 68ba117 | 2019-06-05 11:15:08 -0400 | [diff] [blame] | 288 | SkASSERT(shaderCaps->textureSwizzleAppliedInShader()); |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 289 | fSamplerSwizzles.push_back(swizzle); |
| 290 | SkASSERT(fSamplerSwizzles.count() == fSamplers.count()); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 291 | return GrGLSLUniformHandler::SamplerHandle(fSamplers.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 { |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 295 | for (int i = 0; i < fSamplers.count(); ++i) { |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 296 | const UniformInfo& sampler = fSamplers[i]; |
| 297 | SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType); |
| 298 | if (visibility == sampler.fVisibility) { |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 299 | sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 300 | out->append(";\n"); |
| 301 | } |
| 302 | } |
| 303 | |
Greg Daniel | aa352de | 2017-07-17 09:05:16 -0400 | [diff] [blame] | 304 | #ifdef SK_DEBUG |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 305 | bool firstOffsetCheck = false; |
Greg Daniel | aa352de | 2017-07-17 09:05:16 -0400 | [diff] [blame] | 306 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 307 | const UniformInfo& localUniform = fUniforms[i]; |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 308 | if (!firstOffsetCheck) { |
| 309 | // Check to make sure we are starting our offset at 0 so the offset qualifier we |
| 310 | // set on each variable in the uniform block is valid. |
| 311 | SkASSERT(0 == localUniform.fUBOffset); |
| 312 | firstOffsetCheck = true; |
Greg Daniel | aa352de | 2017-07-17 09:05:16 -0400 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | #endif |
| 316 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 317 | SkString uniformsString; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 318 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 319 | const UniformInfo& localUniform = fUniforms[i]; |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 320 | if (visibility & localUniform.fVisibility) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 321 | if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) { |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 322 | localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 323 | uniformsString.append(";\n"); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | } |
Greg Daniel | aa352de | 2017-07-17 09:05:16 -0400 | [diff] [blame] | 327 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 328 | if (!uniformsString.isEmpty()) { |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 329 | out->appendf("layout (set=%d, binding=%d) uniform uniformBuffer\n{\n", |
| 330 | kUniformBufferDescSet, kUniformBinding); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 331 | out->appendf("%s\n};\n", uniformsString.c_str()); |
| 332 | } |
egdaniel | 4ee1cda | 2016-02-26 08:18:49 -0800 | [diff] [blame] | 333 | } |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 334 | |
| 335 | uint32_t GrVkUniformHandler::getRTHeightOffset() const { |
| 336 | uint32_t result; |
| 337 | uint32_t currentOffset = fCurrentUBOOffset; |
| 338 | get_ubo_aligned_offset(&result, ¤tOffset, kFloat_GrSLType, 0); |
| 339 | return result; |
| 340 | } |