Repository navigation
Expand file tree
/
Copy pathfg_xefg.hpp
More file actions
3814 lines (3697 loc) · 179 KB
/
Copy pathfg_xefg.hpp
File metadata and controls
3814 lines (3697 loc) · 179 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
/*
* Copyright (C) 2026
* SPDX-License-Identifier: MIT
*/
#pragma once
#include <windows.h>
#include <psapi.h>
#include <d3d11.h>
#include <d3d12.h>
#include <dxgi1_4.h>
#include <wrl/client.h>
#if defined(NR_NEXUS_DEV_CONTROL)
#include <intrin.h>
#endif
#include <array>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <cwchar>
#include <exception>
#include <iterator>
#include <map>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "../../utils/directx.hpp"
#include "detour_guard.hpp"
#include "fg_frame.hpp"
#include "output_framerate.hpp"
#include "submission_tracker.hpp"
#if defined(NR_NEXUS_DEV_CONTROL)
#include "nr_edit_fg_observation.hpp"
#endif
#if defined(NR_NEXUS_DEV_CONTROL) && defined(NR_NEXUS_TEST_FG_OUTPUT_PROBE)
#include "nr_fg_output_probe.hpp"
#endif
#if defined(NR_NEXUS_DEV_CONTROL) && defined(NR_NEXUS_TEST_XEFG_PRESENT_PROBE)
#include "nr_xefg_present_probe.hpp"
#endif
namespace renodx::addons::dlss5::fg::xefg {
struct PresentSubmissionFact {
const char* reason = "unobserved";
std::pair<uint64_t, uint64_t> recording;
uintptr_t queue = 0;
uintptr_t fence = 0;
uint64_t fence_value = 0;
uint64_t completed_value = 0;
bool signaled = false;
};
using OrdinarySubmissionFact = PresentSubmissionFact;
using PrepareRuntime = void (*)(ID3D12Device*) noexcept;
struct OrdinaryPresentAttempt {
uintptr_t owner = 0;
uint64_t serial = 0;
explicit operator bool() const { return owner != 0 && serial != 0; }
bool operator==(const OrdinaryPresentAttempt&) const = default;
};
enum class OrdinaryPresentClaim : uint8_t { NO_MATCH, COVERED, CONFLICT };
// Values only: the ordinary owner retains and retires its own GPU operands.
// SDK epochs describe the original recording, never a new color version.
struct OrdinaryPresentReceipt {
OrdinaryPresentAttempt attempt;
output_framerate::OutputIdentity output;
uintptr_t swapchain = 0;
uintptr_t backbuffer = 0;
uint32_t subresource = 0;
uint32_t x = 0;
uint32_t y = 0;
uint32_t width = 0;
uint32_t height = 0;
uintptr_t context = 0;
uint64_t epoch = 0;
uint64_t call = 0;
uint64_t first_execute = 0;
submission::SubmissionProofState first_state = submission::SubmissionProofState::kUnsubmitted;
uintptr_t first_fence = 0;
uint64_t first_value = 0;
uint64_t first_completed_value = 0;
bool executed = false;
bool output_ready = false;
bool group_complete = false;
bool proofs_healthy = false;
bool proofs_complete = false;
bool game_reset_observed = false;
OrdinarySubmissionFact submission;
};
using ClaimOrdinaryPresent = OrdinaryPresentClaim (*)(const FrameInput&) noexcept;
using FinishOrdinaryPresent = void (*)(const OrdinaryPresentReceipt&, const FrameInput&, uint64_t, HRESULT) noexcept;
using InvalidateOrdinaryPresent = void (*)(uintptr_t) noexcept;
using FindOrdinaryPresentAttempt = OrdinaryPresentAttempt (*)(uintptr_t, uintptr_t) noexcept;
using EndOrdinaryPresent = void (*)(OrdinaryPresentAttempt, HRESULT) noexcept;
struct BridgeSourceIdentity {
uintptr_t device11 = 0;
uintptr_t handle = 0;
uint64_t generation = 0;
uintptr_t device12 = 0;
bool operator==(const BridgeSourceIdentity&) const = default;
};
#if defined(NR_NEXUS_DEV_CONTROL)
enum class BridgeEventKind : uint8_t { ENABLE_BEGIN, ENABLE_END, LINK_REVOKED, LINK_OBSERVED };
enum class BridgePresentMethod : uint8_t { UNOBSERVED, PRESENT, PRESENT1 };
struct BridgeSourceEntry {
BridgePresentMethod method = BridgePresentMethod::UNOBSERVED;
uintptr_t return_address = 0;
uint32_t thread_id = 0;
uint32_t proxy_entry_depth = 0;
uint32_t interval = 0;
uint32_t flags = 0;
};
struct BridgeEvent {
BridgeEventKind kind = BridgeEventKind::ENABLE_BEGIN;
uint64_t sequence = 0;
int64_t observed_ns = 0;
// Next-statement sample after the real SDK call, not an exact function-body boundary.
// ENABLE_END's observed_ns is the later receipt commit.
int64_t sdk_return_ns = 0;
uint32_t thread_id = 0;
uintptr_t context_identity = 0;
uint64_t declaration = 0;
uint64_t epoch = 0;
uint64_t link_generation = 0;
// ENABLE_BEGIN keeps the initial attempt snapshot, captured before its source observer.
// ENABLE_END keeps the link state after receipt commit.
uintptr_t source_swapchain = 0;
uint64_t source_swapchain_generation = 0;
// SDK events keep the source sampled before the call; link events keep the affected link's source.
BridgeSourceIdentity source;
const char* reason = "unobserved";
uint32_t enabled_request = 0;
int32_t sdk_status = 0;
bool off_observed = false;
BridgeSourceEntry source_entry;
};
struct BridgeEventSnapshot {
std::array<BridgeEvent, 32> events{};
uint32_t count = 0;
uint64_t last_sequence = 0;
uint64_t overwritten = 0;
};
struct EnabledContextMetadata {
bool available = false;
const char* reason = "unobserved";
uintptr_t context_identity = 0;
uint64_t epoch = 0;
uint64_t generation = 0;
};
#endif
enum class BridgeSnapshotState : uint8_t { UNOBSERVED, RUNTIME_BUSY, SHUTTING_DOWN, LOCKED };
struct BridgeSourceObservation {
BridgeSourceIdentity identity;
uintptr_t queried_device11 = 0;
BridgeSnapshotState state = BridgeSnapshotState::UNOBSERVED;
uint32_t candidates = 0;
uint32_t matches = 0;
uintptr_t queried_device12 = 0;
bool operator==(const BridgeSourceObservation&) const = default;
};
using BridgeSourceObserver = BridgeSourceObservation (*)(uintptr_t device11) noexcept;
inline const char* BridgeSourceReason(const BridgeSourceObservation& observation) noexcept {
switch (observation.state) {
case BridgeSnapshotState::UNOBSERVED: return "unobserved";
case BridgeSnapshotState::RUNTIME_BUSY: return "runtime_busy";
case BridgeSnapshotState::SHUTTING_DOWN: return "shutting_down";
case BridgeSnapshotState::LOCKED: break;
}
if (observation.candidates == 0) return "no_live_source";
const bool device_matches = (observation.queried_device12 != 0
? observation.queried_device11 == 0 && observation.identity.device11 == 0
&& observation.identity.device12 == observation.queried_device12
: observation.identity.device12 == 0 && observation.identity.device11 == observation.queried_device11);
if (observation.matches == 0 || !device_matches) return "device_mismatch";
if (observation.matches != 1 || observation.identity.handle == 0 || observation.identity.generation == 0) return "ambiguous";
return "matched";
}
struct BridgeDeviceObservation {
bool object_present = false;
bool swapchain_unwrapped = false;
HRESULT swapchain_result = E_PENDING;
HRESULT device11_result = E_PENDING;
HRESULT device12_result = E_PENDING;
// Prefer GetDevice11's returned object; use GetDevice12 when it is absent.
uintptr_t returned_device_identity = 0;
HRESULT device11_interface_result = E_PENDING;
HRESULT device12_interface_result = E_PENDING;
uintptr_t device11_interface_identity = 0;
uintptr_t device12_interface_identity = 0;
uint32_t returned_device_request = 0;
bool operator==(const BridgeDeviceObservation&) const = default;
};
struct BridgeOffEvidence {
bool available = false;
const char* reason = "unobserved";
uintptr_t context = 0;
uintptr_t proxy = 0;
uintptr_t application_device11 = 0;
uintptr_t proxy_device11 = 0;
uint64_t declaration = 0;
uint64_t epoch = 0;
uint64_t write_epoch = 0;
BridgeSourceIdentity source;
BridgeSourceObservation before;
BridgeSourceObservation after;
const char* init_kind = "unobserved";
BridgeDeviceObservation application;
BridgeDeviceObservation proxy_device;
const char* source_link_reason = "source_swapchain_unobserved";
uintptr_t source_swapchain = 0;
uint64_t source_swapchain_generation = 0;
uint64_t source_link_generation = 0;
bool uses_source_link = false;
uintptr_t context_identity = 0;
uint32_t source_present_thread_id = 0;
uint32_t source_present_proxy_entry_depth = 0;
output_framerate::OutputIdentity output_identity;
bool MatchesOutput(const output_framerate::Snapshot& output) const {
return available && context_identity != 0 && output.available && output.fg_off
&& output.source == output_framerate::Source::XEFG_STATUS && output.identity == context_identity
&& output_identity && output.output == output_identity;
}
bool operator==(const BridgeOffEvidence&) const = default;
};
// Public x64 XeSS-FG ABI, fixed Intel 8fe81bdb/de0fb9c1 headers. No SDK
// object layout is inspected. BACKBUFFER tags describe a region only.
namespace abi {
struct Extent { uint32_t x; uint32_t y; };
struct Resource {
int32_t type;
int32_t validity;
Extent base;
Extent size;
ID3D12Resource* resource;
D3D12_RESOURCE_STATES state;
};
struct Constants {
float view[16];
float projection[16];
float jitter_x;
float jitter_y;
float motion_scale_x;
float motion_scale_y;
uint32_t reset;
float frame_time_ms;
};
struct Init {
IDXGISwapChain* application_swapchain;
uint32_t flags;
uint32_t max_interpolated;
uint32_t creation_mask;
uint32_t visible_mask;
ID3D12Heap* buffer_heap;
uint64_t buffer_offset;
ID3D12Heap* texture_heap;
uint64_t texture_offset;
ID3D12PipelineLibrary* library;
int32_t ui_mode;
};
struct PresentStatus {
uint32_t frames_presented;
int32_t frame_gen_result;
uint32_t enabled;
};
static_assert(sizeof(Resource) == 40 && offsetof(Resource, resource) == 24);
static_assert(sizeof(Constants) == 152 && offsetof(Constants, reset) == 144);
static_assert(sizeof(Init) == 72 && offsetof(Init, ui_mode) == 64);
} // namespace abi
namespace internal {
inline std::atomic<Retire> retire{nullptr};
inline std::atomic<PrepareRuntime> prepare_runtime{nullptr};
} // namespace internal
// Call only after SDK/configuration locks are released. The owner may already
// hold runtime_mutex; maintenance neither enters NGX nor waits for its turn.
inline void ReconcileNrService(ID3D12Device* prepare_device = nullptr) noexcept {
if (const auto callback = internal::retire.load(std::memory_order_acquire); callback != nullptr) {
callback();
}
if (prepare_device != nullptr) {
if (const auto prepare = internal::prepare_runtime.load(std::memory_order_acquire); prepare != nullptr) {
prepare(prepare_device);
}
}
}
namespace internal {
using Microsoft::WRL::ComPtr;
inline constexpr int32_t kUiModeAuto = 0;
inline std::atomic<Service> service{nullptr};
inline std::atomic<ClaimOrdinaryPresent> claim_ordinary_present{nullptr};
inline std::atomic<FinishOrdinaryPresent> finish_ordinary_present{nullptr};
inline std::atomic<InvalidateOrdinaryPresent> invalidate_ordinary_present{nullptr};
inline std::atomic<FindOrdinaryPresentAttempt> find_ordinary_present_attempt{nullptr};
inline std::atomic<EndOrdinaryPresent> end_ordinary_present{nullptr};
inline std::atomic<PrepareCapture> prepare_capture{nullptr};
inline std::atomic<bool (*)() noexcept> requested{nullptr};
inline std::atomic_bool block_optional_hudless{false};
// Startup-only diagnostic switch. When false, XeFG SDK exports and its proxy
// Present hooks are left untouched so a host can isolate addon interception
// without detaching a live context.
inline std::atomic_bool sdk_detours_enabled{true};
inline std::atomic_bool stopped{false};
inline std::atomic_bool owns_stream{false};
inline std::atomic<BridgeSourceObserver> bridge_source_observer{nullptr};
inline std::atomic<BridgeSourceObserver> d3d12_source_observer{nullptr};
inline std::atomic<Result> result{Result::NOT_SERVED};
inline std::atomic<Result> display_result{Result::NOT_SERVED};
inline std::atomic_uint64_t submitted_frames{0};
inline void PublishResult(Result value) noexcept {
display_result.store(value, std::memory_order_release);
result.store(value, std::memory_order_release);
}
inline thread_local uint32_t proxy_depth = 0;
// SDK Present1 may call Present even when FG does not own the stream.
inline thread_local uint32_t proxy_entry_depth = 0;
struct Surface {
ComPtr<ID3D12Resource> texture;
ComPtr<ID3D12Fence> consumer_fence;
uint64_t consumer_value = 0;
bool reserved = true;
bool unproven = false;
};
struct TaggedResource {
abi::Resource data{};
ComPtr<ID3D12Resource> resource;
std::shared_ptr<Surface> copy;
};
struct Frame {
std::array<TaggedResource, 4> resources;
std::vector<std::shared_ptr<Surface>> captures;
abi::Constants constants{};
abi::Resource region{};
bool has_constants = false;
bool has_region = false;
bool rejected = false;
bool presenting = false;
};
// Resource identities rotate with the swapchain/capture pool. Log their values
// in samples, but only nullness changes consume the contract-change budget.
struct TraceResource {
bool present = false;
int32_t type = -1;
int32_t validity = -1;
abi::Extent base{};
abi::Extent size{};
uintptr_t resource = 0;
uint32_t state = 0;
bool operator==(const TraceResource& other) const {
return present == other.present && type == other.type && validity == other.validity
&& base.x == other.base.x && base.y == other.base.y && size.x == other.size.x && size.y == other.size.y
&& (resource != 0) == (other.resource != 0) && state == other.state;
}
};
struct TraceResourceCall {
TraceResource original;
TraceResource forwarded;
bool replaced = false;
bool sdk_called = false;
int32_t status = 0;
bool nr_requested = false;
bool ui_composition = false;
bool operator==(const TraceResourceCall&) const = default;
};
template <typename Sample>
struct ContractTrace {
Sample last{};
uint32_t last_id = 0;
uint32_t frame_samples = 0;
uint32_t change_samples = 0;
bool seen = false;
bool Observe(uint32_t id, const Sample& sample) {
bool emit = false;
if (frame_samples < 3 && (!seen || id != last_id)) {
++frame_samples;
emit = true;
} else if (seen && !(sample == last) && change_samples < 8) {
++change_samples;
emit = true;
}
last = sample;
last_id = id;
seen = true;
return emit;
}
};
struct PresentHandoff {
output_framerate::OutputIdentity output;
uintptr_t context = 0;
uintptr_t context_identity = 0;
uint64_t epoch = 0;
uint64_t application_frame = 0;
uint64_t call = 0;
uint64_t declaration = 0;
uintptr_t backbuffer = 0;
uintptr_t hudless = 0;
int64_t sdk_return_ns = 0;
uint32_t present_id = 0;
const char* reason = "unobserved";
Result service_result = Result::NOT_SERVED;
HRESULT sdk_result = E_FAIL;
int32_t status_result = 0;
uint32_t status_enabled = 0;
bool sdk_enabled_at_entry = false;
bool has_present_id = false;
bool has_guides = false;
bool color_executed = false;
bool ordinary_executed = false;
bool owner_current_at_return = false;
bool status_queried = false;
bool status_current = false;
bool sdk_result_available = false;
PresentSubmissionFact service_submission;
OrdinarySubmissionFact ordinary_submission;
OrdinaryPresentClaim ordinary_claim = OrdinaryPresentClaim::NO_MATCH;
OrdinaryPresentReceipt ordinary_receipt;
};
struct Context {
ComPtr<ID3D12CommandQueue> queue;
ComPtr<ID3D12Device> device;
ComPtr<IUnknown> device_identity;
ComPtr<ID3D12Fence> fence;
uintptr_t proxy_identity = 0;
abi::Init init{};
DXGI_COLOR_SPACE_TYPE color_space = DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709;
std::map<uint32_t, std::shared_ptr<Frame>> frames;
std::vector<std::shared_ptr<Surface>> surfaces;
uint64_t epoch = 1;
#if defined(NR_NEXUS_DEV_CONTROL)
uint64_t fg_environment_generation = 0;
#endif
uint64_t application_frame = 0;
uint64_t last_submission_frame = 0;
uint64_t next_fence = 0;
uint32_t present_id = 0;
uint32_t last_id = 0;
bool fresh_id = false;
bool last_id_known = false;
bool enabled = false;
ComPtr<IUnknown> bridge_application_device11;
ComPtr<IUnknown> bridge_proxy_device11;
const char* bridge_init_kind = "unobserved";
BridgeDeviceObservation bridge_application_observation;
BridgeDeviceObservation bridge_proxy_observation;
bool bridge_context_ready = false;
bool bridge_proxy_ready = false;
bool bridge_off_observed = false;
bool bridge_present_fg_on = false;
bool bridge_enable_ambiguous = false;
uint32_t bridge_enable_inflight = 0;
uint64_t bridge_declaration = 0;
uint64_t bridge_off_epoch = 0;
uint64_t bridge_write_epoch = 0;
output_framerate::OutputIdentity bridge_off_output;
BridgeSourceIdentity bridge_off_source;
BridgeSourceObservation bridge_before_observation;
BridgeSourceObservation bridge_after_observation;
const char* bridge_off_reason = "disable_unobserved";
uintptr_t bridge_source_swapchain = 0;
uint64_t bridge_source_generation = 0;
BridgeSourceIdentity bridge_link_source;
uint64_t bridge_link_generation = 0;
const char* bridge_link_reason = "source_swapchain_unobserved";
bool bridge_off_uses_source_link = false;
uint64_t bridge_off_link_generation = 0;
uint32_t requested_interpolated = 0;
bool requested_interpolated_known = false;
output_framerate::OutputIdentity output_identity;
uintptr_t application_swapchain_identity = 0;
uint64_t present_serial = 0;
uint64_t color_serial = 0;
uint64_t color_submitted_frame = 0;
uint64_t color_submitted_epoch = 0;
struct PresentColors {
uintptr_t backbuffer = 0;
uintptr_t hudless = 0;
bool xefg_owned = false;
bool ordinary_committed = false;
OrdinarySubmissionFact ordinary_submission;
};
std::map<uint64_t, PresentColors> present_colors;
std::array<uintptr_t, 16> backbuffers{};
uint32_t handoff_frames_left = 0;
PresentHandoff last_handoff_write;
uint64_t status_ambiguous_through = 0;
uint32_t presents_in_flight = 0;
bool omit_optional_hudless = false;
bool ui_composition = false;
// Lifetime budgets: at most 3 tagged frame samples + 8 contract changes per
// resource kind (slot 5 covers null/unknown tags), and 1 + 8 UI-state calls.
std::array<ContractTrace<TraceResourceCall>, 6> resource_trace;
ContractTrace<std::array<int64_t, 3>> ui_trace;
int32_t (*get_status)(void*, abi::PresentStatus*) = nullptr;
std::chrono::steady_clock::time_point interval_start = std::chrono::steady_clock::now();
uint64_t interval_presents = 0;
uint64_t interval_tagged_presents = 0;
uint64_t interval_untagged_presents = 0;
uint64_t interval_displayed = 0;
uint64_t interval_submitted = 0;
uint64_t interval_status_samples = 0;
// Protected by state.mutex; bind role counts to a completed Present rather
// than inferring executed work from its available input resources.
uint64_t service_stats_serial = 0;
uint32_t service_hudless_processes = 0;
uint32_t service_backbuffer_processes = 0;
bool service_composed = false;
std::atomic_uint64_t interval_nr_frames{0};
std::atomic_uint64_t interval_nr_processes{0};
std::atomic_uint64_t interval_nr_evaluates{0};
std::atomic_uint64_t interval_nr_hudless_processes{0};
std::atomic_uint64_t interval_nr_backbuffer_processes{0};
std::atomic_uint64_t interval_nr_two_color_frames{0};
std::atomic_uint64_t interval_nr_composed_frames{0};
std::atomic_uint64_t interval_hudless_blocked{0};
std::atomic_uint64_t interval_hudless_sdk_calls{0};
std::atomic_uint64_t interval_hudless_sdk_success{0};
std::atomic_uint64_t interval_input_hudless{0};
std::atomic_uint64_t interval_input_ui{0};
};
struct BridgeSourceSwapchain {
uintptr_t owner = 0;
uintptr_t identity = 0;
ComPtr<IUnknown> device11;
uint64_t generation = 0;
bool hook_ready = false;
std::weak_ptr<Context> context;
uintptr_t proxy = 0;
const char* reason = "source_present_unobserved";
ComPtr<IUnknown> device12;
uint32_t source_present_thread_id = 0;
uint32_t source_present_proxy_entry_depth = 0;
};
struct State {
std::shared_mutex mutex;
std::map<void*, std::shared_ptr<Context>> contexts;
std::vector<std::shared_ptr<Context>> retired_contexts;
uint64_t bridge_declaration = 0;
std::map<uintptr_t, std::shared_ptr<BridgeSourceSwapchain>> bridge_swapchains;
uint64_t bridge_source_generation = 0;
uint64_t bridge_link_generation = 0;
#if defined(NR_NEXUS_DEV_CONTROL)
std::array<BridgeEvent, 32> bridge_events{};
uint64_t bridge_event_sequence = 0;
uint64_t fg_environment_generation = 0;
#endif
};
inline State state;
inline std::shared_mutex install_mutex;
inline std::atomic<DWORD> module_bytes{0};
#if defined(NR_NEXUS_DEV_CONTROL)
// state.mutex is held by callers. No callbacks, allocation, or file I/O occur here.
inline void RecordBridgeEventLocked(BridgeEvent event) {
event.sequence = ++state.bridge_event_sequence;
event.observed_ns = output_framerate::NowNs();
event.thread_id = GetCurrentThreadId();
state.bridge_events[(event.sequence - 1) % state.bridge_events.size()] = event;
}
#endif
// state.mutex is held by callers. A host link never substitutes for a Disable
// receipt, and its failure must not invalidate the independent direct path.
inline void RevokeBridgeSourceLinkLocked(Context* context, const char* reason
#if defined(NR_NEXUS_DEV_CONTROL)
, const BridgeSourceEntry& source_entry = {}
#endif
) {
#if defined(NR_NEXUS_DEV_CONTROL)
const bool had_link = context->bridge_source_swapchain != 0;
// Revocation keeps the old identity, but its generation and Off state describe the committed revocation.
BridgeEvent event{
.kind = BridgeEventKind::LINK_REVOKED,
.context_identity = reinterpret_cast<uintptr_t>(context),
.declaration = context->bridge_declaration,
.epoch = context->epoch,
.source_swapchain = context->bridge_source_swapchain,
.source_swapchain_generation = context->bridge_source_generation,
.source = context->bridge_link_source,
.reason = reason,
.source_entry = source_entry};
#endif
if (context->bridge_source_swapchain != 0) {
context->bridge_link_generation = ++state.bridge_link_generation;
output_framerate::ObserveXeFGOutputLink(context->output_identity, 0, 0);
}
context->bridge_source_swapchain = 0;
context->bridge_source_generation = 0;
context->bridge_link_source = {};
context->bridge_link_reason = reason;
if (context->bridge_off_uses_source_link) {
context->bridge_off_observed = false;
context->bridge_off_reason = reason;
}
#if defined(NR_NEXUS_DEV_CONTROL)
if (had_link) {
event.link_generation = context->bridge_link_generation;
event.off_observed = context->bridge_off_observed;
RecordBridgeEventLocked(event);
}
#endif
}
inline bool BridgeSourceLinkCurrentLocked(const Context& context) {
const auto source = state.bridge_swapchains.find(context.bridge_source_swapchain);
if (source == state.bridge_swapchains.end() || !source->second->hook_ready
|| source->second->generation != context.bridge_source_generation
|| source->second->context.lock().get() != &context || source->second->proxy != context.proxy_identity
|| context.bridge_link_source.handle == 0 || context.bridge_link_source.generation == 0) return false;
if (context.bridge_link_source.device12 == 0) {
return source->second->device12 == nullptr && source->second->device11 != nullptr
&& reinterpret_cast<uintptr_t>(source->second->device11.Get()) == context.bridge_link_source.device11;
}
if (context.bridge_link_source.device11 != 0 || source->second->device11 != nullptr
|| source->second->device12 == nullptr || source->second->device12.Get() != context.device_identity.Get()
|| reinterpret_cast<uintptr_t>(source->second->device12.Get()) != context.bridge_link_source.device12) return false;
return std::count_if(state.bridge_swapchains.begin(), state.bridge_swapchains.end(), [&](const auto& entry) {
const auto& [owner, registered] = entry;
return registered->device12 == source->second->device12;
}) == 1;
}
inline void ClearBridgeSourceObservationsLocked(Context* context, const char* reason) {
RevokeBridgeSourceLinkLocked(context, reason);
for (const auto& [owner, source] : state.bridge_swapchains) {
if (source->context.lock().get() == context) {
source->context.reset();
source->proxy = 0;
source->reason = reason;
}
}
}
inline bool Requested() noexcept {
const auto policy = requested.load(std::memory_order_acquire);
return !stopped.load(std::memory_order_acquire) && policy != nullptr && policy();
}
inline void LogDiagnostic(const char* message) noexcept {
#if defined(RESHADE_API_LIBRARY)
ReShadeLogMessage(nullptr, static_cast<int>(reshade::log::level::info), message);
#else
const auto module = reshade::internal::get_reshade_module_handle();
if (module == nullptr) return;
const auto function = reinterpret_cast<void (*)(void*, int, const char*)>(
GetProcAddress(module, "ReShadeLogMessage"));
if (function == nullptr) return;
function(reshade::internal::get_current_module_handle(), static_cast<int>(reshade::log::level::info), message);
#endif
}
inline void LogHandoff(const char* event, const PresentHandoff& fact) noexcept {
char message[2560];
std::snprintf(message, sizeof(message),
"DLSS5 XeFG present_handoff event=%s context=%llx context_identity=%llx output=%llx output_view=%llx output_generation=%llu "
"epoch=%llu call=%llu declaration=%llu application_frame=%llu bb=%llx hudless=%llx subresource=0 "
"sdk_enabled_at_entry=%u present_id=%u fresh_id=%u guides=%u reason=%s service_result=%u "
"xefg_execute=%u ordinary_execute=%u fence_value=%llu sdk_return_ns=%lld sdk_hresult=0x%08x "
"sdk_result_available=%u owner_current_at_return=%u status_queried=%u status_current=%u status_result=%d status_enabled=%u "
"xefg[list=%llu generation=%llu queue=%llx fence=%llx completed=%llu signaled=%u] "
"ordinary[reason=%s list=%llu generation=%llu queue=%llx fence=%llx value=%llu completed=%llu signaled=%u] "
"ordinary_receipt[claim=%u owner=%llx attempt=%llu output=%llx output_view=%llx output_generation=%llu "
"context=%llx epoch=%llu call=%llu subresource=%u region=%u,%u,%u,%u ready=%u group=%u "
"first_execute=%llu first_state=%u first_fence=%llx first_value=%llu first_completed=%llu healthy=%u complete=%u]",
event, static_cast<unsigned long long>(fact.context), static_cast<unsigned long long>(fact.context_identity),
static_cast<unsigned long long>(fact.output.swapchain),
static_cast<unsigned long long>(fact.output.view), static_cast<unsigned long long>(fact.output.generation),
static_cast<unsigned long long>(fact.epoch), static_cast<unsigned long long>(fact.call),
static_cast<unsigned long long>(fact.declaration), static_cast<unsigned long long>(fact.application_frame),
static_cast<unsigned long long>(fact.backbuffer), static_cast<unsigned long long>(fact.hudless),
static_cast<unsigned>(fact.sdk_enabled_at_entry), fact.present_id, static_cast<unsigned>(fact.has_present_id),
static_cast<unsigned>(fact.has_guides), fact.reason, static_cast<unsigned>(fact.service_result),
static_cast<unsigned>(fact.color_executed), static_cast<unsigned>(fact.ordinary_executed),
static_cast<unsigned long long>(fact.service_submission.fence_value), static_cast<long long>(fact.sdk_return_ns),
static_cast<unsigned>(fact.sdk_result), static_cast<unsigned>(fact.sdk_result_available),
static_cast<unsigned>(fact.owner_current_at_return),
static_cast<unsigned>(fact.status_queried), static_cast<unsigned>(fact.status_current),
fact.status_result, fact.status_enabled,
static_cast<unsigned long long>(fact.service_submission.recording.first),
static_cast<unsigned long long>(fact.service_submission.recording.second),
static_cast<unsigned long long>(fact.service_submission.queue), static_cast<unsigned long long>(fact.service_submission.fence),
static_cast<unsigned long long>(fact.service_submission.completed_value),
static_cast<unsigned>(fact.service_submission.signaled), fact.ordinary_submission.reason,
static_cast<unsigned long long>(fact.ordinary_submission.recording.first),
static_cast<unsigned long long>(fact.ordinary_submission.recording.second),
static_cast<unsigned long long>(fact.ordinary_submission.queue), static_cast<unsigned long long>(fact.ordinary_submission.fence),
static_cast<unsigned long long>(fact.ordinary_submission.fence_value),
static_cast<unsigned long long>(fact.ordinary_submission.completed_value),
static_cast<unsigned>(fact.ordinary_submission.signaled), static_cast<unsigned>(fact.ordinary_claim),
static_cast<unsigned long long>(fact.ordinary_receipt.attempt.owner),
static_cast<unsigned long long>(fact.ordinary_receipt.attempt.serial),
static_cast<unsigned long long>(fact.ordinary_receipt.output.swapchain),
static_cast<unsigned long long>(fact.ordinary_receipt.output.view),
static_cast<unsigned long long>(fact.ordinary_receipt.output.generation),
static_cast<unsigned long long>(fact.ordinary_receipt.context),
static_cast<unsigned long long>(fact.ordinary_receipt.epoch),
static_cast<unsigned long long>(fact.ordinary_receipt.call), fact.ordinary_receipt.subresource,
fact.ordinary_receipt.x, fact.ordinary_receipt.y, fact.ordinary_receipt.width, fact.ordinary_receipt.height,
static_cast<unsigned>(fact.ordinary_receipt.output_ready), static_cast<unsigned>(fact.ordinary_receipt.group_complete),
static_cast<unsigned long long>(fact.ordinary_receipt.first_execute), static_cast<unsigned>(fact.ordinary_receipt.first_state),
static_cast<unsigned long long>(fact.ordinary_receipt.first_fence),
static_cast<unsigned long long>(fact.ordinary_receipt.first_value),
static_cast<unsigned long long>(fact.ordinary_receipt.first_completed_value),
static_cast<unsigned>(fact.ordinary_receipt.proofs_healthy), static_cast<unsigned>(fact.ordinary_receipt.proofs_complete));
LogDiagnostic(message);
}
inline void TraceResourceContract(void* handle, ID3D12CommandList* list, uint32_t id,
const abi::Resource* original, const abi::Resource* forwarded,
bool sdk_called, int32_t status) {
if (stopped.load(std::memory_order_acquire) || !sdk_detours_enabled.load(std::memory_order_acquire)) return;
const auto snapshot = [](const abi::Resource* data) {
if (data == nullptr) return TraceResource{};
return TraceResource{.present = true, .type = data->type, .validity = data->validity,
.base = data->base, .size = data->size, .resource = reinterpret_cast<uintptr_t>(data->resource),
.state = static_cast<uint32_t>(data->state)};
};
TraceResourceCall sample{.original = snapshot(original), .forwarded = snapshot(forwarded),
.replaced = sdk_called && original != forwarded, .sdk_called = sdk_called, .status = status,
.nr_requested = Requested()};
uint32_t frame_samples = 0;
uint32_t change_samples = 0;
int32_t ui_mode = 0;
{
std::unique_lock lock(state.mutex);
const auto it = state.contexts.find(handle);
if (it == state.contexts.end()) return;
sample.ui_composition = it->second->ui_composition;
ui_mode = it->second->init.ui_mode;
const size_t slot = (original != nullptr && original->type >= 0 && original->type <= 4
? static_cast<size_t>(original->type) : 5);
auto& trace = it->second->resource_trace[slot];
if (!trace.Observe(id, sample)) return;
frame_samples = trace.frame_samples;
change_samples = trace.change_samples;
}
constexpr const char* names[]{"HUDLess", "Depth", "Motion", "UI", "BackbufferRegion", "NullOrUnknown"};
const auto& raw = sample.original;
const auto& actual = sample.forwarded;
const auto* name = names[(raw.present && raw.type >= 0 && raw.type <= 4 ? raw.type : 5)];
char sdk_status[24] = "not_called";
if (sdk_called) std::snprintf(sdk_status, sizeof(sdk_status), "%d", status);
char message[1024];
std::snprintf(message, sizeof(message),
"DLSS5 XeFG resource_contract context=%llx present_id=%u tag=%s list=%llx nr_requested=%u "
"ui_mode=%d ui_composition=%u frame_sample=%u/3 change_sample=%u/8 "
"original[present=%u type=%d validity=%d base=%u,%u size=%u,%u resource=%llx state=%u] "
"forwarded[present=%u type=%d validity=%d base=%u,%u size=%u,%u resource=%llx state=%u] "
"replaced=%u sdk_called=%u sdk_status=%s adapter_return=%d",
static_cast<unsigned long long>(reinterpret_cast<uintptr_t>(handle)), id, name,
static_cast<unsigned long long>(reinterpret_cast<uintptr_t>(list)), static_cast<unsigned>(sample.nr_requested),
ui_mode, static_cast<unsigned>(sample.ui_composition), frame_samples, change_samples,
static_cast<unsigned>(raw.present), raw.type, raw.validity, raw.base.x, raw.base.y, raw.size.x, raw.size.y,
static_cast<unsigned long long>(raw.resource), raw.state,
static_cast<unsigned>(actual.present), actual.type, actual.validity, actual.base.x, actual.base.y, actual.size.x, actual.size.y,
static_cast<unsigned long long>(actual.resource), actual.state,
static_cast<unsigned>(sample.replaced), static_cast<unsigned>(sdk_called), sdk_status, status);
LogDiagnostic(message);
}
inline void RecordServiceStats(uintptr_t context_value, uint32_t process_calls,
uint32_t evaluates, uint32_t hudless_processes,
uint32_t backbuffer_processes, bool two_color,
bool composed) noexcept {
auto* context = reinterpret_cast<Context*>(context_value);
if (context == nullptr) return;
std::unique_lock lock(state.mutex);
++context->service_stats_serial;
context->service_hudless_processes = hudless_processes;
context->service_backbuffer_processes = backbuffer_processes;
context->service_composed = composed;
context->interval_nr_frames.fetch_add(1, std::memory_order_relaxed);
context->interval_nr_processes.fetch_add(process_calls, std::memory_order_relaxed);
context->interval_nr_evaluates.fetch_add(evaluates, std::memory_order_relaxed);
context->interval_nr_hudless_processes.fetch_add(hudless_processes, std::memory_order_relaxed);
context->interval_nr_backbuffer_processes.fetch_add(backbuffer_processes, std::memory_order_relaxed);
context->interval_nr_two_color_frames.fetch_add(two_color, std::memory_order_relaxed);
context->interval_nr_composed_frames.fetch_add(composed, std::memory_order_relaxed);
}
inline void RefreshOwnership() {
bool active = false;
{
std::shared_lock lock(state.mutex);
for (const auto& [handle, context] : state.contexts) {
active |= context->enabled && context->queue != nullptr && context->proxy_identity != 0;
}
}
owns_stream.store(active, std::memory_order_release);
}
inline uintptr_t Identity(IUnknown* object) {
ComPtr<IUnknown> identity;
if (object == nullptr || FAILED(object->QueryInterface(IID_PPV_ARGS(&identity)))) return 0;
return reinterpret_cast<uintptr_t>(identity.Get());
}
// Called only while the SDK callback still owns a valid swapchain argument.
inline ComPtr<IUnknown> BridgeDeviceIdentity(IUnknown* object, BridgeDeviceObservation* observation = nullptr) {
if (observation != nullptr) {
*observation = {.object_present = object != nullptr};
}
ComPtr<IDXGISwapChain> swapchain;
ComPtr<IUnknown> device;
if (object == nullptr) return {};
const auto swapchain_result = object->QueryInterface(IID_PPV_ARGS(&swapchain));
if (observation != nullptr) {
observation->swapchain_result = swapchain_result;
}
if (FAILED(swapchain_result)) return {};
auto* native_swapchain = swapchain.Get();
if (renodx::utils::directx::NativeFromReShadeProxy(&native_swapchain)) {
// Own the borrowed base before releasing the outer interface, then query
// the known base's device rather than the wrapper's reported interface.
swapchain = native_swapchain;
if (observation != nullptr) {
observation->swapchain_unwrapped = true;
}
}
const auto device11_result = swapchain->GetDevice(__uuidof(ID3D11Device), reinterpret_cast<void**>(device.GetAddressOf()));
if (observation == nullptr) {
if (FAILED(device11_result) || device == nullptr) return {};
ComPtr<ID3D11Device> device11_interface;
if (FAILED(device->QueryInterface(IID_PPV_ARGS(&device11_interface))) || device11_interface == nullptr) return {};
return ComPtr<IUnknown>(renodx::utils::directx::NativeIdentity(device11_interface.Get()));
}
observation->device11_result = device11_result;
ComPtr<IUnknown> device12;
observation->device12_result = swapchain->GetDevice(__uuidof(ID3D12Device), reinterpret_cast<void**>(device12.GetAddressOf()));
if (SUCCEEDED(device11_result) && device != nullptr) {
observation->returned_device_request = 11;
} else if (SUCCEEDED(observation->device12_result) && device12 != nullptr) {
observation->returned_device_request = 12;
device = std::move(device12);
} else {
return {};
}
observation->returned_device_identity = Identity(device.Get());
ComPtr<ID3D11Device> device11_interface;
observation->device11_interface_result = device->QueryInterface(IID_PPV_ARGS(&device11_interface));
if (SUCCEEDED(observation->device11_interface_result)) {
observation->device11_interface_identity = Identity(device11_interface.Get());
}
ComPtr<ID3D12Device> device12_interface;
observation->device12_interface_result = device->QueryInterface(IID_PPV_ARGS(&device12_interface));
if (SUCCEEDED(observation->device12_interface_result)) {
observation->device12_interface_identity = Identity(device12_interface.Get());
}
if (FAILED(observation->device11_interface_result) || device11_interface == nullptr
|| observation->returned_device_identity == 0
|| observation->device11_interface_identity != observation->returned_device_identity) return {};
return ComPtr<IUnknown>(renodx::utils::directx::NativeIdentity(device11_interface.Get()));
}
inline bool BridgeDeviceCaptureReady(const BridgeDeviceObservation& observation, IUnknown* device11, bool allow_absent) {
if (!observation.object_present) return allow_absent;
if (FAILED(observation.swapchain_result) || observation.returned_device_identity == 0
|| (observation.returned_device_request != 11 && observation.returned_device_request != 12)) return false;
if (SUCCEEDED(observation.device11_interface_result)) {
return device11 != nullptr && observation.device11_interface_identity == observation.returned_device_identity;
}
return observation.device11_interface_result == E_NOINTERFACE && SUCCEEDED(observation.device12_interface_result)
&& observation.device12_interface_identity == observation.returned_device_identity;
}
inline ComPtr<IUnknown> D3D12SourceDeviceIdentity(IDXGISwapChain* swapchain) {
if (swapchain == nullptr) return {};
renodx::utils::directx::NativeFromReShadeProxy(&swapchain);
ComPtr<ID3D12Device> device;
if (FAILED(swapchain->GetDevice(IID_PPV_ARGS(&device)))) return {};
return ComPtr<IUnknown>(renodx::utils::directx::NativeIdentity(device.Get()));
}
inline uint64_t InvalidateBridgeOff(void* handle, const char* reason,
bool invalidate_context = false, bool invalidate_proxy = false) {
std::unique_lock lock(state.mutex);
if (const auto it = state.contexts.find(handle); it != state.contexts.end()) {
auto& context = *it->second;
context.bridge_off_observed = false;
context.bridge_before_observation = {};
context.bridge_after_observation = {};
context.bridge_off_reason = reason;
context.bridge_declaration = ++state.bridge_declaration;
if (invalidate_context || invalidate_proxy) {
ClearBridgeSourceObservationsLocked(&context, reason);
}
if (invalidate_context) {
context.bridge_context_ready = false;
}
if (invalidate_proxy) {
context.bridge_proxy_ready = false;
}
return context.bridge_declaration;
}
return 0;
}
inline std::shared_ptr<Frame> GetFrame(Context* context, uint32_t id) {
if (const auto it = context->frames.find(id); it != context->frames.end()) {
return it->second->presenting ? nullptr : it->second;
}
// A caller may tag ahead. Keep the admission bounded without discarding a
// frame whose SDK still holds one of our substituted resources.
if (context->frames.size() >= 16) return nullptr;
auto frame = std::make_shared<Frame>();
context->frames.emplace(id, frame);
return frame;
}
inline void ReleaseFrames(Context* context) {
output_framerate::ReleaseXeFGOutput(context->output_identity);
context->output_identity = {};
for (const auto& [id, frame] : context->frames) {
for (const auto& resource : frame->resources) {
if (resource.copy != nullptr) resource.copy->unproven = true;
}
}
context->frames.clear();
context->fresh_id = false;
context->last_id_known = false;
context->last_submission_frame = 0;
++context->epoch;
++context->bridge_write_epoch;
#if defined(NR_NEXUS_DEV_CONTROL)
edit_fg_observation::StopEnvironment(2, reinterpret_cast<uintptr_t>(context));
context->fg_environment_generation = ++state.fg_environment_generation;
#if defined(NR_NEXUS_TEST_XEFG_PRESENT_PROBE)
xefg_present_probe::Observe([&] {
xefg_present_probe::UpdateContext(reinterpret_cast<uintptr_t>(context), context->epoch,
context->fg_environment_generation);
});
#endif
#if defined(NR_NEXUS_TEST_FG_OUTPUT_PROBE)
fg_output_probe::UpdateXefg(reinterpret_cast<uintptr_t>(context), context->epoch,
context->fg_environment_generation);
#endif
#endif
}
inline void RegisterOutput(Context* context) {
if (output_framerate::IsCurrentOutput(context->output_identity)) return;
context->output_identity = output_framerate::RegisterXeFGOutput(context->proxy_identity,
reinterpret_cast<uintptr_t>(context->device_identity.Get()), reinterpret_cast<uintptr_t>(context),
context->application_swapchain_identity);
output_framerate::ObserveXeFGMetadata(context->output_identity, context->init.max_interpolated + 1,
context->requested_interpolated_known ? context->requested_interpolated + 1 : 0);
if (BridgeSourceLinkCurrentLocked(*context)) {
const auto& source = *state.bridge_swapchains.at(context->bridge_source_swapchain);
output_framerate::ObserveXeFGOutputLink(context->output_identity, source.owner, source.identity);
}
}
inline std::shared_ptr<Surface> AcquireSurface(const std::shared_ptr<Context>& context,
const D3D12_RESOURCE_DESC& description) {
std::shared_ptr<Surface> surface;
{
std::unique_lock lock(state.mutex);
for (const auto& candidate : context->surfaces) {
if (candidate->reserved || candidate->unproven
|| !submission::ResourceReleasable(candidate->texture.Get())) continue;
if (candidate->consumer_fence != nullptr
&& candidate->consumer_fence->GetCompletedValue() < candidate->consumer_value) continue;
const auto have = candidate->texture->GetDesc();
if (have.Width == description.Width && have.Height == description.Height
&& have.Format == description.Format) {
surface = candidate;
surface->reserved = true;
break;
}
}
if (surface == nullptr && context->surfaces.size() >= 64) return nullptr;
}
if (surface != nullptr) return surface;
D3D12_RESOURCE_DESC copy = description;
copy.Alignment = 0;
copy.MipLevels = 1;
copy.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
copy.Flags &= ~(D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
D3D12_HEAP_PROPERTIES heap{};
heap.Type = D3D12_HEAP_TYPE_DEFAULT;
surface = std::make_shared<Surface>();
if (FAILED(context->device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, ©,