1#![deny(missing_docs)]
2
3pub mod ops;
12
13pub trait ImageSize {
15 fn get_size(&self) -> (u32, u32);
17
18 #[inline(always)]
20 fn get_width(&self) -> u32 {
21 let (w, _) = self.get_size();
22 w
23 }
24
25 #[inline(always)]
27 fn get_height(&self) -> u32 {
28 let (_, h) = self.get_size();
29 h
30 }
31}
32
33#[derive(Clone, Copy)]
35pub struct TextureSettings {
36 convert_gamma: bool,
38 compress: bool,
40 generate_mipmap: bool,
42 min: Filter,
44 mag: Filter,
46 mipmap: Filter,
48 wrap_u: Wrap,
50 wrap_v: Wrap,
52 border_color: [f32; 4],
54}
55
56impl TextureSettings {
57 pub fn new() -> TextureSettings {
59 TextureSettings {
60 convert_gamma: false,
61 compress: false,
62 generate_mipmap: false,
63 min: Filter::Linear,
64 mag: Filter::Linear,
65 mipmap: Filter::Linear,
66 wrap_u: Wrap::ClampToEdge,
67 wrap_v: Wrap::ClampToEdge,
68 border_color: [0.0, 0.0, 0.0, 1.0],
69 }
70 }
71
72 pub fn get_convert_gamma(&self) -> bool { self.convert_gamma }
74 pub fn set_convert_gamma(&mut self, val: bool) { self.convert_gamma = val; }
76 pub fn convert_gamma(mut self, val: bool) -> Self {
78 self.set_convert_gamma(val);
79 self
80 }
81
82 pub fn get_compress(&self) -> bool { self.compress }
84 pub fn set_compress(&mut self, val: bool) { self.compress = val; }
86 pub fn compress(mut self, val: bool) -> Self {
88 self.set_compress(val);
89 self
90 }
91
92 pub fn get_generate_mipmap(&self) -> bool { self.generate_mipmap }
94 pub fn set_generate_mipmap(&mut self, val: bool) {
96 self.generate_mipmap = val;
97 }
98 pub fn generate_mipmap(mut self, val: bool) -> Self {
100 self.set_generate_mipmap(val);
101 self
102 }
103
104 pub fn get_min(&self) -> Filter { self.min }
106 pub fn set_min(&mut self, val: Filter) {
108 self.min = val
109 }
110 pub fn min(mut self, val: Filter) -> Self {
112 self.set_min(val);
113 self
114 }
115
116 pub fn get_mag(&self) -> Filter { self.mag }
118 pub fn set_mag(&mut self, val: Filter) {
120 self.mag = val;
121 }
122 pub fn mag(mut self, val: Filter) -> Self {
124 self.set_mag(val);
125 self
126 }
127
128 pub fn get_mipmap(&self) -> Filter { self.mipmap }
130 pub fn set_mipmap(&mut self, val: Filter) {
132 self.set_generate_mipmap(true);
133 self.mag = val;
134 }
135 pub fn mipmap(mut self, val: Filter) -> Self {
137 self.set_mag(val);
138 self
139 }
140
141 pub fn get_filter(&self) -> (Filter, Filter) { (self.min, self.mag) }
143 pub fn set_filter(&mut self, val: Filter) {
145 self.set_min(val);
146 self.set_mag(val);
147 }
148
149 pub fn filter(mut self, val: Filter) -> Self {
151 self.set_filter(val);
152 self
153 }
154
155 pub fn get_wrap_u(&self) -> Wrap {
157 self.wrap_u
158 }
159 pub fn set_wrap_u(& mut self, val: Wrap) {
161 self.wrap_u = val
162 }
163 pub fn wrap_u(mut self, val: Wrap) -> Self {
165 self.set_wrap_u(val);
166 self
167 }
168
169 pub fn get_wrap_v(&self) -> Wrap {
171 self.wrap_v
172 }
173 pub fn set_wrap_v(& mut self, val: Wrap) {
175 self.wrap_v = val
176 }
177 pub fn wrap_v(mut self, val: Wrap) -> Self {
179 self.set_wrap_v(val);
180 self
181 }
182
183 pub fn get_border_color(&self) -> [f32; 4] {
185 self.border_color
186 }
187 pub fn set_border_color(&mut self, val: [f32; 4]) {
189 self.border_color = val
190 }
191 pub fn border_color(mut self, val: [f32; 4]) -> Self {
193 self.set_border_color(val);
194 self
195 }
196
197}
198
199#[derive(Copy, Clone, Debug)]
201pub enum Format {
202 Rgba8,
204}
205
206pub trait TextureOp<F> {
208 type Error: core::fmt::Debug;
210}
211
212pub trait CreateTexture<F>: TextureOp<F> + ImageSize + Sized {
214 fn create<S: Into<[u32; 2]>>(
216 factory: &mut F,
217 format: Format,
218 memory: &[u8],
219 size: S,
220 settings: &TextureSettings
221 ) -> Result<Self, Self::Error>;
222}
223
224pub trait UpdateTexture<F>: TextureOp<F> + ImageSize + Sized {
226 fn update<O, S>(
231 &mut self,
232 factory: &mut F,
233 format: Format,
234 memory: &[u8],
235 offset: O,
236 size: S,
237 ) -> Result<(), Self::Error>
238 where O: Into<[u32; 2]>,
239 S: Into<[u32; 2]>;
240}
241
242#[derive(Copy, Clone, Debug)]
244pub enum Filter {
245 Linear,
247 Nearest
249}
250
251#[derive(Copy, Clone, Debug, PartialEq)]
253pub enum Wrap {
254 Repeat,
256 MirroredRepeat,
258 ClampToEdge,
260 ClampToBorder,
262}