locum 0.1.0

Captures everything a Direct3D 11 game draws in a frame, to look at, query, change and replay: d3d11.dll and dxgi.dll beside the game that watch every call on its way to the real runtime.
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
//! The calls besides draws that change what is on the GPU, recorded as events while a capture runs: maps, updates,
//! copies, clears, resolves, mip generation, discards.
//!
//! **Maps.** While a capture runs, a write map hands the game a shadow in locum's memory instead of the driver's
//! (write-combined, slow to read) memory; at `Unmap` the whole shadow is copied to the driver's memory and the bytes
//! the game wrote are recorded. A discard map's shadow starts as a known pattern, so what differs from it is what was
//! written; any other write map's shadow starts as the resource's contents (from locum's own copy when it has one,
//! else read from the mapping) and what changed is what was written. The game's data reaches the driver in full
//! either way. Read maps are passed through.

use core::ffi::c_void;
use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering};

use locum_capture::cost::Cost;
use locum_capture::layout;
use locum_capture::{hash, maps, stacks, writer};
use serde_json::{Value, json};
use windows::Win32::Graphics::Direct3D11::*;
use windows::core::{HRESULT, Interface};

pub use super::Ctx;
use super::ids::{resource_id, view_ids};
use super::{armed, new_event, record};
use crate::d3d11::registry;
use crate::settings::settings;

/// A map handed out with a shadow.
struct Shadow {
    real: *mut c_void,
    data: Vec<u8>,
    /// What the shadow started as, when not the discard pattern.
    reference: Option<Vec<u8>>,
    map_type: i32,
    row_pitch: u32,
    depth_pitch: u32,
    resource_id: u64,
    context: u32,
    deferred: bool,
}

unsafe impl Send for Shadow {}

/// Shadows by (context, resource, subresource) address.
type Shadows = HashMap<(usize, usize, u32), Shadow>;
static SHADOWS: Mutex<Option<Shadows>> = Mutex::new(None);
/// Maps given a shadow (bytes) and unmaps of one recorded, on the game's threads.
pub static SHADOWED: Cost = Cost::new();
static OUTSTANDING: AtomicUsize = AtomicUsize::new(0);
/// The last bytes locum wrote through each (context, resource, subresource) during this capture: the contents a
/// following no-overwrite map starts from.
type Mirrors = HashMap<(u32, u64, u32), Vec<u8>>;
static MIRRORS: Mutex<Option<Mirrors>> = Mutex::new(None);

/// Clears the mirrors when a capture ends.
pub(super) fn reset() {
    *MIRRORS.lock().unwrap_or_else(|e| e.into_inner()) = None;
}

