Skip to main content

cvkg_render_gpu/
types.rs

1//! Core data types, internal structs, and rendering contexts.
2use crate::vertex::Vertex;
3use cvkg_core::Rect;
4
5/// SvgModel — A collection of tessellated triangles representing a vector icon.
6/// Paths are stored as independent sub-models, each with its own vertex range
7/// and local transform, enabling per-path manipulation (e.g. in an SVG editor).
8#[derive(Clone, Debug)]
9pub struct SvgModel {
10    /// All vertices for all paths in this SVG.
11    pub vertices: Vec<Vertex>,
12    /// All indices for all paths in this SVG.
13    pub indices: Vec<u32>,
14    /// The SVG viewBox defining the coordinate space.
15    pub view_box: Rect,
16    /// Per-path sub-models, each with its own vertex range and local transform.
17    pub paths: Vec<SvgPath>,
18    /// Animations parsed from SVG `<animate>` elements.
19    pub animations: Vec<SvgAnimation>,
20}
21
22/// A single path within an SVG model, with its own vertex range and local transform.
23/// Multiple paths can share the same underlying vertex buffer but are drawn
24/// independently with different transforms.
25#[derive(Clone, Debug)]
26pub struct SvgPath {
27    /// The element id from the SVG (e.g. "t1", "path2").
28    pub id: String,
29    /// Range into SvgModel.vertices for this path's vertices.
30    pub vertex_range: std::ops::Range<usize>,
31    /// Range into SvgModel.indices for this path's indices.
32    pub index_range: std::ops::Range<usize>,
33    /// Local transform offset applied when drawing this path.
34    /// This allows per-path positioning, rotation, and scaling.
35    pub local_transform: SvgTransform,
36}
37
38/// A 2D affine transform for SVG path positioning.
39#[derive(Clone, Debug, Default)]
40pub struct SvgTransform {
41    /// Translation in SVG user units.
42    pub translate: [f32; 2],
43    /// Rotation in degrees.
44    pub rotation: f32,
45    /// Scale factor (1.0 = no scaling).
46    pub scale: f32,
47}
48
49#[derive(Clone, Debug)]
50pub struct SvgAnimation {
51    pub target_id: String,
52    pub attribute_name: String,
53    pub from_val: f32,
54    pub to_val: f32,
55    pub duration: f32,
56    pub vertex_range: std::ops::Range<usize>,
57}
58
59/// Represents a single batched GPU draw call.
60/// Batches are broken whenever the active texture or primitive mode changes.
61#[derive(Debug, Clone)]
62pub(crate) struct DrawCall {
63    pub texture_id: Option<u32>,
64    pub scissor_rect: Option<Rect>,
65    pub index_start: u32,
66    pub index_count: u32,
67    /// Material routing tag — determines which pass this draw call is routed to
68    /// in the multi-pass Backdrop Capture pipeline.
69    pub material: cvkg_core::DrawMaterial,
70    pub target_id: Option<u64>,
71    pub instance_start: u32,
72}
73
74pub struct OffscreenEffectConfig {
75    pub target_id: u64,
76    pub effect: String,
77    pub blend_mode: u32,
78    pub effect_args: [f32; 16],
79}
80
81#[derive(Debug, Clone, Copy)]
82pub(crate) struct ShadowState {
83    pub radius: f32,
84    pub color: [f32; 4],
85    pub _offset: [f32; 2],
86}
87
88pub(crate) struct SurfaceContext {
89    pub(crate) surface: wgpu::Surface<'static>,
90    pub(crate) config: wgpu::SurfaceConfiguration,
91    pub(crate) scene_texture: wgpu::TextureView,
92    pub(crate) scene_msaa_texture: wgpu::TextureView,
93    pub(crate) scene_bind_group: wgpu::BindGroup,
94    pub(crate) scene_texture_bind_group: wgpu::BindGroup,
95    pub(crate) depth_texture_view: wgpu::TextureView,
96    pub(crate) blur_tex_a: crate::kvasir::resource::ResourceId,
97    pub(crate) blur_tex_b: crate::kvasir::resource::ResourceId,
98    pub(crate) bloom_tex_a: crate::kvasir::resource::ResourceId,
99    pub(crate) bloom_tex_b: crate::kvasir::resource::ResourceId,
100    pub(crate) blur_env_bind_group_a: wgpu::BindGroup,
101    pub(crate) blur_env_bind_group_b: wgpu::BindGroup,
102    pub(crate) bloom_env_bind_group_a: wgpu::BindGroup,
103    pub(crate) bloom_env_bind_group_b: wgpu::BindGroup,
104    pub(crate) scale_factor: f32,
105    pub(crate) sampler: wgpu::Sampler,
106}
107
108/// HeadlessContext — A rendering target for surface-less execution.
109pub struct HeadlessContext {
110    pub scene_texture: wgpu::TextureView,
111    pub scene_msaa_texture: wgpu::TextureView,
112    pub scene_bind_group: wgpu::BindGroup,
113    pub scene_texture_bind_group: wgpu::BindGroup,
114    pub depth_texture_view: wgpu::TextureView,
115    pub blur_tex_a: crate::kvasir::resource::ResourceId,
116    pub blur_tex_b: crate::kvasir::resource::ResourceId,
117    pub bloom_tex_a: crate::kvasir::resource::ResourceId,
118    pub bloom_tex_b: crate::kvasir::resource::ResourceId,
119    pub blur_env_bind_group_a: wgpu::BindGroup,
120    pub blur_env_bind_group_b: wgpu::BindGroup,
121    pub bloom_env_bind_group_a: wgpu::BindGroup,
122    pub bloom_env_bind_group_b: wgpu::BindGroup,
123    pub scale_factor: f32,
124    pub sampler: wgpu::Sampler,
125    pub width: u32,
126    pub height: u32,
127    pub output_texture: wgpu::Texture,
128    pub output_view: wgpu::TextureView,
129}
130
131pub(crate) const MAX_VERTICES: usize = 100_000;
132pub(crate) const MAX_INDICES: usize = 150_000;
133
134#[repr(C)]
135#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
136pub struct EffectUniforms {
137    pub time: f32,
138    pub pad0: f32,
139    pub size: [f32; 2],
140    pub args: [f32; 16],
141}
142
143/// Per-draw-call glass instance parameters.
144/// Passed as push constants (fast path, no buffer allocation) or via
145/// a dedicated bind group for per-element blur sampling.
146#[repr(C)]
147#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
148pub struct GlassInstanceUniforms {
149    /// Local tint override: [r, g, b, weight].
150    /// weight=0 = use theme tint only, weight=1 = use local tint only.
151    pub tint_override: [f32; 4],
152    /// Per-instance IOR override. 0.0 = use theme default (1.45).
153    pub ior_override: f32,
154    /// Blur strength multiplier. 1.0 = normal, 2.0 = double blur.
155    pub blur_multiplier: f32,
156    /// Frost intensity override. 0.0 = theme default.
157    pub frost_override: f32,
158    /// Scissor rect in physical pixels: [x, y, width, height].
159    /// Used for per-element backdrop blur sampling.
160    pub scissor_px: [f32; 4],
161    /// Portal index: which per-element blur texture to sample.
162    /// 0 = main scene blur (default), 1+ = portal region blur.
163    pub portal_index: f32,
164    pub _pad: f32,
165}
166
167impl Default for GlassInstanceUniforms {
168    fn default() -> Self {
169        Self {
170            tint_override: [0.0; 4],
171            ior_override: 0.0,
172            blur_multiplier: 1.0,
173            frost_override: 0.0,
174            scissor_px: [0.0; 4],
175            portal_index: 0.0,
176            _pad: 0.0,
177        }
178    }
179}