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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//! Typed GPU resource wrappers.
//!
//! This module provides lightweight wrappers around wgpu types that add
//! type safety and metadata tracking without fully encapsulating wgpu.
//!
//! # Types
//!
//! - [`TypedBuffer`]: A buffer that tracks its element type, length, and usage
//! - [`GpuTexture`]: A texture with cached view and metadata
//! - [`StorageTexture`]: A texture for compute shader read/write access
//!
//! # Example
//!
//! ```ignore
//! use astrelis_render::{GraphicsContext, TypedBuffer};
//!
//! let ctx = GraphicsContext::new_owned_sync().expect("Failed to create graphics context");
//!
//! // Create a typed buffer of f32 values
//! let data = [1.0f32, 2.0, 3.0, 4.0];
//! let buffer = TypedBuffer::new(
//!     ctx.device(),
//!     Some("My Buffer"),
//!     &data,
//!     wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
//! );
//!
//! // Later, update the buffer
//! buffer.write(ctx.queue(), &[5.0, 6.0, 7.0, 8.0]);
//! ```

use std::marker::PhantomData;

use crate::extension::{AsWgpu, IntoWgpu};

// =============================================================================
// TypedBuffer
// =============================================================================

/// A GPU buffer with type-safe element tracking.
///
/// This wrapper adds:
/// - Type safety via generics
/// - Automatic length tracking
/// - Convenient write operations
///
/// The underlying buffer is directly accessible via the `AsWgpu` trait.
pub struct TypedBuffer<T: bytemuck::Pod> {
    buffer: wgpu::Buffer,
    len: u32,
    usage: wgpu::BufferUsages,
    _marker: PhantomData<T>,
}

impl<T: bytemuck::Pod> TypedBuffer<T> {
    /// Create a new typed buffer with initial data.
    ///
    /// # Arguments
    ///
    /// * `device` - The wgpu device to create the buffer on
    /// * `label` - Optional debug label for the buffer
    /// * `data` - Initial data to populate the buffer with
    /// * `usage` - Buffer usage flags
    pub fn new(
        device: &wgpu::Device,
        label: Option<&str>,
        data: &[T],
        usage: wgpu::BufferUsages,
    ) -> Self {
        use wgpu::util::DeviceExt;

        let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label,
            contents: bytemuck::cast_slice(data),
            usage,
        });

        Self {
            buffer,
            len: data.len() as u32,
            usage,
            _marker: PhantomData,
        }
    }

    /// Create an empty typed buffer with a given capacity.
    ///
    /// # Arguments
    ///
    /// * `device` - The wgpu device to create the buffer on
    /// * `label` - Optional debug label for the buffer
    /// * `capacity` - Number of elements the buffer can hold
    /// * `usage` - Buffer usage flags
    pub fn with_capacity(
        device: &wgpu::Device,
        label: Option<&str>,
        capacity: u32,
        usage: wgpu::BufferUsages,
    ) -> Self {
        let size = (capacity as usize * std::mem::size_of::<T>()) as u64;

        let buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label,
            size,
            usage,
            mapped_at_creation: false,
        });

        Self {
            buffer,
            len: 0,
            usage,
            _marker: PhantomData,
        }
    }

    /// Get the number of elements in the buffer.
    #[inline]
    pub fn len(&self) -> u32 {
        self.len
    }

    /// Check if the buffer is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Get the size of the buffer in bytes.
    #[inline]
    pub fn size(&self) -> u64 {
        self.len as u64 * std::mem::size_of::<T>() as u64
    }

    /// Get the capacity of the buffer in bytes.
    #[inline]
    pub fn capacity_bytes(&self) -> u64 {
        self.buffer.size()
    }

    /// Get the capacity in number of elements.
    #[inline]
    pub fn capacity(&self) -> u32 {
        (self.buffer.size() / std::mem::size_of::<T>() as u64) as u32
    }

    /// Get the buffer usage flags.
    #[inline]
    pub fn usage(&self) -> wgpu::BufferUsages {
        self.usage
    }

    /// Get a slice of the entire buffer.
    #[inline]
    pub fn slice(&self) -> wgpu::BufferSlice<'_> {
        self.buffer.slice(..)
    }

    /// Get a slice of a portion of the buffer.
    #[inline]
    pub fn slice_range(&self, range: std::ops::Range<u32>) -> wgpu::BufferSlice<'_> {
        let start = range.start as u64 * std::mem::size_of::<T>() as u64;
        let end = range.end as u64 * std::mem::size_of::<T>() as u64;
        self.buffer.slice(start..end)
    }

    /// Write data to the buffer.
    ///
    /// The buffer must have been created with `COPY_DST` usage.
    pub fn write(&self, queue: &wgpu::Queue, data: &[T]) {
        queue.write_buffer(&self.buffer, 0, bytemuck::cast_slice(data));
    }

    /// Write data to the buffer at an offset.
    ///
    /// The buffer must have been created with `COPY_DST` usage.
    pub fn write_at(&self, queue: &wgpu::Queue, offset: u32, data: &[T]) {
        let byte_offset = offset as u64 * std::mem::size_of::<T>() as u64;
        queue.write_buffer(&self.buffer, byte_offset, bytemuck::cast_slice(data));
    }

    /// Get the buffer as a binding resource (for bind groups).
    #[inline]
    pub fn as_binding(&self) -> wgpu::BindingResource<'_> {
        self.buffer.as_entire_binding()
    }

    /// Get a reference to the underlying buffer.
    #[inline]
    pub fn buffer(&self) -> &wgpu::Buffer {
        &self.buffer
    }
}

