1 /* 2 * Copyright 2021 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_DrawPass_DEFINED 9 #define skgpu_graphite_DrawPass_DEFINED 10 11 #include "include/core/SkColor.h" 12 #include "include/core/SkRect.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/private/base/SkTArray.h" 15 #include "src/base/SkEnumBitMask.h" 16 #include "src/gpu/graphite/DrawCommands.h" 17 #include "src/gpu/graphite/DrawTypes.h" 18 #include "src/gpu/graphite/GraphicsPipelineDesc.h" 19 #include "src/gpu/graphite/ResourceTypes.h" 20 #include "src/gpu/graphite/TextureProxy.h" 21 22 #include <memory> 23 24 struct SkImageInfo; 25 26 namespace skgpu::graphite { 27 28 class BoundsManager; 29 class CommandBuffer; 30 class DrawList; 31 class GraphicsPipeline; 32 class Recorder; 33 struct RenderPassDesc; 34 class ResourceProvider; 35 class RuntimeEffectDictionary; 36 class Sampler; 37 class TextureDataBlock; 38 class Texture; 39 enum class UniformSlot; 40 41 /** 42 * DrawPass is analogous to a subpass, storing the drawing operations in the order they are stored 43 * in the eventual command buffer, as well as the surface proxy the operations are intended for. 44 * DrawPasses are grouped into a RenderPassTask for execution within a single render pass if the 45 * subpasses are compatible with each other. 46 * 47 * Unlike DrawList, DrawPasses are immutable and represent as closely as possible what will be 48 * stored in the command buffer while being flexible as to how the pass is incorporated. Depending 49 * on the backend, it may even be able to write accumulated vertex and uniform data directly to 50 * mapped GPU memory, although that is the extent of the CPU->GPU work they perform before they are 51 * executed by a RenderPassTask. 52 */ 53 class DrawPass { 54 public: 55 ~DrawPass(); 56 57 // Create a DrawPass that renders the DrawList into `target` with the given load/store ops and 58 // clear color. If the DrawList has draws that required a dst readback texture copy to sample 59 // from in the shader, it must be provided in `dstCopy` and a copy task must be executed before 60 // the DrawPass is executed. 61 static std::unique_ptr<DrawPass> Make(Recorder*, 62 std::unique_ptr<DrawList>, 63 sk_sp<TextureProxy> target, 64 const SkImageInfo& targetInfo, 65 std::pair<LoadOp, StoreOp>, 66 std::array<float, 4> clearColor, 67 sk_sp<TextureProxy> dstCopy, 68 SkIPoint dstCopyOffset); 69 70 // Defined relative to the top-left corner of the surface the DrawPass renders to, and is 71 // contained within its dimensions. bounds()72 const SkIRect& bounds() const { return fBounds; } target()73 TextureProxy* target() const { return fTarget.get(); } ops()74 std::pair<LoadOp, StoreOp> ops() const { return fOps; } clearColor()75 std::array<float, 4> clearColor() const { return fClearColor; } 76 requiresDstTexture()77 bool requiresDstTexture() const { return false; } requiresMSAA()78 bool requiresMSAA() const { return fRequiresMSAA; } 79 depthStencilFlags()80 SkEnumBitMask<DepthStencilFlags> depthStencilFlags() const { return fDepthStencilFlags; } 81 vertexBufferSize()82 size_t vertexBufferSize() const { return 0; } uniformBufferSize()83 size_t uniformBufferSize() const { return 0; } 84 85 // Instantiate and prepare any resources used by the DrawPass that require the Recorder's 86 // ResourceProvider. This includes things likes GraphicsPipelines, sampled Textures, Samplers, 87 // etc. 88 bool prepareResources(ResourceProvider*, 89 const RuntimeEffectDictionary*, 90 const RenderPassDesc&); 91 commands()92 DrawPassCommands::List::Iter commands() const { 93 return fCommandList.commands(); 94 } 95 getPipeline(size_t index)96 const GraphicsPipeline* getPipeline(size_t index) const { 97 return fFullPipelines[index].get(); 98 } 99 const Texture* getTexture(size_t index) const; 100 const Sampler* getSampler(size_t index) const; 101 sampledTextures()102 skia_private::TArray<sk_sp<TextureProxy>> sampledTextures() const { return fSampledTextures; } 103 104 void addResourceRefs(CommandBuffer*) const; 105 106 private: 107 class SortKey; 108 109 DrawPass(sk_sp<TextureProxy> target, 110 std::pair<LoadOp, StoreOp> ops, 111 std::array<float, 4> clearColor); 112 113 DrawPassCommands::List fCommandList; 114 115 sk_sp<TextureProxy> fTarget; 116 SkIRect fBounds; 117 118 std::pair<LoadOp, StoreOp> fOps; 119 std::array<float, 4> fClearColor; 120 121 SkEnumBitMask<DepthStencilFlags> fDepthStencilFlags = DepthStencilFlags::kNone; 122 bool fRequiresMSAA = false; 123 124 // The pipelines are referenced by index in BindGraphicsPipeline, but that will index into a 125 // an array of actual GraphicsPipelines. 126 skia_private::TArray<GraphicsPipelineDesc> fPipelineDescs; 127 skia_private::TArray<SamplerDesc> fSamplerDescs; 128 129 // These resources all get instantiated during prepareResources. 130 skia_private::TArray<sk_sp<GraphicsPipeline>> fFullPipelines; 131 skia_private::TArray<sk_sp<TextureProxy>> fSampledTextures; 132 skia_private::TArray<sk_sp<Sampler>> fSamplers; 133 }; 134 135 } // namespace skgpu::graphite 136 137 #endif // skgpu_graphite_DrawPass_DEFINED 138