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 #include "experimental/graphite/src/CommandBuffer.h"
9
10 #include "experimental/graphite/src/GraphicsPipeline.h"
11 #include "src/core/SkTraceEvent.h"
12
13 #include "experimental/graphite/src/Buffer.h"
14 #include "experimental/graphite/src/Sampler.h"
15 #include "experimental/graphite/src/Texture.h"
16 #include "experimental/graphite/src/TextureProxy.h"
17
18 namespace skgpu {
19
CommandBuffer()20 CommandBuffer::CommandBuffer() {}
21
~CommandBuffer()22 CommandBuffer::~CommandBuffer() {
23 this->releaseResources();
24 }
25
releaseResources()26 void CommandBuffer::releaseResources() {
27 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
28
29 fTrackedResources.reset();
30 }
31
trackResource(sk_sp<Resource> resource)32 void CommandBuffer::trackResource(sk_sp<Resource> resource) {
33 fTrackedResources.push_back(std::move(resource));
34 }
35
beginRenderPass(const RenderPassDesc & renderPassDesc,sk_sp<Texture> colorTexture,sk_sp<Texture> resolveTexture,sk_sp<Texture> depthStencilTexture)36 bool CommandBuffer::beginRenderPass(const RenderPassDesc& renderPassDesc,
37 sk_sp<Texture> colorTexture,
38 sk_sp<Texture> resolveTexture,
39 sk_sp<Texture> depthStencilTexture) {
40 if (!this->onBeginRenderPass(renderPassDesc, colorTexture.get(), resolveTexture.get(),
41 depthStencilTexture.get())) {
42 return false;
43 }
44
45 if (colorTexture) {
46 this->trackResource(std::move(colorTexture));
47 }
48 if (resolveTexture) {
49 this->trackResource(std::move(resolveTexture));
50 }
51 if (depthStencilTexture) {
52 this->trackResource(std::move(depthStencilTexture));
53 }
54 #ifdef SK_DEBUG
55 if (renderPassDesc.fColorAttachment.fLoadOp == LoadOp::kClear &&
56 (renderPassDesc.fColorAttachment.fStoreOp == StoreOp::kStore ||
57 renderPassDesc.fColorResolveAttachment.fStoreOp == StoreOp::kStore)) {
58 fHasWork = true;
59 }
60 #endif
61
62 return true;
63 }
64
bindGraphicsPipeline(sk_sp<GraphicsPipeline> graphicsPipeline)65 void CommandBuffer::bindGraphicsPipeline(sk_sp<GraphicsPipeline> graphicsPipeline) {
66 this->onBindGraphicsPipeline(graphicsPipeline.get());
67 this->trackResource(std::move(graphicsPipeline));
68 }
69
bindUniformBuffer(UniformSlot slot,sk_sp<Buffer> uniformBuffer,size_t offset)70 void CommandBuffer::bindUniformBuffer(UniformSlot slot,
71 sk_sp<Buffer> uniformBuffer,
72 size_t offset) {
73 this->onBindUniformBuffer(slot, uniformBuffer.get(), offset);
74 this->trackResource(std::move(uniformBuffer));
75 }
76
bindVertexBuffers(sk_sp<Buffer> vertexBuffer,size_t vertexOffset,sk_sp<Buffer> instanceBuffer,size_t instanceOffset)77 void CommandBuffer::bindVertexBuffers(sk_sp<Buffer> vertexBuffer, size_t vertexOffset,
78 sk_sp<Buffer> instanceBuffer, size_t instanceOffset) {
79 this->onBindVertexBuffers(vertexBuffer.get(), vertexOffset,
80 instanceBuffer.get(), instanceOffset);
81 if (vertexBuffer) {
82 this->trackResource(std::move(vertexBuffer));
83 }
84 if (instanceBuffer) {
85 this->trackResource(std::move(instanceBuffer));
86 }
87 }
88
bindIndexBuffer(sk_sp<Buffer> indexBuffer,size_t bufferOffset)89 void CommandBuffer::bindIndexBuffer(sk_sp<Buffer> indexBuffer, size_t bufferOffset) {
90 this->onBindIndexBuffer(indexBuffer.get(), bufferOffset);
91 if (indexBuffer) {
92 this->trackResource(std::move(indexBuffer));
93 }
94 }
95
bindDrawBuffers(BindBufferInfo vertices,BindBufferInfo instances,BindBufferInfo indices)96 void CommandBuffer::bindDrawBuffers(BindBufferInfo vertices,
97 BindBufferInfo instances,
98 BindBufferInfo indices) {
99 this->bindVertexBuffers(sk_ref_sp(vertices.fBuffer), vertices.fOffset,
100 sk_ref_sp(instances.fBuffer), instances.fOffset);
101 this->bindIndexBuffer(sk_ref_sp(indices.fBuffer), indices.fOffset);
102 }
103
bindTextures(const TextureBindEntry * entries,int count)104 void CommandBuffer::bindTextures(const TextureBindEntry* entries, int count) {
105 this->onBindTextures(entries, count);
106 for (int i = 0; i < count; ++i) {
107 SkASSERT(entries[i].fTexture);
108 this->trackResource(entries[i].fTexture);
109 }
110 }
111
bindSamplers(const SamplerBindEntry * entries,int count)112 void CommandBuffer::bindSamplers(const SamplerBindEntry* entries, int count) {
113 this->onBindSamplers(entries, count);
114 for (int i = 0; i < count; ++i) {
115 SkASSERT(entries[i].fSampler);
116 this->trackResource(entries[i].fSampler);
117 }
118 }
119
copyTextureToBuffer(sk_sp<skgpu::Texture> texture,SkIRect srcRect,sk_sp<skgpu::Buffer> buffer,size_t bufferOffset,size_t bufferRowBytes)120 bool CommandBuffer::copyTextureToBuffer(sk_sp<skgpu::Texture> texture,
121 SkIRect srcRect,
122 sk_sp<skgpu::Buffer> buffer,
123 size_t bufferOffset,
124 size_t bufferRowBytes) {
125 SkASSERT(texture);
126 SkASSERT(buffer);
127
128 if (!this->onCopyTextureToBuffer(texture.get(), srcRect, buffer.get(), bufferOffset,
129 bufferRowBytes)) {
130 return false;
131 }
132
133 this->trackResource(std::move(texture));
134 this->trackResource(std::move(buffer));
135
136 SkDEBUGCODE(fHasWork = true;)
137
138 return true;
139 }
140
copyBufferToTexture(sk_sp<skgpu::Buffer> buffer,sk_sp<skgpu::Texture> texture,const BufferTextureCopyData * copyData,int count)141 bool CommandBuffer::copyBufferToTexture(sk_sp<skgpu::Buffer> buffer,
142 sk_sp<skgpu::Texture> texture,
143 const BufferTextureCopyData* copyData,
144 int count) {
145 SkASSERT(buffer);
146 SkASSERT(texture);
147 SkASSERT(count > 0 && copyData);
148
149 if (!this->onCopyBufferToTexture(buffer.get(), texture.get(), copyData, count)) {
150 return false;
151 }
152
153 this->trackResource(std::move(buffer));
154 this->trackResource(std::move(texture));
155
156 SkDEBUGCODE(fHasWork = true;)
157
158 return true;
159 }
160
161 } // namespace skgpu
162