Skip to main content

cvkg_render_gpu/
lib.rs

1//! # CVKG Agentic Development Guidelines (v1.2)
2//!
3//! All AI agents contributing to this crate MUST follow ALL seven rules:
4//!
5//! ── Karpathy Guidelines (1–4) ────────────────────────────────────────────
6//! 1. THINK FIRST     -- State assumptions. Surface ambiguity. Push back on complexity.
7//! 2. STAY SIMPLE     -- Minimum code. No speculative features. No unasked-for abstractions.
8//! 3. BE SURGICAL     -- Touch only what's required. Own your orphans. Don't improve neighbors.
9//! 4. VERIFY GOALS    -- Turn tasks into checkable criteria. Loop until they pass. Never commit broken.
10//!
11//! ── CVKG Extended Protocols (5–7) ────────────────────────────────────────
12//! 5. TRIPLE-PASS     -- Read the target, its surrounding context, and its full call graph
13//!                      at least THREE TIMES before making any edit or revision.
14//! 6. COMMENT ALL     -- Every major pub fn, unsafe block, and non-trivial algorithm in
15//!                      every .rs/.ts/.h/.wgsl file MUST have a descriptive doc comment.
16//!                      Comments describe WHY and WHAT CONTRACT, not HOW mechanically.
17//! 7. MONITOR LOOPS   -- Check every tool call / command for progress every 30 seconds.
18//!                      After 3 consecutive identical failures, stop, write BLOCKED.md,
19//!                      and move to unblocked work. Never silently accept a broken state.
20//!
21//! Sources:
22//!   Karpathy: https://github.com/multica-ai/andrej-karpathy-skills
23//!   CVKG Extended: Section 2 of the CVKG Design Specification
24#![allow(
25    clippy::type_complexity,
26    clippy::unwrap_or_default,
27    dead_code,
28    unused_variables,
29    unused_imports,
30    unused_mut,
31    unused_parens
32)]
33
34mod kvasir;
35mod material;
36
37// Re-export material types for downstream users
38pub use material::builtins;
39pub use material::{CompiledMaterial, MaterialCompiler, MaterialError, MaterialGraph, MaterialOp};
40
41pub mod accessibility;
42pub mod ai;
43mod api;
44pub mod draw;
45pub mod filter;
46pub mod passes;
47pub mod pyramid;
48pub mod renderer;
49mod surtr_util;
50pub mod types;
51pub mod vertex;
52
53pub mod heim;
54pub use heim::SkylinePacker;
55
56// P1-1 (phase 6): subsystems module. Each subsystem (config,
57// geometry, text, svg, particles) is a self-contained module
58// that can be tested, reviewed, and modified in isolation.
59pub mod subsystems;
60pub use subsystems::RendererConfig;
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    use super::heim::SkylinePacker;
67
68    #[test]
69    fn test_shelf_packer_basic() {
70        let mut packer = SkylinePacker::new(100, 100);
71        assert_eq!(packer.pack(10, 10), Some((0, 0)));
72        assert_eq!(packer.pack(20, 15), Some((10, 0)));
73    }
74
75    #[test]
76    fn test_shelf_packer_wrap() {
77        let mut packer = SkylinePacker::new(100, 100);
78        packer.pack(60, 10);
79        assert_eq!(packer.pack(50, 20), Some((0, 10)));
80    }
81
82    #[test]
83    fn test_parse_svg_animations() {
84        let svg = r##"
85            <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
86                <g id="spinner">
87                    <animateTransform attributeName="transform" type="rotate" from="0" to="360" dur="2s" />
88                </g>
89                <circle id="pulse">
90                    <animate attributeName="opacity" from="0.5" to="1.0" dur="0.5s" />
91                </circle>
92                <!-- Edge cases: xlink:href, ms suffix, values list -->
93                <rect>
94                    <animate xlink:href="#myRect" attributeName="x" values="10; 20; 30" dur="500ms" />
95                </rect>
96            </svg>
97        "##;
98        let anims = draw::parse_svg_animations(svg.as_bytes());
99        assert_eq!(anims.len(), 3);
100
101        assert_eq!(anims[0].target_id, "spinner");
102        assert_eq!(anims[0].keyframe_values, vec![0.0, 360.0]);
103
104        assert_eq!(anims[1].target_id, "pulse");
105        assert_eq!(anims[1].attribute_name, "opacity");
106        assert_eq!(anims[1].duration, 0.5);
107        assert_eq!(anims[1].keyframe_values, vec![0.5, 1.0]);
108
109        assert_eq!(anims[2].target_id, "myRect");
110        assert_eq!(anims[2].attribute_name, "x");
111        assert_eq!(anims[2].duration, 0.5); // 500ms parsed as 0.5
112        assert_eq!(anims[2].keyframe_values, vec![10.0, 20.0, 30.0]);
113    }
114
115    #[test]
116    fn test_shelf_packer_full() {
117        let mut packer = SkylinePacker::new(10, 10);
118        assert_eq!(packer.pack(11, 5), None);
119        assert_eq!(packer.pack(5, 11), None);
120    }
121}
122
123// P1-12 fix: on wasm32/WebGL2, texture binding arrays are not supported.
124// The bind group layout uses count: None (single texture) on WASM, so the
125// WGSL must declare t_diffuse as a single texture, not a binding_array.
126// We swap the three affected WGSL files (common, material_opaque, bloom)
127// to WASM-specific variants on wasm32 targets. All other shader files
128// (shapes, material_glass, bifrost, color_blind, tonemap, particles) are
129// the same on both targets.
130#[cfg(target_arch = "wasm32")]
131pub(crate) const WGSL_COMMON: &str = include_str!("shaders/common_wasm.wgsl");
132#[cfg(not(target_arch = "wasm32"))]
133pub(crate) const WGSL_COMMON: &str = include_str!("shaders/common.wgsl");
134
135pub(crate) const WGSL_SHAPES: &str = include_str!("shaders/shapes.wgsl");
136
137#[cfg(target_arch = "wasm32")]
138pub(crate) const WGSL_MATERIAL_OPAQUE: &str = include_str!("shaders/material_opaque_wasm.wgsl");
139#[cfg(not(target_arch = "wasm32"))]
140pub(crate) const WGSL_MATERIAL_OPAQUE: &str = include_str!("shaders/material_opaque.wgsl");
141
142pub(crate) const WGSL_MATERIAL_GLASS: &str = include_str!("shaders/material_glass.wgsl");
143pub(crate) const WGSL_BIFROST: &str = include_str!("shaders/bifrost.wgsl");
144
145#[cfg(target_arch = "wasm32")]
146pub(crate) const WGSL_BLOOM: &str = include_str!("shaders/bloom_wasm.wgsl");
147#[cfg(not(target_arch = "wasm32"))]
148pub(crate) const WGSL_BLOOM: &str = include_str!("shaders/bloom.wgsl");
149
150pub(crate) const WGSL_COLOR_BLIND: &str = include_str!("shaders/color_blind.wgsl");
151pub(crate) const WGSL_TONEMAP: &str = include_str!("shaders/tonemap.wgsl");
152pub(crate) const WGSL_PARTICLES: &str = include_str!("shaders/particles.wgsl");
153
154pub mod color_blindness;
155
156// Re-export ColorBlindMode for downstream users
157pub use color_blindness::ColorBlindMode;
158
159// ShieldWall -- re-export AccessKit types so callers can build tree updates
160// without depending on accesskit directly.
161pub use accesskit::{
162    ActionHandler, ActionRequest, ActivationHandler, DeactivationHandler, Node, NodeId, Role, Tree,
163    TreeId, TreeUpdate,
164};
165pub use accesskit_winit::Adapter as ShieldWallAdapter;
166
167// Re-export ColorTheme and SceneUniforms for cvkg-render-gpu users
168pub use cvkg_core::{ColorTheme, SceneUniforms};
169
170pub use renderer::GpuRenderer;
171
172// P1-35: SVG filter graph integration (moved to filter/ module)
173pub use types::{SvgAnimation, SvgModel};
174pub use vertex::{InstanceData, Vertex};