/// `Map` on a context. `real` makes the real call.
///
/// # Safety
/// The arguments are the call's.
#[allow(clippy::too_many_arguments)]
pub unsafe fn map(
    Ctx {
        c,
        id: context,
        deferred,
    }: Ctx,
    resource: *mut c_void,
    sub: u32,
    map_type: D3D11_MAP,
    out: *mut D3D11_MAPPED_SUBRESOURCE,
    real: impl FnOnce() -> HRESULT,
) -> HRESULT {
    if !armed() {
        return real();
    }
    let Some(r) = (unsafe { ID3D11Resource::from_raw_borrowed(&resource) }) else {
        return real();
    };
    let id = resource_id(r);
    let write = map_type != D3D11_MAP_READ;
    let mut e = new_event(
        "map",
        "Map",
        context,
        deferred,
        json!({"resource": id, "sub": sub, "type": map_type.0}),
    );
    // The resource is copied before the map (a mapped resource cannot be copied); a write is recorded at the unmap,
    // a read now.
    if write {
        e.kind = "map_begin";
        e.writes.push(id);
        super::touch_only(c, &e);
    } else {
        e.kind = "map_read";
        e.reads.push(id);
        super::touch_only(c, &e);
    }
    let hr = real();
    if hr.is_err() || out.is_null() || !write {
        if hr.is_ok() && !write {
            record(c, e);
        }
        return hr;
    }
    let Some((kind, Some(s))) = registry::sub_of(id, sub) else {
        return hr;
    };
    let m = unsafe { &mut *out };
    let size = if kind == "buffer" {
        s.size as usize
    } else if s.depth > 1 {
        m.DepthPitch as usize * s.depth as usize
    } else {
        m.RowPitch as usize * s.rows as usize
    };
    if size == 0 || m.pData.is_null() {
        return hr;
    }
    let started = std::time::Instant::now();
    let mut data = vec![0u8; size];
    let reference = if map_type == D3D11_MAP_WRITE_DISCARD {
        maps::fill(&mut data);
        None
    } else {
        // A dynamic resource changes only through maps, so what locum last wrote through this context is what its
        // memory holds; anything else (a staging resource the GPU copies into) is read from the map itself.
        let mirror = dynamic(r)
            .then(|| {
                MIRRORS
                    .lock()
                    .unwrap_or_else(|e| e.into_inner())
                    .as_ref()
                    .and_then(|m| m.get(&(context, id, sub)).filter(|v| v.len() == size).cloned())
            })
            .flatten();
        match mirror {
            Some(v) => data = v,
            None => unsafe { core::ptr::copy_nonoverlapping(m.pData.cast::<u8>(), data.as_mut_ptr(), size) },
        }
        Some(data.clone())
    };
    let shadow = Shadow {
        real: m.pData,
        data,
        reference,
        map_type: map_type.0,
        row_pitch: m.RowPitch,
        depth_pitch: m.DepthPitch,
        resource_id: id,
        context,
        deferred,
    };
    let mut shadows = SHADOWS.lock().unwrap_or_else(|e| e.into_inner());
    let map = shadows.get_or_insert_with(HashMap::new);
    let key = (c.as_raw() as usize, resource as usize, sub);
    let entry = map.entry(key).insert_entry(shadow);
    m.pData = entry.get().data.as_ptr() as *mut c_void;
    OUTSTANDING.fetch_add(1, Ordering::Relaxed);
    SHADOWED.add(started, size as u64);
    hr
}

/// `Unmap` on a context. `real` makes the real call.
///
/// # Safety
/// The arguments are the call's.
pub unsafe fn unmap(c: &ID3D11DeviceContext4, resource: *mut c_void, sub: u32, real: impl FnOnce()) {
    if OUTSTANDING.load(Ordering::Relaxed) == 0 {
        return real();
    }
    let shadow = SHADOWS
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .as_mut()
        .and_then(|m| m.remove(&(c.as_raw() as usize, resource as usize, sub)));
    let Some(shadow) = shadow else { return real() };
    OUTSTANDING.fetch_sub(1, Ordering::Relaxed);
    // A discarding map hands out memory with nothing in it, so all of the shadow goes (the pattern where the game
    // wrote nothing); any other map gets back only the words the game wrote, and the rest stays as it was.
    match &shadow.reference {
        None => unsafe {
            core::ptr::copy_nonoverlapping(shadow.data.as_ptr(), shadow.real.cast::<u8>(), shadow.data.len())
        },
        Some(reference) => {
            for (a, b) in maps::changed(&shadow.data, Some(reference)) {
                unsafe {
                    core::ptr::copy_nonoverlapping(
                        shadow.data.as_ptr().add(a),
                        shadow.real.cast::<u8>().add(a),
                        b - a,
                    )
                };
            }
        }
    }
    real();
    if !armed() {
        return;
    }
    let started = std::time::Instant::now();
    let ranges = maps::written(&shadow.data, shadow.reference.as_deref());
    let texture = registry::sub_of(shadow.resource_id, sub).filter(|(kind, _)| *kind != "buffer");
    let (bytes, args) = match texture {
        // A texture's map is recorded as its whole subresource, tightly packed.
        Some((_, s)) => {
            let Some(s) = s else { return };
            let mut packed = Vec::with_capacity(s.size as usize);
            unsafe {
                layout::pack(
                    &s,
                    shadow.data.as_ptr(),
                    shadow.row_pitch as usize,
                    shadow.depth_pitch as usize,
                    &mut packed,
                )
            };
            (
                packed,
                json!({"resource": shadow.resource_id, "sub": sub, "type": shadow.map_type, "whole": true}),
            )
        }
        None => {
            let mut out = Vec::new();
            for &(a, b) in &ranges {
                out.extend_from_slice(&shadow.data[a..b]);
            }
            let r: Vec<Value> = ranges.iter().map(|&(a, b)| json!([a, b - a])).collect();
            (
                out,
                json!({"resource": shadow.resource_id, "sub": sub, "type": shadow.map_type, "ranges": r}),
            )
        }
    };
    MIRRORS
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .get_or_insert_with(HashMap::new)
        .insert((shadow.context, shadow.resource_id, sub), shadow.data);
    let key = hash::key(&bytes);
    let blobs = &settings().blobs;
    writer::blob(blobs, key.clone(), bytes, true);
    let mut e = new_event("map", "Unmap", shadow.context, shadow.deferred, args);
    e.data = Some(key);
    e.stack = stacks::capture();
    e.writes.push(shadow.resource_id);
    record(c, e);
    SHADOWED.add(started, 0);
}

