mod3d_base/texture.rs
1//a Imports
2use std::cell::{Ref, RefCell};
3
4use crate::{BufferElementType, Renderable};
5
6//a Texture
7//tp Texture
8/// A texture is managed by the library as a byte slice which has up
9/// to three dimensions - minimally a width, with a 2D texture having
10/// a non-zero height, and a 3D texture with a non-zero depth
11///
12/// The 'elements' that make up each entry of the texture can be
13/// multiples of 1 to 4 of a fundamental element type (int or float,
14/// of 8, 16 or 32 bits as permitted)
15///
16/// After the texture has been created, it may be instantiated within
17/// the client, when a texture client handle is created by the client;
18/// this must be easily Cloned, particuarly if the texture is used in
19/// more than one instantiable object
20pub struct Texture<'texture, R: Renderable> {
21 /// The underlying data for the texture
22 pub data: &'texture [u8],
23 /// Width, height, and depth of the texture - width must be
24 /// non-zero
25 ///
26 /// If height is zero then the texture is 1D, and depth must be 0
27 ///
28 /// If height is non-zero and depth is zero then the texture is 2D
29 pub dims: (usize, usize, usize),
30 /// Number of elements per texture entry (1,2,3 or 4)
31 ///
32 /// An RGB texture would be 3; an RGBA texture 4.
33 pub elements_per_data: u32,
34 /// The type of each element
35 ///
36 /// For most image textures this is Int8
37 pub ele_type: BufferElementType,
38 /// Client handle/value
39 rc_client: RefCell<R::Texture>,
40}
41
42//ip Debug for Texture
43impl<'texture, R: Renderable> std::fmt::Debug for Texture<'texture, R>
44where
45 R: Renderable,
46{
47 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
48 writeln!(
49 fmt,
50 "Texture {{dims:{:?}, {:?}*{}, client:{:?}}}",
51 self.dims, self.ele_type, self.elements_per_data, self.rc_client
52 )?;
53 Ok(())
54 }
55}
56
57//ip Display for Texture
58impl<'texture, R: Renderable> std::fmt::Display for Texture<'texture, R>
59where
60 R: Renderable,
61{
62 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
63 writeln!(fmt, "Texture:")?;
64 writeln!(fmt, " dims: {:?}", self.dims)?;
65 Ok(())
66 }
67}
68
69///ip Texture
70impl<'texture, R: Renderable> Texture<'texture, R> {
71 //cp new
72 /// Create a new [Texture] object with no additional attributes
73 pub fn new(
74 data: &'texture [u8],
75 dims: (usize, usize, usize),
76 ele_type: BufferElementType,
77 elements_per_data: u32,
78 ) -> Self {
79 let rc_client = Default::default();
80 Self {
81 data,
82 dims,
83 ele_type,
84 elements_per_data,
85 rc_client,
86 }
87 }
88
89 //ap dims
90 /// Get the dimensions of the texture
91 pub fn dims(&self) -> &(usize, usize, usize) {
92 &self.dims
93 }
94
95 //ap data
96 /// Get the data slice for the texture
97 pub fn data(&self) -> &[u8] {
98 self.data
99 }
100
101 //ap data_type
102 /// Get the data slice for the texture
103 pub fn data_type(&self) -> (u32, BufferElementType) {
104 (self.elements_per_data, self.ele_type)
105 }
106
107 //mp create_client
108 /// Create the client texture
109 pub fn create_client(&self, renderer: &mut R) {
110 *(self.rc_client.borrow_mut()) = renderer.create_texture_client(self);
111 }
112
113 //ap borrow_client
114 /// Borrow the client
115 pub fn borrow_client(&self) -> Ref<R::Texture> {
116 self.rc_client.borrow()
117 }
118}