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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
use astrelis_core::profiling::profile_function;

use crate::context::GraphicsContext;
use crate::types::{GpuTexture, TypedBuffer, UniformBuffer};
use std::sync::Arc;

/// Low-level extensible renderer that simplifies WGPU resource management.
///
/// This provides a foundation for higher-level renderers like TextRenderer, SceneRenderer, etc.
/// It manages common rendering state and provides utilities for resource creation.
pub struct Renderer {
    context: Arc<GraphicsContext>,
}

impl Renderer {
    /// Create a new renderer with the given graphics context.
    pub fn new(context: Arc<GraphicsContext>) -> Self {
        Self { context }
    }

    /// Get the graphics context.
    pub fn context(&self) -> &GraphicsContext {
        &self.context
    }

    /// Get the device.
    pub fn device(&self) -> &wgpu::Device {
        self.context.device()
    }

    /// Get the queue.
    pub fn queue(&self) -> &wgpu::Queue {
        self.context.queue()
    }

    /// Create a shader module from WGSL source.
    pub fn create_shader(&self, label: Option<&str>, source: &str) -> wgpu::ShaderModule {
        profile_function!();
        self.context
            .device()
            .create_shader_module(wgpu::ShaderModuleDescriptor {
                label,
                source: wgpu::ShaderSource::Wgsl(source.into()),
            })
    }

    /// Create a vertex buffer with data.
    pub fn create_vertex_buffer<T: bytemuck::Pod>(
        &self,
        label: Option<&str>,
        data: &[T],
    ) -> wgpu::Buffer {
        profile_function!();
        let buffer = self
            .context
            .device()
            .create_buffer(&wgpu::BufferDescriptor {
                label,
                size: std::mem::size_of_val(data) as u64,
                usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });

        self.context
            .queue()
            .write_buffer(&buffer, 0, bytemuck::cast_slice(data));

        buffer
    }

    /// Create an index buffer with data.
    pub fn create_index_buffer<T: bytemuck::Pod>(
        &self,
        label: Option<&str>,
        data: &[T],
    ) -> wgpu::Buffer {
        profile_function!();
        let buffer = self
            .context
            .device()
            .create_buffer(&wgpu::BufferDescriptor {
                label,
                size: std::mem::size_of_val(data) as u64,
                usage: wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });

        self.context
            .queue()
            .write_buffer(&buffer, 0, bytemuck::cast_slice(data));

        buffer
    }

    /// Create a uniform buffer with data.
    pub fn create_uniform_buffer<T: bytemuck::Pod>(
        &self,
        label: Option<&str>,
        data: &T,
    ) -> wgpu::Buffer {
        profile_function!();
        let buffer = self
            .context
            .device()
            .create_buffer(&wgpu::BufferDescriptor {
                label,
                size: std::mem::size_of::<T>() as u64,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });

        self.context.queue().write_buffer(
            &buffer,
            0,
            bytemuck::cast_slice(std::slice::from_ref(data)),
        );

        buffer
    }

    /// Update a uniform buffer with new data.
    pub fn update_uniform_buffer<T: bytemuck::Pod>(&self, buffer: &wgpu::Buffer, data: &T) {
        self.context.queue().write_buffer(
            buffer,
            0,
            bytemuck::cast_slice(std::slice::from_ref(data)),
        );
    }

    /// Create an empty storage buffer.
    ///
    /// # Arguments
    ///
    /// * `label` - Optional debug label
    /// * `size` - Size in bytes
    /// * `read_only` - If true, creates a read-only storage buffer (STORAGE),
    ///   otherwise creates a read-write storage buffer (STORAGE | COPY_DST)
    pub fn create_storage_buffer(
        &self,
        label: Option<&str>,
        size: u64,
        read_only: bool,
    ) -> wgpu::Buffer {
        profile_function!();
        let usage = if read_only {
            wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST
        } else {
            wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST
                | wgpu::BufferUsages::COPY_SRC
        };

        self.context
            .device()
            .create_buffer(&wgpu::BufferDescriptor {
                label,
                size,
                usage,
                mapped_at_creation: false,
            })
    }

