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 /**
61  * Returns true if the format is compressed.
62  */
63 bool GrVkFormatIsCompressed(VkFormat);
64 
65 /**
66  * Maps a vk format into the CompressionType enum if applicable.
67  */
68 bool GrVkFormatToCompressionType(VkFormat vkFormat, SkImage::CompressionType* compressionType);
69 
70 #if GR_TEST_UTILS
GrVkFormatToStr(VkFormat vkFormat)71 static constexpr const char* GrVkFormatToStr(VkFormat vkFormat) {
72     switch (vkFormat) {
73         case VK_FORMAT_R8G8B8A8_UNORM:           return "R8G8B8A8_UNORM";
74         case VK_FORMAT_R8_UNORM:                 return "R8_UNORM";
75         case VK_FORMAT_B8G8R8A8_UNORM:           return "B8G8R8A8_UNORM";
76         case VK_FORMAT_R5G6B5_UNORM_PACK16:      return "R5G6B5_UNORM_PACK16";
77         case VK_FORMAT_R16G16B16A16_SFLOAT:      return "R16G16B16A16_SFLOAT";
78         case VK_FORMAT_R16_SFLOAT:               return "R16_SFLOAT";
79         case VK_FORMAT_R8G8B8_UNORM:             return "R8G8B8_UNORM";
80         case VK_FORMAT_R8G8_UNORM:               return "R8G8_UNORM";
81         case VK_FORMAT_A2B10G10R10_UNORM_PACK32: return "A2B10G10R10_UNORM_PACK32";
82         case VK_FORMAT_B4G4R4A4_UNORM_PACK16:    return "B4G4R4A4_UNORM_PACK16";
83         case VK_FORMAT_R4G4B4A4_UNORM_PACK16:    return "R4G4B4A4_UNORM_PACK16";
84         case VK_FORMAT_R32G32B32A32_SFLOAT:      return "R32G32B32A32_SFLOAT";
85         case VK_FORMAT_R8G8B8A8_SRGB:            return "R8G8B8A8_SRGB";
86         case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:  return "ETC2_R8G8B8_UNORM_BLOCK";
87         case VK_FORMAT_R16_UNORM:                return "R16_UNORM";
88         case VK_FORMAT_R16G16_UNORM:             return "R16G16_UNORM";
89         case VK_FORMAT_R16G16B16A16_UNORM:       return "R16G16B16A16_UNORM";
90         case VK_FORMAT_R16G16_SFLOAT:            return "R16G16_SFLOAT";
91 
92         default:                                 return "Unknown";
93     }
94 }
95 
96 #endif
97 #endif
98