Skip to main content

qualia_core_db/render/gpu/
source_frame.rs

1//! Full authored-mesh publication through the existing PortalGpu buffers.
2use super::PortalGpu;
3use crate::render::source_frame::{
4    decode_bound_model, validate_resident_mesh, SourceFrameBindLimits, SourceFrameBinding,
5    SourceFrameError, SourceFrameReceipt, SourceMeshFrame,
6};
7
8impl PortalGpu {
9    pub fn bind_source_motion_model(
10        &mut self,
11        model: &[u8],
12        source_sha256: [u8; 32],
13        source_generation: u64,
14        limits: SourceFrameBindLimits,
15    ) -> Result<SourceFrameBinding, SourceFrameError> {
16        self.bind_source_motion_model_at_epoch(model, source_sha256, source_generation, 1, limits)
17    }
18
19    /// Commit the caller's current transport epoch with cold model admission;
20    /// no fallible epoch-advance step is needed after binding has mutated state.
21    pub fn bind_source_motion_model_at_epoch(
22        &mut self,
23        model: &[u8],
24        source_sha256: [u8; 32],
25        source_generation: u64,
26        transport_epoch: u64,
27        limits: SourceFrameBindLimits,
28    ) -> Result<SourceFrameBinding, SourceFrameError> {
29        if transport_epoch == 0 {
30            return Err(SourceFrameError::Input);
31        }
32        if self.is_device_lost() {
33            return Err(SourceFrameError::Backend);
34        }
35        if self.source_motion.externally_controlled() {
36            return Err(SourceFrameError::Stale);
37        }
38        // First profile is one full authored mesh, not instanced copies or an
39        // independently driven artefact joint layered over the native player.
40        if !self.source_motion_instance_supported() || self.artefact_joint.is_some() {
41            return Err(SourceFrameError::Backend);
42        }
43        if source_generation == 0 || source_sha256 == [0; 32] {
44            return Err(SourceFrameError::Input);
45        }
46        let mesh = self.mesh.as_ref().ok_or(SourceFrameError::Backend)?;
47        let (decoded, model_sha256) = decode_bound_model(model, limits)?;
48        validate_resident_mesh(&decoded, &mesh.cpu_positions, &mesh.cpu_indices)?;
49        let binding = SourceFrameBinding {
50            model_sha256,
51            source_sha256,
52            source_generation,
53            mesh_generation: self.mesh_generation,
54            transport_epoch,
55            vertex_count: mesh.vertex_count,
56            index_count: mesh.cpu_indices.len() as u32,
57        };
58        // Binding is the last operation; failed cold admission leaves prior state.
59        self.source_motion.bind(binding)?;
60        self.invalidate_source_pose_picks(false);
61        Ok(binding)
62    }
63
64    pub fn source_motion_binding(&self) -> Option<SourceFrameBinding> {
65        if self.is_device_lost() {
66            None
67        } else {
68            self.source_motion.binding()
69        }
70    }
71
72    #[allow(dead_code)]
73    pub(crate) fn source_motion_mesh_counts(&self) -> Option<(u32, u32)> {
74        self.mesh
75            .as_ref()
76            .map(|mesh| (mesh.vertex_count, mesh.cpu_indices.len() as u32))
77    }
78    pub fn retire_source_motion_binding(&mut self) {
79        self.source_motion.retire();
80        self.invalidate_source_pose_picks(false);
81    }
82    pub fn advance_source_motion_epoch(
83        &mut self,
84        expected: SourceFrameBinding,
85        next_epoch: u64,
86    ) -> Result<SourceFrameBinding, SourceFrameError> {
87        if self.is_device_lost() {
88            return Err(SourceFrameError::Backend);
89        }
90        if expected.mesh_generation != self.mesh_generation {
91            return Err(SourceFrameError::Stale);
92        }
93        let next = self.source_motion.advance_epoch(expected, next_epoch)?;
94        self.invalidate_source_pose_picks(false);
95        self.temporal_reset_pending = true;
96        Ok(next)
97    }
98    pub fn apply_source_motion_frame(
99        &mut self,
100        frame: SourceMeshFrame<'_>,
101    ) -> Result<SourceFrameReceipt, SourceFrameError> {
102        if self.is_device_lost() {
103            return Err(SourceFrameError::Backend);
104        }
105        if !self.source_motion_instance_supported() || self.artefact_joint.is_some() {
106            return Err(SourceFrameError::Backend);
107        }
108        let (next, receipt) = self.source_motion.prepare(&frame, self.mesh_generation)?;
109        let mesh = self.mesh.as_ref().ok_or(SourceFrameError::Backend)?;
110        if mesh.vertex_count != frame.binding.vertex_count
111            || mesh.cpu_indices.len() != frame.binding.index_count as usize
112        {
113            return Err(SourceFrameError::ResidentMismatch);
114        }
115        // Complete validation precedes the existing no-fail upload commit.
116        if !self.write_mesh_animated_frames(0, frame.positions, frame.normals, frame.tangents) {
117            return Err(SourceFrameError::Upload);
118        }
119        self.source_motion = next;
120        self.invalidate_source_pose_picks(true);
121        // No per-vertex previous-pose motion vectors are supplied by this seam.
122        // Reset history rather than pretending camera-only vectors describe skinning.
123        self.temporal_reset_pending = true;
124        Ok(receipt)
125    }
126
127    fn source_motion_instance_supported(&self) -> bool {
128        const IDENTITY: [[f32; 4]; 4] = [
129            [1., 0., 0., 0.],
130            [0., 1., 0., 0.],
131            [0., 0., 1., 0.],
132            [0., 0., 0., 1.],
133        ];
134        let [instance] = self.mesh_instance_source.as_slice() else {
135            return false;
136        };
137        let expected = crate::render::instance_culling::GpuInstanceRecord::new(IDENTITY, 0);
138        instance.world_from_local == expected.world_from_local
139            && instance.normal_from_local == expected.normal_from_local
140            && instance.orientation_sign == expected.orientation_sign
141    }
142
143    fn invalidate_source_pose_picks(&mut self, retain_unsubmitted_pointer: bool) {
144        // Pointer coordinates not submitted yet may target the new pose. Old
145        // readbacks/results cannot claim to describe it.
146        self.pick_readback.invalidate(retain_unsubmitted_pointer);
147        self.pick_semantic_result = None;
148    }
149}