    /// Create a storage buffer initialized with data.
    ///
    /// # Arguments
    ///
    /// * `label` - Optional debug label
    /// * `data` - Initial data to write to the buffer
    /// * `read_only` - If true, creates a read-only storage buffer,
    ///   otherwise creates a read-write storage buffer
    pub fn create_storage_buffer_init<T: bytemuck::Pod>(
        &self,
        label: Option<&str>,
        data: &[T],
        read_only: bool,
    ) -> wgpu::Buffer {
        let usage = if read_only {
            wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST
        } else {
            wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST
                | wgpu::BufferUsages::COPY_SRC
        };

        let buffer = self
            .context
            .device()
            .create_buffer(&wgpu::BufferDescriptor {
                label,
                size: std::mem::size_of_val(data) as u64,
                usage,
                mapped_at_creation: false,
            });

        self.context
            .queue()
            .write_buffer(&buffer, 0, bytemuck::cast_slice(data));

        buffer
    }

    /// Update a storage buffer with new data at the specified offset.
    ///
    /// # Arguments
    ///
    /// * `buffer` - The buffer to update
    /// * `offset` - Byte offset into the buffer
    /// * `data` - Data to write
    pub fn update_storage_buffer<T: bytemuck::Pod>(
        &self,
        buffer: &wgpu::Buffer,
        offset: u64,
        data: &[T],
    ) {
        self.context
            .queue()
            .write_buffer(buffer, offset, bytemuck::cast_slice(data));
    }

    /// Create a texture with descriptor.
    pub fn create_texture(&self, descriptor: &wgpu::TextureDescriptor) -> wgpu::Texture {
        self.context.device().create_texture(descriptor)
    }

    /// Create a 2D texture with data.
    pub fn create_texture_2d(
        &self,
        label: Option<&str>,
        width: u32,
        height: u32,
        format: wgpu::TextureFormat,
        usage: wgpu::TextureUsages,
        data: &[u8],
    ) -> wgpu::Texture {
        let size = wgpu::Extent3d {
            width,
            height,
            depth_or_array_layers: 1,
        };

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

        let bytes_per_pixel = format.block_copy_size(None).unwrap();

        self.context.queue().write_texture(
            wgpu::TexelCopyTextureInfo {
                texture: &texture,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            data,
            wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(width * bytes_per_pixel),
                rows_per_image: Some(height),
            },
            size,
        );

        texture
    }

    /// Create a sampler with descriptor.
    pub fn create_sampler(&self, descriptor: &wgpu::SamplerDescriptor) -> wgpu::Sampler {
        self.context.device().create_sampler(descriptor)
    }

    /// Create a simple linear sampler.
    pub fn create_linear_sampler(&self, label: Option<&str>) -> wgpu::Sampler {
        self.context
            .device()
            .create_sampler(&wgpu::SamplerDescriptor {
                label,
                address_mode_u: wgpu::AddressMode::ClampToEdge,
                address_mode_v: wgpu::AddressMode::ClampToEdge,
                address_mode_w: wgpu::AddressMode::ClampToEdge,
                mag_filter: wgpu::FilterMode::Linear,
                min_filter: wgpu::FilterMode::Linear,
                mipmap_filter: wgpu::FilterMode::Nearest,
                ..Default::default()
            })
    }

    /// Create a simple nearest sampler.
    pub fn create_nearest_sampler(&self, label: Option<&str>) -> wgpu::Sampler {
        self.context
            .device()
            .create_sampler(&wgpu::SamplerDescriptor {
                label,
                address_mode_u: wgpu::AddressMode::ClampToEdge,
                address_mode_v: wgpu::AddressMode::ClampToEdge,
                address_mode_w: wgpu::AddressMode::ClampToEdge,
                mag_filter: wgpu::FilterMode::Nearest,
                min_filter: wgpu::FilterMode::Nearest,
                mipmap_filter: wgpu::FilterMode::Nearest,
                ..Default::default()
            })
    }

