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
mod texture2d;
#[doc(inline)]
pub use texture2d::*;
mod texture_cube_map;
#[doc(inline)]
pub use texture_cube_map::*;
mod depth_texture2d;
#[doc(inline)]
pub use depth_texture2d::*;
mod texture2d_array;
#[doc(inline)]
pub use texture2d_array::*;
mod texture3d;
#[doc(inline)]
pub use texture3d::*;
mod depth_texture2d_array;
#[doc(inline)]
pub use depth_texture2d_array::*;
mod depth_texture_cube_map;
#[doc(inline)]
pub use depth_texture_cube_map::*;
use data_type::*;
pub use three_d_asset::texture::{
Interpolation, Texture2D as CpuTexture, Texture3D as CpuTexture3D, TextureData, Wrapping,
};
pub trait TextureDataType: DataType {}
impl TextureDataType for u8 {}
impl TextureDataType for f16 {}
impl TextureDataType for f32 {}
impl<T: TextureDataType + PrimitiveDataType> TextureDataType for Vector2<T> {}
impl<T: TextureDataType + PrimitiveDataType> TextureDataType for Vector3<T> {}
impl<T: TextureDataType + PrimitiveDataType> TextureDataType for Vector4<T> {}
impl<T: TextureDataType + PrimitiveDataType> TextureDataType for [T; 2] {}
impl<T: TextureDataType + PrimitiveDataType> TextureDataType for [T; 3] {}
impl<T: TextureDataType + PrimitiveDataType> TextureDataType for [T; 4] {}
impl TextureDataType for Color {}
impl TextureDataType for Quat {}
impl<T: TextureDataType + ?Sized> TextureDataType for &T {}
pub trait DepthTextureDataType: DepthDataType {}
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Default, Debug)]
pub struct f24 {}
impl DepthTextureDataType for f16 {}
impl DepthTextureDataType for f24 {}
impl DepthTextureDataType for f32 {}
#[derive(Clone, Copy)]
#[allow(missing_docs)]
pub enum ColorTexture<'a> {
Single(&'a Texture2D),
Array {
texture: &'a Texture2DArray,
layers: &'a [u32],
},
CubeMap {
texture: &'a TextureCubeMap,
sides: &'a [CubeMapSide],
},
}
impl ColorTexture<'_> {
pub fn width(&self) -> u32 {
match self {
ColorTexture::Single(texture) => texture.width(),
ColorTexture::Array { texture, .. } => texture.width(),
ColorTexture::CubeMap { texture, .. } => texture.width(),
}
}
pub fn height(&self) -> u32 {
match self {
ColorTexture::Single(texture) => texture.height(),
ColorTexture::Array { texture, .. } => texture.height(),
ColorTexture::CubeMap { texture, .. } => texture.height(),
}
}
}
#[derive(Clone, Copy)]
#[allow(missing_docs)]
pub enum DepthTexture<'a> {
Single(&'a DepthTexture2D),
Array {
texture: &'a DepthTexture2DArray,
layer: u32,
},
CubeMap {
texture: &'a DepthTextureCubeMap,
side: CubeMapSide,
},
}
impl DepthTexture<'_> {
pub fn width(&self) -> u32 {
match self {
DepthTexture::Single(texture) => texture.width(),
DepthTexture::Array { texture, .. } => texture.width(),
DepthTexture::CubeMap { texture, .. } => texture.width(),
}
}
pub fn height(&self) -> u32 {
match self {
DepthTexture::Single(texture) => texture.height(),
DepthTexture::Array { texture, .. } => texture.height(),
DepthTexture::CubeMap { texture, .. } => texture.height(),
}
}
}
use crate::core::*;
fn generate(context: &Context) -> crate::context::Texture {
unsafe { context.create_texture().expect("Failed creating texture") }
}
fn set_parameters(
context: &Context,
target: u32,
min_filter: Interpolation,
mag_filter: Interpolation,
mip_map_filter: Option<Interpolation>,
wrap_s: Wrapping,
wrap_t: Wrapping,
wrap_r: Option<Wrapping>,
) {
unsafe {
match mip_map_filter {
None => context.tex_parameter_i32(
target,
crate::context::TEXTURE_MIN_FILTER,
interpolation_from(min_filter),
),
Some(Interpolation::Nearest) => {
if min_filter == Interpolation::Nearest {
context.tex_parameter_i32(
target,
crate::context::TEXTURE_MIN_FILTER,
crate::context::NEAREST_MIPMAP_NEAREST as i32,
);
} else {
context.tex_parameter_i32(
target,
crate::context::TEXTURE_MIN_FILTER,
crate::context::LINEAR_MIPMAP_NEAREST as i32,
)
}
}
Some(Interpolation::Linear) => {
if min_filter == Interpolation::Nearest {
context.tex_parameter_i32(
target,
crate::context::TEXTURE_MIN_FILTER,
crate::context::NEAREST_MIPMAP_LINEAR as i32,
);
} else {
context.tex_parameter_i32(
target,
crate::context::TEXTURE_MIN_FILTER,
crate::context::LINEAR_MIPMAP_LINEAR as i32,
)
}
}
}
context.tex_parameter_i32(
target,
crate::context::TEXTURE_MAG_FILTER,
interpolation_from(mag_filter),
);
context.tex_parameter_i32(
target,
crate::context::TEXTURE_WRAP_S,
wrapping_from(wrap_s),
);
context.tex_parameter_i32(
target,
crate::context::TEXTURE_WRAP_T,
wrapping_from(wrap_t),
);
if let Some(r) = wrap_r {
context.tex_parameter_i32(target, crate::context::TEXTURE_WRAP_R, wrapping_from(r));
}
}
}
fn calculate_number_of_mip_maps(
mip_map_filter: Option<Interpolation>,
width: u32,
height: u32,
depth: Option<u32>,
) -> u32 {
if mip_map_filter.is_some()
&& width == height
&& depth.map(|d| d == width).unwrap_or(true)
&& width.is_power_of_two()
{
(width as f64).log2() as u32 + 1
} else {
1
}
}
fn wrapping_from(wrapping: Wrapping) -> i32 {
(match wrapping {
Wrapping::Repeat => crate::context::REPEAT,
Wrapping::MirroredRepeat => crate::context::MIRRORED_REPEAT,
Wrapping::ClampToEdge => crate::context::CLAMP_TO_EDGE,
}) as i32
}
fn interpolation_from(interpolation: Interpolation) -> i32 {
(match interpolation {
Interpolation::Nearest => crate::context::NEAREST,
Interpolation::Linear => crate::context::LINEAR,
}) as i32
}
fn check_data_length<T: TextureDataType>(
width: u32,
height: u32,
depth: u32,
data_byte_size: usize,
data_len: usize,
) {
let expected_bytes = width as usize * height as usize * depth as usize * data_byte_size;
let actual_bytes = data_len * std::mem::size_of::<T>();
if expected_bytes != actual_bytes {
panic!(
"invalid size of texture data (got {} bytes but expected {} bytes)",
expected_bytes, actual_bytes
)
}
}
fn ru8_data(t: &CpuTexture) -> &[u8] {
if let TextureData::RU8(data) = &t.data {
data
} else {
panic!("all of the images used for cube map sides must have the same texture data type")
}
}
fn rgu8_data(t: &CpuTexture) -> &[[u8; 2]] {
if let TextureData::RgU8(data) = &t.data {
data
} else {
panic!("all of the images used for cube map sides must have the same texture data type")
}
}
fn rgbu8_data(t: &CpuTexture) -> &[[u8; 3]] {
if let TextureData::RgbU8(data) = &t.data {
data
} else {
panic!("all of the images used for cube map sides must have the same texture data type")
}
}
fn rgbau8_data(t: &CpuTexture) -> &[[u8; 4]] {
if let TextureData::RgbaU8(data) = &t.data {
data
} else {
panic!("all of the images used for cube map sides must have the same texture data type")
}
}
fn rf16_data(t: &CpuTexture) -> &[f16] {
if let TextureData::RF16(data) = &t.data {
data
} else {
panic!("all of the images used for cube map sides must have the same texture data type")
}
}
fn rgf16_data(t: &CpuTexture) -> &[[f16; 2]] {
if let TextureData::RgF16(data) = &t.data {
data
} else {
panic!("all of the images used for cube map sides must have the same texture data type")
}
}
fn rgbf16_data(t: &CpuTexture) -> &[[f16; 3]] {
if let TextureData::RgbF16(data) = &t.data {
data
} else {
panic!("all of the images used for cube map sides must have the same texture data type")
}
}
fn rgbaf16_data(t: &CpuTexture) -> &[[f16; 4]] {
if let TextureData::RgbaF16(data) = &t.data {
data
} else {
panic!("all of the images used for cube map sides must have the same texture data type")
}
}
fn rf32_data(t: &CpuTexture) -> &[f32] {
if let TextureData::RF32(data) = &t.data {
data
} else {
panic!("all of the images used for cube map sides must have the same texture data type")
}
}
fn rgf32_data(t: &CpuTexture) -> &[[f32; 2]] {
if let TextureData::RgF32(data) = &t.data {
data
} else {
panic!("all of the images used for cube map sides must have the same texture data type")
}
}
fn rgbf32_data(t: &CpuTexture) -> &[[f32; 3]] {
if let TextureData::RgbF32(data) = &t.data {
data
} else {
panic!("all of the images used for cube map sides must have the same texture data type")
}
}
fn rgbaf32_data(t: &CpuTexture) -> &[[f32; 4]] {
if let TextureData::RgbaF32(data) = &t.data {
data
} else {
panic!("all of the images used for cube map sides must have the same texture data type")
}
}