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