Skip to main content

qualia_core_db/render/gpu/
animated_upload.rs

1//! Allocation-free upload of native skinning output, including surface frames
2//! and compact GPU PGA `Motor` dual-quaternion palettes.
3use super::{materials, PortalGpu};
4use crate::render::physics::Aabb;
5use crate::render::skeletal::gpu_skin::{
6    GpuJointMotorPair, MAX_GPU_RIG_JOINTS, MAX_GPU_SKELETON_INSTANCES,
7};
8
9/// Uploads a compact per-instance PGA `Motor` skinning palette (`<= 128` joints, 64 bytes/joint)
10/// directly to a GPU storage buffer at `instance_rig_offset` (in joint slots) with zero heap
11/// allocations (`HotZeroHeap`).
12///
13/// Replaces the legacy 3× full-vertex-buffer upload (`positions`, `normals`, `tangents`)
14/// when hardware vertex-shader dual-quaternion skinning is active.
15pub fn upload_rig_motor_palette_gpu(
16    queue: &etalon::Queue,
17    palette_buffer: &etalon::Buffer,
18    instance_rig_offset: u32,
19    palette: &[GpuJointMotorPair],
20) -> bool {
21    let count = palette.len().min(MAX_GPU_RIG_JOINTS);
22    if count == 0 {
23        return false;
24    }
25    let max_total_joints = MAX_GPU_RIG_JOINTS * MAX_GPU_SKELETON_INSTANCES;
26    let first_joint = instance_rig_offset as usize;
27    let Some(end_joint) = first_joint.checked_add(count) else {
28        return false;
29    };
30    if end_joint > max_total_joints {
31        return false;
32    }
33    if palette[..count].iter().any(|m| {
34        m.current_r
35            .iter()
36            .chain(m.current_d.iter())
37            .chain(m.prev_r.iter())
38            .chain(m.prev_d.iter())
39            .any(|v| !v.is_finite())
40    }) {
41        return false;
42    }
43    let byte_offset = (first_joint * core::mem::size_of::<GpuJointMotorPair>()) as u64;
44    queue.write_buffer(
45        palette_buffer,
46        byte_offset,
47        bytemuck::cast_slice(&palette[..count]),
48    );
49    true
50}
51
52impl PortalGpu {
53    /// Uploads a compact GPU PGA `Motor` palette into `palette_buffer` using this device's queue.
54    pub fn upload_rig_motor_palette(
55        &self,
56        palette_buffer: &etalon::Buffer,
57        instance_rig_offset: u32,
58        palette: &[GpuJointMotorPair],
59    ) -> bool {
60        upload_rig_motor_palette_gpu(&self.queue, palette_buffer, instance_rig_offset, palette)
61    }
62
63    /// Writes `mesh-animated-frames`.
64    pub fn write_mesh_animated_frames(
65        &mut self,
66        start: u32,
67        positions: &[[f32; 3]],
68        normals: &[[f32; 3]],
69        tangents: &[[f32; 4]],
70    ) -> bool {
71        let Some(mesh) = self.mesh.as_mut() else {
72            return false;
73        };
74        let first = start as usize;
75        let Some(end) = first.checked_add(positions.len()) else {
76            return false;
77        };
78        if positions.is_empty()
79            || end > mesh.vertex_count as usize
80            || normals.len() != positions.len()
81            || tangents.len() != positions.len()
82            || positions
83                .iter()
84                .flatten()
85                .chain(normals.iter().flatten())
86                .chain(tangents.iter().flatten())
87                .any(|v| !v.is_finite())
88        {
89            return false;
90        }
91        if mesh
92            .normal_workspace
93            .validate_position_span(mesh.cpu_positions.len(), first, end)
94            .is_err()
95        {
96            return false;
97        }
98        mesh.cpu_positions[first..end].copy_from_slice(positions);
99        // Maintain the legacy edit workspace so later topology-based edits
100        // start from the actual posed geometry, without replacing skin normals.
101        mesh.normal_workspace
102            .update_positions_prevalidated(&mesh.cpu_positions, first, end);
103        self.queue.write_buffer(
104            &mesh.vertex_buf,
105            (first * 12) as u64,
106            bytemuck::cast_slice(positions),
107        );
108        self.queue.write_buffer(
109            &mesh.normal_buf,
110            (first * 12) as u64,
111            bytemuck::cast_slice(normals),
112        );
113        self.queue.write_buffer(
114            &mesh.tangent_buf,
115            (first * 16) as u64,
116            bytemuck::cast_slice(tangents),
117        );
118        materials::update_draw_bounds(
119            &mut mesh.material_gpu.draws,
120            &mesh.cpu_positions,
121            &mesh.cpu_indices,
122        );
123        materials::update_draw_bounds(
124            &mut mesh.shadow_draws,
125            &mesh.cpu_positions,
126            &mesh.cpu_indices,
127        );
128        self.mesh_base_aabb = Aabb::from_points(&mesh.cpu_positions);
129        self.last_visibility_key = None;
130        self.shadow_map_dirty = true;
131        // Any generic writer retires external source identity. The source-bound
132        // owner restores its already-preflighted next state after this commit.
133        self.pick_readback.invalidate(true);
134        self.pick_semantic_result = None;
135        self.source_motion.retire();
136        true
137    }
138}