-
Notifications
You must be signed in to change notification settings - Fork 0
/
looping_vulkan_control_flow_generator.go
3404 lines (2789 loc) · 117 KB
/
looping_vulkan_control_flow_generator.go
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vulkan
import (
"context"
"fmt"
"github.com/google/gapid/core/app/status"
"github.com/google/gapid/core/context/keys"
"github.com/google/gapid/core/event/task"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/math/interval"
"github.com/google/gapid/gapir"
"github.com/google/gapid/gapis/api"
"github.com/google/gapid/gapis/api/controlFlowGenerator"
"github.com/google/gapid/gapis/api/transform"
"github.com/google/gapid/gapis/capture"
"github.com/google/gapid/gapis/memory"
"github.com/google/gapid/gapis/replay"
"github.com/google/gapid/gapis/replay/builder"
"github.com/google/gapid/gapis/replay/protocol"
"github.com/google/gapid/gapis/replay/value"
)
type statewatcher struct {
memoryWrites map[memory.PoolID]*interval.U64SpanList
ignore bool // Ignore tracking current command
}
func (b *statewatcher) OnBeginCmd(ctx context.Context, cmdID api.CmdID, cmd api.Cmd) {
}
func (b *statewatcher) OnEndCmd(ctx context.Context, cmdID api.CmdID, cmd api.Cmd) {
}
func (b *statewatcher) OnBeginSubCmd(ctx context.Context, subIdx api.SubCmdIdx, recordIdx api.RecordIdx) {
}
func (b *statewatcher) OnRecordSubCmd(ctx context.Context, recordIdx api.RecordIdx) {
}
func (b *statewatcher) OnEndSubCmd(ctx context.Context) {
}
func (b *statewatcher) OnReadFrag(ctx context.Context, owner api.RefObject, frag api.Fragment, valueRef api.RefObject, track bool) {
}
func (b *statewatcher) OnWriteFrag(ctx context.Context, owner api.RefObject, frag api.Fragment, oldValueRef api.RefObject, newValueRef api.RefObject, track bool) {
}
func (b *statewatcher) OnWriteSlice(ctx context.Context, slice memory.Slice) {
if b.ignore {
return
}
span := interval.U64Span{
Start: slice.Base(),
End: slice.Base() + slice.Size(),
}
poolID := slice.Pool()
if _, ok := b.memoryWrites[poolID]; !ok {
b.memoryWrites[poolID] = &interval.U64SpanList{}
}
interval.Merge(b.memoryWrites[poolID], span, true)
}
func (b *statewatcher) OnReadSlice(ctx context.Context, slice memory.Slice) {
}
func (b *statewatcher) OnWriteObs(ctx context.Context, observations []api.CmdObservation) {
}
func (b *statewatcher) OnReadObs(ctx context.Context, observations []api.CmdObservation) {
}
func (b *statewatcher) OpenForwardDependency(ctx context.Context, dependencyID interface{}) {
}
func (b *statewatcher) CloseForwardDependency(ctx context.Context, dependencyID interface{}) {
}
func (b *statewatcher) DropForwardDependency(ctx context.Context, dependencyID interface{}) {
}
type backupMemory struct {
memory VkDeviceMemory
size VkDeviceSize
offset VkDeviceSize
}
type loopCallbackFunc func(ctx context.Context, request *gapir.FenceReadyRequest)
type loopCallbacks struct {
preLoop loopCallbackFunc
postLoop loopCallbackFunc
}
type loopingVulkanControlFlowGenerator struct {
ctx context.Context
chain *transform.TransformChain
out transform.Writer
capture *capture.GraphicsCapture
loopStartIdx api.CmdID
loopEndIdx api.CmdID
loopCount int32
loopCallbacks loopCallbacks
capturedLoopCmds []api.Cmd
capturedLoopCmdIds []api.CmdID
watcher *statewatcher
loopStartState *api.GlobalState
loopEndState *api.GlobalState
instanceToDestroy map[VkInstance]bool
instanceToCreate map[VkInstance]bool
deviceToDestroy map[VkDevice]bool
deviceToCreate map[VkDevice]bool
memoryToFree map[VkDeviceMemory]bool
memoryToAllocate map[VkDeviceMemory]bool
memoryToUnmap map[VkDeviceMemory]bool
memoryToMap map[VkDeviceMemory]bool
memoryForStagingBuffer map[VkDevice]*backupMemory
bufferToDestroy map[VkBuffer]bool
bufferChanged map[VkBuffer]bool
bufferToCreate map[VkBuffer]bool
bufferToRestore map[VkBuffer]VkBuffer
bufferViewToDestroy map[VkBufferView]bool
bufferViewToCreate map[VkBufferView]bool
surfaceToDestroy map[VkSurfaceKHR]bool
surfaceToCreate map[VkSurfaceKHR]bool
swapchainToDestroy map[VkSwapchainKHR]bool
swapchainToCreate map[VkSwapchainKHR]bool
imageToDestroy map[VkImage]bool
imageChanged map[VkImage]bool
imageToCreate map[VkImage]bool
imageToRestore map[VkImage]VkImage
imageViewToDestroy map[VkImageView]bool
imageViewToCreate map[VkImageView]bool
samplerYcbcrConversionToDestroy map[VkSamplerYcbcrConversion]bool
samplerYcbcrConversionToCreate map[VkSamplerYcbcrConversion]bool
samplerToDestroy map[VkSampler]bool
samplerToCreate map[VkSampler]bool
shaderModuleToDestroy map[VkShaderModule]bool
shaderModuleToCreate map[VkShaderModule]bool
descriptorSetLayoutToDestroy map[VkDescriptorSetLayout]bool
descriptorSetLayoutToCreate map[VkDescriptorSetLayout]bool
pipelineLayoutToDestroy map[VkPipelineLayout]bool
pipelineLayoutToCreate map[VkPipelineLayout]bool
pipelineCacheToDestroy map[VkPipelineCache]bool
pipelineCacheToCreate map[VkPipelineCache]bool
pipelineToDestroy map[VkPipeline]bool
computePipelineToCreate map[VkPipeline]bool
graphicsPipelineToCreate map[VkPipeline]bool
descriptorPoolToDestroy map[VkDescriptorPool]bool
descriptorPoolToCreate map[VkDescriptorPool]bool
descriptorSetToFree map[VkDescriptorSet]bool
descriptorSetToAllocate map[VkDescriptorSet]bool
descriptorSetChanged map[VkDescriptorSet]bool
descriptorSetAutoFreed map[VkDescriptorSet]bool
semaphoreToDestroy map[VkSemaphore]bool
semaphoreChanged map[VkSemaphore]bool
semaphoreToCreate map[VkSemaphore]bool
fenceToDestroy map[VkFence]bool
fenceChanged map[VkFence]bool
fenceToCreate map[VkFence]bool
eventToDestroy map[VkEvent]bool
eventChanged map[VkEvent]bool
eventToCreate map[VkEvent]bool
framebufferToDestroy map[VkFramebuffer]bool
framebufferToCreate map[VkFramebuffer]bool
renderPassToDestroy map[VkRenderPass]bool
renderPassToCreate map[VkRenderPass]bool
queryPoolToDestroy map[VkQueryPool]bool
queryPoolToCreate map[VkQueryPool]bool
commandPoolToDestroy map[VkCommandPool]bool
commandPoolToCreate map[VkCommandPool]bool
commandBufferToFree map[VkCommandBuffer]bool
commandBufferToAllocate map[VkCommandBuffer]bool
commandBufferToRecord map[VkCommandBuffer]bool
loopCountPtr value.Pointer
loopTerminated bool
lastObservedCommand api.CmdID
totalMemoryAllocated uint64
}
// NewLoopingVulkanControlFlowGenerator generates a simple control flow
// that takes initial and real commands and transforms all of them
func NewLoopingVulkanControlFlowGenerator(ctx context.Context, chain *transform.TransformChain, out transform.Writer, graphicsCapture *capture.GraphicsCapture, loopStart api.CmdID, loopEnd api.CmdID, loopCount int32, loopCallbacks loopCallbacks) controlFlowGenerator.ControlFlowGenerator {
if api.CmdID.Real(loopStart) >= api.CmdID.Real(loopEnd) {
log.F(ctx, true, "FrameLoop: Cannot create FrameLoop for zero or negative length loop")
return nil
}
if loopStart == api.CmdNoID || loopEnd == api.CmdNoID {
log.F(ctx, true, "FrameLoop: Cannot create FrameLoop that starts or ends on api.CmdNoID")
return nil
}
return &loopingVulkanControlFlowGenerator{
ctx: ctx,
chain: chain,
out: out,
capture: graphicsCapture,
loopStartIdx: api.CmdID.Real(loopStart),
loopEndIdx: api.CmdID.Real(loopEnd),
loopCount: loopCount,
loopCallbacks: loopCallbacks,
capturedLoopCmds: nil,
capturedLoopCmdIds: nil,
watcher: &statewatcher{
memoryWrites: make(map[memory.PoolID]*interval.U64SpanList),
},
instanceToDestroy: make(map[VkInstance]bool),
instanceToCreate: make(map[VkInstance]bool),
deviceToDestroy: make(map[VkDevice]bool),
deviceToCreate: make(map[VkDevice]bool),
memoryToFree: make(map[VkDeviceMemory]bool),
memoryToAllocate: make(map[VkDeviceMemory]bool),
memoryToUnmap: make(map[VkDeviceMemory]bool),
memoryToMap: make(map[VkDeviceMemory]bool),
memoryForStagingBuffer: make(map[VkDevice]*backupMemory),
bufferToDestroy: make(map[VkBuffer]bool),
bufferChanged: make(map[VkBuffer]bool),
bufferToCreate: make(map[VkBuffer]bool),
bufferToRestore: make(map[VkBuffer]VkBuffer),
bufferViewToDestroy: make(map[VkBufferView]bool),
bufferViewToCreate: make(map[VkBufferView]bool),
surfaceToDestroy: make(map[VkSurfaceKHR]bool),
surfaceToCreate: make(map[VkSurfaceKHR]bool),
swapchainToDestroy: make(map[VkSwapchainKHR]bool),
swapchainToCreate: make(map[VkSwapchainKHR]bool),
imageToDestroy: make(map[VkImage]bool),
imageChanged: make(map[VkImage]bool),
imageToCreate: make(map[VkImage]bool),
imageToRestore: make(map[VkImage]VkImage),
imageViewToDestroy: make(map[VkImageView]bool),
imageViewToCreate: make(map[VkImageView]bool),
samplerYcbcrConversionToDestroy: make(map[VkSamplerYcbcrConversion]bool),
samplerYcbcrConversionToCreate: make(map[VkSamplerYcbcrConversion]bool),
samplerToDestroy: make(map[VkSampler]bool),
samplerToCreate: make(map[VkSampler]bool),
shaderModuleToDestroy: make(map[VkShaderModule]bool),
shaderModuleToCreate: make(map[VkShaderModule]bool),
descriptorSetLayoutToDestroy: make(map[VkDescriptorSetLayout]bool),
descriptorSetLayoutToCreate: make(map[VkDescriptorSetLayout]bool),
pipelineLayoutToDestroy: make(map[VkPipelineLayout]bool),
pipelineLayoutToCreate: make(map[VkPipelineLayout]bool),
pipelineCacheToDestroy: make(map[VkPipelineCache]bool),
pipelineCacheToCreate: make(map[VkPipelineCache]bool),
pipelineToDestroy: make(map[VkPipeline]bool),
computePipelineToCreate: make(map[VkPipeline]bool),
graphicsPipelineToCreate: make(map[VkPipeline]bool),
descriptorPoolToDestroy: make(map[VkDescriptorPool]bool),
descriptorPoolToCreate: make(map[VkDescriptorPool]bool),
descriptorSetToFree: make(map[VkDescriptorSet]bool),
descriptorSetToAllocate: make(map[VkDescriptorSet]bool),
descriptorSetChanged: make(map[VkDescriptorSet]bool),
descriptorSetAutoFreed: make(map[VkDescriptorSet]bool),
semaphoreToDestroy: make(map[VkSemaphore]bool),
semaphoreChanged: make(map[VkSemaphore]bool),
semaphoreToCreate: make(map[VkSemaphore]bool),
fenceToDestroy: make(map[VkFence]bool),
fenceChanged: make(map[VkFence]bool),
fenceToCreate: make(map[VkFence]bool),
eventToDestroy: make(map[VkEvent]bool),
eventChanged: make(map[VkEvent]bool),
eventToCreate: make(map[VkEvent]bool),
framebufferToDestroy: make(map[VkFramebuffer]bool),
framebufferToCreate: make(map[VkFramebuffer]bool),
renderPassToDestroy: make(map[VkRenderPass]bool),
renderPassToCreate: make(map[VkRenderPass]bool),
queryPoolToDestroy: make(map[VkQueryPool]bool),
queryPoolToCreate: make(map[VkQueryPool]bool),
commandPoolToDestroy: make(map[VkCommandPool]bool),
commandPoolToCreate: make(map[VkCommandPool]bool),
commandBufferToFree: make(map[VkCommandBuffer]bool),
commandBufferToAllocate: make(map[VkCommandBuffer]bool),
commandBufferToRecord: make(map[VkCommandBuffer]bool),
loopTerminated: false,
lastObservedCommand: api.CmdNoID,
}
}
func (f *loopingVulkanControlFlowGenerator) TransformAll(ctx context.Context) error {
numberOfCmds := f.chain.GetNumOfRemainingCommands()
ctx = status.Start(ctx, "Running LoopingVulkanControlFlow <count:%v>", numberOfCmds)
defer status.Finish(ctx)
var id transform.CommandID
defer recoverFromPanic(id)
subctx := keys.Clone(context.Background(), ctx)
for !f.chain.IsEndOfCommands() {
id = f.chain.GetCurrentCommandID()
if id.GetCommandType() == transform.TransformCommand {
cmdID := uint64(id.GetID())
if cmdID%100 == 99 {
status.UpdateProgress(ctx, cmdID, numberOfCmds)
}
}
cmdId := id.GetID()
// Lets capture and update the last observed frame from f. From this point on use the local lastObservedCommand variable.
lastObservedCommand := f.lastObservedCommand
f.lastObservedCommand = cmdId
if cmds, err := f.chain.ProcessNextTransformedCommands(subctx); err == nil {
log.D(ctx, "FrameLoop: looping %v times from %v to %v. Current CmdID/CmDs = %v/%v", f.loopCount, f.loopStartIdx, f.loopEndIdx, id, cmds)
if err := task.StopReason(ctx); err != nil {
return err
}
if lastObservedCommand != api.CmdNoID && lastObservedCommand > api.CmdID.Real(cmdId) {
return fmt.Errorf("FrameLoop: expected next observed command ID to be >= last observed command ID")
}
// Are we before the loop or just at the start of it?
if lastObservedCommand == api.CmdNoID || lastObservedCommand < f.loopStartIdx {
// This is the start of the loop.
if api.CmdID.Real(cmdId) >= f.loopStartIdx && cmdId != api.CmdNoID {
log.D(ctx, "FrameLoop: start loop at cmdId %v, cmds %v.", cmdId, cmds)
for _, cmd := range cmds {
f.capturedLoopCmds = append(f.capturedLoopCmds, cmd)
f.capturedLoopCmdIds = append(f.capturedLoopCmdIds, cmdId)
}
continue
} else { // The current command is before the loop begins and needs no special treatment. Just pass-through.
log.D(ctx, "FrameLoop: before loop at cmdId %v, cmds %v.", cmdId, cmds)
if err := f.buildCommands(ctx, cmdId, cmds, f.out); err != nil {
return err
}
if api.CmdID.Real(cmdId) == f.loopStartIdx-1 {
f.loopStartState = f.cloneState(ctx, f.chain.State()) // ALAN: This is going to have the effects of the first command applied, we actually want the state one command earlier.
}
continue
}
} else if f.loopTerminated == false { // We're not before or at the start of the loop: thus, are we inside the loop or just at the end of it?
// This is the end of the loop. We have a lot of deferred things to do.
if api.CmdID.Real(cmdId) >= f.loopEndIdx && cmdId != api.CmdNoID {
if lastObservedCommand == api.CmdNoID {
return fmt.Errorf("FrameLoop: Somehow, the FrameLoop ended before it began. Did an earlier transform delete the whole loop? Were your loop indexes realistic?")
}
if len(f.capturedLoopCmdIds) != len(f.capturedLoopCmds) {
return fmt.Errorf("FrameLoop: Control flow error: Somehow, the number of captured commands and commandIds are not equal.") // TODO: Compress this list
}
f.loopTerminated = true
log.D(ctx, "FrameLoop: end loop at cmdId %v, cmds is %v.", cmdId, cmds)
// This command is the last in the loop so lets add it to the captured commands so we don't need to special case it.
for _, cmd := range cmds {
f.capturedLoopCmds = append(f.capturedLoopCmds, cmd)
f.capturedLoopCmdIds = append(f.capturedLoopCmdIds, cmdId)
}
// Some things we're going to need for the next work...
globalState := f.out.State()
stateBuilder := GetState(globalState).newStateBuilder(ctx, newTransformerOutput(f.out))
// If we're looping once or less this stuff isn't needed. If we're looping N > 1 times then we need to do the first N-1 loops.
if f.loopCount > 1 {
// Do start loop stuff.
{
// Now that we know the complete contents of the loop (only since we've just seen it finish!)...
// We can finally run over the loop contents looking for resources that have changed.
// This is required so we can emit extra instructions before the loop capturing the values of
// anything that we need to restore at the end of the loop. Do that now.
f.buildEndState(ctx, globalState)
f.detectChangedResources(ctx)
f.updateChangedResourcesMap(ctx, stateBuilder)
// Back up the resources that change in the loop (as indentified above)
if err := f.backupChangedResources(ctx, stateBuilder); err != nil {
return fmt.Errorf("FrameLoop: Failed to backup changed resources: %v", err)
}
}
// Mark branch target for loop jump
{
// Write out some custom bytecode for the loop.
stateBuilder.write(stateBuilder.cb.Custom(func(ctx context.Context, s *api.GlobalState, b *builder.Builder) error {
f.loopCountPtr = b.AllocateMemory(4)
b.Push(value.S32(f.loopCount - 1))
b.Store(f.loopCountPtr)
b.JumpLabel(uint32(0x1))
return nil
}))
}
// Do first N-1 iterations of mid-loop stuff.
if err := f.writeLoopContents(ctx, f.out); err != nil {
return err
}
// Do state rewind stuff.
{
// Now we need to emit the instructions to reset the state, before the conditional branch back to the start of the loop.
if err := f.resetResources(ctx, stateBuilder); err != nil {
return fmt.Errorf("FrameLoop: Failed to reset changed resources %v.", err)
}
}
// Write out the conditional jump to the start of the state rewind code to provide the actual looping behaviour
{
stateBuilder.write(stateBuilder.cb.Custom(func(ctx context.Context, s *api.GlobalState, b *builder.Builder) error {
b.Load(protocol.Type_Int32, f.loopCountPtr)
b.Sub(1)
b.Clone(0)
b.Store(f.loopCountPtr)
b.JumpNZ(uint32(0x1))
return nil
}))
}
}
// Start the perfetto
{
cmds := make([]api.Cmd, 0)
cmds = append(cmds, f.createVkDeviceWaitIdleCommandsForDevices(ctx, f.out.State())...)
waitForFenceCmd := f.createWaitForFence(ctx, uint32(id.GetID()), func(ctx context.Context, request *gapir.FenceReadyRequest) {
f.loopCallbacks.preLoop(ctx, request)
})
cmds = append(cmds, waitForFenceCmd)
if err := f.buildCommands(ctx, cmdId, cmds, f.out); err != nil {
return err
}
}
// Do the final iteration of mid-loop stuff.
if err := f.writeLoopContents(ctx, f.out); err != nil {
return err
}
// End the perfetto
{
cmds := make([]api.Cmd, 0)
cmds = append(cmds, f.createVkDeviceWaitIdleCommandsForDevices(ctx, f.out.State())...)
fenceID := uint32(0x3ffffff)
waitForFenceCmd := f.createWaitForFence(ctx, fenceID, func(ctx context.Context, request *gapir.FenceReadyRequest) {
f.loopCallbacks.postLoop(ctx, request)
})
cmds = append(cmds, waitForFenceCmd)
if err := f.buildCommands(ctx, cmdId, cmds, f.out); err != nil {
return err
}
}
// Finally, we've done all the processing for a loop. Nothing left to do.
continue
} else { // We're currently inside the loop.
// Lets just remember the command we've seen so we can do all the work we need at the end of the loop.
// This is done because the information we need to transform the loop is only available at that time;
// due to the possibility of preceeding transforms modifing the loop contents in-flight.
for _, cmd := range cmds {
f.capturedLoopCmds = append(f.capturedLoopCmds, cmd)
f.capturedLoopCmdIds = append(f.capturedLoopCmdIds, cmdId)
}
log.D(ctx, "FrameLoop: inside loop at cmdId %v, cmds %v.", cmdId, cmds)
continue
}
} else { // We're after the loop. Again, we can simply pass-through commands.
if err := f.buildCommands(ctx, cmdId, cmds, f.out); err != nil {
return err
}
continue
}
} else {
log.E(ctx, "FrameLoop: error processing command: %v.", err)
return err
}
}
return nil
}
func (f *loopingVulkanControlFlowGenerator) writeLoopContents(ctx context.Context, out transform.Writer) error {
// Iterate through the loop contents, emitting instructions one by one.
for cmdIndex, cmd := range f.capturedLoopCmds {
if err := out.MutateAndWrite(ctx, f.capturedLoopCmdIds[cmdIndex], cmd); err != nil {
return err
}
}
return nil
}
func recoverFromPanic(id transform.CommandID) {
r := recover()
if r == nil {
return
}
switch id.GetCommandType() {
case transform.TransformCommand:
panic(fmt.Errorf("Panic at command %v\n%v", id.GetID(), r))
case transform.EndCommand:
panic(fmt.Errorf("Panic at end command\n%v", r))
default:
panic(fmt.Errorf("Panic at Unknown command type\n%v", r))
}
}
func (f *loopingVulkanControlFlowGenerator) cloneState(ctx context.Context, state *api.GlobalState) *api.GlobalState {
clone := f.capture.NewUninitializedState(ctx)
clone.Memory = state.Memory.Clone()
for apiState, graphicsApi := range state.APIs {
clonedState := graphicsApi.Clone()
clonedState.SetupInitialState(ctx, clone)
clone.APIs[apiState] = clonedState
}
return clone
}
func (f *loopingVulkanControlFlowGenerator) buildEndState(ctx context.Context, startState *api.GlobalState) {
currentState := f.cloneState(ctx, f.loopStartState)
st := GetState(currentState)
st.PreSubcommand = func(i interface{}) {
cr, ok := i.(CommandReferenceʳ)
if ok {
args := GetCommandArgs(ctx, cr, st)
switch ar := args.(type) {
case VkCmdBeginRenderPassXArgsʳ:
rp := st.RenderPasses().Get(ar.RenderPassBeginInfo().RenderPass())
f.watcher.ignore = true
for i := uint32(0); i < uint32(rp.AttachmentDescriptions().Len()); i++ {
att := rp.AttachmentDescriptions().Get(i)
if att.InitialLayout() == VkImageLayout_VK_IMAGE_LAYOUT_UNDEFINED ||
att.LoadOp() == VkAttachmentLoadOp_VK_ATTACHMENT_LOAD_OP_CLEAR ||
att.LoadOp() == VkAttachmentLoadOp_VK_ATTACHMENT_LOAD_OP_DONT_CARE ||
att.StencilLoadOp() == VkAttachmentLoadOp_VK_ATTACHMENT_LOAD_OP_CLEAR ||
att.StencilLoadOp() == VkAttachmentLoadOp_VK_ATTACHMENT_LOAD_OP_DONT_CARE {
f.watcher.ignore = false
break
}
}
case VkCmdPipelineBarrierArgsʳ:
f.watcher.ignore = true
}
}
}
st.PostSubcommand = func(i interface{}) {
f.watcher.ignore = false
}
// Loop through each command mutating the shadow state and looking at what has been created/destroyed
err := api.ForeachCmd(ctx, f.capturedLoopCmds, true, func(ctx context.Context, cmdId api.CmdID, cmd api.Cmd) error {
cmd.Extras().Observations().ApplyReads(currentState.Memory.ApplicationPool())
cmd.Extras().Observations().ApplyWrites(currentState.Memory.ApplicationPool())
switch cmd.(type) {
// Instances
case *VkCreateInstance:
vkCmd := cmd.(*VkCreateInstance)
instance, err := vkCmd.PInstance().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "Instance %v created.", instance)
f.instanceToDestroy[instance] = true
case *VkDestroyInstance:
vkCmd := cmd.(*VkDestroyInstance)
instance := vkCmd.Instance()
log.D(ctx, "Instance %v destroyed.", instance)
if _, ok := f.instanceToDestroy[instance]; ok {
delete(f.instanceToDestroy, instance)
} else {
f.instanceToCreate[instance] = true
}
// Device
case *VkCreateDevice:
vkCmd := cmd.(*VkCreateDevice)
device, err := vkCmd.PDevice().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "Device %v created.", device)
f.deviceToDestroy[device] = true
case *VkDestroyDevice:
vkCmd := cmd.(*VkDestroyDevice)
device := vkCmd.Device()
log.D(ctx, "Device %v destroyed.", device)
if _, ok := f.deviceToDestroy[device]; ok {
delete(f.deviceToDestroy, device)
} else {
f.deviceToCreate[device] = true
}
// Memories
case *VkAllocateMemory:
vkCmd := cmd.(*VkAllocateMemory)
mem, err := vkCmd.PMemory().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "Memory %v allocated", mem)
f.memoryToFree[mem] = true
case *VkFreeMemory:
vkCmd := cmd.(*VkFreeMemory)
mem := vkCmd.Memory()
log.D(ctx, "Memory %v freed", mem)
if _, ok := f.memoryToFree[mem]; ok {
delete(f.memoryToFree, mem)
} else {
f.memoryToAllocate[mem] = true
}
// Memory mappings
case *VkMapMemory:
vkCmd := cmd.(*VkMapMemory)
mem := vkCmd.Memory()
log.D(ctx, "Memory %v mapped", mem)
f.memoryToUnmap[mem] = true
case *VkUnmapMemory:
vkCmd := cmd.(*VkUnmapMemory)
mem := vkCmd.Memory()
log.D(ctx, "Memory %v unmapped", mem)
if _, ok := f.memoryToUnmap[mem]; ok {
delete(f.memoryToUnmap, mem)
} else {
f.memoryToMap[mem] = true
}
// Buffers.
case *VkCreateBuffer:
vkCmd := cmd.(*VkCreateBuffer)
buffer, err := vkCmd.PBuffer().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "Buffer %v created.", buffer)
f.bufferToDestroy[buffer] = true
case *VkDestroyBuffer:
vkCmd := cmd.(*VkDestroyBuffer)
buffer := vkCmd.Buffer()
log.D(ctx, "Buffer %v destroyed.", buffer)
if _, ok := f.bufferToDestroy[buffer]; ok {
delete(f.bufferToDestroy, buffer)
} else {
f.bufferToCreate[buffer] = true
}
// Surfaces
case *VkCreateXlibSurfaceKHR:
vkCmd := cmd.(*VkCreateXlibSurfaceKHR)
surface, err := vkCmd.PSurface().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "Surface %v created", surface)
f.surfaceToDestroy[surface] = true
case *VkCreateWaylandSurfaceKHR:
vkCmd := cmd.(*VkCreateWaylandSurfaceKHR)
surface, err := vkCmd.PSurface().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "Surface %v created", surface)
f.surfaceToDestroy[surface] = true
case *VkCreateWin32SurfaceKHR:
vkCmd := cmd.(*VkCreateWin32SurfaceKHR)
surface, err := vkCmd.PSurface().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "Surface %v created", surface)
f.surfaceToDestroy[surface] = true
case *VkCreateAndroidSurfaceKHR:
vkCmd := cmd.(*VkCreateAndroidSurfaceKHR)
surface, err := vkCmd.PSurface().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "Surface %v created", surface)
f.surfaceToDestroy[surface] = true
case *VkCreateDisplayPlaneSurfaceKHR:
vkCmd := cmd.(*VkCreateDisplayPlaneSurfaceKHR)
surface, err := vkCmd.PSurface().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "Surface %v created", surface)
f.surfaceToDestroy[surface] = true
case *VkCreateMacOSSurfaceMVK:
vkCmd := cmd.(*VkCreateMacOSSurfaceMVK)
surface, err := vkCmd.PSurface().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "Surface %v created", surface)
f.surfaceToDestroy[surface] = true
case *VkDestroySurfaceKHR:
vkCmd := cmd.(*VkDestroySurfaceKHR)
surface := vkCmd.Surface()
log.D(ctx, "Surface %v destroyed", surface)
if _, ok := f.surfaceToDestroy[surface]; ok {
delete(f.surfaceToDestroy, surface)
} else {
f.surfaceToCreate[surface] = true
}
// Swapchains
case *VkCreateSwapchainKHR:
vkCmd := cmd.(*VkCreateSwapchainKHR)
swapchain, err := vkCmd.PSwapchain().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "Swapchain %v created", swapchain)
f.swapchainToDestroy[swapchain] = true
case *VkDestroySwapchainKHR:
vkCmd := cmd.(*VkDestroySwapchainKHR)
swapchain := vkCmd.Swapchain()
log.D(ctx, "Swapchain %v destroyed", swapchain)
if _, ok := f.swapchainToDestroy[swapchain]; ok {
delete(f.swapchainToDestroy, swapchain)
} else {
f.swapchainToCreate[swapchain] = true
}
// BufferViews
case *VkCreateBufferView:
vkCmd := cmd.(*VkCreateBufferView)
buffer, err := vkCmd.PView().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "BuferView %v created", buffer)
f.bufferViewToDestroy[buffer] = true
case *VkDestroyBufferView:
vkCmd := cmd.(*VkDestroyBufferView)
bufferView := vkCmd.BufferView()
log.D(ctx, "BufferView %v destroyed", bufferView)
if _, ok := f.bufferViewToDestroy[bufferView]; ok {
delete(f.bufferViewToDestroy, bufferView)
} else {
f.bufferViewToCreate[bufferView] = true
}
// Images
case *VkCreateImage:
vkCmd := cmd.(*VkCreateImage)
img, err := vkCmd.PImage().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "Image %v created", img)
f.imageToDestroy[img] = true
case *VkDestroyImage:
vkCmd := cmd.(*VkDestroyImage)
img := vkCmd.Image()
log.D(ctx, "Image %v destroyed", img)
if _, ok := f.imageToDestroy[img]; ok {
delete(f.imageToDestroy, img)
} else {
f.imageToCreate[img] = true
}
// ImageViews
case *VkCreateImageView:
vkCmd := cmd.(*VkCreateImageView)
img, err := vkCmd.PView().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "ImageView %v created", img)
f.imageViewToDestroy[img] = true
case *VkDestroyImageView:
vkCmd := cmd.(*VkDestroyImageView)
img := vkCmd.ImageView()
log.D(ctx, "ImageView %v destroyed", img)
if _, ok := f.imageViewToDestroy[img]; ok {
delete(f.imageViewToDestroy, img)
} else {
f.imageViewToCreate[img] = true
}
// SamplerYcbcrConversion(s)
case *VkCreateSamplerYcbcrConversion:
vkCmd := cmd.(*VkCreateSamplerYcbcrConversion)
samplerYcbcrConversion, err := vkCmd.PYcbcrConversion().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "SamplerYcbcrConversion %v created", samplerYcbcrConversion)
f.samplerYcbcrConversionToDestroy[samplerYcbcrConversion] = true
case *VkDestroySamplerYcbcrConversion:
vkCmd := cmd.(*VkDestroySamplerYcbcrConversion)
samplerYcbcrConversion := vkCmd.YcbcrConversion()
log.D(ctx, "SamplerYcbcrConversion %v destroyed", samplerYcbcrConversion)
if _, ok := f.samplerYcbcrConversionToDestroy[samplerYcbcrConversion]; ok {
delete(f.samplerYcbcrConversionToDestroy, samplerYcbcrConversion)
} else {
f.samplerYcbcrConversionToCreate[samplerYcbcrConversion] = true
}
// Sampler(s)
case *VkCreateSampler:
vkCmd := cmd.(*VkCreateSampler)
sampler, err := vkCmd.PSampler().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "Sampler %v created", sampler)
f.samplerToDestroy[sampler] = true
case *VkDestroySampler:
vkCmd := cmd.(*VkDestroySampler)
sampler := vkCmd.Sampler()
log.D(ctx, "Sampler %v destroyed", sampler)
if _, ok := f.samplerToDestroy[sampler]; ok {
delete(f.samplerToDestroy, sampler)
} else {
f.samplerToCreate[sampler] = true
}
// ShaderModule(s)
case *VkCreateShaderModule:
vkCmd := cmd.(*VkCreateShaderModule)
shaderModule, err := vkCmd.PShaderModule().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "ShaderModule %v created", shaderModule)
f.shaderModuleToDestroy[shaderModule] = true
case *VkDestroyShaderModule:
vkCmd := cmd.(*VkDestroyShaderModule)
shaderModule := vkCmd.ShaderModule()
log.D(ctx, "ShaderModule %v destroyed", shaderModule)
if _, ok := f.shaderModuleToDestroy[shaderModule]; ok {
delete(f.shaderModuleToDestroy, shaderModule)
} else {
f.shaderModuleToCreate[shaderModule] = true
}
// DescriptionSetLayout(s)
case *VkCreateDescriptorSetLayout:
vkCmd := cmd.(*VkCreateDescriptorSetLayout)
descriptorSetLayout, err := vkCmd.PSetLayout().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "DescriptorSetLayout %v created", descriptorSetLayout)
f.descriptorSetLayoutToDestroy[descriptorSetLayout] = true
case *VkDestroyDescriptorSetLayout:
vkCmd := cmd.(*VkDestroyDescriptorSetLayout)
descriptorSetLayout := vkCmd.DescriptorSetLayout()
log.D(ctx, "DescriptorSetLayout %v destroyed", descriptorSetLayout)
if _, ok := f.descriptorSetLayoutToDestroy[descriptorSetLayout]; ok {
delete(f.descriptorSetLayoutToDestroy, descriptorSetLayout)
} else {
f.descriptorSetLayoutToCreate[descriptorSetLayout] = true
}
// PipelineLayout(s)
case *VkCreatePipelineLayout:
vkCmd := cmd.(*VkCreatePipelineLayout)
pipelineLayout, err := vkCmd.PPipelineLayout().Read(ctx, vkCmd, currentState, nil)
if err != nil {
return err
}
log.D(ctx, "PipelineLayout %v created", pipelineLayout)
f.pipelineLayoutToDestroy[pipelineLayout] = true
case *VkDestroyPipelineLayout:
vkCmd := cmd.(*VkDestroyPipelineLayout)
pipelineLayout := vkCmd.PipelineLayout()
log.D(ctx, "PipelineLayout %v destroyed", pipelineLayout)
if _, ok := f.pipelineLayoutToDestroy[pipelineLayout]; ok {
delete(f.pipelineLayoutToDestroy, pipelineLayout)
} else {