Debian Codegraph @

src/gpu/graphite/vk/VulkanGraphicsPipeline.h

raw
1 /*
2 * Copyright 2023 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_VulkanGraphicsPipeline_DEFINED
9 #define skgpu_graphite_VulkanGraphicsPipeline_DEFINED
10
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkSpan.h"
13 #include "include/gpu/vk/VulkanTypes.h"
14 #include "include/private/base/SkTArray.h"
15 #include "src/gpu/Blend.h"
16 #include "src/gpu/graphite/Attribute.h"
17 #include "src/gpu/graphite/DrawTypes.h"
18 #include "src/gpu/graphite/GraphicsPipeline.h"
19 #include "src/gpu/graphite/Renderer.h"
20 #include "src/gpu/graphite/vk/VulkanGraphiteUtils.h"
21 #include "src/gpu/graphite/vk/VulkanSampler.h"
22
23 namespace SkSL {
24 class Compiler;
25 }
26
27 namespace skgpu::graphite {
28
29 class Attribute;
30 class GraphicsPipelineDesc;
31 class RuntimeEffectDictionary;
32 class VulkanResourceProvider;
33 class VulkanSharedContext;
34 struct RenderPassDesc;
35 class TextureInfo;
36 class VulkanRenderPass;
37
38 // VulkanProgramInfo owns the underlying vulkan objects and are deleted automatically.
39 class VulkanProgramInfo {
40 public:
41 ~VulkanProgramInfo();
42
43 static std::unique_ptr<VulkanProgramInfo> Make(const VulkanSharedContext* sharedContext) {
44 SkASSERT(sharedContext);
45 return std::unique_ptr<VulkanProgramInfo>(new VulkanProgramInfo(sharedContext));
46 }
47
48 VkShaderModule vs() const { return fVS; }
49 VkShaderModule fs() const { return fFS; }
50 VkPipelineLayout layout() const { return fLayout; }
51
52 // Relinquishes ownership of the VkPipelineLayout and no longer holds a pointer to it.
53 VkPipelineLayout releaseLayout() {
54 VkPipelineLayout layout = fLayout;
55 fLayout = VK_NULL_HANDLE;
56 return layout;
57 }
58
59 // The modules and layout can be set at most once
60 bool setVertexShader(VkShaderModule vs) {
61 SkASSERT(fVS == VK_NULL_HANDLE);
62 fVS = vs;
63 return fVS != VK_NULL_HANDLE;
64 }
65 bool setFragmentShader(VkShaderModule fs) {
66 SkASSERT(fFS == VK_NULL_HANDLE);
67 fFS = fs;
68 return fFS != VK_NULL_HANDLE;
69 }
70 bool setLayout(VkPipelineLayout layout) {
71 SkASSERT(fLayout == VK_NULL_HANDLE);
72 fLayout = layout;
73 return fLayout != VK_NULL_HANDLE;
74 }
75
76 private:
77 VulkanProgramInfo(const VulkanProgramInfo&) = delete;
78 explicit VulkanProgramInfo(const VulkanSharedContext* sharedContext)
79 : fSharedContext(sharedContext) {}
80
81 const VulkanSharedContext* fSharedContext; // For cleanup
82
83 VkShaderModule fVS = VK_NULL_HANDLE;
84 VkShaderModule fFS = VK_NULL_HANDLE;
85 VkPipelineLayout fLayout = VK_NULL_HANDLE;
86 };
87
88 class VulkanGraphicsPipeline final : public GraphicsPipeline {
89 public:
90 inline static constexpr unsigned int kCombinedUniformIndex = 0; // Paint AND renderstep!
91 inline static constexpr unsigned int kGradientBufferIndex = 1;
92 inline static constexpr unsigned int kNumUniformBuffers = 2;
93
94 // For now, rigidly assign all descriptor types to be at statically-defined set indices.
95 // TODO(b/274762935): Make the bindings and descriptor set organization more flexible.
96 inline static constexpr unsigned int kDstAsInputDescSetIndex = 0;
97 inline static constexpr unsigned int kUniformBufferDescSetIndex = 1;
98 inline static constexpr unsigned int kTextureBindDescSetIndex = 2;
99 inline static constexpr unsigned int kLoadMsaaFromResolveInputDescSetIndex = 3;
100 inline static constexpr unsigned int kMaxNumDescSets = 4;
101
102 inline static constexpr unsigned int kStaticDataBufferIndex = 0;
103 inline static constexpr unsigned int kAppendDataBufferIndex = 1;
104 inline static constexpr unsigned int kNumInputBuffers = 2;
105
106 // Define a static DescriptorData to represent input attachments which have the same values
107 // across all pipelines (we currently only ever use one input attachment within a set).
108 inline static const DescriptorData kInputAttachmentDescriptor = {
109 DescriptorType::kInputAttachment, /*count=*/1,
110 /*bindingIdx=*/0, // We only expect to encounter one input attachment
111 PipelineStageFlags::kFragmentShader};
112
113 static sk_sp<VulkanGraphicsPipeline> Make(VulkanSharedContext*,
114 const RuntimeEffectDictionary*,
115 const UniqueKey&,
116 const GraphicsPipelineDesc&,
117 const RenderPassDesc&,
118 SkEnumBitMask<PipelineCreationFlags>,
119 uint32_t compilationID);
120
121 // The created program info can be provided to MakeLoadMSAAPipeline for reuse with different
122 // render pass descriptions. Returns null on failure.
123 static std::unique_ptr<VulkanProgramInfo> CreateLoadMSAAProgram(const VulkanSharedContext*);
124
125 static sk_sp<VulkanGraphicsPipeline> MakeLoadMSAAPipeline(
126 VulkanSharedContext*,
127 const VulkanProgramInfo& loadMSAAProgram,
128 const RenderPassDesc&);
129
130 ~VulkanGraphicsPipeline() override {}
131
132 VkPipelineLayout layout() const {
133 SkASSERT(fPipelineLayout != VK_NULL_HANDLE);
134 return fPipelineLayout;
135 }
136
137 VkPipeline pipeline() const {
138 SkASSERT(fPipeline != VK_NULL_HANDLE);
139 return fPipeline;
140 }
141
142 // Returns null if the ith sampler is not an immutable sampler.
143 const VulkanSampler* immutableSampler(int i) const {
144 return fImmutableSamplers[i].get();
145 }
146
147 // Update any dynamic state (including none, if dynamic state is not available) that has changed
148 // since the previously bound Graphite pipeline.
149 void updateDynamicState(const VulkanSharedContext*,
150 VkCommandBuffer commandBuffer,
151 const VulkanGraphicsPipeline* previous) const;
152
153 private:
154 using VertexInputBindingDescriptions =
155 skia_private::STArray<2, VkVertexInputBindingDescription2EXT>;
156 using VertexInputAttributeDescriptions =
157 skia_private::STArray<16, VkVertexInputAttributeDescription2EXT>;
158
159 VulkanGraphicsPipeline(const VulkanSharedContext* sharedContext,
160 const PipelineInfo& pipelineInfo,
161 std::string_view pipelineLabel,
162 VkPipelineLayout,
163 VkPipeline,
164 VkPipeline,
165 bool ownsPipelineLayout,
166 skia_private::TArray<sk_sp<VulkanSampler>>&& immutableSamplers,
167 RenderStep::RenderStepID renderStepID,
168 PrimitiveType primitiveType,
169 const DepthStencilSettings& depthStencilSettings,
170 VertexInputBindingDescriptions&& vertexBindingDescriptions,
171 VertexInputAttributeDescriptions&& vertexAttributeDescriptions);
172
173 void freeGpuData() override;
174
175 // The fragment shader can be null if no shading is performed by the pipeline.
176 // This function does not cleanup any of the VulkanProgramInfo's objects on success or failure.
177 static VkPipeline MakePipeline(VulkanSharedContext*,
178 const VulkanProgramInfo&,
179 int subpassIndex,
180 PrimitiveType,
181 VkVertexInputRate appendInputRate,
182 SkSpan<const Attribute> staticAttrs,
183 SkSpan<const Attribute> appendAttrs,
184 VertexInputBindingDescriptions& vertexBindingDescriptions,
185 VertexInputAttributeDescriptions& vertexAttributeDescriptions,
186 const DepthStencilSettings&,
187 const BlendInfo&,
188 const RenderPassDesc&,
189 VkPipeline*);
190
191 VkPipelineLayout fPipelineLayout = VK_NULL_HANDLE;
192 VkPipeline fPipeline = VK_NULL_HANDLE;
193 VkPipeline fShadersPipeline = VK_NULL_HANDLE;
194 bool fOwnsPipelineLayout = true;
195
196 // Hold a ref to immutable samplers used such that their lifetime is properly managed.
197 const skia_private::TArray<sk_sp<VulkanSampler>> fImmutableSamplers;
198
199 // State that needs to be set dynamically. When a new pipeline is bound, only the diff with the
200 // previous pipeline is set. This is not optimal, and eventually the front-end should calculate
201 // this diff and set the state directly instead of the graphics pipeline tracking it, at which
202 // point these members can be removed (b/414645289).
203 //
204 // Render step ID is used to quickly determine if the vertex attribute description needs an
205 // update. A few render steps have identical vertex attribute structures, so updating the
206 // vertex attribute descriptions on render step ID change can be slightly suboptimal. This can
207 // be further optimized by having a "render step ID -> unique vertex attribute structure" table
208 // in the future.
209 PrimitiveType fPrimitiveType;
210 DepthStencilSettings fDepthStencilSettings;
211 RenderStep::RenderStepID fRenderStepID;
212 // The Vulkan vertex attribute descriptions are cached to avoid recomputing them every time.
213 VertexInputBindingDescriptions fVertexBindingDescriptions;
214 VertexInputAttributeDescriptions fVertexAttributeDescriptions;
215 };
216
217 } // namespace skgpu::graphite
218
219 #endif // skgpu_graphite_MtlGraphicsPipeline_DEFINED