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
//a Imports
use std::cell::RefCell;
use crate::{BufferDescriptor, BufferElementType, Renderable, VertexAttr, VertexDesc};
//a BufferDataAccessor
//tp BufferDataAccessor
/// A subset of a `BufferData`, used for vertex attributes;
/// hence for use in a vertex attribute pointer.
///
/// A `BufferDataAccessor` is used for a single attribute of a set of data, such as
/// Position or Normal.
pub struct BufferDataAccessor<'a, R: Renderable> {
/// The `BufferData` that contains the actual vertex attribute data
desc: &'a BufferDescriptor<'a, R>,
/// Element index in [BufferDescriptor]
desc_index: u8,
/// The client bound to data\[byte_offset\] .. + byte_length
///
/// This must be held as a [RefCell] as the [BufferData] is
/// created early in the process, prior to any `BufferDataAccessor`s using
/// it - which then have shared references to the daata - but the
/// client is created afterwards
rc_client: RefCell<R::DataAccessor>,
}
//ip Display for Object
impl<'a, R: Renderable> std::fmt::Debug for BufferDataAccessor<'a, R>
where
R: Renderable,
{
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(
fmt,
"BufferDataAccessor{{{:?}}}",
self.desc.element(self.desc_index as usize),
)
}
}
//ip BufferDataAccessor
impl<'a, R: Renderable> BufferDataAccessor<'a, R> {
//fp new
/// Create a new view of a `BufferData`
pub fn new(desc: &'a BufferDescriptor<'a, R>, desc_index: u8) -> Self {
let rc_client = RefCell::new(R::DataAccessor::default());
Self {
desc,
desc_index,
rc_client,
}
}
//mp create_client
/// Create the render buffer required by the BufferDataAccessor
pub fn create_client(&self, renderable: &mut R) {
use std::ops::DerefMut;
renderable.init_data_accessor_client(self.rc_client.borrow_mut().deref_mut(), self);
}
//ap borrow_client
/// Borrow the client
pub fn borrow_client(&self) -> std::cell::Ref<R::DataAccessor> {
self.rc_client.borrow()
}
//ap desc_index
/// desc_index
pub fn desc_index(&self) -> u8 {
self.desc_index
}
//ap desc
/// desc
pub fn desc(&self) -> &BufferDescriptor<'a, R> {
&self.desc
}
//ap vertex_desc
/// Retrieve the vertex attribute this field is for
#[inline]
pub fn vertex_desc(&self) -> &VertexDesc {
self.desc.element(self.desc_index as usize)
}
//ap vertex_attr
/// Retrieve the vertex attribute this field is for
#[inline]
pub fn vertex_attr(&self) -> VertexAttr {
self.vertex_desc().vertex_attr()
}
//ap byte_offset
/// Retrieve the byte_offset within the [BufferData] for this field
#[inline]
pub fn byte_offset(&self) -> u32 {
self.vertex_desc().byte_offset() as u32 + self.desc.byte_offset()
}
//ap ele_type
/// Retrieve the [BufferElementType] of the field
#[inline]
pub fn ele_type(&self) -> BufferElementType {
self.vertex_desc().ele_type()
}
//ap count
/// Get the count of the number of elements in the field
#[inline]
pub fn count(&self) -> u32 {
self.vertex_desc().count()
}
//ap byte_length
/// Get the byte length of the field
pub fn byte_length(&self) -> u32 {
self.vertex_desc().byte_length()
}
//zz All done
}
//ip Display for BufferDataAccessor
impl<'a, R: Renderable> std::fmt::Display for BufferDataAccessor<'a, R> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
<Self as std::fmt::Debug>::fmt(self, f)
}
}
//ip DefaultIndentedDisplay for BufferDataAccessor
impl<'a, R: Renderable> indent_display::DefaultIndentedDisplay for BufferDataAccessor<'a, R> {}