crystal_api/
object.rs

1use std::sync::Arc;
2
3use crate::{Buffer, GpuSamplerSet, Pipeline, mesh::Mesh};
4
5/// Used for GPU mesh data
6pub struct MeshBuffer {
7    pub(crate) mesh: Arc<Mesh>,
8    pub(crate) vertices: Arc<dyn Buffer>,
9    pub(crate) indices: Arc<dyn Buffer>,
10}
11
12/// Unified object used in GPU operations
13pub struct Object {
14    pub(crate) pipeline: Arc<dyn Pipeline>,
15    pub(crate) mesh_buffer: Option<Arc<MeshBuffer>>,
16    pub(crate) sampler: Option<Arc<GpuSamplerSet>>,
17    pub(crate) groups: Option<[u32; 3]>,
18    pub(crate) index: u32,
19    pub(crate) array: u32,
20}
21
22unsafe impl Sync for Object {}
23unsafe impl Send for Object {}
24
25#[allow(dead_code)]
26impl Object {
27    /// Creates compute object
28    pub fn compute(pipeline: Arc<dyn Pipeline>, groups: [u32; 3]) -> Arc<Self> {
29        Arc::new(Self {
30            pipeline,
31            mesh_buffer: None,
32            sampler: None,
33            groups: Some(groups),
34            index: 0,
35            array: 0,
36        })
37    }
38
39    /// Creates compute object with textures
40    pub fn compute_with_textures(
41        pipeline: Arc<dyn Pipeline>,
42        groups: [u32; 3],
43        sampler: Arc<GpuSamplerSet>,
44    ) -> Arc<Self> {
45        Arc::new(Self {
46            pipeline,
47            mesh_buffer: None,
48            sampler: Some(sampler),
49            groups: Some(groups),
50            index: 0,
51            array: 0,
52        })
53    }
54
55    /// Creates graphics object with mesh only
56    pub fn with_mesh(index: u32, pipeline: Arc<dyn Pipeline>, mesh: Arc<MeshBuffer>) -> Arc<Self> {
57        Arc::new(Self {
58            pipeline,
59            mesh_buffer: Some(mesh),
60            sampler: None,
61            groups: None,
62            index,
63            array: 1,
64        })
65    }
66
67    /// Creates graphics object with mesh and textures
68    pub fn with_mesh_sampled(
69        index: u32,
70        pipeline: Arc<dyn Pipeline>,
71        mesh: Arc<MeshBuffer>,
72        sampler: Arc<GpuSamplerSet>,
73    ) -> Arc<Self> {
74        Arc::new(Self {
75            pipeline,
76            mesh_buffer: Some(mesh),
77            sampler: Some(sampler),
78            groups: None,
79            index,
80            array: 1,
81        })
82    }
83
84    /// Creates array of graphics objects with mesh
85    pub fn with_mesh_array(
86        index: u32,
87        pipeline: Arc<dyn Pipeline>,
88        mesh: Arc<MeshBuffer>,
89        array: u32,
90    ) -> Arc<Self> {
91        Arc::new(Self {
92            pipeline,
93            mesh_buffer: Some(mesh),
94            sampler: None,
95            groups: None,
96            index,
97            array,
98        })
99    }
100
101    /// Creates array of graphics objects with mesh and textures
102    pub fn with_mesh_sampled_array(
103        index: u32,
104        pipeline: Arc<dyn Pipeline>,
105        mesh: Arc<MeshBuffer>,
106        sampler: Arc<GpuSamplerSet>,
107        array: u32,
108    ) -> Arc<Self> {
109        Arc::new(Self {
110            pipeline,
111            mesh_buffer: Some(mesh),
112            sampler: Some(sampler),
113            groups: None,
114            index,
115            array,
116        })
117    }
118}