concinnity-device 0.19.119

GPU backends (Metal, Vulkan, DirectX) behind a device facade for Concinnity
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
//! Hot-reload rebuild of the shared static + skinned vertex / index buffers
//! when a re-imported `.glb` no longer fits in its init-time slot. Mirrors
//! metal/resources/geometry_rebuild.rs + metal/resources/skinning.rs's
//! `rebuild_skinned_geometry`; the DEFAULT-heap buffers force a CPU
//! round-trip (READBACK staging for the old contents, UPLOAD staging for
//! the new ones) where Metal can just read `StorageModeShared` `contents()`.

use concinnity_core::gfx::mesh_payload::{SkinnedVertex, Vertex};
use concinnity_core::render::backend;
use concinnity_core::render::error::{RenderError, RenderResult};
use concinnity_core::render::geometry_repack;
use concinnity_core::render::rt_geom;
use windows::Win32::Graphics::Direct3D12::*;
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32_UINT;

use super::super::com;
use super::super::context::DxContext;
use super::super::texture::{one_shot_submit, transition_barrier};
use crate::directx::error::map_hresult;

// cn-debug-only asset hot-reload geometry rebuild; dead from the FFI lib
// crate's roots, live in the concinnity binary. The two module-level helper fns
// below (`read_typed_vec` / `write_upload_buffer`) are walked through these
// suppressed-but-live-root methods, so they need no attribute of their own.
// See the note on the analogous block in [directx/decal.rs].
impl DxContext {
    // Swap in rebuilt shared static-mesh buffers for the draws named in
    // `changes`, when a reloaded mesh no longer fits its slot. The DEFAULT-heap
    // buffers are read back through READBACK staging for the repack, and the
    // result is uploaded through UPLOAD staging into fresh DEFAULT-heap
    // buffers. The streamed-mesh sub-allocators are not preserved, so a later
    // `upload_mesh` fails allocation.
    pub(crate) fn rebuild_static_geometry(
        &mut self,
        changes: Vec<backend::DrawGeometryUpdate>,
    ) -> RenderResult<()> {
        // Stop the GPU + CPU pipelines so the readback + swap can run safely.
        // Costs a frame-time stall but only fires under `cn debug` when the
        // source `.glb` size actually changed.
        self.wait_idle();

        // Read the current shared buffers back to CPU memory via READBACK
        // staging (DEFAULT-heap resources are not CPU-mappable). One one-shot
        // submit transitions both buffers to COPY_SOURCE, copies each to its
        // matching staging buffer, then transitions back; `wait_idle` above
        // gates the source side; the one-shot's internal fence wait gates
        // the destination.
        let old_v_bytes = self.scene.geometry.vertex_buffer_view.SizeInBytes as u64;
        let old_i_bytes = self.scene.geometry.index_buffer_view.SizeInBytes as u64;
        let v_readback = self.hw.alloc.alloc_buffer(
            old_v_bytes,
            D3D12_HEAP_TYPE_READBACK,
            D3D12_RESOURCE_STATE_COPY_DEST,
        )?;
        let i_readback = self.hw.alloc.alloc_buffer(
            old_i_bytes,
            D3D12_HEAP_TYPE_READBACK,
            D3D12_RESOURCE_STATE_COPY_DEST,
        )?;
        // SAFETY: the command list is in the recording state, and every resource, descriptor and
        // slice these commands name is live for the call.
        one_shot_submit(&self.hw.device, &self.hw.command_queue, |cmd| unsafe {
            let v_src = transition_barrier(
                &self.scene.geometry.vertex_buffer,
                D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER,
                D3D12_RESOURCE_STATE_COPY_SOURCE,
            );
            let i_src = transition_barrier(
                &self.scene.geometry.index_buffer,
                D3D12_RESOURCE_STATE_INDEX_BUFFER,
                D3D12_RESOURCE_STATE_COPY_SOURCE,
            );
            cmd.ResourceBarrier(&[v_src, i_src]);
            cmd.CopyBufferRegion(
                &*v_readback,
                0,
                &*self.scene.geometry.vertex_buffer,
                0,
                old_v_bytes,
            );
            cmd.CopyBufferRegion(
                &*i_readback,
                0,
                &*self.scene.geometry.index_buffer,
                0,
                old_i_bytes,
            );
            let v_back = transition_barrier(
                &self.scene.geometry.vertex_buffer,
                D3D12_RESOURCE_STATE_COPY_SOURCE,
                D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER,
            );
            let i_back = transition_barrier(
                &self.scene.geometry.index_buffer,
                D3D12_RESOURCE_STATE_COPY_SOURCE,
                D3D12_RESOURCE_STATE_INDEX_BUFFER,
            );
            cmd.ResourceBarrier(&[v_back, i_back]);
        })?;

        // Map the readback staging buffers and copy into typed CPU Vecs.
        // The static IB is `u32` (set when the buffer was created in
        // init/mod.rs); the VB stride is `size_of::<Vertex>()`.
        let old_v_count = (old_v_bytes as usize) / std::mem::size_of::<Vertex>();
        let old_i_count = (old_i_bytes as usize) / std::mem::size_of::<u32>();
        let old_vertices: Vec<Vertex> = read_typed_vec(&v_readback, old_v_count)?;
        let old_indices: Vec<u32> = read_typed_vec(&i_readback, old_i_count)?;

        let repacked = geometry_repack::repack_static_geometry(
            &self.state.draw.objects,
            &old_vertices,
            &old_indices,
            changes,
        )?;
        if repacked.ignored_changes > 0 {
            tracing::warn!(
                "rebuild_static_geometry: {} change(s) targeted draw indices not in \
                 draw_objects (ignored)",
                repacked.ignored_changes
            );
        }
        let new_vertices = repacked.vertices;
        let new_indices = repacked.indices;

        // Allocate the new DEFAULT-heap buffers + UPLOAD-heap staging copies
        // and ship the rebuilt contents in a single one-shot submit. The new
        // resources start in COMMON; CopyBufferRegion promotes them to
        // COPY_DEST implicitly, then a barrier puts each into its read state.
        let new_v_bytes = std::mem::size_of_val(new_vertices.as_slice()) as u64;
        let new_i_bytes = std::mem::size_of_val(new_indices.as_slice()) as u64;
        let new_vbuf = self.hw.alloc.alloc_buffer(
            new_v_bytes,
            D3D12_HEAP_TYPE_DEFAULT,
            D3D12_RESOURCE_STATE_COMMON,
        )?;
        let new_ibuf = self.hw.alloc.alloc_buffer(
            new_i_bytes,
            D3D12_HEAP_TYPE_DEFAULT,
            D3D12_RESOURCE_STATE_COMMON,
        )?;
        let v_upload = self.hw.alloc.alloc_buffer(
            new_v_bytes,
            D3D12_HEAP_TYPE_UPLOAD,
            D3D12_RESOURCE_STATE_GENERIC_READ,
        )?;
        let i_upload = self.hw.alloc.alloc_buffer(
            new_i_bytes,
            D3D12_HEAP_TYPE_UPLOAD,
            D3D12_RESOURCE_STATE_GENERIC_READ,
        )?;
        write_upload_buffer(&v_upload, bytemuck::cast_slice(&new_vertices))?;
        write_upload_buffer(&i_upload, bytemuck::cast_slice(&new_indices))?;
        // SAFETY: the command list is in the recording state, and every resource, descriptor and
        // slice these commands name is live for the call.
        one_shot_submit(&self.hw.device, &self.hw.command_queue, |cmd| unsafe {
            cmd.CopyBufferRegion(&*new_vbuf, 0, &*v_upload, 0, new_v_bytes);
            cmd.CopyBufferRegion(&*new_ibuf, 0, &*i_upload, 0, new_i_bytes);
            let v_dst = transition_barrier(
                &new_vbuf,
                D3D12_RESOURCE_STATE_COPY_DEST,
                D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER,
            );
            let i_dst = transition_barrier(
                &new_ibuf,
                D3D12_RESOURCE_STATE_COPY_DEST,
                D3D12_RESOURCE_STATE_INDEX_BUFFER,
            );
            cmd.ResourceBarrier(&[v_dst, i_dst]);
        })?;

        // Commit the swap: rewrite per-draw layouts, point the live views at
        // the new buffers, then drop the old buffer references. After this
        // line the old `vertex_buffer` / `index_buffer` resources are
        // unreachable; the next-frame fence wait has already happened, so
        // the COM refcount drop is safe.
        for (layout, obj) in repacked
            .layouts
            .into_iter()
            .zip(&mut self.state.draw.objects)
        {
            layout.apply_to(obj);
        }
        self.scene.geometry.vertex_buffer_view = D3D12_VERTEX_BUFFER_VIEW {
            BufferLocation: com::gpu_va(&new_vbuf),
            SizeInBytes: new_v_bytes as u32,
            StrideInBytes: std::mem::size_of::<Vertex>() as u32,
        };
        self.scene.geometry.index_buffer_view = D3D12_INDEX_BUFFER_VIEW {
            BufferLocation: com::gpu_va(&new_ibuf),
            SizeInBytes: new_i_bytes as u32,
            Format: DXGI_FORMAT_R32_UINT,
        };
        self.scene.geometry.vertex_buffer = new_vbuf;
        self.scene.geometry.index_buffer = new_ibuf;

        // The RT acceleration structure was built against the buffers just
        // dropped and the per-draw offsets just rewritten, so its BLAS hold the
        // old geometry and its geometry table would address the new buffers at
        // old offsets. Rebuild it over the fresh layout. `wait_idle` above plus
        // the one-shot fence waits leave the GPU quiet, so the synchronous
        // rebuild is safe here.
        if self.rt.accel.is_some() {
            self.rebuild_rt_accel();
        }
        Ok(())
    }

