1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrVkCommandBuffer.h"
#include "GrVkGpu.h"
#include "GrVkFramebuffer.h"
#include "GrVkImage.h"
#include "GrVkImageView.h"
#include "GrVkIndexBuffer.h"
#include "GrVkPipeline.h"
#include "GrVkPipelineState.h"
#include "GrVkRenderPass.h"
#include "GrVkRenderTarget.h"
#include "GrVkPipelineState.h"
#include "GrVkTransferBuffer.h"
#include "GrVkUtil.h"
#include "GrVkVertexBuffer.h"
#include "SkRect.h"
void GrVkCommandBuffer::invalidateState() {
for (auto& boundInputBuffer : fBoundInputBuffers) {
boundInputBuffer = VK_NULL_HANDLE;
}
fBoundIndexBuffer = VK_NULL_HANDLE;
memset(&fCachedViewport, 0, sizeof(VkViewport));
fCachedViewport.width = - 1.0f; // Viewport must have a width greater than 0
memset(&fCachedScissor, 0, sizeof(VkRect2D));
fCachedScissor.offset.x = -1; // Scissor offset must be greater that 0 to be valid
for (int i = 0; i < 4; ++i) {
fCachedBlendConstant[i] = -1.0;
}
}
void GrVkCommandBuffer::freeGPUData(const GrVkGpu* gpu) const {
SkASSERT(!fIsActive);
for (int i = 0; i < fTrackedResources.count(); ++i) {
fTrackedResources[i]->unref(gpu);
}
for (int i = 0; i < fTrackedRecycledResources.count(); ++i) {
fTrackedRecycledResources[i]->recycle(const_cast<GrVkGpu*>(gpu));
}
GR_VK_CALL(gpu->vkInterface(), FreeCommandBuffers(gpu->device(), gpu->cmdPool(),
1, &fCmdBuffer));
this->onFreeGPUData(gpu);
}
void GrVkCommandBuffer::abandonGPUData() const {
for (int i = 0; i < fTrackedResources.count(); ++i) {
fTrackedResources[i]->unrefAndAbandon();
}
for (int i = 0; i < fTrackedRecycledResources.count(); ++i) {
// We don't recycle resources when abandoning them.
fTrackedRecycledResources[i]->unrefAndAbandon();
}
}
void GrVkCommandBuffer::reset(GrVkGpu* gpu) {
SkASSERT(!fIsActive);
for (int i = 0; i < fTrackedResources.count(); ++i) {
fTrackedResources[i]->unref(gpu);
}
for (int i = 0; i < fTrackedRecycledResources.count(); ++i) {
fTrackedRecycledResources[i]->recycle(const_cast<GrVkGpu*>(gpu));
}
if (++fNumResets > kNumRewindResetsBeforeFullReset) {
fTrackedResources.reset();
fTrackedRecycledResources.reset();
fTrackedResources.setReserve(kInitialTrackedResourcesCount);
fTrackedRecycledResources.setReserve(kInitialTrackedResourcesCount);
fNumResets = 0;
} else {
fTrackedResources.rewind();
fTrackedRecycledResources.rewind();
}
this->invalidateState();
// we will retain resources for later use
VkCommandBufferResetFlags flags = 0;
GR_VK_CALL(gpu->vkInterface(), ResetCommandBuffer(fCmdBuffer, flags));
this->onReset(gpu);
}
////////////////////////////////////////////////////////////////////////////////
// CommandBuffer commands
////////////////////////////////////////////////////////////////////////////////
void GrVkCommandBuffer::pipelineBarrier(const GrVkGpu* gpu,
VkPipelineStageFlags srcStageMask,
VkPipelineStageFlags dstStageMask,
bool byRegion,
BarrierType barrierType,
void* barrier) const {
SkASSERT(fIsActive);
// For images we can have barriers inside of render passes but they require us to add more
// support in subpasses which need self dependencies to have barriers inside them. Also, we can
// never have buffer barriers inside of a render pass. For now we will just assert that we are
// not in a render pass.
SkASSERT(!fActiveRenderPass);
VkDependencyFlags dependencyFlags = byRegion ? VK_DEPENDENCY_BY_REGION_BIT : 0;
switch (barrierType) {
case kMemory_BarrierType: {
const VkMemoryBarrier* barrierPtr = reinterpret_cast<VkMemoryBarrier*>(barrier);
GR_VK_CALL(gpu->vkInterface(), CmdPipelineBarrier(fCmdBuffer, srcStageMask,
dstStageMask, dependencyFlags,
1, barrierPtr,
0, nullptr,
0, nullptr));
break;
}
case kBufferMemory_BarrierType: {
const VkBufferMemoryBarrier* barrierPtr =
reinterpret_cast<VkBufferMemoryBarrier*>(barrier);
GR_VK_CALL(gpu->vkInterface(), CmdPipelineBarrier(fCmdBuffer, srcStageMask,
dstStageMask, dependencyFlags,
0, nullptr,
1, barrierPtr,
0, nullptr));
break;
}
case kImageMemory_BarrierType: {
const VkImageMemoryBarrier* barrierPtr =
reinterpret_cast<VkImageMemoryBarrier*>(barrier);
GR_VK_CALL(gpu->vkInterface(), CmdPipelineBarrier(fCmdBuffer, srcStageMask,
dstStageMask, dependencyFlags,
0, nullptr,
0, nullptr,
1, barrierPtr));
break;
}
}
}
void GrVkCommandBuffer::bindInputBuffer(GrVkGpu* gpu, uint32_t binding,
const GrVkVertexBuffer* vbuffer) {
VkBuffer vkBuffer = vbuffer->buffer();
SkASSERT(VK_NULL_HANDLE != vkBuffer);
SkASSERT(binding < kMaxInputBuffers);
// TODO: once vbuffer->offset() no longer always returns 0, we will need to track the offset
// to know if we can skip binding or not.
if (vkBuffer != fBoundInputBuffers[binding]) {
VkDeviceSize offset = vbuffer->offset();
GR_VK_CALL(gpu->vkInterface(), CmdBindVertexBuffers(fCmdBuffer,
binding,
1,
&vkBuffer,
&offset));
fBoundInputBuffers[binding] = vkBuffer;
addResource(vbuffer->resource());
}
}
void GrVkCommandBuffer::bindIndexBuffer(GrVkGpu* gpu, const GrVkIndexBuffer* ibuffer) {
VkBuffer vkBuffer = ibuffer->buffer();
SkASSERT(VK_NULL_HANDLE != vkBuffer);
// TODO: once ibuffer->offset() no longer always returns 0, we will need to track the offset
// to know if we can skip binding or not.
if (vkBuffer != fBoundIndexBuffer) {
GR_VK_CALL(gpu->vkInterface(), CmdBindIndexBuffer(fCmdBuffer,
vkBuffer,
ibuffer->offset(),
VK_INDEX_TYPE_UINT16));
fBoundIndexBuffer = vkBuffer;
addResource(ibuffer->resource());
}
}
void GrVkCommandBuffer::clearAttachments(const GrVkGpu* gpu,
int numAttachments,
const VkClearAttachment* attachments,
int numRects,
const VkClearRect* clearRects) const {
SkASSERT(fIsActive);
SkASSERT(fActiveRenderPass);
SkASSERT(numAttachments > 0);
SkASSERT(numRects > 0);
#ifdef SK_DEBUG
for (int i = 0; i < numAttachments; ++i) {
if (attachments[i].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) {
uint32_t testIndex;
SkAssertResult(fActiveRenderPass->colorAttachmentIndex(&testIndex));
SkASSERT(testIndex == attachments[i].colorAttachment);
}
}
#endif
GR_VK_CALL(gpu->vkInterface(), CmdClearAttachments(fCmdBuffer,
numAttachments,
attachments,
numRects,
clearRects));
}
void GrVkCommandBuffer::bindDescriptorSets(const GrVkGpu* gpu,
GrVkPipelineState* pipelineState,
VkPipelineLayout layout,
uint32_t firstSet,
uint32_t setCount,
const VkDescriptorSet* descriptorSets,
uint32_t dynamicOffsetCount,
const uint32_t* dynamicOffsets) {
SkASSERT(fIsActive);
GR_VK_CALL(gpu->vkInterface(), CmdBindDescriptorSets(fCmdBuffer,
VK_PIPELINE_BIND_POINT_GRAPHICS,
layout,
firstSet,
setCount,
descriptorSets,
dynamicOffsetCount,
dynamicOffsets));
pipelineState->addUniformResources(*this);
}
void GrVkCommandBuffer::bindDescriptorSets(const GrVkGpu* gpu,
const SkTArray<const GrVkRecycledResource*>& recycled,
const SkTArray<const GrVkResource*>& resources,
VkPipelineLayout layout,
uint32_t firstSet,
uint32_t setCount,
const VkDescriptorSet* descriptorSets,
uint32_t dynamicOffsetCount,
const uint32_t* dynamicOffsets) {
SkASSERT(fIsActive);
GR_VK_CALL(gpu->vkInterface(), CmdBindDescriptorSets(fCmdBuffer,
VK_PIPELINE_BIND_POINT_GRAPHICS,
layout,
firstSet,
setCount,
descriptorSets,
dynamicOffsetCount,
dynamicOffsets));
for (int i = 0; i < recycled.count(); ++i) {
this->addRecycledResource(recycled[i]);
}
for (int i = 0; i < resources.count(); ++i) {
this->addResource(resources[i]);
}
}
void GrVkCommandBuffer::bindPipeline(const GrVkGpu* gpu, const GrVkPipeline* pipeline) {
SkASSERT(fIsActive);
GR_VK_CALL(gpu->vkInterface(), CmdBindPipeline(fCmdBuffer,
VK_PIPELINE_BIND_POINT_GRAPHICS,
pipeline->pipeline()));
this->addResource(pipeline);
}
void GrVkCommandBuffer::drawIndexed(const GrVkGpu* gpu,
uint32_t indexCount,
uint32_t instanceCount,
uint32_t firstIndex,
int32_t vertexOffset,
uint32_t firstInstance) const {
SkASSERT(fIsActive);
SkASSERT(fActiveRenderPass);
GR_VK_CALL(gpu->vkInterface(), CmdDrawIndexed(fCmdBuffer,
indexCount,
instanceCount,
firstIndex,
vertexOffset,
firstInstance));
}
void GrVkCommandBuffer::draw(const GrVkGpu* gpu,
uint32_t vertexCount,
uint32_t instanceCount,
uint32_t firstVertex,
uint32_t firstInstance) const {
SkASSERT(fIsActive);
SkASSERT(fActiveRenderPass);
GR_VK_CALL(gpu->vkInterface(), CmdDraw(fCmdBuffer,
vertexCount,
instanceCount,
firstVertex,
firstInstance));
}
void GrVkCommandBuffer::setViewport(const GrVkGpu* gpu,
uint32_t firstViewport,
uint32_t viewportCount,
const VkViewport* viewports) {
SkASSERT(fIsActive);
SkASSERT(1 == viewportCount);
if (memcmp(viewports, &fCachedViewport, sizeof(VkViewport))) {
GR_VK_CALL(gpu->vkInterface(), CmdSetViewport(fCmdBuffer,
firstViewport,
viewportCount,
viewports));
fCachedViewport = viewports[0];
}
}
void GrVkCommandBuffer::setScissor(const GrVkGpu* gpu,
uint32_t firstScissor,
uint32_t scissorCount,
const VkRect2D* scissors) {
SkASSERT(fIsActive);
SkASSERT(1 == scissorCount);
if (memcmp(scissors, &fCachedScissor, sizeof(VkRect2D))) {
GR_VK_CALL(gpu->vkInterface(), CmdSetScissor(fCmdBuffer,
firstScissor,
scissorCount,
scissors));
fCachedScissor = scissors[0];
}
}
void GrVkCommandBuffer::setBlendConstants(const GrVkGpu* gpu,
const float blendConstants[4]) {
SkASSERT(fIsActive);
if (memcmp(blendConstants, fCachedBlendConstant, 4 * sizeof(float))) {
GR_VK_CALL(gpu->vkInterface(), CmdSetBlendConstants(fCmdBuffer, blendConstants));
memcpy(fCachedBlendConstant, blendConstants, 4 * sizeof(float));
}
}
///////////////////////////////////////////////////////////////////////////////
// PrimaryCommandBuffer
////////////////////////////////////////////////////////////////////////////////
GrVkPrimaryCommandBuffer::~GrVkPrimaryCommandBuffer() {
// Should have ended any render pass we're in the middle of
SkASSERT(!fActiveRenderPass);
}
GrVkPrimaryCommandBuffer* GrVkPrimaryCommandBuffer::Create(const GrVkGpu* gpu,
VkCommandPool cmdPool) {
const VkCommandBufferAllocateInfo cmdInfo = {
VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
nullptr, // pNext
cmdPool, // commandPool
VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1 // bufferCount
};
VkCommandBuffer cmdBuffer;
VkResult err = GR_VK_CALL(gpu->vkInterface(), AllocateCommandBuffers(gpu->device(),
&cmdInfo,
&cmdBuffer));
if (err) {
return nullptr;
}
return new GrVkPrimaryCommandBuffer(cmdBuffer);
}
void GrVkPrimaryCommandBuffer::begin(const GrVkGpu* gpu) {
SkASSERT(!fIsActive);
VkCommandBufferBeginInfo cmdBufferBeginInfo;
memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
cmdBufferBeginInfo.pNext = nullptr;
cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
cmdBufferBeginInfo.pInheritanceInfo = nullptr;
GR_VK_CALL_ERRCHECK(gpu->vkInterface(), BeginCommandBuffer(fCmdBuffer,
&cmdBufferBeginInfo));
fIsActive = true;
}
void GrVkPrimaryCommandBuffer::end(const GrVkGpu* gpu) {
SkASSERT(fIsActive);
SkASSERT(!fActiveRenderPass);
GR_VK_CALL_ERRCHECK(gpu->vkInterface(), EndCommandBuffer(fCmdBuffer));
this->invalidateState();
fIsActive = false;
}
void GrVkPrimaryCommandBuffer::beginRenderPass(const GrVkGpu* gpu,
const GrVkRenderPass* renderPass,
const VkClearValue clearValues[],
const GrVkRenderTarget& target,
const SkIRect& bounds,
bool forSecondaryCB) {
SkASSERT(fIsActive);
SkASSERT(!fActiveRenderPass);
SkASSERT(renderPass->isCompatible(target));
VkRenderPassBeginInfo beginInfo;
VkRect2D renderArea;
renderArea.offset = { bounds.fLeft , bounds.fTop };
renderArea.extent = { (uint32_t)bounds.width(), (uint32_t)bounds.height() };
memset(&beginInfo, 0, sizeof(VkRenderPassBeginInfo));
beginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
beginInfo.pNext = nullptr;
beginInfo.renderPass = renderPass->vkRenderPass();
beginInfo.framebuffer = target.framebuffer()->framebuffer();
beginInfo.renderArea = renderArea;
beginInfo.clearValueCount = renderPass->clearValueCount();
beginInfo.pClearValues = clearValues;
VkSubpassContents contents = forSecondaryCB ? VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS
: VK_SUBPASS_CONTENTS_INLINE;
GR_VK_CALL(gpu->vkInterface(), CmdBeginRenderPass(fCmdBuffer, &beginInfo, contents));
fActiveRenderPass = renderPass;
this->addResource(renderPass);
target.addResources(*this);
}
void GrVkPrimaryCommandBuffer::endRenderPass(const GrVkGpu* gpu) {
SkASSERT(fIsActive);
SkASSERT(fActiveRenderPass);
GR_VK_CALL(gpu->vkInterface(), CmdEndRenderPass(fCmdBuffer));
fActiveRenderPass = nullptr;
}
void GrVkPrimaryCommandBuffer::executeCommands(const GrVkGpu* gpu,
GrVkSecondaryCommandBuffer* buffer) {
SkASSERT(fIsActive);
SkASSERT(!buffer->fIsActive);
SkASSERT(fActiveRenderPass);
SkASSERT(fActiveRenderPass->isCompatible(*buffer->fActiveRenderPass));
GR_VK_CALL(gpu->vkInterface(), CmdExecuteCommands(fCmdBuffer, 1, &buffer->fCmdBuffer));
buffer->ref();
fSecondaryCommandBuffers.push_back(buffer);
// When executing a secondary command buffer all state (besides render pass state) becomes
// invalidated and must be reset. This includes bound buffers, pipelines, dynamic state, etc.
this->invalidateState();
}
static void submit_to_queue(const GrVkInterface* interface,
VkQueue queue,
VkFence fence,
uint32_t waitCount,
const VkSemaphore* waitSemaphores,
const VkPipelineStageFlags* waitStages,
uint32_t commandBufferCount,
const VkCommandBuffer* commandBuffers,
uint32_t signalCount,
const VkSemaphore* signalSemaphores) {
VkSubmitInfo submitInfo;
memset(&submitInfo, 0, sizeof(VkSubmitInfo));
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.pNext = nullptr;
submitInfo.waitSemaphoreCount = waitCount;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = commandBufferCount;
submitInfo.pCommandBuffers = commandBuffers;
submitInfo.signalSemaphoreCount = signalCount;
submitInfo.pSignalSemaphores = signalSemaphores;
GR_VK_CALL_ERRCHECK(interface, QueueSubmit(queue, 1, &submitInfo, fence));
}
void GrVkPrimaryCommandBuffer::submitToQueue(
const GrVkGpu* gpu,
VkQueue queue,
GrVkGpu::SyncQueue sync,
SkTArray<GrVkSemaphore::Resource*>& signalSemaphores,
SkTArray<GrVkSemaphore::Resource*>& waitSemaphores) {
SkASSERT(!fIsActive);
VkResult err;
if (VK_NULL_HANDLE == fSubmitFence) {
VkFenceCreateInfo fenceInfo;
memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
err = GR_VK_CALL(gpu->vkInterface(), CreateFence(gpu->device(), &fenceInfo, nullptr,
&fSubmitFence));
SkASSERT(!err);
} else {
GR_VK_CALL(gpu->vkInterface(), ResetFences(gpu->device(), 1, &fSubmitFence));
}
int signalCount = signalSemaphores.count();
int waitCount = waitSemaphores.count();
if (0 == signalCount && 0 == waitCount) {
// This command buffer has no dependent semaphores so we can simply just submit it to the
// queue with no worries.
submit_to_queue(gpu->vkInterface(), queue, fSubmitFence, 0, nullptr, nullptr,
1, &fCmdBuffer, 0, nullptr);
} else {
GrVkSemaphore::Resource::AcquireMutex();
SkTArray<VkSemaphore> vkSignalSems(signalCount);
for (int i = 0; i < signalCount; ++i) {
if (signalSemaphores[i]->shouldSignal()) {
this->addResource(signalSemaphores[i]);
vkSignalSems.push_back(signalSemaphores[i]->semaphore());
}
}
SkTArray<VkSemaphore> vkWaitSems(waitCount);
SkTArray<VkPipelineStageFlags> vkWaitStages(waitCount);
for (int i = 0; i < waitCount; ++i) {
if (waitSemaphores[i]->shouldWait()) {
this->addResource(waitSemaphores[i]);
vkWaitSems.push_back(waitSemaphores[i]->semaphore());
vkWaitStages.push_back(VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
}
}
submit_to_queue(gpu->vkInterface(), queue, fSubmitFence,
vkWaitSems.count(), vkWaitSems.begin(), vkWaitStages.begin(),
1, &fCmdBuffer,
vkSignalSems.count(), vkSignalSems.begin());
// Since shouldSignal/Wait do not require a mutex to be held, we must make sure that we mark
// the semaphores after we've submitted. Thus in the worst case another submit grabs the
// mutex and then realizes it doesn't need to submit the semaphore. We will never end up
// where a semaphore doesn't think it needs to be submitted (cause of querying
// shouldSignal/Wait), but it should need to.
for (int i = 0; i < signalCount; ++i) {
signalSemaphores[i]->markAsSignaled();
}
for (int i = 0; i < waitCount; ++i) {
waitSemaphores[i]->markAsWaited();
}
GrVkSemaphore::Resource::ReleaseMutex();
}
if (GrVkGpu::kForce_SyncQueue == sync) {
err = GR_VK_CALL(gpu->vkInterface(),
WaitForFences(gpu->device(), 1, &fSubmitFence, true, UINT64_MAX));
if (VK_TIMEOUT == err) {
SkDebugf("Fence failed to signal: %d\n", err);
SK_ABORT("failing");
}
SkASSERT(!err);
// Destroy the fence
GR_VK_CALL(gpu->vkInterface(), DestroyFence(gpu->device(), fSubmitFence, nullptr));
fSubmitFence = VK_NULL_HANDLE;
}
}
bool GrVkPrimaryCommandBuffer::finished(const GrVkGpu* gpu) const {
if (VK_NULL_HANDLE == fSubmitFence) {
return true;
}
VkResult err = GR_VK_CALL(gpu->vkInterface(), GetFenceStatus(gpu->device(), fSubmitFence));
switch (err) {
case VK_SUCCESS:
return true;
case VK_NOT_READY:
return false;
default:
SkDebugf("Error getting fence status: %d\n", err);
SK_ABORT("failing");
break;
}
return false;
}
void GrVkPrimaryCommandBuffer::onReset(GrVkGpu* gpu) {
for (int i = 0; i < fSecondaryCommandBuffers.count(); ++i) {
gpu->resourceProvider().recycleSecondaryCommandBuffer(fSecondaryCommandBuffers[i]);
}
fSecondaryCommandBuffers.reset();
}
void GrVkPrimaryCommandBuffer::copyImage(const GrVkGpu* gpu,
GrVkImage* srcImage,
VkImageLayout srcLayout,
GrVkImage* dstImage,
VkImageLayout dstLayout,
uint32_t copyRegionCount,
const VkImageCopy* copyRegions) {
SkASSERT(fIsActive);
SkASSERT(!fActiveRenderPass);
this->addResource(srcImage->resource());
this->addResource(dstImage->resource());
GR_VK_CALL(gpu->vkInterface(), CmdCopyImage(fCmdBuffer,
srcImage->image(),
srcLayout,
dstImage->image(),
dstLayout,
copyRegionCount,
copyRegions));
}
void GrVkPrimaryCommandBuffer::blitImage(const GrVkGpu* gpu,
const GrVkResource* srcResource,
VkImage srcImage,
VkImageLayout srcLayout,
const GrVkResource* dstResource,
VkImage dstImage,
VkImageLayout dstLayout,
uint32_t blitRegionCount,
const VkImageBlit* blitRegions,
VkFilter filter) {
SkASSERT(fIsActive);
SkASSERT(!fActiveRenderPass);
this->addResource(srcResource);
this->addResource(dstResource);
GR_VK_CALL(gpu->vkInterface(), CmdBlitImage(fCmdBuffer,
srcImage,
srcLayout,
dstImage,
dstLayout,
blitRegionCount,
blitRegions,
filter));
}
void GrVkPrimaryCommandBuffer::blitImage(const GrVkGpu* gpu,
const GrVkImage& srcImage,
const GrVkImage& dstImage,
uint32_t blitRegionCount,
const VkImageBlit* blitRegions,
VkFilter filter) {
this->blitImage(gpu,
srcImage.resource(),
srcImage.image(),
srcImage.currentLayout(),
dstImage.resource(),
dstImage.image(),
dstImage.currentLayout(),
blitRegionCount,
blitRegions,
filter);
}
void GrVkPrimaryCommandBuffer::copyImageToBuffer(const GrVkGpu* gpu,
GrVkImage* srcImage,
VkImageLayout srcLayout,
GrVkTransferBuffer* dstBuffer,
uint32_t copyRegionCount,
const VkBufferImageCopy* copyRegions) {
SkASSERT(fIsActive);
SkASSERT(!fActiveRenderPass);
this->addResource(srcImage->resource());
this->addResource(dstBuffer->resource());
GR_VK_CALL(gpu->vkInterface(), CmdCopyImageToBuffer(fCmdBuffer,
srcImage->image(),
srcLayout,
dstBuffer->buffer(),
copyRegionCount,
copyRegions));
}
void GrVkPrimaryCommandBuffer::copyBufferToImage(const GrVkGpu* gpu,
GrVkTransferBuffer* srcBuffer,
GrVkImage* dstImage,
VkImageLayout dstLayout,
uint32_t copyRegionCount,
const VkBufferImageCopy* copyRegions) {
SkASSERT(fIsActive);
SkASSERT(!fActiveRenderPass);
this->addResource(srcBuffer->resource());
this->addResource(dstImage->resource());
GR_VK_CALL(gpu->vkInterface(), CmdCopyBufferToImage(fCmdBuffer,
srcBuffer->buffer(),
dstImage->image(),
dstLayout,
copyRegionCount,
copyRegions));
}
void GrVkPrimaryCommandBuffer::copyBuffer(GrVkGpu* gpu,
GrVkBuffer* srcBuffer,
GrVkBuffer* dstBuffer,
uint32_t regionCount,
const VkBufferCopy* regions) {
SkASSERT(fIsActive);
SkASSERT(!fActiveRenderPass);
#ifdef SK_DEBUG
for (uint32_t i = 0; i < regionCount; ++i) {
const VkBufferCopy& region = regions[i];
SkASSERT(region.size > 0);
SkASSERT(region.srcOffset < srcBuffer->size());
SkASSERT(region.dstOffset < dstBuffer->size());
SkASSERT(region.srcOffset + region.size <= srcBuffer->size());
SkASSERT(region.dstOffset + region.size <= dstBuffer->size());
}
#endif
this->addResource(srcBuffer->resource());
this->addResource(dstBuffer->resource());
GR_VK_CALL(gpu->vkInterface(), CmdCopyBuffer(fCmdBuffer,
srcBuffer->buffer(),
dstBuffer->buffer(),
regionCount,
regions));
}
void GrVkPrimaryCommandBuffer::updateBuffer(GrVkGpu* gpu,
GrVkBuffer* dstBuffer,
VkDeviceSize dstOffset,
VkDeviceSize dataSize,
const void* data) {
SkASSERT(fIsActive);
SkASSERT(!fActiveRenderPass);
SkASSERT(0 == (dstOffset & 0x03)); // four byte aligned
// TODO: handle larger transfer sizes
SkASSERT(dataSize <= 65536);
SkASSERT(0 == (dataSize & 0x03)); // four byte aligned
this->addResource(dstBuffer->resource());
GR_VK_CALL(gpu->vkInterface(), CmdUpdateBuffer(fCmdBuffer,
dstBuffer->buffer(),
dstOffset,
dataSize,
(const uint32_t*) data));
}
void GrVkPrimaryCommandBuffer::clearColorImage(const GrVkGpu* gpu,
GrVkImage* image,
const VkClearColorValue* color,
uint32_t subRangeCount,
const VkImageSubresourceRange* subRanges) {
SkASSERT(fIsActive);
SkASSERT(!fActiveRenderPass);
this->addResource(image->resource());
GR_VK_CALL(gpu->vkInterface(), CmdClearColorImage(fCmdBuffer,
image->image(),
image->currentLayout(),
color,
subRangeCount,
subRanges));
}
void GrVkPrimaryCommandBuffer::clearDepthStencilImage(const GrVkGpu* gpu,
GrVkImage* image,
const VkClearDepthStencilValue* color,
uint32_t subRangeCount,
const VkImageSubresourceRange* subRanges) {
SkASSERT(fIsActive);
SkASSERT(!fActiveRenderPass);
this->addResource(image->resource());
GR_VK_CALL(gpu->vkInterface(), CmdClearDepthStencilImage(fCmdBuffer,
image->image(),
image->currentLayout(),
color,
subRangeCount,
subRanges));
}
void GrVkPrimaryCommandBuffer::resolveImage(GrVkGpu* gpu,
const GrVkImage& srcImage,
const GrVkImage& dstImage,
uint32_t regionCount,
const VkImageResolve* regions) {
SkASSERT(fIsActive);
SkASSERT(!fActiveRenderPass);
this->addResource(srcImage.resource());
this->addResource(dstImage.resource());
GR_VK_CALL(gpu->vkInterface(), CmdResolveImage(fCmdBuffer,
srcImage.image(),
srcImage.currentLayout(),
dstImage.image(),
dstImage.currentLayout(),
regionCount,
regions));
}
void GrVkPrimaryCommandBuffer::onFreeGPUData(const GrVkGpu* gpu) const {
SkASSERT(!fActiveRenderPass);
// Destroy the fence, if any
if (VK_NULL_HANDLE != fSubmitFence) {
GR_VK_CALL(gpu->vkInterface(), DestroyFence(gpu->device(), fSubmitFence, nullptr));
}
}
///////////////////////////////////////////////////////////////////////////////
// SecondaryCommandBuffer
////////////////////////////////////////////////////////////////////////////////
GrVkSecondaryCommandBuffer* GrVkSecondaryCommandBuffer::Create(const GrVkGpu* gpu,
VkCommandPool cmdPool) {
const VkCommandBufferAllocateInfo cmdInfo = {
VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
nullptr, // pNext
cmdPool, // commandPool
VK_COMMAND_BUFFER_LEVEL_SECONDARY, // level
1 // bufferCount
};
VkCommandBuffer cmdBuffer;
VkResult err = GR_VK_CALL(gpu->vkInterface(), AllocateCommandBuffers(gpu->device(),
&cmdInfo,
&cmdBuffer));
if (err) {
return nullptr;
}
return new GrVkSecondaryCommandBuffer(cmdBuffer);
}
void GrVkSecondaryCommandBuffer::begin(const GrVkGpu* gpu, const GrVkFramebuffer* framebuffer,
const GrVkRenderPass* compatibleRenderPass) {
SkASSERT(!fIsActive);
SkASSERT(compatibleRenderPass);
fActiveRenderPass = compatibleRenderPass;
VkCommandBufferInheritanceInfo inheritanceInfo;
memset(&inheritanceInfo, 0, sizeof(VkCommandBufferInheritanceInfo));
inheritanceInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO;
inheritanceInfo.pNext = nullptr;
inheritanceInfo.renderPass = fActiveRenderPass->vkRenderPass();
inheritanceInfo.subpass = 0; // Currently only using 1 subpass for each render pass
inheritanceInfo.framebuffer = framebuffer ? framebuffer->framebuffer() : VK_NULL_HANDLE;
inheritanceInfo.occlusionQueryEnable = false;
inheritanceInfo.queryFlags = 0;
inheritanceInfo.pipelineStatistics = 0;
VkCommandBufferBeginInfo cmdBufferBeginInfo;
memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
cmdBufferBeginInfo.pNext = nullptr;
cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT |
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
cmdBufferBeginInfo.pInheritanceInfo = &inheritanceInfo;
GR_VK_CALL_ERRCHECK(gpu->vkInterface(), BeginCommandBuffer(fCmdBuffer,
&cmdBufferBeginInfo));
fIsActive = true;
}
void GrVkSecondaryCommandBuffer::end(const GrVkGpu* gpu) {
SkASSERT(fIsActive);
GR_VK_CALL_ERRCHECK(gpu->vkInterface(), EndCommandBuffer(fCmdBuffer));
this->invalidateState();
fIsActive = false;
}
|