1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use {
    super::{Data, Lease},
    crate::{
        math::{Quat, Sphere},
        pak::{
            model::{Builder, Mesh},
            IndexType,
        },
    },
    std::{
        cell::{Ref, RefCell, RefMut},
        fmt::{Debug, Error, Formatter},
    },
};

/// Data and length
pub type DataBuffer = (Lease<Data>, u64);

/// Data, length, and write mask (1 bit per index; all staged data is indexed)
pub type StagingBuffers = (Lease<Data>, u64, Lease<Data>);

// TODO: Could not force the lifetime to work without an explicit function which means I'm missing something really basic
#[inline]
fn deref_str<S: AsRef<str>>(s: &Option<S>) -> Option<&str> {
    if let Some(s) = s {
        Some(s.as_ref())
    } else {
        None
    }
}

pub struct MeshIter<'a> {
    filter: Option<MeshFilter>,
    idx: usize,
    model: &'a Model,
}

impl<'a> Iterator for MeshIter<'a> {
    type Item = &'a Mesh;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(filter) = self.filter {
            if let Some(mesh) = self.model.meshes.get(filter.0 as usize + self.idx) {
                if mesh.name() == self.model.meshes[self.idx].name() {
                    self.idx += 1;
                    return Some(mesh);
                }
            }

            None
        } else if let Some(mesh) = self.model.meshes.get(self.idx) {
            self.idx += 1;
            Some(mesh)
        } else {
            None
        }
    }
}

/// A reference to an individual mesh name, which may be shared by multiple meshes. It is undefined
/// behavior to use a MeshFilter with any Model other than the one it was received from.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct MeshFilter(u16);

/// A drawable collection of individually adressable meshes.
pub struct Model {
    idx_buf: RefCell<Lease<Data>>,
    idx_buf_len: u64,
    idx_ty: IndexType,
    meshes: Vec<Mesh>,
    staging: RefCell<Option<StagingBuffers>>,
    vertex_buf: RefCell<Lease<Data>>,
    vertex_buf_len: u64,
}

impl Model {
    /// Meshes must be sorted by name
    pub(crate) fn new(
        meshes: Vec<Mesh>,
        idx_ty: IndexType,
        idx_buf: DataBuffer,
        vertex_buf: DataBuffer,
        staging: StagingBuffers,
    ) -> Self {
        let (idx_buf, idx_buf_len) = idx_buf;
        let (vertex_buf, vertex_buf_len) = vertex_buf;

        Self {
            idx_buf: RefCell::new(idx_buf),
            idx_buf_len,
            idx_ty,
            meshes,
            staging: RefCell::new(Some(staging)),
            vertex_buf: RefCell::new(vertex_buf),
            vertex_buf_len,
        }
    }

    /// Constructs a `Builder` with the given vertex count.
    pub fn mesh<N>(vertex_count: u32) -> Builder<N> {
        Builder::new(vertex_count)
    }

    /// Gets the `Sphere` which defines the rough bounding area for this model.
    pub fn bounds(&self) -> Sphere {
        todo!("Get bounds")
    }

    /// Gets a small value which allows filtered drawing of this mesh.prelude_all
    ///
    /// A returned `None` indicates the given name was not found in this model.
    ///
    /// _NOTE:_ This API may be a little dangerous because there are no lifetimes or anything
    /// telling you about the danger of getting this value from one `Model` and using it on
    /// another. Just don't do that please, or help me patch it.
    ///
    /// ## Examples
    ///
    /// ```
    /// let material: Material = ...
    /// let foo: ModelRef = ...
    /// let bar = foo.filter("bar");
    ///
    /// let frame: Render = ...
    /// frame.draw().record(&camera, [
    ///     // 🔍 Note the foo-bar tuple here which says "Draw the bar part of foo"
    ///     Draw::model((foo, bar), material, Mat4::identity()),
    /// ]);
    ///
    /// ...
    /// ```
    pub fn filter<N: AsRef<str>>(&self, name: Option<N>) -> Option<MeshFilter> {
        let name_str = deref_str(&name);
        match self
            .meshes
            .binary_search_by(|probe| probe.name().cmp(&name_str))
        {
            Err(_) => None,
            Ok(mut idx) => {
                // Rewind to the start of this same-named group
                while idx > 0 {
                    let next_idx = idx - 1;
                    if self.meshes[next_idx].name() == name_str {
                        idx = next_idx;
                    } else {
                        break;
                    }
                }

                Some(MeshFilter(idx as _))
            }
        }
    }

