forked from sonicnext-dev/MarathonRecomp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo.cpp
More file actions
8174 lines (7041 loc) · 301 KB
/
Copy pathvideo.cpp
File metadata and controls
8174 lines (7041 loc) · 301 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 "video.h"
#include "imgui/imgui_common.h"
#include "imgui/imgui_snapshot.h"
#include "imgui/imgui_font_builder.h"
#include <app.h>
#include <bc_diff.h>
#include <cpu/guest_thread.h>
#include <cstdint>
#include <cstdio>
#include <decompressor.h>
#include <kernel/function.h>
#include <kernel/heap.h>
#include <hid/hid.h>
#include <kernel/memory.h>
#include <kernel/xdbf.h>
#include <plume_render_interface.h>
#include <res/bc_diff/button_bc_diff.bin.h>
#include <res/font/im_font_atlas.dds.h>
#include <shader/shader_cache.h>
#include <Marathon.h>
#include <ui/achievement_menu.h>
#include <ui/achievement_overlay.h>
#include <ui/button_window.h>
#include <ui/fader.h>
#include <ui/imgui_utils.h>
#include <ui/installer_wizard.h>
#include <ui/message_window.h>
#include <ui/options_menu.h>
#include <ui/game_window.h>
#include <ui/black_bar.h>
#include <patches/aspect_ratio_patches.h>
#include <user/config.h>
#include <sdl_listener.h>
#include <xxHashMap.h>
#include <os/process.h>
#if defined(ASYNC_PSO_DEBUG) || defined(PSO_CACHING)
#include <magic_enum/magic_enum.hpp>
#endif
#include "../../tools/XenosRecomp/XenosRecomp/shader_common.h"
#ifdef MARATHON_RECOMP_D3D12
#include "shader/hlsl/blend_color_alpha_ps.hlsl.dxil.h"
#include "shader/hlsl/copy_vs.hlsl.dxil.h"
#include "shader/hlsl/copy_color_ps.hlsl.dxil.h"
#include "shader/hlsl/copy_depth_ps.hlsl.dxil.h"
#include "shader/hlsl/csd_filter_ps.hlsl.dxil.h"
#include "shader/hlsl/csd_no_tex_vs.hlsl.dxil.h"
#include "shader/hlsl/csd_vs.hlsl.dxil.h"
#include "shader/hlsl/enhanced_burnout_blur_vs.hlsl.dxil.h"
#include "shader/hlsl/enhanced_burnout_blur_ps.hlsl.dxil.h"
#include "shader/hlsl/gamma_correction_ps.hlsl.dxil.h"
#include "shader/hlsl/gaussian_blur_3x3.hlsl.dxil.h"
#include "shader/hlsl/gaussian_blur_5x5.hlsl.dxil.h"
#include "shader/hlsl/gaussian_blur_7x7.hlsl.dxil.h"
#include "shader/hlsl/gaussian_blur_9x9.hlsl.dxil.h"
#include "shader/hlsl/imgui_ps.hlsl.dxil.h"
#include "shader/hlsl/imgui_vs.hlsl.dxil.h"
#include "shader/hlsl/resolve_msaa_color_2x.hlsl.dxil.h"
#include "shader/hlsl/resolve_msaa_color_4x.hlsl.dxil.h"
#include "shader/hlsl/resolve_msaa_color_8x.hlsl.dxil.h"
#include "shader/hlsl/resolve_msaa_depth_2x.hlsl.dxil.h"
#include "shader/hlsl/resolve_msaa_depth_4x.hlsl.dxil.h"
#include "shader/hlsl/resolve_msaa_depth_8x.hlsl.dxil.h"
#endif
#ifdef MARATHON_RECOMP_METAL
#include "shader/msl/blend_color_alpha_ps.metal.metallib.h"
#include "shader/msl/copy_vs.metal.metallib.h"
#include "shader/msl/copy_color_ps.metal.metallib.h"
#include "shader/msl/copy_depth_ps.metal.metallib.h"
#include "shader/msl/csd_filter_ps.metal.metallib.h"
#include "shader/msl/csd_no_tex_vs.metal.metallib.h"
#include "shader/msl/csd_vs.metal.metallib.h"
#include "shader/msl/enhanced_burnout_blur_vs.metal.metallib.h"
#include "shader/msl/enhanced_burnout_blur_ps.metal.metallib.h"
#include "shader/msl/gamma_correction_ps.metal.metallib.h"
#include "shader/msl/gaussian_blur_3x3.metal.metallib.h"
#include "shader/msl/gaussian_blur_5x5.metal.metallib.h"
#include "shader/msl/gaussian_blur_7x7.metal.metallib.h"
#include "shader/msl/gaussian_blur_9x9.metal.metallib.h"
#include "shader/msl/imgui_ps.metal.metallib.h"
#include "shader/msl/imgui_vs.metal.metallib.h"
#include "shader/msl/resolve_msaa_color_2x.metal.metallib.h"
#include "shader/msl/resolve_msaa_color_4x.metal.metallib.h"
#include "shader/msl/resolve_msaa_color_8x.metal.metallib.h"
#include "shader/msl/resolve_msaa_depth_2x.metal.metallib.h"
#include "shader/msl/resolve_msaa_depth_4x.metal.metallib.h"
#include "shader/msl/resolve_msaa_depth_8x.metal.metallib.h"
#endif
#include "shader/hlsl/blend_color_alpha_ps.hlsl.spirv.h"
#include "shader/hlsl/copy_vs.hlsl.spirv.h"
#include "shader/hlsl/copy_color_ps.hlsl.spirv.h"
#include "shader/hlsl/copy_depth_ps.hlsl.spirv.h"
#include "shader/hlsl/csd_filter_ps.hlsl.spirv.h"
#include "shader/hlsl/csd_no_tex_vs.hlsl.spirv.h"
#include "shader/hlsl/csd_vs.hlsl.spirv.h"
#include "shader/hlsl/enhanced_burnout_blur_vs.hlsl.spirv.h"
#include "shader/hlsl/enhanced_burnout_blur_ps.hlsl.spirv.h"
#include "shader/hlsl/gamma_correction_ps.hlsl.spirv.h"
#include "shader/hlsl/gaussian_blur_3x3.hlsl.spirv.h"
#include "shader/hlsl/gaussian_blur_5x5.hlsl.spirv.h"
#include "shader/hlsl/gaussian_blur_7x7.hlsl.spirv.h"
#include "shader/hlsl/gaussian_blur_9x9.hlsl.spirv.h"
#include "shader/hlsl/imgui_ps.hlsl.spirv.h"
#include "shader/hlsl/imgui_vs.hlsl.spirv.h"
#include "shader/hlsl/resolve_msaa_color_2x.hlsl.spirv.h"
#include "shader/hlsl/resolve_msaa_color_4x.hlsl.spirv.h"
#include "shader/hlsl/resolve_msaa_color_8x.hlsl.spirv.h"
#include "shader/hlsl/resolve_msaa_depth_2x.hlsl.spirv.h"
#include "shader/hlsl/resolve_msaa_depth_4x.hlsl.spirv.h"
#include "shader/hlsl/resolve_msaa_depth_8x.hlsl.spirv.h"
#ifdef _WIN32
extern "C"
{
__declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
}
#endif
namespace plume
{
#ifdef MARATHON_RECOMP_D3D12
extern std::unique_ptr<RenderInterface> CreateD3D12Interface();
#endif
#ifdef MARATHON_RECOMP_METAL
extern std::unique_ptr<RenderInterface> CreateMetalInterface();
#endif
#ifdef PLUME_SDL_VULKAN_ENABLED
extern std::unique_ptr<RenderInterface> CreateVulkanInterface(RenderWindow sdlWindow);
#else
extern std::unique_ptr<RenderInterface> CreateVulkanInterface();
#endif
static std::unique_ptr<RenderInterface> CreateVulkanInterfaceWrapper() {
#ifdef PLUME_SDL_VULKAN_ENABLED
return CreateVulkanInterface(GameWindow::s_renderWindow);
#else
return CreateVulkanInterface();
#endif
}
}
using namespace plume;
#pragma pack(push, 1)
struct PipelineState
{
GuestShader* vertexShader = nullptr;
GuestShader* pixelShader = nullptr;
GuestVertexDeclaration* vertexDeclaration = nullptr;
bool zEnable = true;
bool zWriteEnable = true;
bool stencilEnable = false;
bool stencilTwoSided = false;
RenderBlend srcBlend = RenderBlend::ONE;
RenderBlend destBlend = RenderBlend::ZERO;
RenderCullMode cullMode = RenderCullMode::NONE;
RenderFrontFace frontFace = RenderFrontFace::CLOCKWISE;
RenderComparisonFunction zFunc = RenderComparisonFunction::LESS;
RenderComparisonFunction stencilFunc = RenderComparisonFunction::ALWAYS;
RenderStencilOp stencilFail = RenderStencilOp::KEEP;
RenderStencilOp stencilZFail = RenderStencilOp::KEEP;
RenderStencilOp stencilPass = RenderStencilOp::KEEP;
RenderComparisonFunction stencilFuncCCW = RenderComparisonFunction::ALWAYS;
RenderStencilOp stencilFailCCW = RenderStencilOp::KEEP;
RenderStencilOp stencilZFailCCW = RenderStencilOp::KEEP;
RenderStencilOp stencilPassCCW = RenderStencilOp::KEEP;
uint32_t stencilMask = 0xFFFFFFFF;
uint32_t stencilWriteMask = 0xFFFFFFFF;
uint32_t stencilRef = 0;
bool alphaBlendEnable = false;
RenderBlendOperation blendOp = RenderBlendOperation::ADD;
float slopeScaledDepthBias = 0.0f;
int32_t depthBias = 0;
RenderBlend srcBlendAlpha = RenderBlend::ONE;
RenderBlend destBlendAlpha = RenderBlend::ZERO;
RenderBlendOperation blendOpAlpha = RenderBlendOperation::ADD;
uint32_t colorWriteEnable = uint32_t(RenderColorWriteEnable::ALL);
RenderPrimitiveTopology primitiveTopology = RenderPrimitiveTopology::TRIANGLE_LIST;
uint8_t vertexStrides[16]{};
RenderFormat renderTargetFormat{};
RenderFormat depthStencilFormat{};
RenderSampleCounts sampleCount = RenderSampleCount::COUNT_1;
bool enableAlphaToCoverage = false;
uint32_t specConstants = 0;
};
#pragma pack(pop)
struct UploadAllocation
{
const RenderBuffer* buffer;
uint64_t offset;
uint8_t* memory;
uint64_t deviceAddress;
};
struct SharedConstants
{
uint32_t texture2DIndices[16]{};
uint32_t texture2DArrayIndices[16]{};
uint32_t textureCubeIndices[16]{};
uint32_t samplerIndices[16]{};
uint32_t booleans{};
uint32_t swappedTexcoords{};
uint32_t swappedNormals{};
uint32_t swappedBinormals{};
uint32_t swappedTangents{};
uint32_t swappedBlendWeights{};
float halfPixelOffsetX{};
float halfPixelOffsetY{};
float clipPlane[4]{};
bool clipPlaneEnabled{};
float alphaThreshold{};
uint32_t conditionalSurveyIndex{};
uint32_t conditionalRenderingIndex{};
};
// Depth bias values here are only used when the render device has
// dynamic depth bias capability enabled. Otherwise, they get unused
// and the values get assigned in the pipeline state instead.
static GuestSurface* g_renderTarget;
static GuestSurface* g_depthStencil;
static RenderFramebuffer* g_framebuffer;
static RenderViewport g_viewport(0.0f, 0.0f, 1280.0f, 720.0f);
static PipelineState g_pipelineState;
static int32_t g_depthBias;
static float g_slopeScaledDepthBias;
static uint32_t g_vertexShaderConstants[0x400];
static uint32_t g_pixelShaderConstants[0x380];
static SharedConstants g_sharedConstants;
static GuestTexture* g_textures[16];
static RenderSamplerDesc g_samplerDescs[16];
static bool g_scissorTestEnable = false;
static RenderRect g_scissorRect;
static RenderVertexBufferView g_vertexBufferViews[16];
static RenderInputSlot g_inputSlots[16];
static RenderIndexBufferView g_indexBufferView({}, 0, RenderFormat::R16_UINT);
struct DirtyStates
{
bool renderTargetAndDepthStencil;
bool viewport;
bool pipelineState;
bool depthBias;
bool sharedConstants;
bool scissorRect;
bool vertexShaderConstants;
uint8_t vertexStreamFirst;
uint8_t vertexStreamLast;
bool indices;
bool pixelShaderConstants;
DirtyStates(bool value)
: renderTargetAndDepthStencil(value)
, viewport(value)
, pipelineState(value)
, depthBias(value)
, sharedConstants(value)
, scissorRect(value)
, vertexShaderConstants(value)
, vertexStreamFirst(value ? 0 : 255)
, vertexStreamLast(value ? 15 : 0)
, indices(value)
, pixelShaderConstants(value)
{
}
};
static DirtyStates g_dirtyStates(true);
template<typename T>
static void SetDirtyValue(bool& dirtyState, T& dest, const T& src)
{
if (dest != src)
{
dest = src;
dirtyState = true;
}
}
static constexpr size_t PROFILER_VALUE_COUNT = 256;
static size_t g_profilerValueIndex;
struct Profiler
{
std::atomic<double> value;
double values[PROFILER_VALUE_COUNT];
std::chrono::steady_clock::time_point start;
void Begin()
{
start = std::chrono::steady_clock::now();
}
void End()
{
value = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count();
}
void Set(double v)
{
value = v;
}
void Reset()
{
End();
Begin();
}
double UpdateAndReturnAverage()
{
values[g_profilerValueIndex] = value;
return std::accumulate(values, values + PROFILER_VALUE_COUNT, 0.0) / PROFILER_VALUE_COUNT;
}
};
static double g_applicationValues[PROFILER_VALUE_COUNT];
static Profiler g_gpuFrameProfiler;
static Profiler g_presentProfiler;
static Profiler g_frameFenceProfiler;
static Profiler g_presentWaitProfiler;
static Profiler g_swapChainAcquireProfiler;
static bool g_profilerVisible;
static bool g_profilerWasToggled;
#if !defined(MARATHON_RECOMP_D3D12) && !defined(MARATHON_RECOMP_METAL)
static constexpr Backend g_backend = Backend::VULKAN;
#else
static Backend g_backend;
#endif
static bool g_triangleStripWorkaround = false;
static std::unique_ptr<RenderInterface> g_interface;
static std::unique_ptr<RenderDevice> g_device;
static RenderDeviceCapabilities g_capabilities;
static constexpr size_t NUM_FRAMES = 2;
static constexpr size_t NUM_QUERIES = 2;
static uint32_t g_frame = 0;
static uint32_t g_nextFrame = 1;
static std::unique_ptr<RenderCommandQueue> g_queue;
static std::unique_ptr<RenderCommandList> g_commandLists[NUM_FRAMES];
static std::unique_ptr<RenderCommandFence> g_commandFences[NUM_FRAMES];
static std::unique_ptr<RenderQueryPool> g_queryPools[NUM_FRAMES];
static bool g_commandListStates[NUM_FRAMES];
static Mutex g_copyMutex;
static std::unique_ptr<RenderCommandQueue> g_copyQueue;
static std::unique_ptr<RenderCommandList> g_copyCommandList;
static std::unique_ptr<RenderCommandFence> g_copyCommandFence;
static Mutex g_discardMutex;
static std::unique_ptr<RenderCommandList> g_discardCommandList;
static std::unique_ptr<RenderCommandFence> g_discardCommandFence;
static std::unique_ptr<RenderSwapChain> g_swapChain;
static bool g_swapChainValid;
static constexpr RenderFormat BACKBUFFER_FORMAT = RenderFormat::B8G8R8A8_UNORM;
static std::unique_ptr<RenderCommandSemaphore> g_acquireSemaphores[NUM_FRAMES];
static std::unique_ptr<RenderCommandSemaphore> g_renderSemaphores[NUM_FRAMES];
static uint32_t g_backBufferIndex;
static std::unique_ptr<GuestSurface> g_backBufferHolder;
static GuestSurface* g_backBuffer;
static std::unique_ptr<RenderTexture> g_intermediaryBackBufferTexture;
static uint32_t g_intermediaryBackBufferTextureWidth;
static uint32_t g_intermediaryBackBufferTextureHeight;
static uint32_t g_intermediaryBackBufferTextureDescriptorIndex;
static std::unique_ptr<RenderPipeline> g_gammaCorrectionPipeline;
static std::unique_ptr<RenderDescriptorSet> g_textureDescriptorSet;
static std::unique_ptr<RenderDescriptorSet> g_samplerDescriptorSet;
static constexpr uint32_t CONDITIONAL_SURVEY_MAX = 64;
static std::unique_ptr<RenderBuffer> g_conditionalSurveyBuffer;
static std::unique_ptr<RenderDescriptorSet> g_conditionalSurveyDescriptorSet;
enum
{
TEXTURE_DESCRIPTOR_NULL_TEXTURE_2D,
TEXTURE_DESCRIPTOR_NULL_TEXTURE_2D_ARRAY,
TEXTURE_DESCRIPTOR_NULL_TEXTURE_CUBE,
TEXTURE_DESCRIPTOR_NULL_COUNT
};
struct TextureDescriptorAllocator
{
Mutex mutex;
uint32_t capacity = TEXTURE_DESCRIPTOR_NULL_COUNT;
std::vector<uint32_t> freed;
uint32_t allocate()
{
std::lock_guard lock(mutex);
uint32_t value;
if (!freed.empty())
{
value = freed.back();
freed.pop_back();
}
else
{
value = capacity;
++capacity;
}
return value;
}
void free(uint32_t value)
{
assert(value != NULL);
std::lock_guard lock(mutex);
freed.push_back(value);
}
};
static std::unique_ptr<RenderTexture> g_blankTextures[TEXTURE_DESCRIPTOR_NULL_COUNT];
static std::unique_ptr<RenderTextureView> g_blankTextureViews[TEXTURE_DESCRIPTOR_NULL_COUNT];
static TextureDescriptorAllocator g_textureDescriptorAllocator;
static std::unique_ptr<RenderPipelineLayout> g_pipelineLayout;
static xxHashMap<std::unique_ptr<RenderPipeline>> g_pipelines;
#ifdef ASYNC_PSO_DEBUG
static std::atomic<uint32_t> g_pipelinesCreatedInRenderThread;
static std::atomic<uint32_t> g_pipelinesCreatedAsynchronously;
static std::atomic<uint32_t> g_pipelinesDropped;
static std::atomic<uint32_t> g_pipelinesCurrentlyCompiling;
static std::string g_pipelineDebugText;
static Mutex g_debugMutex;
#endif
#ifdef PSO_CACHING
static xxHashMap<PipelineState> g_pipelineStatesToCache;
static Mutex g_pipelineCacheMutex;
#endif
static std::atomic<uint32_t> g_compilingPipelineTaskCount;
static std::atomic<uint32_t> g_pendingPipelineTaskCount;
enum class PipelineTaskType
{
Null,
DatabaseData,
PrecompilePipelines,
RecompilePipelines
};
struct PipelineTask
{
PipelineTaskType type{};
// boost::shared_ptr<Hedgehog::Database::CDatabaseData> databaseData;
};
static Mutex g_pipelineTaskMutex;
static std::vector<PipelineTask> g_pipelineTaskQueue;
//static void EnqueuePipelineTask(PipelineTaskType type, const boost::shared_ptr<Hedgehog::Database::CDatabaseData>& databaseData)
//{
// // Precompiled pipelines deliberately do not increment
// // this counter to overlap the compilation with intro logos.
// if (type != PipelineTaskType::PrecompilePipelines)
// ++g_compilingPipelineTaskCount;
//
// {
// std::lock_guard lock(g_pipelineTaskMutex);
// g_pipelineTaskQueue.emplace_back(type, databaseData);
// }
//
// if ((++g_pendingPipelineTaskCount) == 1)
// g_pendingPipelineTaskCount.notify_one();
//}
static const PipelineState g_pipelineStateCache[] =
{
#include "cache/pipeline_state_cache.h"
};
#include "cache/vertex_element_cache.h"
static uint8_t* const g_vertexDeclarationCache[] =
{
#include "cache/vertex_declaration_cache.h"
};
static xxHashMap<std::pair<uint32_t, std::unique_ptr<RenderSampler>>> g_samplerStates;
static Mutex g_vertexDeclarationMutex;
static xxHashMap<GuestVertexDeclaration*> g_vertexDeclarations;
struct UploadBuffer
{
static constexpr size_t SIZE = 16 * 1024 * 1024;
std::unique_ptr<RenderBuffer> buffer;
uint8_t* memory = nullptr;
uint64_t deviceAddress = 0;
};
struct UploadAllocator
{
std::vector<UploadBuffer> buffers;
uint32_t index = 0;
uint32_t offset = 0;
UploadAllocation allocate(uint32_t size, uint32_t alignment)
{
assert(size <= UploadBuffer::SIZE);
offset = (offset + alignment - 1) & ~(alignment - 1);
if (offset + size > UploadBuffer::SIZE)
{
++index;
offset = 0;
}
if (buffers.size() <= index)
buffers.resize(index + 1);
auto& buffer = buffers[index];
if (buffer.buffer == nullptr)
{
buffer.buffer = g_device->createBuffer(RenderBufferDesc::UploadBuffer(UploadBuffer::SIZE, RenderBufferFlag::CONSTANT | RenderBufferFlag::VERTEX | RenderBufferFlag::INDEX | RenderBufferFlag::DEVICE_ADDRESSABLE));
buffer.memory = reinterpret_cast<uint8_t*>(buffer.buffer->map());
buffer.deviceAddress = buffer.buffer->getDeviceAddress();
}
auto ref = buffer.buffer->at(offset);
offset += size;
return { ref.ref, ref.offset, buffer.memory + ref.offset, buffer.deviceAddress + ref.offset };
}
template<bool TByteSwap, typename T>
UploadAllocation allocate(const T* memory, uint32_t size, uint32_t alignment)
{
auto result = allocate(size, alignment);
if constexpr (TByteSwap)
{
auto destination = reinterpret_cast<T*>(result.memory);
for (size_t i = 0; i < size; i += sizeof(T))
{
*destination = ByteSwap(*memory);
++destination;
++memory;
}
}
else
{
memcpy(result.memory, memory, size);
}
return result;
}
void reset()
{
index = 0;
offset = 0;
}
};
static UploadAllocator g_uploadAllocators[NUM_FRAMES];
struct IntermediaryUploadAllocator
{
static constexpr size_t SIZE = 16 * 1024 * 1024;
std::vector<std::unique_ptr<uint8_t[]>> buffers;
uint32_t index = 0;
uint32_t offset = 0;
uint8_t* allocate(uint32_t size)
{
assert(size <= SIZE);
if (offset + size > SIZE)
{
++index;
offset = 0;
}
if (buffers.size() <= index)
buffers.resize(index + 1);
auto& buffer = buffers[index];
if (buffer == nullptr)
buffer = std::make_unique_for_overwrite<uint8_t[]>(SIZE);
auto result = buffer.get() + offset;
offset += ((size + 0xF) & ~0xF);
return result;
}
uint8_t* allocate(const void* memory, uint32_t size)
{
auto result = allocate(size);
memcpy(result, memory, size);
return result;
}
void reset()
{
index = 0;
offset = 0;
}
};
static IntermediaryUploadAllocator g_intermediaryUploadAllocator;
static std::vector<GuestResource*> g_tempResources[NUM_FRAMES];
static std::vector<std::unique_ptr<RenderBuffer>> g_tempBuffers[NUM_FRAMES];
template<GuestPrimitiveType PrimitiveType>
struct PrimitiveIndexData
{
std::vector<uint16_t> indexData;
RenderBufferReference indexBuffer;
uint32_t currentIndexCount = 0;
uint32_t prepare(uint32_t guestPrimCount)
{
uint32_t primCount;
uint32_t indexCountPerPrimitive;
switch (PrimitiveType)
{
case D3DPT_TRIANGLEFAN:
primCount = guestPrimCount - 2;
indexCountPerPrimitive = 3;
break;
case D3DPT_QUADLIST:
primCount = guestPrimCount / 4;
indexCountPerPrimitive = 6;
break;
default:
assert(false && "Unknown primitive type.");
break;
}
uint32_t indexCount = primCount * indexCountPerPrimitive;
if (indexData.size() < indexCount)
{
const size_t oldPrimCount = indexData.size() / indexCountPerPrimitive;
indexData.resize(indexCount);
for (size_t i = oldPrimCount; i < primCount; i++)
{
switch (PrimitiveType)
{
case D3DPT_TRIANGLEFAN:
{
indexData[i * 3 + 0] = 0;
indexData[i * 3 + 1] = static_cast<uint16_t>(i + 1);
indexData[i * 3 + 2] = static_cast<uint16_t>(i + 2);
break;
}
case D3DPT_QUADLIST:
{
indexData[i * 6 + 0] = static_cast<uint16_t>(i * 4 + 0);
indexData[i * 6 + 1] = static_cast<uint16_t>(i * 4 + 1);
indexData[i * 6 + 2] = static_cast<uint16_t>(i * 4 + 2);
indexData[i * 6 + 3] = static_cast<uint16_t>(i * 4 + 0);
indexData[i * 6 + 4] = static_cast<uint16_t>(i * 4 + 2);
indexData[i * 6 + 5] = static_cast<uint16_t>(i * 4 + 3);
break;
}
default:
assert(false && "Unknown primitive type.");
break;
}
}
}
if (indexBuffer == NULL || currentIndexCount < indexCount)
{
auto allocation = g_uploadAllocators[g_frame].allocate<false>(indexData.data(), indexCount * 2, 2);
indexBuffer = allocation.buffer->at(allocation.offset);
currentIndexCount = indexCount;
}
SetDirtyValue(g_dirtyStates.indices, g_indexBufferView.buffer, indexBuffer);
SetDirtyValue(g_dirtyStates.indices, g_indexBufferView.size, indexCount * 2);
SetDirtyValue(g_dirtyStates.indices, g_indexBufferView.format, RenderFormat::R16_UINT);
return indexCount;
}
void reset()
{
indexBuffer = {};
currentIndexCount = 0;
}
};
static PrimitiveIndexData<D3DPT_TRIANGLEFAN> g_triangleFanIndexData;
static PrimitiveIndexData<D3DPT_QUADLIST> g_quadIndexData;
static void DestructTempResources()
{
for (auto resource : g_tempResources[g_frame])
{
switch (resource->type)
{
case ResourceType::Texture:
case ResourceType::VolumeTexture:
case ResourceType::ArrayTexture:
{
const auto texture = reinterpret_cast<GuestTexture*>(resource);
if (texture->mappedMemory != nullptr) {
g_userHeap.Free(texture->mappedMemory);
}
g_textureDescriptorSet->setTexture(texture->descriptorIndex, nullptr, {});
g_textureDescriptorAllocator.free(texture->descriptorIndex);
if (texture->patchedTexture != nullptr)
{
g_textureDescriptorSet->setTexture(texture->patchedTexture->descriptorIndex, nullptr, {});
g_textureDescriptorAllocator.free(texture->patchedTexture->descriptorIndex);
}
texture->~GuestTexture();
break;
}
case ResourceType::VertexBuffer:
case ResourceType::IndexBuffer:
{
const auto buffer = reinterpret_cast<GuestBuffer*>(resource);
if (buffer->mappedMemory != nullptr)
g_userHeap.Free(buffer->mappedMemory);
buffer->~GuestBuffer();
break;
}
case ResourceType::RenderTarget:
case ResourceType::DepthStencil:
{
const auto surface = reinterpret_cast<GuestSurface*>(resource);
if (surface->descriptorIndex != NULL)
{
g_textureDescriptorSet->setTexture(surface->descriptorIndex, nullptr, {});
g_textureDescriptorAllocator.free(surface->descriptorIndex);
}
surface->~GuestSurface();
break;
}
case ResourceType::VertexDeclaration:
reinterpret_cast<GuestVertexDeclaration*>(resource)->~GuestVertexDeclaration();
break;
case ResourceType::VertexShader:
case ResourceType::PixelShader:
{
reinterpret_cast<GuestShader*>(resource)->~GuestShader();
break;
}
}
g_userHeap.Free(resource);
}
g_tempResources[g_frame].clear();
g_tempBuffers[g_frame].clear();
}
static std::thread::id g_presentThreadId = std::this_thread::get_id();
static std::atomic<bool> g_readyForCommands;
// PPC_FUNC_IMPL(__imp__sub_824ECA00);
// PPC_FUNC(sub_824ECA00)
// {
// g_readyForCommands.wait(false);
// g_presentThreadId = std::this_thread::get_id();
// __imp__sub_824ECA00(ctx, base);
// }
static ankerl::unordered_dense::map<RenderTexture*, RenderTextureLayout> g_barrierMap;
static void AddBarrier(GuestBaseTexture* texture, RenderTextureLayout layout)
{
if (texture != nullptr && texture->layout != layout)
{
g_barrierMap[texture->texture] = layout;
texture->layout = layout;
}
}
static std::vector<RenderTextureBarrier> g_barriers;
static void FlushBarriers()
{
if (!g_barrierMap.empty())
{
for (auto& [texture, layout] : g_barrierMap)
g_barriers.emplace_back(texture, layout);
g_commandLists[g_frame]->barriers(RenderBarrierStage::GRAPHICS | RenderBarrierStage::COPY, g_barriers);
g_barrierMap.clear();
g_barriers.clear();
}
}
static std::unique_ptr<uint8_t[]> g_shaderCache;
static std::unique_ptr<uint8_t[]> g_buttonBcDiff;
static void LoadEmbeddedResources()
{
switch (g_backend)
{
case Backend::VULKAN:
g_shaderCache = std::make_unique<uint8_t[]>(g_spirvCacheDecompressedSize);
ZSTD_decompress(g_shaderCache.get(), g_spirvCacheDecompressedSize, g_compressedSpirvCache, g_spirvCacheCompressedSize);
break;
#if defined(MARATHON_RECOMP_D3D12)
case Backend::D3D12:
g_shaderCache = std::make_unique<uint8_t[]>(g_dxilCacheDecompressedSize);
ZSTD_decompress(g_shaderCache.get(), g_dxilCacheDecompressedSize, g_compressedDxilCache, g_dxilCacheCompressedSize);
break;
#elif defined(MARATHON_RECOMP_METAL)
case Backend::METAL:
g_shaderCache = std::make_unique<uint8_t[]>(g_airCacheDecompressedSize);
ZSTD_decompress(g_shaderCache.get(), g_airCacheDecompressedSize, g_compressedAirCache, g_airCacheCompressedSize);
break;
#endif
default:
assert(false);
}
g_buttonBcDiff = decompressZstd(g_button_bc_diff, g_button_bc_diff_uncompressed_size);
}
enum class CsdFilterState
{
Unknown,
On,
Off
};
static CsdFilterState g_csdFilterState;
static ankerl::unordered_dense::set<GuestSurface*> g_pendingSurfaceCopies;
static ankerl::unordered_dense::set<GuestSurface*> g_pendingResolves;
enum class RenderCommandType
{
SetRenderState,
DestructResource,
UnlockTextureRect,
UnlockBuffer16,
UnlockBuffer32,
DrawImGui,
ExecuteCommandList,
BeginCommandList,
StretchRect,
SetRenderTarget,
SetDepthStencilSurface,
ExecutePendingStretchRectCommands,
Clear,
SetViewport,
SetTexture,
SetScissorRect,
SetSamplerState,
SetBooleans,
SetVertexShaderConstants,
SetPixelShaderConstants,
AddPipeline,
DrawPrimitive,
DrawIndexedPrimitive,
DrawPrimitiveUP,
SetVertexDeclaration,
SetVertexShader,
SetStreamSource,
SetIndices,
SetPixelShader,
SetConditionalSurvey,
SetConditionalRendering,
};
struct RenderCommand
{
RenderCommandType type;
union
{
struct
{
GuestRenderState type;
uint32_t value;
} setRenderState;
struct
{
GuestResource* resource;
} destructResource;
struct
{
GuestTexture* texture;
} unlockTextureRect;
struct
{
GuestBuffer* buffer;
} unlockBuffer;
struct
{
GuestDevice* device;
uint32_t flags;
GuestTexture* texture;
uint32_t destSliceOrFace;
} stretchRect;
struct
{
GuestSurface* renderTarget;
} setRenderTarget;
struct
{
GuestSurface* depthStencil;
} setDepthStencilSurface;
struct
{
uint32_t flags;
float color[4];
float z;
uint32_t stencil;
} clear;
struct
{
float x;
float y;
float width;
float height;
float minDepth;
float maxDepth;
} setViewport;
struct
{
uint32_t index;
GuestTexture* texture;
} setTexture;
struct
{
int32_t left;
int32_t top;
int32_t right;
int32_t bottom;
} setScissorRect;
struct
{
uint32_t index;
uint32_t data0;
uint32_t data3;
uint32_t data5;
} setSamplerState;
struct
{
uint32_t booleans;