locum 0.1.6

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.
Documentation
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
//! Persistent tile mappings, ordered tiled APIs, and canonical seeds for shared physical pool tiles.
mod packed;
use super::{Ctx, ids::resource_id, new_event, record};
use crate::d3d11::registry;
use locum_capture::{hash, stacks, writer};
use locum_d3d11::tiles::{Layout, Mappings, Range, Region};
use serde_json::{Value, json};
use std::{
    collections::{BTreeMap, BTreeSet},
    ffi::c_void,
    sync::Mutex,
};
use windows::{
    Win32::Graphics::Direct3D11::*,
    core::{IUnknown, Interface},
};

pub fn special(resource: &ID3D11Resource) -> bool {
    crate::d3d11::registry::dims_of(resource).is_some_and(|d| {
        d.misc & (D3D11_RESOURCE_MISC_TILED.0 as u32 | D3D11_RESOURCE_MISC_TILE_POOL.0 as u32) != 0
    })
}
/// A barrier can name either a resource or a view; its underlying resource supplies the dependency.
pub fn id(raw: *mut c_void) -> u64 {
    let Some(o) = (unsafe { IUnknown::from_raw_borrowed(&raw) }) else {
        return 0;
    };
    let resource = o.cast::<ID3D11Resource>().ok().or_else(|| {
        o.cast::<ID3D11View>()
            .ok()
            .and_then(|v| unsafe { v.GetResource() }.ok())
    });
    resource.as_ref().map_or(0, |r| {
        if super::armed() {
            resource_id(r)
        } else {
            crate::d3d11::registry::id_of(r)
        }
    })
}
/// # Safety
/// `p` is null or points to `count` elements of this call's array.
pub unsafe fn array<T>(p: *const T, count: u32, f: impl Fn(&T) -> Value) -> Value {
    if p.is_null() {
        return Value::Null;
    }
    Value::Array(
        unsafe { std::slice::from_raw_parts(p, count as usize) }
            .iter()
            .map(f)
            .collect(),
    )
}
pub fn coordinate(c: &D3D11_TILED_RESOURCE_COORDINATE) -> Value {
    json!([c.X, c.Y, c.Z, c.Subresource])
}
pub fn size(s: &D3D11_TILE_REGION_SIZE) -> Value {
    json!([s.NumTiles, s.bUseBox.as_bool(), s.Width, s.Height, s.Depth])
}
/// # Safety
/// The pointer is null or references the call's coordinate.
pub unsafe fn coord(p: *const D3D11_TILED_RESOURCE_COORDINATE) -> Value {
    unsafe { p.as_ref() }.map_or(Value::Null, coordinate)
}
/// # Safety
/// The pointer is null or references the call's tile-region size.
pub unsafe fn region(p: *const D3D11_TILE_REGION_SIZE) -> Value {
    unsafe { p.as_ref() }.map_or(Value::Null, size)
}
/// # Safety
/// The pointers describe the valid source memory of UpdateTiles.
pub unsafe fn data(p: *const c_void, size: *const D3D11_TILE_REGION_SIZE) -> Option<Vec<u8>> {
    if p.is_null() {
        return None;
    }
    let s = unsafe { size.as_ref() }?;
    let count = if s.bUseBox.as_bool() {
        u64::from(s.Width) * u64::from(s.Height) * u64::from(s.Depth)
    } else {
        u64::from(s.NumTiles)
    };
    let bytes = usize::try_from(count.checked_mul(65536)?).ok()?;
    Some(unsafe { std::slice::from_raw_parts(p.cast::<u8>(), bytes) }.to_vec())
}
#[allow(clippy::too_many_arguments)]
pub fn event(
    ctx: Ctx,
    kind: &'static str,
    call: &'static str,
    args: Value,
    reads: &[u64],
    writes: &[u64],
    data: Option<Vec<u8>>,
) {
    if !super::armed() {
        return;
    }
    packed::check_call(kind, &args);
    let mut e = new_event(kind, call, ctx.id, ctx.deferred, args);
    e.reads = reads.iter().copied().filter(|id| *id != 0).collect();
    e.writes = writes.iter().copied().filter(|id| *id != 0).collect();
    e.stack = stacks::capture();
    if let Some(data) = data {
        let key = hash::key(&data);
        writer::blob(&crate::settings::settings().blobs, key.clone(), data, true);
        e.data = Some(key);
    }
    if matches!(kind, "tile_map" | "tile_copy_map" | "tile_resize") && !ctx.deferred {
        dependencies(&mut e);
        if super::state().records(false) {
            super::state().timeline.push(e);
        }
    } else {
        record(ctx.c, e);
    }
}

