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
use crate::{ImageShaderBindings, Pt};
/// Rectangle bounds for defining sub-regions of images.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Bounds {
/// X coordinate of the top-left corner.
pub x: Pt,
/// Y coordinate of the top-left corner.
pub y: Pt,
/// Width of the bounds.
pub width: Pt,
/// Height of the bounds.
pub height: Pt,
}
/// Rectangle bounds in pure physical GPU pixels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PixelBounds {
pub x: u32,
pub y: u32,
pub width: u32,
pub height: u32,
}
impl Bounds {
/// Returns the width of the bounds.
pub fn width(&self) -> Pt {
self.width
}
/// Returns the height of the bounds.
pub fn height(&self) -> Pt {
self.height
}
/// Returns the X coordinate of the top-left corner.
pub fn x(&self) -> Pt {
self.x
}
/// Returns the Y coordinate of the top-left corner.
pub fn y(&self) -> Pt {
self.y
}
/// Creates new bounds with the specified dimensions.
pub fn new(x: Pt, y: Pt, width: Pt, height: Pt) -> Self {
Self {
x,
y,
width,
height,
}
}
}
/// Handle to an image resource.
///
/// An image references a sub-rectangle of a [`Texture`][crate::Texture].
/// It can be used as a source for drawing into other images, or as a render target itself.
///
/// Use `target.draw(ctx, &source, options)` to draw a source image into a target image.
/// The `screen` image provided in [`Spot::draw`][crate::Spot::draw] is the default window target.
#[derive(Debug, Clone, Copy)]
pub struct Image {
pub(crate) id: u32,
pub(crate) texture_id: u32,
pub(crate) x: Pt,
pub(crate) y: Pt,
pub(crate) width: Pt,
pub(crate) height: Pt,
pub(crate) pixel_bounds: PixelBounds,
}
impl Image {
/// Creates a new texture-backed full image from RGBA8 data.
pub fn new(
ctx: &mut crate::Context,
width: Pt,
height: Pt,
rgba: &[u8],
) -> anyhow::Result<Self> {
let pixel_width = width.0.round() as u32;
let pixel_height = height.0.round() as u32;
Ok(ctx.register_image(pixel_width, pixel_height, width, height, rgba))
}
/// Returns the logical width of the image.
pub fn width(self) -> Pt {
self.width
}
/// Returns the logical height of the image.
pub fn height(self) -> Pt {
self.height
}
/// Returns the internal unique identifier for this image.
pub fn id(self) -> u32 {
self.id
}
/// Returns the owning texture identifier.
pub fn texture_id(self) -> u32 {
self.texture_id
}
/// Returns whether the backing texture has been uploaded and is ready for drawing.
pub fn is_ready(self, ctx: &crate::Context) -> bool {
ctx.registry
.textures
.get(self.texture_id as usize)
.and_then(|v| v.as_ref())
.map(|entry| entry.is_ready(ctx.registry.gpu_generation))
.unwrap_or(false)
}
/// Returns the physical pixel bounds of the image in the texture or atlas.
pub fn pixel_bounds(self) -> PixelBounds {
self.pixel_bounds
}
pub(crate) fn index(self) -> usize {
self.id as usize
}
}
impl PartialEq for Image {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for Image {}
impl std::hash::Hash for Image {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
impl Image {
#[cfg(feature = "utils")]
pub(crate) fn new_from_rgba8_with_pixels(
ctx: &mut crate::Context,
pixel_width: u32,
pixel_height: u32,
width: Pt,
height: Pt,
rgba: &[u8],
) -> anyhow::Result<Self> {
Ok(crate::Texture::new_from_rgba8_with_pixels(
ctx,
pixel_width,
pixel_height,
width,
height,
rgba,
)?
.view())
}
/// Creates a full-view copy of an existing image.
pub fn from_image(ctx: &mut crate::Context, image: Image) -> anyhow::Result<Self> {
Self::sub_image(
ctx,
image,
Bounds {
x: image.x,
y: image.y,
width: image.width,
height: image.height,
},
)
}
/// Creates a sub-image from a region of an existing image.
pub fn sub_image(
ctx: &mut crate::Context,
image: Image,
bounds: Bounds,
) -> anyhow::Result<Self> {
if bounds.x.0 < 0.0
|| bounds.y.0 < 0.0
|| bounds.x.0 + bounds.width.0 > image.width.0 + 0.001
|| bounds.y.0 + bounds.height.0 > image.height.0 + 0.001
{
anyhow::bail!(
"Sub-image bounds [x:{}, y:{}, w:{}, h:{}] are out of range for parent image [w:{}, h:{}]",
bounds.x.0,
bounds.y.0,
bounds.width.0,
bounds.height.0,
image.width.0,
image.height.0
);
}
let id = ctx.register_sub_image(image, bounds)?;
Ok(Self {
id,
texture_id: image.texture_id,
x: image.x + bounds.x,
y: image.y + bounds.y,
width: bounds.width,
height: bounds.height,
pixel_bounds: ctx.registry.images[id as usize]
.as_ref()
.unwrap()
.pixel_bounds,
})
}
/// Draws a drawable into this image (as a target) with the specified options.
///
/// `options.position()` is interpreted relative to this target image's top-left corner.
/// To draw to the screen, use the `screen` image provided in `Spot::draw`.
pub fn draw<D: crate::Drawable>(
self,
ctx: &mut crate::Context,
drawable: D,
options: D::Options,
) {
drawable.draw_to(ctx, self, options);
}
/// Draws a source image into this target with a custom image shader.
///
/// `options.position()` is interpreted relative to this target's top-left corner.
pub fn draw_with_shader<S: Into<crate::Image>>(
self,
ctx: &mut crate::Context,
source: S,
shader_id: u32,
options: crate::DrawOption,
shader_opts: crate::ShaderOpts,
) {
self.draw_with_shader_bindings(
ctx,
source,
shader_id,
options,
shader_opts,
ImageShaderBindings::default(),
);
}
/// Draws a source image into this target with custom image shader bindings.
pub fn draw_with_shader_bindings<S: Into<crate::Image>>(
self,
ctx: &mut crate::Context,
source: S,
shader_id: u32,
options: crate::DrawOption,
shader_opts: crate::ShaderOpts,
shader_bindings: ImageShaderBindings,
) {
let source = source.into();
let target_texture_id = ctx.resolve_target_texture_id(self);
ctx.push(crate::drawable::DrawCommand::Image(Box::new(
crate::drawable::ImageCommand {
id: source.id,
target_texture_id,
opts: options,
shader_id,
shader_opts,
shader_bindings,
size: [source.width, source.height],
},
)));
}
/// Returns the source-texture bounds of this image.
pub fn bounds(self) -> Bounds {
Bounds {
x: self.x,
y: self.y,
width: self.width,
height: self.height,
}
}
/// Destroys the image.
pub fn destroy(self, ctx: &mut crate::Context) -> bool {
ctx.registry
.images
.get_mut(self.index())
.and_then(|v| v.take())
.is_some()
}
}
#[derive(Debug, Clone)]
pub(crate) struct ImageEntry {
pub(crate) texture_id: u32,
pub(crate) bounds: Bounds,
pub(crate) pixel_bounds: PixelBounds,
pub(crate) visible: bool,
}
impl ImageEntry {
pub(crate) fn new(texture_id: u32, bounds: Bounds, pixel_bounds: PixelBounds) -> Self {
Self {
texture_id,
bounds,
pixel_bounds,
visible: true,
}
}
}
impl crate::Drawable for &Image {
type Options = crate::DrawOption;
fn draw_to(self, ctx: &mut crate::Context, target: crate::Image, options: Self::Options) {
let target_texture_id = ctx.resolve_target_texture_id(target);
ctx.push(crate::drawable::DrawCommand::Image(Box::new(
crate::drawable::ImageCommand {
id: self.id,
target_texture_id,
opts: options,
shader_id: 0,
shader_opts: crate::ShaderOpts::default(),
shader_bindings: ImageShaderBindings::default(),
size: [self.width, self.height],
},
)));
}
}