astrelis-render 0.2.4

Astrelis Core Rendering Module
Documentation
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Texture atlas with non-uniform rectangle packing.
//!
//! Provides efficient texture packing for UI elements, sprites, and other 2D graphics.
//!
//! # Example
//!
//! ```ignore
//! use astrelis_render::{TextureAtlas, GraphicsContext};
//!
//! let context = GraphicsContext::new_owned_sync().expect("Failed to create graphics context");
//! let mut atlas = TextureAtlas::new(context.clone(), 512, wgpu::TextureFormat::Rgba8UnormSrgb);
//!
//! // Insert images
//! let key1 = AtlasKey::new("icon1");
//! if let Some(entry) = atlas.insert(key1, &image_data, Vec2::new(32.0, 32.0)) {
//!     println!("Inserted at UV: {:?}", entry.uv_rect);
//! }
//!
//! // Upload to GPU
//! atlas.upload(&context);
//!
//! // Retrieve UV coordinates
//! if let Some(entry) = atlas.get(&key1) {
//!     // Use entry.uv_rect for rendering
//! }
//! ```

use astrelis_core::profiling::profile_function;

use crate::GraphicsContext;
use crate::extension::AsWgpu;
use crate::types::GpuTexture;
use ahash::HashMap;
use std::sync::Arc;

/// Rectangle for atlas packing (not coordinate-space aware).
///
/// This is a simple rectangle type used internally for texture atlas packing.
/// It does not distinguish between logical/physical coordinates since atlas
/// operations work in texture-local pixel space.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AtlasRect {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

impl AtlasRect {
    /// Create a new atlas rectangle.
    pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }
}

/// Rectangle type for atlas packing (internal alias).
type Rect = AtlasRect;

/// Unique key for an atlas entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AtlasKey(u64);

impl AtlasKey {
    /// Create a new atlas key from a string.
    pub fn new(s: &str) -> Self {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        s.hash(&mut hasher);
        Self(hasher.finish())
    }

    /// Create an atlas key from a u64.
    pub fn from_u64(id: u64) -> Self {
        Self(id)
    }

    /// Get the raw u64 value.
    pub fn as_u64(&self) -> u64 {
        self.0
    }
}

/// An entry in the texture atlas.
#[derive(Debug, Clone, Copy)]
pub struct AtlasEntry {
    /// Rectangle in pixel coordinates within the atlas.
    pub rect: Rect,
    /// Rectangle in normalized UV coordinates (0.0 to 1.0).
    pub uv_rect: Rect,
}

impl AtlasEntry {
    /// Create a new atlas entry.
    pub fn new(rect: Rect, atlas_size: f32) -> Self {
        let uv_rect = Rect {
            x: rect.x / atlas_size,
            y: rect.y / atlas_size,
            width: rect.width / atlas_size,
            height: rect.height / atlas_size,
        };

        Self { rect, uv_rect }
    }
}

/// Rectangle packing algorithm.
#[derive(Debug, Clone)]
#[allow(dead_code)]
enum PackerNode {
    /// Empty node that can be split.
    Empty { rect: Rect },
    /// Filled node with an entry.
    Filled { rect: Rect, key: AtlasKey },
    /// Split node with two children.
    Split {
        rect: Rect,
        left: Box<PackerNode>,
        right: Box<PackerNode>,
    },
}

impl PackerNode {
    /// Create a new empty node.
    fn new(rect: Rect) -> Self {
        Self::Empty { rect }
    }

    /// Try to insert a rectangle into this node.
    fn insert(&mut self, key: AtlasKey, width: f32, height: f32) -> Option<Rect> {
        match self {
            PackerNode::Empty { rect } => {
                // Check if the rectangle fits
                if width > rect.width || height > rect.height {
                    return None;
                }

                // Perfect fit
                if width == rect.width && height == rect.height {
                    let result = *rect;
                    *self = PackerNode::Filled { rect: *rect, key };
                    return Some(result);
                }

                // Split the node
                let rect_copy = *rect;

                // Decide whether to split horizontally or vertically
                let horizontal_waste = rect.width - width;
                let vertical_waste = rect.height - height;

                let (left_rect, right_rect) = if horizontal_waste > vertical_waste {
                    // Split horizontally (left/right)
                    (
                        Rect {
                            x: rect.x,
                            y: rect.y,
                            width,
                            height: rect.height,
                        },
                        Rect {
                            x: rect.x + width,
                            y: rect.y,
                            width: rect.width - width,
                            height: rect.height,
                        },
                    )
                } else {
                    // Split vertically (top/bottom)
                    (
                        Rect {
                            x: rect.x,
                            y: rect.y,
                            width: rect.width,
                            height,
                        },
                        Rect {
                            x: rect.x,
                            y: rect.y + height,
                            width: rect.width,
                            height: rect.height - height,
                        },
                    )
                };

                let mut left = Box::new(PackerNode::new(left_rect));
                let right = Box::new(PackerNode::new(right_rect));

                // Insert into the left node
                let result = left.insert(key, width, height);

                *self = PackerNode::Split {
                    rect: rect_copy,
                    left,
                    right,
                };

                result
            }
            PackerNode::Filled { .. } => None,
            PackerNode::Split { left, right, .. } => {
                // Try left first, then right
                left.insert(key, width, height)
                    .or_else(|| right.insert(key, width, height))
            }
        }
    }
}