impl<T: bytemuck::Pod> AsWgpu for TypedBuffer<T> {
    type WgpuType = wgpu::Buffer;

    fn as_wgpu(&self) -> &Self::WgpuType {
        &self.buffer
    }
}

impl<T: bytemuck::Pod> IntoWgpu for TypedBuffer<T> {
    type WgpuType = wgpu::Buffer;

    fn into_wgpu(self) -> Self::WgpuType {
        self.buffer
    }
}

// =============================================================================
// GpuTexture
// =============================================================================

/// A GPU texture with cached view and metadata.
///
/// This wrapper provides:
/// - Automatic view creation and caching
/// - Size and format metadata
/// - Convenient accessors
pub struct GpuTexture {
    texture: wgpu::Texture,
    view: wgpu::TextureView,
    size: wgpu::Extent3d,
    format: wgpu::TextureFormat,
    sample_count: u32,
}

impl GpuTexture {
    /// Create a new GPU texture.
    pub fn new(device: &wgpu::Device, descriptor: &wgpu::TextureDescriptor) -> Self {
        let texture = device.create_texture(descriptor);
        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());

        Self {
            texture,
            view,
            size: descriptor.size,
            format: descriptor.format,
            sample_count: descriptor.sample_count,
        }
    }

    /// Create a simple 2D texture.
    pub fn new_2d(
        device: &wgpu::Device,
        label: Option<&str>,
        width: u32,
        height: u32,
        format: wgpu::TextureFormat,
        usage: wgpu::TextureUsages,
    ) -> Self {
        Self::new(
            device,
            &wgpu::TextureDescriptor {
                label,
                size: wgpu::Extent3d {
                    width,
                    height,
                    depth_or_array_layers: 1,
                },
                mip_level_count: 1,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format,
                usage,
                view_formats: &[],
            },
        )
    }

    /// Create a texture from raw data.
    pub fn from_data(
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        label: Option<&str>,
        width: u32,
        height: u32,
        format: wgpu::TextureFormat,
        data: &[u8],
    ) -> Self {
        let texture = Self::new_2d(
            device,
            label,
            width,
            height,
            format,
            wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
        );

        queue.write_texture(
            wgpu::TexelCopyTextureInfo {
                texture: &texture.texture,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            data,
            wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(width * format.block_copy_size(None).unwrap_or(4)),
                rows_per_image: Some(height),
            },
            wgpu::Extent3d {
                width,
                height,
                depth_or_array_layers: 1,
            },
        );

        texture
    }

    /// Get the texture view.
    #[inline]
    pub fn view(&self) -> &wgpu::TextureView {
        &self.view
    }

    /// Get the texture size.
    #[inline]
    pub fn size(&self) -> wgpu::Extent3d {
        self.size
    }

    /// Get the texture width.
    #[inline]
    pub fn width(&self) -> u32 {
        self.size.width
    }

    /// Get the texture height.
    #[inline]
    pub fn height(&self) -> u32 {
        self.size.height
    }

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

    /// Get the sample count.
    #[inline]
    pub fn sample_count(&self) -> u32 {
        self.sample_count
    }

    /// Get the texture as a binding resource.
    #[inline]
    pub fn as_binding(&self) -> wgpu::BindingResource<'_> {
        wgpu::BindingResource::TextureView(&self.view)
    }

    /// Create a custom view with different parameters.
    pub fn create_view(&self, descriptor: &wgpu::TextureViewDescriptor) -> wgpu::TextureView {
        self.texture.create_view(descriptor)
    }
}

impl AsWgpu for GpuTexture {
    type WgpuType = wgpu::Texture;

    fn as_wgpu(&self) -> &Self::WgpuType {
        &self.texture
    }
}

impl IntoWgpu for GpuTexture {
    type WgpuType = wgpu::Texture;

    fn into_wgpu(self) -> Self::WgpuType {
        self.texture
    }
}

// =============================================================================
// StorageTexture
// =============================================================================