struct Entry {
    layout: Layout,
    mappings: Mappings,
    pool: Option<ID3D11Resource>,
    /// Why the mappings are no longer known: a change the model couldn't follow. It stays until a change replaces the
    /// whole page table (one into another pool).
    error: Option<String>,
    /// Counts the changes, so a capture looks at the mappings again only when they changed.
    generation: u64,
}
impl Entry {
    fn new(resource: &ID3D11Resource) -> Result<Self, String> {
        Ok(Self {
            layout: locum_d3d11::tiles::layout(resource)?,
            mappings: Mappings::default(),
            pool: None,
            error: None,
            generation: 0,
        })
    }
}
static MAPPINGS: Mutex<BTreeMap<u64, Entry>> = Mutex::new(BTreeMap::new());
fn mappings() -> std::sync::MutexGuard<'static, BTreeMap<u64, Entry>> {
    MAPPINGS.lock().unwrap_or_else(|e| e.into_inner())
}
static POOL_SIZES: Mutex<BTreeMap<u64, u64>> = Mutex::new(BTreeMap::new());
pub fn pool_size(id: u64) -> Option<u64> {
    POOL_SIZES
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .get(&id)
        .copied()
}
pub fn resized(raw: *mut c_void, bytes: u64) {
    let id = id(raw);
    POOL_SIZES
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .insert(id, bytes);
    // Mapped tiles beyond the new size remain alive. Only unreferenced tail tiles can be discarded.
    if super::armed() {
        let live: BTreeSet<u32> = mappings()
            .values()
            .filter(|e| e.mappings.pool == Some(id))
            .flat_map(|e| e.mappings.tiles.values().copied())
            .collect();
        super::state()
            .tile_seeded
            .retain(|&(pool, tile)| pool != id || u64::from(tile) * 65536 < bytes || live.contains(&tile));
        if live.iter().any(|&tile| u64::from(tile) * 65536 >= bytes) && super::state().records(false) {
            gap("pool shrink retains mappings beyond its new size; replay of that lifetime is not verified yet".into());
        }
    }
}
pub fn destroyed(id: u64) {
    POOL_SIZES.lock().unwrap_or_else(|e| e.into_inner()).remove(&id);
    packed::destroyed(id);
    // Releasing a pool can enter the registry's destruction callback again.
    let old = mappings().remove(&id);
    drop(old);
}
/// Runs `f` on the resource's entry, made first if it has none, in place: a page table can hold a great many tiles.
/// Whatever `f` returns is dropped after the lock is released, so a pool reference it takes out of the entry can
/// enter the registry's destruction callback (which takes the lock) safely.
fn with_entry<R>(resource: &ID3D11Resource, f: impl FnOnce(&mut Entry) -> R) -> Result<R, String> {
    let id = registry::id_of(resource);
    let fresh = if mappings().contains_key(&id) {
        None
    } else {
        Some(Entry::new(resource)?)
    };
    let mut all = mappings();
    let entry = match fresh {
        Some(fresh) => all.entry(id).or_insert(fresh),
        None => all
            .get_mut(&id)
            .ok_or("the resource's tile mappings were dropped")?,
    };
    Ok(f(entry))
}
fn number(v: &Value) -> u32 {
    v.as_u64().unwrap_or(0) as u32
}
fn location(v: &Value) -> Option<[u32; 4]> {
    v.as_array().map(|_| [0, 1, 2, 3].map(|i| number(&v[i])))
}
fn area(v: &Value) -> Option<Region> {
    v.as_array().map(|_| Region {
        count: number(&v[0]),
        boxed: v[1].as_bool().unwrap_or(false),
        width: number(&v[2]),
        height: number(&v[3]),
        depth: number(&v[4]),
    })
}
/// What a CopyTileMappings copies, read from its source.
struct Source {
    pool: Option<u64>,
    values: Vec<Option<u32>>,
    resource: Option<ID3D11Resource>,
}
#[derive(Clone)]
pub struct Change {
    kind: &'static str,
    args: Value,
    dst: ID3D11Resource,
    src: Option<ID3D11Resource>,
}
impl Change {
    fn apply(&self) -> Result<(), String> {
        let a = &self.args;
        // A copy reads the source's mappings before it changes the destination's: they can be the same resource.
        let source = match (self.kind, &self.src) {
            ("tile_copy_map", Some(src)) => Some(with_entry(src, |s| {
                let from = s.layout.region(location(&a["src_at"]), area(&a["size"]))?;
                Ok::<_, String>(Source {
                    pool: s.mappings.pool,
                    values: s.mappings.at(&from),
                    resource: s.pool.clone(),
                })
            })??),
            ("tile_copy_map", None) => return Err("CopyTileMappings has no source".into()),
            _ => None,
        };
        let released = with_entry(&self.dst, |dst| {
            dst.generation += 1;
            let pool_before = dst.mappings.pool;
            let result = self.change(dst, source);
            if result.is_ok() {
                packed::mapped(dst);
            }
            if result.is_ok() && dst.mappings.pool != pool_before {
                dst.error = None;
            }
            if let Err(e) = &result {
                dst.error.get_or_insert_with(|| e.clone());
            }
            result
        })??;
        // The pool reference the entry let go of is released here, after the lock.
        drop(released);
        Ok(())
    }