    // Rebuild the shared skinned-mesh vertex + index buffers, swapping in
    // fresh geometry for the slots named in `changes`. Driven by asset
    // hot-reload (`cn debug` only) when a `SkinnedMesh` re-import has a
    // different vertex / index count than its init-time slot. Walks every
    // `SkinnedDrawObject` in order: for each slot in `changes`, the new
    // vertices / indices are appended to a fresh CPU buffer; for unchanged
    // slots, the current geometry is read back from the live skinned
    // buffers (DEFAULT heap → READBACK staging + one-shot GPU copy + Map)
    // and copied with index rebasing from the slot's old `vertex_base`
    // onto its new one. New DEFAULT-heap buffers are created at the
    // post-rebuild size and the contents uploaded through UPLOAD staging;
    // the old buffers are dropped only after the swap commits. Returns a
    // `SkinnedSlotLayout` per slot (in `skinned_index` order) so the
    // asset-hot-reload caller can refresh its `SkinnedMeshSourceEntry`s.
    //
    // The skinned IB stays `DXGI_FORMAT_R32_UINT`; the skinned pipelines,
    // shadow / velocity / SSAO / SSR variants, and per-slot metadata
    // (`texture_slot` / `normal_map_slot` / `material` / `joint_count`)
    // are untouched. Skeleton-shape changes (joint-count mismatch) route
    // through `update_skinned_skeleton`, not this call.
    pub(crate) fn rebuild_skinned_geometry(
        &mut self,
        changes: Vec<backend::SkinnedDrawGeometryUpdate>,
    ) -> RenderResult<Vec<backend::SkinnedSlotLayout>> {
        let v_buf = self.skinned.vertex_buffer.clone().ok_or_else(|| {
            RenderError::Other(
                "rebuild_skinned_geometry: no skinned vertex buffer (was upload_skinned called?)"
                    .into(),
            )
        })?;
        let i_buf = self.skinned.index_buffer.clone().ok_or_else(|| {
            RenderError::Other(
                "rebuild_skinned_geometry: no skinned index buffer (was upload_skinned called?)"
                    .into(),
            )
        })?;

        self.wait_idle();

        // Read the live skinned buffers back to CPU memory via READBACK
        // staging. Same one-shot pattern as `rebuild_static_geometry`.
        let old_v_bytes = self.skinned.vertex_buffer_view.SizeInBytes as u64;
        let old_i_bytes = self.skinned.index_buffer_view.SizeInBytes as u64;
        let v_readback = self.hw.alloc.alloc_buffer(
            old_v_bytes,
            D3D12_HEAP_TYPE_READBACK,
            D3D12_RESOURCE_STATE_COPY_DEST,
        )?;
        let i_readback = self.hw.alloc.alloc_buffer(
            old_i_bytes,
            D3D12_HEAP_TYPE_READBACK,
            D3D12_RESOURCE_STATE_COPY_DEST,
        )?;
        // SAFETY: the command list is in the recording state, and every resource, descriptor and
        // slice these commands name is live for the call.
        one_shot_submit(&self.hw.device, &self.hw.command_queue, |cmd| unsafe {
            let v_src = transition_barrier(
                &v_buf,
                D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER,
                D3D12_RESOURCE_STATE_COPY_SOURCE,
            );
            let i_src = transition_barrier(
                &i_buf,
                D3D12_RESOURCE_STATE_INDEX_BUFFER,
                D3D12_RESOURCE_STATE_COPY_SOURCE,
            );
            cmd.ResourceBarrier(&[v_src, i_src]);
            cmd.CopyBufferRegion(&*v_readback, 0, &*v_buf, 0, old_v_bytes);
            cmd.CopyBufferRegion(&*i_readback, 0, &*i_buf, 0, old_i_bytes);
            let v_back = transition_barrier(
                &v_buf,
                D3D12_RESOURCE_STATE_COPY_SOURCE,
                D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER,
            );
            let i_back = transition_barrier(
                &i_buf,
                D3D12_RESOURCE_STATE_COPY_SOURCE,
                D3D12_RESOURCE_STATE_INDEX_BUFFER,
            );
            cmd.ResourceBarrier(&[v_back, i_back]);
        })?;

        // Skinned VB stride is `size_of::<SkinnedVertex>()`; skinned IB is
        // `u32` (matches the format set in `upload_skinned`).
        let old_v_count = (old_v_bytes as usize) / std::mem::size_of::<SkinnedVertex>();
        let old_i_count = (old_i_bytes as usize) / std::mem::size_of::<u32>();
        let old_vertices: Vec<SkinnedVertex> = read_typed_vec(&v_readback, old_v_count)?;
        let old_indices: Vec<u32> = read_typed_vec(&i_readback, old_i_count)?;

        let repacked = geometry_repack::repack_skinned_geometry(
            &self.state.skinned.draw_objects,
            &old_vertices,
            &old_indices,
            changes,
        )?;
        if repacked.ignored_changes > 0 {
            tracing::warn!(
                "rebuild_skinned_geometry: {} change(s) targeted skinned indices not \
                 in skinned_draw_objects (ignored)",
                repacked.ignored_changes
            );
        }
        let new_vertices = &repacked.vertices;
        let new_indices = &repacked.indices;

        // Allocate new DEFAULT-heap buffers + UPLOAD staging copies and ship
        // the rebuilt contents in a single one-shot submit.
        let new_v_bytes = std::mem::size_of_val(new_vertices.as_slice()) as u64;
        let new_i_bytes = std::mem::size_of_val(new_indices.as_slice()) as u64;
        // Whole u32 words for the index buffer; see `upload_skinned`.
        let ibuf_bytes = rt_geom::skinned_index_buffer_bytes(new_indices.len()) as u64;
        let new_vbuf = self.hw.alloc.alloc_buffer(
            new_v_bytes,
            D3D12_HEAP_TYPE_DEFAULT,
            D3D12_RESOURCE_STATE_COMMON,
        )?;
        let new_ibuf = self.hw.alloc.alloc_buffer(
            ibuf_bytes,
            D3D12_HEAP_TYPE_DEFAULT,
            D3D12_RESOURCE_STATE_COMMON,
        )?;
        let v_upload = self.hw.alloc.alloc_buffer(
            new_v_bytes,
            D3D12_HEAP_TYPE_UPLOAD,
            D3D12_RESOURCE_STATE_GENERIC_READ,
        )?;
        let i_upload = self.hw.alloc.alloc_buffer(
            ibuf_bytes,
            D3D12_HEAP_TYPE_UPLOAD,
            D3D12_RESOURCE_STATE_GENERIC_READ,
        )?;
        write_upload_buffer(&v_upload, bytemuck::cast_slice(new_vertices))?;
        write_upload_buffer(&i_upload, bytemuck::cast_slice(new_indices))?;
        // SAFETY: the command list is in the recording state, and every resource, descriptor and
        // slice these commands name is live for the call.
        one_shot_submit(&self.hw.device, &self.hw.command_queue, |cmd| unsafe {
            cmd.CopyBufferRegion(&*new_vbuf, 0, &*v_upload, 0, new_v_bytes);
            cmd.CopyBufferRegion(&*new_ibuf, 0, &*i_upload, 0, new_i_bytes);
            let v_dst = transition_barrier(
                &new_vbuf,
                D3D12_RESOURCE_STATE_COPY_DEST,
                D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER,
            );
            let i_dst = transition_barrier(
                &new_ibuf,
                D3D12_RESOURCE_STATE_COPY_DEST,
                D3D12_RESOURCE_STATE_INDEX_BUFFER,
            );
            cmd.ResourceBarrier(&[v_dst, i_dst]);
        })?;

        // Commit: rewrite per-slot layouts, repoint the live views at the
        // new buffers, and drop the old buffer COM references.
        repacked.apply_to(&mut self.state.skinned.draw_objects);
        self.skinned.vertex_buffer_view = D3D12_VERTEX_BUFFER_VIEW {
            BufferLocation: com::gpu_va(&new_vbuf),
            SizeInBytes: new_v_bytes as u32,
            StrideInBytes: std::mem::size_of::<SkinnedVertex>() as u32,
        };
        self.skinned.index_buffer_view = D3D12_INDEX_BUFFER_VIEW {
            BufferLocation: com::gpu_va(&new_ibuf),
            SizeInBytes: new_i_bytes as u32,
            Format: DXGI_FORMAT_R32_UINT,
        };
        self.skinned.vertex_buffer = Some(new_vbuf);
        self.skinned.index_buffer = Some(new_ibuf);
        Ok(repacked.layouts())
    }
}

