xref: /aosp_15_r20/external/skia/src/gpu/graphite/vk/VulkanGraphiteUtilsPriv.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2022 Google LLC
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 skgpu_graphite_VulkanGraphiteUtilsPriv_DEFINED
9 #define skgpu_graphite_VulkanGraphiteUtilsPriv_DEFINED
10 
11 #include "include/core/SkSpan.h"
12 #include "include/gpu/vk/VulkanTypes.h"
13 #include "src/gpu/graphite/DescriptorData.h"
14 #include "src/gpu/graphite/Log.h"
15 #include "src/gpu/vk/VulkanInterface.h"
16 
17 #include <string>
18 
19 // Helper macros to call functions on the VulkanInterface without checking for errors. Note: This
20 // cannot require a VulkanSharedContext because driver calls are needed before the shared context
21 // has been initialized.
22 #define VULKAN_CALL(IFACE, X) (IFACE)->fFunctions.f##X
23 
24 // Must be called before checkVkResult, since this does not log if the VulkanSharedContext is
25 // already considering the device to be lost.
26 #define VULKAN_LOG_IF_NOT_SUCCESS(SHARED_CONTEXT, RESULT, X, ...)                      \
27     do {                                                                               \
28         if (RESULT != VK_SUCCESS && !(SHARED_CONTEXT)->isDeviceLost()) {               \
29             SkDebugf("Failed vulkan call. Error: %d, " X "\n", RESULT, ##__VA_ARGS__); \
30         }                                                                              \
31     } while (false)
32 
33 #define VULKAN_CALL_RESULT(SHARED_CONTEXT, RESULT, X)                     \
34     do {                                                                  \
35         (RESULT) = VULKAN_CALL((SHARED_CONTEXT)->interface(), X);         \
36         SkASSERT(VK_SUCCESS == RESULT || VK_ERROR_DEVICE_LOST == RESULT); \
37         VULKAN_LOG_IF_NOT_SUCCESS(SHARED_CONTEXT, RESULT, #X);            \
38         (SHARED_CONTEXT)->checkVkResult(RESULT);                          \
39     } while (false)
40 
41 // same as VULKAN_CALL but checks for success
42 #define VULKAN_CALL_ERRCHECK(SHARED_CONTEXT, X) \
43     VkResult SK_MACRO_APPEND_LINE(ret);         \
44     VULKAN_CALL_RESULT(SHARED_CONTEXT, SK_MACRO_APPEND_LINE(ret), X)
45 
46 #define VULKAN_CALL_RESULT_NOCHECK(IFACE, RESULT, X) \
47     do {                                             \
48         (RESULT) = VULKAN_CALL(IFACE, X);            \
49     } while (false)
50 namespace skgpu::graphite {
51 
52 class VulkanSharedContext;
53 
54 VkShaderModule createVulkanShaderModule(const VulkanSharedContext*,
55                                         const std::string& spirv,
56                                         VkShaderStageFlagBits);
57 
58 VkDescriptorType DsTypeEnumToVkDs(DescriptorType);
59 void DescriptorDataToVkDescSetLayout(const VulkanSharedContext*,
60                                      const SkSpan<DescriptorData>&,
61                                      VkDescriptorSetLayout*);
62 
63 namespace ycbcrPackaging {
64 
65 // Functions to aid with packaging ycbcr information in a compact way.
66 int numInt32sNeeded(const VulkanYcbcrConversionInfo&);
67 
68 uint32_t nonFormatInfoAsUInt32(const VulkanYcbcrConversionInfo&);
69 
70 static constexpr int kUsesExternalFormatBits  = 1;
71 static constexpr int kYcbcrModelBits          = 3;
72 static constexpr int kYcbcrRangeBits          = 1;
73 static constexpr int kXChromaOffsetBits       = 1;
74 static constexpr int kYChromaOffsetBits       = 1;
75 static constexpr int kChromaFilterBits        = 1;
76 static constexpr int kForceExplicitReconBits  = 1;
77 static constexpr int kComponentBits           = 3;
78 
79 static constexpr int kUsesExternalFormatShift = 0;
80 static constexpr int kYcbcrModelShift         = kUsesExternalFormatShift +
81                                                 kUsesExternalFormatBits;
82 static constexpr int kYcbcrRangeShift         = kYcbcrModelShift         + kYcbcrModelBits;
83 static constexpr int kXChromaOffsetShift      = kYcbcrRangeShift         + kYcbcrRangeBits;
84 static constexpr int kYChromaOffsetShift      = kXChromaOffsetShift      + kXChromaOffsetBits;
85 static constexpr int kChromaFilterShift       = kYChromaOffsetShift      + kYChromaOffsetBits;
86 static constexpr int kForceExplicitReconShift = kChromaFilterShift       + kChromaFilterBits;
87 static constexpr int kComponentRShift         = kForceExplicitReconShift + kComponentBits;
88 static constexpr int kComponentGShift         = kComponentRShift         + kComponentBits;
89 static constexpr int kComponentBShift         = kComponentGShift         + kComponentBits;
90 static constexpr int kComponentAShift         = kComponentBShift         + kComponentBits;
91 
92 static constexpr uint32_t kUseExternalFormatMask =
93         ((1 << kUsesExternalFormatBits) - 1) << kUsesExternalFormatShift;
94 static constexpr uint32_t kYcbcrModelMask =
95         ((1 << kYcbcrModelBits) - 1) << kYcbcrModelShift;
96 static constexpr uint32_t kYcbcrRangeMask =
97         ((1 << kYcbcrRangeBits) - 1) << kYcbcrRangeShift;
98 static constexpr uint32_t kXChromaOffsetMask =
99         ((1 << kXChromaOffsetBits) - 1) << kXChromaOffsetShift;
100 static constexpr uint32_t kYChromaOffsetMask =
101         ((1 << kYChromaOffsetBits) - 1) << kYChromaOffsetShift;
102 static constexpr uint32_t kChromaFilterMask =
103         ((1 << kChromaFilterBits) - 1) << kChromaFilterShift;
104 static constexpr uint32_t kForceExplicitReconMask =
105         ((1 << kForceExplicitReconBits) - 1) << kForceExplicitReconShift;
106 static constexpr uint32_t kComponentRMask = ((1 << kComponentBits) - 1) << kComponentRShift;
107 static constexpr uint32_t kComponentBMask = ((1 << kComponentBits) - 1) << kComponentGShift;
108 static constexpr uint32_t kComponentGMask = ((1 << kComponentBits) - 1) << kComponentBShift;
109 static constexpr uint32_t kComponentAMask = ((1 << kComponentBits) - 1) << kComponentAShift;
110 } // namespace ycbcrPackaging
111 
112 bool vkFormatIsSupported(VkFormat);
113 
114 VkShaderStageFlags PipelineStageFlagsToVkShaderStageFlags(SkEnumBitMask<PipelineStageFlags>);
115 
116 } // namespace skgpu::graphite
117 
118 #endif // skgpu_graphite_VulkanGraphiteUtilsPriv_DEFINED
119