mln_engine/draw/
indexes.rs1use mln_gfx::gpu::{BufferId, BufferUsage, Gpu, GpuError};
8#[cfg(target_family = "wasm")]
9use mln_wire::host_bytes::HostBytes;
10
11use crate::draw::buffer::Buffer;
12use crate::draw::context::Context;
13use crate::draw::owned::Held;
14use crate::draw::resources::Device;
15pub(crate) use crate::draw::store::SharedIndexes;
16
17enum IndexData {
19 Vec(Vec<u16>),
20 #[cfg(target_family = "wasm")]
23 Host(HostBytes),
24}
25
26enum Place {
28 Data(IndexData),
29 Buffer(Buffer),
30}
31
32pub struct Indexes {
35 len: usize,
36 place: Place,
37}
38
39impl Indexes {
40 #[must_use]
42 pub fn new(indexes: Vec<u16>) -> Indexes {
43 Indexes { len: indexes.len(), place: Place::Data(IndexData::Vec(indexes)) }
44 }
45
46 #[cfg(target_family = "wasm")]
48 #[must_use]
49 pub(crate) fn from_host(indexes: HostBytes) -> Indexes {
50 debug_assert!(indexes.len().is_multiple_of(size_of::<u16>()));
51 Indexes { len: indexes.len() / size_of::<u16>(), place: Place::Data(IndexData::Host(indexes)) }
52 }
53
54 #[must_use]
56 pub(crate) fn len(&self) -> usize {
57 self.len
58 }
59
60 #[must_use]
62 pub(crate) fn has_data(&self) -> bool {
63 matches!(self.place, Place::Data(_))
64 }
65
66 #[must_use]
68 fn buffer(&self) -> Option<BufferId> {
69 match &self.place {
70 Place::Buffer(buffer) => Some(buffer.id()),
71 Place::Data(_) => None,
72 }
73 }
74
75 pub(crate) fn upload(&mut self, gpu: &mut dyn Gpu, device: &mut Device) -> Result<(), GpuError> {
79 let Place::Data(data) = &self.place else {
80 return Ok(());
81 };
82 let buffer = match data {
83 IndexData::Vec(indexes) => device.buffer(gpu, BufferUsage::Index, bytemuck::cast_slice(indexes))?,
84 #[cfg(target_family = "wasm")]
85 IndexData::Host(bytes) => device.buffer_from_host(gpu, BufferUsage::Index, bytes)?,
86 };
87 self.place = Place::Buffer(buffer);
88 Ok(())
89 }
90}
91
92pub enum DrawIndexes {
95 Own(Indexes),
96 Shared(SharedIndexes),
97}
98
99impl From<Indexes> for DrawIndexes {
100 fn from(indexes: Indexes) -> DrawIndexes {
101 DrawIndexes::Own(indexes)
102 }
103}
104
105impl From<SharedIndexes> for DrawIndexes {
106 fn from(indexes: SharedIndexes) -> DrawIndexes {
107 DrawIndexes::Shared(indexes)
108 }
109}
110
111impl DrawIndexes {
112 #[must_use]
114 pub fn new(indexes: Vec<u16>) -> DrawIndexes {
115 DrawIndexes::Own(Indexes::new(indexes))
116 }
117
118 fn indexes<'a>(&'a self, context: &'a Context) -> Option<&'a Indexes> {
120 match self {
121 DrawIndexes::Own(indexes) => Some(indexes),
122 DrawIndexes::Shared(shared) => context.indexes(shared),
123 }
124 }
125
126 #[must_use]
128 pub fn len(&self, context: &Context) -> usize {
129 self.indexes(context).map_or(0, Indexes::len)
130 }
131
132 pub fn has_data(&self, context: &Context) -> bool {
134 self.indexes(context).is_some_and(Indexes::has_data)
135 }
136
137 pub fn upload(&mut self, gpu: &mut dyn Gpu, context: &mut Context) -> Result<(), GpuError> {
139 match self {
140 DrawIndexes::Own(indexes) => indexes.upload(gpu, context.device()),
141 DrawIndexes::Shared(shared) => context.upload_indexes(gpu, shared),
142 }
143 }
144
145 #[must_use]
147 pub fn buffer(&self, context: &Context) -> Option<BufferId> {
148 self.indexes(context).and_then(Indexes::buffer)
149 }
150
151 pub(crate) fn held(&self) -> Option<Held> {
154 match self {
155 DrawIndexes::Own(_) => None,
156 DrawIndexes::Shared(shared) => Some(Held::Indexes(shared.clone())),
157 }
158 }
159}