/// Whether `r` was made with `D3D11_USAGE_DYNAMIC`.
fn dynamic(r: &ID3D11Resource) -> bool {
    let usage = if let Ok(b) = r.cast::<ID3D11Buffer>() {
        let mut d = D3D11_BUFFER_DESC::default();
        unsafe { b.GetDesc(&mut d) };
        d.Usage
    } else if let Ok(t) = r.cast::<ID3D11Texture2D>() {
        let mut d = D3D11_TEXTURE2D_DESC::default();
        unsafe { t.GetDesc(&mut d) };
        d.Usage
    } else if let Ok(t) = r.cast::<ID3D11Texture1D>() {
        let mut d = D3D11_TEXTURE1D_DESC::default();
        unsafe { t.GetDesc(&mut d) };
        d.Usage
    } else if let Ok(t) = r.cast::<ID3D11Texture3D>() {
        let mut d = D3D11_TEXTURE3D_DESC::default();
        unsafe { t.GetDesc(&mut d) };
        d.Usage
    } else {
        return false;
    };
    usage == D3D11_USAGE_DYNAMIC
}

fn res(raw: *mut c_void) -> Option<ID3D11Resource> {
    unsafe { ID3D11Resource::from_raw_borrowed(&raw) }.cloned()
}

fn view(raw: *mut c_void) -> Option<ID3D11View> {
    unsafe { ID3D11View::from_raw_borrowed(&raw) }.cloned()
}

fn box_json(b: *const D3D11_BOX) -> Value {
    if b.is_null() {
        return Value::Null;
    }
    let b = unsafe { &*b };
    json!([b.left, b.top, b.front, b.right, b.bottom, b.back])
}

/// An event that reads `reads` and writes `writes`.
fn op(
    Ctx {
        c,
        id: context,
        deferred,
    }: Ctx,
    kind: &'static str,
    call: &'static str,
    args: Value,
    reads: &[u64],
    writes: &[u64],
) {
    let mut e = new_event(kind, call, context, deferred, args);
    e.reads = reads.to_vec();
    e.writes = writes.to_vec();
    record(c, e);
}

pub fn copy_resource(
    Ctx {
        c,
        id: context,
        deferred,
    }: Ctx,
    dst: *mut c_void,
    src: *mut c_void,
) {
    let (Some(d), Some(s)) = (res(dst), res(src)) else {
        return;
    };
    let (d, s) = (resource_id(&d), resource_id(&s));
    op(
        Ctx {
            c,
            id: context,
            deferred,
        },
        "copy",
        "CopyResource",
        json!({"dst": d, "src": s}),
        &[s],
        &[d],
    );
}

#[allow(clippy::too_many_arguments)]
pub fn copy_region(
    Ctx {
        c,
        id: context,
        deferred,
    }: Ctx,
    call: &'static str,
    dst: *mut c_void,
    dst_sub: u32,
    at: [u32; 3],
    src: *mut c_void,
    src_sub: u32,
    region: *const D3D11_BOX,
    flags: u32,
) {
    let (Some(d), Some(s)) = (res(dst), res(src)) else {
        return;
    };
    let (d, s) = (resource_id(&d), resource_id(&s));
    let args = json!({"dst": d, "dst_sub": dst_sub, "at": at, "src": s, "src_sub": src_sub, "box": box_json(region),
        "flags": flags});
    op(
        Ctx {
            c,
            id: context,
            deferred,
        },
        "copy_region",
        call,
        args,
        &[s],
        &[d],
    );
}