    /// Create a bind group layout.
    pub fn create_bind_group_layout(
        &self,
        label: Option<&str>,
        entries: &[wgpu::BindGroupLayoutEntry],
    ) -> wgpu::BindGroupLayout {
        profile_function!();
        self.context
            .device()
            .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label, entries })
    }

    /// Create a bind group.
    pub fn create_bind_group(
        &self,
        label: Option<&str>,
        layout: &wgpu::BindGroupLayout,
        entries: &[wgpu::BindGroupEntry],
    ) -> wgpu::BindGroup {
        profile_function!();
        self.context
            .device()
            .create_bind_group(&wgpu::BindGroupDescriptor {
                label,
                layout,
                entries,
            })
    }

    /// Create a pipeline layout.
    pub fn create_pipeline_layout(
        &self,
        label: Option<&str>,
        bind_group_layouts: &[&wgpu::BindGroupLayout],
        push_constant_ranges: &[wgpu::PushConstantRange],
    ) -> wgpu::PipelineLayout {
        profile_function!();
        self.context
            .device()
            .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label,
                bind_group_layouts,
                push_constant_ranges,
            })
    }

    /// Create a render pipeline.
    pub fn create_render_pipeline(
        &self,
        descriptor: &wgpu::RenderPipelineDescriptor,
    ) -> wgpu::RenderPipeline {
        profile_function!();
        self.context.device().create_render_pipeline(descriptor)
    }

    /// Create a compute pipeline.
    pub fn create_compute_pipeline(
        &self,
        descriptor: &wgpu::ComputePipelineDescriptor,
    ) -> wgpu::ComputePipeline {
        profile_function!();
        self.context.device().create_compute_pipeline(descriptor)
    }

    /// Create a command encoder.
    pub fn create_command_encoder(&self, label: Option<&str>) -> wgpu::CommandEncoder {
        self.context
            .device()
            .create_command_encoder(&wgpu::CommandEncoderDescriptor { label })
    }

    /// Submit command buffers to the queue.
    pub fn submit<I>(&self, command_buffers: I)
    where
        I: IntoIterator<Item = wgpu::CommandBuffer>,
    {
        self.context.queue().submit(command_buffers);
    }

    // =========================================================================
    // Typed Buffer Methods
    // =========================================================================

    /// Create a typed vertex buffer with data.
    ///
    /// Returns a `TypedBuffer<T>` that tracks element count and provides type-safe operations.
    pub fn create_typed_vertex_buffer<T: bytemuck::Pod>(
        &self,
        label: Option<&str>,
        data: &[T],
    ) -> TypedBuffer<T> {
        TypedBuffer::new(
            self.context.device(),
            label,
            data,
            wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
        )
    }

    /// Create a typed index buffer with data.
    ///
    /// Returns a `TypedBuffer<T>` that tracks element count and provides type-safe operations.
    pub fn create_typed_index_buffer<T: bytemuck::Pod>(
        &self,
        label: Option<&str>,
        data: &[T],
    ) -> TypedBuffer<T> {
        TypedBuffer::new(
            self.context.device(),
            label,
            data,
            wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST,
        )
    }

    /// Create a typed uniform buffer with data.
    ///
    /// Returns a `UniformBuffer<T>` that provides type-safe uniform operations.
    pub fn create_typed_uniform<T: bytemuck::Pod>(
        &self,
        label: Option<&str>,
        data: &T,
    ) -> UniformBuffer<T> {
        UniformBuffer::new_uniform(self.context.device(), label, data)
    }

    /// Create a GPU texture with cached view and metadata.
    ///
    /// Returns a `GpuTexture` that provides convenient access to the texture, view, and metadata.
    pub fn create_gpu_texture_2d(
        &self,
        label: Option<&str>,
        width: u32,
        height: u32,
        format: wgpu::TextureFormat,
        usage: wgpu::TextureUsages,
    ) -> GpuTexture {
        GpuTexture::new_2d(self.context.device(), label, width, height, format, usage)
    }

    /// Create a GPU texture from raw data.
    ///
    /// Returns a `GpuTexture` with data uploaded to the GPU.
    pub fn create_gpu_texture_from_data(
        &self,
        label: Option<&str>,
        width: u32,
        height: u32,
        format: wgpu::TextureFormat,
        data: &[u8],
    ) -> GpuTexture {
        profile_function!();
        GpuTexture::from_data(
            self.context.device(),
            self.context.queue(),
            label,
            width,
            height,
            format,
            data,
        )
    }
}

