Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "GrVkUtil.h" |
| 9 | |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 10 | #include "vk/GrVkGpu.h" |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 11 | #include "SkSLCompiler.h" |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 12 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 13 | bool GrPixelConfigToVkFormat(GrPixelConfig config, VkFormat* format) { |
| 14 | VkFormat dontCare; |
| 15 | if (!format) { |
| 16 | format = &dontCare; |
| 17 | } |
| 18 | |
| 19 | switch (config) { |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 20 | case kUnknown_GrPixelConfig: |
| 21 | return false; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 22 | case kRGBA_8888_GrPixelConfig: |
| 23 | *format = VK_FORMAT_R8G8B8A8_UNORM; |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 24 | return true; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame^] | 25 | case kRGB_888_GrPixelConfig: |
| 26 | // TODO: VK_FORMAT_R8G8B8_UNORM |
| 27 | return false; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 28 | case kBGRA_8888_GrPixelConfig: |
| 29 | *format = VK_FORMAT_B8G8R8A8_UNORM; |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 30 | return true; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 31 | case kSRGBA_8888_GrPixelConfig: |
| 32 | *format = VK_FORMAT_R8G8B8A8_SRGB; |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 33 | return true; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 34 | case kSBGRA_8888_GrPixelConfig: |
| 35 | *format = VK_FORMAT_B8G8R8A8_SRGB; |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 36 | return true; |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 37 | case kRGBA_1010102_GrPixelConfig: |
| 38 | *format = VK_FORMAT_A2B10G10R10_UNORM_PACK32; |
| 39 | return true; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 40 | case kRGB_565_GrPixelConfig: |
| 41 | *format = VK_FORMAT_R5G6B5_UNORM_PACK16; |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 42 | return true; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 43 | case kRGBA_4444_GrPixelConfig: |
egdaniel | 3fe0327 | 2016-08-15 10:59:17 -0700 | [diff] [blame] | 44 | // R4G4B4A4 is not required to be supported so we actually |
| 45 | // store the data is if it was B4G4R4A4 and swizzle in shaders |
| 46 | *format = VK_FORMAT_B4G4R4A4_UNORM_PACK16; |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 47 | return true; |
Greg Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 48 | case kAlpha_8_GrPixelConfig: // fall through |
| 49 | case kAlpha_8_as_Red_GrPixelConfig: |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 50 | *format = VK_FORMAT_R8_UNORM; |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 51 | return true; |
Greg Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 52 | case kAlpha_8_as_Alpha_GrPixelConfig: |
| 53 | return false; |
Brian Osman | 986563b | 2017-01-10 14:20:02 -0500 | [diff] [blame] | 54 | case kGray_8_GrPixelConfig: |
Greg Daniel | 7af060a | 2017-12-05 16:27:11 -0500 | [diff] [blame] | 55 | case kGray_8_as_Red_GrPixelConfig: |
Brian Osman | 986563b | 2017-01-10 14:20:02 -0500 | [diff] [blame] | 56 | *format = VK_FORMAT_R8_UNORM; |
| 57 | return true; |
Greg Daniel | 7af060a | 2017-12-05 16:27:11 -0500 | [diff] [blame] | 58 | case kGray_8_as_Lum_GrPixelConfig: |
| 59 | return false; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 60 | case kRGBA_float_GrPixelConfig: |
| 61 | *format = VK_FORMAT_R32G32B32A32_SFLOAT; |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 62 | return true; |
csmartdalton | 6aa0e11 | 2017-02-08 16:14:11 -0500 | [diff] [blame] | 63 | case kRG_float_GrPixelConfig: |
| 64 | *format = VK_FORMAT_R32G32_SFLOAT; |
| 65 | return true; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 66 | case kRGBA_half_GrPixelConfig: |
| 67 | *format = VK_FORMAT_R16G16B16A16_SFLOAT; |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 68 | return true; |
Greg Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 69 | case kAlpha_half_GrPixelConfig: // fall through |
| 70 | case kAlpha_half_as_Red_GrPixelConfig: |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 71 | *format = VK_FORMAT_R16_SFLOAT; |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 72 | return true; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 73 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 74 | SK_ABORT("Unexpected config"); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 75 | return false; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Greg Daniel | 8a3f55c | 2018-03-14 17:32:12 +0000 | [diff] [blame] | 78 | GrPixelConfig GrVkFormatToPixelConfig(VkFormat format) { |
| 79 | switch (format) { |
| 80 | case VK_FORMAT_R8G8B8A8_UNORM: |
| 81 | return kRGBA_8888_GrPixelConfig; |
| 82 | case VK_FORMAT_B8G8R8A8_UNORM: |
| 83 | return kBGRA_8888_GrPixelConfig; |
| 84 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 85 | return kSRGBA_8888_GrPixelConfig; |
| 86 | case VK_FORMAT_B8G8R8A8_SRGB: |
| 87 | return kSBGRA_8888_GrPixelConfig; |
| 88 | case VK_FORMAT_A2B10G10R10_UNORM_PACK32: |
| 89 | return kRGBA_1010102_GrPixelConfig; |
| 90 | case VK_FORMAT_R5G6B5_UNORM_PACK16: |
| 91 | return kRGB_565_GrPixelConfig; |
| 92 | break; |
| 93 | case VK_FORMAT_B4G4R4A4_UNORM_PACK16: |
| 94 | // R4G4B4A4 is not required to be supported so we actually |
| 95 | // store RGBA_4444 data as B4G4R4A4. |
| 96 | return kRGBA_4444_GrPixelConfig; |
| 97 | case VK_FORMAT_R8_UNORM: |
| 98 | return kAlpha_8_GrPixelConfig; |
| 99 | case VK_FORMAT_R32G32B32A32_SFLOAT: |
| 100 | return kRGBA_float_GrPixelConfig; |
| 101 | case VK_FORMAT_R32G32_SFLOAT: |
| 102 | return kRG_float_GrPixelConfig; |
| 103 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
| 104 | return kRGBA_half_GrPixelConfig; |
| 105 | case VK_FORMAT_R16_SFLOAT: |
| 106 | return kAlpha_half_GrPixelConfig; |
| 107 | default: |
| 108 | return kUnknown_GrPixelConfig; |
| 109 | } |
| 110 | } |
| 111 | |
Greg Daniel | 81b8059 | 2017-12-13 10:20:04 -0500 | [diff] [blame] | 112 | bool GrVkFormatPixelConfigPairIsValid(VkFormat format, GrPixelConfig config) { |
| 113 | switch (format) { |
| 114 | case VK_FORMAT_R8G8B8A8_UNORM: |
| 115 | return kRGBA_8888_GrPixelConfig == config; |
| 116 | case VK_FORMAT_B8G8R8A8_UNORM: |
| 117 | return kBGRA_8888_GrPixelConfig == config; |
| 118 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 119 | return kSRGBA_8888_GrPixelConfig == config; |
| 120 | case VK_FORMAT_B8G8R8A8_SRGB: |
| 121 | return kSBGRA_8888_GrPixelConfig == config; |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 122 | case VK_FORMAT_A2B10G10R10_UNORM_PACK32: |
| 123 | return kRGBA_1010102_GrPixelConfig == config; |
Greg Daniel | 81b8059 | 2017-12-13 10:20:04 -0500 | [diff] [blame] | 124 | case VK_FORMAT_R5G6B5_UNORM_PACK16: |
| 125 | return kRGB_565_GrPixelConfig == config; |
| 126 | case VK_FORMAT_B4G4R4A4_UNORM_PACK16: |
| 127 | // R4G4B4A4 is not required to be supported so we actually |
| 128 | // store RGBA_4444 data as B4G4R4A4. |
| 129 | return kRGBA_4444_GrPixelConfig == config; |
| 130 | case VK_FORMAT_R8_UNORM: |
| 131 | return kAlpha_8_GrPixelConfig == config || |
| 132 | kAlpha_8_as_Red_GrPixelConfig == config || |
| 133 | kGray_8_GrPixelConfig == config || |
| 134 | kGray_8_as_Red_GrPixelConfig == config; |
| 135 | case VK_FORMAT_R32G32B32A32_SFLOAT: |
| 136 | return kRGBA_float_GrPixelConfig == config; |
| 137 | case VK_FORMAT_R32G32_SFLOAT: |
| 138 | return kRG_float_GrPixelConfig == config; |
| 139 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
| 140 | return kRGBA_half_GrPixelConfig == config; |
| 141 | case VK_FORMAT_R16_SFLOAT: |
| 142 | return kAlpha_half_GrPixelConfig == config || |
| 143 | kAlpha_half_as_Red_GrPixelConfig == config; |
| 144 | default: |
| 145 | return false; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | bool GrVkFormatIsSupported(VkFormat format) { |
| 150 | switch (format) { |
| 151 | case VK_FORMAT_R8G8B8A8_UNORM: |
| 152 | case VK_FORMAT_B8G8R8A8_UNORM: |
| 153 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 154 | case VK_FORMAT_B8G8R8A8_SRGB: |
| 155 | case VK_FORMAT_R8G8B8A8_SINT: |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 156 | case VK_FORMAT_A2B10G10R10_UNORM_PACK32: |
Greg Daniel | 81b8059 | 2017-12-13 10:20:04 -0500 | [diff] [blame] | 157 | case VK_FORMAT_R5G6B5_UNORM_PACK16: |
| 158 | case VK_FORMAT_B4G4R4A4_UNORM_PACK16: |
| 159 | case VK_FORMAT_R8_UNORM: |
| 160 | case VK_FORMAT_R32G32B32A32_SFLOAT: |
| 161 | case VK_FORMAT_R32G32_SFLOAT: |
| 162 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
| 163 | case VK_FORMAT_R16_SFLOAT: |
| 164 | return true; |
| 165 | default: |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 170 | bool GrVkFormatIsSRGB(VkFormat format, VkFormat* linearFormat) { |
| 171 | VkFormat linearFmt = format; |
| 172 | switch (format) { |
| 173 | case VK_FORMAT_R8_SRGB: |
| 174 | linearFmt = VK_FORMAT_R8_UNORM; |
| 175 | break; |
| 176 | case VK_FORMAT_R8G8_SRGB: |
| 177 | linearFmt = VK_FORMAT_R8G8_UNORM; |
| 178 | break; |
| 179 | case VK_FORMAT_R8G8B8_SRGB: |
| 180 | linearFmt = VK_FORMAT_R8G8B8_UNORM; |
| 181 | break; |
| 182 | case VK_FORMAT_B8G8R8_SRGB: |
| 183 | linearFmt = VK_FORMAT_B8G8R8_UNORM; |
| 184 | break; |
| 185 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 186 | linearFmt = VK_FORMAT_R8G8B8A8_UNORM; |
| 187 | break; |
| 188 | case VK_FORMAT_B8G8R8A8_SRGB: |
| 189 | linearFmt = VK_FORMAT_B8G8R8A8_UNORM; |
| 190 | break; |
| 191 | case VK_FORMAT_A8B8G8R8_SRGB_PACK32: |
| 192 | linearFmt = VK_FORMAT_A8B8G8R8_UNORM_PACK32; |
| 193 | break; |
| 194 | case VK_FORMAT_BC1_RGB_SRGB_BLOCK: |
| 195 | linearFmt = VK_FORMAT_BC1_RGB_UNORM_BLOCK; |
| 196 | break; |
| 197 | case VK_FORMAT_BC1_RGBA_SRGB_BLOCK: |
| 198 | linearFmt = VK_FORMAT_BC1_RGBA_UNORM_BLOCK; |
| 199 | break; |
| 200 | case VK_FORMAT_BC2_SRGB_BLOCK: |
| 201 | linearFmt = VK_FORMAT_BC2_UNORM_BLOCK; |
| 202 | break; |
| 203 | case VK_FORMAT_BC3_SRGB_BLOCK: |
| 204 | linearFmt = VK_FORMAT_BC3_UNORM_BLOCK; |
| 205 | break; |
| 206 | case VK_FORMAT_BC7_SRGB_BLOCK: |
| 207 | linearFmt = VK_FORMAT_BC7_UNORM_BLOCK; |
| 208 | break; |
| 209 | case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK: |
| 210 | linearFmt = VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK; |
| 211 | break; |
| 212 | case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK: |
| 213 | linearFmt = VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK; |
| 214 | break; |
| 215 | case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK: |
| 216 | linearFmt = VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK; |
| 217 | break; |
| 218 | case VK_FORMAT_ASTC_4x4_SRGB_BLOCK: |
| 219 | linearFmt = VK_FORMAT_ASTC_4x4_UNORM_BLOCK; |
| 220 | break; |
| 221 | case VK_FORMAT_ASTC_5x4_SRGB_BLOCK: |
| 222 | linearFmt = VK_FORMAT_ASTC_5x4_UNORM_BLOCK; |
| 223 | break; |
| 224 | case VK_FORMAT_ASTC_5x5_SRGB_BLOCK: |
| 225 | linearFmt = VK_FORMAT_ASTC_5x5_UNORM_BLOCK; |
| 226 | break; |
| 227 | case VK_FORMAT_ASTC_6x5_SRGB_BLOCK: |
| 228 | linearFmt = VK_FORMAT_ASTC_6x5_UNORM_BLOCK; |
| 229 | break; |
| 230 | case VK_FORMAT_ASTC_6x6_SRGB_BLOCK: |
| 231 | linearFmt = VK_FORMAT_ASTC_6x6_UNORM_BLOCK; |
| 232 | break; |
| 233 | case VK_FORMAT_ASTC_8x5_SRGB_BLOCK: |
| 234 | linearFmt = VK_FORMAT_ASTC_8x5_UNORM_BLOCK; |
| 235 | break; |
| 236 | case VK_FORMAT_ASTC_8x6_SRGB_BLOCK: |
| 237 | linearFmt = VK_FORMAT_ASTC_8x6_UNORM_BLOCK; |
| 238 | break; |
| 239 | case VK_FORMAT_ASTC_8x8_SRGB_BLOCK: |
| 240 | linearFmt = VK_FORMAT_ASTC_8x8_UNORM_BLOCK; |
| 241 | break; |
| 242 | case VK_FORMAT_ASTC_10x5_SRGB_BLOCK: |
| 243 | linearFmt = VK_FORMAT_ASTC_10x5_UNORM_BLOCK; |
| 244 | break; |
| 245 | case VK_FORMAT_ASTC_10x6_SRGB_BLOCK: |
| 246 | linearFmt = VK_FORMAT_ASTC_10x6_UNORM_BLOCK; |
| 247 | break; |
| 248 | case VK_FORMAT_ASTC_10x8_SRGB_BLOCK: |
| 249 | linearFmt = VK_FORMAT_ASTC_10x8_UNORM_BLOCK; |
| 250 | break; |
| 251 | case VK_FORMAT_ASTC_10x10_SRGB_BLOCK: |
| 252 | linearFmt = VK_FORMAT_ASTC_10x10_UNORM_BLOCK; |
| 253 | break; |
| 254 | case VK_FORMAT_ASTC_12x10_SRGB_BLOCK: |
| 255 | linearFmt = VK_FORMAT_ASTC_12x10_UNORM_BLOCK; |
| 256 | break; |
| 257 | case VK_FORMAT_ASTC_12x12_SRGB_BLOCK: |
| 258 | linearFmt = VK_FORMAT_ASTC_12x12_UNORM_BLOCK; |
| 259 | break; |
| 260 | default: |
| 261 | break; |
| 262 | } |
| 263 | if (linearFormat) { |
| 264 | *linearFormat = linearFmt; |
| 265 | } |
| 266 | return (linearFmt != format); |
| 267 | } |
| 268 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 269 | bool GrSampleCountToVkSampleCount(uint32_t samples, VkSampleCountFlagBits* vkSamples) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 270 | SkASSERT(samples >= 1); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 271 | switch (samples) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 272 | case 1: |
| 273 | *vkSamples = VK_SAMPLE_COUNT_1_BIT; |
| 274 | return true; |
| 275 | case 2: |
| 276 | *vkSamples = VK_SAMPLE_COUNT_2_BIT; |
| 277 | return true; |
| 278 | case 4: |
egdaniel | bf63e61 | 2016-08-17 06:26:16 -0700 | [diff] [blame] | 279 | *vkSamples = VK_SAMPLE_COUNT_4_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 280 | return true; |
| 281 | case 8: |
egdaniel | bf63e61 | 2016-08-17 06:26:16 -0700 | [diff] [blame] | 282 | *vkSamples = VK_SAMPLE_COUNT_8_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 283 | return true; |
| 284 | case 16: |
egdaniel | bf63e61 | 2016-08-17 06:26:16 -0700 | [diff] [blame] | 285 | *vkSamples = VK_SAMPLE_COUNT_16_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 286 | return true; |
| 287 | case 32: |
egdaniel | bf63e61 | 2016-08-17 06:26:16 -0700 | [diff] [blame] | 288 | *vkSamples = VK_SAMPLE_COUNT_32_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 289 | return true; |
| 290 | case 64: |
egdaniel | bf63e61 | 2016-08-17 06:26:16 -0700 | [diff] [blame] | 291 | *vkSamples = VK_SAMPLE_COUNT_64_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 292 | return true; |
| 293 | default: |
| 294 | return false; |
| 295 | } |
| 296 | } |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 297 | |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 298 | SkSL::Program::Kind vk_shader_stage_to_skiasl_kind(VkShaderStageFlagBits stage) { |
| 299 | if (VK_SHADER_STAGE_VERTEX_BIT == stage) { |
| 300 | return SkSL::Program::kVertex_Kind; |
| 301 | } |
Chris Dalton | 33607c6 | 2017-07-07 11:00:48 -0600 | [diff] [blame] | 302 | if (VK_SHADER_STAGE_GEOMETRY_BIT == stage) { |
| 303 | return SkSL::Program::kGeometry_Kind; |
| 304 | } |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 305 | SkASSERT(VK_SHADER_STAGE_FRAGMENT_BIT == stage); |
| 306 | return SkSL::Program::kFragment_Kind; |
| 307 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 308 | |
| 309 | VkShaderStageFlagBits skiasl_kind_to_vk_shader_stage(SkSL::Program::Kind kind) { |
| 310 | if (SkSL::Program::kVertex_Kind == kind) { |
| 311 | return VK_SHADER_STAGE_VERTEX_BIT; |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 312 | } |
Chris Dalton | 33607c6 | 2017-07-07 11:00:48 -0600 | [diff] [blame] | 313 | if (SkSL::Program::kGeometry_Kind == kind) { |
| 314 | return VK_SHADER_STAGE_GEOMETRY_BIT; |
| 315 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 316 | SkASSERT(SkSL::Program::kFragment_Kind == kind); |
| 317 | return VK_SHADER_STAGE_FRAGMENT_BIT; |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 318 | } |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 319 | |
| 320 | bool GrCompileVkShaderModule(const GrVkGpu* gpu, |
| 321 | const char* shaderString, |
| 322 | VkShaderStageFlagBits stage, |
| 323 | VkShaderModule* shaderModule, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 324 | VkPipelineShaderStageCreateInfo* stageInfo, |
| 325 | const SkSL::Program::Settings& settings, |
| 326 | SkSL::Program::Inputs* outInputs) { |
| 327 | std::unique_ptr<SkSL::Program> program = gpu->shaderCompiler()->convertProgram( |
| 328 | vk_shader_stage_to_skiasl_kind(stage), |
Brian Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 329 | SkSL::String(shaderString), |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 330 | settings); |
| 331 | if (!program) { |
| 332 | SkDebugf("SkSL error:\n%s\n", gpu->shaderCompiler()->errorText().c_str()); |
| 333 | SkASSERT(false); |
| 334 | } |
| 335 | *outInputs = program->fInputs; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 336 | SkSL::String code; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 337 | if (!gpu->shaderCompiler()->toSPIRV(*program, &code)) { |
| 338 | SkDebugf("%s\n", gpu->shaderCompiler()->errorText().c_str()); |
| 339 | return false; |
| 340 | } |
| 341 | |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 342 | VkShaderModuleCreateInfo moduleCreateInfo; |
| 343 | memset(&moduleCreateInfo, 0, sizeof(VkShaderModuleCreateInfo)); |
| 344 | moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
| 345 | moduleCreateInfo.pNext = nullptr; |
| 346 | moduleCreateInfo.flags = 0; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 347 | moduleCreateInfo.codeSize = code.size(); |
| 348 | moduleCreateInfo.pCode = (const uint32_t*)code.c_str(); |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 349 | |
| 350 | VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateShaderModule(gpu->device(), |
| 351 | &moduleCreateInfo, |
| 352 | nullptr, |
| 353 | shaderModule)); |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 354 | if (err) { |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | memset(stageInfo, 0, sizeof(VkPipelineShaderStageCreateInfo)); |
| 359 | stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 360 | stageInfo->pNext = nullptr; |
| 361 | stageInfo->flags = 0; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 362 | stageInfo->stage = skiasl_kind_to_vk_shader_stage(program->fKind); |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 363 | stageInfo->module = *shaderModule; |
| 364 | stageInfo->pName = "main"; |
| 365 | stageInfo->pSpecializationInfo = nullptr; |
| 366 | |
| 367 | return true; |
| 368 | } |