    /// Changes the page table; returns the pool reference the entry held before, when it now holds another.
    fn change(&self, dst: &mut Entry, source: Option<Source>) -> Result<Option<ID3D11Resource>, String> {
        let a = &self.args;
        if let Some(Source {
            pool,
            values,
            resource,
        }) = source
        {
            let to = dst.layout.region(location(&a["dst_at"]), area(&a["size"]))?;
            dst.mappings.copy(&to, pool, &values)?;
            return Ok(std::mem::replace(&mut dst.pool, resource));
        }
        let mut locations = Vec::new();
        for i in 0..number(&a["regions"]) as usize {
            locations.extend(
                dst.layout
                    .region(location(&a["starts"][i]), area(&a["sizes"][i]))?,
            );
        }
        // Null counts: one range covers every location, several cover one tile each.
        let ranges: Vec<_> = (0..number(&a["ranges"]) as usize)
            .map(|i| Range {
                flag: number(&a["range_flags"][i]),
                offset: number(&a["offsets"][i]),
                count: a["counts"][i].as_u64().map_or(
                    if number(&a["ranges"]) == 1 {
                        locations.len() as u32
                    } else {
                        1
                    },
                    |n| n as u32,
                ),
            })
            .collect();
        dst.mappings
            .update(self.src.as_ref().map(registry::id_of), &locations, &ranges)?;
        Ok(match &self.src {
            Some(pool) => dst.pool.replace(pool.clone()),
            None => None,
        })
    }
}
static PENDING: Mutex<BTreeMap<u32, Vec<Change>>> = Mutex::new(BTreeMap::new());
fn pending() -> std::sync::MutexGuard<'static, BTreeMap<u32, Vec<Change>>> {
    PENDING.lock().unwrap_or_else(|e| e.into_inner())
}
/// Forgets a destroyed deferred context's changes, handing them back: they hold resources, which the caller releases
/// when that is safe (not inside the context's destruction).
pub fn context_destroyed(id: u32) -> Option<Vec<Change>> {
    pending().remove(&id)
}
/// Successful mapping calls are tracked outside captures too. Deferred changes wait for execution.
/// # Safety
/// `dst` and non-null `src` are live resources supplied to the successful call.
pub unsafe fn changed(ctx: Ctx, kind: &'static str, args: Value, dst: *mut c_void, src: *mut c_void) {
    let Some(dst) = (unsafe { ID3D11Resource::from_raw_borrowed(&dst) }) else {
        return;
    };
    let src = unsafe { ID3D11Resource::from_raw_borrowed(&src) }.cloned();
    let change = Change {
        kind,
        args,
        dst: dst.clone(),
        src,
    };
    if ctx.deferred {
        pending().entry(ctx.id).or_default().push(change);
    } else if let Err(e) = change.apply() {
        crate::log::log!("tile mapping tracking: {e}");
    }
}
/// Capture the old mapping before a call that changes it or resizes its pool.
pub fn before(ctx: Ctx, resources: &[*mut c_void]) {
    if !super::armed() || ctx.deferred {
        return;
    }
    let mut e = new_event("touch", "tile mapping before", ctx.id, false, Value::Null);
    e.reads = resources.iter().map(|&r| id(r)).filter(|&id| id != 0).collect();
    super::first_touch(ctx.c, &e);
}
/// Add physical-pool dependencies: writes through an alias must survive slicing another alias.
pub(super) fn dependencies(event: &mut super::Event) {
    let mut resources = Vec::new();
    {
        let entries = mappings();
        if event.kind == "tile_barrier" && (event.args["before"] == 0 || event.args["after"] == 0) {
            for e in entries.values() {
                if let Some(pool) = &e.pool {
                    resources.push((pool.clone(), false));
                    resources.push((pool.clone(), true));
                }
            }
        }
        for (ids, write) in [(&event.reads, false), (&event.writes, true)] {
            for id in ids {
                if let Some(pool) = entries.get(id).and_then(|e| e.pool.clone()) {
                    resources.push((pool, write));
                }
            }
        }
    }
    for (pool, write) in resources {
        let id = resource_id(&pool);
        if write {
            event.writes.push(id);
        } else {
            event.reads.push(id);
        }
    }
}
fn gap(error: String) {
    *super::state().gaps.entry(("tiled".into(), error)).or_default() += 1;
}
/// Initial page tables and canonical seeds for physical tiles, issued through their actual mapping.
pub(super) fn touch(c: &ID3D11DeviceContext4, resource: &ID3D11Resource) {
    let id = resource_id(resource);
    let Some(dims) = registry::dims_of(resource) else {
        return;
    };
    if dims.misc & D3D11_RESOURCE_MISC_TILE_POOL.0 as u32 != 0 {
        super::state().tile_initialized.insert(id);
        return;
    }
    // Every event that touches a tiled resource comes here; its mappings are only read again after they changed.
    let seen = super::state().tile_seen.get(&id).copied();
    let packed_generation = packed::generation();
    let looked = with_entry(resource, |e| {
        (seen != Some((e.generation, packed_generation))).then(|| {
            (
                e.generation,
                e.error.clone(),
                e.layout.clone(),
                e.mappings.tiles.iter().map(|(&v, &p)| (v, p)).collect::<Vec<_>>(),
                e.pool.clone(),
            )
        })
    });
    let (generation, error, layout, mapped, pool) = match looked {
        Ok(Some(looked)) => looked,
        Ok(None) => return,
        Err(e) => {
            gap(e);
            return;
        }
    };
    super::state()
        .tile_seen
        .insert(id, (generation, packed_generation));
    if let Some(error) = error {
        gap(error);
        return;
    }
    let pool = pool.as_ref().map_or(0, resource_id);
    packed::check_aliases(id, pool, &layout, &mapped);
    if pool_size(pool).is_some_and(|bytes| mapped.iter().any(|&(_, tile)| u64::from(tile) * 65536 >= bytes)) {
        gap("pool has live mappings beyond its shrunken size; native bootstrap is not verified yet".into());
    }
    if super::state().tile_initialized.insert(id) {
        let l = &layout;
        let mut boot = new_event(
            "tile_bootstrap",
            "InitialTileMappings",
            0,
            false,
            json!({"dst": id, "pool": pool, "tiles": mapped, "layout": l.json()}),
        );
        boot.writes.push(id);
        if pool != 0 {
            boot.reads.push(pool);
            super::state().tile_initialized.insert(pool);
        }
        super::state().timeline.push(boot);
    }
    packed::seed(c, resource, id, pool, &layout, &mapped);
    let standard: Vec<_> = mapped
        .into_iter()
        .filter(|&(v, _)| match layout.coordinate(v) {
            Ok(at) => !layout.is_packed(at[3]),
            Err(error) => {
                gap(error);
                false
            }
        })
        .collect();
    let mut chosen = BTreeSet::new();
    let tiles: Vec<_> = {
        let s = super::state();
        standard
            .into_iter()
            .filter(|&(_, p)| !s.tile_seeded.contains(&(pool, p)) && chosen.insert(p))
            .collect()
    };
    for chunk in tiles.chunks(256) {
        let coordinates: Result<Vec<_>, _> = chunk.iter().map(|&(v, _)| layout.coordinate(v)).collect();
        let coordinates = match coordinates {
            Ok(v) => v,
            Err(e) => {
                gap(e);
                continue;
            }
        };
        let bytes = coordinates.len() as u64 * 65536;
        if super::state().in_flight_bytes + bytes > super::contents::IN_FLIGHT {
            super::read_in_flight(c);
        }
        let rb = match unsafe { locum_d3d11::readback::issue_tiles(c, resource, &coordinates) } {
            Ok(rb) => rb,
            Err(e) => {
                gap(e);
                continue;
            }
        };
        let mut seed = new_event(
            "tile_seed",
            "InitialTileContents",
            0,
            false,
            json!({"dst": id, "pool": pool, "coordinates": coordinates}),
        );
        seed.reads.push(id);
        seed.writes.push(pool);
        seed.data_readback = Some(rb);
        let mut s = super::state();
        s.in_flight_bytes += bytes;
        s.tile_seeded.extend(chunk.iter().map(|&(_, p)| (pool, p)));
        let at = s.timeline.len();
        s.tile_seeds.push(at);
        s.timeline.push(seed);
    }
}

