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
//! Batched renderer example demonstrating the unified instance rendering system.
//!
//! Renders various shapes using the `BatchRenderer2D`:
//! - Solid colored quads at different depths
//! - Rounded rectangles with SDF corners
//! - Border-only outlines
//! - Semi-transparent overlapping quads
//! - Animated position and color changes
//!
//! Run with: `cargo run -p astrelis-render --example batched_renderer`
//!
//! Select render tier with `--tier`:
//!   `cargo run -p astrelis-render --example batched_renderer -- --tier auto`
//!   `cargo run -p astrelis-render --example batched_renderer -- --tier 1`      (or `direct`)
//!   `cargo run -p astrelis-render --example batched_renderer -- --tier 2`      (or `indirect`)
//!   `cargo run -p astrelis-render --example batched_renderer -- --tier 3`      (or `bindless`)

use std::collections::HashMap;
use std::sync::Arc;

use astrelis_core::logging;
use astrelis_render::batched::{
    BatchRenderer2D, BestBatchCapability2D, BindlessBatchCapability2D, DirectBatchCapability2D,
    DrawBatch2D, DrawType2D, IndirectBatchCapability2D, RenderTier, UnifiedInstance2D,
    create_batch_renderer_2d,
};
use astrelis_render::{
    Color, GraphicsContext, GraphicsContextDescriptor, RenderWindow, RenderWindowBuilder,
};
use astrelis_winit::WindowId;
use astrelis_winit::app::run_app;
use astrelis_winit::window::{WindowDescriptor, WinitPhysicalSize};

const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;

struct App {
    context: Arc<GraphicsContext>,
    windows: HashMap<WindowId, RenderWindow>,
    renderer: Box<dyn BatchRenderer2D>,
    depth_texture: wgpu::Texture,
    depth_view: wgpu::TextureView,
    depth_width: u32,
    depth_height: u32,
    frame_count: u64,
}

impl App {
    fn ensure_depth_buffer(&mut self, width: u32, height: u32) {
        if self.depth_width == width && self.depth_height == height {
            return;
        }
        let w = width.max(1);
        let h = height.max(1);
        let texture = self
            .context
            .device()
            .create_texture(&wgpu::TextureDescriptor {
                label: Some("example_depth"),
                size: wgpu::Extent3d {
                    width: w,
                    height: h,
                    depth_or_array_layers: 1,
                },
                mip_level_count: 1,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format: DEPTH_FORMAT,
                usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
                view_formats: &[],
            });
        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
        self.depth_texture = texture;
        self.depth_view = view;
        self.depth_width = w;
        self.depth_height = h;
    }

    /// Build an orthographic projection matrix for the given viewport size.
    /// Maps (0,0) at top-left to (width, height) at bottom-right.
    /// Z range: 0.0 (far) to 1.0 (near), matching GreaterEqual depth compare.
    fn ortho_projection(width: f32, height: f32) -> [[f32; 4]; 4] {
        [
            [2.0 / width, 0.0, 0.0, 0.0],
            [0.0, -2.0 / height, 0.0, 0.0],
            [0.0, 0.0, 1.0, 0.0],
            [-1.0, 1.0, 0.0, 1.0],
        ]
    }

