mod3d_base/
buffer_data_accessor.rs

1//a Imports
2use std::cell::RefCell;
3
4use crate::{BufferDescriptor, BufferElementType, Renderable, VertexAttr, VertexDesc};
5
6//a BufferDataAccessor
7//tp BufferDataAccessor
8/// A subset of a `BufferData`, used for vertex attributes;
9/// hence for use in a vertex attribute pointer.
10///
11/// A `BufferDataAccessor` is used for a single attribute of a set of data, such as
12/// Position or Normal.
13pub struct BufferDataAccessor<'a, R: Renderable> {
14    /// The `BufferData` that contains the actual vertex attribute data
15    desc: &'a BufferDescriptor<'a, R>,
16
17    /// Element index in [BufferDescriptor]
18    desc_index: u8,
19
20    /// The client bound to data\[byte_offset\] .. + byte_length
21    ///
22    /// This must be held as a [RefCell] as the [BufferData] is
23    /// created early in the process, prior to any `BufferDataAccessor`s using
24    /// it - which then have shared references to the daata - but the
25    /// client is created afterwards
26    rc_client: RefCell<R::DataAccessor>,
27}
28
29//ip Display for Object
30impl<'a, R: Renderable> std::fmt::Debug for BufferDataAccessor<'a, R>
31where
32    R: Renderable,
33{
34    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
35        write!(
36            fmt,
37            "BufferDataAccessor{{{:?}}}",
38            self.desc.element(self.desc_index as usize),
39        )
40    }
41}
42
43//ip BufferDataAccessor
44impl<'a, R: Renderable> BufferDataAccessor<'a, R> {
45    //fp new
46    /// Create a new view of a `BufferData`
47    pub fn new(desc: &'a BufferDescriptor<'a, R>, desc_index: u8) -> Self {
48        let rc_client = RefCell::new(R::DataAccessor::default());
49        Self {
50            desc,
51            desc_index,
52            rc_client,
53        }
54    }
55
56    //mp create_client
57    /// Create the render buffer required by the BufferDataAccessor
58    pub fn create_client(&self, renderable: &mut R) {
59        use std::ops::DerefMut;
60        renderable.init_data_accessor_client(self.rc_client.borrow_mut().deref_mut(), self);
61    }
62
63    //ap borrow_client
64    /// Borrow the client
65    pub fn borrow_client(&self) -> std::cell::Ref<R::DataAccessor> {
66        self.rc_client.borrow()
67    }
68
69    //ap desc_index
70    /// desc_index
71    pub fn desc_index(&self) -> u8 {
72        self.desc_index
73    }
74
75    //ap desc
76    /// desc
77    pub fn desc(&self) -> &BufferDescriptor<'a, R> {
78        &self.desc
79    }
80
81    //ap vertex_desc
82    /// Retrieve the vertex attribute this field is for
83    #[inline]
84    pub fn vertex_desc(&self) -> &VertexDesc {
85        self.desc.element(self.desc_index as usize)
86    }
87
88    //ap vertex_attr
89    /// Retrieve the vertex attribute this field is for
90    #[inline]
91    pub fn vertex_attr(&self) -> VertexAttr {
92        self.vertex_desc().vertex_attr()
93    }
94
95    //ap byte_offset
96    /// Retrieve the byte_offset within the [BufferData] for this field
97    #[inline]
98    pub fn byte_offset(&self) -> u32 {
99        self.vertex_desc().byte_offset() as u32 + self.desc.byte_offset()
100    }
101
102    //ap ele_type
103    /// Retrieve the [BufferElementType] of the field
104    #[inline]
105    pub fn ele_type(&self) -> BufferElementType {
106        self.vertex_desc().ele_type()
107    }
108
109    //ap count
110    /// Get the count of the number of elements in the field
111    #[inline]
112    pub fn count(&self) -> u32 {
113        self.vertex_desc().count()
114    }
115
116    //ap byte_length
117    /// Get the byte length of the field
118    pub fn byte_length(&self) -> u32 {
119        self.vertex_desc().byte_length()
120    }
121
122    //zz All done
123}
124
125//ip Display for BufferDataAccessor
126impl<'a, R: Renderable> std::fmt::Display for BufferDataAccessor<'a, R> {
127    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
128        <Self as std::fmt::Debug>::fmt(self, f)
129    }
130}
131
132//ip DefaultIndentedDisplay for BufferDataAccessor
133impl<'a, R: Renderable> indent_display::DefaultIndentedDisplay for BufferDataAccessor<'a, R> {}