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
//! [`TextureArray`]: several images of the same size as one texture, for
//! 3D materials that pick between many (terrain layers, foliage variants).
use crate::{Ctx, assets};
/// What a [`TextureArray`]'s pixels hold.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u32)]
pub enum TextureKind {
/// Colors, stored as sRGB (as images are painted); shaders read them as
/// linear light.
#[default]
Color = 0,
/// Numbers, read as stored: normal maps, masks, blend weights.
Data = 1,
}
/// Several images of the same size in one texture, for materials that use
/// many: terrain layers, foliage variants. Made with
/// [`Ctx::create_texture_array`] or [`Ctx::load_texture_array`]; a cheap
/// handle. Custom shaders read one through
/// [`MaterialDesc::custom_arrays`](crate::MaterialDesc::custom_arrays), and
/// it gets mipmaps.
///
/// ```no_run
/// # use ponko::*;
/// # fn f(ctx: &mut Ctx) {
/// let ground = ctx.load_texture_array(&["grass.png", "dirt.png", "rock.png"], TextureKind::Color);
/// assert_eq!(ground.layers(), 3);
/// # }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(C)] // shared with C/C++ (`CrabbyTextureArray`)
pub struct TextureArray {
pub(crate) id: u32,
pub(crate) width: u32,
pub(crate) height: u32,
pub(crate) layers: u32,
}
impl TextureArray {
/// Width of each image, in pixels.
///
/// ```
/// # use ponko::*;
/// # fn f(a: TextureArray) { let _ = a.width(); }
/// ```
#[must_use]
pub fn width(&self) -> u32 {
self.width
}
/// Height of each image, in pixels.
///
/// ```
/// # use ponko::*;
/// # fn f(a: TextureArray) { let _ = a.height(); }
/// ```
#[must_use]
pub fn height(&self) -> u32 {
self.height
}
/// How many images it holds.
///
/// ```
/// # use ponko::*;
/// # fn f(a: TextureArray) { let _ = a.layers(); }
/// ```
#[must_use]
pub fn layers(&self) -> u32 {
self.layers
}
}
impl Ctx {
/// Makes a [`TextureArray`] from images of `width` × `height` RGBA
/// pixels (4 bytes each), one per layer.
///
/// ```
/// # use ponko::*;
/// # fn f(ctx: &mut Ctx) {
/// let red = [255, 0, 0, 255].repeat(4);
/// let blue = [0, 0, 255, 255].repeat(4);
/// let tints = ctx.create_texture_array(2, 2, &[&red, &blue], TextureKind::Color);
/// assert_eq!(tints.layers(), 2);
/// # }
/// ```
///
/// # Panics
///
/// If there are no layers, the size is zero or larger than the GPU
/// takes, or a layer isn't `width * height * 4` bytes.
pub fn create_texture_array(
&mut self,
width: u32,
height: u32,
layers: &[&[u8]],
kind: TextureKind,
) -> TextureArray {
self.try_create_texture_array(width, height, layers, kind)
.unwrap_or_else(|e| panic!("ponko: {e}"))
}
/// [`Ctx::create_texture_array`] that returns errors (for the C API).
#[doc(hidden)]
pub fn try_create_texture_array(
&mut self,
width: u32,
height: u32,
layers: &[&[u8]],
kind: TextureKind,
) -> Result<TextureArray, String> {
if layers.is_empty() {
return Err("a texture array needs at least one layer".into());
}
if width == 0 || height == 0 {
return Err(format!("texture size must not be zero (got {width} x {height})"));
}
let expected = width as usize * height as usize * 4;
if let Some((i, layer)) = layers.iter().enumerate().find(|(_, l)| l.len() != expected) {
return Err(format!(
"layer {i} of a {width} x {height} texture array needs {expected} bytes of RGBA, got {}",
layer.len()
));
}
let id = self.assets3d.arrays.len() as u32;
if let Some(renderer) = self.renderer.as_mut()
&& !self.headless
{
let (max, max_layers) = renderer.max_texture_array_size();
if width > max || height > max || layers.len() as u32 > max_layers {
return Err(format!(
"a {width} x {height} texture array of {} layers is larger than this GPU takes \
({max} x {max}, {max_layers} layers)",
layers.len()
));
}
let gpu_id = renderer.create_texture_array(width, height, layers, kind == TextureKind::Data);
debug_assert_eq!(gpu_id, id);
}
let array = TextureArray {
id,
width,
height,
layers: layers.len() as u32,
};
self.assets3d.arrays.push(array);
self.assets3d.array_data.push(kind == TextureKind::Data);
Ok(array)
}
/// Loads images from `assets/` (PNG, JPEG, ...) into a
/// [`TextureArray`], one layer each, in order. They must all be the
/// same size. Each call makes a new array.
///
/// ```no_run
/// # use ponko::*;
/// # fn f(ctx: &mut Ctx) {
/// let normals = ctx.load_texture_array(&["grass_n.png", "rock_n.png"], TextureKind::Data);
/// # }
/// ```
///
/// # Panics
///
/// With a clear message if a file is missing or not an image, or the
/// sizes differ.
pub fn load_texture_array(&mut self, paths: &[&str], kind: TextureKind) -> TextureArray {
self.try_load_texture_array(paths, kind)
.unwrap_or_else(|e| panic!("ponko: {e}"))
}
/// [`Ctx::load_texture_array`] that returns errors (for the C API).
#[doc(hidden)]
pub fn try_load_texture_array(&mut self, paths: &[&str], kind: TextureKind) -> Result<TextureArray, String> {
let mut images = Vec::with_capacity(paths.len());
for path in paths {
let file = assets::resolve(path)?;
images.push(assets::load_image_at(&file, path)?);
}
let Some(first) = images.first() else {
return Err("load_texture_array: no files given".into());
};
let (width, height) = (first.width, first.height);
if let Some((i, other)) = images
.iter()
.enumerate()
.find(|(_, im)| (im.width, im.height) != (width, height))
{
return Err(format!(
"load_texture_array: {} is {} x {}, but {} is {width} x {height}; every layer must be the same size",
paths[i], other.width, other.height, paths[0]
));
}
let layers: Vec<&[u8]> = images.iter().map(|im| im.rgba.as_slice()).collect();
self.try_create_texture_array(width, height, &layers, kind)
}
/// Whether this game made `array`, as it is (for the C API, which takes
/// ids and sizes).
#[doc(hidden)]
#[must_use]
pub fn has_texture_array(&self, array: TextureArray) -> bool {
self.assets3d.arrays.get(array.id as usize) == Some(&array)
}
/// Sets the texture array [`Decal3D`](crate::Decal3D)s take their images
/// from (`None`: flat-colored decals). Every decal shares it, so a
/// whole game's decals are drawn with fixed bindings: put the images in
/// its layers (or in parts of layers, with `uv_min`/`uv_max`), color
/// images and normal maps together.
///
/// Make the array with [`TextureKind::Data`]: color images are decoded
/// from sRGB when a decal draws them, and normal maps are read as
/// stored, which one array can only do with its pixels left as they are.
/// Leave a few pixels of padding around atlas entries, as mipmaps blend
/// them.
///
/// ```no_run
/// # use ponko::*;
/// # fn f(ctx: &mut Ctx) {
/// let images = ctx.load_texture_array(&["blood.png", "tyre.png", "tyre_n.png"], TextureKind::Data);
/// ctx.set_decal_textures(Some(images));
/// # }
/// ```
///
/// # Panics
///
/// If the array wasn't made by this context or isn't `TextureKind::Data`.
pub fn set_decal_textures(&mut self, array: Option<TextureArray>) {
self.try_set_decal_textures(array)
.unwrap_or_else(|e| panic!("ponko: {e}"))
}
/// [`Ctx::set_decal_textures`] that returns errors (for the C API).
#[doc(hidden)]
pub fn try_set_decal_textures(&mut self, array: Option<TextureArray>) -> Result<(), String> {
if let Some(a) = array {
if !self.has_texture_array(a) {
return Err("decal textures: that texture array wasn't made by this context".into());
}
if !self.assets3d.array_data.get(a.id as usize).copied().unwrap_or(false) {
return Err(
"decal textures: make the array with TextureKind::Data (decals decode colors themselves)".into(),
);
}
}
self.assets3d.decal_array = array;
Ok(())
}
/// The texture array decals draw their images from, if one is set.
///
/// ```
/// # use ponko::*;
/// # fn f(ctx: &Ctx) { assert!(ctx.decal_textures().is_none()); }
/// ```
#[must_use]
pub fn decal_textures(&self) -> Option<TextureArray> {
self.assets3d.decal_array
}
/// Whether this GPU draws projected decals: it needs one more sampled
/// texture in the mesh shader than WebGL2 (and GPUs at the default
/// limit) have. Where it's false, [`Decal3D`](crate::Decal3D)s are
/// skipped and everything else draws as usual. False in headless runs.
///
/// ```
/// # use ponko::*;
/// # fn f(ctx: &Ctx) { let _ = ctx.supports_decals(); }
/// ```
#[must_use]
pub fn supports_decals(&self) -> bool {
self.renderer.as_ref().is_some_and(|r| r.supports_decals())
}
}