/// Builder for creating a render pipeline with sensible defaults.
///
/// # Example
///
/// ```ignore
/// let pipeline = RenderPipelineBuilder::new(&renderer)
///     .label("My Pipeline")
///     .shader(&shader)
///     .layout(&layout)
///     .vertex_buffer(vertex_layout)
///     .color_target(wgpu::ColorTargetState {
///         format: surface_format,
///         blend: Some(wgpu::BlendState::REPLACE),
///         write_mask: wgpu::ColorWrites::ALL,
///     })
///     .build();
/// ```
pub struct RenderPipelineBuilder<'a> {
    renderer: &'a Renderer,
    label: Option<&'a str>,
    shader: Option<&'a wgpu::ShaderModule>,
    vertex_entry: &'a str,
    fragment_entry: &'a str,
    layout: Option<&'a wgpu::PipelineLayout>,
    vertex_buffers: Vec<wgpu::VertexBufferLayout<'a>>,
    color_targets: Vec<Option<wgpu::ColorTargetState>>,
    primitive: wgpu::PrimitiveState,
    depth_stencil: Option<wgpu::DepthStencilState>,
    multisample: wgpu::MultisampleState,
}

impl<'a> RenderPipelineBuilder<'a> {
    /// Create a new builder with default primitive, depth, and multisample state.
    pub fn new(renderer: &'a Renderer) -> Self {
        Self {
            renderer,
            label: None,
            shader: None,
            vertex_entry: "vs_main",
            fragment_entry: "fs_main",
            layout: None,
            vertex_buffers: Vec::new(),
            color_targets: Vec::new(),
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                strip_index_format: None,
                front_face: wgpu::FrontFace::Ccw,
                cull_mode: Some(wgpu::Face::Back),
                polygon_mode: wgpu::PolygonMode::Fill,
                unclipped_depth: false,
                conservative: false,
            },
            depth_stencil: None,
            multisample: wgpu::MultisampleState {
                count: 1,
                mask: !0,
                alpha_to_coverage_enabled: false,
            },
        }
    }

    /// Set a debug label for the pipeline.
    pub fn label(mut self, label: &'a str) -> Self {
        self.label = Some(label);
        self
    }

    /// Set the shader module (required).
    pub fn shader(mut self, shader: &'a wgpu::ShaderModule) -> Self {
        self.shader = Some(shader);
        self
    }

    /// Set the vertex shader entry point. Defaults to `"vs_main"`.
    pub fn vertex_entry(mut self, entry: &'a str) -> Self {
        self.vertex_entry = entry;
        self
    }

    /// Set the fragment shader entry point. Defaults to `"fs_main"`.
    pub fn fragment_entry(mut self, entry: &'a str) -> Self {
        self.fragment_entry = entry;
        self
    }

    /// Set the pipeline layout (required).
    pub fn layout(mut self, layout: &'a wgpu::PipelineLayout) -> Self {
        self.layout = Some(layout);
        self
    }

    /// Add a vertex buffer layout. Can be called multiple times for multiple slots.
    pub fn vertex_buffer(mut self, layout: wgpu::VertexBufferLayout<'a>) -> Self {
        self.vertex_buffers.push(layout);
        self
    }

    /// Add a color target state. Can be called multiple times for MRT.
    pub fn color_target(mut self, target: wgpu::ColorTargetState) -> Self {
        self.color_targets.push(Some(target));
        self
    }

    /// Override the primitive state (topology, cull mode, etc.).
    pub fn primitive(mut self, primitive: wgpu::PrimitiveState) -> Self {
        self.primitive = primitive;
        self
    }

    /// Set the depth/stencil state. Disabled by default.
    pub fn depth_stencil(mut self, depth_stencil: wgpu::DepthStencilState) -> Self {
        self.depth_stencil = Some(depth_stencil);
        self
    }

    /// Override the multisample state. Defaults to 1 sample, no alpha-to-coverage.
    pub fn multisample(mut self, multisample: wgpu::MultisampleState) -> Self {
        self.multisample = multisample;
        self
    }

    /// Build the render pipeline.
    ///
    /// # Panics
    ///
    /// Panics if `shader` or `layout` has not been set.
    pub fn build(self) -> wgpu::RenderPipeline {
        let shader = self.shader.expect("Shader module is required");
        let layout = self.layout.expect("Pipeline layout is required");

        self.renderer
            .create_render_pipeline(&wgpu::RenderPipelineDescriptor {
                label: self.label,
                layout: Some(layout),
                vertex: wgpu::VertexState {
                    module: shader,
                    entry_point: Some(self.vertex_entry),
                    buffers: &self.vertex_buffers,
                    compilation_options: wgpu::PipelineCompilationOptions::default(),
                },
                fragment: Some(wgpu::FragmentState {
                    module: shader,
                    entry_point: Some(self.fragment_entry),
                    targets: &self.color_targets,
                    compilation_options: wgpu::PipelineCompilationOptions::default(),
                }),
                primitive: self.primitive,
                depth_stencil: self.depth_stencil,
                multisample: self.multisample,
                multiview: None,
                cache: None,
            })
    }
}

