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 #ifndef GrVkUtil_DEFINED
9 #define GrVkUtil_DEFINED
10
11 #include "include/gpu/GrTypes.h"
12 #include "include/gpu/vk/GrVkTypes.h"
13 #include "include/private/base/SkMacros.h"
14 #include "include/private/gpu/ganesh/GrTypesPriv.h"
15 #include "src/gpu/ganesh/GrColor.h"
16 #include "src/gpu/ganesh/GrDataUtils.h"
17 #include "src/gpu/vk/VulkanInterface.h"
18 #include "src/sksl/ir/SkSLProgram.h"
19
20 namespace SkSL { struct ProgramSettings; }
21 class GrVkGpu;
22
23 // makes a Vk call on the interface
24 #define GR_VK_CALL(IFACE, X) (IFACE)->fFunctions.f##X
25
26 // Note: must be called before checkVkResult, since this does not log if the GPU is already
27 // considering the device to be lost.
28 #define GR_VK_LOG_IF_NOT_SUCCESS(GPU, RESULT, X, ...) \
29 do { \
30 if (RESULT != VK_SUCCESS && !GPU->isDeviceLost()) { \
31 SkDebugf("Failed vulkan call. Error: %d, " X "\n", RESULT, ##__VA_ARGS__); \
32 } \
33 } while (false)
34
35 #define GR_VK_CALL_RESULT(GPU, RESULT, X) \
36 do { \
37 (RESULT) = GR_VK_CALL(GPU->vkInterface(), X); \
38 SkASSERT(VK_SUCCESS == RESULT || VK_ERROR_DEVICE_LOST == RESULT); \
39 GR_VK_LOG_IF_NOT_SUCCESS(GPU, RESULT, #X); \
40 GPU->checkVkResult(RESULT); \
41 } while (false)
42
43 #define GR_VK_CALL_RESULT_NOCHECK(GPU, RESULT, X) \
44 do { \
45 (RESULT) = GR_VK_CALL(GPU->vkInterface(), X); \
46 GPU->checkVkResult(RESULT); \
47 } while (false)
48
49 // same as GR_VK_CALL but checks for success
50 #define GR_VK_CALL_ERRCHECK(GPU, X) \
51 VkResult SK_MACRO_APPEND_LINE(ret); \
52 GR_VK_CALL_RESULT(GPU, SK_MACRO_APPEND_LINE(ret), X) \
53
54
55 bool GrVkFormatIsSupported(VkFormat);
56
GrVkFormatDesc(VkFormat vkFormat)57 static constexpr GrColorFormatDesc GrVkFormatDesc(VkFormat vkFormat) {
58 switch (vkFormat) {
59 case VK_FORMAT_R8G8B8A8_UNORM:
60 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm);
61 case VK_FORMAT_R8_UNORM:
62 return GrColorFormatDesc::MakeR(8, GrColorTypeEncoding::kUnorm);
63 case VK_FORMAT_B8G8R8A8_UNORM:
64 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm);
65 case VK_FORMAT_R5G6B5_UNORM_PACK16:
66 return GrColorFormatDesc::MakeRGB(5, 6, 5, GrColorTypeEncoding::kUnorm);
67 case VK_FORMAT_B5G6R5_UNORM_PACK16:
68 return GrColorFormatDesc::MakeRGB(5, 6, 5, GrColorTypeEncoding::kUnorm);
69 case VK_FORMAT_R16G16B16A16_SFLOAT:
70 return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kFloat);
71 case VK_FORMAT_R16_SFLOAT:
72 return GrColorFormatDesc::MakeR(16, GrColorTypeEncoding::kFloat);
73 case VK_FORMAT_R8G8B8_UNORM:
74 return GrColorFormatDesc::MakeRGB(8, GrColorTypeEncoding::kUnorm);
75 case VK_FORMAT_R8G8_UNORM:
76 return GrColorFormatDesc::MakeRG(8, GrColorTypeEncoding::kUnorm);
77 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
78 return GrColorFormatDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm);
79 case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
80 return GrColorFormatDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm);
81 case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
82 return GrColorFormatDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm);
83 case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
84 return GrColorFormatDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm);
85 case VK_FORMAT_R8G8B8A8_SRGB:
86 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kSRGBUnorm);
87 case VK_FORMAT_R16_UNORM:
88 return GrColorFormatDesc::MakeR(16, GrColorTypeEncoding::kUnorm);
89 case VK_FORMAT_R16G16_UNORM:
90 return GrColorFormatDesc::MakeRG(16, GrColorTypeEncoding::kUnorm);
91 case VK_FORMAT_R16G16B16A16_UNORM:
92 return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kUnorm);
93 case VK_FORMAT_R16G16_SFLOAT:
94 return GrColorFormatDesc::MakeRG(16, GrColorTypeEncoding::kFloat);
95
96 // Compressed texture formats are not expected to have a description.
97 case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK: return GrColorFormatDesc::MakeInvalid();
98 case VK_FORMAT_BC1_RGB_UNORM_BLOCK: return GrColorFormatDesc::MakeInvalid();
99 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK: return GrColorFormatDesc::MakeInvalid();
100
101 // This type only describes color channels.
102 case VK_FORMAT_S8_UINT: return GrColorFormatDesc::MakeInvalid();
103 case VK_FORMAT_D24_UNORM_S8_UINT: return GrColorFormatDesc::MakeInvalid();
104 case VK_FORMAT_D32_SFLOAT_S8_UINT: return GrColorFormatDesc::MakeInvalid();
105
106 default: return GrColorFormatDesc::MakeInvalid();
107 }
108 }
109
110 bool GrCompileVkShaderModule(GrVkGpu* gpu,
111 const std::string& shaderString,
112 VkShaderStageFlagBits stage,
113 VkShaderModule* shaderModule,
114 VkPipelineShaderStageCreateInfo* stageInfo,
115 const SkSL::ProgramSettings& settings,
116 std::string* outSPIRV,
117 SkSL::Program::Interface* outInterface);
118
119 bool GrInstallVkShaderModule(GrVkGpu* gpu,
120 const std::string& spirv,
121 VkShaderStageFlagBits stage,
122 VkShaderModule* shaderModule,
123 VkPipelineShaderStageCreateInfo* stageInfo);
124
125 #endif
126