aga 2.0.0

AgenticGraphicsAcceleration — standalone agentic-first GPU rendering backend; wgpu replacement with Vulkan, OpenGL, and complete ontology
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
//! Application runner — the agpu equivalent of eframe's `run_native`.
//!
//! Uses winit for windowing and event dispatch, wgpu for GPU rendering,
//! and integrates the agpu `Model` trait to drive the Elm architecture.
//!
//! Every layer is agent-discoverable via the ontology.

use crate::context::GpuContext;
use crate::core::{Position, Rect};
use crate::event::{HitMap, convert_window_event};
use crate::ontology::{OntologyRegistry, SemanticRole, UiNode, UiTree};
use crate::painter::AgpuPainter;
use crate::renderer::ShapeRenderer;
use crate::runtime::{Command, Frame, Model, ProgramOptions, Subscription};
use crate::text::TextEngine;
use crate::types::BackendPreference;
use std::sync::Arc;
use std::time::{Duration, Instant};
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::window::{Window, WindowId};

/// The agpu application runner.
///
/// Wraps an agpu `Model` and drives it with wgpu rendering and winit events.
pub struct AgpuApp<M: Model> {
    model: M,
    options: ProgramOptions,
}

impl<M: Model + 'static> AgpuApp<M> {
    /// Create a new application with the given model.
    pub fn new(model: M) -> Self {
        Self {
            model,
            options: ProgramOptions::default(),
        }
    }

    /// Override the default program options.
    pub fn with_options(mut self, options: ProgramOptions) -> Self {
        self.options = options;
        self
    }

    /// Set the GPU backend preference.
    pub fn with_backend(mut self, preference: BackendPreference) -> Self {
        self.options.backend = preference;
        self
    }

    /// Run the application. Blocks until the window closes.
    pub fn run(self) -> Result<(), Box<dyn std::error::Error>> {
        let event_loop = EventLoop::new()?;
        event_loop.set_control_flow(ControlFlow::Poll);

        let mut handler = AppHandler::Uninitialised {
            model: self.model,
            options: self.options,
        };

        event_loop.run_app(&mut handler)?;
        Ok(())
    }
}

// ── Internal handler ────────────────────────────────────────────────

/// Internal state machine — uninitialised until the first `resumed` call
/// because wgpu/winit require an active event loop to create the window.
enum AppHandler<M: Model> {
    Uninitialised {
        model: M,
        options: ProgramOptions,
    },
    Running(Box<RunningApp<M>>),
    /// Sentinel after an error or close.
    Exited,
}

struct RunningApp<M: Model> {
    model: M,
    window: Arc<Window>,
    gpu: GpuContext,
    surface: wgpu::Surface<'static>,
    surface_config: wgpu::SurfaceConfiguration,
    surface_format: wgpu::TextureFormat,
    shapes: ShapeRenderer,
    text: TextEngine,
    hit_map: HitMap,
    ontology: OntologyRegistry,
    running: bool,
    cursor_position: Position,
    tick_rate: Option<Duration>,
    last_tick: Instant,
    msaa_texture: Option<wgpu::TextureView>,
    active_timers: Vec<(String, Duration, Instant)>,
    pending_delays: Vec<(String, Instant)>,
}

