crystal_api/
gpu_data.rs

1use std::{
2    sync::{Arc, Mutex},
3    usize,
4};
5
6use crate::traits;
7
8/// ```AsBytes``` represents a memory region as slice of bytes
9pub trait AsBytes {
10    /// ```as_bytes``` represents a memory region as slice of bytes
11    fn as_bytes(&self) -> &[u8];
12}
13
14impl<T> AsBytes for Vec<T> {
15    fn as_bytes(&self) -> &[u8] {
16        unsafe {
17            std::slice::from_raw_parts(self.as_ptr() as *const u8, self.len() * size_of::<T>())
18        }
19    }
20}
21
22impl<T> AsBytes for &[T] {
23    fn as_bytes(&self) -> &[u8] {
24        unsafe {
25            std::slice::from_raw_parts(self.as_ptr() as *const u8, self.len() * size_of::<T>())
26        }
27    }
28}
29
30/// ## Used for textures bindings in shaders
31pub struct GpuSamplerSet {
32    pub(crate) id: Mutex<usize>,
33    pub(crate) textures: Vec<(u32, Arc<dyn traits::Texture>)>,
34}
35
36impl GpuSamplerSet {
37    /// Creates new GpuSamplerSet from textures and its bindings
38    pub fn from_textures(textures: &[(u32, Arc<dyn traits::Texture>)]) -> Arc<Self> {
39        Arc::new(Self {
40            id: Mutex::new(usize::MAX),
41            textures: textures.to_vec(),
42        })
43    }
44}