Repository navigation
Expand file tree
/
Copy pathResourceTrackingTests.cpp
More file actions
1958 lines (1828 loc) · 86.7 KB
/
Copy pathResourceTrackingTests.cpp
File metadata and controls
1958 lines (1828 loc) · 86.7 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
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
#include "graphics/guest_gpu/gpu_defs.h"
#include "graphics/shader/recompiler/ir/BindingLayout.h"
#include "graphics/shader/recompiler/ir/ResourceMaterialization.h"
#include "graphics/shader/recompiler/ir/ResourceTracking.h"
#include "graphics/shader/recompiler/ir/ScalarProvenance.h"
#include "graphics/shader/recompiler/ir/ShaderInfoCollection.h"
#include "graphics/shader/recompiler/ir/SrtPatcher.h"
#include "graphics/shader/recompiler/ir/SrtWalker.h"
#include "graphics/shader/shaderBindings.h"
#include <algorithm>
#include <array>
#include <iostream>
#include <stdexcept>
namespace {
using namespace Libs::Graphics::ShaderRecompiler::IR;
using Libs::Graphics::ShaderComputeInputInfo;
using Libs::Graphics::ShaderPixelInputInfo;
using Libs::Graphics::ShaderTextureResource;
using Libs::Graphics::ShaderType;
using Libs::Graphics::ShaderVertexInputInfo;
namespace Prospero = Libs::Graphics::Prospero;
namespace Decoder = Libs::Graphics::ShaderRecompiler::Decoder;
void Check(bool condition, const char* message) {
if (!condition) {
throw std::runtime_error(message);
}
}
Operand Sgpr(uint32_t reg) {
Operand operand;
operand.kind = OperandKind::Register;
operand.reg.file = RegisterFile::Scalar;
operand.reg.index = reg;
return operand;
}
Operand Imm(uint32_t value) {
Operand operand;
operand.kind = OperandKind::ImmediateU32;
operand.imm = value;
return operand;
}
Instruction Move(uint32_t pc, uint32_t dst, uint32_t src) {
Instruction inst;
inst.pc = pc;
inst.op = Opcode::MoveU32;
inst.dst = Sgpr(dst);
inst.src[0] = Sgpr(src);
inst.src_count = 1;
return inst;
}
Instruction MoveImmediate(uint32_t pc, uint32_t dst, uint32_t value) {
auto inst = Move(pc, dst, 0);
inst.src[0] = Imm(value);
return inst;
}
Instruction BufferUse(uint32_t pc, uint32_t base, Opcode op = Opcode::BufferLoadDword) {
Instruction inst;
inst.pc = pc;
inst.op = op;
inst.memory.kind = ResourceKind::Buffer;
inst.memory.resource = base / 4;
return inst;
}
Instruction ImageUse(uint32_t pc, Opcode op, ResourceKind kind, Decoder::ImageDimension dimension,
uint32_t resource = 0, uint32_t sampler = 2) {
Instruction inst;
inst.pc = pc;
inst.op = op;
inst.memory.kind = kind;
inst.memory.resource = resource;
inst.memory.sampler = sampler;
inst.memory.image_dimension = dimension;
return inst;
}
Instruction ScalarLoad(uint32_t pc, uint32_t dst, uint32_t base, uint32_t offset) {
Instruction inst;
inst.pc = pc;
inst.op = Opcode::SLoadDword;
inst.dst = Sgpr(dst);
inst.memory.kind = ResourceKind::ScalarBuffer;
inst.memory.resource = base;
inst.memory.offset = offset;
return inst;
}
Instruction ScalarBufferLoad(uint32_t pc, uint32_t dst, uint32_t descriptor, uint32_t offset) {
auto inst = ScalarLoad(pc, dst, descriptor * 4, offset);
inst.op = Opcode::SBufferLoadDword;
inst.memory.resource = descriptor;
return inst;
}
void MakeScalarMemoryGroup(std::vector<Instruction>* instructions) {
const auto count = static_cast<uint32_t>(instructions->size());
for (uint32_t i = 0; i < count; i++) {
(*instructions)[i].memory.component_index = i;
(*instructions)[i].memory.component_count = count;
}
}
Instruction Export(uint32_t pc, ExportTargetKind kind, uint32_t index = 0, uint32_t en = 0xf) {
Instruction inst;
inst.pc = pc;
inst.op = Opcode::Export;
inst.export_info.kind = kind;
inst.export_info.index = index;
inst.export_info.en = en;
return inst;
}
struct TestMemory {
uint64_t base = 0x1000;
std::array<uint32_t, 8> words {};
uint32_t reads = 0;
uint32_t fail_after = UINT32_MAX;
};
bool ReadTestMemory(void* userdata, uint64_t address, uint32_t* value) {
auto* memory = static_cast<TestMemory*>(userdata);
if (memory == nullptr || value == nullptr || address < memory->base ||
address - memory->base >= memory->words.size() * sizeof(uint32_t) ||
memory->reads >= memory->fail_after) {
return false;
}
const auto index = static_cast<size_t>((address - memory->base) / sizeof(uint32_t));
*value = memory->words[index];
memory->reads++;
return true;
}
const StageInput* FindInput(const ShaderInfo& info, StageInputKind kind, uint32_t location = 0) {
for (const auto& input: info.inputs) {
if (input.kind == kind && input.location == location) {
return &input;
}
}
return nullptr;
}
const StageOutput* FindOutput(const ShaderInfo& info, StageOutputKind kind, uint32_t index = 0) {
for (const auto& output: info.outputs) {
if (output.kind == kind && output.index == index) {
return &output;
}
}
return nullptr;
}
void Prepare(Program& program) {
std::string error;
if (!BuildScalarProvenance(program, &error) || !BuildSrtPlan(program, &error) ||
!PatchSrtReads(program, &error) || !TrackResources(program, &error)) {
throw std::runtime_error(error);
}
}
void TestDenseBufferPatching() {
Program program;
program.blocks.resize(1);
auto first = BufferUse(4, 0);
first.memory.offset = 4;
first.memory.formatted = true;
auto write = BufferUse(8, 0, Opcode::BufferStoreDword);
write.memory.offset = 12;
auto atomic = BufferUse(10, 0, Opcode::AtomicAddU32);
auto& insts = program.blocks[0].instructions;
insts = {first, atomic, write, Move(12, 4, 20),
Move(16, 5, 21), Move(20, 6, 22), Move(24, 7, 23), BufferUse(28, 4)};
Prepare(program);
Check(program.info.buffers.size() == 2, "buffer sources were not densely deduplicated");
const auto& resource = program.info.buffers[0];
Check(resource.read && resource.written && resource.atomic && resource.formatted &&
resource.max_byte_extent == 16 && resource.first_use_pc == 4,
"buffer access facts were not merged");
Check(insts[0].memory.resource == 0 && insts[1].memory.resource == 0 &&
insts[2].memory.resource == 0 && insts[7].memory.resource == 1,
"buffer operands were not patched to dense indices");
Check(insts[0].memory.resource_source == ScalarProvenance::Undefined &&
insts[7].memory.resource_source == ScalarProvenance::Undefined,
"patched instructions retained duplicate descriptor source handles");
std::string error;
Check(!TrackResources(program, &error) && error.find("already tracked") != std::string::npos,
"resource tracking was not guarded against a second patch pass");
}
void TestScalarAndVectorBufferAlias() {
Program program;
program.blocks.resize(1);
Instruction scalar;
scalar.pc = 4;
scalar.op = Opcode::SBufferLoadDword;
scalar.dst = Sgpr(20);
scalar.src[0] = Imm(0);
scalar.src_count = 1;
scalar.memory.kind = ResourceKind::ScalarBuffer;
scalar.memory.resource = 2;
program.blocks[0].instructions = {scalar, BufferUse(8, 8)};
Prepare(program);
Check(program.info.buffers.size() == 1 && program.info.buffers[0].scalar,
"scalar and vector uses of one descriptor were split");
Check(program.blocks[0].instructions[0].memory.resource == 0 &&
program.blocks[0].instructions[1].memory.resource == 0,
"scalar/vector alias did not share one dense index");
}
void TestBufferImageAliasIsLinkedDuringTracking() {
Program program;
program.blocks.resize(1);
program.blocks[0].instructions = {
BufferUse(4, 0), BufferUse(8, 8),
ImageUse(12, Opcode::ImageLoad, ResourceKind::Image, Decoder::ImageDimension::Dim2D)};
Prepare(program);
Check(program.info.buffers.size() == 2 && program.info.images.size() == 1 &&
program.info.buffers[0].image_alias == 0 &&
program.info.buffers[1].image_alias == BufferResource::NoImageAlias,
"buffer/image descriptor provenance aliases were not linked during "
"tracking");
}
void TestImagesAndSamplers() {
Program program;
program.blocks.resize(1);
auto sample0 =
ImageUse(4, Opcode::ImageSample, ResourceKind::Image, Decoder::ImageDimension::Dim2D);
auto sample1 =
ImageUse(8, Opcode::ImageSample, ResourceKind::Image, Decoder::ImageDimension::Dim2D);
auto volume =
ImageUse(12, Opcode::ImageSample, ResourceKind::Image, Decoder::ImageDimension::Dim3D);
auto compare =
ImageUse(16, Opcode::ImageSample, ResourceKind::Image, Decoder::ImageDimension::Dim2D);
compare.memory.image_sample_flags = Decoder::ImageSampleFlagCompare;
auto storage = ImageUse(20, Opcode::ImageStore, ResourceKind::StorageImage,
Decoder::ImageDimension::Dim2D);
auto storage_mip = storage;
storage_mip.pc = 24;
storage_mip.memory.image_has_mip = true;
auto atomic = ImageUse(28, Opcode::AtomicAddU32, ResourceKind::StorageImageUint,
Decoder::ImageDimension::Dim2D);
program.blocks[0].instructions = {sample0, sample1, volume, compare,
storage, storage_mip, atomic};
Prepare(program);
Check(program.info.images.size() == 6 && program.info.samplers.size() == 1 &&
program.info.sampled_pairs.size() == 3,
"image view classes or samplers were deduplicated incorrectly");
const auto& insts = program.blocks[0].instructions;
Check(insts[0].memory.resource == 0 && insts[1].memory.resource == 0 &&
insts[2].memory.resource == 1 && insts[3].memory.resource == 2 &&
insts[4].memory.resource == 3 && insts[5].memory.resource == 4 &&
insts[6].memory.resource == 5 && insts[0].memory.sampler == 0 &&
insts[2].memory.sampler == 0,
"image/sampler operands were not patched to dense list indices");
Check(program.info.images[0].read && !program.info.images[0].written &&
program.info.images[2].depth_compare && program.info.images[3].written &&
program.info.images[4].mip_mode == ImageMipMode::DynamicStorage &&
program.info.images[5].atomic &&
program.info.sampled_pairs[0].sampler == program.info.sampled_pairs[1].sampler,
"image access facts were wrong");
}
void TestDynamicPhiResource() {
Program program;
program.blocks.resize(4);
program.blocks[0].successors = {1, 2};
program.blocks[1].predecessors = {0};
program.blocks[1].successors = {3};
program.blocks[2].predecessors = {0};
program.blocks[2].successors = {3};
program.blocks[3].predecessors = {1, 2};
Instruction left;
left.op = Opcode::MoveU32;
left.dst = Sgpr(0);
left.src[0] = Imm(1);
left.src_count = 1;
Instruction right = left;
right.pc = 4;
right.src[0] = Imm(2);
program.blocks[1].instructions = {left};
program.blocks[2].instructions = {right};
program.blocks[3].instructions = {BufferUse(8, 0)};
std::string error;
Check(BuildScalarProvenance(program, &error) && BuildSrtPlan(program, &error) &&
PatchSrtReads(program, &error),
error.c_str());
const auto original_source = program.blocks[3].instructions[0].memory.resource_source;
Check(!TrackResources(program, &error) &&
error.find("unsupported GPU selection") != std::string::npos &&
program.blocks[3].instructions[0].memory.resource_source == original_source &&
!program.resource_tracking_complete,
"control-flow descriptor was patched without an executable GPU selector");
}
void TestTrackingRequiresCompletedSrtPlan() {
Program program;
program.stage = ShaderType::Compute;
program.shader_hash = 0x8899;
program.blocks.resize(4);
program.blocks[0].successors = {1, 2};
program.blocks[1].predecessors = {0};
program.blocks[1].successors = {3};
program.blocks[2].predecessors = {0};
program.blocks[2].successors = {3};
program.blocks[3].predecessors = {1, 2};
program.blocks[1].instructions = {MoveImmediate(0, 0, 1)};
program.blocks[2].instructions = {MoveImmediate(4, 0, 2)};
program.blocks[3].instructions = {BufferUse(8, 0)};
std::string error;
Check(BuildScalarProvenance(program, &error), error.c_str());
Check(!program.srt_plan_complete, "provenance unexpectedly marked the SRT plan complete");
const auto original_source = program.blocks[3].instructions[0].memory.resource_source;
BufferResource existing_info;
existing_info.source = 777;
program.info.buffers.push_back(existing_info);
Check(!TrackResources(program, &error) &&
error.find("SRT plan is not ready") != std::string::npos &&
program.blocks[3].instructions[0].memory.resource == 0 &&
program.blocks[3].instructions[0].memory.resource_source == original_source &&
program.info.buffers.size() == 1 && program.info.buffers[0].source == 777 &&
!program.resource_tracking_complete,
"tracking bypassed the SRT-plan readiness invariant or partially "
"patched the program");
}
void TestCyclicResourceIsRejected() {
Program program;
program.blocks.resize(2);
program.blocks[0].successors = {1};
program.blocks[1].predecessors = {0, 1};
program.blocks[1].successors = {1};
Instruction increment;
increment.op = Opcode::IAddU32;
increment.dst = Sgpr(0);
increment.src[0] = Sgpr(0);
increment.src[1] = Imm(1);
increment.src_count = 2;
program.blocks[1].instructions = {BufferUse(4, 0), increment};
std::string error;
Check(BuildScalarProvenance(program, &error) && BuildSrtPlan(program, &error) &&
PatchSrtReads(program, &error),
error.c_str());
Check(!TrackResources(program, &error) &&
error.find("unsupported GPU selection") != std::string::npos &&
!program.resource_tracking_complete,
"cyclic descriptor was patched without a bindless/direct path");
}
void TestUnknownSourceFailsWithoutPatching() {
Program program;
program.stage = ShaderType::Pixel;
program.shader_hash = 0x12345678;
program.blocks.resize(1);
auto valid = BufferUse(4, 0);
Instruction unsupported;
unsupported.op = Opcode::SelectU32;
unsupported.dst = Sgpr(4);
unsupported.src[0] = Sgpr(20);
unsupported.src_count = 1;
program.blocks[0].instructions = {valid, unsupported, BufferUse(0x44, 4)};
std::string error;
Check(BuildScalarProvenance(program, &error) && BuildSrtPlan(program, &error) &&
PatchSrtReads(program, &error),
error.c_str());
BufferResource existing_info;
existing_info.source = 777;
program.info.buffers.push_back(existing_info);
Check(!TrackResources(program, &error), "unknown descriptor unexpectedly tracked");
Check(error.find("hash=0x0000000012345678") != std::string::npos &&
error.find("stage=pixel") != std::string::npos &&
error.find("pc=0x00000044") != std::string::npos &&
error.find("unknown value") != std::string::npos,
"unknown descriptor error lost shader context");
Check(!program.resource_tracking_complete && program.info.buffers.size() == 1 &&
program.info.buffers[0].source == 777 &&
program.blocks[0].instructions[0].memory.resource == 0 &&
program.blocks[0].instructions[0].memory.resource_source !=
ScalarProvenance::Undefined &&
program.blocks[0].instructions[2].memory.resource == 1 &&
program.blocks[0].instructions[2].memory.resource_source !=
ScalarProvenance::Undefined,
"failed tracking partially patched the program");
}
void TestResourceLimitFailsTransactionally() {
Program program;
program.stage = ShaderType::Compute;
program.blocks.resize(1);
auto& insts = program.blocks[0].instructions;
for (uint32_t i = 0; i <= ShaderInfo::MaxBuffers; i++) {
insts.push_back(MoveImmediate(i * 8, 0, i));
insts.push_back(BufferUse(i * 8 + 4, 0));
}
std::string error;
Check(BuildScalarProvenance(program, &error) && BuildSrtPlan(program, &error) &&
PatchSrtReads(program, &error),
error.c_str());
const auto first_source = insts[1].memory.resource_source;
Check(!TrackResources(program, &error) &&
error.find("buffer resource limit exceeded") != std::string::npos &&
insts[1].memory.resource_source == first_source && program.info.buffers.empty() &&
!program.resource_tracking_complete,
"resource limit failure partially patched the program");
}
void TestComputeShaderInfoCollection() {
Program program;
program.stage = ShaderType::Compute;
program.blocks.resize(1);
Instruction add_tid;
add_tid.op = Opcode::DsReadAddtidB32;
program.blocks[0].instructions = {add_tid, BufferUse(4, 0)};
Prepare(program);
const auto buffers = program.info.buffers;
const auto images = program.info.images;
const auto samplers = program.info.samplers;
const auto pairs = program.info.sampled_pairs;
ShaderComputeInputInfo compute;
compute.group_id[1] = true;
compute.dispatch_thread_dimensions = true;
std::string error;
Check(CollectShaderInfo(program, {.compute = &compute}, &error), error.c_str());
Check(program.shader_info_complete && program.info.inputs.size() == 3 &&
FindInput(program.info, StageInputKind::WorkgroupId) != nullptr &&
FindInput(program.info, StageInputKind::LocalInvocationId) == nullptr &&
FindInput(program.info, StageInputKind::LocalInvocationIndex) != nullptr &&
FindInput(program.info, StageInputKind::GlobalInvocationId) != nullptr &&
program.info.buffers == buffers && program.info.images == images &&
program.info.samplers == samplers && program.info.sampled_pairs == pairs,
"compute addtid input discovery or resource preservation was wrong");
Program metadata_program;
metadata_program.stage = ShaderType::Compute;
metadata_program.blocks.resize(1);
Instruction xor_address;
xor_address.op = Opcode::BitwiseXor3U32;
metadata_program.blocks[0].instructions = {xor_address};
Prepare(metadata_program);
compute = {};
compute.thread_ids_num = 2;
Check(CollectShaderInfo(metadata_program, {.compute = &compute}, &error), error.c_str());
Check(FindInput(metadata_program.info, StageInputKind::LocalInvocationId) != nullptr &&
FindInput(metadata_program.info, StageInputKind::LocalInvocationIndex) != nullptr &&
metadata_program.info.has_bitwise_xor,
"compute thread metadata or XOR-address heuristic was not collected");
}
void TestVertexShaderInfoCollection() {
Program program;
program.stage = ShaderType::Vertex;
program.blocks.resize(1);
Instruction attr0;
attr0.op = Opcode::LoadInputF32;
attr0.input_info.attr = 0;
attr0.input_info.chan = 2;
Instruction attr2 = attr0;
attr2.input_info.attr = 2;
attr2.input_info.chan = 0;
program.blocks[0].instructions = {attr0, attr2, Export(8, ExportTargetKind::Position),
Export(12, ExportTargetKind::Parameter, 2)};
Prepare(program);
ShaderVertexInputInfo vertex;
vertex.resources_num = 3;
std::string error;
Check(CollectShaderInfo(program, {.vertex = &vertex}, &error), error.c_str());
const auto* input0 = FindInput(program.info, StageInputKind::Parameter, 0);
const auto* input2 = FindInput(program.info, StageInputKind::Parameter, 2);
Check(program.info.inputs.size() == 4 && input0 != nullptr && input0->component_count == 3 &&
input2 != nullptr && input2->component_count == 1 &&
FindInput(program.info, StageInputKind::VertexIndex) != nullptr &&
FindInput(program.info, StageInputKind::InstanceIndex) != nullptr &&
FindOutput(program.info, StageOutputKind::Position) != nullptr &&
FindOutput(program.info, StageOutputKind::Parameter, 2) != nullptr,
"vertex inputs or exports were not collected from lowered IR");
const auto info = program.info;
const auto resources_complete = program.resource_tracking_complete;
const auto srt_complete = program.srt_plan_complete;
const auto patching_complete = program.srt_patching_complete;
Check(!CollectShaderInfo(program, {.vertex = &vertex}, &error) &&
error.find("already collected") != std::string::npos && program.info == info &&
program.shader_info_complete &&
program.resource_tracking_complete == resources_complete &&
program.srt_plan_complete == srt_complete &&
program.srt_patching_complete == patching_complete,
"repeated shader info collection mutated the immutable interface");
}
void TestPixelShaderInfoCollection() {
Program program;
program.stage = ShaderType::Pixel;
program.blocks.resize(1);
program.blocks[0].instructions = {
Export(4, ExportTargetKind::Mrt, 3), Export(8, ExportTargetKind::MrtZ, 0, 0x4),
Export(12, ExportTargetKind::Mrt, 3), Export(16, ExportTargetKind::Mrt, 7, 0)};
Prepare(program);
ShaderPixelInputInfo pixel;
pixel.ps_pos_x = true;
pixel.ps_front_face = true;
pixel.input_num = 2;
pixel.ps_depth_export_enable = true;
pixel.ps_sample_mask_export_enable = true;
std::string error;
Check(CollectShaderInfo(program, {.pixel = &pixel}, &error), error.c_str());
Check(program.info.inputs.size() == 4 &&
FindInput(program.info, StageInputKind::FragCoord) != nullptr &&
FindInput(program.info, StageInputKind::FrontFacing) != nullptr &&
FindInput(program.info, StageInputKind::Parameter, 0) != nullptr &&
FindInput(program.info, StageInputKind::Parameter, 1) != nullptr &&
program.info.outputs.size() == 2 &&
FindOutput(program.info, StageOutputKind::Mrt, 3) != nullptr &&
FindOutput(program.info, StageOutputKind::Mrt, 7) == nullptr &&
FindOutput(program.info, StageOutputKind::Depth) == nullptr &&
FindOutput(program.info, StageOutputKind::SampleMask) != nullptr,
"pixel inputs, disabled exports, or sample-mask-only MRTZ collection "
"was wrong");
Program depth_program;
depth_program.stage = ShaderType::Pixel;
depth_program.blocks.resize(1);
depth_program.blocks[0].instructions = {Export(4, ExportTargetKind::MrtZ, 0, 0x1)};
Prepare(depth_program);
Check(CollectShaderInfo(depth_program, {.pixel = &pixel}, &error), error.c_str());
Check(depth_program.info.outputs.size() == 1 &&
FindOutput(depth_program.info, StageOutputKind::Depth) != nullptr &&
FindOutput(depth_program.info, StageOutputKind::SampleMask) == nullptr,
"depth-only MRTZ export incorrectly enabled sample-mask output");
}
void TestShaderInfoCollectionIsTransactional() {
Program program;
program.stage = ShaderType::Compute;
StageInput sentinel;
sentinel.kind = StageInputKind::Parameter;
sentinel.location = 9;
program.info.inputs.push_back(sentinel);
const auto info = program.info;
std::string error;
Check(!CollectShaderInfo(program, {}, &error) &&
error.find("not tracked") != std::string::npos && program.info == info &&
!program.shader_info_complete,
"pre-track shader info collection mutated program state");
Prepare(program);
const auto tracked_info = program.info;
const auto srt_complete = program.srt_plan_complete;
const auto patching_complete = program.srt_patching_complete;
Check(!CollectShaderInfo(program, {}, &error) &&
error.find("requires compute metadata") != std::string::npos &&
program.info == tracked_info && !program.shader_info_complete &&
program.resource_tracking_complete && program.srt_plan_complete == srt_complete &&
program.srt_patching_complete == patching_complete,
"missing stage metadata committed incomplete shader info");
program.stage = ShaderType::Unknown;
Check(!CollectShaderInfo(program, {}, &error) &&
error.find("unsupported shader stage") != std::string::npos &&
program.info == tracked_info && !program.shader_info_complete &&
program.resource_tracking_complete && program.srt_plan_complete == srt_complete &&
program.srt_patching_complete == patching_complete,
"unsupported-stage shader info collection was not transactional");
}
void TestShaderInfoMetadataValidation() {
std::string error;
Program vertex_program;
vertex_program.stage = ShaderType::Vertex;
vertex_program.blocks.resize(1);
Instruction bad_input;
bad_input.op = Opcode::LoadInputF32;
bad_input.input_info.attr = 0;
bad_input.input_info.chan = 4;
vertex_program.blocks[0].instructions = {bad_input};
Prepare(vertex_program);
ShaderVertexInputInfo vertex;
vertex.resources_num = 1;
const auto vertex_info = vertex_program.info;
Check(!CollectShaderInfo(vertex_program, {.vertex = &vertex}, &error) &&
error.find("vertex input reference") != std::string::npos &&
vertex_program.info == vertex_info && !vertex_program.shader_info_complete,
"out-of-range vertex channel produced immutable malformed info");
vertex.resources_num = -1;
Check(!CollectShaderInfo(vertex_program, {.vertex = &vertex}, &error) &&
error.find("vertex resource count") != std::string::npos &&
vertex_program.info == vertex_info && !vertex_program.shader_info_complete,
"negative vertex resource count produced immutable malformed info");
Program pixel_program;
pixel_program.stage = ShaderType::Pixel;
pixel_program.blocks.resize(1);
Prepare(pixel_program);
ShaderPixelInputInfo pixel;
pixel.input_num = 33;
const auto pixel_info = pixel_program.info;
Check(!CollectShaderInfo(pixel_program, {.pixel = &pixel}, &error) &&
error.find("pixel input count") != std::string::npos &&
pixel_program.info == pixel_info && !pixel_program.shader_info_complete,
"out-of-range pixel input count produced immutable malformed info");
Program compute_program;
compute_program.stage = ShaderType::Compute;
compute_program.blocks.resize(1);
Prepare(compute_program);
ShaderComputeInputInfo compute;
compute.thread_ids_num = 4;
const auto compute_info = compute_program.info;
Check(!CollectShaderInfo(compute_program, {.compute = &compute}, &error) &&
error.find("thread ID count") != std::string::npos &&
compute_program.info == compute_info && !compute_program.shader_info_complete,
"out-of-range compute metadata produced immutable malformed info");
}
void TestTrackingRequiresSrtPatching() {
Program program;
program.blocks.resize(1);
program.blocks[0].instructions = {BufferUse(4, 0)};
std::string error;
Check(BuildScalarProvenance(program, &error) && BuildSrtPlan(program, &error), error.c_str());
const auto source = program.blocks[0].instructions[0].memory.resource_source;
Check(!TrackResources(program, &error) &&
error.find("SRT reads were not patched") != std::string::npos &&
program.blocks[0].instructions[0].memory.resource_source == source &&
!program.resource_tracking_complete,
"resource tracking bypassed SRT patch completion");
Check(PatchSrtReads(program, &error) && TrackResources(program, &error), error.c_str());
}
void TestDynamicSrtReadRemainsExplicit() {
Program program;
program.blocks.resize(1);
auto& insts = program.blocks[0].instructions;
for (uint32_t i = 0; i < 8; i++) {
auto load = ScalarLoad(i * 4, i, 16, i * 4);
load.src[0] = Sgpr(20);
load.src_count = 1;
insts.push_back(load);
}
insts.push_back(ImageUse(0x40, Opcode::ImageStore, ResourceKind::StorageImage,
Decoder::ImageDimension::Dim2D));
std::string error;
Check(BuildScalarProvenance(program, &error) && BuildSrtPlan(program, &error) &&
PatchSrtReads(program, &error),
error.c_str());
Check(program.srt.reads.empty() && program.srt.dynamic_reads.size() == 8 &&
program.srt_patching_complete,
"dynamic SRT offsets were incorrectly assigned fixed flat slots");
for (uint32_t i = 0; i < 8; i++) {
Check(insts[i].op == Opcode::SLoadDword &&
insts[i].memory.kind == ResourceKind::ScalarBuffer,
"dynamic SRT read was rewritten as an immediate flat load");
}
Check(TrackResources(program, &error), error.c_str());
}
void TestSrtPatchingFailureIsTransactional() {
Program program;
program.blocks.resize(1);
auto& insts = program.blocks[0].instructions;
for (uint32_t i = 0; i < 4; i++) {
insts.push_back(ScalarLoad(i * 4, i, 16, i * 4));
}
insts.push_back(BufferUse(0x20, 0));
std::string error;
Check(BuildScalarProvenance(program, &error) && BuildSrtPlan(program, &error) &&
program.srt.reads.size() == 4,
error.c_str());
insts[0].scalar_value = ScalarProvenance::Undefined;
const auto instructions = insts;
const auto provenance = program.provenance;
const auto srt = program.srt;
Check(!PatchSrtReads(program, &error) &&
error.find("no scalar-load producer") != std::string::npos && insts == instructions &&
program.provenance == provenance && program.srt == srt &&
!program.srt_patching_complete,
"failed SRT patching partially changed the program");
}
void TestScalarMemoryGroupsSnapshotOperands() {
const auto CheckGroup = [](bool buffer) {
Program program;
program.blocks.resize(1);
auto& insts = program.blocks[0].instructions;
for (uint32_t i = 0; i < 4; i++) {
insts.push_back(buffer ? ScalarBufferLoad(4, 16 + i, 4, i * 4)
: ScalarLoad(4, 16 + i, 16, i * 4));
}
MakeScalarMemoryGroup(&insts);
insts.push_back(BufferUse(8, 16));
std::string error;
Check(BuildScalarProvenance(program, &error) && BuildSrtPlan(program, &error) &&
program.srt.reads.size() == 4 && PatchSrtReads(program, &error),
error.c_str());
for (uint32_t i = 0; i < 4; i++) {
Check(insts[i].op == Opcode::LoadSrtDword && insts[i].src[0].imm == i,
"overlapping scalar-memory group did not snapshot its operands");
}
};
CheckGroup(false);
CheckGroup(true);
const auto CheckOffsetOverlap = [](bool buffer) {
Program program;
program.blocks.resize(1);
auto& insts = program.blocks[0].instructions;
for (uint32_t i = 0; i < 4; i++) {
auto load =
buffer ? ScalarBufferLoad(4, 20 + i, 4, i * 4) : ScalarLoad(4, 20 + i, 16, i * 4);
load.src[0] = Sgpr(20);
load.src_count = 1;
insts.push_back(load);
}
MakeScalarMemoryGroup(&insts);
insts.push_back(BufferUse(8, 20));
std::string error;
Check(BuildScalarProvenance(program, &error) && BuildSrtPlan(program, &error) &&
program.srt.dynamic_reads.size() == 4 && PatchSrtReads(program, &error),
error.c_str());
for (uint32_t i = 0; i < 4; i++) {
const auto value = insts[i].scalar_value;
const auto& node = program.provenance.values[value];
const auto offset_arg = buffer ? 4u : 2u;
Check(program.provenance.values[node.args[offset_arg]].op == ScalarValueOp::UserData &&
program.provenance.values[node.args[offset_arg]].imm == 20 &&
insts[i].op == (buffer ? Opcode::SBufferLoadDword : Opcode::SLoadDword),
"overlapping scalar-memory offset was read after a component write");
}
};
CheckOffsetOverlap(false);
CheckOffsetOverlap(true);
}
void TestSrtPatchingHandlesGvnAndMoveForwarding() {
Program program;
program.blocks.resize(1);
auto& insts = program.blocks[0].instructions;
for (uint32_t copy = 0; copy < 2; copy++) {
for (uint32_t i = 0; i < 4; i++) {
insts.push_back(ScalarLoad(copy * 0x20 + i * 4, copy * 4 + i, 16, i * 4));
}
}
for (uint32_t i = 0; i < 4; i++) {
insts.push_back(Move(0x40 + i * 4, 8 + i, i));
}
insts.push_back(BufferUse(0x60, 4));
insts.push_back(BufferUse(0x64, 8));
std::string error;
Check(BuildScalarProvenance(program, &error) && BuildSrtPlan(program, &error) &&
program.srt.reads.size() == 4 && PatchSrtReads(program, &error),
error.c_str());
for (uint32_t i = 0; i < 8; i++) {
Check(insts[i].op == Opcode::LoadSrtDword && insts[i].src[0].imm == i % 4,
"all producers of a GVN'd SRT read were not patched");
}
for (uint32_t i = 8; i < 12; i++) {
Check(insts[i].op == Opcode::MoveU32,
"move forwarding was incorrectly rewritten as an SRT load");
}
}
void TestSrtPatchingHandlesCfgProducers() {
Program program;
program.blocks.resize(4);
program.blocks[0].successors = {1, 2};
program.blocks[1].predecessors = {0};
program.blocks[1].successors = {3};
program.blocks[2].predecessors = {0};
program.blocks[2].successors = {3};
program.blocks[3].predecessors = {1, 2};
for (uint32_t block = 1; block <= 2; block++) {
for (uint32_t i = 0; i < 4; i++) {
program.blocks[block].instructions.push_back(
ScalarLoad(block * 0x20 + i * 4, i, 16, i * 4));
}
}
program.blocks[3].instructions = {BufferUse(0x60, 0)};
std::string error;
Check(BuildScalarProvenance(program, &error) && BuildSrtPlan(program, &error) &&
program.srt.reads.size() == 4 && PatchSrtReads(program, &error),
error.c_str());
for (uint32_t block = 1; block <= 2; block++) {
for (uint32_t i = 0; i < 4; i++) {
const auto& inst = program.blocks[block].instructions[i];
Check(inst.op == Opcode::LoadSrtDword && inst.src[0].imm == i,
"CFG-equivalent SRT producer was not patched");
}
}
}
void TestSrtPatchPlanValidation() {
Program program;
program.blocks.resize(1);
auto& insts = program.blocks[0].instructions;
for (uint32_t i = 0; i < 4; i++) {
insts.push_back(ScalarLoad(i * 4, i, 16, i * 4));
}
insts.push_back(BufferUse(0x20, 0));
std::string error;
Check(BuildScalarProvenance(program, &error) && BuildSrtPlan(program, &error), error.c_str());
program.srt.reads = {program.srt.reads[0], program.srt.reads[0]};
const auto instructions = insts;
Check(!PatchSrtReads(program, &error) &&
error.find("dense value-to-offset bijection") != std::string::npos &&
insts == instructions && !program.srt_patching_complete,
"duplicate SRT flat slots were accepted or partially patched");
Program dynamic;
dynamic.blocks.resize(1);
dynamic.blocks[0].instructions = {BufferUse(4, 0)};
Check(BuildScalarProvenance(dynamic, &error) && BuildSrtPlan(dynamic, &error), error.c_str());
dynamic.srt.dynamic_sources = {ScalarProvenance::Undefined};
Check(!PatchSrtReads(dynamic, &error) &&
error.find("invalid dynamic descriptor source") != std::string::npos &&
!dynamic.srt_patching_complete,
"undefined dynamic descriptor source was accepted");
}
void TestMaterializationSharesReadConstEvaluation() {
Program program;
program.stage = ShaderType::Pixel;
program.shader_hash = 0x10203040;
program.blocks.resize(1);
auto& insts = program.blocks[0].instructions;
for (uint32_t i = 0; i < 8; i++) {
insts.push_back(ScalarLoad(i * 4, i, 16, i * 4));
}
for (uint32_t i = 0; i < 4; i++) {
insts.push_back(MoveImmediate(0x20 + i * 4, 8 + i, 0xa0 + i));
}
insts.push_back(
ImageUse(0x40, Opcode::ImageSample, ResourceKind::Image, Decoder::ImageDimension::Dim2D));
insts.push_back(ImageUse(0x44, Opcode::ImageStore, ResourceKind::StorageImage,
Decoder::ImageDimension::Dim2D));
Prepare(program);
Check(program.info.images.size() == 2 && program.info.samplers.size() == 1,
"materialization test did not preserve sampled/storage view topology");
Check(program.srt.reads.size() == 8 && program.srt_patching_complete,
"immediate descriptor reads did not produce a compact SRT patch plan");
for (uint32_t i = 0; i < 8; i++) {
Check(insts[i].op == Opcode::LoadSrtDword && insts[i].src_count == 1 &&
insts[i].src[0].kind == OperandKind::ImmediateU32 && insts[i].src[0].imm == i &&
insts[i].memory.kind == ResourceKind::None,
"immediate SRT read was not patched to its dense flat-buffer slot");
}
std::string error;
Check(!PatchSrtReads(program, &error) && error.find("already patched") != std::string::npos,
"repeated SRT patching was accepted");
TestMemory memory;
for (uint32_t i = 0; i < memory.words.size(); i++) {
memory.words[i] = 0x100 + i;
}
memory.words[1] |= Prospero::GpuEnumValue(Prospero::BufferFormat::k8UNorm) << 20u;
memory.words[3] |= Prospero::GpuEnumValue(Prospero::ImageType::kColor2D) << 28u;
std::array<uint32_t, 32> user_data {};
user_data[16] = static_cast<uint32_t>(memory.base);
user_data[17] = static_cast<uint32_t>(memory.base >> 32u);
SrtRuntime runtime {user_data, 0, ReadTestMemory, &memory};
ResourceSnapshot snapshot;
error.clear();
Check(MaterializeResources(program, runtime, snapshot, &error), error.c_str());
Check(snapshot.buffers.empty() && snapshot.images.size() == 2 &&
snapshot.samplers.size() == 1 && snapshot.images[0] == snapshot.images[1],
"dense runtime snapshot did not preserve resource order or aliases");
Check(snapshot.flattened_srt.size() == 8 &&
std::equal(snapshot.flattened_srt.begin(), snapshot.flattened_srt.end(),
memory.words.begin()) &&
snapshot.user_data == std::vector<uint32_t>(user_data.begin(), user_data.end()),
"runtime snapshot omitted flattened SRT or current user data");
Check(memory.reads == 8, "aliased image views repeated ReadConst evaluation "
"instead of sharing it");
for (uint32_t i = 0; i < 8; i++) {
Check(snapshot.images[0].dwords[i] == memory.words[i],
"materialized image descriptor contains the wrong dword");
}
}
void TestInvalidImagesMaterializeAsNull() {
Program sampled;
sampled.stage = ShaderType::Pixel;
sampled.user_data_count = 8;
sampled.blocks.resize(1);
sampled.blocks[0].instructions = {
ImageUse(0x40, Opcode::ImageLoad, ResourceKind::Image, Decoder::ImageDimension::Dim2D)};
Prepare(sampled);
Check(sampled.info.images.size() == 1 && sampled.info.samplers.empty(),
"sampled-image normalization test has unexpected resource topology");
constexpr std::array<uint32_t, 8> stale = {0x00000004, 0x00000004, 0xc0061060, 0x06000514,
0x20010000, 0xa4580290, 0x00000004, 0x00000001};
std::string error;
for (uint32_t type = 0; type < 8; type++) {
auto descriptor = stale;
descriptor[1] |= Prospero::GpuEnumValue(Prospero::BufferFormat::k8UNorm) << 20u;
descriptor[3] = (descriptor[3] & 0x0fffffffu) | (type << 28u);
ResourceSnapshot snapshot;
Check(MaterializeResources(sampled, {descriptor}, snapshot, &error) &&
snapshot.images.size() == 1 &&
std::all_of(snapshot.images[0].dwords.begin(), snapshot.images[0].dwords.end(),
[](uint32_t word) { return word == 0; }) &&
ValidateResourceSpecialization(sampled, snapshot, &error),
error.c_str());
}
constexpr std::array<uint32_t, 8> packet = {0xc0071058, 0xe80eeeb8, 0x00000000, 0xffffffff,
0xffffffff, 0x00000001, 0x00000000, 0x00000113};
ResourceSnapshot packet_snapshot;
Check(MaterializeResources(sampled, {packet}, packet_snapshot, &error) &&
std::all_of(packet_snapshot.images[0].dwords.begin(),
packet_snapshot.images[0].dwords.end(),
[](uint32_t word) { return word == 0; }),
"invalid MSAA image words were not normalized to null");
struct MsaaCase {
uint32_t base_level;
uint32_t fragments;
uint32_t max_mip;
bool valid;
};
constexpr std::array msaa_cases = {
MsaaCase {0, 1, 1, true}, MsaaCase {0, 2, 2, true}, MsaaCase {0, 3, 3, true},
MsaaCase {1, 1, 1, false}, MsaaCase {0, 2, 1, false}, MsaaCase {0, 0, 0, false},
MsaaCase {0, 4, 4, false},
};
constexpr std::array msaa_types = {
Prospero::ImageType::kColor2DMsaa,
Prospero::ImageType::kColor2DMsaaArray,
};
for (const auto type: msaa_types) {
for (const auto& test: msaa_cases) {
std::array<uint32_t, 8> msaa {};
msaa[0] = 1;
msaa[1] = 36u << 20u;
msaa[3] = (Prospero::GpuEnumValue(type) << 28u) | (test.base_level << 12u) |
(test.fragments << 16u);
msaa[5] = test.max_mip << 4u;
ResourceSnapshot msaa_snapshot;
const auto materialized = MaterializeResources(sampled, {msaa}, msaa_snapshot, &error);
const auto preserved =
materialized &&
std::equal(msaa.begin(), msaa.end(), msaa_snapshot.images[0].dwords.begin());
const auto is_null =
materialized && std::all_of(msaa_snapshot.images[0].dwords.begin(),
msaa_snapshot.images[0].dwords.end(),
[](uint32_t word) { return word == 0; });
Check(materialized && (test.valid ? preserved : is_null),
"MSAA image descriptor validity mismatch");
}
}
std::array<uint32_t, 8> invalid_format {};
invalid_format[0] = 1;
invalid_format[3] = Prospero::GpuEnumValue(Prospero::ImageType::kColor2D) << 28u;
ResourceSnapshot invalid_format_snapshot;
Check(MaterializeResources(sampled, {invalid_format}, invalid_format_snapshot, &error) &&
std::all_of(invalid_format_snapshot.images[0].dwords.begin(),
invalid_format_snapshot.images[0].dwords.end(),
[](uint32_t word) { return word == 0; }),
"invalid-format image descriptor was not normalized to null");
auto valid = stale;
valid[1] |= Prospero::GpuEnumValue(Prospero::BufferFormat::k8UNorm) << 20u;
valid[3] =
(valid[3] & 0x0fffffffu) | (Prospero::GpuEnumValue(Prospero::ImageType::kColor2D) << 28u);
ResourceSnapshot valid_snapshot;
Check(MaterializeResources(sampled, {valid}, valid_snapshot, &error) &&
std::equal(valid.begin(), valid.end(), valid_snapshot.images[0].dwords.begin()),
"valid sampled image descriptor was normalized");
Program storage;
storage.stage = ShaderType::Compute;
storage.user_data_count = 8;
storage.blocks.resize(1);
storage.blocks[0].instructions = {ImageUse(0x40, Opcode::ImageStore, ResourceKind::StorageImage,
Decoder::ImageDimension::Dim2D)};
Prepare(storage);
ResourceSnapshot storage_snapshot;
Check(MaterializeResources(storage, {stale}, storage_snapshot, &error) &&
std::all_of(storage_snapshot.images[0].dwords.begin(),
storage_snapshot.images[0].dwords.end(),