impl<M: Model + 'static> RunningApp<M> {
    fn new(
        model: M,
        options: &ProgramOptions,
        event_loop: &ActiveEventLoop,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let attrs = Window::default_attributes()
            .with_title(model.title())
            .with_inner_size(winit::dpi::LogicalSize::new(
                options.width as f64,
                options.height as f64,
            ))
            .with_resizable(options.resizable);

        let window = Arc::new(event_loop.create_window(attrs)?);

        // Create a temporary instance to create the surface,
        // then let GpuContext handle backend selection with fallback.
        let preference = options.backend;
        let backends = preference.to_backends();
        let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
            backends,
            ..Default::default()
        });

        let surface = instance.create_surface(window.clone())?;

        let gpu = pollster::block_on(GpuContext::new(&surface, preference))?;

        let size = window.inner_size();
        let surface_caps = surface.get_capabilities(gpu.adapter());
        let format = surface_caps
            .formats
            .iter()
            .find(|f| !f.is_srgb())
            .copied()
            .unwrap_or(surface_caps.formats[0]);

        let present_mode = if options.vsync {
            wgpu::PresentMode::AutoVsync
        } else {
            wgpu::PresentMode::AutoNoVsync
        };

        let surface_config = wgpu::SurfaceConfiguration {
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
            format,
            width: size.width.max(1),
            height: size.height.max(1),
            present_mode,
            alpha_mode: surface_caps.alpha_modes[0],
            view_formats: vec![],
            desired_maximum_frame_latency: 3,
        };
        surface.configure(gpu.device(), &surface_config);

        let shapes = ShapeRenderer::new(
            &gpu,
            format,
            options.width,
            options.height,
            options.msaa_samples,
        );
        let text = TextEngine::new(&gpu, format);

        let msaa_texture = if options.msaa_samples > 1 {
            Some(create_msaa_texture(
                gpu.device(),
                format,
                size.width.max(1),
                size.height.max(1),
                options.msaa_samples,
            ))
        } else {
            None
        };

        let mut ontology = OntologyRegistry::new();
        model.register_ontology(&mut ontology);

        let init_cmd = model.init();

        let mut app = Self {
            model,
            window,
            gpu,
            surface,
            surface_config,
            surface_format: format,
            shapes,
            text,
            hit_map: HitMap::new(),
            ontology,
            running: true,
            cursor_position: Position::ZERO,
            tick_rate: options.tick_rate,
            last_tick: Instant::now(),
            msaa_texture,
            active_timers: Vec::new(),
            pending_delays: Vec::new(),
        };

        app.process_command(init_cmd);
        Ok(app)
    }

    fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
        if new_size.width > 0 && new_size.height > 0 {
            self.surface_config.width = new_size.width;
            self.surface_config.height = new_size.height;
            self.surface
                .configure(self.gpu.device(), &self.surface_config);
            self.shapes
                .set_viewport(new_size.width as f32, new_size.height as f32);
            if self.shapes.sample_count() > 1 {
                self.msaa_texture = Some(create_msaa_texture(
                    self.gpu.device(),
                    self.surface_format,
                    new_size.width,
                    new_size.height,
                    self.shapes.sample_count(),
                ));
            }
        }
    }

    fn render(&mut self) {
        let frame = match self.surface.get_current_texture() {
            Ok(f) => f,
            Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
                self.surface
                    .configure(self.gpu.device(), &self.surface_config);
                return;
            }
            Err(wgpu::SurfaceError::OutOfMemory) => {
                log::error!("agpu: out of GPU memory");
                self.running = false;
                return;
            }
            Err(e) => {
                log::warn!("agpu: surface error: {e:?}");
                return;
            }
        };

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

        // Begin frame
        self.shapes.begin_frame();
        self.hit_map.clear();

        let area = Rect::new(
            0.0,
            0.0,
            self.surface_config.width as f32,
            self.surface_config.height as f32,
        );

        // Run model view
        {
            let mut painter = AgpuPainter::new(&mut self.shapes, &mut self.text);
            let mut frame = Frame::new(area, &mut self.hit_map, &mut painter);
            self.model.view(&mut frame);

            let nodes = frame.take_nodes();
            if !nodes.is_empty() {
                let mut root = UiNode::new("root", SemanticRole::Container);
                root.children = nodes;
                self.ontology.set_tree(UiTree::new(root));
            }
        }

        // GPU render pass
        let mut encoder =
            self.gpu
                .device()
                .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                    label: Some("agpu_encoder"),
                });

        {
            let (target_view, resolve_target) = if let Some(msaa_view) = &self.msaa_texture {
                (msaa_view, Some(&view))
            } else {
                (&view, None)
            };
            let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("agpu_pass"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: target_view,
                    resolve_target,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(wgpu::Color {
                            r: 0.08,
                            g: 0.08,
                            b: 0.10,
                            a: 1.0,
                        }),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                depth_stencil_attachment: None,
                timestamp_writes: None,
                occlusion_query_set: None,
            });

            self.shapes.flush(&self.gpu, &mut pass);
            self.text.flush(
                &self.gpu,
                &mut pass,
                self.surface_config.width,
                self.surface_config.height,
            );
        }

        self.gpu.queue().submit(std::iter::once(encoder.finish()));
        frame.present();
        self.text.trim();
    }

    fn process_command(&mut self, cmd: Command<M::Msg>) {
        match cmd {
            Command::None => {}
            Command::Quit => {
                self.running = false;
            }
            Command::Batch(cmds) => {
                for c in cmds {
                    self.process_command(c);
                }
            }
            Command::Message(msg) => {
                let cmd = self.model.update(msg);
                self.process_command(cmd);
            }
            Command::SetTickRate(d) => {
                self.tick_rate = Some(d);
            }
            Command::ExportOntology => {
                self.model.register_ontology(&mut self.ontology);
            }
            Command::AgentAction {
                agent_id,
                action,
                params,
            } => {
                // Validate params against the ontology schema if a node exists
                if let Some(node) = self.ontology.find_node(&agent_id) {
                    if let Err(e) =
                        self.ontology
                            .validate_action_params(&node.widget_type, &action, &params)
                    {
                        log::warn!("AgentAction validation failed for {agent_id}.{action}: {e}");
                        return;
                    }
                }
                // Dispatch to the model as an event
                let ev = crate::event::Event::AgentAction {
                    agent_id,
                    action,
                    params,
                };
                if let Some(msg) = self.model.handle_event(ev) {
                    let cmd = self.model.update(msg);
                    self.process_command(cmd);
                }
            }
            Command::Task(task) => {
                let msg = task();
                let cmd = self.model.update(msg);
                self.process_command(cmd);
            }
            Command::TaskWithTimeout {
                task,
                timeout,
                on_timeout,
            } => {
                use std::sync::mpsc;
                let (tx, rx) = mpsc::channel();
                std::thread::spawn(move || {
                    let result = task();
                    let _ = tx.send(result);
                });
                let msg = match rx.recv_timeout(timeout) {
                    Ok(result) => result,
                    Err(_) => on_timeout,
                };
                let cmd = self.model.update(msg);
                self.process_command(cmd);
            }
            Command::TaskCancellable { task, token } => {
                let msg = task(token);
                let cmd = self.model.update(msg);
                self.process_command(cmd);
            }
        }
    }

    fn refresh_subscriptions(&mut self) {
        let subs = self.model.subscriptions();
        // Reconcile active timers
        let new_ids: Vec<String> = subs
            .iter()
            .filter_map(|s| match s {
                Subscription::Timer { id, .. } => Some(id.clone()),
                _ => None,
            })
            .collect();
        // Remove timers no longer subscribed
        self.active_timers.retain(|(id, _, _)| new_ids.contains(id));
        // Add new timers
        for sub in &subs {
            if let Subscription::Timer { id, interval, .. } = sub {
                if !self.active_timers.iter().any(|(aid, _, _)| aid == id) {
                    self.active_timers
                        .push((id.clone(), *interval, Instant::now()));
                }
            }
        }
        // Add new delays
        for sub in subs {
            if let Subscription::Delay { id, duration, .. } = &sub {
                if !self.pending_delays.iter().any(|(did, _)| did == id) {
                    self.pending_delays
                        .push((id.clone(), Instant::now() + *duration));
                }
            }
        }
    }
}