/// Texture atlas with dynamic rectangle packing.
pub struct TextureAtlas {
    /// GPU texture with cached view and metadata.
    texture: GpuTexture,
    entries: HashMap<AtlasKey, AtlasEntry>,
    packer: PackerNode,
    context: Arc<GraphicsContext>,
    /// Pending uploads (key, data, rect)
    pending_uploads: Vec<(AtlasKey, Vec<u8>, Rect)>,
    dirty: bool,
}

impl TextureAtlas {
    /// Create a new texture atlas.
    ///
    /// # Arguments
    ///
    /// * `context` - Graphics context
    /// * `size` - Size of the atlas texture (must be power of 2)
    /// * `format` - Texture format
    pub fn new(context: Arc<GraphicsContext>, size: u32, format: wgpu::TextureFormat) -> Self {
        profile_function!();
        let texture = GpuTexture::new_2d(
            context.device(),
            Some("TextureAtlas"),
            size,
            size,
            format,
            wgpu::TextureUsages::TEXTURE_BINDING
                | wgpu::TextureUsages::COPY_DST
                | wgpu::TextureUsages::RENDER_ATTACHMENT,
        );

        let packer = PackerNode::new(Rect {
            x: 0.0,
            y: 0.0,
            width: size as f32,
            height: size as f32,
        });

        Self {
            texture,
            entries: HashMap::default(),
            packer,
            context,
            pending_uploads: Vec::new(),
            dirty: false,
        }
    }

    /// Insert an image into the atlas.
    ///
    /// Returns the atlas entry if the image was successfully inserted.
    /// Returns None if there's no space in the atlas.
    ///
    /// # Arguments
    ///
    /// * `key` - Unique key for this image
    /// * `image_data` - Raw pixel data (must match atlas format)
    /// * `size` - Size of the image in pixels
    pub fn insert(
        &mut self,
        key: AtlasKey,
        image_data: &[u8],
        width: u32,
        height: u32,
    ) -> Option<AtlasEntry> {
        profile_function!();
        // Check if already exists
        if let Some(entry) = self.entries.get(&key) {
            return Some(*entry);
        }

        // Try to pack the rectangle
        let rect = self.packer.insert(key, width as f32, height as f32)?;

        // Create entry
        let entry = AtlasEntry::new(rect, self.texture.width() as f32);
        self.entries.insert(key, entry);

        // Queue upload
        self.pending_uploads.push((key, image_data.to_vec(), rect));
        self.dirty = true;

        Some(entry)
    }

    /// Get an atlas entry by key.
    pub fn get(&self, key: &AtlasKey) -> Option<&AtlasEntry> {
        self.entries.get(key)
    }

    /// Check if the atlas contains a key.
    pub fn contains(&self, key: &AtlasKey) -> bool {
        self.entries.contains_key(key)
    }

    /// Upload all pending data to the GPU.
    pub fn upload(&mut self) {
        profile_function!();
        if !self.dirty {
            return;
        }

        let format = self.texture.format();
        for (_, data, rect) in &self.pending_uploads {
            let bytes_per_pixel = match format {
                wgpu::TextureFormat::Rgba8UnormSrgb | wgpu::TextureFormat::Rgba8Unorm => 4,
                wgpu::TextureFormat::Bgra8UnormSrgb | wgpu::TextureFormat::Bgra8Unorm => 4,
                wgpu::TextureFormat::R8Unorm => 1,
                _ => 4, // Default to 4 bytes
            };

            self.context.queue().write_texture(
                wgpu::TexelCopyTextureInfo {
                    texture: self.texture.as_wgpu(),
                    mip_level: 0,
                    origin: wgpu::Origin3d {
                        x: rect.x as u32,
                        y: rect.y as u32,
                        z: 0,
                    },
                    aspect: wgpu::TextureAspect::All,
                },
                data,
                wgpu::TexelCopyBufferLayout {
                    offset: 0,
                    bytes_per_row: Some(rect.width as u32 * bytes_per_pixel),
                    rows_per_image: Some(rect.height as u32),
                },
                wgpu::Extent3d {
                    width: rect.width as u32,
                    height: rect.height as u32,
                    depth_or_array_layers: 1,
                },
            );
        }

        self.pending_uploads.clear();
        self.dirty = false;
    }

    /// Get the texture view for binding.
    pub fn texture_view(&self) -> &wgpu::TextureView {
        self.texture.view()
    }

    /// Get the texture for advanced use cases.
    pub fn texture(&self) -> &wgpu::Texture {
        self.texture.as_wgpu()
    }

    /// Get the size of the atlas.
    pub fn size(&self) -> u32 {
        self.texture.width()
    }

