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/SkMacros.h"
14 #include "src/gpu/GrColor.h"
15 #include "src/gpu/GrDataUtils.h"
16 #include "src/gpu/vk/GrVkInterface.h"
17 #include "src/sksl/ir/SkSLProgram.h"
18
19 class GrVkGpu;
20
21 // makes a Vk call on the interface
22 #define GR_VK_CALL(IFACE, X) (IFACE)->fFunctions.f##X
23 // same as GR_VK_CALL but checks for success
24 #ifdef SK_DEBUG
25 #define GR_VK_CALL_ERRCHECK(IFACE, X) \
26 VkResult SK_MACRO_APPEND_LINE(ret) = GR_VK_CALL(IFACE, X); \
27 SkASSERT(VK_SUCCESS == SK_MACRO_APPEND_LINE(ret))
28 #else
29 #define GR_VK_CALL_ERRCHECK(IFACE, X) (void) GR_VK_CALL(IFACE, X)
30 #endif
31
32 bool GrVkFormatIsSupported(VkFormat);
33
34 bool GrVkFormatNeedsYcbcrSampler(VkFormat format);
35
36 #ifdef SK_DEBUG
37 /**
38 * Returns true if the passed in VkFormat and GrColorType are compatible with each other.
39 */
40 bool GrVkFormatColorTypePairIsValid(VkFormat, GrColorType);
41 #endif
42
43 bool GrSampleCountToVkSampleCount(uint32_t samples, VkSampleCountFlagBits* vkSamples);
44
45 bool GrCompileVkShaderModule(const GrVkGpu* gpu,
46 const SkSL::String& shaderString,
47 VkShaderStageFlagBits stage,
48 VkShaderModule* shaderModule,
49 VkPipelineShaderStageCreateInfo* stageInfo,
50 const SkSL::Program::Settings& settings,
51 SkSL::String* outSPIRV,
52 SkSL::Program::Inputs* outInputs);
53
54 bool GrInstallVkShaderModule(const GrVkGpu* gpu,
55 const SkSL::String& spirv,
56 VkShaderStageFlagBits stage,
57 VkShaderModule* shaderModule,
58 VkPipelineShaderStageCreateInfo* stageInfo);
59
60 size_t GrVkBytesPerFormat(VkFormat);
61
62 /**
63 * Returns true if the format is compressed.
64 */
65 bool GrVkFormatIsCompressed(VkFormat);
66
67 /**
68 * Maps a vk format into the CompressionType enum if applicable.
69 */
70 bool GrVkFormatToCompressionType(VkFormat vkFormat, SkImage::CompressionType* compressionType);
71
72 #if GR_TEST_UTILS
GrVkFormatToStr(VkFormat vkFormat)73 static constexpr const char* GrVkFormatToStr(VkFormat vkFormat) {
74 switch (vkFormat) {
75 case VK_FORMAT_R8G8B8A8_UNORM: return "R8G8B8A8_UNORM";
76 case VK_FORMAT_R8_UNORM: return "R8_UNORM";
77 case VK_FORMAT_B8G8R8A8_UNORM: return "B8G8R8A8_UNORM";
78 case VK_FORMAT_R5G6B5_UNORM_PACK16: return "R5G6B5_UNORM_PACK16";
79 case VK_FORMAT_R16G16B16A16_SFLOAT: return "R16G16B16A16_SFLOAT";
80 case VK_FORMAT_R16_SFLOAT: return "R16_SFLOAT";
81 case VK_FORMAT_R8G8B8_UNORM: return "R8G8B8_UNORM";
82 case VK_FORMAT_R8G8_UNORM: return "R8G8_UNORM";
83 case VK_FORMAT_A2B10G10R10_UNORM_PACK32: return "A2B10G10R10_UNORM_PACK32";
84 case VK_FORMAT_B4G4R4A4_UNORM_PACK16: return "B4G4R4A4_UNORM_PACK16";
85 case VK_FORMAT_R4G4B4A4_UNORM_PACK16: return "R4G4B4A4_UNORM_PACK16";
86 case VK_FORMAT_R32G32B32A32_SFLOAT: return "R32G32B32A32_SFLOAT";
87 case VK_FORMAT_R8G8B8A8_SRGB: return "R8G8B8A8_SRGB";
88 case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK: return "ETC2_R8G8B8_UNORM_BLOCK";
89 case VK_FORMAT_R16_UNORM: return "R16_UNORM";
90 case VK_FORMAT_R16G16_UNORM: return "R16G16_UNORM";
91 case VK_FORMAT_R16G16B16A16_UNORM: return "R16G16B16A16_UNORM";
92 case VK_FORMAT_R16G16_SFLOAT: return "R16G16_SFLOAT";
93
94 default: return "Unknown";
95 }
96 }
97
98 #endif
99 #endif
100