// ── winit ApplicationHandler ────────────────────────────────────────

impl<M: Model + 'static> ApplicationHandler for AppHandler<M> {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        // Initialise on first resume
        if let AppHandler::Uninitialised { .. } = self {
            let taken = std::mem::replace(self, AppHandler::Exited);
            if let AppHandler::Uninitialised { model, options } = taken {
                match RunningApp::new(model, &options, event_loop) {
                    Ok(app) => {
                        *self = AppHandler::Running(Box::new(app));
                    }
                    Err(e) => {
                        log::error!("agpu: failed to initialise: {e}");
                        event_loop.exit();
                    }
                }
            }
        }
    }

    fn window_event(
        &mut self,
        event_loop: &ActiveEventLoop,
        _window_id: WindowId,
        event: WindowEvent,
    ) {
        let app = match self {
            AppHandler::Running(app) => app.as_mut(),
            _ => return,
        };

        // Handle resize before converting events
        if let WindowEvent::Resized(size) = event {
            app.resize(size);
        }

        // Track cursor position
        if let WindowEvent::CursorMoved { position, .. } = &event {
            app.cursor_position = Position::new(position.x as f32, position.y as f32);
        }

        // Convert and dispatch events
        let events = convert_window_event(&event);
        for mut ev in events {
            // Patch mouse events with current cursor position
            if let crate::event::Event::Mouse(ref mut mouse) = ev {
                if mouse.position == Position::ZERO {
                    mouse.position = app.cursor_position;
                }
            }
            if let Some(msg) = app.model.handle_event(ev) {
                let cmd = app.model.update(msg);
                app.process_command(cmd);
            }
        }

        // Redraw
        if let WindowEvent::RedrawRequested = event {
            app.render();
        }

        if !app.running {
            event_loop.exit();
        }
    }

    fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
        if let AppHandler::Running(app) = self {
            let now = Instant::now();

            // Tick subscriptions: check active timers
            let mut fired_timer_ids = Vec::new();
            for (id, interval, last_fire) in &mut app.active_timers {
                if now.duration_since(*last_fire) >= *interval {
                    *last_fire = now;
                    fired_timer_ids.push(id.clone());
                }
            }
            if !fired_timer_ids.is_empty() {
                // Re-fetch subscriptions to get message factories
                let subs = app.model.subscriptions();
                for sub in &subs {
                    if let Subscription::Timer { id, msg, .. } = sub {
                        if fired_timer_ids.contains(id) {
                            let m = msg();
                            let cmd = app.model.update(m);
                            app.process_command(cmd);
                        }
                    }
                }
            }

            // Check pending one-shot delays
            let mut fired_delays = Vec::new();
            app.pending_delays.retain(|(id, deadline)| {
                if now >= *deadline {
                    fired_delays.push(id.clone());
                    false
                } else {
                    true
                }
            });
            if !fired_delays.is_empty() {
                let subs = app.model.subscriptions();
                for sub in subs {
                    if let Subscription::Delay { id, msg, .. } = sub {
                        if fired_delays.contains(&id) {
                            let cmd = app.model.update(msg);
                            app.process_command(cmd);
                        }
                    }
                }
            }

            // Main tick
            match app.tick_rate {
                Some(rate) => {
                    if now.duration_since(app.last_tick) >= rate {
                        app.last_tick = now;
                        if let Some(msg) = app.model.handle_event(crate::event::Event::Tick) {
                            let cmd = app.model.update(msg);
                            app.process_command(cmd);
                        }
                        app.window.request_redraw();
                    }
                }
                None => {
                    app.window.request_redraw();
                }
            }

            // Refresh subscription state after updates
            app.refresh_subscriptions();
        }
    }
}

/// Create a multisampled texture view for MSAA rendering.
fn create_msaa_texture(
    device: &wgpu::Device,
    format: wgpu::TextureFormat,
    width: u32,
    height: u32,
    sample_count: u32,
) -> wgpu::TextureView {
    let texture = device.create_texture(&wgpu::TextureDescriptor {
        label: Some("agpu_msaa_texture"),
        size: wgpu::Extent3d {
            width,
            height,
            depth_or_array_layers: 1,
        },
        mip_level_count: 1,
        sample_count,
        dimension: wgpu::TextureDimension::D2,
        format,
        usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
        view_formats: &[],
    });
    texture.create_view(&wgpu::TextureViewDescriptor::default())
}