/// Access mode for storage textures in compute shaders.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StorageTextureAccess {
    /// Read-only access in shaders.
    ReadOnly,
    /// Write-only access in shaders.
    WriteOnly,
    /// Read-write access in shaders.
    ReadWrite,
}

impl StorageTextureAccess {
    /// Convert to wgpu storage texture access.
    pub fn to_wgpu(self) -> wgpu::StorageTextureAccess {
        match self {
            Self::ReadOnly => wgpu::StorageTextureAccess::ReadOnly,
            Self::WriteOnly => wgpu::StorageTextureAccess::WriteOnly,
            Self::ReadWrite => wgpu::StorageTextureAccess::ReadWrite,
        }
    }
}

/// A texture for compute shader read/write access.
///
/// Storage textures are used when compute shaders need to write
/// to or read from textures directly.
pub struct StorageTexture {
    texture: wgpu::Texture,
    view: wgpu::TextureView,
    size: wgpu::Extent3d,
    format: wgpu::TextureFormat,
    access: StorageTextureAccess,
}

impl StorageTexture {
    /// Create a new storage texture.
    pub fn new(
        device: &wgpu::Device,
        label: Option<&str>,
        width: u32,
        height: u32,
        format: wgpu::TextureFormat,
        access: StorageTextureAccess,
    ) -> Self {
        let size = wgpu::Extent3d {
            width,
            height,
            depth_or_array_layers: 1,
        };

        let texture = device.create_texture(&wgpu::TextureDescriptor {
            label,
            size,
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format,
            usage: wgpu::TextureUsages::STORAGE_BINDING
                | wgpu::TextureUsages::COPY_SRC
                | wgpu::TextureUsages::COPY_DST,
            view_formats: &[],
        });

        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());

        Self {
            texture,
            view,
            size,
            format,
            access,
        }
    }

    /// Get the texture view.
    #[inline]
    pub fn view(&self) -> &wgpu::TextureView {
        &self.view
    }

    /// Get the texture size.
    #[inline]
    pub fn size(&self) -> wgpu::Extent3d {
        self.size
    }

    /// Get the texture width.
    #[inline]
    pub fn width(&self) -> u32 {
        self.size.width
    }

    /// Get the texture height.
    #[inline]
    pub fn height(&self) -> u32 {
        self.size.height
    }

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

    /// Get the access mode.
    #[inline]
    pub fn access(&self) -> StorageTextureAccess {
        self.access
    }

    /// Get the texture as a binding resource.
    #[inline]
    pub fn as_binding(&self) -> wgpu::BindingResource<'_> {
        wgpu::BindingResource::TextureView(&self.view)
    }
}

impl AsWgpu for StorageTexture {
    type WgpuType = wgpu::Texture;

    fn as_wgpu(&self) -> &Self::WgpuType {
        &self.texture
    }
}

impl IntoWgpu for StorageTexture {
    type WgpuType = wgpu::Texture;

    fn into_wgpu(self) -> Self::WgpuType {
        self.texture
    }
}

// =============================================================================
// UniformBuffer (Convenience type)
// =============================================================================

/// A uniform buffer for shader uniforms.
///
/// This is a convenience type for buffers that contain uniform data.
pub type UniformBuffer<T> = TypedBuffer<T>;

impl<T: bytemuck::Pod> TypedBuffer<T> {
    /// Create a new uniform buffer.
    pub fn new_uniform(device: &wgpu::Device, label: Option<&str>, data: &T) -> Self {
        use wgpu::util::DeviceExt;

        let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label,
            contents: bytemuck::bytes_of(data),
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
        });

        Self {
            buffer,
            len: 1,
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            _marker: PhantomData,
        }
    }

    /// Write a single uniform value to the buffer.
    pub fn write_uniform(&self, queue: &wgpu::Queue, data: &T) {
        queue.write_buffer(&self.buffer, 0, bytemuck::bytes_of(data));
    }
}

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

    #[repr(C)]
    #[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
    struct TestData {
        x: f32,
        y: f32,
        z: f32,
        w: f32,
    }

    #[test]
    fn test_typed_buffer_size() {
        // Test that size calculations are correct
        assert_eq!(std::mem::size_of::<TestData>(), 16);
        assert_eq!(std::mem::size_of::<f32>(), 4);
    }

    #[test]
    fn test_storage_texture_access_conversion() {
        assert_eq!(
            StorageTextureAccess::ReadOnly.to_wgpu(),
            wgpu::StorageTextureAccess::ReadOnly
        );
        assert_eq!(
            StorageTextureAccess::WriteOnly.to_wgpu(),
            wgpu::StorageTextureAccess::WriteOnly
        );
        assert_eq!(
            StorageTextureAccess::ReadWrite.to_wgpu(),
            wgpu::StorageTextureAccess::ReadWrite
        );
    }
}