Debian Codegraph @

src/gpu/graphite/vk/VulkanGraphiteUtils.h

raw
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/graphite/vk/VulkanGraphiteTypes.h"
13 #include "include/gpu/vk/VulkanTypes.h"
14 #include "src/gpu/graphite/DescriptorData.h"
15 #include "src/gpu/graphite/Log.h"
16 #include "src/gpu/vk/VulkanInterface.h"
17 #include "src/sksl/codegen/SkSLNativeShader.h"
18
19 #include <string>
20
21 class SkStream;
22 class SkWStream;
23
24 // Uncomment to log all Vulkan commands from Graphite
25 // #define VULKAN_DEBUG_LOG(X) SkDebugf("vk%s (%s:%d)\n", #X, __FILE__, __LINE__)
26
27 // Or uncomment to trace all Vulkan commands from Graphite
28 // TODO(b/471244369): expose this functionality as a build argument.
29 // #include "include/private/base/SkMacros.h"
30 // #include "src/core/SkTraceEvent.h"
31 // #define VULKAN_DEBUG_LOG(X) \
32 // TRACE_EVENT1_ALWAYS("skia.gpu", "vk" #X, "line", __FILE__ ":" SK_MACRO_STRINGIFY(__LINE__))
33
34 // Helper macros to call functions on the VulkanInterface without checking for errors. Note: This
35 // cannot require a VulkanSharedContext because driver calls are needed before the shared context
36 // has been initialized.
37 #ifdef VULKAN_DEBUG_LOG
38 #define VULKAN_CALL(IFACE, X) \
39 ([&]() { \
40 VULKAN_DEBUG_LOG(X); \
41 return (IFACE)->fFunctions.f##X; \
42 })()
43 #else
44 #define VULKAN_CALL(IFACE, X) (IFACE)->fFunctions.f##X
45 #endif
46
47 // Must be called before checkVkResult, since this does not log if the VulkanSharedContext is
48 // already considering the device to be lost.
49 #define VULKAN_LOG_IF_NOT_SUCCESS(SHARED_CONTEXT, RESULT, X, ...) \
50 do { \
51 if (RESULT != VK_SUCCESS && !(SHARED_CONTEXT)->isDeviceLost()) { \
52 SkDebugf("Failed vulkan call. Error: %d, " X "\n", RESULT, ##__VA_ARGS__); \
53 } \
54 } while (false)
55
56 #define VULKAN_CALL_RESULT(SHARED_CONTEXT, RESULT, X) \
57 do { \
58 (RESULT) = VULKAN_CALL((SHARED_CONTEXT)->interface(), X); \
59 SkASSERT(VK_SUCCESS == RESULT || VK_ERROR_DEVICE_LOST == RESULT); \
60 VULKAN_LOG_IF_NOT_SUCCESS(SHARED_CONTEXT, RESULT, #X); \
61 (SHARED_CONTEXT)->checkVkResult(RESULT); \
62 } while (false)
63
64 // same as VULKAN_CALL but checks for success
65 #define VULKAN_CALL_ERRCHECK(SHARED_CONTEXT, X) \
66 VkResult SK_MACRO_APPEND_LINE(ret); \
67 VULKAN_CALL_RESULT(SHARED_CONTEXT, SK_MACRO_APPEND_LINE(ret), X)
68
69 #define VULKAN_CALL_RESULT_NOCHECK(IFACE, RESULT, X) \
70 do { \
71 (RESULT) = VULKAN_CALL(IFACE, X); \
72 } while (false)
73 namespace skgpu::graphite {
74
75 class VulkanSharedContext;
76 struct RenderPassDesc;
77 enum class TextureFormat : uint8_t;
78
79 VkShaderModule CreateVulkanShaderModule(const VulkanSharedContext*,
80 const SkSL::NativeShader& spirv,
81 VkShaderStageFlagBits);
82
83 VkDescriptorType DsTypeEnumToVkDs(DescriptorType);
84 void DescriptorDataToVkDescSetLayout(const VulkanSharedContext*,
85 const SkSpan<DescriptorData>&,
86 VkDescriptorSetLayout*);
87
88 TextureFormat VkFormatToTextureFormat(VkFormat);
89 VkFormat TextureFormatToVkFormat(TextureFormat);
90
91 VkImageAspectFlags GetVkImageAspectFlags(TextureFormat);
92
93 constexpr VkSampleCountFlagBits SampleCountToVkSampleCount(SampleCount sampleCount) {
94 static_assert(VK_SAMPLE_COUNT_1_BIT == (uint8_t) SampleCount::k1);
95 static_assert(VK_SAMPLE_COUNT_2_BIT == (uint8_t) SampleCount::k2);
96 static_assert(VK_SAMPLE_COUNT_4_BIT == (uint8_t) SampleCount::k4);
97 static_assert(VK_SAMPLE_COUNT_8_BIT == (uint8_t) SampleCount::k8);
98 static_assert(VK_SAMPLE_COUNT_16_BIT == (uint8_t) SampleCount::k16);
99 return static_cast<VkSampleCountFlagBits>((uint8_t) sampleCount);
100 }
101
102 VkShaderStageFlags PipelineStageFlagsToVkShaderStageFlags(SkEnumBitMask<PipelineStageFlags>);
103
104 // When multisampling is used, Graphite never retains the multisampled data at the end of the render
105 // pass. It is always resolved to the single sampled color attachment. If the next multisampled
106 // render pass requires the single sampled data to be loaded (for example to blend with), then the
107 // MSAA data is initialized from the resolve attachment. In that case,
108 // RenderPassDescWillLoadMSAAFromResolve returns true.
109 // When VK_EXT_multisampled_render_to_single_sampled is available, this load is automatically done
110 // by the driver/hardware (in which case RenderPassDescWillImplicitlyLoadMSAA returns true).
111 // Otherwise Graphite has to manually do that with a draw call.
112 bool RenderPassDescWillLoadMSAAFromResolve(const RenderPassDesc& renderPassDesc);
113 bool RenderPassDescWillImplicitlyLoadMSAA(const RenderPassDesc& renderPassDesc);
114 // Adjust the load/store ops of the render pass to something common, since they do not affect the
115 // pipeline and the render pass remains compatible.
116 RenderPassDesc MakePipelineCompatibleRenderPass(const RenderPassDesc& renderPassDesc);
117
118 namespace BackendTextures {
119 VkImage GetVkImage(const BackendTexture&);
120 VkImageLayout GetVkImageLayout(const BackendTexture&);
121 uint32_t GetVkQueueFamilyIndex(const BackendTexture&);
122 VulkanAlloc GetMemoryAlloc(const BackendTexture&);
123
124 void SetMutableState(BackendTexture*, const skgpu::MutableTextureState&);
125 sk_sp<skgpu::MutableTextureState> GetMutableState(const BackendTexture&);
126 } // namespace BackendTextures
127
128 } // namespace skgpu::graphite
129
130 #endif // skgpu_graphite_VulkanGraphiteUtilsPriv_DEFINED