/
Record.cpp
1129 lines (922 loc) · 28.2 KB
/
Record.cpp
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) 2017- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <algorithm>
#include <cstring>
#include <functional>
#include <vector>
#include <snappy-c.h>
#include "base/stringutil.h"
#include "Common/Common.h"
#include "Common/FileUtil.h"
#include "Common/Log.h"
#include "Core/Core.h"
#include "Core/CoreTiming.h"
#include "Core/ELF/ParamSFO.h"
#include "Core/FileSystems/MetaFileSystem.h"
#include "Core/HLE/sceDisplay.h"
#include "Core/HLE/sceKernelMemory.h"
#include "Core/MemMap.h"
#include "Core/MIPS/MIPS.h"
#include "Core/System.h"
#include "GPU/GPUInterface.h"
#include "GPU/GPUState.h"
#include "GPU/ge_constants.h"
#include "GPU/Common/TextureDecoder.h"
#include "GPU/Common/VertexDecoderCommon.h"
#include "GPU/Debugger/Record.h"
namespace GPURecord {
static const char *HEADER = "PPSSPPGE";
static const int VERSION = 2;
static bool active = false;
static bool nextFrame = false;
static bool writePending = false;
static std::function<void(const std::string &)> writeCallback;
enum class CommandType : u8 {
INIT = 0,
REGISTERS = 1,
VERTICES = 2,
INDICES = 3,
CLUT = 4,
TRANSFERSRC = 5,
MEMSET = 6,
MEMCPYDEST = 7,
MEMCPYDATA = 8,
DISPLAY = 9,
TEXTURE0 = 0x10,
TEXTURE1 = 0x11,
TEXTURE2 = 0x12,
TEXTURE3 = 0x13,
TEXTURE4 = 0x14,
TEXTURE5 = 0x15,
TEXTURE6 = 0x16,
TEXTURE7 = 0x17,
};
#pragma pack(push, 1)
struct Command {
CommandType type;
u32 sz;
u32 ptr;
};
#pragma pack(pop)
static std::vector<u8> pushbuf;
static std::vector<Command> commands;
static std::vector<u32> lastRegisters;
static std::vector<u32> lastTextures;
// TODO: Maybe move execute to another file?
class DumpExecute {
public:
~DumpExecute();
bool Run();
private:
void SyncStall();
bool SubmitCmds(void *p, u32 sz);
void SubmitListEnd();
void Init(u32 ptr, u32 sz);
void Registers(u32 ptr, u32 sz);
void Vertices(u32 ptr, u32 sz);
void Indices(u32 ptr, u32 sz);
void Clut(u32 ptr, u32 sz);
void TransferSrc(u32 ptr, u32 sz);
void Memset(u32 ptr, u32 sz);
void MemcpyDest(u32 ptr, u32 sz);
void Memcpy(u32 ptr, u32 sz);
void Texture(int level, u32 ptr, u32 sz);
void Display(u32 ptr, u32 sz);
u32 execMemcpyDest = 0;
u32 execListBuf = 0;
u32 execListPos = 0;
u32 execListID = 0;
const int LIST_BUF_SIZE = 256 * 1024;
std::vector<u32> execListQueue;
u16 lastBufw_[8]{};
};
// This class maps pushbuffer (dump data) sections to PSP memory.
// Dumps can be larger than available PSP memory, because they include generated data too.
//
// If possible, it maps to dynamically allocated "slabs" so nearby access is fast.
// Otherwise it uses "extra" allocations to manage sections that straddle two slabs.
// Slabs are managed with LRU, extra buffers are round-robin.
class BufMapping {
public:
// Returns a pointer to contiguous memory for this access, or else 0 (failure).
u32 Map(u32 bufpos, u32 sz, const std::function<void()> &flush);
// Clear and reset allocations made.
void Reset() {
slabGeneration_ = 0;
extraOffset_ = 0;
for (int i = 0; i < SLAB_COUNT; ++i) {
slabs_[i].Free();
}
for (int i = 0; i < EXTRA_COUNT; ++i) {
extra_[i].Free();
}
}
protected:
u32 MapSlab(u32 bufpos, const std::function<void()> &flush);
u32 MapExtra(u32 bufpos, u32 sz, const std::function<void()> &flush);
enum {
// These numbers kept low because we only have 24 MB of user memory to map into.
SLAB_SIZE = 1 * 1024 * 1024,
// 10 is the number of texture units + verts + inds.
// In the worst case, we could concurrently need 10 slabs/extras at the same time.
SLAB_COUNT = 10,
EXTRA_COUNT = 10,
};
// The current "generation". Static simply as a convenience for access.
// This increments on every allocation, for a simple LRU.
static int slabGeneration_;
// An aligned large mapping of the pushbuffer in PSP RAM.
struct SlabInfo {
u32 psp_pointer_;
u32 buf_pointer_;
int last_used_;
bool Matches(u32 bufpos) {
// We check psp_pointer_ because bufpos = 0 is valid, and the initial value.
return buf_pointer_ == bufpos && psp_pointer_ != 0;
}
// Automatically marks used for LRU purposes.
u32 Ptr(u32 bufpos) {
last_used_ = slabGeneration_;
return psp_pointer_ + (bufpos - buf_pointer_);
}
int Age() const {
// If not allocated, it's as expired as it's gonna get.
if (psp_pointer_ == 0)
return std::numeric_limits<int>::max();
return slabGeneration_ - last_used_;
}
bool Alloc();
void Free();
bool Setup(u32 bufpos);
};
// An adhoc mapping of the pushbuffer (either larger than a slab or straddling slabs.)
// Remember: texture data, verts, etc. must be contiguous.
struct ExtraInfo {
u32 psp_pointer_;
u32 buf_pointer_;
u32 size_;
bool Matches(u32 bufpos, u32 sz) {
// We check psp_pointer_ because bufpos = 0 is valid, and the initial value.
return buf_pointer_ == bufpos && psp_pointer_ != 0 && size_ >= sz;
}
u32 Ptr() {
return psp_pointer_;
}
bool Alloc(u32 bufpos, u32 sz);
void Free();
};
SlabInfo slabs_[SLAB_COUNT];
u32 extraOffset_ = 0;
ExtraInfo extra_[EXTRA_COUNT];
};
static BufMapping execMapping;
u32 BufMapping::Map(u32 bufpos, u32 sz, const std::function<void()> &flush) {
int slab1 = bufpos / SLAB_SIZE;
int slab2 = (bufpos + sz - 1) / SLAB_SIZE;
if (slab1 == slab2) {
// Doesn't straddle, so we can just map to a slab.
return MapSlab(bufpos, flush);
} else {
// We need contiguous, so we'll just allocate separately.
return MapExtra(bufpos, sz, flush);
}
}
u32 BufMapping::MapSlab(u32 bufpos, const std::function<void()> &flush) {
u32 slab_pos = (bufpos / SLAB_SIZE) * SLAB_SIZE;
int best = 0;
for (int i = 0; i < SLAB_COUNT; ++i) {
if (slabs_[i].Matches(slab_pos)) {
return slabs_[i].Ptr(bufpos);
}
if (slabs_[i].Age() > slabs_[best].Age()) {
best = i;
}
}
// Stall before mapping a new slab.
flush();
// Okay, we need to allocate.
if (!slabs_[best].Setup(slab_pos)) {
return 0;
}
return slabs_[best].Ptr(bufpos);
}
u32 BufMapping::MapExtra(u32 bufpos, u32 sz, const std::function<void()> &flush) {
for (int i = 0; i < EXTRA_COUNT; ++i) {
// Might be likely to reuse larger buffers straddling slabs.
if (extra_[i].Matches(bufpos, sz)) {
return extra_[i].Ptr();
}
}
// Stall first, so we don't stomp existing RAM.
flush();
int i = extraOffset_;
extraOffset_ = (extraOffset_ + 1) % EXTRA_COUNT;
if (!extra_[i].Alloc(bufpos, sz)) {
// Let's try to power on - hopefully none of these are still in use.
for (int i = 0; i < EXTRA_COUNT; ++i) {
extra_[i].Free();
}
if (!extra_[i].Alloc(bufpos, sz)) {
return 0;
}
}
return extra_[i].Ptr();
}
bool BufMapping::SlabInfo::Alloc() {
u32 sz = SLAB_SIZE;
psp_pointer_ = userMemory.Alloc(sz, false, "Slab");
if (psp_pointer_ == -1) {
psp_pointer_ = 0;
}
return psp_pointer_ != 0;
}
void BufMapping::SlabInfo::Free() {
if (psp_pointer_) {
userMemory.Free(psp_pointer_);
psp_pointer_ = 0;
buf_pointer_ = 0;
last_used_ = 0;
}
}
bool BufMapping::ExtraInfo::Alloc(u32 bufpos, u32 sz) {
// Make sure we've freed any previous allocation first.
Free();
u32 allocSize = sz;
psp_pointer_ = userMemory.Alloc(allocSize, false, "Straddle extra");
if (psp_pointer_ == -1) {
psp_pointer_ = 0;
}
if (psp_pointer_ == 0) {
return false;
}
buf_pointer_ = bufpos;
size_ = sz;
Memory::MemcpyUnchecked(psp_pointer_, pushbuf.data() + bufpos, sz);
return true;
}
void BufMapping::ExtraInfo::Free() {
if (psp_pointer_) {
userMemory.Free(psp_pointer_);
psp_pointer_ = 0;
buf_pointer_ = 0;
}
}
bool BufMapping::SlabInfo::Setup(u32 bufpos) {
// If it already has RAM, we're simply taking it over. Slabs come only in one size.
if (psp_pointer_ == 0) {
if (!Alloc()) {
return false;
}
}
buf_pointer_ = bufpos;
u32 sz = std::min((u32)SLAB_SIZE, (u32)pushbuf.size() - bufpos);
Memory::MemcpyUnchecked(psp_pointer_, pushbuf.data() + bufpos, sz);
slabGeneration_++;
last_used_ = slabGeneration_;
return true;
}
int BufMapping::slabGeneration_ = 0;
static void FlushRegisters() {
if (!lastRegisters.empty()) {
Command last{CommandType::REGISTERS};
last.ptr = (u32)pushbuf.size();
last.sz = (u32)(lastRegisters.size() * sizeof(u32));
pushbuf.resize(pushbuf.size() + last.sz);
memcpy(pushbuf.data() + last.ptr, lastRegisters.data(), last.sz);
lastRegisters.clear();
commands.push_back(last);
}
}
static std::string GenRecordingFilename() {
const std::string dumpDir = GetSysDirectory(DIRECTORY_DUMP);
const std::string prefix = dumpDir + "/" + g_paramSFO.GetDiscID();
File::CreateFullPath(dumpDir);
for (int n = 1; n < 10000; ++n) {
std::string filename = StringFromFormat("%s_%04d.ppdmp", prefix.c_str(), n);
if (!File::Exists(filename)) {
return filename;
}
}
return StringFromFormat("%s_%04d.ppdmp", prefix.c_str(), 9999);
}
static void EmitDisplayBuf() {
struct DisplayBufData {
PSPPointer<u8> topaddr;
u32 linesize, pixelFormat;
};
DisplayBufData disp{};
__DisplayGetFramebuf(&disp.topaddr, &disp.linesize, &disp.pixelFormat, 0);
u32 ptr = (u32)pushbuf.size();
u32 sz = (u32)sizeof(disp);
pushbuf.resize(pushbuf.size() + sz);
memcpy(pushbuf.data() + ptr, &disp, sz);
commands.push_back({CommandType::DISPLAY, sz, ptr});
}
static void BeginRecording() {
u32 ptr = (u32)pushbuf.size();
u32 sz = 512 * 4;
pushbuf.resize(pushbuf.size() + sz);
gstate.Save((u32_le *)(pushbuf.data() + ptr));
commands.push_back({CommandType::INIT, sz, ptr});
}
static void WriteCompressed(FILE *fp, const void *p, size_t sz) {
size_t compressed_size = snappy_max_compressed_length(sz);
u8 *compressed = new u8[compressed_size];
snappy_compress((const char *)p, sz, (char *)compressed, &compressed_size);
u32 write_size = (u32)compressed_size;
fwrite(&write_size, sizeof(write_size), 1, fp);
fwrite(compressed, compressed_size, 1, fp);
delete [] compressed;
}
static std::string WriteRecording() {
FlushRegisters();
EmitDisplayBuf();
const std::string filename = GenRecordingFilename();
NOTICE_LOG(G3D, "Recording filename: %s", filename.c_str());
FILE *fp = File::OpenCFile(filename, "wb");
fwrite(HEADER, 8, 1, fp);
fwrite(&VERSION, sizeof(VERSION), 1, fp);
u32 sz = (u32)commands.size();
fwrite(&sz, sizeof(sz), 1, fp);
u32 bufsz = (u32)pushbuf.size();
fwrite(&bufsz, sizeof(bufsz), 1, fp);
WriteCompressed(fp, commands.data(), commands.size() * sizeof(Command));
WriteCompressed(fp, pushbuf.data(), bufsz);
fclose(fp);
return filename;
}
static void GetVertDataSizes(int vcount, const void *indices, u32 &vbytes, u32 &ibytes) {
VertexDecoder vdec;
VertexDecoderOptions opts{};
vdec.SetVertexType(gstate.vertType, opts);
if (indices) {
u16 lower = 0;
u16 upper = 0;
GetIndexBounds(indices, vcount, gstate.vertType, &lower, &upper);
vbytes = (upper + 1) * vdec.VertexSize();
u32 idx = gstate.vertType & GE_VTYPE_IDX_MASK;
if (idx == GE_VTYPE_IDX_8BIT) {
ibytes = vcount * sizeof(u8);
} else if (idx == GE_VTYPE_IDX_16BIT) {
ibytes = vcount * sizeof(u16);
} else if (idx == GE_VTYPE_IDX_32BIT) {
ibytes = vcount * sizeof(u32);
}
} else {
vbytes = vcount * vdec.VertexSize();
}
}
static const u8 *mymemmem(const u8 *haystack, size_t hlen, const u8 *needle, size_t nlen) {
if (!nlen) {
return nullptr;
}
const u8 *last_possible = haystack + hlen - nlen;
int first = *needle;
const u8 *p = haystack;
while (p <= last_possible) {
p = (const u8 *)memchr(p, first, last_possible - p + 1);
if (!p) {
return nullptr;
}
if (!memcmp(p, needle, nlen)) {
return p;
}
p++;
}
return nullptr;
}
static Command EmitCommandWithRAM(CommandType t, const void *p, u32 sz) {
FlushRegisters();
Command cmd{t, sz, 0};
if (sz) {
// If at all possible, try to find it already in the buffer.
const u8 *prev = nullptr;
const size_t NEAR_WINDOW = std::max((int)sz * 2, 1024 * 10);
// Let's try nearby first... it will often be nearby.
if (pushbuf.size() > NEAR_WINDOW) {
prev = mymemmem(pushbuf.data() + pushbuf.size() - NEAR_WINDOW, NEAR_WINDOW, (const u8 *)p, sz);
}
if (!prev) {
prev = mymemmem(pushbuf.data(), pushbuf.size(), (const u8 *)p, sz);
}
if (prev) {
cmd.ptr = (u32)(prev - pushbuf.data());
} else {
cmd.ptr = (u32)pushbuf.size();
int pad = 0;
if (cmd.ptr & 0xF) {
pad = 0x10 - (cmd.ptr & 0xF);
cmd.ptr += pad;
}
pushbuf.resize(pushbuf.size() + sz + pad);
if (pad) {
memset(pushbuf.data() + cmd.ptr - pad, 0, pad);
}
memcpy(pushbuf.data() + cmd.ptr, p, sz);
}
}
commands.push_back(cmd);
return cmd;
}
static void EmitTextureData(int level, u32 texaddr) {
GETextureFormat format = gstate.getTextureFormat();
int w = gstate.getTextureWidth(level);
int h = gstate.getTextureHeight(level);
int bufw = GetTextureBufw(level, texaddr, format);
int extraw = w > bufw ? w - bufw : 0;
u32 sizeInRAM = (textureBitsPerPixel[format] * (bufw * h + extraw)) / 8;
u32 bytes = Memory::ValidSize(texaddr, sizeInRAM);
if (Memory::IsValidAddress(texaddr)) {
FlushRegisters();
CommandType type = CommandType((int)CommandType::TEXTURE0 + level);
const u8 *p = Memory::GetPointerUnchecked(texaddr);
// Dumps are huge - let's try to find this already emitted.
for (u32 prevptr : lastTextures) {
if (pushbuf.size() < prevptr + bytes) {
continue;
}
if (memcmp(pushbuf.data() + prevptr, p, bytes) == 0) {
commands.push_back({type, bytes, prevptr});
// Okay, that was easy. Bail out.
return;
}
}
// Not there, gotta emit anew.
Command cmd = EmitCommandWithRAM(type, p, bytes);
lastTextures.push_back(cmd.ptr);
}
}
static void FlushPrimState(int vcount) {
// TODO: Eventually, how do we handle texturing from framebuf/zbuf?
// TODO: Do we need to preload color/depth/stencil (in case from last frame)?
// We re-flush textures always in case the game changed them... kinda expensive.
// TODO: Dirty textures on transfer/stall/etc. somehow?
// TODO: Or maybe de-dup by validating if it has changed?
for (int level = 0; level < 8; ++level) {
u32 texaddr = gstate.getTextureAddress(level);
if (texaddr) {
EmitTextureData(level, texaddr);
}
}
const void *verts = Memory::GetPointer(gstate_c.vertexAddr);
const void *indices = nullptr;
if ((gstate.vertType & GE_VTYPE_IDX_MASK) != GE_VTYPE_IDX_NONE) {
indices = Memory::GetPointer(gstate_c.indexAddr);
}
u32 ibytes = 0;
u32 vbytes = 0;
GetVertDataSizes(vcount, indices, vbytes, ibytes);
if (indices) {
EmitCommandWithRAM(CommandType::INDICES, indices, ibytes);
}
if (verts) {
EmitCommandWithRAM(CommandType::VERTICES, verts, vbytes);
}
}
static void EmitTransfer(u32 op) {
FlushRegisters();
// This may not make a lot of sense right now, unless it's to a framebuf...
if (!Memory::IsVRAMAddress(gstate.getTransferDstAddress())) {
// Skip, not VRAM, so can't affect drawing (we flush textures each prim.)
return;
}
u32 srcBasePtr = gstate.getTransferSrcAddress();
u32 srcStride = gstate.getTransferSrcStride();
int srcX = gstate.getTransferSrcX();
int srcY = gstate.getTransferSrcY();
int width = gstate.getTransferWidth();
int height = gstate.getTransferHeight();
int bpp = gstate.getTransferBpp();
u32 srcBytes = ((srcY + height - 1) * srcStride + (srcX + width)) * bpp;
srcBytes = Memory::ValidSize(srcBasePtr, srcBytes);
EmitCommandWithRAM(CommandType::TRANSFERSRC, Memory::GetPointerUnchecked(srcBasePtr), srcBytes);
lastRegisters.push_back(op);
}
static void EmitClut(u32 op) {
u32 addr = gstate.getClutAddress();
u32 bytes = (op & 0x3F) * 32;
bytes = Memory::ValidSize(addr, bytes);
EmitCommandWithRAM(CommandType::CLUT, Memory::GetPointerUnchecked(addr), bytes);
lastRegisters.push_back(op);
}
static void EmitPrim(u32 op) {
FlushPrimState(op & 0x0000FFFF);
lastRegisters.push_back(op);
}
static void EmitBezierSpline(u32 op) {
int ucount = op & 0xFF;
int vcount = (op >> 8) & 0xFF;
FlushPrimState(ucount * vcount);
lastRegisters.push_back(op);
}
bool IsActive() {
return active;
}
bool IsActivePending() {
return nextFrame || active;
}
bool Activate() {
if (!nextFrame) {
nextFrame = true;
return true;
}
return false;
}
void SetCallback(const std::function<void(const std::string &)> callback) {
writeCallback = callback;
}
static void FinishRecording() {
// We're done - this was just to write the result out.
std::string filename = WriteRecording();
commands.clear();
pushbuf.clear();
NOTICE_LOG(SYSTEM, "Recording finished");
writePending = false;
active = false;
if (writeCallback)
writeCallback(filename);
writeCallback = nullptr;
}
void NotifyCommand(u32 pc) {
if (!active) {
return;
}
if (writePending) {
FinishRecording();
return;
}
const u32 op = Memory::Read_U32(pc);
const GECommand cmd = GECommand(op >> 24);
switch (cmd) {
case GE_CMD_VADDR:
case GE_CMD_IADDR:
case GE_CMD_JUMP:
case GE_CMD_CALL:
case GE_CMD_RET:
case GE_CMD_END:
case GE_CMD_SIGNAL:
case GE_CMD_FINISH:
case GE_CMD_BASE:
case GE_CMD_OFFSETADDR:
case GE_CMD_ORIGIN:
// These just prepare future commands, and are flushed with those commands.
// TODO: Maybe add a command just to log that these were hit?
break;
case GE_CMD_BOUNDINGBOX:
case GE_CMD_BJUMP:
// Since we record each command, this is theoretically not relevant.
// TODO: Output a CommandType to validate this.
break;
case GE_CMD_PRIM:
EmitPrim(op);
break;
case GE_CMD_BEZIER:
case GE_CMD_SPLINE:
EmitBezierSpline(op);
break;
case GE_CMD_LOADCLUT:
EmitClut(op);
break;
case GE_CMD_TRANSFERSTART:
EmitTransfer(op);
break;
default:
lastRegisters.push_back(op);
break;
}
}
void NotifyMemcpy(u32 dest, u32 src, u32 sz) {
if (!active) {
return;
}
if (Memory::IsVRAMAddress(dest)) {
FlushRegisters();
Command cmd{CommandType::MEMCPYDEST, sizeof(dest), (u32)pushbuf.size()};
pushbuf.resize(pushbuf.size() + sizeof(dest));
memcpy(pushbuf.data() + cmd.ptr, &dest, sizeof(dest));
sz = Memory::ValidSize(dest, sz);
EmitCommandWithRAM(CommandType::MEMCPYDATA, Memory::GetPointer(dest), sz);
}
}
void NotifyMemset(u32 dest, int v, u32 sz) {
if (!active) {
return;
}
struct MemsetCommand {
u32 dest;
int value;
u32 sz;
};
if (Memory::IsVRAMAddress(dest)) {
sz = Memory::ValidSize(dest, sz);
MemsetCommand data{dest, v, sz};
FlushRegisters();
Command cmd{CommandType::MEMSET, sizeof(data), (u32)pushbuf.size()};
pushbuf.resize(pushbuf.size() + sizeof(data));
memcpy(pushbuf.data() + cmd.ptr, &data, sizeof(data));
}
}
void NotifyUpload(u32 dest, u32 sz) {
if (!active) {
return;
}
NotifyMemcpy(dest, dest, sz);
}
void NotifyFrame() {
if (active && !writePending && !commands.empty()) {
// Delay write until the first command of the next frame, so we get the right display buf.
NOTICE_LOG(SYSTEM, "Recording complete - waiting to get display buffer");
writePending = true;
}
if (nextFrame && (gstate_c.skipDrawReason & SKIPDRAW_SKIPFRAME) == 0) {
NOTICE_LOG(SYSTEM, "Recording starting...");
active = true;
nextFrame = false;
lastTextures.clear();
BeginRecording();
}
}
void DumpExecute::SyncStall() {
gpu->UpdateStall(execListID, execListPos);
s64 listTicks = gpu->GetListTicks(execListID);
if (listTicks != -1) {
currentMIPS->downcount -= listTicks - CoreTiming::GetTicks();
}
// Make sure downcount doesn't overflow.
CoreTiming::ForceCheck();
}
bool DumpExecute::SubmitCmds(void *p, u32 sz) {
if (execListBuf == 0) {
u32 allocSize = LIST_BUF_SIZE;
execListBuf = userMemory.Alloc(allocSize, "List buf");
if (execListBuf == -1) {
execListBuf = 0;
}
if (execListBuf == 0) {
ERROR_LOG(SYSTEM, "Unable to allocate for display list");
return false;
}
execListPos = execListBuf;
Memory::Write_U32(GE_CMD_NOP << 24, execListPos);
execListPos += 4;
gpu->EnableInterrupts(false);
auto optParam = PSPPointer<PspGeListArgs>::Create(0);
execListID = gpu->EnqueueList(execListBuf, execListPos, -1, optParam, false);
gpu->EnableInterrupts(true);
}
u32 pendingSize = (int)execListQueue.size() * sizeof(u32);
// Validate space for jump.
u32 allocSize = pendingSize + sz + 8;
if (execListPos + allocSize >= execListBuf + LIST_BUF_SIZE) {
Memory::Write_U32((GE_CMD_BASE << 24) | ((execListBuf >> 8) & 0x00FF0000), execListPos);
Memory::Write_U32((GE_CMD_JUMP << 24) | (execListBuf & 0x00FFFFFF), execListPos + 4);
execListPos = execListBuf;
// Don't continue until we've stalled.
SyncStall();
}
Memory::MemcpyUnchecked(execListPos, execListQueue.data(), pendingSize);
execListPos += pendingSize;
u32 writePos = execListPos;
Memory::MemcpyUnchecked(execListPos, p, sz);
execListPos += sz;
// TODO: Unfortunate. Maybe Texture commands should contain the bufw instead.
// The goal here is to realistically combine prims in dumps. Stalling for the bufw flushes.
u32_le *ops = (u32_le *)Memory::GetPointer(writePos);
for (u32 i = 0; i < sz / 4; ++i) {
u32 cmd = ops[i] >> 24;
if (cmd >= GE_CMD_TEXBUFWIDTH0 && cmd <= GE_CMD_TEXBUFWIDTH7) {
int level = cmd - GE_CMD_TEXBUFWIDTH0;
u16 bufw = ops[i] & 0xFFFF;
// NOP the address part of the command to avoid a flush too.
if (bufw == lastBufw_[level])
ops[i] = GE_CMD_NOP << 24;
else
ops[i] = (gstate.texbufwidth[level] & 0xFFFF0000) | bufw;
lastBufw_[level] = bufw;
}
// Since we're here anyway, also NOP out texture addresses.
// This makes Step Tex not hit phantom textures.
if (cmd >= GE_CMD_TEXADDR0 && cmd <= GE_CMD_TEXADDR7) {
ops[i] = GE_CMD_NOP << 24;
}
}
execListQueue.clear();
return true;
}
void DumpExecute::SubmitListEnd() {
if (execListPos == 0) {
return;
}
// There's always space for the end, same size as a jump.
Memory::Write_U32(GE_CMD_FINISH << 24, execListPos);
Memory::Write_U32(GE_CMD_END << 24, execListPos + 4);
execListPos += 8;
SyncStall();
gpu->ListSync(execListID, 0);
}
void DumpExecute::Init(u32 ptr, u32 sz) {
gstate.Restore((u32_le *)(pushbuf.data() + ptr));
gpu->ReapplyGfxState();
}
void DumpExecute::Registers(u32 ptr, u32 sz) {
SubmitCmds(pushbuf.data() + ptr, sz);
}
void DumpExecute::Vertices(u32 ptr, u32 sz) {
u32 psp = execMapping.Map(ptr, sz, std::bind(&DumpExecute::SyncStall, this));
if (psp == 0) {
ERROR_LOG(SYSTEM, "Unable to allocate for vertices");
return;
}
execListQueue.push_back((GE_CMD_BASE << 24) | ((psp >> 8) & 0x00FF0000));
execListQueue.push_back((GE_CMD_VADDR << 24) | (psp & 0x00FFFFFF));
}
void DumpExecute::Indices(u32 ptr, u32 sz) {
u32 psp = execMapping.Map(ptr, sz, std::bind(&DumpExecute::SyncStall, this));
if (psp == 0) {
ERROR_LOG(SYSTEM, "Unable to allocate for indices");
return;
}
execListQueue.push_back((GE_CMD_BASE << 24) | ((psp >> 8) & 0x00FF0000));
execListQueue.push_back((GE_CMD_IADDR << 24) | (psp & 0x00FFFFFF));
}
void DumpExecute::Clut(u32 ptr, u32 sz) {
u32 psp = execMapping.Map(ptr, sz, std::bind(&DumpExecute::SyncStall, this));
if (psp == 0) {
ERROR_LOG(SYSTEM, "Unable to allocate for clut");
return;
}
execListQueue.push_back((GE_CMD_CLUTADDRUPPER << 24) | ((psp >> 8) & 0x00FF0000));
execListQueue.push_back((GE_CMD_CLUTADDR << 24) | (psp & 0x00FFFFFF));
}
void DumpExecute::TransferSrc(u32 ptr, u32 sz) {
u32 psp = execMapping.Map(ptr, sz, std::bind(&DumpExecute::SyncStall, this));
if (psp == 0) {
ERROR_LOG(SYSTEM, "Unable to allocate for transfer");
return;
}
// Need to sync in order to access gstate.transfersrcw.
SyncStall();
execListQueue.push_back((gstate.transfersrcw & 0xFF00FFFF) | ((psp >> 8) & 0x00FF0000));
execListQueue.push_back(((GE_CMD_TRANSFERSRC) << 24) | (psp & 0x00FFFFFF));
}
void DumpExecute::Memset(u32 ptr, u32 sz) {
struct MemsetCommand {
u32 dest;
int value;
u32 sz;
};
const MemsetCommand *data = (const MemsetCommand *)(pushbuf.data() + ptr);
if (Memory::IsVRAMAddress(data->dest)) {
SyncStall();
gpu->PerformMemorySet(data->dest, (u8)data->value, data->sz);
}
}
void DumpExecute::MemcpyDest(u32 ptr, u32 sz) {
execMemcpyDest = *(const u32 *)(pushbuf.data() + ptr);
}
void DumpExecute::Memcpy(u32 ptr, u32 sz) {
if (Memory::IsVRAMAddress(execMemcpyDest)) {
SyncStall();
Memory::MemcpyUnchecked(execMemcpyDest, pushbuf.data() + ptr, sz);
gpu->PerformMemoryUpload(execMemcpyDest, sz);
}
}
void DumpExecute::Texture(int level, u32 ptr, u32 sz) {
u32 psp = execMapping.Map(ptr, sz, std::bind(&DumpExecute::SyncStall, this));
if (psp == 0) {
ERROR_LOG(SYSTEM, "Unable to allocate for texture");
return;
}
u32 bufwCmd = GE_CMD_TEXBUFWIDTH0 + level;
u32 addrCmd = GE_CMD_TEXADDR0 + level;
execListQueue.push_back((bufwCmd << 24) | ((psp >> 8) & 0x00FF0000) | lastBufw_[level]);
execListQueue.push_back((addrCmd << 24) | (psp & 0x00FFFFFF));
}
void DumpExecute::Display(u32 ptr, u32 sz) {
struct DisplayBufData {
PSPPointer<u8> topaddr;
u32 linesize, pixelFormat;
};
DisplayBufData *disp = (DisplayBufData *)(pushbuf.data() + ptr);
// Sync up drawing.
SyncStall();
__DisplaySetFramebuf(disp->topaddr.ptr, disp->linesize, disp->pixelFormat, 1);
__DisplaySetFramebuf(disp->topaddr.ptr, disp->linesize, disp->pixelFormat, 0);
}
DumpExecute::~DumpExecute() {
execMemcpyDest = 0;
if (execListBuf) {
userMemory.Free(execListBuf);
execListBuf = 0;
}