    /// Generate the demo instances for the current frame.
    fn build_instances(&self, width: f32, height: f32) -> Vec<UnifiedInstance2D> {
        let t = self.frame_count as f32 / 60.0;
        let mut instances = Vec::new();

        // --- Background panel (gray, full viewport, furthest back) ---
        instances.push(UnifiedInstance2D {
            position: [10.0, 10.0],
            size: [width - 20.0, height - 20.0],
            color: [0.15, 0.15, 0.18, 1.0],
            border_radius: 12.0,
            z_depth: 0.01,
            draw_type: DrawType2D::Quad as u32,
            ..Default::default()
        });

        // --- Grid of colored quads ---
        let cols = 5;
        let rows = 3;
        let margin = 30.0;
        let gap = 10.0;
        let cell_w = (width - 2.0 * margin - (cols as f32 - 1.0) * gap) / cols as f32;
        let cell_h = (height * 0.5 - margin - (rows as f32 - 1.0) * gap) / rows as f32;

        for row in 0..rows {
            for col in 0..cols {
                let x = margin + col as f32 * (cell_w + gap);
                let y = margin + row as f32 * (cell_h + gap);
                let idx = row * cols + col;

                // Hue-shift color based on grid position
                let hue = (idx as f32 / (rows * cols) as f32) * 360.0;
                let (r, g, b) = hsl_to_rgb(hue, 0.7, 0.55);

                instances.push(UnifiedInstance2D {
                    position: [x, y],
                    size: [cell_w, cell_h],
                    color: [r, g, b, 1.0],
                    border_radius: 6.0,
                    z_depth: 0.1 + idx as f32 * 0.001,
                    draw_type: DrawType2D::Quad as u32,
                    ..Default::default()
                });
            }
        }

        // --- Animated floating rounded rect ---
        let float_x = width * 0.5 + (t * 0.8).sin() * width * 0.25 - 60.0;
        let float_y = height * 0.35 + (t * 1.2).cos() * 30.0;
        instances.push(UnifiedInstance2D {
            position: [float_x, float_y],
            size: [120.0, 50.0],
            color: [1.0, 0.85, 0.2, 0.9],
            border_radius: 25.0,
            z_depth: 0.8,
            draw_type: DrawType2D::Quad as u32,
            ..Default::default()
        });

        // --- Border-only outlines (bottom area) ---
        let outline_y = height * 0.6;
        for i in 0..4 {
            let x = margin + i as f32 * 140.0;
            let thickness = 1.0 + i as f32;
            let radius = 4.0 + i as f32 * 8.0;
            instances.push(UnifiedInstance2D {
                position: [x, outline_y],
                size: [120.0, 80.0],
                color: [0.4, 0.8, 1.0, 1.0],
                border_radius: radius,
                border_thickness: thickness,
                z_depth: 0.5,
                draw_type: DrawType2D::Quad as u32,
                ..Default::default()
            });
        }

        // --- Overlapping transparent quads (demonstrating depth + alpha) ---
        let overlap_x = width * 0.5 - 100.0;
        let overlap_y = height * 0.75;
        let colors = [
            [1.0, 0.3, 0.3, 0.6],
            [0.3, 1.0, 0.3, 0.6],
            [0.3, 0.3, 1.0, 0.6],
        ];
        for (i, color) in colors.iter().enumerate() {
            let offset = i as f32 * 40.0;
            instances.push(UnifiedInstance2D {
                position: [overlap_x + offset, overlap_y + offset * 0.5],
                size: [120.0, 80.0],
                color: *color,
                border_radius: 8.0,
                z_depth: 0.6 + i as f32 * 0.05,
                draw_type: DrawType2D::Quad as u32,
                ..Default::default()
            });
        }

        // --- Pulsing circle (via large border_radius) ---
        let pulse = ((t * 2.0).sin() * 0.5 + 0.5) * 0.4 + 0.6;
        let circle_size = 60.0 * pulse;
        instances.push(UnifiedInstance2D {
            position: [width - margin - circle_size, outline_y + 10.0],
            size: [circle_size, circle_size],
            color: [1.0, 0.5, 0.0, 0.95],
            border_radius: circle_size * 0.5,
            z_depth: 0.7,
            draw_type: DrawType2D::Quad as u32,
            ..Default::default()
        });

        // --- Small shader-clipped quad (demonstrating clip rect) ---
        let clip_x = margin;
        let clip_y = height * 0.75;
        instances.push(UnifiedInstance2D {
            position: [clip_x, clip_y],
            size: [200.0, 60.0],
            color: [0.9, 0.2, 0.7, 1.0],
            border_radius: 4.0,
            z_depth: 0.55,
            draw_type: DrawType2D::Quad as u32,
            // Clip to a smaller region to demonstrate shader clipping
            clip_min: [clip_x + 20.0, clip_y + 10.0],
            clip_max: [clip_x + 160.0, clip_y + 50.0],
            ..Default::default()
        });

        instances
    }
}

/// Simple HSL to RGB conversion.
fn hsl_to_rgb(h: f32, s: f32, l: f32) -> (f32, f32, f32) {
    let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
    let h_prime = h / 60.0;
    let x = c * (1.0 - (h_prime % 2.0 - 1.0).abs());
    let (r1, g1, b1) = if h_prime < 1.0 {
        (c, x, 0.0)
    } else if h_prime < 2.0 {
        (x, c, 0.0)
    } else if h_prime < 3.0 {
        (0.0, c, x)
    } else if h_prime < 4.0 {
        (0.0, x, c)
    } else if h_prime < 5.0 {
        (x, 0.0, c)
    } else {
        (c, 0.0, x)
    };
    let m = l - c * 0.5;
    (r1 + m, g1 + m, b1 + m)
}

/// Parse the `--tier` CLI argument to select a render tier.
///
/// The three tiers represent increasing levels of GPU capability:
/// - **Tier 1 (Direct):** One `draw()` call per texture group. Works on all hardware.
/// - **Tier 2 (Indirect):** Uses `multi_draw_indirect()` per texture group, batching
///   draw calls into GPU-side indirect buffers. Requires `MULTI_DRAW_INDIRECT`.
/// - **Tier 3 (Bindless):** Single `multi_draw_indirect()` per frame using texture
///   binding arrays. Requires `TEXTURE_BINDING_ARRAY` + `MULTI_DRAW_INDIRECT`.
///
/// Passing `--tier auto` (or omitting the flag) lets the engine choose the best
/// tier supported by the current GPU.
fn parse_tier() -> Option<RenderTier> {
    let args: Vec<String> = std::env::args().collect();
    for (i, arg) in args.iter().enumerate() {
        if arg == "--tier"
            && let Some(value) = args.get(i + 1)
        {
            return match value.as_str() {
                "1" | "direct" => Some(RenderTier::Direct),
                "2" | "indirect" => Some(RenderTier::Indirect),
                "3" | "bindless" => Some(RenderTier::Bindless),
                "auto" => None,
                other => {
                    eprintln!(
                        "Unknown tier '{other}'. Options: 1|direct, 2|indirect, 3|bindless, auto"
                    );
                    std::process::exit(1);
                }
            };
        }
    }
    // Default: auto-detect
    None
}

