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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
use std::marker::PhantomData;
use pixel::Pixel;
#[derive(Clone, Copy, Debug)]
pub enum Wrap {
ClampToEdge,
Repeat,
MirroredRepeat
}
#[derive(Clone, Copy, Debug)]
pub enum Filter {
Nearest,
Linear
}
#[derive(Clone, Copy, Debug)]
pub enum DepthComparison {
Never,
Always,
Equal,
NotEqual,
Less,
LessOrEqual,
Greater,
GreaterOrEqual
}
pub trait Dimensionable {
type Size;
type Offset;
fn dim() -> Dim;
fn width(size: Self::Size) -> u32 where Self::Size: Copy;
fn height(_: Self::Size) -> u32 where Self::Size: Copy { 1 }
fn depth(_: Self::Size) -> u32 where Self::Size: Copy { 1 }
fn x_offset(offset: Self::Offset) -> u32 where Self::Offset: Copy;
fn y_offset(_: Self::Offset) -> u32 where Self::Offset: Copy { 1 }
fn z_offset(_: Self::Offset) -> u32 where Self::Offset: Copy { 1 }
fn zero_offset() -> Self::Offset;
}
pub fn dim_capacity<D>(size: D::Size) -> u32 where D: Dimensionable, D::Size: Copy {
D::width(size) * D::height(size) * D::depth(size)
}
#[derive(Clone, Copy, Debug)]
pub enum Dim {
Dim1,
Dim2,
Dim3,
Cubemap
}
#[derive(Clone, Copy, Debug)]
pub struct Dim1;
impl Dimensionable for Dim1 {
type Size = u32;
type Offset = u32;
fn dim() -> Dim { Dim::Dim1 }
fn width(w: Self::Size) -> u32 { w }
fn x_offset(off: Self::Offset) -> u32 { off }
fn zero_offset() -> Self::Offset { 0 }
}
#[derive(Clone, Copy, Debug)]
pub struct Dim2;
impl Dimensionable for Dim2 {
type Size = (u32, u32);
type Offset = (u32, u32);
fn dim() -> Dim { Dim::Dim2 }
fn width(size: Self::Size) -> u32 { size.0 }
fn height(size: Self::Size) -> u32 { size.1 }
fn x_offset(off: Self::Offset) -> u32 { off.0 }
fn y_offset(off: Self::Offset) -> u32 { off.1 }
fn zero_offset() -> Self::Offset { (0, 0) }
}
#[derive(Clone, Copy, Debug)]
pub struct Dim3;
impl Dimensionable for Dim3 {
type Size = (u32, u32, u32);
type Offset = (u32, u32, u32);
fn dim() -> Dim { Dim::Dim3 }
fn width(size: Self::Size) -> u32 { size.0 }
fn height(size: Self::Size) -> u32 { size.1 }
fn depth(size: Self::Size) -> u32 { size.2 }
fn x_offset(off: Self::Offset) -> u32 { off.0 }
fn y_offset(off: Self::Offset) -> u32 { off.1 }
fn z_offset(off: Self::Offset) -> u32 { off.2 }
fn zero_offset() -> Self::Offset { (0, 0, 0) }
}
#[derive(Clone, Copy, Debug)]
pub struct Cubemap;
impl Dimensionable for Cubemap {
type Size = u32;
type Offset = (u32, u32, CubeFace);
fn dim() -> Dim { Dim::Cubemap }
fn width(s: Self::Size) -> u32 { s }
fn height(s: Self::Size) -> u32 { s }
fn depth(_: Self::Size) -> u32 { 6 }
fn x_offset(off: Self::Offset) -> u32 { off.0 }
fn y_offset(off: Self::Offset) -> u32 { off.1 }
fn z_offset(off: Self::Offset) -> u32 {
match off.2 {
CubeFace::PositiveX => 0,
CubeFace::NegativeX => 1,
CubeFace::PositiveY => 2,
CubeFace::NegativeY => 3,
CubeFace::PositiveZ => 4,
CubeFace::NegativeZ => 5
}
}
fn zero_offset() -> Self::Offset { (0, 0, CubeFace::PositiveX) }
}
#[derive(Clone, Copy, Debug)]
pub enum CubeFace {
PositiveX,
NegativeX,
PositiveY,
NegativeY,
PositiveZ,
NegativeZ
}
pub trait Layerable {
fn layering() -> Layering;
}
#[derive(Clone, Copy, Debug)]
pub enum Layering {
Flat,
Layered
}
#[derive(Clone, Copy, Debug)]
pub struct Flat;
impl Layerable for Flat { fn layering() -> Layering { Layering::Flat } }
#[derive(Clone, Copy, Debug)]
pub struct Layered;
impl Layerable for Layered { fn layering() -> Layering { Layering::Layered } }
pub trait HasTexture {
type ATexture;
fn new_texture<L, D, P>(size: D::Size, mipmaps: usize, sampler: &Sampler) -> Self::ATexture
where L: Layerable,
D: Dimensionable,
D::Size: Copy,
P: Pixel;
fn free(tex: &mut Self::ATexture);
fn clear_part<L, D, P>(tex: &Self::ATexture, gen_mimpmaps: bool, offset: D::Offset, size: D::Size, pixel: P::Encoding)
where L: Layerable, D: Dimensionable, D::Offset: Copy, D::Size: Copy, P: Pixel, P::Encoding: Copy;
fn upload_part<L, D, P>(tex: &Self::ATexture, gen_mipmaps: bool, offset: D::Offset, size: D::Size, texels: &Vec<P::Encoding>)
where L: Layerable, D::Offset: Copy, D::Size: Copy, D: Dimensionable, P: Pixel;
fn upload_part_raw<L, D, P>(tex: &Self::ATexture, gen_mipmaps: bool, offset: D::Offset, size: D::Size, texels: &Vec<P::RawEncoding>)
where L: Layerable, D::Offset: Copy, D::Size: Copy, D: Dimensionable, P: Pixel;
fn get_raw_texels<P>(tex: &Self::ATexture) -> Vec<P::RawEncoding> where P: Pixel, P::RawEncoding: Copy;
}
#[derive(Debug)]
pub struct Texture<C, L, D, P> where C: HasTexture, L: Layerable, D: Dimensionable, P: Pixel {
pub repr: C::ATexture,
pub size: D::Size,
pub mipmaps: usize,
_l: PhantomData<L>,
_c: PhantomData<C>,
_p: PhantomData<P>
}
impl<C, L, D, P> Drop for Texture<C, L, D, P> where C: HasTexture, L: Layerable, D: Dimensionable, P: Pixel {
fn drop(&mut self) {
C::free(&mut self.repr)
}
}
impl<C, L, D, P> Texture<C, L, D, P>
where C: HasTexture,
L: Layerable,
D: Dimensionable,
D::Size: Copy,
P: Pixel {
pub fn new(size: D::Size, mipmaps: usize, sampler: &Sampler) -> Self {
let mipmaps = mipmaps + 1;
let tex = C::new_texture::<L, D, P>(size, mipmaps, sampler);
Texture {
repr: tex,
size: size,
mipmaps: mipmaps,
_c: PhantomData,
_l: PhantomData,
_p: PhantomData
}
}
pub fn from_raw(texture: C::ATexture, size: D::Size, mipmaps: usize) -> Self {
Texture {
repr: texture,
size: size,
mipmaps: mipmaps + 1,
_c: PhantomData,
_l: PhantomData,
_p: PhantomData
}
}
pub fn clear_part(&self, gen_mipmaps: bool, offset: D::Offset, size: D::Size, pixel: P::Encoding)
where D::Offset: Copy,
D::Size: Copy,
P::Encoding: Copy {
C::clear_part::<L, D, P>(&self.repr, gen_mipmaps, offset, size, pixel)
}
pub fn clear(&self, gen_mipmaps: bool, pixel: P::Encoding)
where D::Offset: Copy,
D::Size: Copy,
P::Encoding: Copy {
self.clear_part(gen_mipmaps, D::zero_offset(), self.size, pixel)
}
pub fn upload_part(&self, gen_mipmaps: bool, offset: D::Offset, size: D::Size, texels: &Vec<P::Encoding>)
where D::Offset: Copy,
D::Size: Copy {
C::upload_part::<L, D, P>(&self.repr, gen_mipmaps, offset, size, texels)
}
pub fn upload(&self, gen_mipmaps: bool, texels: &Vec<P::Encoding>)
where D::Offset: Copy,
D::Size: Copy {
self.upload_part(gen_mipmaps, D::zero_offset(), self.size, texels)
}
pub fn upload_part_raw(&self, gen_mipmaps: bool, offset: D::Offset, size: D::Size, texels: &Vec<P::RawEncoding>)
where D::Offset: Copy,
D::Size: Copy {
C::upload_part_raw::<L, D, P>(&self.repr, gen_mipmaps, offset, size, texels)
}
pub fn upload_raw(&self, gen_mipmaps: bool, texels: &Vec<P::RawEncoding>)
where D::Offset: Copy,
D::Size: Copy {
self.upload_part_raw(gen_mipmaps, D::zero_offset(), self.size, texels)
}
pub fn get_raw_texels(&self) -> Vec<P::RawEncoding> where P: Pixel, P::RawEncoding: Copy {
C::get_raw_texels::<P>(&self.repr)
}
}
#[derive(Clone, Copy, Debug)]
pub struct Sampler {
pub wrap_r: Wrap,
pub wrap_s: Wrap,
pub wrap_t: Wrap,
pub minification: Filter,
pub magnification: Filter,
pub depth_comparison: Option<DepthComparison>
}
impl Default for Sampler {
fn default() -> Self {
Sampler {
wrap_r: Wrap::ClampToEdge,
wrap_s: Wrap::ClampToEdge,
wrap_t: Wrap::ClampToEdge,
minification: Filter::Linear,
magnification: Filter::Linear,
depth_comparison: None
}
}
}