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