/// A default texture is mapped during WriteToSubresource: never initiate a GPU copy here. If Map ran before
/// this captured frame, no initial copy was possible; preserve the upload but reject the incomplete timeline.
pub fn default_write_start(dst: *mut c_void) {
    let Some(resource) = res(dst) else { return };
    let id = resource_id(&resource);
    let mut s = super::state();
    if s.phase != super::Phase::Recording {
        return;
    }
    if let Some(t) = s.resources.get_mut(&id)
        && !t.touched
    {
        t.touched = true;
        let why = format!(
            "WriteToSubresource on #{id}: texture mapped before its initial contents could be captured"
        );
        s.contents.insert(id, Err(why.clone()));
        *s.gaps.entry(("default_map".into(), why)).or_default() += 1;
    }
}

/// `UpdateSubresource` while armed: the bytes it writes (canonical layout of the region) go to the blob store.
///
/// # Safety
/// The arguments are the call's.
#[allow(clippy::too_many_arguments)]
pub unsafe fn update(
    Ctx {
        c,
        id: context,
        deferred,
    }: Ctx,
    call: &'static str,
    dst: *mut c_void,
    sub: u32,
    region: *const D3D11_BOX,
    data: *const c_void,
    row_pitch: u32,
    depth_pitch: u32,
    flags: u32,
) {
    let Some(d) = res(dst) else { return };
    let id = resource_id(&d);
    let region_ref = if region.is_null() {
        None
    } else {
        Some(unsafe { &*region })
    };
    let bytes = unsafe { registry::updated(&d, sub, region_ref, data, row_pitch, depth_pitch, true) };
    let mut e = new_event(
        "update",
        call,
        context,
        deferred,
        json!({"dst": id, "sub": sub, "box": box_json(region), "flags": flags}),
    );
    if let Some((bytes, key)) = bytes {
        let blobs = &settings().blobs;
        writer::blob(blobs, key.clone(), bytes, true);
        e.data = Some(key);
    }
    e.stack = stacks::capture();
    e.writes.push(id);
    record(c, e);
}

fn view_target(raw: *mut c_void) -> Option<(u64, u64)> {
    view(raw).map(|v| view_ids(&v))
}

pub fn clear_rtv(
    Ctx {
        c,
        id: context,
        deferred,
    }: Ctx,
    rtv: *mut c_void,
    color: *const f32,
) {
    let Some((v, r)) = view_target(rtv) else { return };
    let color: [f32; 4] = if color.is_null() {
        [0.0; 4]
    } else {
        unsafe { *(color as *const [f32; 4]) }
    };
    op(
        Ctx {
            c,
            id: context,
            deferred,
        },
        "clear",
        "ClearRenderTargetView",
        json!({"view": v, "color": color}),
        &[],
        &[r],
    );
}

pub fn clear_dsv(
    Ctx {
        c,
        id: context,
        deferred,
    }: Ctx,
    dsv: *mut c_void,
    flags: u32,
    depth: f32,
    stencil: u8,
) {
    let Some((v, r)) = view_target(dsv) else { return };
    op(
        Ctx {
            c,
            id: context,
            deferred,
        },
        "clear",
        "ClearDepthStencilView",
        json!({"view": v, "flags": flags, "depth": depth, "stencil": stencil}),
        &[],
        &[r],
    );
}

pub fn clear_uav(
    Ctx {
        c,
        id: context,
        deferred,
    }: Ctx,
    call: &'static str,
    uav: *mut c_void,
    values: Value,
) {
    let Some((v, r)) = view_target(uav) else { return };
    op(
        Ctx {
            c,
            id: context,
            deferred,
        },
        "clear",
        call,
        json!({"view": v, "values": values}),
        &[],
        &[r],
    );
}

