Repository navigation
Expand file tree
/
Copy pathbridgeprobe.c
More file actions
3649 lines (3610 loc) · 223 KB
/
Copy pathbridgeprobe.c
File metadata and controls
3649 lines (3610 loc) · 223 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
// Profiling-only (bridgeprobe.txt in the game folder): finds the four places the DLSS frame bridge needs (dlss/DLSS_LOG.md,
// Attila's DLSS_BRIDGE_GUIDE.md section 6). Looking only: nothing that is drawn changes.
// Rome 2's renderer code does not match Attila's (no stage driver or set-camera found by shape), so this works at the
// D3D11 level, the guide's fallback:
// 1. Present: CreateDXGIFactory1 (looked up with GetProcAddress, like D3D11CreateDevice) hands out the real factory with
// CreateSwapChain wrapped; the swapchain's Present is wrapped. Swapchain description and callers go to the log.
// 2./3. When bridge_go.txt appears in the game folder, the next frame (Present to Present) is captured into
// bridge_capture_<n>.txt: every render-target change, clear and vertex-shader constant buffer 0 bind, with the targets
// (back buffer or not, size, format, depth buffer), draws per target set and the game's return addresses; plus
// half-size dumps of the back buffer whenever the game leaves a back-buffer target set it drew into, and at Present
// -> where the UI starts and which depth buffer is the main one.
// 4. For that frame and 30 more, every write into a camera constant buffer (camera_VS_PS: 548 bytes used, view_projection
// at 144, same layout as Attila's) goes to bridge_camera_<n>.bin with its frame, buffer and game caller.
// Live frame bridge (fgbridge_on.txt next to bridgeprobe.txt: helper64.exe path, Streamline dir, frames to generate 0..3,
// options; dlss/DLSS_LOG.md 5/6): the game's Present is replaced by the helper's (fgbridge.c), as in Attila, with the
// HUD-less point and the camera found from D3D11 calls: the camera is a camera-buffer write while the swapchain-sized
// scene depth is bound; the HUD-less point is the target change after draws into the back buffer, once the scene depth
// was drawn into this frame.
#define COBJMACROS
#include <windows.h>
#include <d3d11.h>
#include <d3d11_1.h>
#include <dxgi.h>
#include <dxgi1_2.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <math.h>
#include "builds.h" // in_image (the reactive mask stage check)
#include "iathook.h"
static wchar_t g_dir[MAX_PATH];
static void (*g_log)(const char *fmt, ...);
static unsigned g_base, g_size; // empire.retail.dll
static ID3D11Device *g_dev;
static ID3D11DeviceContext *g_ctx;
static IDXGISwapChain *g_sc;
static int g_creates;
static unsigned g_frame, g_presents, g_present_other_thread, g_sync[5], g_main_tid, g_nested;
static struct { unsigned ret, game_ret, n; } g_callers[8];
static int g_armed, g_capturing, g_captures, g_camera_left;
static unsigned g_dumps, g_sets, g_draws, g_set_draws, g_set_bb, g_cam_writes;
static FILE *g_cap, *g_cam;
static ID3D11Texture2D *g_staging;
static const GUID iid_resource = {0xdc8e63f3, 0xd12b, 0x4952, {0xb4, 0x7b, 0x5e, 0x45, 0x02, 0x6a, 0x86, 0x2d}};
static const GUID iid_device = {0xdb6f6ddb, 0xac77, 0x4e88, {0x82, 0x53, 0x81, 0x9d, 0xf9, 0xbb, 0xf1, 0x40}};
// fgbridge.c (Attila's, unchanged)
int fgb_start(ID3D11Device *, ID3D11DeviceContext *, HWND, UINT, UINT, DXGI_FORMAT, const wchar_t *, const wchar_t *, const wchar_t *,
unsigned, void (*)(const char *, ...));
void fgb_hudless(ID3D11Resource *backbuffer, ID3D11Resource *depth, const float vp[16]);
void fgb_present(ID3D11Resource *backbuffer);
int fgb_active(void);
unsigned fgb_frames(void), fgb_pace_timeouts(void), fgb_fg_max(void);
void fgb_stop(void);
IDXGISwapChain *fgb_fallback_frame(ID3D11Resource *src, HRESULT(STDMETHODCALLTYPE *create)(IDXGIFactory *, IUnknown *, DXGI_SWAP_CHAIN_DESC *, IDXGISwapChain **));
void fgb_fallback_close(void);
void *fgb_vt_original(void **vt, size_t slot);
// rslink.cpp: ReShade's effects at the HUD-less point (DLSS add-on on, ReShade in the game folder)
int rsl_attach(HMODULE self, HMODULE reshade, void (*log)(const char *, ...), int (*swapchain_cb)(void *), const wchar_t *state);
int rsl_effects(ID3D11DeviceContext *ctx, ID3D11Resource *bb, ID3D11Resource *depth);
void rsl_camera(const float vp[16]);
void fgb_hud_post(ID3D11Resource *backbuffer);
void *fgb_unhooked(void *fn);
void fgb_set_test_not_nvidia(int on);
void fgb_cpu_stats(double *hud_ms, double *present_ms, double *pace_ms, unsigned *frames);
void fgb_gpu_stats(double ms[4], unsigned *frames, unsigned *lost);
void fgb_wait_stats(unsigned *queued, unsigned *frames);
void fgb_motion_cost(double *gpu_ms, unsigned *gpu_n, double *cpu_ms, unsigned *cpu_n);
void fgb_motion_wind_cost(double *gpu_ms, unsigned *n);
void fgb_gpu_totals(double *interval_ms, unsigned *intervals, double *hud_ms, unsigned *huds);
void fgb_set_latency_cap(int on);
void fgb_latency_stats(double *wait_ms, unsigned *waits, unsigned *frames);
void fgb_set_sr(int on, UINT render_w, UINT render_h);
void fgb_jitter(float *jx, float *jy);
void fgb_set_jitter(float jx, float jy);
void fgb_jitter_at(int d, float *jx, float *jy);
void fgb_sr_stats(unsigned *frames, unsigned *timeouts, double *wait_avg_ms, double *wait_max_ms);
static void jit_apply(ID3D11DeviceContext *c, int force), jit_frame_end(void), cam_copy_write(ID3D11Resource *r, const void *src);
static int is_cam_copy(ID3D11Buffer *b);
unsigned fgb_shown(void);
void fgb_set_fg(unsigned frames_to_generate);
void fgb_set_frame_limit(unsigned us);
void fgb_set_preset(unsigned preset);
int fgb_helper_has_presets(void);
void fgb_set_sharpness(float con);
void fgb_set_vsync(int on);
int fgb_helper_has_vsync(void);
int fgb_driver_vsync(void); // NVIDIA's driver Vertical sync for the helper: 2 / 3 / 4 forced on / off / Fast Sync
unsigned fgb_sr_backend(void), fgb_fg_backend(void); // 0 none, 1 NVIDIA, 2 AMD FSR
void fgb_set_tech(unsigned tech); // 0 NVIDIA only, 1 auto, 2 AMD FSR for both, 3 DLSS + AMD FSR frame generation
int fgb_restart(unsigned tech); // a new helper with this technology (F11 switch, stuck fences)
int fgb_can_restart(void);
int fgb_fences_lost(void);
// the F11 panel (fgpanel.c, Attila's; Rome 2: Super Resolution off / DLAA only so far)
typedef struct { void (*sr)(int mode); void (*fg)(int frames); void (*motion)(int on); void (*cap)(int choice); void (*preset)(int choice); void (*sharp)(int choice); void (*view)(int choice); void (*vsync)(int on); void (*fps)(int on); void (*tech)(int sr, int fg); void (*jtest)(int mode); } FgpApply;
int fgp_init(ID3D11Device *, ID3D11DeviceContext *, UINT bw, UINT bh, DXGI_FORMAT fmt, int sr, int fg, int motion, FgpApply apply);
void fgp_frame(ID3D11Resource *backbuffer, HWND hwnd);
void fgp_set_fps(double game, double shown);
void fgp_set_sr_choices(int n);
void fgp_set_cap(int choice, unsigned fps);
void fgp_set_preset(int choice);
void fgp_set_sharp(int choice);
void fgp_set_presets_available(int on);
void fgp_set_vsync(int on);
void fgp_set_vsync_available(int on);
void fgp_set_vsync_driver(int v);
void fgp_set_fps_counter(int corner);
void fgp_set_key(int vk, const wchar_t *name);
void fgb_release_gpu(void) __attribute__((weak)); // fgbridge.c / r2motion.c: the bridge's video memory back (weak for the offline tests)
void r2m_release(void) __attribute__((weak));
int fgb_d3d12_ok(ID3D11Device *dev) __attribute__((weak)); // fgbridge.c: the card has Direct3D 12 feature level 12_0
const wchar_t *fgb_gpu_name(void);
void fgp_set_fg(int fg);
void fgp_set_tech(int sr, int fg);
void fgp_di8_created(void *di);
int fgp_move_device(ID3D11Device *dev, ID3D11DeviceContext *ctx, UINT bw, UINT bh);
void fgp_set_note(const wchar_t *note);
const wchar_t *fgb_gpu_name(void);
int fgb_d3d12_ok(ID3D11Device *dev); // fgbridge.c: the card has Direct3D 12 feature level 12_0
extern void (*g_dev_di8_created)(void *di); // proxy.c: the game's DirectInput (the panel hides its mouse buttons while over it)
// per-object motion (r2motion.c, dlss/DLSS_LOG.md 19-23)
extern volatile int r2m_internal;
int r2m_init(ID3D11Device *, ID3D11DeviceContext *, ID3D11Texture2D *mv, void (*log)(const char *fmt, ...));
void r2m_set_enabled(int on);
void r2m_set_cull(float px);
void r2m_set_parts(int soldiers, int trees);
float r2m_cull(void);
void r2m_set_view(int on); // dev builds only
void r2m_wind_draw(ID3D11DeviceContext *c, int kind, const void *game_il, const D3D11_INPUT_ELEMENT_DESC *e, UINT n, int indexed, UINT count,
UINT inst, UINT start, INT base, UINT sinst);
extern void (*r2m_after_wind)(void);
void r2m_draw(ID3D11DeviceContext *c, int type, const void *game_il, const D3D11_INPUT_ELEMENT_DESC *e, UINT n, int indexed, UINT count,
UINT inst, UINT start, INT base, UINT sinst);
int r2m_pass(ID3D11DepthStencilView *dsv, const float *cam, UINT rw, UINT rh, float jx, float jy, float min_depth, float max_depth);
void fgb_fill_begin(void); // fgbridge.c: the camera-only vectors kept for the hole fill (before any own-motion draw)
void r2m_frame_end(void);
void r2m_report(void (*log)(const char *fmt, ...));
void r2m_refilled(const void *res);
void r2m_show(ID3D11Resource *bb, UINT rw, UINT rh); // dev builds only
ID3D11Texture2D *fgb_mv_texture(void);
void fgb_set_motion(int (*pass)(ID3D11Resource *depth, float jx, float jy));
static int g_motion, g_motion_on, g_view; // the motion pass is set up / switched on (fgbridge_on.txt "motion", F11 panel); its debug view
unsigned long long g_anim_sum_test[2]; // tests: their own animated shaders' checksums (2 bones, 4 bones)
unsigned long long g_crop_sum_test[2]; // tests: their full-screen terrain stand-in's checksums (vertex, pixel shader)
// live bridge state: 0 not started, 1 running, 2 off for the session (the game presents through the fallback)
static int g_live, g_live_state;
static int g_start_failed; // the add-on is installed but did not start (helper, GPU, Proton): the panel says so (start_failed_panel)
static wchar_t g_helper[MAX_PATH], g_sl[MAX_PATH];
static char g_on_lines[2][MAX_PATH], g_on_test[32]; // fgbridge_on.txt's paths and test options, kept for settings_save
static wchar_t g_settings[MAX_PATH]; // the release add-on's FrameBridge\settings.txt (the F11 panel saves to it); empty in dev runs
static wchar_t g_logdir[MAX_PATH]; // where the helper writes its logs: FrameBridge (release add-on) or the game folder
static unsigned g_fg;
static UINT g_live_w, g_live_h;
static IDXGISwapChain *g_hid_sc; // the game's swapchain on the hidden window
// ReShade (rslink.cpp): it wraps the game's swapchain, so the bridge's Present / GetDesc hooks go on the real one under
// the wrapper (g_rs_sc): ReShade draws its menu before the frame goes to the helper, and its menu reads the game window's keys
static int g_rs, g_rs_tried, g_rs_creating, g_rs_fb; // g_rs_fb: the fallback's Present through ReShade's wrapper
static IDXGISwapChain *g_rs_sc, *g_rs_new;
static HWND g_hid_hwnd; // the hidden window the game's swapchain was made on
static unsigned g_rs_frames; // frames with ReShade's effects at the HUD-less point
static HWND g_out_hwnd; // the game's window (the helper presents into it)
static float g_vp[16]; // this frame's main camera view-projection (camera_VS_PS @144)
static unsigned g_vp_frame = ~0u, g_scene_frame = ~0u, g_hud_frame = ~0u;
static int g_lv_bb, g_lv_scene; // the bound target set: back buffer as target 0 / swapchain-sized scene depth
static UINT g_rt_w, g_rt_h; // its size (render target 0, else the depth buffer)
static int g_rt_overlay; // target 0 is an ocean input (below): 1 overlay, no depth (full size), 2 reflection (bottom-left)
static ID3D11Resource *g_ovres[8]; // the ocean's overlay / reflection textures (one reference held), oldest replaced
static int g_ovkind[8]; // 1 overlay, 2 reflection
static unsigned g_novres, g_ocean_frame = ~0u; // textures found so far; the frame of the last ocean draw
static unsigned g_lv_draws; // draws since the last target change
static ID3D11Resource *g_scene_depth; // one reference held
static ID3D11DepthStencilView *g_scene_dsv; // our own writable view of it (for the depth pass below native size)
static int g_sr; // DLSS SR: 0 off, 1 DLAA, 2..5 Quality..Ultra Performance (fgbridge_on.txt line 4, F11 panel)
static UINT g_rw, g_rh; // render size (the screen with DLAA or off)
static int g_frame_scaled; // this frame started jittered / scaled (its HUD-less copy goes through DLSS)
static unsigned g_br_native; // scene frames shown native (not scaled: the first after a loading screen)
static unsigned g_br_hud, g_br_novp, g_br_msaa, g_fb_frames;
static unsigned g_br_camw, g_br_camw_scene, g_br_scene_frames; // camera writes seen / while the scene depth was bound; scene frames
// time the game spends inside Map / UpdateSubresource while the bridge runs (in game the game's own work per frame took ~13 ms
// with the bridge vs ~5 without, dlss/DLSS_LOG.md 11; guess: the driver stalls these behind the bridge's GPU wait)
static LONGLONG g_t_map, g_t_map_max, g_t_upd, g_t_upd_max, g_t_frame_max, g_t_last_present;
static unsigned g_n_map, g_n_upd, g_n_frames;
static int in_game(unsigned a) { return a >= g_base + 0x1000 && a < g_base + g_size; }
// up to n return-address-looking values inside empire.retail.dll on the stack above sp (the first is the real caller;
// later ones may be stale values, they only help to find the calling function in Ghidra)
static int game_callers(const unsigned *sp, unsigned *out, int n) {
int k = 0;
for (int i = 0; i < 512 && k < n; i++) {
MEMORY_BASIC_INFORMATION m;
if (i % 64 == 0 && (!VirtualQuery(sp + i, &m, sizeof m) || m.State != MEM_COMMIT)) break;
if (in_game(sp[i])) out[k++] = sp[i] - g_base;
}
return k;
}
#define CALLERS(buf) \
unsigned cs_[3] = {0}; \
int ncs_ = game_callers((const unsigned *)__builtin_frame_address(0), cs_, 3); \
char buf[64]; \
snprintf(buf, sizeof buf, "+0x%X +0x%X +0x%X", ncs_ > 0 ? cs_[0] : 0, ncs_ > 1 ? cs_[1] : 0, ncs_ > 2 ? cs_[2] : 0)
static ID3D11Resource *backbuffer(void) {
ID3D11Resource *bb = NULL;
if (g_sc) IDXGISwapChain_GetBuffer(g_sc, 0, &iid_resource, (void **)&bb);
return bb;
}
static void desc_of(ID3D11View *v, char *out, size_t len, ID3D11Resource *bb) {
if (!v) { snprintf(out, len, "-"); return; }
ID3D11Resource *r;
ID3D11View_GetResource(v, &r);
D3D11_RESOURCE_DIMENSION dim;
ID3D11Resource_GetType(r, &dim);
if (dim == D3D11_RESOURCE_DIMENSION_TEXTURE2D) {
D3D11_TEXTURE2D_DESC d;
ID3D11Texture2D_GetDesc((ID3D11Texture2D *)r, &d);
snprintf(out, len, "%s%ux%u f%u b%x a%u m%u s%u @%p", r == bb ? "BACKBUFFER " : "", d.Width, d.Height, d.Format, d.BindFlags,
d.ArraySize, d.MipLevels, d.SampleDesc.Count, (void *)r);
} else snprintf(out, len, "dim%d @%p", dim, (void *)r);
ID3D11Resource_Release(r);
}
// a resource for the capture: size, format, usage and CPU access (staging = read back by the CPU)
static void res_desc(ID3D11Resource *r, char *out, size_t len) {
D3D11_RESOURCE_DIMENSION dim;
ID3D11Resource_GetType(r, &dim);
if (dim == D3D11_RESOURCE_DIMENSION_TEXTURE2D) {
D3D11_TEXTURE2D_DESC d;
ID3D11Texture2D_GetDesc((ID3D11Texture2D *)r, &d);
snprintf(out, len, "%ux%u f%u usage %u cpu 0x%x a%u m%u @%p", d.Width, d.Height, d.Format, d.Usage, d.CPUAccessFlags, d.ArraySize,
d.MipLevels, (void *)r);
} else if (dim == D3D11_RESOURCE_DIMENSION_BUFFER) {
D3D11_BUFFER_DESC d;
ID3D11Buffer_GetDesc((ID3D11Buffer *)r, &d);
snprintf(out, len, "buffer %u bytes usage %u cpu 0x%x @%p", d.ByteWidth, d.Usage, d.CPUAccessFlags, (void *)r);
} else snprintf(out, len, "dim%d @%p", dim, (void *)r);
}
// capture (G7, the campaign flicker below native size): every copy from a texture of the screen's size, and every Map for
// reading (what the CPU reads back, e.g. for the terrain streaming)
static int big_tex(ID3D11Resource *r) {
D3D11_RESOURCE_DIMENSION dim;
ID3D11Resource_GetType(r, &dim);
if (dim != D3D11_RESOURCE_DIMENSION_TEXTURE2D) return 0;
D3D11_TEXTURE2D_DESC d;
ID3D11Texture2D_GetDesc((ID3D11Texture2D *)r, &d);
return d.Width * 4 >= g_live_w && d.Height * 4 >= g_live_h;
}
#ifdef PROFILING // the flicker probe (flickprobe.h, included below)
static void fp_set(ID3D11RenderTargetView *rtv0, ID3D11DepthStencilView *dsv), fp_draw(ID3D11DeviceContext *c), fp_terrain(void),
fp_copy(int kind, ID3D11Resource *dst, ID3D11Resource *src, UINT bytes);
#define FP_COPY(kind, dst, src, bytes) fp_copy(kind, dst, src, bytes)
#else
#define FP_COPY(kind, dst, src, bytes) ((void)0)
#endif
static FILE *g_copylog; // dev: bridge_copies.txt -> 120 frames of copies / reads into bridge_copies.txt.log
static unsigned g_copylog_left;
static FILE *copy_log(void) { return g_capturing && g_cap ? g_cap : g_copylog; }
static void STDMETHODCALLTYPE w_copyres(ID3D11DeviceContext *c, ID3D11Resource *dst, ID3D11Resource *src);
static void STDMETHODCALLTYPE w_copysub(ID3D11DeviceContext *c, ID3D11Resource *dst, UINT ds, UINT x, UINT y, UINT z, ID3D11Resource *src,
UINT ss, const D3D11_BOX *box);
// the targets bound right now; returns 1 if the back buffer is render target 0
static int targets(char *out, size_t len) {
ID3D11RenderTargetView *rtv[2] = {0};
ID3D11DepthStencilView *dsv = NULL;
ID3D11DeviceContext_OMGetRenderTargets(g_ctx, 2, rtv, &dsv);
ID3D11Resource *bb = backbuffer(), *r0 = NULL;
char a[160], b[160], c[160];
desc_of((ID3D11View *)rtv[0], a, sizeof a, bb);
desc_of((ID3D11View *)rtv[1], b, sizeof b, bb);
desc_of((ID3D11View *)dsv, c, sizeof c, bb);
snprintf(out, len, "rt0 [%s] rt1 [%s] depth [%s]", a, b, c);
if (rtv[0]) ID3D11RenderTargetView_GetResource(rtv[0], &r0);
int is_bb = r0 && r0 == bb;
if (r0) ID3D11Resource_Release(r0);
for (int i = 0; i < 2; i++) if (rtv[i]) ID3D11RenderTargetView_Release(rtv[i]);
if (dsv) ID3D11DepthStencilView_Release(dsv);
if (bb) ID3D11Resource_Release(bb);
return is_bb;
}
// this frame's motion vectors (render pixels, R16G16_FLOAT, after the models' pass) at render size:
// bridge_mv_<capture>.raw (u32 width, height, DXGI format, then the rows)
static void dump_mv(void) {
ID3D11Texture2D *mv = fgb_mv_texture();
if (!mv) return;
D3D11_TEXTURE2D_DESC d;
ID3D11Texture2D_GetDesc(mv, &d);
UINT w = g_rw && g_rw <= d.Width ? g_rw : d.Width, h = g_rh && g_rh <= d.Height ? g_rh : d.Height;
d.Usage = D3D11_USAGE_STAGING; d.BindFlags = 0; d.CPUAccessFlags = D3D11_CPU_ACCESS_READ; d.MiscFlags = 0;
ID3D11Texture2D *st = NULL;
if (FAILED(ID3D11Device_CreateTexture2D(g_dev, &d, NULL, &st))) return;
ID3D11DeviceContext_CopyResource(g_ctx, (ID3D11Resource *)st, (ID3D11Resource *)mv);
D3D11_MAPPED_SUBRESOURCE m;
if (SUCCEEDED(ID3D11DeviceContext_Map(g_ctx, (ID3D11Resource *)st, 0, D3D11_MAP_READ, 0, &m))) {
wchar_t p[MAX_PATH];
_snwprintf(p, MAX_PATH, L"%ls\\bridge_mv_%d.raw", g_dir, g_captures);
FILE *f = _wfopen(p, L"wb");
if (f) {
unsigned hdr[3] = {w, h, d.Format};
fwrite(hdr, 4, 3, f);
for (UINT y = 0; y < h; y++) fwrite((const unsigned char *)m.pData + (size_t)y * m.RowPitch, 4, w, f);
fclose(f);
fprintf(g_cap, " -> motion vectors %ux%u to bridge_mv_%d.raw\n", w, h, g_captures);
}
ID3D11DeviceContext_Unmap(g_ctx, (ID3D11Resource *)st, 0);
}
ID3D11Texture2D_Release(st);
}
static unsigned g_seq_left, g_seq_n; // dev: frames still to dump at Present (bridge_seq.txt), the sequence number
// half-size dump of the back buffer into p (header: w, h, format; then 32-bit pixels); 1 when written
static int save_bb(const wchar_t *p) {
ID3D11Resource *bb = backbuffer();
if (!bb) { if (g_cap) fprintf(g_cap, " -> no back buffer to dump\n"); return 0; }
D3D11_TEXTURE2D_DESC d;
ID3D11Texture2D_GetDesc((ID3D11Texture2D *)bb, &d);
HRESULT hr;
if (!g_staging) {
D3D11_TEXTURE2D_DESC s = d;
s.Usage = D3D11_USAGE_STAGING; s.BindFlags = 0; s.CPUAccessFlags = D3D11_CPU_ACCESS_READ; s.MiscFlags = 0;
s.SampleDesc.Count = 1; s.SampleDesc.Quality = 0; s.MipLevels = 1; s.ArraySize = 1;
if (FAILED(hr = ID3D11Device_CreateTexture2D(g_dev, &s, NULL, &g_staging))) {
if (g_cap) fprintf(g_cap, " -> dump: staging texture failed 0x%08lx\n", hr);
ID3D11Resource_Release(bb);
return 0;
}
}
ID3D11DeviceContext_CopyResource(g_ctx, (ID3D11Resource *)g_staging, bb);
ID3D11Resource_Release(bb);
D3D11_MAPPED_SUBRESOURCE m;
if (FAILED(hr = ID3D11DeviceContext_Map(g_ctx, (ID3D11Resource *)g_staging, 0, D3D11_MAP_READ, 0, &m))) {
if (g_cap) fprintf(g_cap, " -> dump: map failed 0x%08lx\n", hr);
return 0;
}
FILE *f = _wfopen(p, L"wb");
if (f) {
unsigned hw = d.Width / 2, hh = d.Height / 2, hdr[3] = {hw, hh, d.Format};
fwrite(hdr, 4, 3, f);
static unsigned row[4096];
for (unsigned y = 0; y < hh && hw <= 4096; y++) {
const unsigned *src = (const unsigned *)((const unsigned char *)m.pData + (size_t)y * 2 * m.RowPitch);
for (unsigned x = 0; x < hw; x++) row[x] = src[x * 2];
fwrite(row, 4, hw, f);
}
fclose(f);
}
ID3D11DeviceContext_Unmap(g_ctx, (ID3D11Resource *)g_staging, 0);
return f != NULL;
}
// bridge_bb_<capture>_<k>.raw during a capture
static void dump_bb(const char *where) {
if (g_dumps >= 40) return;
wchar_t p[MAX_PATH];
_snwprintf(p, MAX_PATH, L"%ls\\bridge_bb_%d_%02u.raw", g_dir, g_captures, g_dumps);
if (!save_bb(p)) return;
fprintf(g_cap, " -> back buffer dump %02u (%s)\n", g_dumps, where);
g_dumps++;
}
// half-size grey picture of the scene depth (R24G8): bridge_depth_<capture>_<k>.raw, same layout as the back-buffer dumps
// (format 28); grey = (1 - depth) stretched between this picture's nearest and farthest pixel
static void dump_depth(const char *where) {
static ID3D11Texture2D *st;
static unsigned k;
if (!g_scene_depth || k >= 8) return;
D3D11_TEXTURE2D_DESC d;
ID3D11Texture2D_GetDesc((ID3D11Texture2D *)g_scene_depth, &d);
if (d.Format != DXGI_FORMAT_R24G8_TYPELESS || d.SampleDesc.Count != 1) return;
if (!st) {
D3D11_TEXTURE2D_DESC s = d;
s.Usage = D3D11_USAGE_STAGING; s.BindFlags = 0; s.CPUAccessFlags = D3D11_CPU_ACCESS_READ; s.MiscFlags = 0;
if (FAILED(ID3D11Device_CreateTexture2D(g_dev, &s, NULL, &st))) return;
}
ID3D11DeviceContext_CopyResource(g_ctx, (ID3D11Resource *)st, g_scene_depth);
D3D11_MAPPED_SUBRESOURCE m;
if (FAILED(ID3D11DeviceContext_Map(g_ctx, (ID3D11Resource *)st, 0, D3D11_MAP_READ, 0, &m))) return;
unsigned hw = d.Width / 2, hh = d.Height / 2, lo = 0xFFFFFF, hi = 0;
for (unsigned y = 0; y < hh; y++)
for (unsigned x = 0; x < hw; x++) {
unsigned v = ((const unsigned *)((const unsigned char *)m.pData + (size_t)y * 2 * m.RowPitch))[x * 2] & 0xFFFFFF;
if (v < 0xFFFFFF) { if (v < lo) lo = v; if (v > hi) hi = v; }
}
wchar_t p[MAX_PATH];
_snwprintf(p, MAX_PATH, L"%ls\\bridge_depth_%d_%u.raw", g_dir, g_captures, k);
FILE *f = _wfopen(p, L"wb");
if (f) {
unsigned hdr[3] = {hw, hh, 28};
fwrite(hdr, 4, 3, f);
static unsigned row[4096];
for (unsigned y = 0; y < hh && hw <= 4096; y++) {
const unsigned *src = (const unsigned *)((const unsigned char *)m.pData + (size_t)y * 2 * m.RowPitch);
for (unsigned x = 0; x < hw; x++) {
unsigned v = src[x * 2] & 0xFFFFFF;
unsigned g = v >= 0xFFFFFF || hi <= lo ? 0 : 255 - (unsigned)(255.0 * (v - lo) / (hi - lo));
row[x] = 0xFF000000u | g << 16 | g << 8 | g;
}
fwrite(row, 4, hw, f);
}
fclose(f);
}
ID3D11DeviceContext_Unmap(g_ctx, (ID3D11Resource *)st, 0);
fprintf(g_cap, " -> depth dump %u (%s): nearest 0x%06X farthest 0x%06X\n", k, where, lo, hi);
k++;
}
// ---- context hooks (vtable slots). d3d11.dll switches the immediate context between several vtables (offline test: Draw and
// Clear were not seen after the first frames) and can put its own entries back (Attila), so every table seen is patched
// and the original function is looked up by the table the call came through.
typedef void(STDMETHODCALLTYPE *omrt_fn)(ID3D11DeviceContext *, UINT, ID3D11RenderTargetView *const *, ID3D11DepthStencilView *);
typedef void(STDMETHODCALLTYPE *omrtuav_fn)(ID3D11DeviceContext *, UINT, ID3D11RenderTargetView *const *, ID3D11DepthStencilView *, UINT,
UINT, ID3D11UnorderedAccessView *const *, const UINT *);
typedef void(STDMETHODCALLTYPE *cbs_fn)(ID3D11DeviceContext *, UINT, UINT, ID3D11Buffer *const *);
typedef void(STDMETHODCALLTYPE *crtv_fn)(ID3D11DeviceContext *, ID3D11RenderTargetView *, const FLOAT[4]);
typedef void(STDMETHODCALLTYPE *cdsv_fn)(ID3D11DeviceContext *, ID3D11DepthStencilView *, UINT, FLOAT, UINT8);
typedef HRESULT(STDMETHODCALLTYPE *map_fn)(ID3D11DeviceContext *, ID3D11Resource *, UINT, D3D11_MAP, UINT, D3D11_MAPPED_SUBRESOURCE *);
typedef void(STDMETHODCALLTYPE *unmap_fn)(ID3D11DeviceContext *, ID3D11Resource *, UINT);
typedef void(STDMETHODCALLTYPE *upd_fn)(ID3D11DeviceContext *, ID3D11Resource *, UINT, const D3D11_BOX *, const void *, UINT, UINT);
typedef void(STDMETHODCALLTYPE *dip_fn)(ID3D11DeviceContext *, UINT, UINT, INT); // DrawIndexed
typedef void(STDMETHODCALLTYPE *d_fn)(ID3D11DeviceContext *, UINT, UINT); // Draw
typedef void(STDMETHODCALLTYPE *dii_fn)(ID3D11DeviceContext *, UINT, UINT, UINT, INT, UINT); // DrawIndexedInstanced
typedef void(STDMETHODCALLTYPE *di_fn)(ID3D11DeviceContext *, UINT, UINT, UINT, UINT); // DrawInstanced
typedef void(STDMETHODCALLTYPE *rsvp_fn)(ID3D11DeviceContext *, UINT, const D3D11_VIEWPORT *);
typedef void(STDMETHODCALLTYPE *rsgetvp_fn)(ID3D11DeviceContext *, UINT *, D3D11_VIEWPORT *);
typedef void(STDMETHODCALLTYPE *omdss_fn)(ID3D11DeviceContext *, ID3D11DepthStencilState *, UINT);
typedef void(STDMETHODCALLTYPE *copyres_fn)(ID3D11DeviceContext *, ID3D11Resource *, ID3D11Resource *);
typedef void(STDMETHODCALLTYPE *copysub_fn)(ID3D11DeviceContext *, ID3D11Resource *, UINT, UINT, UINT, UINT, ID3D11Resource *, UINT, const D3D11_BOX *);
typedef void(STDMETHODCALLTYPE *disp_fn)(ID3D11DeviceContext *, UINT, UINT, UINT);
typedef void(STDMETHODCALLTYPE *psset_fn)(ID3D11DeviceContext *, ID3D11PixelShader *, ID3D11ClassInstance *const *, UINT);
enum { S_OMRT, S_OMRTUAV, S_VSCB, S_CRTV, S_CDSV, S_MAP, S_UNMAP, S_UPD, S_DIP, S_D, S_DII, S_DI, S_RSVP, S_RSGETVP, S_OMDSS, S_PSCB, S_VSSET, S_PSSAMP, S_DISP, S_PSSET,
S_COPYRES, S_COPYSUB, S_PSSRV, S_VSSRV, S_IALAYOUT, NSLOT };
static struct { void **vt; void *orig[NSLOT]; int rw; } g_vts[16]; // rw: the table is in writable memory
static unsigned g_rh_calls, g_rh_patches, g_rh_foreign; // re-hook checks, slots hooked again, foreign slots left alone
static LONGLONG g_rh_ticks; // QPC ticks spent where a slot differed
static int g_nvt;
static void *orig_of(const void *obj, int s) {
void **vt = *(void ***)obj;
for (int i = 0; i < g_nvt; i++)
if (g_vts[i].vt == vt && g_vts[i].orig[s]) return g_vts[i].orig[s];
for (int i = 0; i < g_nvt; i++) // a table that copied our entry from another one: that table's original
if (g_vts[i].orig[s]) return g_vts[i].orig[s];
return NULL;
}
#define ORIG(type, s, c) ((type)orig_of((c), (s)))
static void hook_context(void);
// a target set ends: dump the back buffer if the game drew into it
static void set_end(const char *why) {
if (g_set_bb && g_set_draws) {
char w[80];
snprintf(w, sizeof w, "%u draws into the back buffer, %s", g_set_draws, why);
dump_bb(w);
}
}
static void set_begin(int is_bb) { g_set_bb = is_bb; g_set_draws = 0; }
static void log_targets(const char *what, const char *callers) {
char t[600];
set_end("before the next target change");
int bb = targets(t, sizeof t);
fprintf(g_cap, "#%u %s (game %s), draws so far %u: %s\n", g_sets++, what, callers, g_draws, t);
set_begin(bb);
}
// live bridge, at every target change of the game's context (before it takes effect): the HUD-less point is the change
// after draws into the back buffer once the scene depth was drawn into this frame (captures: the 5 draws of the scene's
// copy into the back buffer; the banners and the HUD follow). Before the scene (4 back-buffer draws at frame start) it
// is not.
static void fl_hud(void); // flash probe (below)
static const void *g_lv_rtv, *g_lv_dsv; // the target 0 / depth views the glue last saw bound (pointers only)
static int rt_sync(ID3D11DeviceContext *c); // (below: a target change the hooks did not see)
static void live_targets(ID3D11RenderTargetView *rtv0, ID3D11DepthStencilView *dsv) {
g_lv_rtv = rtv0, g_lv_dsv = dsv;
#ifdef PROFILING
fp_set(rtv0, dsv);
#endif
if (g_lv_bb && g_lv_draws && g_scene_frame == g_frame && g_hud_frame != g_frame) {
g_hud_frame = g_frame;
fl_hud();
jit_frame_end(); // the UI and what follows: the game's own viewports
ID3D11Resource *bb = backbuffer();
int cam = g_vp_frame == g_frame;
if (!cam) g_br_novp++; // no main camera seen: no motion vectors (a scaled frame still goes through DLSS, last camera)
// ReShade: its effects before the HUD-less copy (frame generation's two images both have them), or with DLSS SR
// on DLSS's output after it (full size, the depth back at full size too), then handed to the helper as frame
// generation's HUD-less image
int sr_trip = g_sr && g_frame_scaled && bb && g_scene_depth; // DLSS SR runs in this frame's fgb_hudless
if (g_rs && bb && !sr_trip) g_rs_frames += rsl_effects(g_ctx, bb, g_scene_depth);
if (g_sr && !g_frame_scaled) g_br_native++; // a scene frame not scaled (the first after loading): native, no DLSS
else if ((cam || g_frame_scaled) && bb && g_scene_depth) {
// below native size fgbridge.c puts the depth back to full size for what is drawn after (banners), on the
// depth bound now; Rome 2's HUD-less point comes after a back-buffer pass without depth, so the scene's is bound
if (g_capturing && g_cap) dump_depth("at the HUD-less point, before the bridge restores it");
if ((g_rw != g_live_w || g_rh != g_live_h) && g_scene_dsv) ORIG(omrt_fn, S_OMRT, g_ctx)(g_ctx, 0, NULL, g_scene_dsv);
fgb_hudless(bb, g_scene_depth, g_vp);
g_br_hud++;
}
if (g_rs && sr_trip && rsl_effects(g_ctx, bb, g_scene_depth)) { g_rs_frames++; fgb_hud_post(bb); }
if (g_rs && cam) rsl_camera(g_vp);
if (bb) ID3D11Resource_Release(bb);
}
ID3D11Resource *r = NULL, *bb = backbuffer();
D3D11_RESOURCE_DIMENSION dim;
D3D11_TEXTURE2D_DESC td = {0};
if (rtv0) ID3D11RenderTargetView_GetResource(rtv0, &r);
if (r) ID3D11Resource_GetType(r, &dim);
if (r && dim == D3D11_RESOURCE_DIMENSION_TEXTURE2D) ID3D11Texture2D_GetDesc((ID3D11Texture2D *)r, &td);
g_rt_w = td.Width;
g_rt_h = td.Height;
g_lv_bb = r && r == bb;
g_rt_overlay = 0;
for (unsigned k = 0; r && k < g_novres && k < 8; k++)
if (g_ovres[k] == r && (g_ovkind[k] == 1 ? !dsv : g_ocean_frame != g_frame)) g_rt_overlay = g_ovkind[k];
if (r) ID3D11Resource_Release(r);
if (bb) ID3D11Resource_Release(bb);
g_lv_draws = 0;
g_lv_scene = 0;
if (!dsv) return;
ID3D11Resource *d = NULL;
ID3D11DepthStencilView_GetResource(dsv, &d);
ID3D11Resource_GetType(d, &dim);
memset(&td, 0, sizeof td);
if (dim == D3D11_RESOURCE_DIMENSION_TEXTURE2D) ID3D11Texture2D_GetDesc((ID3D11Texture2D *)d, &td);
if (!rtv0) { g_rt_w = td.Width; g_rt_h = td.Height; }
if (td.Width == g_live_w && td.Height == g_live_h) {
if (td.SampleDesc.Count > 1) { // the game's MSAA: DLSS needs it off (its own anti-aliasing replaces it)
if (!g_br_msaa++) g_log("frame bridge: the game renders with MSAA (%u samples) - set anti-aliasing off in the game's "
"graphics options for DLSS; until then frames go out without motion vectors", td.SampleDesc.Count);
} else {
g_lv_scene = 1;
if (g_scene_frame != g_frame) g_br_scene_frames++;
g_scene_frame = g_frame;
if (d != g_scene_depth) {
if (g_scene_depth) ID3D11Resource_Release(g_scene_depth);
if (g_scene_dsv) ID3D11DepthStencilView_Release(g_scene_dsv);
g_scene_dsv = NULL; // made again for the new depth below
g_scene_depth = d;
d = NULL; // the reference is kept
}
if (!g_scene_dsv) {
// our own writable view of it, for the depth pass below native size: the game binds the depth read-only
// in its lighting / fog passes (to sample it at the same time), and writes through that view are dropped -
// the restored depth stayed shrunk and cut the banners (capture 2026-10-29)
D3D11_DEPTH_STENCIL_VIEW_DESC vd;
ID3D11DepthStencilView_GetDesc(dsv, &vd);
vd.Flags = 0;
if (FAILED(ID3D11Device_CreateDepthStencilView(g_dev, g_scene_depth, &vd, &g_scene_dsv))) g_scene_dsv = NULL;
}
}
}
if (d) ID3D11Resource_Release(d);
}
// AMD FSR's reactive mask (Attila 6.10.0, AGENT_LOG 163): the transparent effects' stage (smoke, fire, other effects drawn into
// the lit HDR scene with depth, before bloom and tone mapping) - the scene is copied when it starts and compared when the
// next target is set. ToB (captures g1 / g2, battles): the game's targets for it are set with +0x17EB200 among the game
// values on the stack (+0x180F065 +0x179BEC0 +0x17EB200), the R11G11B10 HDR scene with the scene depth (#35; the set before
// it, #34, the same format with another depth and no draws, is ended at once and overwritten). ToB 64A47C87 only: its
// bytes there are checked at the bridge start, other builds get no mask (logged). Not seen in the campaign captures.
void fgb_react_begin(ID3D11Resource *hdr);
void fgb_react_end(void);
unsigned fgb_react_frames(void);
static void lod_slots(ID3D11DeviceContext *c); // the mip bias slot fix (below w_pssamp)
static unsigned g_fx_ret; // the stage's return address (rva), 0: not this game build
static int g_fx_in;
static void fx_targets(ID3D11DeviceContext *c, UINT n, ID3D11RenderTargetView *const *rtv, ID3D11DepthStencilView *dsv) {
if (g_fx_in) { g_fx_in = 0; fgb_react_end(); return; }
if (!g_fx_ret || !n || !rtv || !rtv[0] || !dsv || !g_lv_scene || c != g_ctx || fgb_sr_backend() != 2) return;
D3D11_RENDER_TARGET_VIEW_DESC rd;
ID3D11RenderTargetView_GetDesc(rtv[0], &rd);
if (rd.Format != DXGI_FORMAT_R11G11B10_FLOAT) return;
unsigned cs[3] = {0};
int k = game_callers((const unsigned *)__builtin_frame_address(0), cs, 3);
if (!(k > 1 && cs[1] == g_fx_ret) && !(k > 2 && cs[2] == g_fx_ret)) return;
ID3D11Resource *r = NULL;
ID3D11RenderTargetView_GetResource(rtv[0], &r);
if (r) { fgb_react_begin(r); ID3D11Resource_Release(r); g_fx_in = 1; }
}
static void STDMETHODCALLTYPE w_omrt(ID3D11DeviceContext *c, UINT n, ID3D11RenderTargetView *const *rtv, ID3D11DepthStencilView *dsv) { if (r2m_internal) { ORIG(omrt_fn, S_OMRT, c)(c, n, rtv, dsv); return; }
if (g_live_state == 1 && c == g_ctx) live_targets(n && rtv ? rtv[0] : NULL, dsv);
ORIG(omrt_fn, S_OMRT, c)(c, n, rtv, dsv);
// d3d11 puts its own Draw entries back into the table during a frame (bridgelive_test with the real bridge: after
// frame 0 the composite's draws were no longer seen, the vtable pointer unchanged): hooked again at each target change
if ((g_capturing || g_camera_left || g_live_state == 1) && c == g_ctx) hook_context();
if (g_live_state == 1 && c == g_ctx) jit_apply(c, 0);
if (g_live_state == 1 && c == g_ctx) fx_targets(c, n, rtv, dsv), lod_slots(c);
if (g_capturing && c == g_ctx) { CALLERS(cs); log_targets("OMSetRenderTargets", cs); }
}
static void STDMETHODCALLTYPE w_omrtuav(ID3D11DeviceContext *c, UINT n, ID3D11RenderTargetView *const *rtv, ID3D11DepthStencilView *dsv,
UINT us, UINT un, ID3D11UnorderedAccessView *const *uav, const UINT *ic) { if (r2m_internal) { ORIG(omrtuav_fn, S_OMRTUAV, c)(c, n, rtv, dsv, us, un, uav, ic); return; }
if (g_live_state == 1 && c == g_ctx && n != D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL) live_targets(n && rtv ? rtv[0] : NULL, dsv);
ORIG(omrtuav_fn, S_OMRTUAV, c)(c, n, rtv, dsv, us, un, uav, ic);
if (g_live_state == 1 && c == g_ctx) { hook_context(); jit_apply(c, 0); lod_slots(c); }
if (g_capturing && c == g_ctx && n != D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL) { CALLERS(cs); log_targets("OMSetRenderTargetsAndUAVs", cs); }
}
static void STDMETHODCALLTYPE w_vscb(ID3D11DeviceContext *c, UINT start, UINT n, ID3D11Buffer *const *b) { if (r2m_internal) { ORIG(cbs_fn, S_VSCB, c)(c, start, n, b); return; }
ORIG(cbs_fn, S_VSCB, c)(c, start, n, b);
// live too: in game the camera's Map / Unmap were missed in about 2 of 3 frames with the re-hook at target changes
// only (first in-game run, 2026-10-28); the capture, which also re-hooked here, saw every camera write
if ((g_capturing || g_camera_left || g_live_state == 1) && c == g_ctx) hook_context();
if (g_capturing && c == g_ctx && start == 0 && n && b && b[0]) {
D3D11_BUFFER_DESC d;
ID3D11Buffer_GetDesc(b[0], &d);
CALLERS(cs);
fprintf(g_cap, " VS cb0 = @%p (%u bytes) (game %s), draws so far %u\n", (void *)b[0], d.ByteWidth, cs, g_draws);
}
}
static void STDMETHODCALLTYPE w_crtv(ID3D11DeviceContext *c, ID3D11RenderTargetView *v, const FLOAT col[4]) { if (r2m_internal) { ORIG(crtv_fn, S_CRTV, c)(c, v, col); return; }
ORIG(crtv_fn, S_CRTV, c)(c, v, col);
if (g_capturing && c == g_ctx) {
char t[160];
ID3D11Resource *bb = backbuffer();
desc_of((ID3D11View *)v, t, sizeof t, bb);
if (bb) ID3D11Resource_Release(bb);
CALLERS(cs);
fprintf(g_cap, " clear colour [%s] (game %s)\n", t, cs);
}
}
static void STDMETHODCALLTYPE w_cdsv(ID3D11DeviceContext *c, ID3D11DepthStencilView *v, UINT fl, FLOAT depth, UINT8 st) { if (r2m_internal) { ORIG(cdsv_fn, S_CDSV, c)(c, v, fl, depth, st); return; }
ORIG(cdsv_fn, S_CDSV, c)(c, v, fl, depth, st);
if (g_capturing && c == g_ctx) {
char t[160];
desc_of((ID3D11View *)v, t, sizeof t, NULL);
CALLERS(cs);
fprintf(g_cap, " clear depth [%s] flags %u to %g stencil %u (game %s)\n", t, fl, depth, st, cs);
}
}
// camera constant buffers: constant buffers of 544..576 bytes (camera_VS_PS uses 548)
static ID3D11Resource *g_cam_res;
static void *g_cam_ptr;
static int is_cam_buffer(ID3D11Resource *r) {
D3D11_RESOURCE_DIMENSION dim;
ID3D11Resource_GetType(r, &dim);
if (dim != D3D11_RESOURCE_DIMENSION_BUFFER) return 0;
D3D11_BUFFER_DESC d;
ID3D11Buffer_GetDesc((ID3D11Buffer *)r, &d);
return (d.BindFlags & D3D11_BIND_CONSTANT_BUFFER) && d.ByteWidth >= 544 && d.ByteWidth <= 576;
}
static void cam_record(ID3D11Resource *r, const void *data, const char *how) {
unsigned cs[3] = {0};
game_callers((const unsigned *)__builtin_frame_address(0), cs, 3);
unsigned rec[8] = {0x43414D31u /* "CAM1" */, g_frame, (unsigned)(UINT_PTR)r, cs[0], cs[1], cs[2], 544, 0};
fwrite(rec, 4, 8, g_cam);
fwrite(data, 1, 544, g_cam);
g_cam_writes++;
if (g_capturing) {
const float *f = (const float *)data;
fprintf(g_cap, " camera write %s @%p (game +0x%X +0x%X): position (%.2f %.2f %.2f) screen (%.0f %.0f) viewport (%.0f %.0f %.0f %.0f) vp row0 (%.4f %.4f %.4f %.4f), draws so far %u\n",
how, (void *)r, cs[0], cs[1], f[0], f[1], f[2], f[108], f[109], f[116], f[117], f[118], f[119], f[36], f[37], f[38], f[39], g_draws);
}
}
// a camera write while the scene depth is bound is the main view (captures: the shadow, reflection and environment views
// are written with other depth buffers or none): its view_projection (@144) is the frame's camera
static int g_mp; // the motion probe is on (below; 2: capture at once, for the offline test)
static FILE *g_mpf; // its capture frame's file
static void mp_map(ID3D11Resource *r, D3D11_MAP t);
static float g_cam_block[140]; // the main camera block, kept for the motion probe
// flash probe (debug view on, DLSS_LOG 27): every main-camera candidate of the last 8 frames, to explain a frame whose
// motion vectors jump over the whole screen
typedef struct { const void *res; unsigned draws; float nearz, farz, vp[16]; } FlCand;
typedef struct { unsigned frame, n, hud, gap; float change, vp[16]; FlCand c[24]; } FlFrame;
static FlFrame g_fl[8];
static ID3D11Resource *g_fl_res; // the buffer of the camera write being taken
static unsigned g_fl_last_hud = ~0u;
static float g_fl_last_vp[16];
static FlFrame *fl_frame(void) {
FlFrame *f = &g_fl[g_frame & 7];
if (f->frame != g_frame) { memset(f, 0, sizeof *f); f->frame = g_frame; }
return f;
}
static void live_camera(const void *cam) {
memcpy(g_vp, (const char *)cam + 144, 64);
g_vp_frame = g_frame;
if (g_mp) memcpy(g_cam_block, cam, sizeof g_cam_block);
if (g_view) {
FlFrame *f = fl_frame();
if (f->n < 24) {
FlCand *k = &f->c[f->n];
k->res = g_fl_res; k->draws = g_draws;
k->nearz = ((const float *)cam)[100]; k->farz = ((const float *)cam)[101];
memcpy(k->vp, (const char *)cam + 144, 64);
}
f->n++;
}
}
static float fl_change(const float *a, const float *b) { // relative size of the difference of two matrices
double d = 0, s = 0;
for (int i = 0; i < 16; i++) d += (double)(a[i] - b[i]) * (a[i] - b[i]), s += (double)a[i] * a[i];
return s > 0 ? (float)sqrt(d / s) : 0;
}
static void fl_hud(void) { // at the HUD-less point: the camera taken, its change from the last HUD-less frame's
if (!g_view) return;
FlFrame *f = fl_frame();
f->hud = 1;
memcpy(f->vp, g_vp, 64);
f->gap = g_fl_last_hud == ~0u ? 0 : g_frame - g_fl_last_hud;
f->change = g_fl_last_hud == ~0u ? 0 : fl_change(g_vp, g_fl_last_vp);
memcpy(g_fl_last_vp, g_vp, 64);
g_fl_last_hud = g_frame;
}
static void fl_log_frame(const FlFrame *f, const char *what) {
g_log(" %s frame %u: HUD-less %d, frames since the last HUD-less %u, camera change %.4f, %u camera writes with the scene depth:",
what, f->frame, f->hud, f->gap, f->change, f->n);
for (unsigned i = 0; i < f->n && i < 24; i++) {
const FlCand *k = &f->c[i];
g_log(" #%u buffer %p at draw %u: near %g far %g, differs from the taken %.4f; rows %.4f %.4f %.4f %.4f | %.4f %.4f %.4f %.4f | "
"%.4f %.4f %.4f %.4f | %.4f %.4f %.4f %.4f", i, k->res, k->draws, k->nearz, k->farz, fl_change(k->vp, f->vp), k->vp[0], k->vp[1],
k->vp[2], k->vp[3], k->vp[4], k->vp[5], k->vp[6], k->vp[7], k->vp[8], k->vp[9], k->vp[10], k->vp[11], k->vp[12], k->vp[13],
k->vp[14], k->vp[15]);
}
}
static float fl_h2f(unsigned short h) {
unsigned e = (h >> 10) & 31, m = h & 1023;
float v = e == 31 ? 65504 : e ? ldexpf((float)(m | 1024), (int)e - 25) : ldexpf((float)m, -24); // inf / NaN read as the largest
return h >> 15 ? -v : v;
}
static unsigned g_fl_flashes, g_fl_multi, g_fl_frames;
static float g_fl_max_change;
// at Present (debug view on, a frame with vectors): 9 motion vectors copied for reading 3 frames later (no wait); a frame
// whose median motion is over 8 px and 4x the recent median is a flash: logged with its camera candidates
static void fl_present(ID3D11Texture2D *mv) {
static ID3D11Texture2D *st[4];
static unsigned st_frame[4], rw[4], rh[4];
static float hist[30];
static unsigned nhist;
if (!st[0]) {
D3D11_TEXTURE2D_DESC d = {9, 1, 1, 1, DXGI_FORMAT_R16G16_FLOAT, {1, 0}, D3D11_USAGE_STAGING, 0, D3D11_CPU_ACCESS_READ, 0};
for (int i = 0; i < 4; i++) if (FAILED(ID3D11Device_CreateTexture2D(g_dev, &d, NULL, &st[i]))) return;
for (int i = 0; i < 4; i++) st_frame[i] = ~0u;
}
FlFrame *f = fl_frame();
g_fl_frames++;
int other = 0; // a frame with a camera candidate other than the one taken
for (unsigned i = 0; i < f->n && i < 24; i++) other |= fl_change(f->c[i].vp, f->vp) > 1e-5f;
g_fl_multi += other;
if (f->change > g_fl_max_change) g_fl_max_change = f->change;
// the copy 3 frames ago, if the GPU has finished it
int s = (int)(g_frame & 3);
D3D11_MAPPED_SUBRESOURCE m;
if (st_frame[s] != ~0u && SUCCEEDED(ID3D11DeviceContext_Map(g_ctx, (ID3D11Resource *)st[s], 0, D3D11_MAP_READ, D3D11_MAP_FLAG_DO_NOT_WAIT, &m))) {
float g[9], sorted[9];
for (int i = 0; i < 9; i++) {
const unsigned short *h = (const unsigned short *)m.pData + i * 2;
float x = fl_h2f(h[0]), y = fl_h2f(h[1]);
g[i] = sorted[i] = sqrtf(x * x + y * y);
}
ID3D11DeviceContext_Unmap(g_ctx, (ID3D11Resource *)st[s], 0);
for (int i = 1; i < 9; i++) for (int j = i; j > 0 && sorted[j] < sorted[j - 1]; j--) { float t = sorted[j]; sorted[j] = sorted[j - 1]; sorted[j - 1] = t; }
float med = sorted[4], hm = 0;
if (nhist) {
float hs[30];
unsigned n = nhist < 30 ? nhist : 30;
memcpy(hs, hist, n * sizeof *hs);
for (unsigned i = 1; i < n; i++) for (unsigned j = i; j > 0 && hs[j] < hs[j - 1]; j--) { float t = hs[j]; hs[j] = hs[j - 1]; hs[j - 1] = t; }
hm = hs[n / 2];
}
unsigned fr = st_frame[s];
if (nhist >= 10 && med > 8 && med > 4 * hm && g_fl_flashes++ < 30) {
g_log("motion flash at frame %u (render %ux%u): median motion %.1f px (recent median %.1f); the 9 points %.1f %.1f %.1f | %.1f %.1f "
"%.1f | %.1f %.1f %.1f", fr, rw[s], rh[s], med, hm, g[0], g[1], g[2], g[3], g[4], g[5], g[6], g[7], g[8]);
const FlFrame *a = &g_fl[fr & 7];
if (a->frame == fr) {
fl_log_frame(a, "this");
for (unsigned b = fr - 1, k = 0; k < 6; b--, k++) // and the last HUD-less frame before it
if (g_fl[b & 7].frame == b && g_fl[b & 7].hud) { fl_log_frame(&g_fl[b & 7], "previous HUD-less"); break; }
} else g_log(" (its camera record is gone)");
}
hist[nhist++ % 30] = med;
st_frame[s] = ~0u;
}
if (st_frame[s] == ~0u) { // this frame's 9 points (a 3 x 3 grid over the render size)
for (int i = 0; i < 9; i++) {
UINT x = g_rw * (1 + 2 * (i % 3)) / 6, y = g_rh * (1 + 2 * (i / 3)) / 6;
D3D11_BOX b = {x, y, 0, x + 1, y + 1, 1};
ID3D11DeviceContext_CopySubresourceRegion(g_ctx, (ID3D11Resource *)st[s], 0, (UINT)i, 0, 0, (ID3D11Resource *)mv, 0, &b);
}
st_frame[s] = g_frame; rw[s] = g_rw; rh[s] = g_rh;
}
}
static void STDMETHODCALLTYPE w_copyres(ID3D11DeviceContext *c, ID3D11Resource *dst, ID3D11Resource *src) {
if (c == g_ctx && g_live_state == 1) hook_context(); // (re-hook at every hooked call, G8)
if (!r2m_internal && copy_log() && c == g_ctx && src && dst && big_tex(src)) {
char a[160], b[160];
CALLERS(cs);
res_desc(src, a, sizeof a), res_desc(dst, b, sizeof b);
fprintf(copy_log(), " copy (whole) %s -> %s (game %s), draws so far %u\n", a, b, cs, g_draws);
}
if (!r2m_internal && c == g_ctx && dst) FP_COPY(0, dst, src, 0);
ORIG(copyres_fn, S_COPYRES, c)(c, dst, src);
}
static void STDMETHODCALLTYPE w_copysub(ID3D11DeviceContext *c, ID3D11Resource *dst, UINT ds, UINT x, UINT y, UINT z, ID3D11Resource *src,
UINT ss, const D3D11_BOX *box) {
if (!r2m_internal && c == g_ctx && g_live_state == 1) hook_context(); // (re-hook at every hooked call, G8)
if (!r2m_internal && copy_log() && c == g_ctx && src && dst && big_tex(src)) {
char a[160], b[160];
CALLERS(cs);
res_desc(src, a, sizeof a), res_desc(dst, b, sizeof b);
fprintf(copy_log(), " copy (part %u,%u-%u,%u) %s -> %s at %u,%u (game %s), draws so far %u\n", box ? box->left : 0, box ? box->top : 0,
box ? box->right : 0, box ? box->bottom : 0, a, b, x, y, cs, g_draws);
}
if (!r2m_internal && c == g_ctx && dst) FP_COPY(1, dst, src, box ? (box->right - box->left) * (box->bottom - box->top) : 0);
ORIG(copysub_fn, S_COPYSUB, c)(c, dst, ds, x, y, z, src, ss, box);
}
static HRESULT STDMETHODCALLTYPE w_map(ID3D11DeviceContext *c, ID3D11Resource *r, UINT sub, D3D11_MAP t, UINT fl, D3D11_MAPPED_SUBRESOURCE *m) { if (r2m_internal) return ORIG(map_fn, S_MAP, c)(c, r, sub, t, fl, m);
if (c == g_ctx && g_live_state == 1) hook_context(); // (re-hook at every hooked call, G8)
if (copy_log() && c == g_ctx && r && (t == D3D11_MAP_READ || t == D3D11_MAP_READ_WRITE)) {
char a[160];
CALLERS(cs);
res_desc(r, a, sizeof a);
fprintf(copy_log(), " map for reading %s flags 0x%x (game %s), draws so far %u\n", a, fl, cs, g_draws);
}
if (c == g_ctx && r && (t == D3D11_MAP_READ || t == D3D11_MAP_READ_WRITE)) FP_COPY(3, r, NULL, 0);
LARGE_INTEGER t0 = {0}, t1;
if (g_live_state == 1) QueryPerformanceCounter(&t0);
HRESULT hr = ORIG(map_fn, S_MAP, c)(c, r, sub, t, fl, m);
if (t0.QuadPart) {
QueryPerformanceCounter(&t1);
LONGLONG d = t1.QuadPart - t0.QuadPart;
g_t_map += d;
if (d > g_t_map_max) g_t_map_max = d;
g_n_map++;
}
if (((g_camera_left && g_cam) || g_live_state == 1) && c == g_ctx && SUCCEEDED(hr) && m && (t == D3D11_MAP_WRITE_DISCARD || t == D3D11_MAP_WRITE) &&
is_cam_buffer(r)) {
g_cam_res = r;
g_cam_ptr = m->pData;
}
if (g_mpf && c == g_ctx && SUCCEEDED(hr)) mp_map(r, t); // motion probe capture frame: the vertex buffer Map pattern
if (g_motion && c == g_ctx && SUCCEEDED(hr) && t == D3D11_MAP_WRITE_DISCARD) r2m_refilled(r); // a recorded tree draw's data is gone
return hr;
}
static void STDMETHODCALLTYPE w_unmap(ID3D11DeviceContext *c, ID3D11Resource *r, UINT sub) { if (r2m_internal) { ORIG(unmap_fn, S_UNMAP, c)(c, r, sub); return; }
if (c == g_ctx && g_live_state == 1) hook_context(); // (re-hook at every hooked call, G8)
if (g_cam_res == r && g_cam_ptr && c == g_ctx) {
if (g_cam) cam_record(r, g_cam_ptr, "map");
if (g_live_state == 1) rt_sync(c); // (a target change it did not see: the camera rule needs the scene depth bound, G8)
if (g_live_state == 1) g_br_camw++, g_br_camw_scene += g_lv_scene, cam_copy_write(r, g_cam_ptr);
g_fl_res = r;
if (g_lv_scene && g_hud_frame != g_frame) live_camera(g_cam_ptr); // not after the HUD-less point (a UI portrait, G1)
g_cam_res = NULL;
g_cam_ptr = NULL;
}
ORIG(unmap_fn, S_UNMAP, c)(c, r, sub);
}
static void STDMETHODCALLTYPE w_upd(ID3D11DeviceContext *c, ID3D11Resource *r, UINT sub, const D3D11_BOX *box, const void *data, UINT rp, UINT dp) { if (r2m_internal) { ORIG(upd_fn, S_UPD, c)(c, r, sub, box, data, rp, dp); return; }
if (c == g_ctx && g_live_state == 1) hook_context(); // (re-hook at every hooked call, G8)
if (c == g_ctx && r) FP_COPY(2, r, NULL, box ? rp * (box->bottom - box->top) : 0);
if (((g_camera_left && g_cam) || g_live_state == 1) && c == g_ctx && !box && data && is_cam_buffer(r)) {
if (g_camera_left && g_cam) cam_record(r, data, "update");
if (g_live_state == 1) rt_sync(c);
if (g_live_state == 1) g_br_camw++, g_br_camw_scene += g_lv_scene, cam_copy_write(r, data);
g_fl_res = r;
if (g_lv_scene && g_hud_frame != g_frame) live_camera(data);
}
LARGE_INTEGER t0 = {0}, t1;
if (g_live_state == 1) QueryPerformanceCounter(&t0);
if (g_motion && c == g_ctx) r2m_refilled(r);
ORIG(upd_fn, S_UPD, c)(c, r, sub, box, data, rp, dp);
if (t0.QuadPart) {
QueryPerformanceCounter(&t1);
LONGLONG d = t1.QuadPart - t0.QuadPart;
g_t_upd += d;
if (d > g_t_upd_max) g_t_upd_max = d;
g_n_upd++;
}
}
// ---- Motion probe (per-object motion vectors, game run G1 of dlss/DLSS_LOG.md 19; fgbridge_on.txt line 4 "motionprobe").
// Records by itself, once per scene kind (battle: camera far plane > 2000, campaign map: below), after animated models
// have been drawn for 20 s, three consecutive frames into motion_<kind>_<k>.bin: every main-view draw (scene depth
// bound) whose vertex shader animates (its bytecode names the cbuffer variant_animation_size) with its input layout,
// vertex / index / instance buffers, draw arguments, VS constant buffers and textures, GPU copies of its instance records,
// of the animation textures and of the meshes (each buffer once); the main camera block; the Maps on vertex buffers; and
// a summary of all main-view draws by vertex shader. Animated vertex shaders' bytecode goes to motion_vs_<checksum>.bin.
// Chunks: u32 tag, u32 size, payload (tools/motion_dump.py reads them). Capture frames take seconds (staging copies).
typedef HRESULT(STDMETHODCALLTYPE *cvs_fn)(ID3D11Device *, const void *, SIZE_T, ID3D11ClassLinkage *, ID3D11VertexShader **);
typedef HRESULT(STDMETHODCALLTYPE *cil_fn)(ID3D11Device *, const D3D11_INPUT_ELEMENT_DESC *, UINT, const void *, SIZE_T, ID3D11InputLayout **);
typedef void(STDMETHODCALLTYPE *vsset_fn)(ID3D11DeviceContext *, ID3D11VertexShader *, ID3D11ClassInstance *const *, UINT);
static cvs_fn r_cvs;
static cil_fn r_cil;
typedef struct { const void *vs; unsigned long long sum; int anim, type, ocean, fsq, wind; unsigned draws, insts; } MpVs; // type: motion pass shader (-1 none); fsq: full-screen terrain; wind: names wind variables (trees / grass)
static MpVs g_mpvs[4096];
static volatile LONG g_nmpvs;
static volatile LONG g_mph[8192]; // shader pointer -> g_mpvs index + 1 (open addressing)
typedef struct { const void *il; UINT n; D3D11_INPUT_ELEMENT_DESC e[16]; char name[16][24]; } MpIl;
static MpIl g_mpil[1024];
static volatile LONG g_nmpil;
static const void *g_cur_vs; // the vertex shader bound (VSSetShader)
static MpVs *g_cur_mpvs; // its entry
static unsigned g_mp_frame_anim; // animated main-view draws this frame
static int g_mp_kind = -1, g_mp_done[2], g_mp_left, g_mp_k;
static int g_mp_ext, g_mp_extn; // bridge_go.txt with the probe on: one more probe frame each time (benchmark runs), numbered
static DWORD g_mp_seen[2];
static FILE *g_mpmesh; // the meshes (each buffer once per capture)
static const void *g_mp_wf[2048], *g_mp_ws[4096]; // written already: this frame (textures, layouts), this capture (meshes)
static unsigned g_mp_nwf, g_mp_nws;
typedef struct { const void *buf; UINT type, size, n; } MpMap;
static MpMap g_mpmaps[256];
static unsigned g_nmpmaps;
static unsigned long long mp_sum(const void *bc, SIZE_T len) { // the DXBC checksum (bytes 4..11), else FNV-1a
if (len >= 20 && !memcmp(bc, "DXBC", 4)) { unsigned long long s; memcpy(&s, (const char *)bc + 4, 8); return s; }
unsigned long long h = 1469598103934665603ull;
for (SIZE_T i = 0; i < len; i++) h = (h ^ ((const unsigned char *)bc)[i]) * 1099511628211ull;
return h;
}
// ToB's swaying vegetation (its vertex shaders are Attila's byte for byte; r2windvs.h): 2 tree_a, 3 tree_b, 4 grass, by
// the DXBC checksum of each shader model's copy in data.pack (sm 3.0 / 4.1 / 5.0), else -1
static int tob_wind_kind(unsigned long long sum) {
if (sum == 0x670b004d450d96e4ull || sum == 0xc5dc3bb1e96b400dull || sum == 0x6cd8d3f26dd71c1full) return 2; // rigidbranchcommon_v3
if (sum == 0xe1819b1bf937ad85ull || sum == 0xa33af6c2791b94e6ull || sum == 0xfd417f2c91a65ec5ull) return 3; // rigidleafcommon_v3
if (sum == 0x5a0fb4388080f41eull || sum == 0x2377e2110a5e285full) return 4; // rigidgrass
return -1;
}
static int bc_has(const void *bc, SIZE_T len, const char *s) { // the bytecode names s (its RDEF: cbuffers, variables)
SIZE_T n = strlen(s);
for (SIZE_T k = 0; k + n <= len; k++) if (!memcmp((const char *)bc + k, s, n)) return 1;
return 0;
}
// Naval battles (Attila AGENT_LOG 128, same engine): before the main view the game draws the wake foam and the selection
// capsules into two screen-sized targets without depth, which only the ocean's pixel shaders read (colour_overlay_texture,
// ui_colour_overlay_texture: data_rome2.pack, no other dx11 shader names them), at uv = clip.xy / clip.w * (0.5, -0.5) +
// 0.5 from the ocean's vertex shader: full-screen coordinates. Shrunk with the scene they landed in the top-left corner
// and showed away from the ships. So the textures the ocean samples there are learned at its draws (their slots differ
// by water quality: t6 / t7 low, t7 / t9 ultra; read from each pixel shader's RDEF) and drawn into at full size.
// The water reflection too (only the ocean names reflection_texture; user: "the water ... cuts off so close to the
// cam"): a mirrored view drawn into a half-size target, read at uv = (SV_Position * 1 / screen + ripple, then y -> 1 - y)
// (dx11 ultra: add r6.z, -r6.y, l(1.0)). Shrunk into the top-left, the flipped lookup missed it for the upper part of
// the water. Shrunk into the bottom-left instead, the game's own lookup lands exactly on it (row H - r * y_full); its
// pixel shaders get a camera copy patched for that corner. Only until the frame's first ocean draw (it reads it there;
// the game may use the same texture for something else later in the frame).
// Lakes, sea on land maps and rivers (waterplane.hlsl, rigidriver.hlsl; user: "water is messed up" at Quality, a coastal
// land battle) read theirs the same way under another name, s_terrain_reflection (t2: sample_l at (x, 1 - y)): the
// mirrored terrain / sky pass and the screen-space pass into it, then the water. Learned at the water's own draws (its
// vertex shader is not the ocean's).
typedef HRESULT(STDMETHODCALLTYPE *cps_fn)(ID3D11Device *, const void *, SIZE_T, ID3D11ClassLinkage *, ID3D11PixelShader **);
static cps_fn r_cps;
static struct { const void *ps; unsigned slots, rslots, tmcb; int ssr, crop0; } g_ovps[64]; // the ocean's pixel shaders (overlay /
// reflection slots), the screen-space reflection one,
// the tone mapper's and the campaign terrain's (below)
static volatile LONG g_novps;
#define overlay_slots(bc, len, name) rdef_slots(bc, len, name, 2)
static unsigned rdef_slots(const void *bc, SIZE_T len, const char *name, unsigned type) { // slots of the resources of a type
// (2 texture, 0 cbuffer) named *name* (DXBC RDEF)
const unsigned char *b = bc;
unsigned nc, off, slots = 0;
if (len < 32 || memcmp(b, "DXBC", 4)) return 0;
memcpy(&nc, b + 28, 4);
for (unsigned i = 0; i < nc && 32 + 4 * i + 4 <= len; i++) {
memcpy(&off, b + 32 + 4 * i, 4);
if (off > len - 24 || memcmp(b + off, "RDEF", 4)) continue;
const unsigned char *r = b + off + 8;
unsigned rlen, nb, bo;
memcpy(&rlen, b + off + 4, 4);
memcpy(&nb, r + 8, 4);
memcpy(&bo, r + 12, 4);
if (rlen > len - off - 8) return 0;
for (unsigned j = 0; j < nb && bo + 32 * j + 32 <= rlen; j++) {
unsigned e[8];
memcpy(e, r + bo + 32 * j, 32); // name, type (2: texture), return type, dimension, samples, bind point, count, flags
if (e[1] != type || e[5] >= 32 || e[0] >= rlen) continue;
const char *nm = (const char *)r + e[0];
if (memchr(nm, 0, rlen - e[0]) && strstr(nm, name)) slots |= 1u << e[5];
}
}
return slots;
}
static struct { const void *ps; unsigned long long sum; } g_capps[8192]; // every pixel shader's DXBC checksum (captures)
static volatile LONG g_ncapps;
// Pixel shaders whose camera block is not in slot 0 (RDEF; 26 kinds in the packs: the 3D UI's forward lighting, campaign
// borders, MLAA, the low-quality sprites, the terrain's virtual-texture page touch): ps_cb_apply only swaps slot 0, so
// below native size these get the patched copy at their draws (cs_begin). slot 0xFF: a shader created at a freed one's
// address that has it in slot 0. pt: ps30_page_touch (cs_begin, below).
static struct { const void *volatile ps; unsigned char slot, pt; } g_camps[1024];
// Per pixel shader, what the per-draw fixes look for (set at creation), and the bound one's (PSSetShader, w_psset): draws
// by any other shader skip them (asking d3d11 for the shader at every draw cost 70-80 ns a draw: 0.46 ms a frame in a
// 5700-draw naval battle). The fixes still check the shader they are given.
enum { PSF_WATER = 1, PSF_TM = 2, PSF_CAM = 4, PSF_BLOOM = 8 };
// Rome 2's tone mapper fix (its DLSS_LOG 41: auto-exposure measured the black outside the render-size corner) is off in
// ToB: its hdr_tone_mapper is Attila's kind (the final pass samples the bloom at the screen position, so stretching the
// frame over the fixed-size bloom passes would move the bloom; Attila AGENT_LOG 139). Needs a capture to tell the
// exposure passes from the bloom ones first. The stand-in test switches it on.
static int g_tm_fix;
static struct { const void *volatile ps; unsigned char f; } g_psf[2048];
static unsigned g_cur_psf;
static void psf_set(const void *ps, unsigned f) { // loader threads too
for (unsigned h = ((unsigned)(UINT_PTR)ps >> 4) & 2047, k = 0; k < 2048; h = (h + 1) & 2047, k++) {
if (g_psf[h].ps == ps || (!g_psf[h].ps && !InterlockedCompareExchangePointer((void *volatile *)&g_psf[h].ps, (void *)ps, NULL))) {
g_psf[h].f = (unsigned char)f;
return;
}
} // shortcut: 2048 at most (the packs have about 200 such shaders), the rest skip the fixes
}
static unsigned psf_get(const void *ps) {
for (unsigned h = ((unsigned)(UINT_PTR)ps >> 4) & 2047, k = 0; k < 2048 && g_psf[h].ps; h = (h + 1) & 2047, k++)
if (g_psf[h].ps == ps) return g_psf[h].f;
return 0;
}
static void STDMETHODCALLTYPE w_psset(ID3D11DeviceContext *c, ID3D11PixelShader *ps, ID3D11ClassInstance *const *ci, UINT n) {
if (!r2m_internal && c == g_ctx) g_cur_psf = ps ? psf_get(ps) : 0;