    /// Get the texture format.
    pub fn format(&self) -> wgpu::TextureFormat {
        self.texture.format()
    }

    /// Get the number of entries in the atlas.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Check if the atlas is empty.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Clear all entries from the atlas.
    pub fn clear(&mut self) {
        self.entries.clear();
        self.pending_uploads.clear();
        let size = self.texture.width();
        self.packer = PackerNode::new(Rect {
            x: 0.0,
            y: 0.0,
            width: size as f32,
            height: size as f32,
        });
        self.dirty = false;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_atlas_key() {
        let key1 = AtlasKey::new("test");
        let key2 = AtlasKey::new("test");
        let key3 = AtlasKey::new("other");

        assert_eq!(key1, key2);
        assert_ne!(key1, key3);
    }

    #[test]
    fn test_atlas_entry_uv() {
        let rect = Rect {
            x: 0.0,
            y: 0.0,
            width: 64.0,
            height: 64.0,
        };
        let entry = AtlasEntry::new(rect, 256.0);

        assert_eq!(entry.uv_rect.x, 0.0);
        assert_eq!(entry.uv_rect.y, 0.0);
        assert_eq!(entry.uv_rect.width, 0.25);
        assert_eq!(entry.uv_rect.height, 0.25);
    }

    #[test]
    fn test_packer_insertion() {
        let mut packer = PackerNode::new(Rect {
            x: 0.0,
            y: 0.0,
            width: 256.0,
            height: 256.0,
        });

        let key1 = AtlasKey::new("rect1");
        let rect1 = packer.insert(key1, 64.0, 64.0);
        assert!(rect1.is_some());

        let key2 = AtlasKey::new("rect2");
        let rect2 = packer.insert(key2, 32.0, 32.0);
        assert!(rect2.is_some());

        // Try to insert something too large
        let key3 = AtlasKey::new("rect3");
        let rect3 = packer.insert(key3, 512.0, 512.0);
        assert!(rect3.is_none());
    }

    #[test]
    fn test_atlas_basic() {
        let context = GraphicsContext::new_owned_sync().expect("Failed to create graphics context");
        let mut atlas = TextureAtlas::new(context, 256, wgpu::TextureFormat::Rgba8UnormSrgb);

        assert_eq!(atlas.size(), 256);
        assert_eq!(atlas.len(), 0);
        assert!(atlas.is_empty());

        // Create a 32x32 red square
        let mut image_data = vec![0u8; 32 * 32 * 4];
        for i in 0..(32 * 32) {
            image_data[i * 4] = 255; // R
            image_data[i * 4 + 1] = 0; // G
            image_data[i * 4 + 2] = 0; // B
            image_data[i * 4 + 3] = 255; // A
        }

        let key = AtlasKey::new("red_square");
        let entry = atlas.insert(key, &image_data, 32, 32);
        assert!(entry.is_some());
        assert_eq!(atlas.len(), 1);

        // Check retrieval
        let retrieved = atlas.get(&key);
        assert!(retrieved.is_some());

        // Upload to GPU
        atlas.upload();
    }

    #[test]
    fn test_atlas_multiple_inserts() {
        let context = GraphicsContext::new_owned_sync().expect("Failed to create graphics context");
        let mut atlas = TextureAtlas::new(context, 256, wgpu::TextureFormat::Rgba8UnormSrgb);

        // Insert multiple images
        for i in 0..10 {
            let image_data = vec![0u8; 16 * 16 * 4];
            let key = AtlasKey::new(&format!("image_{}", i));
            let entry = atlas.insert(key, &image_data, 16, 16);
            assert!(entry.is_some());
        }

        assert_eq!(atlas.len(), 10);
        atlas.upload();
    }

    #[test]
    fn test_atlas_duplicate_key() {
        let context = GraphicsContext::new_owned_sync().expect("Failed to create graphics context");
        let mut atlas = TextureAtlas::new(context, 256, wgpu::TextureFormat::Rgba8UnormSrgb);

        let image_data = vec![0u8; 32 * 32 * 4];
        let key = AtlasKey::new("duplicate");

        let entry1 = atlas.insert(key, &image_data, 32, 32);
        assert!(entry1.is_some());

        let entry2 = atlas.insert(key, &image_data, 32, 32);
        assert!(entry2.is_some());

        // Should only have one entry
        assert_eq!(atlas.len(), 1);
    }

    #[test]
    fn test_atlas_clear() {
        let context = GraphicsContext::new_owned_sync().expect("Failed to create graphics context");
        let mut atlas = TextureAtlas::new(context, 256, wgpu::TextureFormat::Rgba8UnormSrgb);

        let image_data = vec![0u8; 32 * 32 * 4];
        let key = AtlasKey::new("test");
        atlas.insert(key, &image_data, 32, 32);

        assert_eq!(atlas.len(), 1);

        atlas.clear();

        assert_eq!(atlas.len(), 0);
        assert!(atlas.is_empty());
    }
}