pub fn clear_view(
    Ctx {
        c,
        id: context,
        deferred,
    }: Ctx,
    target: *mut c_void,
    color: *const f32,
    rects: *const windows::Win32::Foundation::RECT,
    count: u32,
) {
    let Some((v, r)) = view_target(target) else { return };
    let color: [f32; 4] = if color.is_null() {
        [0.0; 4]
    } else {
        unsafe { *(color as *const [f32; 4]) }
    };
    let rects: Vec<Value> = (0..count as usize)
        .filter(|_| !rects.is_null())
        .map(|i| {
            let r = unsafe { &*rects.add(i) };
            json!([r.left, r.top, r.right, r.bottom])
        })
        .collect();
    op(
        Ctx {
            c,
            id: context,
            deferred,
        },
        "clear",
        "ClearView",
        json!({"view": v, "color": color, "rects": rects}),
        &[],
        &[r],
    );
}

#[allow(clippy::too_many_arguments)]
pub fn resolve(
    Ctx {
        c,
        id: context,
        deferred,
    }: Ctx,
    dst: *mut c_void,
    dst_sub: u32,
    src: *mut c_void,
    src_sub: u32,
    format: i32,
) {
    let (Some(d), Some(s)) = (res(dst), res(src)) else {
        return;
    };
    let (d, s) = (resource_id(&d), resource_id(&s));
    op(
        Ctx {
            c,
            id: context,
            deferred,
        },
        "resolve",
        "ResolveSubresource",
        json!({"dst": d, "dst_sub": dst_sub, "src": s, "src_sub": src_sub, "format": format}),
        &[s],
        &[d],
    );
}

pub fn generate_mips(
    Ctx {
        c,
        id: context,
        deferred,
    }: Ctx,
    srv: *mut c_void,
) {
    let Some((v, r)) = view_target(srv) else { return };
    op(
        Ctx {
            c,
            id: context,
            deferred,
        },
        "mips",
        "GenerateMips",
        json!({"view": v}),
        &[r],
        &[r],
    );
}

pub fn copy_structure_count(
    Ctx {
        c,
        id: context,
        deferred,
    }: Ctx,
    dst: *mut c_void,
    offset: u32,
    uav: *mut c_void,
) {
    let (Some(d), Some((v, r))) = (res(dst), view_target(uav)) else {
        return;
    };
    let d = resource_id(&d);
    op(
        Ctx {
            c,
            id: context,
            deferred,
        },
        "copy_count",
        "CopyStructureCount",
        json!({"dst": d, "offset": offset, "view": v}),
        &[r],
        &[d],
    );
}

pub fn discard(
    Ctx {
        c,
        id: context,
        deferred,
    }: Ctx,
    call: &'static str,
    target: *mut c_void,
) {
    // A discard leaves the resource undefined: it counts as a write, so the contents are copied before it.
    let id = if call == "DiscardResource" {
        res(target).map(|r| {
            let id = resource_id(&r);
            ("resource", id, id)
        })
    } else {
        view_target(target).map(|(v, r)| ("view", v, r))
    };
    let Some((what, id, resource)) = id else {
        return;
    };
    let mut args = serde_json::Map::new();
    args.insert(what.into(), id.into());
    op(
        Ctx {
            c,
            id: context,
            deferred,
        },
        "discard",
        call,
        Value::Object(args),
        &[],
        &[resource],
    );
}

/// UAV counters set when UAVs are bound (`-1` keeps the counter).
pub fn uav_counters(
    Ctx {
        c,
        id: context,
        deferred,
    }: Ctx,
    call: &'static str,
    start: u32,
    views: *const *mut c_void,
    counts: *const u32,
    n: u32,
) {
    if counts.is_null() || views.is_null() {
        return;
    }
    let mut set = Vec::new();
    let mut written = Vec::new();
    for i in 0..n as usize {
        let count = unsafe { *counts.add(i) };
        if count == u32::MAX {
            continue;
        }
        let raw = unsafe { *views.add(i) };
        if let Some((v, resource)) = view_target(raw) {
            set.push(json!({"slot": start + i as u32, "view": v, "count": count}));
            written.push(resource);
        }
    }
    if !set.is_empty() {
        op(
            Ctx {
                c,
                id: context,
                deferred,
            },
            "uav_counter",
            call,
            json!({"set": set}),
            &[],
            &written,
        );
    }
}