fn main() {
    logging::init();

    run_app(|ctx| {
        let tier_override = parse_tier();

        // Use the capability API to configure GPU requirements.
        // For auto-detect, request the best capability (graceful degradation).
        // For a specific tier, require that tier's capability.
        let descriptor = match tier_override {
            None => GraphicsContextDescriptor::new().request_capability::<BestBatchCapability2D>(),
            Some(RenderTier::Direct) => {
                GraphicsContextDescriptor::new().require_capability::<DirectBatchCapability2D>()
            }
            Some(RenderTier::Indirect) => {
                GraphicsContextDescriptor::new().require_capability::<IndirectBatchCapability2D>()
            }
            Some(RenderTier::Bindless) => {
                GraphicsContextDescriptor::new().require_capability::<BindlessBatchCapability2D>()
            }
        };
        let graphics_ctx =
            pollster::block_on(GraphicsContext::new_owned_with_descriptor(descriptor))
                .expect("Failed to create graphics context");

        let window = ctx
            .create_window(WindowDescriptor {
                title: "Batched Renderer Example".to_string(),
                size: Some(WinitPhysicalSize::new(800.0, 600.0)),
                ..Default::default()
            })
            .expect("Failed to create window");

        let surface_format = wgpu::TextureFormat::Bgra8UnormSrgb;

        let renderable_window = RenderWindowBuilder::new()
            .color_format(surface_format)
            .with_depth_default()
            .build(window, graphics_ctx.clone())
            .expect("Failed to create render window");

        let window_id = renderable_window.id();

        let renderer =
            create_batch_renderer_2d(graphics_ctx.clone(), surface_format, tier_override);

        tracing::info!("Using render tier: {}", renderer.tier());

        // Create initial depth buffer
        let depth_texture = graphics_ctx
            .device()
            .create_texture(&wgpu::TextureDescriptor {
                label: Some("example_depth"),
                size: wgpu::Extent3d {
                    width: 1,
                    height: 1,
                    depth_or_array_layers: 1,
                },
                mip_level_count: 1,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format: DEPTH_FORMAT,
                usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
                view_formats: &[],
            });
        let depth_view = depth_texture.create_view(&wgpu::TextureViewDescriptor::default());

        let mut windows = HashMap::new();
        windows.insert(window_id, renderable_window);

        Box::new(App {
            context: graphics_ctx,
            windows,
            renderer,
            depth_texture,
            depth_view,
            depth_width: 1,
            depth_height: 1,
            frame_count: 0,
        })
    });
}

impl astrelis_winit::app::App for App {
    fn update(
        &mut self,
        _ctx: &mut astrelis_winit::app::AppCtx,
        _time: &astrelis_winit::FrameTime,
    ) {
        self.frame_count += 1;
    }

    fn render(
        &mut self,
        _ctx: &mut astrelis_winit::app::AppCtx,
        window_id: WindowId,
        events: &mut astrelis_winit::event::EventBatch,
    ) {
        // Handle resize and get dimensions (scoped to release window borrow)
        let (phys_width, phys_height) = {
            let Some(window) = self.windows.get_mut(&window_id) else {
                return;
            };

            events.dispatch(|event| {
                if let astrelis_winit::event::Event::WindowResized(size) = event {
                    window.resized(*size);
                    astrelis_winit::event::HandleStatus::consumed()
                } else {
                    astrelis_winit::event::HandleStatus::ignored()
                }
            });

            let phys = window.physical_size();
            (phys.width, phys.height)
        };

        let width = phys_width as f32;
        let height = phys_height as f32;

        if width < 1.0 || height < 1.0 {
            return;
        }

        // Ensure depth buffer matches viewport
        self.ensure_depth_buffer(phys_width, phys_height);

        // Build instances and prepare GPU data
        let instances = self.build_instances(width, height);
        let batch = DrawBatch2D {
            instances,
            textures: vec![],
            projection: Self::ortho_projection(width, height),
        };
        self.renderer.prepare(&batch);

        let stats = self.renderer.stats();
        if self.frame_count.is_multiple_of(120) {
            tracing::info!(
                "Frame {}: {} instances ({} opaque, {} transparent), {} draw calls",
                self.frame_count,
                stats.instance_count,
                stats.opaque_count,
                stats.transparent_count,
                stats.draw_calls,
            );
        }

        // Re-borrow window for rendering
        let window = self.windows.get_mut(&window_id).unwrap();
        let Some(frame) = window.begin_frame() else {
            return; // Surface not available
        };

        // Create render pass with depth stencil attachment
        {
            let mut pass = frame
                .render_pass()
                .clear_color(Color::rgba(0.08, 0.08, 0.1, 1.0))
                .depth_attachment(&self.depth_view)
                .clear_depth(0.0) // 0.0 = far with GreaterEqual
                .label("batched_example_pass")
                .build();
            self.renderer.render(pass.wgpu_pass());
        }
        // Frame auto-submits on drop
    }
}