Skip to main content

scenix_texture/
video.rs

1use scenix_core::ValidationError;
2
3use crate::Texture2D;
4
5/// Mutable CPU-side texture updated frame by frame.
6#[derive(Clone, Debug, PartialEq)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct VideoTexture {
9    /// Current texture frame.
10    pub texture: Texture2D,
11    /// Number of successfully uploaded frames.
12    pub frame_index: u64,
13    /// Whether the texture has changed since the last upload.
14    pub dirty: bool,
15}
16
17impl VideoTexture {
18    /// Creates a video texture from an initial frame.
19    #[inline]
20    pub fn new(texture: Texture2D) -> Self {
21        Self {
22            texture,
23            frame_index: 0,
24            dirty: true,
25        }
26    }
27
28    /// Replaces the current frame and marks the texture dirty.
29    pub fn update_frame(&mut self, data: &[u8]) -> Result<(), ValidationError> {
30        if data.len() != self.texture.base_level_len()? {
31            return Err(ValidationError::OutOfRange);
32        }
33        self.texture.data.clear();
34        self.texture.data.extend_from_slice(data);
35        self.frame_index = self.frame_index.saturating_add(1);
36        self.dirty = true;
37        Ok(())
38    }
39
40    /// Clears the dirty flag.
41    #[inline]
42    pub fn mark_clean(&mut self) {
43        self.dirty = false;
44    }
45}