• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // The number of uint32s required to represent all relevant YCbCr conversion info depends upon
71 // whether we are using a known VkFormat or an external format. With a known format, we only
72 // require 2 - one for non-format information and another to store the VkFormat.
73 static constexpr int kInt32sNeededKnownFormat = 2;
74 // External formats are represented by a uint64 and require us to store format feature flags,
75 // meaning that we end up with 4 uint32s total - 1 for non-format info, 2 for the format, and 1
76 // for format feature flags.
77 static constexpr int kInt32sNeededExternalFormat = 3;
78 
79 static constexpr int kUsesExternalFormatBits  = 1;
80 static constexpr int kYcbcrModelBits          = 3;
81 static constexpr int kYcbcrRangeBits          = 1;
82 static constexpr int kXChromaOffsetBits       = 1;
83 static constexpr int kYChromaOffsetBits       = 1;
84 static constexpr int kChromaFilterBits        = 1;
85 static constexpr int kForceExplicitReconBits  = 1;
86 static constexpr int kComponentBits           = 3;
87 
88 static constexpr int kUsesExternalFormatShift = 0;
89 static constexpr int kYcbcrModelShift         = kUsesExternalFormatShift +
90                                                 kUsesExternalFormatBits;
91 static constexpr int kYcbcrRangeShift         = kYcbcrModelShift         + kYcbcrModelBits;
92 static constexpr int kXChromaOffsetShift      = kYcbcrRangeShift         + kYcbcrRangeBits;
93 static constexpr int kYChromaOffsetShift      = kXChromaOffsetShift      + kXChromaOffsetBits;
94 static constexpr int kChromaFilterShift       = kYChromaOffsetShift      + kYChromaOffsetBits;
95 static constexpr int kForceExplicitReconShift = kChromaFilterShift       + kChromaFilterBits;
96 static constexpr int kComponentRShift         = kForceExplicitReconShift + kComponentBits;
97 static constexpr int kComponentGShift         = kComponentRShift         + kComponentBits;
98 static constexpr int kComponentBShift         = kComponentGShift         + kComponentBits;
99 static constexpr int kComponentAShift         = kComponentBShift         + kComponentBits;
100 
101 static constexpr uint32_t kUseExternalFormatMask =
102         ((1 << kUsesExternalFormatBits) - 1) << kUsesExternalFormatShift;
103 static constexpr uint32_t kYcbcrModelMask =
104         ((1 << kYcbcrModelBits) - 1) << kYcbcrModelShift;
105 static constexpr uint32_t kYcbcrRangeMask =
106         ((1 << kYcbcrRangeBits) - 1) << kYcbcrRangeShift;
107 static constexpr uint32_t kXChromaOffsetMask =
108         ((1 << kXChromaOffsetBits) - 1) << kXChromaOffsetShift;
109 static constexpr uint32_t kYChromaOffsetMask =
110         ((1 << kYChromaOffsetBits) - 1) << kYChromaOffsetShift;
111 static constexpr uint32_t kChromaFilterMask =
112         ((1 << kChromaFilterBits) - 1) << kChromaFilterShift;
113 static constexpr uint32_t kForceExplicitReconMask =
114         ((1 << kForceExplicitReconBits) - 1) << kForceExplicitReconShift;
115 static constexpr uint32_t kComponentRMask = ((1 << kComponentBits) - 1) << kComponentRShift;
116 static constexpr uint32_t kComponentBMask = ((1 << kComponentBits) - 1) << kComponentGShift;
117 static constexpr uint32_t kComponentGMask = ((1 << kComponentBits) - 1) << kComponentBShift;
118 static constexpr uint32_t kComponentAMask = ((1 << kComponentBits) - 1) << kComponentAShift;
119 } // namespace ycbcrPackaging
120 
121 bool vkFormatIsSupported(VkFormat);
122 
123 VkShaderStageFlags PipelineStageFlagsToVkShaderStageFlags(SkEnumBitMask<PipelineStageFlags>);
124 
125 } // namespace skgpu::graphite
126 
127 #endif // skgpu_graphite_VulkanGraphiteUtilsPriv_DEFINED
128