mod3d_base/buffer_descriptor.rs
1//a Imports
2use std::cell::RefCell;
3
4use crate::{BufferData, Renderable, VertexDesc};
5
6//a BufferDescriptor
7//tp BufferDescriptor
8/// A descriptor of a subset of a `BufferData`, used for vertex attributes;
9/// hence for use in a vertex attribute pointer.
10///
11/// A `BufferDescriptor` is used for a single attribute of a set of data, such as
12/// Position or Normal.
13pub struct BufferDescriptor<'a, R: Renderable + ?Sized> {
14 /// The `BufferData` that contains the actual vertex attribute data
15 pub data: &'a BufferData<'a, R>,
16 /// Stride of data in the buffer - 0 for count*sizeof(ele_type)
17 /// Unused for indices
18 pub stride: u32,
19 /// Byte offset to first data inside 'data'
20 pub byte_offset: u32,
21 /// Description of the layout of the elements of the actual portion of buffer data
22 ///
23 /// This could become a reference to a struct that is borrowed here, with its own client ref
24 pub elements: Vec<VertexDesc>,
25 /// The client bound to data\[byte_offset\] .. + byte_length
26 ///
27 /// This must be held as a [RefCell] as the [BufferData] is
28 /// created early in the process, prior to any `BufferDescriptor`s using
29 /// it - which then have shared references to the daata - but the
30 /// client is created afterwards
31 rc_client: RefCell<R::Descriptor>,
32}
33
34//ip Display for Object
35impl<'a, R: Renderable> std::fmt::Debug for BufferDescriptor<'a, R>
36where
37 R: Renderable,
38{
39 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
40 write!(
41 fmt,
42 "BufferDescriptor{{ {:?} @{}+*{}}}",
43 self.data,
44 self.byte_offset,
45 self.stride,
46 // self.ele_type,
47 // self.elements_per_data,
48 // self.rc_client
49 )
50 }
51}
52
53//ip BufferDescriptor
54impl<'a, R: Renderable> BufferDescriptor<'a, R> {
55 //fp new
56 /// Create a new view of a `BufferData`
57 pub fn new(
58 data: &'a BufferData<'a, R>,
59 byte_offset: u32, // offset in bytes from start of data
60 stride: u32, /* stride between elements
61 * (0->count*sizeof(ele_type)) */
62 elements: Vec<VertexDesc>,
63 ) -> Self {
64 let rc_client = RefCell::new(R::Descriptor::default());
65 Self {
66 data,
67 byte_offset,
68 stride,
69 elements,
70 rc_client,
71 }
72 }
73
74 //mp create_client
75 /// Create the render buffer required by the BufferDescriptor
76 pub fn create_client(&self, renderable: &mut R) {
77 use std::ops::DerefMut;
78 renderable.init_buffer_desc_client(self.rc_client.borrow_mut().deref_mut(), self);
79 }
80
81 //ap borrow_client
82 /// Borrow the client
83 pub fn borrow_client(&self) -> std::cell::Ref<R::Descriptor> {
84 self.rc_client.borrow()
85 }
86
87 //zz All done
88}
89
90//ip Display for BufferDescriptor
91impl<'a, R: Renderable> std::fmt::Display for BufferDescriptor<'a, R> {
92 fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
93 std::fmt::Debug::fmt(self, f)
94 }
95}
96
97//ip DefaultIndentedDisplay for BufferDescriptor
98impl<'a, R: Renderable> indent_display::DefaultIndentedDisplay for BufferDescriptor<'a, R> {}