// Byte capacity of a buffer resource. `Width` is the buffer's size in bytes.
fn buffer_bytes(res: &ID3D12Resource) -> u64 {
    // SAFETY: a property query on a live resource; it only reads.
    unsafe { res.GetDesc() }.Width
}

// Map a READBACK-heap buffer and copy `count` `T`s out into a CPU Vec. The
// caller has already gated the GPU writes (via `one_shot_submit`'s internal
// fence wait), so the memcpy sees fully committed bytes. `T` must match the
// buffer's stride exactly.
fn read_typed_vec<T: Copy>(src: &ID3D12Resource, count: usize) -> RenderResult<Vec<T>> {
    let want = (count * std::mem::size_of::<T>()) as u64;
    let have = buffer_bytes(src);
    assert!(
        want <= have,
        "read_typed_vec: {want} bytes exceeds readback buffer {have}"
    );
    let mut ptr = std::ptr::null_mut::<std::ffi::c_void>();
    // SAFETY: the resource is a live CPU-visible buffer, and the out-parameter is a live local that
    // receives the mapping.
    unsafe { src.Map(0, None, Some(&mut ptr)) }
        .map_err(|e| map_hresult(e.code(), "rebuild readback map"))?;
    let mut out: Vec<T> = Vec::with_capacity(count);
    // SAFETY: `Map` returned the live mapping of a READBACK buffer the assert above proved holds at
    // least `count` `T`s, `out` reserved that many, and the two are separate allocations, so the
    // ranges cannot overlap. `set_len` follows a copy that initialized every element.
    unsafe {
        std::ptr::copy_nonoverlapping(ptr as *const T, out.as_mut_ptr(), count);
        out.set_len(count);
        src.Unmap(0, None);
    }
    Ok(out)
}

// Map an UPLOAD-heap buffer and copy `bytes` into it. Standard UPLOAD-heap
// idiom: Map (CPU writes), copy_nonoverlapping, Unmap (driver flushes).
fn write_upload_buffer(dest: &ID3D12Resource, bytes: &[u8]) -> RenderResult<()> {
    let have = buffer_bytes(dest);
    assert!(
        bytes.len() as u64 <= have,
        "write_upload_buffer: {} bytes exceeds upload buffer {have}",
        bytes.len()
    );
    let mut ptr = std::ptr::null_mut::<std::ffi::c_void>();
    // SAFETY: the resource is a live CPU-visible buffer, and the out-parameter is a live local that
    // receives the mapping.
    unsafe { dest.Map(0, None, Some(&mut ptr)) }
        .map_err(|e| map_hresult(e.code(), "rebuild upload map"))?;
    // SAFETY: `Map` returned the live mapping of an UPLOAD buffer the assert above proved is at
    // least `bytes.len()` long, and the source is a separate allocation, so the ranges cannot
    // overlap.
    unsafe {
        std::ptr::copy_nonoverlapping(bytes.as_ptr(), ptr as *mut u8, bytes.len());
        dest.Unmap(0, None);
    }
    Ok(())
}