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: |
Greg Daniel | f18c297 | 2018-09-25 19:16:43 +0000 | [diff] [blame] | 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 | 81b8059 | 2017-12-13 10:20:04 -0500 | [diff] [blame] | 78 | bool GrVkFormatPixelConfigPairIsValid(VkFormat format, GrPixelConfig config) { |
| 79 | switch (format) { |
| 80 | case VK_FORMAT_R8G8B8A8_UNORM: |
| 81 | return kRGBA_8888_GrPixelConfig == config; |
| 82 | case VK_FORMAT_B8G8R8A8_UNORM: |
| 83 | return kBGRA_8888_GrPixelConfig == config; |
| 84 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 85 | return kSRGBA_8888_GrPixelConfig == config; |
| 86 | case VK_FORMAT_B8G8R8A8_SRGB: |
| 87 | return kSBGRA_8888_GrPixelConfig == config; |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 88 | case VK_FORMAT_A2B10G10R10_UNORM_PACK32: |
| 89 | return kRGBA_1010102_GrPixelConfig == config; |
Greg Daniel | 81b8059 | 2017-12-13 10:20:04 -0500 | [diff] [blame] | 90 | case VK_FORMAT_R5G6B5_UNORM_PACK16: |
| 91 | return kRGB_565_GrPixelConfig == config; |
| 92 | case VK_FORMAT_B4G4R4A4_UNORM_PACK16: |
| 93 | // R4G4B4A4 is not required to be supported so we actually |
| 94 | // store RGBA_4444 data as B4G4R4A4. |
| 95 | return kRGBA_4444_GrPixelConfig == config; |
| 96 | case VK_FORMAT_R8_UNORM: |
| 97 | return kAlpha_8_GrPixelConfig == config || |
| 98 | kAlpha_8_as_Red_GrPixelConfig == config || |
| 99 | kGray_8_GrPixelConfig == config || |
| 100 | kGray_8_as_Red_GrPixelConfig == config; |
| 101 | case VK_FORMAT_R32G32B32A32_SFLOAT: |
| 102 | return kRGBA_float_GrPixelConfig == config; |
| 103 | case VK_FORMAT_R32G32_SFLOAT: |
| 104 | return kRG_float_GrPixelConfig == config; |
| 105 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
| 106 | return kRGBA_half_GrPixelConfig == config; |
| 107 | case VK_FORMAT_R16_SFLOAT: |
| 108 | return kAlpha_half_GrPixelConfig == config || |
| 109 | kAlpha_half_as_Red_GrPixelConfig == config; |
| 110 | default: |
| 111 | return false; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | bool GrVkFormatIsSupported(VkFormat format) { |
| 116 | switch (format) { |
| 117 | case VK_FORMAT_R8G8B8A8_UNORM: |
| 118 | case VK_FORMAT_B8G8R8A8_UNORM: |
| 119 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 120 | case VK_FORMAT_B8G8R8A8_SRGB: |
| 121 | case VK_FORMAT_R8G8B8A8_SINT: |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 122 | case VK_FORMAT_A2B10G10R10_UNORM_PACK32: |
Greg Daniel | 81b8059 | 2017-12-13 10:20:04 -0500 | [diff] [blame] | 123 | case VK_FORMAT_R5G6B5_UNORM_PACK16: |
| 124 | case VK_FORMAT_B4G4R4A4_UNORM_PACK16: |
| 125 | case VK_FORMAT_R8_UNORM: |
| 126 | case VK_FORMAT_R32G32B32A32_SFLOAT: |
| 127 | case VK_FORMAT_R32G32_SFLOAT: |
| 128 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
| 129 | case VK_FORMAT_R16_SFLOAT: |
| 130 | return true; |
| 131 | default: |
| 132 | return false; |
| 133 | } |
| 134 | } |
| 135 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 136 | bool GrSampleCountToVkSampleCount(uint32_t samples, VkSampleCountFlagBits* vkSamples) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 137 | SkASSERT(samples >= 1); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 138 | switch (samples) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 139 | case 1: |
| 140 | *vkSamples = VK_SAMPLE_COUNT_1_BIT; |
| 141 | return true; |
| 142 | case 2: |
| 143 | *vkSamples = VK_SAMPLE_COUNT_2_BIT; |
| 144 | return true; |
| 145 | case 4: |
egdaniel | bf63e61 | 2016-08-17 06:26:16 -0700 | [diff] [blame] | 146 | *vkSamples = VK_SAMPLE_COUNT_4_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 147 | return true; |
| 148 | case 8: |
egdaniel | bf63e61 | 2016-08-17 06:26:16 -0700 | [diff] [blame] | 149 | *vkSamples = VK_SAMPLE_COUNT_8_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 150 | return true; |
| 151 | case 16: |
egdaniel | bf63e61 | 2016-08-17 06:26:16 -0700 | [diff] [blame] | 152 | *vkSamples = VK_SAMPLE_COUNT_16_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 153 | return true; |
| 154 | case 32: |
egdaniel | bf63e61 | 2016-08-17 06:26:16 -0700 | [diff] [blame] | 155 | *vkSamples = VK_SAMPLE_COUNT_32_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 156 | return true; |
| 157 | case 64: |
egdaniel | bf63e61 | 2016-08-17 06:26:16 -0700 | [diff] [blame] | 158 | *vkSamples = VK_SAMPLE_COUNT_64_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 159 | return true; |
| 160 | default: |
| 161 | return false; |
| 162 | } |
| 163 | } |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 164 | |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 165 | SkSL::Program::Kind vk_shader_stage_to_skiasl_kind(VkShaderStageFlagBits stage) { |
| 166 | if (VK_SHADER_STAGE_VERTEX_BIT == stage) { |
| 167 | return SkSL::Program::kVertex_Kind; |
| 168 | } |
Chris Dalton | 33607c6 | 2017-07-07 11:00:48 -0600 | [diff] [blame] | 169 | if (VK_SHADER_STAGE_GEOMETRY_BIT == stage) { |
| 170 | return SkSL::Program::kGeometry_Kind; |
| 171 | } |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 172 | SkASSERT(VK_SHADER_STAGE_FRAGMENT_BIT == stage); |
| 173 | return SkSL::Program::kFragment_Kind; |
| 174 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 175 | |
| 176 | VkShaderStageFlagBits skiasl_kind_to_vk_shader_stage(SkSL::Program::Kind kind) { |
| 177 | if (SkSL::Program::kVertex_Kind == kind) { |
| 178 | return VK_SHADER_STAGE_VERTEX_BIT; |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 179 | } |
Chris Dalton | 33607c6 | 2017-07-07 11:00:48 -0600 | [diff] [blame] | 180 | if (SkSL::Program::kGeometry_Kind == kind) { |
| 181 | return VK_SHADER_STAGE_GEOMETRY_BIT; |
| 182 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 183 | SkASSERT(SkSL::Program::kFragment_Kind == kind); |
| 184 | return VK_SHADER_STAGE_FRAGMENT_BIT; |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 185 | } |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 186 | |
| 187 | bool GrCompileVkShaderModule(const GrVkGpu* gpu, |
| 188 | const char* shaderString, |
| 189 | VkShaderStageFlagBits stage, |
| 190 | VkShaderModule* shaderModule, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 191 | VkPipelineShaderStageCreateInfo* stageInfo, |
| 192 | const SkSL::Program::Settings& settings, |
| 193 | SkSL::Program::Inputs* outInputs) { |
| 194 | std::unique_ptr<SkSL::Program> program = gpu->shaderCompiler()->convertProgram( |
| 195 | vk_shader_stage_to_skiasl_kind(stage), |
Brian Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 196 | SkSL::String(shaderString), |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 197 | settings); |
| 198 | if (!program) { |
| 199 | SkDebugf("SkSL error:\n%s\n", gpu->shaderCompiler()->errorText().c_str()); |
| 200 | SkASSERT(false); |
| 201 | } |
| 202 | *outInputs = program->fInputs; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 203 | SkSL::String code; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 204 | if (!gpu->shaderCompiler()->toSPIRV(*program, &code)) { |
| 205 | SkDebugf("%s\n", gpu->shaderCompiler()->errorText().c_str()); |
| 206 | return false; |
| 207 | } |
| 208 | |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 209 | VkShaderModuleCreateInfo moduleCreateInfo; |
| 210 | memset(&moduleCreateInfo, 0, sizeof(VkShaderModuleCreateInfo)); |
| 211 | moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
| 212 | moduleCreateInfo.pNext = nullptr; |
| 213 | moduleCreateInfo.flags = 0; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 214 | moduleCreateInfo.codeSize = code.size(); |
| 215 | moduleCreateInfo.pCode = (const uint32_t*)code.c_str(); |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 216 | |
| 217 | VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateShaderModule(gpu->device(), |
| 218 | &moduleCreateInfo, |
| 219 | nullptr, |
| 220 | shaderModule)); |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 221 | if (err) { |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | memset(stageInfo, 0, sizeof(VkPipelineShaderStageCreateInfo)); |
| 226 | stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 227 | stageInfo->pNext = nullptr; |
| 228 | stageInfo->flags = 0; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 229 | stageInfo->stage = skiasl_kind_to_vk_shader_stage(program->fKind); |
egdaniel | 88987d8 | 2016-09-19 10:17:34 -0700 | [diff] [blame] | 230 | stageInfo->module = *shaderModule; |
| 231 | stageInfo->pName = "main"; |
| 232 | stageInfo->pSpecializationInfo = nullptr; |
| 233 | |
| 234 | return true; |
| 235 | } |