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