• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CommandBuffer_DEFINED
9 #define skgpu_CommandBuffer_DEFINED
10 
11 #include "experimental/graphite/src/DrawTypes.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/private/SkTArray.h"
16 
17 struct SkIRect;
18 
19 namespace skgpu {
20 class Buffer;
21 class Gpu;
22 class GraphicsPipeline;
23 class Texture;
24 class TextureProxy;
25 
26 struct AttachmentDesc {
27     sk_sp<TextureProxy> fTextureProxy;
28     LoadOp fLoadOp;
29     StoreOp fStoreOp;
30 };
31 
32 struct RenderPassDesc {
33     AttachmentDesc fColorAttachment;
34     std::array<float, 4> fClearColor;
35     AttachmentDesc fColorResolveAttachment;
36 
37     AttachmentDesc fStencilDepthAttachment;
38     uint32_t fClearStencil;
39     float fClearDepth;
40 
41     // TODO:
42     // * bounds (TBD whether exact bounds vs. granular)
43     // * input attachments
44 };
45 
46 class CommandBuffer : public SkRefCnt {
47 public:
~CommandBuffer()48     ~CommandBuffer() override {
49         this->releaseResources();
50     }
51 
hasWork()52     bool hasWork() { return fHasWork; }
53 
trackResource(sk_sp<SkRefCnt> resource)54     void trackResource(sk_sp<SkRefCnt> resource) {
55         fTrackedResources.push_back(std::move(resource));
56     }
57 
58     void beginRenderPass(const RenderPassDesc&);
59     virtual void endRenderPass() = 0;
60 
61     //---------------------------------------------------------------
62     // Can only be used within renderpasses
63     //---------------------------------------------------------------
64     void bindGraphicsPipeline(sk_sp<GraphicsPipeline> graphicsPipeline);
65     void bindUniformBuffer(sk_sp<Buffer>, size_t bufferOffset);
66     void bindVertexBuffers(sk_sp<Buffer> vertexBuffer, size_t vertexOffset,
67                            sk_sp<Buffer> instanceBuffer, size_t instanceOffset);
68     void bindIndexBuffer(sk_sp<Buffer> indexBuffer, size_t bufferOffset);
69 
draw(PrimitiveType type,unsigned int baseVertex,unsigned int vertexCount)70     void draw(PrimitiveType type, unsigned int baseVertex, unsigned int vertexCount) {
71         this->onDraw(type, baseVertex, vertexCount);
72         fHasWork = true;
73     }
drawIndexed(PrimitiveType type,unsigned int baseIndex,unsigned int indexCount,unsigned int baseVertex)74     void drawIndexed(PrimitiveType type, unsigned int baseIndex, unsigned int indexCount,
75                      unsigned int baseVertex) {
76         this->onDrawIndexed(type, baseIndex, indexCount, baseVertex);
77         fHasWork = true;
78     }
drawInstanced(PrimitiveType type,unsigned int baseVertex,unsigned int vertexCount,unsigned int baseInstance,unsigned int instanceCount)79     void drawInstanced(PrimitiveType type, unsigned int baseVertex, unsigned int vertexCount,
80                        unsigned int baseInstance, unsigned int instanceCount) {
81         this->onDrawInstanced(type, baseVertex, vertexCount, baseInstance, instanceCount);
82         fHasWork = true;
83     }
drawIndexedInstanced(PrimitiveType type,unsigned int baseIndex,unsigned int indexCount,unsigned int baseVertex,unsigned int baseInstance,unsigned int instanceCount)84     void drawIndexedInstanced(PrimitiveType type, unsigned int baseIndex, unsigned int indexCount,
85                               unsigned int baseVertex, unsigned int baseInstance,
86                               unsigned int instanceCount) {
87         this->onDrawIndexedInstanced(type, baseIndex, indexCount, baseVertex, baseInstance,
88                                      instanceCount);
89         fHasWork = true;
90     }
91 
92     //---------------------------------------------------------------
93     // Can only be used outside renderpasses
94     //---------------------------------------------------------------
95     void copyTextureToBuffer(sk_sp<Texture>,
96                              SkIRect srcRect,
97                              sk_sp<Buffer>,
98                              size_t bufferOffset,
99                              size_t bufferRowBytes);
100 
101 protected:
102     CommandBuffer();
103 
104 private:
105     void releaseResources();
106 
107     virtual void onBeginRenderPass(const RenderPassDesc&) = 0;
108 
109     virtual void onBindGraphicsPipeline(const GraphicsPipeline*) = 0;
110     virtual void onBindUniformBuffer(const Buffer*, size_t bufferOffset) = 0;
111     virtual void onBindVertexBuffers(const Buffer* vertexBuffer, size_t vertexOffset,
112                                      const Buffer* instanceBuffer, size_t instanceOffset) = 0;
113     virtual void onBindIndexBuffer(const Buffer* indexBuffer, size_t bufferOffset) = 0;
114 
115     virtual void onDraw(PrimitiveType type, unsigned int baseVertex, unsigned int vertexCount) = 0;
116     virtual void onDrawIndexed(PrimitiveType type, unsigned int baseIndex, unsigned int indexCount,
117                                unsigned int baseVertex) = 0;
118     virtual void onDrawInstanced(PrimitiveType type,
119                                  unsigned int baseVertex, unsigned int vertexCount,
120                                  unsigned int baseInstance, unsigned int instanceCount) = 0;
121     virtual void onDrawIndexedInstanced(PrimitiveType type, unsigned int baseIndex,
122                                         unsigned int indexCount, unsigned int baseVertex,
123                                         unsigned int baseInstance, unsigned int instanceCount) = 0;
124 
125     virtual void onCopyTextureToBuffer(const Texture*,
126                                        SkIRect srcRect,
127                                        const Buffer*,
128                                        size_t bufferOffset,
129                                        size_t bufferRowBytes) = 0;
130 
131     bool fHasWork = false;
132 
133     inline static constexpr int kInitialTrackedResourcesCount = 32;
134     SkSTArray<kInitialTrackedResourcesCount, sk_sp<SkRefCnt>> fTrackedResources;
135 };
136 
137 } // namespace skgpu
138 
139 #endif // skgpu_CommandBuffer_DEFINED
140