blob: cc6758e24ef3e533f2c26135f746d4903649c0ed [file] [log] [blame]
egdaniel066df7c2016-06-08 14:02:27 -07001/*
2* Copyright 2016 Google Inc.
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 "GrVkGpuCommandBuffer.h"
9
Greg Daniel64cc9aa2018-10-19 13:54:56 -040010#include "GrBackendDrawableInfo.h"
Robert Phillipsbe9aff22019-02-15 11:33:22 -050011#include "GrContextPriv.h"
csmartdalton29df7602016-08-31 11:55:52 -070012#include "GrFixedClip.h"
egdaniel9cb63402016-06-23 08:37:05 -070013#include "GrMesh.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050014#include "GrOpFlushState.h"
egdaniel9cb63402016-06-23 08:37:05 -070015#include "GrPipeline.h"
16#include "GrRenderTargetPriv.h"
egdaniel9cb63402016-06-23 08:37:05 -070017#include "GrTexturePriv.h"
egdaniel066df7c2016-06-08 14:02:27 -070018#include "GrVkCommandBuffer.h"
Ethan Nicholas8e265a72018-12-12 16:22:40 -050019#include "GrVkCommandPool.h"
egdaniel066df7c2016-06-08 14:02:27 -070020#include "GrVkGpu.h"
egdaniel9cb63402016-06-23 08:37:05 -070021#include "GrVkPipeline.h"
egdaniel066df7c2016-06-08 14:02:27 -070022#include "GrVkRenderPass.h"
23#include "GrVkRenderTarget.h"
24#include "GrVkResourceProvider.h"
Greg Daniel64cc9aa2018-10-19 13:54:56 -040025#include "GrVkSemaphore.h"
egdaniel9cb63402016-06-23 08:37:05 -070026#include "GrVkTexture.h"
Greg Daniel64cc9aa2018-10-19 13:54:56 -040027#include "SkDrawable.h"
Greg Daniel36a77ee2016-10-18 10:33:25 -040028#include "SkRect.h"
egdaniel066df7c2016-06-08 14:02:27 -070029
Robert Phillipsb0e93a22017-08-29 08:26:54 -040030void GrVkGpuTextureCommandBuffer::copy(GrSurface* src, GrSurfaceOrigin srcOrigin,
31 const SkIRect& srcRect, const SkIPoint& dstPoint) {
32 fCopies.emplace_back(src, srcOrigin, srcRect, dstPoint);
Greg Daniel500d58b2017-08-24 15:59:33 -040033}
34
35void GrVkGpuTextureCommandBuffer::insertEventMarker(const char* msg) {
36 // TODO: does Vulkan have a correlate?
37}
38
39void GrVkGpuTextureCommandBuffer::submit() {
40 for (int i = 0; i < fCopies.count(); ++i) {
41 CopyInfo& copyInfo = fCopies[i];
Robert Phillipsb0e93a22017-08-29 08:26:54 -040042 fGpu->copySurface(fTexture, fOrigin, copyInfo.fSrc, copyInfo.fSrcOrigin, copyInfo.fSrcRect,
43 copyInfo.fDstPoint);
Greg Daniel500d58b2017-08-24 15:59:33 -040044 }
45}
46
47GrVkGpuTextureCommandBuffer::~GrVkGpuTextureCommandBuffer() {}
48
49////////////////////////////////////////////////////////////////////////////////
50
Robert Phillips6b47c7d2017-08-29 07:24:09 -040051void get_vk_load_store_ops(GrLoadOp loadOpIn, GrStoreOp storeOpIn,
egdaniel066df7c2016-06-08 14:02:27 -070052 VkAttachmentLoadOp* loadOp, VkAttachmentStoreOp* storeOp) {
Robert Phillips95214472017-08-08 18:00:03 -040053 switch (loadOpIn) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -040054 case GrLoadOp::kLoad:
egdaniel066df7c2016-06-08 14:02:27 -070055 *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
egdaniel066df7c2016-06-08 14:02:27 -070056 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -040057 case GrLoadOp::kClear:
egdaniel9cb63402016-06-23 08:37:05 -070058 *loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
59 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -040060 case GrLoadOp::kDiscard:
egdaniel9cb63402016-06-23 08:37:05 -070061 *loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
62 break;
63 default:
64 SK_ABORT("Invalid LoadOp");
egdaniel066df7c2016-06-08 14:02:27 -070065 *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
egdaniel9cb63402016-06-23 08:37:05 -070066 }
67
Robert Phillips95214472017-08-08 18:00:03 -040068 switch (storeOpIn) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -040069 case GrStoreOp::kStore:
egdaniel066df7c2016-06-08 14:02:27 -070070 *storeOp = VK_ATTACHMENT_STORE_OP_STORE;
71 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -040072 case GrStoreOp::kDiscard:
egdaniel066df7c2016-06-08 14:02:27 -070073 *storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
74 break;
brianosman0bbc3712016-06-14 04:53:09 -070075 default:
egdaniel9cb63402016-06-23 08:37:05 -070076 SK_ABORT("Invalid StoreOp");
brianosman0bbc3712016-06-14 04:53:09 -070077 *storeOp = VK_ATTACHMENT_STORE_OP_STORE;
egdaniel066df7c2016-06-08 14:02:27 -070078 }
79}
80
Robert Phillips5b5d84c2018-08-09 15:12:18 -040081GrVkGpuRTCommandBuffer::GrVkGpuRTCommandBuffer(GrVkGpu* gpu)
82 : fCurrentCmdInfo(-1)
Robert Phillips19e51dc2017-08-09 09:30:51 -040083 , fGpu(gpu)
Robert Phillips19e51dc2017-08-09 09:30:51 -040084 , fLastPipelineState(nullptr) {
Brian Salomonc293a292016-11-30 13:38:32 -050085}
86
Greg Daniel500d58b2017-08-24 15:59:33 -040087void GrVkGpuRTCommandBuffer::init() {
Brian Salomonc293a292016-11-30 13:38:32 -050088 GrVkRenderPass::LoadStoreOps vkColorOps(fVkColorLoadOp, fVkColorStoreOp);
89 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -070090
Greg Daniel36a77ee2016-10-18 10:33:25 -040091 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Brian Salomonc293a292016-11-30 13:38:32 -050092 SkASSERT(fCommandBufferInfos.count() == 1);
Greg Daniel22bc8652017-03-22 15:45:43 -040093 fCurrentCmdInfo = 0;
Greg Daniel36a77ee2016-10-18 10:33:25 -040094
Robert Phillips19e51dc2017-08-09 09:30:51 -040095 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
96 const GrVkResourceProvider::CompatibleRPHandle& rpHandle = vkRT->compatibleRenderPassHandle();
egdaniel066df7c2016-06-08 14:02:27 -070097 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -040098 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
99 vkColorOps,
100 vkStencilOps);
egdaniel066df7c2016-06-08 14:02:27 -0700101 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400102 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400103 vkColorOps,
104 vkStencilOps);
egdaniel066df7c2016-06-08 14:02:27 -0700105 }
106
Brian Osmancb3d0872018-10-16 15:19:28 -0400107 cbInfo.fColorClearValue.color.float32[0] = fClearColor[0];
108 cbInfo.fColorClearValue.color.float32[1] = fClearColor[1];
109 cbInfo.fColorClearValue.color.float32[2] = fClearColor[2];
110 cbInfo.fColorClearValue.color.float32[3] = fClearColor[3];
egdaniel9cb63402016-06-23 08:37:05 -0700111
Robert Phillips380b90c2017-08-30 07:41:07 -0400112 if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000113 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Robert Phillips380b90c2017-08-30 07:41:07 -0400114 } else {
115 cbInfo.fBounds.setEmpty();
116 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400117
118 if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) {
119 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
120 } else if (VK_ATTACHMENT_LOAD_OP_LOAD == fVkColorLoadOp &&
121 VK_ATTACHMENT_STORE_OP_STORE == fVkColorStoreOp) {
122 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
123 } else if (VK_ATTACHMENT_LOAD_OP_DONT_CARE == fVkColorLoadOp) {
124 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithDiscard;
125 }
Greg Daniel36a77ee2016-10-18 10:33:25 -0400126
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500127 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Robert Phillips19e51dc2017-08-09 09:30:51 -0400128 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
egdaniel066df7c2016-06-08 14:02:27 -0700129}
130
Greg Daniel070cbaf2019-01-03 17:35:54 -0500131void GrVkGpuRTCommandBuffer::initWrapped() {
132 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
133 SkASSERT(fCommandBufferInfos.count() == 1);
134 fCurrentCmdInfo = 0;
135
136 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
137 SkASSERT(vkRT->wrapsSecondaryCommandBuffer());
138 cbInfo.fRenderPass = vkRT->externalRenderPass();
139 cbInfo.fRenderPass->ref();
140
141 cbInfo.fBounds.setEmpty();
142 cbInfo.fCommandBuffers.push_back(vkRT->getExternalSecondaryCommandBuffer());
143 cbInfo.fCommandBuffers[0]->ref();
144 cbInfo.currentCmdBuf()->begin(fGpu, nullptr, cbInfo.fRenderPass);
145}
Brian Salomonc293a292016-11-30 13:38:32 -0500146
Greg Daniel500d58b2017-08-24 15:59:33 -0400147GrVkGpuRTCommandBuffer::~GrVkGpuRTCommandBuffer() {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400148 this->reset();
egdaniel066df7c2016-06-08 14:02:27 -0700149}
150
Greg Daniel500d58b2017-08-24 15:59:33 -0400151GrGpu* GrVkGpuRTCommandBuffer::gpu() { return fGpu; }
egdaniel9cb63402016-06-23 08:37:05 -0700152
Greg Daniel500d58b2017-08-24 15:59:33 -0400153void GrVkGpuRTCommandBuffer::end() {
Greg Daniel22bc8652017-03-22 15:45:43 -0400154 if (fCurrentCmdInfo >= 0) {
155 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Brian Salomonc293a292016-11-30 13:38:32 -0500156 }
egdaniel066df7c2016-06-08 14:02:27 -0700157}
158
Greg Daniel500d58b2017-08-24 15:59:33 -0400159void GrVkGpuRTCommandBuffer::submit() {
Brian Salomonc293a292016-11-30 13:38:32 -0500160 if (!fRenderTarget) {
161 return;
162 }
Robert Phillips19e51dc2017-08-09 09:30:51 -0400163
164 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Robert Phillips19e51dc2017-08-09 09:30:51 -0400165 GrVkImage* targetImage = vkRT->msaaImage() ? vkRT->msaaImage() : vkRT;
Greg Daniel45a44de2018-02-27 10:07:29 -0500166 GrStencilAttachment* stencil = fRenderTarget->renderTargetPriv().getStencilAttachment();
egdaniel9cb63402016-06-23 08:37:05 -0700167
Greg Daniel36a77ee2016-10-18 10:33:25 -0400168 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
169 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
170
Greg Daniel77b53f62016-10-18 11:48:51 -0400171 for (int j = 0; j < cbInfo.fPreDrawUploads.count(); ++j) {
172 InlineUploadInfo& iuInfo = cbInfo.fPreDrawUploads[j];
173 iuInfo.fFlushState->doUpload(iuInfo.fUpload);
174 }
175
Greg Daniel500d58b2017-08-24 15:59:33 -0400176 for (int j = 0; j < cbInfo.fPreCopies.count(); ++j) {
177 CopyInfo& copyInfo = cbInfo.fPreCopies[j];
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400178 fGpu->copySurface(fRenderTarget, fOrigin, copyInfo.fSrc, copyInfo.fSrcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -0400179 copyInfo.fSrcRect, copyInfo.fDstPoint, copyInfo.fShouldDiscardDst);
Greg Daniel500d58b2017-08-24 15:59:33 -0400180 }
181
Greg Daniel45a44de2018-02-27 10:07:29 -0500182
Greg Daniel38c3d932018-03-16 14:22:30 -0400183 // TODO: Many things create a scratch texture which adds the discard immediately, but then
184 // don't draw to it right away. This causes the discard to be ignored and we get yelled at
185 // for loading uninitialized data. However, once MDB lands with reordering, the discard will
186 // get reordered with the rest of the draw commands and we can remove the discard check.
187 if (cbInfo.fIsEmpty &&
188 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithClear &&
189 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithDiscard) {
Greg Daniel77b53f62016-10-18 11:48:51 -0400190 // We have sumbitted no actual draw commands to the command buffer and we are not using
191 // the render pass to do a clear so there is no need to submit anything.
192 continue;
193 }
Greg Daniel38c3d932018-03-16 14:22:30 -0400194
Greg Daniel070cbaf2019-01-03 17:35:54 -0500195 // We don't want to actually submit the secondary command buffer if it is wrapped.
196 if (this->wrapsSecondaryCommandBuffer()) {
197 // If we have any sampled images set their layout now.
198 for (int j = 0; j < cbInfo.fSampledImages.count(); ++j) {
199 cbInfo.fSampledImages[j]->setImageLayout(fGpu,
200 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
201 VK_ACCESS_SHADER_READ_BIT,
202 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
203 false);
204 }
205
206 // There should have only been one secondary command buffer in the wrapped case so it is
207 // safe to just return here.
208 SkASSERT(fCommandBufferInfos.count() == 1);
209 return;
210 }
211
Greg Danieldbdba602018-04-20 11:52:43 -0400212 // Make sure if we only have a discard load that we execute the discard on the whole image.
213 // TODO: Once we improve our tracking of discards so that we never end up flushing a discard
214 // call with no actually ops, remove this.
215 if (cbInfo.fIsEmpty && cbInfo.fLoadStoreState == LoadStoreState::kStartsWithDiscard) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000216 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Greg Danieldbdba602018-04-20 11:52:43 -0400217 }
218
Greg Daniela41a74a2018-10-09 12:59:23 +0000219 if (cbInfo.fBounds.intersect(0, 0,
220 SkIntToScalar(fRenderTarget->width()),
221 SkIntToScalar(fRenderTarget->height()))) {
Greg Daniel38c3d932018-03-16 14:22:30 -0400222 // Make sure we do the following layout changes after all copies, uploads, or any other
223 // pre-work is done since we may change the layouts in the pre-work. Also since the
224 // draws will be submitted in different render passes, we need to guard againts write
225 // and write issues.
226
227 // Change layout of our render target so it can be used as the color attachment.
Greg Danielf7828d02018-10-09 12:01:32 -0400228 // TODO: If we know that we will never be blending or loading the attachment we could
229 // drop the VK_ACCESS_COLOR_ATTACHMENT_READ_BIT.
Greg Daniel38c3d932018-03-16 14:22:30 -0400230 targetImage->setImageLayout(fGpu,
231 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Greg Danielf7828d02018-10-09 12:01:32 -0400232 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
Greg Daniel38c3d932018-03-16 14:22:30 -0400233 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400234 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400235 false);
236
237 // If we are using a stencil attachment we also need to update its layout
238 if (stencil) {
239 GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
Greg Danielf7828d02018-10-09 12:01:32 -0400240 // We need the write and read access bits since we may load and store the stencil.
241 // The initial load happens in the VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT so we
242 // wait there.
Greg Daniel38c3d932018-03-16 14:22:30 -0400243 vkStencil->setImageLayout(fGpu,
244 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
245 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
246 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400247 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400248 false);
249 }
250
251 // If we have any sampled images set their layout now.
252 for (int j = 0; j < cbInfo.fSampledImages.count(); ++j) {
253 cbInfo.fSampledImages[j]->setImageLayout(fGpu,
254 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
255 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400256 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400257 false);
258 }
259
Greg Daniel36a77ee2016-10-18 10:33:25 -0400260 SkIRect iBounds;
261 cbInfo.fBounds.roundOut(&iBounds);
262
Greg Daniel22bc8652017-03-22 15:45:43 -0400263 fGpu->submitSecondaryCommandBuffer(cbInfo.fCommandBuffers, cbInfo.fRenderPass,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400264 &cbInfo.fColorClearValue, vkRT, fOrigin, iBounds);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400265 }
266 }
egdaniel9cb63402016-06-23 08:37:05 -0700267}
268
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400269void GrVkGpuRTCommandBuffer::set(GrRenderTarget* rt, GrSurfaceOrigin origin,
270 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
271 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
272 SkASSERT(!fRenderTarget);
273 SkASSERT(fCommandBufferInfos.empty());
274 SkASSERT(-1 == fCurrentCmdInfo);
Robert Phillips9da87e02019-02-04 13:26:26 -0500275 SkASSERT(fGpu == rt->getContext()->priv().getGpu());
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400276 SkASSERT(!fLastPipelineState);
277
278 this->INHERITED::set(rt, origin);
279
Greg Daniel070cbaf2019-01-03 17:35:54 -0500280 if (this->wrapsSecondaryCommandBuffer()) {
281 this->initWrapped();
282 return;
283 }
284
Brian Osman9a9baae2018-11-05 15:06:26 -0500285 fClearColor = colorInfo.fClearColor;
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400286
287 get_vk_load_store_ops(colorInfo.fLoadOp, colorInfo.fStoreOp,
288 &fVkColorLoadOp, &fVkColorStoreOp);
289
290 get_vk_load_store_ops(stencilInfo.fLoadOp, stencilInfo.fStoreOp,
291 &fVkStencilLoadOp, &fVkStencilStoreOp);
292
293 this->init();
294}
295
296void GrVkGpuRTCommandBuffer::reset() {
297 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
298 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
299 for (int j = 0; j < cbInfo.fCommandBuffers.count(); ++j) {
300 cbInfo.fCommandBuffers[j]->unref(fGpu);
301 }
302 cbInfo.fRenderPass->unref(fGpu);
303 }
304 fCommandBufferInfos.reset();
305
306 fCurrentCmdInfo = -1;
307
308 fLastPipelineState = nullptr;
309 fRenderTarget = nullptr;
310}
311
Greg Daniel070cbaf2019-01-03 17:35:54 -0500312bool GrVkGpuRTCommandBuffer::wrapsSecondaryCommandBuffer() const {
313 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
314 return vkRT->wrapsSecondaryCommandBuffer();
315}
316
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400317////////////////////////////////////////////////////////////////////////////////
318
Greg Daniel500d58b2017-08-24 15:59:33 -0400319void GrVkGpuRTCommandBuffer::discard() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400320 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Brian Salomonc293a292016-11-30 13:38:32 -0500321
Greg Daniel22bc8652017-03-22 15:45:43 -0400322 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Greg Daniel77b53f62016-10-18 11:48:51 -0400323 if (cbInfo.fIsEmpty) {
Robert Phillips74c627f2017-08-09 10:28:00 -0400324 // Change the render pass to do a don't-care load for both color & stencil
egdaniel37535c92016-06-30 08:23:30 -0700325 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_DONT_CARE,
326 VK_ATTACHMENT_STORE_OP_STORE);
327 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_DONT_CARE,
328 VK_ATTACHMENT_STORE_OP_STORE);
egdaniel37535c92016-06-30 08:23:30 -0700329
Greg Daniel36a77ee2016-10-18 10:33:25 -0400330 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
egdaniel37535c92016-06-30 08:23:30 -0700331
egdaniel37535c92016-06-30 08:23:30 -0700332 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400333 vkRT->compatibleRenderPassHandle();
egdaniel37535c92016-06-30 08:23:30 -0700334 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400335 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
336 vkColorOps,
337 vkStencilOps);
egdaniel37535c92016-06-30 08:23:30 -0700338 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400339 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400340 vkColorOps,
341 vkStencilOps);
egdaniel37535c92016-06-30 08:23:30 -0700342 }
343
Greg Daniel36a77ee2016-10-18 10:33:25 -0400344 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
egdaniel37535c92016-06-30 08:23:30 -0700345 oldRP->unref(fGpu);
Greg Daniel5011f852016-10-28 15:07:16 -0400346 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
Greg Daniela3c68df2018-03-16 13:46:53 -0400347 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithDiscard;
348 // If we are going to discard the whole render target then the results of any copies we did
349 // immediately before to the target won't matter, so just drop them.
350 cbInfo.fPreCopies.reset();
egdaniel37535c92016-06-30 08:23:30 -0700351 }
352}
353
Greg Daniel500d58b2017-08-24 15:59:33 -0400354void GrVkGpuRTCommandBuffer::insertEventMarker(const char* msg) {
Robert Phillips65a88fa2017-08-08 08:36:22 -0400355 // TODO: does Vulkan have a correlate?
356}
357
Greg Daniel500d58b2017-08-24 15:59:33 -0400358void GrVkGpuRTCommandBuffer::onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton94c04682017-11-01 17:15:06 -0600359 SkASSERT(!clip.hasWindowRectangles());
360
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000361 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
362
Greg Daniel65a09272016-10-12 09:47:22 -0400363 GrStencilAttachment* sb = fRenderTarget->renderTargetPriv().getStencilAttachment();
egdaniel9cb63402016-06-23 08:37:05 -0700364 // this should only be called internally when we know we have a
365 // stencil buffer.
366 SkASSERT(sb);
367 int stencilBitCount = sb->bits();
368
369 // The contract with the callers does not guarantee that we preserve all bits in the stencil
370 // during this clear. Thus we will clear the entire stencil to the desired value.
371
372 VkClearDepthStencilValue vkStencilColor;
373 memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue));
csmartdalton29df7602016-08-31 11:55:52 -0700374 if (insideStencilMask) {
egdaniel9cb63402016-06-23 08:37:05 -0700375 vkStencilColor.stencil = (1 << (stencilBitCount - 1));
376 } else {
377 vkStencilColor.stencil = 0;
378 }
379
380 VkClearRect clearRect;
381 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700382 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000383 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000384 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400385 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700386 vkRect = clip.scissorRect();
387 } else {
388 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400389 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
390 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700391 }
392
393 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
394 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
395
396 clearRect.baseArrayLayer = 0;
397 clearRect.layerCount = 1;
398
399 uint32_t stencilIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400400 SkAssertResult(cbInfo.fRenderPass->stencilAttachmentIndex(&stencilIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700401
402 VkClearAttachment attachment;
403 attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
404 attachment.colorAttachment = 0; // this value shouldn't matter
405 attachment.clearValue.depthStencil = vkStencilColor;
406
Greg Daniel22bc8652017-03-22 15:45:43 -0400407 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400408 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400409
410 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000411 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400412 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
413 } else {
414 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
415 }
egdaniel9cb63402016-06-23 08:37:05 -0700416}
417
Brian Osman9a9baae2018-11-05 15:06:26 -0500418void GrVkGpuRTCommandBuffer::onClear(const GrFixedClip& clip, const SkPMColor4f& color) {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400419 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
420
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000421 // parent class should never let us get here with no RT
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700422 SkASSERT(!clip.hasWindowRectangles());
egdaniel9cb63402016-06-23 08:37:05 -0700423
Greg Daniel22bc8652017-03-22 15:45:43 -0400424 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Greg Daniel36a77ee2016-10-18 10:33:25 -0400425
Brian Osman9a9baae2018-11-05 15:06:26 -0500426 VkClearColorValue vkColor = {{color.fR, color.fG, color.fB, color.fA}};
egdaniel9cb63402016-06-23 08:37:05 -0700427
Brian Salomond818ebf2018-07-02 14:08:49 +0000428 if (cbInfo.fIsEmpty && !clip.scissorEnabled()) {
Robert Phillips74c627f2017-08-09 10:28:00 -0400429 // Change the render pass to do a clear load
egdaniel9cb63402016-06-23 08:37:05 -0700430 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_CLEAR,
431 VK_ATTACHMENT_STORE_OP_STORE);
Robert Phillips74c627f2017-08-09 10:28:00 -0400432 // Preserve the stencil buffer's load & store settings
433 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -0700434
Greg Daniel36a77ee2016-10-18 10:33:25 -0400435 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
egdaniel9cb63402016-06-23 08:37:05 -0700436
437 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400438 vkRT->compatibleRenderPassHandle();
egdaniel9cb63402016-06-23 08:37:05 -0700439 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400440 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
441 vkColorOps,
442 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700443 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400444 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400445 vkColorOps,
446 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700447 }
448
Greg Daniel36a77ee2016-10-18 10:33:25 -0400449 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
egdaniel9cb63402016-06-23 08:37:05 -0700450 oldRP->unref(fGpu);
451
Brian Osman9a9baae2018-11-05 15:06:26 -0500452 cbInfo.fColorClearValue.color = {{color.fR, color.fG, color.fB, color.fA}};
Greg Daniela3c68df2018-03-16 13:46:53 -0400453 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
454 // If we are going to clear the whole render target then the results of any copies we did
455 // immediately before to the target won't matter, so just drop them.
456 cbInfo.fPreCopies.reset();
Greg Daniel36a77ee2016-10-18 10:33:25 -0400457
458 // Update command buffer bounds
459 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
egdaniel9cb63402016-06-23 08:37:05 -0700460 return;
461 }
462
463 // We always do a sub rect clear with clearAttachments since we are inside a render pass
464 VkClearRect clearRect;
465 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700466 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000467 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000468 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400469 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700470 vkRect = clip.scissorRect();
471 } else {
472 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400473 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
474 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700475 }
476 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
477 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
478 clearRect.baseArrayLayer = 0;
479 clearRect.layerCount = 1;
480
481 uint32_t colorIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400482 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&colorIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700483
484 VkClearAttachment attachment;
485 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
486 attachment.colorAttachment = colorIndex;
487 attachment.clearValue.color = vkColor;
488
Greg Daniel22bc8652017-03-22 15:45:43 -0400489 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400490 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400491
492 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000493 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400494 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
495 } else {
496 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
497 }
egdaniel9cb63402016-06-23 08:37:05 -0700498 return;
499}
500
Greg Daniel500d58b2017-08-24 15:59:33 -0400501////////////////////////////////////////////////////////////////////////////////
502
503void GrVkGpuRTCommandBuffer::addAdditionalCommandBuffer() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400504 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
505
Greg Daniel22bc8652017-03-22 15:45:43 -0400506 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
507 cbInfo.currentCmdBuf()->end(fGpu);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500508 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Robert Phillips19e51dc2017-08-09 09:30:51 -0400509 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel22bc8652017-03-22 15:45:43 -0400510}
511
Greg Daniel500d58b2017-08-24 15:59:33 -0400512void GrVkGpuRTCommandBuffer::addAdditionalRenderPass() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400513 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
514
Greg Daniel22bc8652017-03-22 15:45:43 -0400515 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Greg Daniel77b53f62016-10-18 11:48:51 -0400516
517 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Greg Daniel22bc8652017-03-22 15:45:43 -0400518 fCurrentCmdInfo++;
Greg Daniel77b53f62016-10-18 11:48:51 -0400519
520 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
521 VK_ATTACHMENT_STORE_OP_STORE);
522 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
523 VK_ATTACHMENT_STORE_OP_STORE);
524
525 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400526 vkRT->compatibleRenderPassHandle();
Greg Daniel77b53f62016-10-18 11:48:51 -0400527 if (rpHandle.isValid()) {
528 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
529 vkColorOps,
530 vkStencilOps);
531 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400532 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel77b53f62016-10-18 11:48:51 -0400533 vkColorOps,
534 vkStencilOps);
535 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400536 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
Greg Daniel77b53f62016-10-18 11:48:51 -0400537
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500538 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Greg Daniel77b53f62016-10-18 11:48:51 -0400539 // It shouldn't matter what we set the clear color to here since we will assume loading of the
540 // attachment.
541 memset(&cbInfo.fColorClearValue, 0, sizeof(VkClearValue));
542 cbInfo.fBounds.setEmpty();
Greg Daniel77b53f62016-10-18 11:48:51 -0400543
Robert Phillips19e51dc2017-08-09 09:30:51 -0400544 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel77b53f62016-10-18 11:48:51 -0400545}
546
Brian Salomon943ed792017-10-30 09:37:55 -0400547void GrVkGpuRTCommandBuffer::inlineUpload(GrOpFlushState* state,
548 GrDeferredTextureUploadFn& upload) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400549 if (!fCommandBufferInfos[fCurrentCmdInfo].fIsEmpty) {
550 this->addAdditionalRenderPass();
Greg Daniel77b53f62016-10-18 11:48:51 -0400551 }
Greg Daniel22bc8652017-03-22 15:45:43 -0400552 fCommandBufferInfos[fCurrentCmdInfo].fPreDrawUploads.emplace_back(state, upload);
Greg Daniel77b53f62016-10-18 11:48:51 -0400553}
554
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400555void GrVkGpuRTCommandBuffer::copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect,
Greg Daniel500d58b2017-08-24 15:59:33 -0400556 const SkIPoint& dstPoint) {
Greg Daniela3c68df2018-03-16 13:46:53 -0400557 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
558 if (!cbInfo.fIsEmpty || LoadStoreState::kStartsWithClear == cbInfo.fLoadStoreState) {
Greg Daniel500d58b2017-08-24 15:59:33 -0400559 this->addAdditionalRenderPass();
560 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400561
Greg Daniel55fa6472018-03-16 16:13:10 -0400562 fCommandBufferInfos[fCurrentCmdInfo].fPreCopies.emplace_back(
563 src, srcOrigin, srcRect, dstPoint,
564 LoadStoreState::kStartsWithDiscard == cbInfo.fLoadStoreState);
565
Greg Daniela3c68df2018-03-16 13:46:53 -0400566 if (LoadStoreState::kLoadAndStore != cbInfo.fLoadStoreState) {
567 // Change the render pass to do a load and store so we don't lose the results of our copy
568 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
569 VK_ATTACHMENT_STORE_OP_STORE);
570 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
571 VK_ATTACHMENT_STORE_OP_STORE);
572
573 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
574
575 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
576 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
577 vkRT->compatibleRenderPassHandle();
578 if (rpHandle.isValid()) {
579 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
580 vkColorOps,
581 vkStencilOps);
582 } else {
583 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
584 vkColorOps,
585 vkStencilOps);
586 }
587 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
588 oldRP->unref(fGpu);
589
590 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
591
592 }
Greg Daniel500d58b2017-08-24 15:59:33 -0400593}
594
egdaniel9cb63402016-06-23 08:37:05 -0700595////////////////////////////////////////////////////////////////////////////////
596
Brian Salomondbf70722019-02-07 11:31:24 -0500597void GrVkGpuRTCommandBuffer::bindGeometry(const GrGpuBuffer* indexBuffer,
598 const GrGpuBuffer* vertexBuffer,
599 const GrGpuBuffer* instanceBuffer) {
Chris Daltonff926502017-05-03 14:36:54 -0400600 GrVkSecondaryCommandBuffer* currCmdBuf = fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf();
egdaniel9cb63402016-06-23 08:37:05 -0700601 // There is no need to put any memory barriers to make sure host writes have finished here.
602 // When a command buffer is submitted to a queue, there is an implicit memory barrier that
603 // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of
604 // an active RenderPass.
egdaniel9cb63402016-06-23 08:37:05 -0700605
Chris Dalton1d616352017-05-31 12:51:23 -0600606 // Here our vertex and instance inputs need to match the same 0-based bindings they were
607 // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance.
608 uint32_t binding = 0;
609
Brian Salomon802cb312018-06-08 18:05:20 -0400610 if (vertexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600611 SkASSERT(vertexBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600612 SkASSERT(!vertexBuffer->isMapped());
613
614 currCmdBuf->bindInputBuffer(fGpu, binding++,
615 static_cast<const GrVkVertexBuffer*>(vertexBuffer));
616 }
617
Brian Salomon802cb312018-06-08 18:05:20 -0400618 if (instanceBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600619 SkASSERT(instanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600620 SkASSERT(!instanceBuffer->isMapped());
621
622 currCmdBuf->bindInputBuffer(fGpu, binding++,
623 static_cast<const GrVkVertexBuffer*>(instanceBuffer));
624 }
Chris Daltonff926502017-05-03 14:36:54 -0400625 if (indexBuffer) {
626 SkASSERT(indexBuffer);
627 SkASSERT(!indexBuffer->isMapped());
egdaniel9cb63402016-06-23 08:37:05 -0700628
Chris Daltonff926502017-05-03 14:36:54 -0400629 currCmdBuf->bindIndexBuffer(fGpu, static_cast<const GrVkIndexBuffer*>(indexBuffer));
egdaniel9cb63402016-06-23 08:37:05 -0700630 }
631}
632
Brian Salomon49348902018-06-26 09:12:38 -0400633GrVkPipelineState* GrVkGpuRTCommandBuffer::prepareDrawState(
634 const GrPrimitiveProcessor& primProc,
635 const GrPipeline& pipeline,
636 const GrPipeline::FixedDynamicState* fixedDynamicState,
637 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
638 GrPrimitiveType primitiveType) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400639 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
640 SkASSERT(cbInfo.fRenderPass);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400641
Greg Daniel99b88e02018-10-03 15:31:20 -0400642 VkRenderPass compatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
643
Greg Daniel9a51a862018-11-30 10:18:14 -0500644 const GrTextureProxy* const* primProcProxies = nullptr;
645 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
646 primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures;
647 } else if (fixedDynamicState) {
648 primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures;
649 }
650
651 SkASSERT(SkToBool(primProcProxies) == SkToBool(primProc.numTextureSamplers()));
652
Greg Daniel09eeefb2017-10-16 15:15:02 -0400653 GrVkPipelineState* pipelineState =
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500654 fGpu->resourceProvider().findOrCreateCompatiblePipelineState(fRenderTarget, fOrigin,
655 pipeline,
egdaniel9cb63402016-06-23 08:37:05 -0700656 primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -0500657 primProcProxies,
egdaniel9cb63402016-06-23 08:37:05 -0700658 primitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -0400659 compatibleRenderPass);
egdaniel9cb63402016-06-23 08:37:05 -0700660 if (!pipelineState) {
661 return pipelineState;
662 }
663
Greg Daniel22bc8652017-03-22 15:45:43 -0400664 if (!cbInfo.fIsEmpty &&
Greg Daniel09eeefb2017-10-16 15:15:02 -0400665 fLastPipelineState && fLastPipelineState != pipelineState &&
Greg Daniele3cd6912017-05-17 11:15:55 -0400666 fGpu->vkCaps().newCBOnPipelineChange()) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400667 this->addAdditionalCommandBuffer();
668 }
Greg Daniel09eeefb2017-10-16 15:15:02 -0400669 fLastPipelineState = pipelineState;
Greg Daniel22bc8652017-03-22 15:45:43 -0400670
Brian Salomonf7232642018-09-19 08:58:08 -0400671 pipelineState->bindPipeline(fGpu, cbInfo.currentCmdBuf());
Brian Salomoncd7907b2018-08-30 08:36:18 -0400672
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500673 pipelineState->setAndBindUniforms(fGpu, fRenderTarget, fOrigin,
674 primProc, pipeline, cbInfo.currentCmdBuf());
Brian Salomonf7232642018-09-19 08:58:08 -0400675
676 // Check whether we need to bind textures between each GrMesh. If not we can bind them all now.
677 bool setTextures = !(dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures);
678 if (setTextures) {
Brian Salomonf7232642018-09-19 08:58:08 -0400679 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, primProcProxies,
680 cbInfo.currentCmdBuf());
681 }
egdaniel9cb63402016-06-23 08:37:05 -0700682
Brian Salomond818ebf2018-07-02 14:08:49 +0000683 if (!pipeline.isScissorEnabled()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400684 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(),
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500685 fRenderTarget, fOrigin,
686 SkIRect::MakeWH(fRenderTarget->width(),
687 fRenderTarget->height()));
Brian Salomon49348902018-06-26 09:12:38 -0400688 } else if (!dynamicStateArrays || !dynamicStateArrays->fScissorRects) {
689 SkASSERT(fixedDynamicState);
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500690 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
691 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400692 fixedDynamicState->fScissorRect);
Chris Dalton46983b72017-06-06 12:27:16 -0600693 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500694 GrVkPipeline::SetDynamicViewportState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget);
695 GrVkPipeline::SetDynamicBlendConstantState(fGpu, cbInfo.currentCmdBuf(),
696 fRenderTarget->config(),
Chris Dalton46983b72017-06-06 12:27:16 -0600697 pipeline.getXferProcessor());
egdaniel9cb63402016-06-23 08:37:05 -0700698
699 return pipelineState;
700}
701
Brian Salomonff168d92018-06-23 15:17:27 -0400702void GrVkGpuRTCommandBuffer::onDraw(const GrPrimitiveProcessor& primProc,
703 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400704 const GrPipeline::FixedDynamicState* fixedDynamicState,
705 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
Greg Daniel500d58b2017-08-24 15:59:33 -0400706 const GrMesh meshes[],
Greg Daniel500d58b2017-08-24 15:59:33 -0400707 int meshCount,
708 const SkRect& bounds) {
egdaniel9cb63402016-06-23 08:37:05 -0700709 if (!meshCount) {
710 return;
711 }
Greg Danielea022cd2018-03-16 11:10:03 -0400712
713 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
714
Brian Salomone782f842018-07-31 13:53:11 -0400715 auto prepareSampledImage = [&](GrTexture* texture, GrSamplerState::Filter filter) {
716 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
717 // We may need to resolve the texture first if it is also a render target
718 GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(vkTexture->asRenderTarget());
719 if (texRT) {
Greg Daniel0a77f432018-12-06 11:23:32 -0500720 fGpu->resolveRenderTargetNoFlush(texRT);
Brian Salomone782f842018-07-31 13:53:11 -0400721 }
722
723 // Check if we need to regenerate any mip maps
724 if (GrSamplerState::Filter::kMipMap == filter &&
725 (vkTexture->width() != 1 || vkTexture->height() != 1)) {
726 SkASSERT(vkTexture->texturePriv().mipMapped() == GrMipMapped::kYes);
727 if (vkTexture->texturePriv().mipMapsAreDirty()) {
728 fGpu->regenerateMipMapLevels(vkTexture);
729 }
730 }
731 cbInfo.fSampledImages.push_back(vkTexture);
732 };
733
Brian Salomonf7232642018-09-19 08:58:08 -0400734 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
735 for (int m = 0, i = 0; m < meshCount; ++m) {
736 for (int s = 0; s < primProc.numTextureSamplers(); ++s, ++i) {
737 auto texture = dynamicStateArrays->fPrimitiveProcessorTextures[i]->peekTexture();
738 prepareSampledImage(texture, primProc.textureSampler(s).samplerState().filter());
739 }
740 }
741 } else {
742 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
743 auto texture = fixedDynamicState->fPrimitiveProcessorTextures[i]->peekTexture();
744 prepareSampledImage(texture, primProc.textureSampler(i).samplerState().filter());
745 }
Brian Salomone782f842018-07-31 13:53:11 -0400746 }
bsalomonb58a2b42016-09-26 06:55:02 -0700747 GrFragmentProcessor::Iter iter(pipeline);
748 while (const GrFragmentProcessor* fp = iter.next()) {
Brian Salomone782f842018-07-31 13:53:11 -0400749 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
750 const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i);
751 prepareSampledImage(sampler.peekTexture(), sampler.samplerState().filter());
752 }
egdaniel2f5792a2016-07-06 08:51:23 -0700753 }
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400754 if (GrTexture* dstTexture = pipeline.peekDstTexture()) {
Greg Danielea022cd2018-03-16 11:10:03 -0400755 cbInfo.fSampledImages.push_back(static_cast<GrVkTexture*>(dstTexture));
Brian Salomon18dfa982017-04-03 16:57:43 -0400756 }
egdaniel2f5792a2016-07-06 08:51:23 -0700757
Chris Daltonbca46e22017-05-15 11:03:26 -0600758 GrPrimitiveType primitiveType = meshes[0].primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400759 GrVkPipelineState* pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
760 dynamicStateArrays, primitiveType);
egdaniel9cb63402016-06-23 08:37:05 -0700761 if (!pipelineState) {
762 return;
763 }
764
Brian Salomond818ebf2018-07-02 14:08:49 +0000765 bool dynamicScissor =
766 pipeline.isScissorEnabled() && dynamicStateArrays && dynamicStateArrays->fScissorRects;
Brian Salomonf7232642018-09-19 08:58:08 -0400767 bool dynamicTextures = dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures;
Brian Salomon49348902018-06-26 09:12:38 -0400768
egdaniel9cb63402016-06-23 08:37:05 -0700769 for (int i = 0; i < meshCount; ++i) {
770 const GrMesh& mesh = meshes[i];
Chris Daltonbca46e22017-05-15 11:03:26 -0600771 if (mesh.primitiveType() != primitiveType) {
Chris Dalton6f241802017-05-08 13:58:38 -0400772 SkDEBUGCODE(pipelineState = nullptr);
Chris Daltonbca46e22017-05-15 11:03:26 -0600773 primitiveType = mesh.primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400774 pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
775 dynamicStateArrays, primitiveType);
Chris Dalton6f241802017-05-08 13:58:38 -0400776 if (!pipelineState) {
777 return;
egdaniel9cb63402016-06-23 08:37:05 -0700778 }
Chris Dalton6f241802017-05-08 13:58:38 -0400779 }
egdaniel9cb63402016-06-23 08:37:05 -0700780
Brian Salomon49348902018-06-26 09:12:38 -0400781 if (dynamicScissor) {
782 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500783 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400784 dynamicStateArrays->fScissorRects[i]);
Chris Dalton46983b72017-06-06 12:27:16 -0600785 }
Brian Salomonf7232642018-09-19 08:58:08 -0400786 if (dynamicTextures) {
787 GrTextureProxy* const* meshProxies = dynamicStateArrays->fPrimitiveProcessorTextures +
788 primProc.numTextureSamplers() * i;
789 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, meshProxies,
790 cbInfo.currentCmdBuf());
791 }
Chris Daltonbca46e22017-05-15 11:03:26 -0600792 SkASSERT(pipelineState);
Brian Salomon802cb312018-06-08 18:05:20 -0400793 mesh.sendToGpu(this);
egdaniel9cb63402016-06-23 08:37:05 -0700794 }
795
Greg Daniel36a77ee2016-10-18 10:33:25 -0400796 cbInfo.fBounds.join(bounds);
Chris Dalton114a3c02017-05-26 15:17:19 -0600797 cbInfo.fIsEmpty = false;
egdaniel066df7c2016-06-08 14:02:27 -0700798}
799
Brian Salomon802cb312018-06-08 18:05:20 -0400800void GrVkGpuRTCommandBuffer::sendInstancedMeshToGpu(GrPrimitiveType,
Greg Daniel500d58b2017-08-24 15:59:33 -0400801 const GrBuffer* vertexBuffer,
802 int vertexCount,
803 int baseVertex,
804 const GrBuffer* instanceBuffer,
805 int instanceCount,
806 int baseInstance) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600807 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500808 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
809 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
810 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
811 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
812 this->bindGeometry(nullptr, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600813 cbInfo.currentCmdBuf()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600814 fGpu->stats()->incNumDraws();
815}
816
Brian Salomon802cb312018-06-08 18:05:20 -0400817void GrVkGpuRTCommandBuffer::sendIndexedInstancedMeshToGpu(GrPrimitiveType,
Greg Daniel500d58b2017-08-24 15:59:33 -0400818 const GrBuffer* indexBuffer,
819 int indexCount,
820 int baseIndex,
821 const GrBuffer* vertexBuffer,
822 int baseVertex,
823 const GrBuffer* instanceBuffer,
824 int instanceCount,
Brian Salomon802cb312018-06-08 18:05:20 -0400825 int baseInstance,
826 GrPrimitiveRestart restart) {
827 SkASSERT(restart == GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -0600828 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500829 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
830 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
831 SkASSERT(!indexBuffer->isCpuBuffer());
832 auto gpuIndexxBuffer = static_cast<const GrGpuBuffer*>(indexBuffer);
833 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
834 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
835 this->bindGeometry(gpuIndexxBuffer, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600836 cbInfo.currentCmdBuf()->drawIndexed(fGpu, indexCount, instanceCount,
837 baseIndex, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600838 fGpu->stats()->incNumDraws();
839}
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400840
841////////////////////////////////////////////////////////////////////////////////
842
843void GrVkGpuRTCommandBuffer::executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
844 GrVkRenderTarget* target = static_cast<GrVkRenderTarget*>(fRenderTarget);
845
846 GrVkImage* targetImage = target->msaaImage() ? target->msaaImage() : target;
847
848 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
849 VkRect2D bounds;
850 bounds.offset = { 0, 0 };
851 bounds.extent = { 0, 0 };
852
853 GrVkDrawableInfo vkInfo;
854 vkInfo.fSecondaryCommandBuffer = cbInfo.currentCmdBuf()->vkCommandBuffer();
855 vkInfo.fCompatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
Greg Danielb353eeb2018-12-05 11:01:58 -0500856 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&vkInfo.fColorAttachmentIndex));
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400857 vkInfo.fFormat = targetImage->imageFormat();
858 vkInfo.fDrawBounds = &bounds;
859
860 GrBackendDrawableInfo info(vkInfo);
861
Eric Karlc0b2ba22019-01-22 19:40:35 -0800862 // After we draw into the command buffer via the drawable, cached state we have may be invalid.
863 cbInfo.currentCmdBuf()->invalidateState();
Eric Karla8878a12019-02-07 18:17:43 -0800864 // Also assume that the drawable produced output.
865 cbInfo.fIsEmpty = false;
Eric Karlc0b2ba22019-01-22 19:40:35 -0800866
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400867 drawable->draw(info);
868 fGpu->addDrawable(std::move(drawable));
869
870 if (bounds.extent.width == 0 || bounds.extent.height == 0) {
871 cbInfo.fBounds.join(target->getBoundsRect());
872 } else {
873 cbInfo.fBounds.join(SkRect::MakeXYWH(bounds.offset.x, bounds.offset.y,
874 bounds.extent.width, bounds.extent.height));
875 }
876}
877