Repository navigation
Expand file tree
/
Copy pathState.cpp
More file actions
922 lines (878 loc) · 56.5 KB
/
Copy pathState.cpp
File metadata and controls
922 lines (878 loc) · 56.5 KB
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
#include <cstdio>
#include "prx/libSceAgcDriver/Graphics/include/State.hpp"
#include "prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp"
#include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp"
#include "prx/libSceAgcDriver/Graphics/include/DccMetadata.hpp"
#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
#include "prx/libc/include/General.hpp"
#include <algorithm>
#include <bit>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <sstream>
namespace AgcDriver::Graphics {
namespace {
std::uint32_t read(const Registers& registers, std::uint32_t offset, RegisterBank bank = RegisterBank::Context) {
NoteRegisterRead(bank, offset);
const auto it = registers.find(offset);
if (it == registers.end()) {
std::ostringstream message;
message << "missing register in " << RegisterBankName(bank) << " bank at DWORD 0x" << std::hex << offset << " (" << std::dec << offset << ')';
throw std::runtime_error("AGC graphics: " + message.str());
}
return it->second;
}
// A register that may be absent (the decoders' find/contains), through the facade.
Registers::const_iterator find(const Registers& registers, std::uint32_t offset, RegisterBank bank = RegisterBank::Context) {
NoteRegisterRead(bank, offset);
return registers.find(offset);
}
float readFloat(const Registers& registers, std::uint32_t offset) {
const auto value = std::bit_cast<float>(read(registers, offset));
if (!std::isfinite(value)) Require(false, "non-finite register at DWORD " + std::to_string(offset));
return value;
}
std::string zeroMessage(std::uint32_t offset, std::uint32_t value, const char* name) {
char detail[64];
std::snprintf(detail, sizeof(detail), " (register 0x%x = 0x%08x)", offset, value);
return std::string(name) + " is unsupported" + detail;
}
void zero(const Registers& registers, std::uint32_t offset, std::uint32_t mask, const char* name, RegisterBank bank = RegisterBank::Context) {
const auto value = read(registers, offset, bank);
if ((value & mask) != 0) throw std::runtime_error(zeroMessage(offset, value, name));
}
std::string vteMessage(std::uint32_t viewportControl) {
std::ostringstream message;
message << "AGC graphics: PA_CL_VTE_CNTL=0x" << std::hex << viewportControl << ": expected 0x43f for homogeneous positions and all viewport transforms; pre-divided coordinates, reciprocal W or disabled transforms are unsupported";
return message.str();
}
std::string conservativeMessage(std::uint32_t control) {
std::ostringstream message;
message << "AGC graphics: PA_SC_CONSERVATIVE_RASTERIZATION_CNTL=0x" << std::hex << control << ": only 0x6000 (off) and 0x6001 (overestimation) are supported";
return message.str();
}
void requireConservativeTriangles(bool triangles, const char* primitive, const char* source, std::uint32_t value) {
if (triangles) return;
std::ostringstream message;
message << "AGC graphics: conservative rasterization of " << primitive << " is unsupported (" << source << "=0x" << std::hex << value << "): only triangles are overestimated";
throw std::runtime_error(message.str());
}
VkConservativeRasterizationModeEXT decodeConservativeRasterization(const QueueState& queue) {
const auto& cx = queue.context;
const auto control = read(cx, 0x313);
if (control == 0x6000u) return VK_CONSERVATIVE_RASTERIZATION_MODE_DISABLED_EXT;
if (control != 0x6001u) throw std::runtime_error(conservativeMessage(control));
const auto stages = read(cx, 0x2d5);
if ((stages & 0x20u) != 0) {
const auto output = read(cx, 0x29b);
const auto type = output & 0x3fu;
const bool perStream = (output & 0x80000000u) != 0;
requireConservativeTriangles(!perStream && type == 2u, perStream ? "per-stream primitive types" : type == 0u ? "points" : type == 1u ? "lines" : "other primitive types", "VGT_GS_OUT_PRIM_TYPE", output);
} else if ((stages & 4u) != 0) {
const auto parameters = read(cx, 0x2db);
const auto domain = parameters & 3u;
const auto topology = (parameters >> 5u) & 7u;
requireConservativeTriangles((domain == 1u || domain == 2u) && (topology == 2u || topology == 3u), topology == 0u ? "points" : topology == 1u || domain == 0u ? "lines" : "other primitive types", "VGT_TF_PARAM", parameters);
} else {
const auto primitive = read(queue.userConfig, 0x242, RegisterBank::UserConfig);
requireConservativeTriangles(primitive == 4u || primitive == 5u || primitive == 6u, primitive == 1u ? "points" : primitive == 2u || primitive == 3u ? "lines" : primitive == 7u || primitive == 17u ? "rectangles" : "other primitive types", "VGT_PRIMITIVE_TYPE", primitive);
}
Require((read(cx, 0x1b3) & 0x44u) == 0, "conservative rasterization with centroid interpolation is unsupported");
return VK_CONSERVATIVE_RASTERIZATION_MODE_OVERESTIMATE_EXT;
}
// The register rules DecodeState and DrawRejection share (the precheck must reject exactly what
// DecodeState would): the masks whose set bits are unsupported, and the depth-control verdict.
// Render target index, viewport index and the misc export vector that carries them are accepted but
// not routed: color targets are single-layer, so layered draws land in layer 0.
constexpr std::uint32_t LayerExports = (1u << 18u) | (1u << 19u) | (1u << 21u) | (1u << 24u);
constexpr std::uint32_t DepthControlMask = ~0x007007f0u;
// EXEC_ON_HIER_FAIL / EXEC_ON_NOOP / EXEC_IF_OVERLAPPED (bits 9, 10, 17) only force the pixel shader
// to run, which it always does here.
constexpr std::uint32_t ShaderControlMask = ~(0x0000f870u | 0x00020600u | 0x00010000u);
constexpr std::uint32_t PixelStageRunsMask = 0x00020747u;
constexpr std::uint32_t AlphaToCoverageMask = ~0x0001ff00u;
constexpr std::uint32_t ScanModeMask = ~0x22u;
constexpr std::uint32_t ScanControlMask = ~0x06023fffu;
constexpr std::uint32_t ScreenOffsetMask = ~0x01ff01ffu;
// Bits 26/27 (ZCLIP_NEAR/FAR_DISABLE) become depth clamping; bit 19 selects the [0, 1] clip space.
constexpr std::uint32_t ClipControlMask = ~(0x80000u | 0x01000000u | 0x0c000000u);
bool zFormatSupported(std::uint32_t format) {
return format == 0u || format == 1u || format == 2u || format == 3u || format == 9u;
}
std::uint32_t shaderControlMask(std::uint32_t zFormat) {
constexpr std::uint32_t zExportEnable = 0x1u;
constexpr std::uint32_t maskExportEnable = 0x100u;
auto mask = ShaderControlMask;
if (zFormat != 0u && zFormatSupported(zFormat)) mask &= ~zExportEnable;
if (zFormat == 9u) mask &= ~maskExportEnable;
return mask;
}
// Debug aid: APS5_IGNORE_DEPTH_TEST=1 renders depth- and stencil-tested draws without a depth
// target as if their tests always passed (wrong occlusion, but the draws run), so stages that
// depth-test everything can exercise the draw paths before depth targets exist.
bool IgnoreDepthTest() {
static const bool ignore = std::getenv("APS5_IGNORE_DEPTH_TEST") != nullptr;
return ignore;
}
bool depthSurfaceBound(const Registers& cx) {
const auto z = find(cx, 0x010);
const auto stencil = find(cx, 0x011);
return (z != cx.end() && (z->second & 3u) != 0) || (stencil != cx.end() && (stencil->second & 1u) != 0);
}
bool depthPlanesAbsent(const Registers& cx) {
const auto z = find(cx, 0x010);
const auto stencil = find(cx, 0x011);
return z != cx.end() && stencil != cx.end() && (z->second & 3u) == 0 && (stencil->second & 1u) == 0;
}
VkStencilOpState stencilFace(std::uint32_t compare, std::uint32_t ops, std::uint32_t refMask, bool readOnly) {
VkStencilOpState face{};
face.compareOp = static_cast<VkCompareOp>(compare);
face.compareMask = (refMask >> 8u) & 0xffu;
face.writeMask = readOnly ? 0u : (refMask >> 16u) & 0xffu;
const auto test = refMask & 0xffu;
const auto opValue = refMask >> 24u;
auto reference = test;
auto fixed = compare == 0 || compare == 7 ? 0u : face.compareMask;
const auto convert = [&](std::uint32_t op) {
if (face.writeMask == 0) return VK_STENCIL_OP_KEEP;
char detail[96];
switch (op) {
case 0: return VK_STENCIL_OP_KEEP;
case 1: return VK_STENCIL_OP_ZERO;
case 2:
case 3:
case 4: {
const auto value = op == 2 ? 0xffu : op == 3 ? test : opValue;
if (((reference ^ value) & fixed & face.writeMask) != 0) {
std::snprintf(detail, sizeof(detail), "AGC graphics: stencil replacement 0x%02x against test value 0x%02x (masks 0x%02x/0x%02x) is unsupported", value, test, face.compareMask, face.writeMask);
throw std::runtime_error(detail);
}
reference = (reference & ~face.writeMask) | (value & face.writeMask);
fixed |= face.writeMask;
return VK_STENCIL_OP_REPLACE;
}
case 5:
case 6:
case 8:
case 9:
if (opValue != 1) {
std::snprintf(detail, sizeof(detail), "AGC graphics: stencil add/subtract of 0x%02x is unsupported", opValue);
throw std::runtime_error(detail);
}
return op == 5 ? VK_STENCIL_OP_INCREMENT_AND_CLAMP : op == 6 ? VK_STENCIL_OP_DECREMENT_AND_CLAMP : op == 8 ? VK_STENCIL_OP_INCREMENT_AND_WRAP : VK_STENCIL_OP_DECREMENT_AND_WRAP;
case 7: return VK_STENCIL_OP_INVERT;
default:
std::snprintf(detail, sizeof(detail), "AGC graphics: stencil operation %u is unsupported", op);
throw std::runtime_error(detail);
}
};
face.failOp = convert(ops & 0xfu);
face.passOp = convert((ops >> 4u) & 0xfu);
face.depthFailOp = convert((ops >> 8u) & 0xfu);
face.reference = reference;
return face;
}
void decodeDepth(const Registers& cx, std::uint32_t depthControl, State& result) {
zero(cx, 0x000, 0x00001f9fu, "depth/stencil clear, copy, resummarize or decompress draws (DB_RENDER_CONTROL)");
const auto view = read(cx, 0x002);
zero(cx, 0x002, 0x3c000000u, "depth mips (DB_DEPTH_VIEW MIP_LEVEL)");
zero(cx, 0x010, 0x000f100cu, "multisampled, partially resident or mipmapped depth (DB_Z_INFO)");
zero(cx, 0x011, 0x00001000u, "partially resident stencil (DB_STENCIL_INFO)");
const auto zFormat = read(cx, 0x010) & 3u;
const bool stencil = (read(cx, 0x011) & 1u) != 0;
Require(zFormat != 2, "Z_24 depth is unsupported");
if (zFormat == 0) depthControl &= ~6u;
if (!stencil) depthControl &= ~1u;
const auto base = [&](std::uint32_t low, std::uint32_t highOffset) {
const auto high = find(cx, highOffset);
return (high == cx.end() ? 0ull : static_cast<std::uint64_t>(high->second & 0xffu) << 40u) | (static_cast<std::uint64_t>(read(cx, low)) << 8u);
};
const bool depthReadOnly = (view & 0x01000000u) != 0;
const bool stencilReadOnly = (view & 0x02000000u) != 0;
DepthTarget depth{};
depth.address = zFormat != 0 ? base(0x012, 0x01a) : 0;
depth.stencilAddress = stencil ? base(0x013, 0x01b) : 0;
Require(zFormat == 0 || depthReadOnly || base(0x014, 0x01c) == depth.address, "depth read and written at different addresses is unsupported");
Require(!stencil || stencilReadOnly || base(0x015, 0x01d) == depth.stencilAddress, "stencil read and written at different addresses is unsupported");
const auto size = read(cx, 0x007);
depth.extent = {(size & 0x3fffu) + 1u, ((size >> 16u) & 0x3fffu) + 1u};
if (const auto slice = view & 0x1fffu; slice != 0) {
if (depth.address != 0) depth.address += static_cast<std::uint64_t>(slice) * DepthSliceBytes(depth.extent, zFormat == 1 ? 2u : 4u);
if (depth.stencilAddress != 0) depth.stencilAddress += static_cast<std::uint64_t>(slice) * DepthSliceBytes(depth.extent, 1u);
}
depth.format = zFormat == 1 ? (stencil ? VK_FORMAT_D16_UNORM_S8_UINT : VK_FORMAT_D16_UNORM) : (stencil ? VK_FORMAT_D32_SFLOAT_S8_UINT : VK_FORMAT_D32_SFLOAT);
depth.clearDepth = readFloat(cx, 0x00b);
depth.clearStencil = static_cast<std::uint8_t>(read(cx, 0x00a) & 0xffu);
result.depth = depth;
result.depthTest = (depthControl & 2u) != 0;
result.depthWrite = result.depthTest && (depthControl & 4u) != 0 && !depthReadOnly;
result.depthCompare = static_cast<VkCompareOp>((depthControl >> 4u) & 7u);
result.depthBoundsTest = (depthControl & 8u) != 0;
if (result.depthBoundsTest) {
Require(zFormat != 0, "depth bounds without a depth plane");
result.minDepthBounds = readFloat(cx, 0x008);
result.maxDepthBounds = readFloat(cx, 0x009);
}
result.stencilTest = (depthControl & 1u) != 0;
if (result.stencilTest) {
const auto ops = read(cx, 0x10b);
result.stencilFront = stencilFace((depthControl >> 8u) & 7u, ops, read(cx, 0x10c), stencilReadOnly);
result.stencilBack = (depthControl & 0x80u) != 0 ? stencilFace((depthControl >> 20u) & 7u, ops >> 12u, read(cx, 0x10d), stencilReadOnly) : result.stencilFront;
}
}
void decodeDepthBias(const Registers& cx, std::uint32_t raster, State& result) {
const bool front = (result.cullMode & VK_CULL_MODE_FRONT_BIT) == 0;
const bool back = (result.cullMode & VK_CULL_MODE_BACK_BIT) == 0;
const bool frontBias = (raster & 0x800u) != 0;
const bool backBias = (raster & 0x1000u) != 0;
if (!(front && frontBias) && !(back && backBias)) return;
if (front && back && (frontBias != backBias || read(cx, 0x2e0) != read(cx, 0x2e2) || read(cx, 0x2e1) != read(cx, 0x2e3))) throw std::runtime_error("AGC graphics: " + zeroMessage(0x205, raster, "depth bias differing between front and back faces"));
const bool d16 = result.depth->format == VK_FORMAT_D16_UNORM || result.depth->format == VK_FORMAT_D16_UNORM_S8_UINT;
const auto format = find(cx, 0x2de) == cx.end() ? (d16 ? 0xf0u : 0x1e9u) : read(cx, 0x2de);
if (format != (d16 ? 0xf0u : 0x1e9u)) throw std::runtime_error("AGC graphics: " + zeroMessage(0x2de, format, "depth bias in units other than the depth format"));
const auto scale = front && frontBias ? 0x2e0u : 0x2e2u;
result.depthBias = true;
result.depthBiasSlope = readFloat(cx, scale) / 16.0f;
result.depthBiasConstant = readFloat(cx, scale + 1u);
result.depthBiasClamp = readFloat(cx, 0x2df);
}
bool depthPassThrough(std::uint32_t depthControl) {
const bool stencil = (depthControl & 1u) != 0;
const bool depth = (depthControl & 2u) != 0;
const bool depthWrite = (depthControl & 4u) != 0;
const bool passThroughDepth = !depth || (!depthWrite && ((depthControl >> 4u) & 7u) == 7u);
const bool passThroughStencil = !stencil || (((depthControl >> 8u) & 7u) == 7u && ((depthControl & 0x80u) == 0 || ((depthControl >> 20u) & 7u) == 7u));
return (stencil || depth) && passThroughDepth && passThroughStencil;
}
std::uint32_t effectiveDepthControl(std::uint32_t depthControl) {
return (depthControl & 3u) == 0 ? depthControl & ~4u : depthControl;
}
bool colorControlSupported(std::uint32_t colorControl, bool hasColorTarget) {
colorControl &= ~1u;
return colorControl == 0xcc0010u || ((colorControl >> 4u) & 7u) == 0u || (!hasColorTarget && (colorControl & ~0x70u) == 0xcc0000u);
}
std::string colorControlMessage(std::uint32_t colorControl) {
static constexpr const char* modes[8] = {"disable", "normal", "eliminate fast clear", "resolve", "decompress", "FMASK decompress", "DCC decompress", "reserved"};
char text[160];
std::snprintf(text, sizeof(text), "AGC graphics: only normal color rendering with copy ROP is supported (CB_COLOR_CONTROL 0x%08x, mode %s)", colorControl, modes[(colorControl >> 4u) & 7u]);
return text;
}
VkBlendFactor blendFactor(std::uint32_t value) {
switch (value) {
case 0: return VK_BLEND_FACTOR_ZERO;
case 1: return VK_BLEND_FACTOR_ONE;
case 2: return VK_BLEND_FACTOR_SRC_COLOR;
case 3: return VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR;
case 4: return VK_BLEND_FACTOR_SRC_ALPHA;
case 5: return VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
case 6: return VK_BLEND_FACTOR_DST_ALPHA;
case 7: return VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA;
case 8: return VK_BLEND_FACTOR_DST_COLOR;
case 9: return VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR;
case 10: return VK_BLEND_FACTOR_SRC_ALPHA_SATURATE;
case 13: return VK_BLEND_FACTOR_CONSTANT_COLOR;
case 14: return VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR;
case 19: return VK_BLEND_FACTOR_CONSTANT_ALPHA;
case 20: return VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA;
default: throw std::runtime_error("AGC graphics: unsupported blend factor " + std::to_string(value));
}
}
VkBlendOp blendOp(std::uint32_t value) {
switch (value) {
case 0: return VK_BLEND_OP_ADD;
case 1: return VK_BLEND_OP_SUBTRACT;
case 2: return VK_BLEND_OP_MIN;
case 3: return VK_BLEND_OP_MAX;
case 4: return VK_BLEND_OP_REVERSE_SUBTRACT;
default: throw std::runtime_error("AGC graphics: unsupported blend operation " + std::to_string(value));
}
}
struct DecodedColorFormat {
VkFormat format;
std::uint32_t elementBytes;
std::uint8_t componentMapping = 0xe4u;
};
// CB_COLOR_INFO FORMAT / NUMBER_TYPE / COMP_SWAP to a Vulkan attachment format. AMD formats list
// components from the least significant bits, as Vulkan's non-packed formats do.
DecodedColorFormat DecodeColorFormat(std::uint32_t format, std::uint32_t number, std::uint32_t swap) {
constexpr std::uint32_t unorm = 0, snorm = 1, sint = 5, srgb = 6, floating = 7;
constexpr std::uint32_t uint = 4;
const auto fail = [&]() -> DecodedColorFormat {
throw std::runtime_error("AGC graphics: unsupported color format " + std::to_string(format) + " number type " + std::to_string(number) + " component swap " + std::to_string(swap));
};
const bool alternate = swap == 1;
const auto single = [&](VkFormat vkFormat, std::uint32_t bytes) { return DecodedColorFormat{vkFormat, bytes, static_cast<std::uint8_t>((0xe4u & ~3u) | swap)}; };
if (swap > 1 && format != 1 && format != 2 && format != 4 && format != 10 && format != 12) return fail();
switch (format) {
case 1:
if (number == unorm) return single(VK_FORMAT_R8_UNORM, 1);
if (number == snorm) return single(VK_FORMAT_R8_SNORM, 1);
if (number == uint) return single(VK_FORMAT_R8_UINT, 1);
if (number == srgb && swap == 0) return single(VK_FORMAT_R8_SRGB, 1);
return fail();
case 2:
if (number == unorm) return single(VK_FORMAT_R16_UNORM, 2);
if (number == snorm) return single(VK_FORMAT_R16_SNORM, 2);
if (number == floating) return single(VK_FORMAT_R16_SFLOAT, 2);
if (number == uint) return single(VK_FORMAT_R16_UINT, 2);
return fail();
case 3:
if (swap != 0) return fail();
if (number == unorm) return {VK_FORMAT_R8G8_UNORM, 2};
if (number == snorm) return {VK_FORMAT_R8G8_SNORM, 2};
return fail();
case 4:
if (number == floating) return single(VK_FORMAT_R32_SFLOAT, 4);
if (number == uint) return single(VK_FORMAT_R32_UINT, 4);
if (number == sint) return single(VK_FORMAT_R32_SINT, 4);
return fail();
case 5:
if (swap != 0) return fail();
if (number == floating) return {VK_FORMAT_R16G16_SFLOAT, 4};
if (number == unorm) return {VK_FORMAT_R16G16_UNORM, 4};
if (number == snorm) return {VK_FORMAT_R16G16_SNORM, 4};
if (number == uint) return {VK_FORMAT_R16G16_UINT, 4};
return fail();
case 6:
// COLOR_10_11_11: red in the low 11 bits, the Vulkan B10G11R11 packing.
if (swap != 0 || number != floating) return fail();
return {VK_FORMAT_B10G11R11_UFLOAT_PACK32, 4};
case 9:
// COLOR_2_10_10_10 keeps red in the low bits, the Vulkan A2B10G10R10 packing.
if (number != unorm) return fail();
return {alternate ? VK_FORMAT_A2R10G10B10_UNORM_PACK32 : VK_FORMAT_A2B10G10R10_UNORM_PACK32, 4};
case 10: {
const auto reversed = static_cast<std::uint8_t>(swap == 2 ? 0x1bu : swap == 3 ? 0x93u : 0xe4u);
if (number == unorm) return {alternate ? VK_FORMAT_B8G8R8A8_UNORM : VK_FORMAT_R8G8B8A8_UNORM, 4, reversed};
if (number == snorm) return {alternate ? VK_FORMAT_B8G8R8A8_SNORM : VK_FORMAT_R8G8B8A8_SNORM, 4, reversed};
if (number == srgb) return {alternate ? VK_FORMAT_B8G8R8A8_SRGB : VK_FORMAT_R8G8B8A8_SRGB, 4, reversed};
return fail();
}
case 11:
if (swap != 0) return fail();
if (number == floating) return {VK_FORMAT_R32G32_SFLOAT, 8};
if (number == uint) return {VK_FORMAT_R32G32_UINT, 8};
return fail();
case 12: {
if (swap == 1) return fail();
const auto reversed = static_cast<std::uint8_t>(swap == 2 ? 0x1bu : swap == 3 ? 0x93u : 0xe4u);
if (number == floating) return {VK_FORMAT_R16G16B16A16_SFLOAT, 8, reversed};
if (number == unorm) return {VK_FORMAT_R16G16B16A16_UNORM, 8, reversed};
if (number == snorm) return {VK_FORMAT_R16G16B16A16_SNORM, 8, reversed};
if (number == uint) return {VK_FORMAT_R16G16B16A16_UINT, 8, reversed};
return fail();
}
case 14:
if (swap != 0) return fail();
if (number == floating) return {VK_FORMAT_R32G32B32A32_SFLOAT, 16};
if (number == uint) return {VK_FORMAT_R32G32B32A32_UINT, 16};
return fail();
default:
return fail();
}
}
void intersect(VkRect2D& result, const Registers& registers, std::uint32_t offset, bool screen) {
const auto tl = read(registers, offset);
const auto br = read(registers, offset + 1);
if (!screen) Require((tl & 0x8000u) == 0 && (br & 0x80008000u) == 0, "scissor reserved bits are unsupported");
const auto x = tl & 0xffffu;
const auto y = (tl >> 16u) & (screen ? 0xffffu : 0x7fffu);
const auto right = br & 0xffffu;
const auto bottom = br >> 16u;
Require(x <= right && y <= bottom, "inverted scissor rectangle");
const auto oldRight = static_cast<std::uint32_t>(result.offset.x) + result.extent.width;
const auto oldBottom = static_cast<std::uint32_t>(result.offset.y) + result.extent.height;
const auto left = std::max(static_cast<std::uint32_t>(result.offset.x), x);
const auto top = std::max(static_cast<std::uint32_t>(result.offset.y), y);
result.offset = {static_cast<std::int32_t>(left), static_cast<std::int32_t>(top)};
result.extent = {std::min(oldRight, right) > left ? std::min(oldRight, right) - left : 0, std::min(oldBottom, bottom) > top ? std::min(oldBottom, bottom) - top : 0};
}
}
ShaderStages DecodeShaderStages(const QueueState& queue) {
const auto value = read(queue.context, 0x2d5);
const auto validate = [&](bool condition, const char* reason) {
if (condition) return;
std::ostringstream prefix;
prefix << "VGT_SHADER_STAGES_EN=0x" << std::hex << value << ": " << reason;
Require(false, prefix.str());
};
validate((value & 0xfc000000u) == 0, "reserved stage bits are set");
validate((value & 3u) != 3u && ((value >> 3u) & 3u) != 3u && ((value >> 6u) & 3u) != 3u, "reserved LS_EN, ES_EN or VS_EN encoding");
const auto primitive = read(queue.userConfig, 0x242, RegisterBank::UserConfig);
const bool tessellation = primitive == 9;
const bool geometry = (value & 0x20u) != 0;
validate(tessellation == ((value & 4u) != 0), "Patch topology and HS_EN disagree");
validate(!tessellation || !geometry, "combined tessellation and geometry is unsupported by the reference path");
const auto path = tessellation ? ShaderPath::Tessellation : geometry ? ShaderPath::Geometry : ShaderPath::Vertex;
ShaderStages result{path, value, (value & 0x00400000u) != 0 ? 32u : 64u, (read(queue.context, 0x1b6) & 0x8000u) != 0 ? 32u : 64u, {}, {}};
APS5_LOG_OUT_DEBUG("DecodeShaderStages value=0x%x primitive=%u path=%u vertexWave=%u", value, primitive, static_cast<unsigned>(result.path), result.vertexWaveSize);
if (path == ShaderPath::Vertex) {
validate((value & 0x2000u) != 0, "legacy vertex routing without PRIMGEN_EN is unsupported");
validate((value & ~0x0247a010u) == 0, "unsupported vertex routing, scheduling or wave-ID state");
} else if (path == ShaderPath::Tessellation) {
validate((value & 0x00600020u) == 0, "wave32 tessellation or geometry amplification is unsupported");
validate((value & ~0x0007ed0du) == 0 && (value & 3u) == 1u && ((value >> 3u) & 3u) == 1u, "unsupported tessellation routing");
const auto config = read(queue.context, 0x2d6);
const auto parameters = read(queue.context, 0x2db);
ShaderRecompiler::TessellationConfiguration tess{(config >> 8u) & 0x3fu, (config >> 14u) & 0x3fu, parameters & 3u, (parameters >> 2u) & 3u, (parameters >> 5u) & 3u};
validate(tess.inputControlPoints != 0 && tess.inputControlPoints <= 32 && tess.outputControlPoints != 0 && tess.outputControlPoints <= 32, "invalid tessellation control-point counts");
validate(tess.domain == 1 && tess.partitioning == 2 && tess.outputTopology == 2, "only triangular, fractional-odd, clockwise tessellation is supported by the reference path");
result.tessellation = tess;
} else {
validate((value & ~0x0047ec30u) == 0, "unsupported geometry routing, fast launch or wave-ID state");
const auto group = read(queue.userConfig, 0x25b, RegisterBank::UserConfig);
const auto vertices = (group >> 9u) & 0x1ffu;
const auto primitives = group & 0x1ffu;
const auto maxVertices = read(queue.context, 0x1ff);
const auto verticesPerPrimitive = read(queue.context, 0x2ce);
validate((primitive == 1 || primitive == 2 || primitive == 4 || primitive == 5 || primitive == 6) && read(queue.context, 0x29b) == 2 && verticesPerPrimitive >= 3, "unsupported geometry input or output assembly");
const auto inputSize = primitive == 1 ? 1u : primitive == 2 ? 2u : 3u;
validate(vertices >= inputSize && maxVertices != 0 && maxVertices <= 256 && verticesPerPrimitive <= 256, "invalid geometry subgroup output");
const auto inputStep = primitive == 5 || primitive == 6 ? 1u : inputSize;
const auto groupPrimitives = std::min({primitives, (vertices - inputSize) / inputStep + 1u, maxVertices / verticesPerPrimitive});
validate(groupPrimitives != 0, "geometry subgroup contains no primitives");
const auto resources = read(queue.shader, 0x8b, RegisterBank::Shader);
validate(((read(queue.shader, 0x8a, RegisterBank::Shader) >> 29u) & 3u) == 3 && ((resources >> 16u) & 3u) == 3, "unsupported geometry VGPR allocation");
const auto esgsItemSize = read(queue.context, 0x2ab);
validate(esgsItemSize != 0 && esgsItemSize * vertices <= 0xffffu, "invalid VGT_ESGS_RING_ITEMSIZE");
const auto threads = std::max({(groupPrimitives - 1u) * inputStep + inputSize, primitives, maxVertices, primitives * (verticesPerPrimitive - 2u)});
result.mesh = ShaderRecompiler::MeshConfiguration{primitive, groupPrimitives, (groupPrimitives - 1u) * inputStep + inputSize, maxVertices, primitives * (verticesPerPrimitive - 2u), ((threads + result.vertexWaveSize - 1u) / result.vertexWaveSize) * result.vertexWaveSize, ((resources >> 19u) & 0xffu) * 128u, 0, esgsItemSize};
}
return result;
}
std::string DepthMaintenanceRejection(const QueueState& queue) {
const auto control = find(queue.context, 0x000);
if (control == queue.context.end() || (control->second & ~0x2063u) == 0) return {};
return zeroMessage(0x000, control->second, "DB_RENDER_CONTROL depth copy, resummarize or decompress");
}
State DecodeState(const QueueState& queue) {
if (auto reason = DepthMaintenanceRejection(queue); !reason.empty()) throw std::runtime_error(reason);
const auto& cx = queue.context;
State result{};
result.conservativeRasterization = decodeConservativeRasterization(queue);
result.stages = DecodeShaderStages(queue);
const auto primitive = read(queue.userConfig, 0x242, RegisterBank::UserConfig);
APS5_LOG_OUT_DEBUG("DecodeState primitive=%u path=%u vertexWave=%u", primitive, static_cast<unsigned>(result.stages.path), result.stages.vertexWaveSize);
switch (primitive) {
case 1: Require(result.stages.mesh.has_value(), "point-list vertex rendering requires point-size output support"); result.topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; break;
case 2: result.topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST; break;
case 7:
case 17:
Require(result.stages.path == ShaderPath::Vertex, "rect-list requires vertex routing");
result.rectList = true;
result.topology = VK_PRIMITIVE_TOPOLOGY_PATCH_LIST;
break;
case 9: result.topology = VK_PRIMITIVE_TOPOLOGY_PATCH_LIST; break;
case 4: result.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; break;
case 5: result.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN; break;
case 6: result.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP; break;
default: throw std::runtime_error("AGC graphics: unsupported primitive type " + std::to_string(primitive));
}
APS5_LOG_OUT_DEBUG("Topology=%u", static_cast<unsigned>(result.topology));
result.primitiveRestart = read(queue.userConfig, 0x24b, RegisterBank::UserConfig) != 0 && !result.rectList && result.topology != VK_PRIMITIVE_TOPOLOGY_PATCH_LIST;
if ((read(cx, 0x207) & LayerExports) != 0) {
static bool reported = false;
if (!reported) {
reported = true;
std::fprintf(stderr, "[gpu] layer/viewport index vertex exports are ignored (PA_CL_VS_OUT_CNTL=0x%08x)\n", read(cx, 0x207));
}
}
zero(cx, 0x207, ~LayerExports, "clip distances, layer, viewport or auxiliary vertex exports");
{
const auto depthControl = read(cx, 0x200);
if ((depthControl & 0xbu) != 0 && depthSurfaceBound(cx)) {
decodeDepth(cx, depthControl, result);
} else if (depthPassThrough(depthControl) || ((depthControl & 3u) != 0 && depthPlanesAbsent(cx))) {
static bool reported = false;
if (!reported) {
reported = true;
std::fprintf(stderr, "[gpu] always-pass depth/stencil state is rendered without a depth target (DB_DEPTH_CONTROL=0x%08x)\n", depthControl);
}
} else if (IgnoreDepthTest()) {
static bool reported = false;
if (!reported) {
reported = true;
std::fprintf(stderr, "[gpu] depth/stencil tests are ignored (APS5_IGNORE_DEPTH_TEST; DB_DEPTH_CONTROL=0x%08x)\n", depthControl);
}
} else {
if ((effectiveDepthControl(depthControl) & DepthControlMask) != 0) zero(cx, 0x200, DepthControlMask, "depth, stencil or conditional color writes");
}
Require((depthControl & 8u) == 0 || result.depth.has_value(), "depth bounds without a depth surface");
Require((depthControl & 0xc0000000u) == 0, "depth-conditional color writes are unsupported");
}
zero(cx, 0x203, shaderControlMask(read(cx, 0x1c4)), "depth export, shader coverage or ordered fragment execution");
zero(cx, 0x2dc, AlphaToCoverageMask, "alpha-to-coverage");
zero(cx, 0x2f8, ~0u, "multisampling or coverage conversion");
zero(cx, 0x292, ScanModeMask, "scan conversion mode");
zero(cx, 0x293, ScanControlMask, "sample iteration, primitive discard or out-of-order rasterization");
zero(cx, 0x80, ~0u, "window offset");
zero(cx, 0x8d, ScreenOffsetMask, "reserved PA_SU_HARDWARE_SCREEN_OFFSET bits");
Require(read(cx, 0x83) == 0xffffu, "clip rectangles are unsupported");
Require((read(cx, 0x8c) & 0xfu) == 0xau, "nonstandard triangle edge rules are unsupported");
Require(read(cx, 0x2f9) == 0x2du, "nonstandard pixel center or vertex quantization is unsupported");
Require(read(cx, 0x30e) == 0xffffffffu && read(cx, 0x30f) == 0xffffffffu, "sample masks are unsupported");
const auto viewportControl = read(cx, 0x206);
if (viewportControl != 0x43fu) throw std::runtime_error(vteMessage(viewportControl));
zero(cx, 0x204, ClipControlMask, "unsupported PA_CL_CLIP_CNTL flags");
result.depthClamp = (read(cx, 0x204) & 0x0c000000u) != 0;
result.negativeOneToOne = (read(cx, 0x204) & 0x80000u) == 0;
const auto raster = read(cx, 0x205);
// Bits 5-10 give the front/back polygon type (2 = filled triangles), which POLY_MODE (bit 3) turns on
// explicitly; KEEP_TOGETHER_ENABLE (bit 24) only affects primitive distribution across the chip.
const auto rasterMode = raster & ~0x7u & ~(1u << 24u) & ~0x1800u;
if (rasterMode != 0 && rasterMode != 0x240u && rasterMode != 0x248u) throw std::runtime_error("AGC graphics: " + zeroMessage(0x205, raster, "polygon mode, depth bias, provoking vertex or nonstandard rasterization"));
result.cullMode = ((raster & 1u) != 0 ? VK_CULL_MODE_FRONT_BIT : 0u) | ((raster & 2u) != 0 ? VK_CULL_MODE_BACK_BIT : 0u);
if (result.rectList) result.cullMode = VK_CULL_MODE_NONE;
if ((raster & 0x1800u) != 0 && result.depth) decodeDepthBias(cx, raster, result);
result.frontFace = (raster & 4u) != 0 ? VK_FRONT_FACE_CLOCKWISE : VK_FRONT_FACE_COUNTER_CLOCKWISE;
APS5_LOG_OUT_DEBUG("Raster=0x%x cullMode=0x%x frontFace=%u negativeOneToOne=%u", raster, static_cast<unsigned>(result.cullMode), static_cast<unsigned>(result.frontFace), result.negativeOneToOne ? 1u : 0u);
const auto shaderMask = read(cx, 0x8f);
// Channels of targets the pixel shader does not export are never written, so the target mask only
// matters where the shader exports.
const auto targetMask = ColorWriteMask(cx);
APS5_LOG_OUT_DEBUG("CB_TARGET_MASK=0x%x CB_SHADER_MASK=0x%x", targetMask, shaderMask);
std::vector<std::uint32_t> exportSlots;
for (std::uint32_t slot = 0; slot < 8; ++slot) {
if (((shaderMask >> (4u * slot)) & 0xfu) != 0) exportSlots.push_back(slot);
}
const auto written = [&](std::uint32_t slot) { return ((targetMask >> (4u * slot)) & 0xfu) != 0; };
std::uint32_t exportCount = 0;
for (std::uint32_t index = 0; index < exportSlots.size(); ++index) {
if (written(exportSlots[index])) exportCount = index + 1;
}
if (((read(cx, 0x202) >> 4u) & 7u) == 0u) exportCount = 0;
result.hasColorTarget = exportCount != 0;
APS5_LOG_OUT_DEBUG("hasColorTarget=%u exports=%u", result.hasColorTarget ? 1u : 0u, exportCount);
// CB_COLOR_CONTROL mode 0 disables color writes, which only matters when a target is written.
if (const auto colorControl = read(cx, 0x202); !colorControlSupported(colorControl, result.hasColorTarget)) throw std::runtime_error(colorControlMessage(colorControl));
zero(cx, 0x1c4, zFormatSupported(read(cx, 0x1c4)) ? 0u : ~0u, "depth or sample-mask export");
const auto exportFormat = result.hasColorTarget ? read(cx, 0x1c5) : 0u;
APS5_LOG_OUT_DEBUG("Export format=%u", exportFormat);
// SPI_SHADER_POS_FORMAT: POS0 must be a 4-component position; later vectors carry the misc/clip
// exports that PA_CL_VS_OUT_CNTL validation above already limits to ignored layer/viewport data.
Require((read(cx, 0x1c3) & 0xfu) == 4, "additional position exports are unsupported");
for (std::uint32_t index = 0; index < exportCount; ++index) {
const auto slot = exportSlots[index];
if (!written(slot)) continue;
// Export formats only matter for the targets the draw writes.
const auto slotExport = (exportFormat >> (4u * index)) & 0xfu;
if (slotExport == 0 || slotExport == 7 || slotExport == 8 || slotExport > 9) throw std::runtime_error("AGC graphics: color export format " + std::to_string(slotExport) + " is unsupported");
auto color = DecodeColorBuffer(cx, slot);
color.exportIndex = index;
APS5_LOG_OUT_DEBUG("Color %u address=0x%llx extent=%ux%u bytes=%llu VkFormat=%u", slot, static_cast<unsigned long long>(color.address), color.extent.width, color.extent.height, static_cast<unsigned long long>(color.bytes), static_cast<unsigned>(color.format));
if (result.colors.empty()) {
result.renderExtent = color.extent;
} else {
result.renderExtent = {std::min(result.renderExtent.width, color.extent.width), std::min(result.renderExtent.height, color.extent.height)};
}
result.colors.push_back(color);
}
if (result.hasColorTarget) {
result.color = result.colors.front();
if (result.depth) result.renderExtent = {std::min(result.renderExtent.width, result.depth->extent.width), std::min(result.renderExtent.height, result.depth->extent.height)};
} else if (result.depth) {
result.renderExtent = result.depth->extent;
} else {
const auto screenBottomRight = read(cx, 0xd);
APS5_LOG_OUT_DEBUG("No color target, screen BR register=0x%x", screenBottomRight);
result.renderExtent = {screenBottomRight & 0xffffu, screenBottomRight >> 16u};
APS5_LOG_OUT_DEBUG("Render extent from screen=%ux%u", result.renderExtent.width, result.renderExtent.height);
Require(result.renderExtent.width != 0 && result.renderExtent.height != 0, "empty framebuffer extent for a draw without color writes");
}
const auto xs = readFloat(cx, 0x10f);
const auto xo = readFloat(cx, 0x110);
const auto ys = readFloat(cx, 0x111);
const auto yo = readFloat(cx, 0x112);
const auto zs = readFloat(cx, 0x113);
const auto zo = readFloat(cx, 0x114);
const auto minDepth = result.negativeOneToOne ? zo - zs : zo;
const auto maxDepth = zo + zs;
APS5_LOG_OUT_DEBUG("Viewport transform scale=(%f,%f,%f) offset=(%f,%f,%f) depth=(%f,%f)", xs, ys, zs, xo, yo, zo, minDepth, maxDepth);
if (!(xs > 0 && ys != 0 && std::isfinite(minDepth) && std::isfinite(maxDepth))) {
std::ostringstream message;
message << "AGC graphics: unsupported viewport transform: scale=(" << xs << ", " << ys << ", " << zs << "), offset=(" << xo << ", " << yo << ", " << zo << "), depth=(" << minDepth << ", " << maxDepth << "), negativeOneToOne=" << result.negativeOneToOne;
throw std::runtime_error(message.str());
}
Require(readFloat(cx, 0xb4) <= readFloat(cx, 0xb5), "inverted viewport depth clamp bounds");
result.viewport = {xo - xs, yo - ys, 2 * xs, 2 * ys, minDepth, maxDepth};
APS5_LOG_OUT_DEBUG("Viewport x=%f y=%f w=%f h=%f minDepth=%f maxDepth=%f", result.viewport.x, result.viewport.y, result.viewport.width, result.viewport.height, result.viewport.minDepth, result.viewport.maxDepth);
result.scissor = {{0, 0}, result.renderExtent};
intersect(result.scissor, cx, 0xc, true);
intersect(result.scissor, cx, 0x81, false);
intersect(result.scissor, cx, 0x90, false);
if ((read(cx, 0x292) & 2u) != 0) intersect(result.scissor, cx, 0x94, false);
APS5_LOG_OUT_DEBUG("Scissor offset=(%d,%d) extent=%ux%u", result.scissor.offset.x, result.scissor.offset.y, result.scissor.extent.width, result.scissor.extent.height);
result.blends.assign(exportCount, VkPipelineColorBlendAttachmentState{});
for (const auto& color : result.colors) {
const auto slot = color.slot;
const auto blend = read(cx, 0x1e0 + slot);
APS5_LOG_OUT_DEBUG("Blend %u control=0x%x", slot, blend);
Require((blend & 0x0000e000u) == 0, "reserved blend control bits");
VkPipelineColorBlendAttachmentState state{};
const auto mapping = color.componentMapping;
const auto exportedMask = (targetMask >> (4u * slot)) & 0xfu;
for (std::uint32_t component = 0; component < 4; ++component) {
if (((exportedMask >> ((mapping >> (2u * component)) & 3u)) & 1u) != 0) state.colorWriteMask |= 1u << component;
}
state.blendEnable = (blend >> 30u) & 1u;
if (state.blendEnable && (mapping == 0x1bu || mapping == 0x93u)) throw std::runtime_error("AGC graphics: blending into a color target with a reversed component order is not implemented");
if (state.blendEnable) {
Require((read(cx, 0x31c + slot * 0xfu) & 0x10000u) == 0, "blend bypass conflicts with enabled blending");
state.srcColorBlendFactor = blendFactor(blend & 0x1fu);
state.dstColorBlendFactor = blendFactor((blend >> 8u) & 0x1fu);
state.colorBlendOp = blendOp((blend >> 5u) & 7u);
const auto alpha = (blend & 0x20000000u) != 0 ? blend >> 16u : blend;
state.srcAlphaBlendFactor = blendFactor(alpha & 0x1fu);
state.dstAlphaBlendFactor = blendFactor((alpha >> 8u) & 0x1fu);
state.alphaBlendOp = blendOp((alpha >> 5u) & 7u);
if ((mapping & 3u) == 3u) {
state.srcColorBlendFactor = state.srcAlphaBlendFactor;
state.dstColorBlendFactor = state.dstAlphaBlendFactor;
state.colorBlendOp = state.alphaBlendOp;
}
for (std::uint32_t i = 0; i < 4; ++i) result.blendConstants[i] = readFloat(cx, 0x105 + i);
}
result.blends[color.exportIndex] = state;
}
if (!result.colors.empty()) result.blend = result.blends[result.colors.front().exportIndex];
APS5_LOG_OUT_DEBUG("DecodeState done colorTarget=%u render=%ux%u topology=%u", result.hasColorTarget ? 1u : 0u, result.renderExtent.width, result.renderExtent.height, static_cast<unsigned>(result.topology));
return result;
}
std::array<std::uint8_t, 8> ExportMappings(const State& state) {
std::array<std::uint8_t, 8> mappings{};
mappings.fill(0xe4u);
for (const auto& color : state.colors) {
if (color.exportIndex < mappings.size()) mappings[color.exportIndex] = color.componentMapping;
}
return mappings;
}
std::size_t CmaskBytes(std::uint32_t width, std::uint32_t height) {
constexpr std::size_t metablockWidth = 1024;
constexpr std::size_t metablockHeight = 512;
constexpr std::size_t metablockBytes = 4096;
return ((width + metablockWidth - 1) / metablockWidth) * ((height + metablockHeight - 1) / metablockHeight) * metablockBytes;
}
std::uint32_t ColorWriteMask(const Registers& context) {
auto mask = read(context, 0x8e) & read(context, 0x8f);
for (std::uint32_t slot = 0; slot < 8; ++slot) {
const auto channels = 0xfu << (slot * 4u);
if ((mask & channels) == 0) continue;
const auto info = find(context, 0x31c + slot * 0xfu);
if (info != context.end() && ((info->second >> 2u) & 0x1fu) == 0) mask &= ~channels;
}
return mask;
}
ColorTarget DecodeColorBuffer(const Registers& cx, std::uint32_t slot) {
const auto stride = slot * 0xfu;
ColorTarget color{};
color.slot = slot;
const auto info = read(cx, 0x31c + stride);
const auto number = (info >> 8u) & 7u;
const auto swap = (info >> 11u) & 3u;
APS5_LOG_OUT_DEBUG("Color %u info=0x%x number=%u swap=%u", slot, info, number, swap);
const auto format = (info >> 2u) & 0x1fu;
const auto decoded = DecodeColorFormat(format, number, swap);
// ROUND_MODE (bit 18) only affects unorm rounding. With DCC_ENABLE (bit 28) the target is written
if ((info & ~(0x00039f7cu | 0x00040000u | 0x10000000u | 0x00002000u)) != 0) throw std::runtime_error("AGC graphics: color compression, DCC, endian conversion, nonstandard rounding or color optimization is unsupported (CB_COLOR_INFO 0x" + [&] { char text[16]; std::snprintf(text, sizeof(text), "%08x", info); return std::string(text); }() + ")");
Require((info & 0x8000u) != 0 || number == 7 || number == 4 || number == 5, "unclamped normalized color is unsupported");
const auto view = read(cx, 0x31b + stride);
Require((view & ~0x3fffffffu) == 0, "reserved CB_COLOR_VIEW bits are set");
const auto slice = view & 0x1fffu;
Require(slice == ((view >> 13u) & 0x1fffu), "color views of several array slices are unsupported");
const auto viewMip = (view >> 26u) & 0xfu;
zero(cx, 0x31d + stride, ~0u, "color samples, fragments or destination alpha override");
const auto attrib2 = read(cx, 0x3b0 + slot);
const auto maxMip = attrib2 >> 28u;
Require(viewMip <= maxMip, "color view mip exceeds the surface");
const auto attrib3 = read(cx, 0x3b8 + slot);
color.tileMode = DecodeColorTileMode(attrib3);
const bool volume = ((attrib3 >> 24u) & 3u) == 2u;
if (volume) {
color.depth = (attrib3 & 0x1fffu) + 1u;
Require(maxMip == 0 && (info & 0x10000000u) == 0, "mipmapped or DCC 3D color targets are unsupported");
Require(slice < color.depth, "the color view slice is beyond the 3D surface");
color.depthSlice = slice;
}
color.extent = {((attrib2 >> 14u) & 0x3fffu) + 1u, (attrib2 & 0x3fffu) + 1u};
color.elementBytes = decoded.elementBytes;
std::uint64_t mipOffset = 0;
color.surfaceExtent = color.extent;
color.mipCount = maxMip + 1u;
color.mip = viewMip;
if (maxMip != 0) {
// A mipmapped surface is addressed like a texture; the view renders into one mip of it.
const auto mips = ComputeElementMipLayout(ColorTextureTileMode(color.tileMode), color.elementBytes, color.extent.width, color.extent.height, maxMip + 1u);
const auto& mip = mips.at(viewMip);
mipOffset = mip.tiledOffset;
color.mipTail = mip.tail;
color.extent = {mip.width, mip.height};
}
const ColorTargetLayout colorLayout(color.extent.width, color.extent.height, color.tileMode, color.elementBytes);
const auto high = read(cx, 0x390 + slot);
Require((high & ~0xffu) == 0, "invalid color address extension");
color.surfaceAddress = (static_cast<std::uint64_t>(high) << 40u) | (static_cast<std::uint64_t>(read(cx, 0x318 + stride)) << 8u);
if (slice != 0 && !volume) color.surfaceAddress += slice * ComputeSurfaceSize(ComputeElementMipLayout(ColorTextureTileMode(color.tileMode), color.elementBytes, color.surfaceExtent.width, color.surfaceExtent.height, color.mipCount), 1);
color.address = color.surfaceAddress + mipOffset;
color.bytes = colorLayout.Bytes();
GuestMemory::CheckRange(reinterpret_cast<const void*>(color.address), color.bytes, colorLayout.Alignment(), true);
color.format = decoded.format;
color.componentMapping = decoded.componentMapping;
for (std::uint32_t word = 0; word < 2; ++word) {
const auto clear = find(cx, 0x323 + word + stride);
color.clearWords[word] = clear == cx.end() ? 0u : clear->second;
}
if ((info & 0x2000u) != 0) {
Require(maxMip == 0 && !volume && slice == 0, "CMASK fast clears of a mipmapped, 3D or array color target are unsupported");
const auto cmaskHigh = find(cx, 0x398 + slot);
color.cmaskAddress = ((cmaskHigh == cx.end() ? 0ull : static_cast<std::uint64_t>(cmaskHigh->second & 0xffu)) << 40u) | (static_cast<std::uint64_t>(read(cx, 0x31f + stride)) << 8u);
Require(color.cmaskAddress != 0, "CMASK fast clears without a CMASK address are unsupported");
color.cmaskBytes = CmaskBytes(color.extent.width, color.extent.height);
}
if ((info & 0x10000000u) != 0) {
if (maxMip == 0) {
const auto dccHigh = find(cx, 0x3a8 + slot);
color.dccAddress = ((dccHigh == cx.end() ? 0ull : static_cast<std::uint64_t>(dccHigh->second & 0xffu)) << 40u) | (static_cast<std::uint64_t>(read(cx, 0x325 + stride)) << 8u);
color.dccAlphaOnMsb = DccAlphaOnMsb(color.format, swap);
color.dccPipeAligned = ((attrib3 >> 30u) & 1u) != 0;
} else {
static bool reported = false;
if (!reported) {
reported = true;
std::fprintf(stderr, "[gpu] DCC keys of mipmapped color targets are ignored\n");
}
}
}
return color;
}
std::optional<ColorMetadataPass> DecodeColorMetadataPass(const QueueState& queue) {
const auto& cx = queue.context;
const auto control = find(cx, 0x202);
if (control == cx.end()) return std::nullopt;
const auto mode = (control->second >> 4u) & 7u;
if (mode != 2u && mode != 6u) return std::nullopt;
ColorMetadataPass pass{mode == 2u ? ColorMetadataPass::Mode::EliminateFastClear : ColorMetadataPass::Mode::DccDecompress, {}};
Require((control->second & ~0x70u) == 0xcc0000u, "CB metadata pass with a nonstandard ROP, dual quads disabled or degamma");
Require((read(cx, 0x200) & 0xfu) == 0 && (read(cx, 0x0) & 0xfu) == 0, "CB metadata pass with depth or stencil work");
zero(cx, 0x2f8, ~0u, "multisampling or coverage conversion");
zero(cx, 0x80, ~0u, "window offset");
const auto viewportControl = read(cx, 0x206);
if (viewportControl != 0x43fu) throw std::runtime_error(vteMessage(viewportControl));
const auto xs = std::fabs(readFloat(cx, 0x10f));
const auto xo = readFloat(cx, 0x110);
const auto ys = std::fabs(readFloat(cx, 0x111));
const auto yo = readFloat(cx, 0x112);
VkRect2D covered{{0, 0}, {0x7fffu, 0x7fffu}};
intersect(covered, cx, 0xc, true);
intersect(covered, cx, 0x81, false);
intersect(covered, cx, 0x90, false);
if ((read(cx, 0x292) & 2u) != 0) intersect(covered, cx, 0x94, false);
const auto targetMask = read(cx, 0x8e);
for (std::uint32_t slot = 0; slot < 8; ++slot) {
if (((targetMask >> (4u * slot)) & 0xfu) == 0 || ((read(cx, 0x31c + slot * 0xfu) >> 2u) & 0x1fu) == 0) continue;
const auto target = DecodeColorBuffer(cx, slot);
Require((read(cx, 0x31c + slot * 0xfu) & 0x10000000u) == 0 || target.dccAddress != 0, "CB metadata pass over a mipmapped DCC color target, whose keys are not modeled");
const auto width = static_cast<float>(target.extent.width);
const auto height = static_cast<float>(target.extent.height);
const bool viewportCovers = xo - xs <= 0.0f && xo + xs >= width && yo - ys <= 0.0f && yo + ys >= height;
const bool scissorCovers = covered.offset.x == 0 && covered.offset.y == 0 && covered.extent.width >= target.extent.width && covered.extent.height >= target.extent.height;
Require(viewportCovers && scissorCovers, "CB metadata pass over part of a color target");
pass.targets.push_back(target);
}
return pass;
}
std::string DrawRejection(const QueueState& queue, bool indexed) {
if (auto reason = DepthMaintenanceRejection(queue); !reason.empty()) return reason;
const auto& cx = queue.context;
// A register a rule needs that is absent gives no verdict here: DecodeState reports it.
const auto value = [&](const Registers& registers, std::uint32_t offset, std::uint32_t& out) {
const auto it = find(registers, offset, ®isters == &queue.userConfig ? RegisterBank::UserConfig : RegisterBank::Context);
if (it == registers.end()) return false;
out = it->second;
return true;
};
std::uint32_t word = 0;
const auto nonzero = [&](const Registers& registers, std::uint32_t offset, std::uint32_t mask, const char* name) {
return value(registers, offset, word) && (word & mask) != 0 ? zeroMessage(offset, word, name) : std::string();
};
const auto require = [&](bool condition, const char* reason) {
return condition ? std::string() : "AGC graphics: " + std::string(reason);
};
if (indexed && value(queue.userConfig, 0x24b, word) && word != 0) {
std::uint32_t primitive = 0;
std::uint32_t resetIndex = 0;
if (value(queue.userConfig, 0x242, primitive) && (primitive & 0x3fu) != 1 && (primitive & 0x3fu) != 2 && (primitive & 0x3fu) != 3 && (primitive & 0x3fu) != 4 && (primitive & 0x3fu) != 5 && (primitive & 0x3fu) != 6) return "AGC graphics: primitive restart is only supported for point, line and triangle topologies";
if (value(cx, 0x103, resetIndex) && (resetIndex & 0xffffu) != 0xffffu) return "AGC graphics: primitive restart index other than all ones is unsupported";
}
if (auto reason = nonzero(cx, 0x207, ~LayerExports, "clip distances, layer, viewport or auxiliary vertex exports"); !reason.empty()) return reason;
if (value(cx, 0x200, word)) {
const bool surface = (word & 0xbu) != 0 && depthSurfaceBound(cx);
if (!surface && !((word & 3u) != 0 && depthPlanesAbsent(cx)) && !depthPassThrough(word) && !IgnoreDepthTest() && (effectiveDepthControl(word) & DepthControlMask) != 0) return zeroMessage(0x200, word, "depth, stencil or conditional color writes");
if (auto reason = require((word & 8u) == 0 || surface, "depth bounds without a depth surface"); !reason.empty()) return reason;
if (auto reason = require((word & 0xc0000000u) == 0, "depth-conditional color writes are unsupported"); !reason.empty()) return reason;
}
std::uint32_t zFormat = 0;
static_cast<void>(value(cx, 0x1c4, zFormat));
if (auto reason = nonzero(cx, 0x203, shaderControlMask(zFormat), "depth export, shader coverage or ordered fragment execution"); !reason.empty()) return reason;
if (auto reason = nonzero(cx, 0x2dc, AlphaToCoverageMask, "alpha-to-coverage"); !reason.empty()) return reason;
if (auto reason = nonzero(cx, 0x2f8, ~0u, "multisampling or coverage conversion"); !reason.empty()) return reason;
if (auto reason = nonzero(cx, 0x292, ScanModeMask, "scan conversion mode"); !reason.empty()) return reason;
if (auto reason = nonzero(cx, 0x293, ScanControlMask, "sample iteration, primitive discard or out-of-order rasterization"); !reason.empty()) return reason;
if (auto reason = nonzero(cx, 0x80, ~0u, "window offset"); !reason.empty()) return reason;
if (auto reason = nonzero(cx, 0x8d, ScreenOffsetMask, "reserved PA_SU_HARDWARE_SCREEN_OFFSET bits"); !reason.empty()) return reason;
if (value(cx, 0x83, word) && word != 0xffffu) return require(false, "clip rectangles are unsupported");
if (value(cx, 0x8c, word) && (word & 0xfu) != 0xau) return require(false, "nonstandard triangle edge rules are unsupported");
if (value(cx, 0x2f9, word) && word != 0x2du) return require(false, "nonstandard pixel center or vertex quantization is unsupported");
if (value(cx, 0x313, word) && word != 0x6000u && word != 0x6001u) return conservativeMessage(word);
std::uint32_t other = 0;
if (value(cx, 0x30e, word) && value(cx, 0x30f, other) && (word != 0xffffffffu || other != 0xffffffffu)) return require(false, "sample masks are unsupported");
if (value(cx, 0x206, word) && word != 0x43fu) return vteMessage(word);
if (auto reason = nonzero(cx, 0x204, ClipControlMask, "unsupported PA_CL_CLIP_CNTL flags"); !reason.empty()) return reason;
std::uint32_t targetMask = 0, shaderMask = 0;
if (value(cx, 0x8e, targetMask) && value(cx, 0x8f, shaderMask) && value(cx, 0x202, word) && !colorControlSupported(word, ColorWriteMask(cx) != 0)) return colorControlMessage(word);
if (auto reason = nonzero(cx, 0x1c4, zFormatSupported(zFormat) ? 0u : ~0u, "depth or sample-mask export"); !reason.empty()) return reason;
if (PixelProgramSkipped(queue)) return NullPixelProgramRejection(queue);
for (const auto offset : {0x1b3u, 0x1b4u, 0x1c5u}) {
if (find(cx, offset) != cx.end()) continue;
char text[64];
std::snprintf(text, sizeof(text), "AGC graphics: missing register at DWORD 0x%x", offset);
return text;
}
return {};
}
bool PixelProgramSkipped(const QueueState& queue) {
const auto low = find(queue.shader, 0x008, RegisterBank::Shader);
const auto high = find(queue.shader, 0x009, RegisterBank::Shader);
if (low != queue.shader.end() && high != queue.shader.end() && low->second == 0 && high->second == 0) return true;
const auto& cx = queue.context;
const auto targetMask = find(cx, 0x8e);
const auto shaderMask = find(cx, 0x8f);
const auto zFormat = find(cx, 0x1c4);
const auto shaderControl = find(cx, 0x203);
if (targetMask == cx.end() || shaderMask == cx.end() || zFormat == cx.end() || shaderControl == cx.end()) return false;
return (targetMask->second & shaderMask->second) == 0 && zFormat->second == 0 && (shaderControl->second & PixelStageRunsMask) == 0;
}
std::string NullPixelProgramRejection(const QueueState& queue) {
const auto targetMask = find(queue.context, 0x8e);
const auto shaderMask = find(queue.context, 0x8f);
if (targetMask == queue.context.end() || shaderMask == queue.context.end()) return "AGC graphics: a draw without a pixel program needs CB_TARGET_MASK and CB_SHADER_MASK";
if (ColorWriteMask(queue.context) != 0) return "AGC graphics: a draw without a pixel program writes color";
return {};
}
std::vector<RegisterRead>*& RegisterReadLog() {
static thread_local std::vector<RegisterRead>* log = nullptr;
return log;
}
bool DrawKeyCovers(RegisterRead read) {
// One bit per register offset of each bank, set from the table once.
static const auto covered = [] {
std::array<std::bitset<1024>, static_cast<std::size_t>(RegisterBank::Count)> bits{};
for (const auto& range : DrawKeyRegisters) {
for (std::uint32_t i = 0; i < range.count; ++i) bits[static_cast<std::size_t>(range.bank)].set(range.first + i);
}
return bits;
}();
return read.bank < RegisterBank::Count && read.offset < 1024 && covered[static_cast<std::size_t>(read.bank)].test(read.offset);
}
}