Skip to main content

zenthra_core/
glyph.rs

1use bytemuck::{Pod, Zeroable};
2
3/// A single instance of a glyph (or a solid background block) to be rendered on the GPU.
4/// This is the common "language" between the text engine and the main renderer.
5#[repr(C)]
6#[derive(Copy, Clone, Debug, Pod, Zeroable)]
7pub struct GlyphInstance {
8    /// Screen position [x, y].
9    pub pos: [f32; 2],
10    /// Screen size [width, height].
11    pub size: [f32; 2],
12    /// UV position [u, v].
13    pub uv_pos: [f32; 2],
14    /// UV size [u_width, v_height].
15    pub uv_size: [f32; 2],
16    /// Text color [r, g, b, a].
17    pub color: [f32; 4],
18    /// Background color [r, g, b, a].
19    pub bg_color: [f32; 4],
20    /// Clip rectangle [x, y, width, height].
21    pub clip_rect: [f32; 4],
22}
23
24impl GlyphInstance {
25    /// Returns the vertex buffer layout for this struct.
26    pub fn desc() -> wgpu::VertexBufferLayout<'static> {
27        wgpu::VertexBufferLayout {
28            array_stride: std::mem::size_of::<GlyphInstance>() as wgpu::BufferAddress,
29            step_mode: wgpu::VertexStepMode::Instance,
30            attributes: &[
31                wgpu::VertexAttribute { offset: 0, shader_location: 0, format: wgpu::VertexFormat::Float32x2 }, // pos
32                wgpu::VertexAttribute { offset: 8, shader_location: 1, format: wgpu::VertexFormat::Float32x2 }, // size
33                wgpu::VertexAttribute { offset: 16, shader_location: 2, format: wgpu::VertexFormat::Float32x2 }, // uv_pos
34                wgpu::VertexAttribute { offset: 24, shader_location: 3, format: wgpu::VertexFormat::Float32x2 }, // uv_size
35                wgpu::VertexAttribute { offset: 32, shader_location: 4, format: wgpu::VertexFormat::Float32x4 }, // color
36                wgpu::VertexAttribute { offset: 48, shader_location: 5, format: wgpu::VertexFormat::Float32x4 }, // bg_color
37                wgpu::VertexAttribute { offset: 64, shader_location: 6, format: wgpu::VertexFormat::Float32x4 }, // clip_rect
38            ],
39        }
40    }
41}