// Private COM data makes the recorded mapping metadata live exactly as long as the real list.
const LIST_KEY: windows::core::GUID = windows::core::GUID::from_u128(0x6c6f6375_6d00_4a5e_9d1b_7a2f8c3e0019);
#[repr(C)]
struct ListData {
    vtable: *const windows::core::IUnknown_Vtbl,
    refs: std::sync::atomic::AtomicU32,
    changes: Vec<Change>,
}
unsafe extern "system" fn query(
    this: *mut c_void,
    iid: *const windows::core::GUID,
    out: *mut *mut c_void,
) -> windows::core::HRESULT {
    if out.is_null() {
        return windows::Win32::Foundation::E_POINTER;
    }
    unsafe {
        *out = std::ptr::null_mut();
    }
    if !iid.is_null() && unsafe { *iid == IUnknown::IID } {
        unsafe {
            *out = this;
            add_ref(this);
        }
        windows::Win32::Foundation::S_OK
    } else {
        windows::Win32::Foundation::E_NOINTERFACE
    }
}
unsafe extern "system" fn add_ref(this: *mut c_void) -> u32 {
    unsafe { &*this.cast::<ListData>() }
        .refs
        .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
        + 1
}
unsafe extern "system" fn release(this: *mut c_void) -> u32 {
    let n = unsafe { &*this.cast::<ListData>() }
        .refs
        .fetch_sub(1, std::sync::atomic::Ordering::AcqRel)
        - 1;
    if n == 0 {
        drop(unsafe { Box::from_raw(this.cast::<ListData>()) });
    }
    n
}
static LIST_VTABLE: windows::core::IUnknown_Vtbl = windows::core::IUnknown_Vtbl {
    QueryInterface: query,
    AddRef: add_ref,
    Release: release,
};
/// # Safety
/// `list` is the successful FinishCommandList result, or null when discarded.
pub unsafe fn finish(context: u32, list: *mut c_void) {
    let changes = pending().remove(&context).unwrap_or_default();
    if changes.is_empty() {
        return;
    }
    let Some(list) = (unsafe { ID3D11CommandList::from_raw_borrowed(&list) }) else {
        return;
    };
    let data = Box::new(ListData {
        vtable: &LIST_VTABLE,
        refs: std::sync::atomic::AtomicU32::new(1),
        changes,
    });
    let unknown = unsafe { IUnknown::from_raw(Box::into_raw(data).cast()) };
    if let Err(e) = unsafe { list.SetPrivateDataInterface(&LIST_KEY, &unknown) } {
        crate::log::log!("command list tile metadata: {e}");
    }
}
/// # Safety
/// `list` is a live command list supplied to ExecuteCommandList.
pub unsafe fn execute(ctx: Ctx, list: *mut c_void) {
    let Some(list) = (unsafe { ID3D11CommandList::from_raw_borrowed(&list) }) else {
        return;
    };
    let mut data: *mut c_void = std::ptr::null_mut();
    let mut bytes = size_of::<*mut c_void>() as u32;
    if unsafe {
        list.GetPrivateData(
            &LIST_KEY,
            &mut bytes,
            Some((&mut data as *mut *mut c_void).cast()),
        )
    }
    .is_err()
        || data.is_null()
    {
        return;
    }
    let unknown = unsafe { IUnknown::from_raw(data) };
    let changes = unsafe { &*data.cast::<ListData>() }.changes.clone();
    drop(unknown);
    if ctx.deferred {
        pending().entry(ctx.id).or_default().extend(changes);
    } else {
        if super::armed() && super::state().records(false) {
            gap("tile mapping changes inside a deferred list require execution-time tile seeds".into());
        }
        for change in changes {
            if let Err(e) = change.apply() {
                gap(e);
            }
        }
    }
}