/// Builder for creating a compute pipeline with common defaults.
///
/// # Example
///
/// ```ignore
/// let pipeline = ComputePipelineBuilder::new(&renderer)
///     .label("My Compute Pipeline")
///     .shader(&shader)
///     .entry("main")
///     .layout(&layout)
///     .build();
/// ```
pub struct ComputePipelineBuilder<'a> {
    renderer: &'a Renderer,
    label: Option<&'a str>,
    shader: Option<&'a wgpu::ShaderModule>,
    entry: &'a str,
    layout: Option<&'a wgpu::PipelineLayout>,
}

impl<'a> ComputePipelineBuilder<'a> {
    /// Create a new compute pipeline builder.
    pub fn new(renderer: &'a Renderer) -> Self {
        Self {
            renderer,
            label: None,
            shader: None,
            entry: "main",
            layout: None,
        }
    }

    /// Set a debug label for the pipeline.
    pub fn label(mut self, label: &'a str) -> Self {
        self.label = Some(label);
        self
    }

    /// Set the shader module.
    pub fn shader(mut self, shader: &'a wgpu::ShaderModule) -> Self {
        self.shader = Some(shader);
        self
    }

    /// Set the entry point function name.
    ///
    /// Defaults to "main".
    pub fn entry(mut self, entry: &'a str) -> Self {
        self.entry = entry;
        self
    }

    /// Set the pipeline layout.
    pub fn layout(mut self, layout: &'a wgpu::PipelineLayout) -> Self {
        self.layout = Some(layout);
        self
    }

    /// Build the compute pipeline.
    ///
    /// # Panics
    ///
    /// Panics if shader or layout is not set.
    pub fn build(self) -> wgpu::ComputePipeline {
        let shader = self.shader.expect("Shader module is required");
        let layout = self.layout.expect("Pipeline layout is required");

        self.renderer
            .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: self.label,
                layout: Some(layout),
                module: shader,
                entry_point: Some(self.entry),
                compilation_options: wgpu::PipelineCompilationOptions::default(),
                cache: None,
            })
    }
}