    pub(crate) fn idx_ty(&self) -> IndexType {
        self.idx_ty
    }

    pub(crate) fn idx_buf_ref(&self) -> (Ref<'_, Lease<Data>>, u64) {
        (self.idx_buf.borrow(), self.idx_buf_len)
    }

    pub(crate) fn idx_buf_mut(&self) -> (RefMut<'_, Lease<Data>>, u64) {
        (self.idx_buf.borrow_mut(), self.idx_buf_len)
    }

    /// Remarks: Guaranteed to be in vertex buffer order (each mesh has a unique block of vertices)
    pub(super) fn meshes(&self) -> MeshIter {
        MeshIter {
            filter: None,
            idx: 0,
            model: self,
        }
    }

    /// Remarks: Guaranteed to be in vertex buffer order (each mesh has a unique block of vertices)
    pub(super) fn meshes_filter(&self, filter: MeshFilter) -> MeshIter {
        MeshIter {
            filter: Some(filter),
            idx: 0,
            model: self,
        }
    }

    /// Remarks: Guaranteed to be in vertex buffer order (each mesh has a unique block of vertices)
    pub(super) fn meshes_filter_is(&self, filter: Option<MeshFilter>) -> MeshIter {
        MeshIter {
            filter,
            idx: 0,
            model: self,
        }
    }

    /// You must submit writes for our buffers if you call this.
    pub(super) fn take_pending_writes(&self) -> Option<StagingBuffers> {
        self.staging.borrow_mut().take()
    }

    /// Gets the `Sphere` which defines the rough bounding area for this model, account for the
    /// given pose.
    pub fn pose_bounds(&self, _pose: &Pose) -> Sphere {
        todo!("Get bounds w/ pose")
    }

    /// Sets a descriptive name for debugging which can be seen with API tracing tools such as
    /// RenderDoc.
    #[cfg(feature = "debug-names")]
    pub fn set_name(&mut self, name: &str) {
        self.idx_buf.borrow_mut().set_name(name);
        self.vertex_buf.borrow_mut().set_name(name);
    }

    pub(crate) fn vertex_buf_ref(&self) -> (Ref<'_, Lease<Data>>, u64) {
        (self.vertex_buf.borrow(), self.vertex_buf_len)
    }

    pub(crate) fn vertex_buf_mut(&self) -> (RefMut<'_, Lease<Data>>, u64) {
        (self.vertex_buf.borrow_mut(), self.vertex_buf_len)
    }
}

impl Debug for Model {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        f.write_str("Model")
    }
}

/// TODO
#[derive(Clone, Debug)]
pub struct Pose {
    joints: Vec<Quat>,
}

impl Pose {
    /// TODO
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            joints: Vec::with_capacity(capacity),
        }
    }

    /// TODO
    pub fn joint<N: AsRef<str>>(&self, _name: N) -> Quat {
        // let name = name.as_ref();
        // match self.joints.binary_search_by(|a| name.cmp(&a.0)) {
        //     Err(_) => panic!("Joint not found"),
        //     Ok(idx) => self.joints[idx].1
        // }
        todo!();
    }

    /// TODO
    pub fn set_joint(&mut self, _name: String, _val: Quat) {
        // match self.joints.binary_search_by(|a| name.cmp(&a.0)) {
        //     Err(idx) => self.joints.insert(idx, (name, val)),
        //     Ok(idx) => self.joints[idx].1 = val,
        // }
    }
}