xref: /aosp_15_r20/external/skia/src/gpu/graphite/vk/VulkanGraphicsPipeline.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/DrawTypes.h"
17 #include "src/gpu/graphite/GraphicsPipeline.h"
18 #include "src/gpu/graphite/vk/VulkanGraphiteUtilsPriv.h"
19 #include "src/gpu/graphite/vk/VulkanSampler.h"
20 
21 namespace SkSL {
22     class Compiler;
23 }
24 
25 namespace skgpu::graphite {
26 
27 class Attribute;
28 class GraphicsPipelineDesc;
29 class RuntimeEffectDictionary;
30 class VulkanResourceProvider;
31 class VulkanSharedContext;
32 struct RenderPassDesc;
33 class TextureInfo;
34 class VulkanRenderPass;
35 
36 class VulkanGraphicsPipeline final : public GraphicsPipeline {
37 public:
38     inline static constexpr unsigned int kIntrinsicUniformBufferIndex = 0;
39     inline static constexpr unsigned int kRenderStepUniformBufferIndex = 1;
40     inline static constexpr unsigned int kPaintUniformBufferIndex = 2;
41     inline static constexpr unsigned int kGradientBufferIndex = 3;
42     inline static constexpr unsigned int kNumUniformBuffers = 4;
43 
44     // For now, rigidly assign all uniform buffer descriptors to be in set 0 and all
45     // texture/samplers to be in set 1.
46     // TODO(b/274762935): Make the bindings and descriptor set organization more flexible.
47     inline static constexpr unsigned int kUniformBufferDescSetIndex = 0;
48     inline static constexpr unsigned int kTextureBindDescSetIndex = 1;
49     // Currently input attachments are only used for loading MSAA from resolve, so we can use the
50     // descriptor set index normally assigned to uniform desc sets.
51     inline static constexpr unsigned int kInputAttachmentDescSetIndex = kUniformBufferDescSetIndex;
52 
53     inline static constexpr unsigned int kVertexBufferIndex = 0;
54     inline static constexpr unsigned int kInstanceBufferIndex = 1;
55     inline static constexpr unsigned int kNumInputBuffers = 2;
56 
57     inline static const DescriptorData kIntrinsicUniformBufferDescriptor = {
58             DescriptorType::kUniformBuffer, /*count=*/1,
59             kIntrinsicUniformBufferIndex,
60             PipelineStageFlags::kVertexShader | PipelineStageFlags::kFragmentShader};
61 
62     // Currently we only ever have one input attachment descriptor by itself within a set, so its
63     // binding index will always be 0.
64     inline static constexpr unsigned int kInputAttachmentBindingIndex = 0;
65     inline static const DescriptorData kInputAttachmentDescriptor = {
66             DescriptorType::kInputAttachment, /*count=*/1,
67             kInputAttachmentBindingIndex,
68             PipelineStageFlags::kFragmentShader};
69 
70     static sk_sp<VulkanGraphicsPipeline> Make(VulkanResourceProvider*,
71                                               const RuntimeEffectDictionary*,
72                                               const GraphicsPipelineDesc&,
73                                               const RenderPassDesc&,
74                                               SkEnumBitMask<PipelineCreationFlags>);
75 
76     static sk_sp<VulkanGraphicsPipeline> MakeLoadMSAAPipeline(
77             const VulkanSharedContext*,
78             VkShaderModule vsModule,
79             VkShaderModule fsModule,
80             VkPipelineShaderStageCreateInfo* pipelineShaderStages,
81             VkPipelineLayout,
82             sk_sp<VulkanRenderPass> compatibleRenderPass,
83             VkPipelineCache,
84             const TextureInfo& dstColorAttachmentTexInfo);
85 
86     static bool InitializeMSAALoadPipelineStructs(
87             const VulkanSharedContext*,
88             VkShaderModule* outVertexShaderModule,
89             VkShaderModule* outFragShaderModule,
90             VkPipelineShaderStageCreateInfo* outShaderStageInfo,
91             VkPipelineLayout* outPipelineLayout);
92 
~VulkanGraphicsPipeline()93     ~VulkanGraphicsPipeline() override {}
94 
layout()95     VkPipelineLayout layout() const {
96         SkASSERT(fPipelineLayout != VK_NULL_HANDLE);
97         return fPipelineLayout;
98     }
99 
pipeline()100     VkPipeline pipeline() const {
101         SkASSERT(fPipeline != VK_NULL_HANDLE);
102         return fPipeline;
103     }
104 
105 private:
106     VulkanGraphicsPipeline(const VulkanSharedContext* sharedContext,
107                            const PipelineInfo& pipelineInfo,
108                            VkPipelineLayout,
109                            VkPipeline,
110                            bool ownsPipelineLayout,
111                            skia_private::TArray<sk_sp<VulkanSampler>> immutableSamplers);
112 
113     void freeGpuData() override;
114 
115     VkPipelineLayout fPipelineLayout = VK_NULL_HANDLE;
116     VkPipeline fPipeline = VK_NULL_HANDLE;
117     bool fOwnsPipelineLayout = true;
118 
119     // Hold a ref to immutable samplers used such that their lifetime is properly managed.
120     const skia_private::TArray<sk_sp<VulkanSampler>> fImmutableSamplers;
121 };
122 
123 } // namespace skgpu::graphite
124 
125 #endif // skgpu_graphite_MtlGraphicsPipeline_DEFINED
126