Skip to main content

runmat_plot/gpu/shaders/vertex/
line_direct.rs

1pub const SHADER: &str = r#"// Direct coordinate transformation vertex shader for precise plot rendering
2// Performs efficient data-to-viewport coordinate mapping without camera transforms
3
4struct Uniforms {
5    // Data bounds in world space
6    data_min: vec2<f32>,    // (x_min, y_min)
7    data_max: vec2<f32>,    // (x_max, y_max)
8    // Viewport bounds in NDC space (where plot should appear)
9    viewport_min: vec2<f32>, // NDC coordinates of viewport bottom-left
10    viewport_max: vec2<f32>, // NDC coordinates of viewport top-right
11}
12
13@group(0) @binding(0)
14var<uniform> uniforms: Uniforms;
15
16struct VertexInput {
17    @location(0) position: vec3<f32>,
18    @location(1) color: vec4<f32>,
19    @location(2) normal: vec3<f32>,
20    @location(3) tex_coords: vec2<f32>,
21}
22
23struct VertexOutput {
24    @builtin(position) clip_position: vec4<f32>,
25    @location(0) color: vec4<f32>,
26    @location(1) world_position: vec3<f32>,
27}
28
29@vertex
30fn vs_main(input: VertexInput) -> VertexOutput {
31    var out: VertexOutput;
32
33    // Transform data coordinates to normalized device coordinates within viewport bounds
34    let data_range = uniforms.data_max - uniforms.data_min;
35    let viewport_range = uniforms.viewport_max - uniforms.viewport_min;
36
37    // Normalize data position to [0, 1] within data bounds
38    let normalized_pos = (input.position.xy - uniforms.data_min) / data_range;
39
40    // Map to viewport NDC range
41    let ndc_pos = uniforms.viewport_min + normalized_pos * viewport_range;
42
43    // Create final clip position
44    out.clip_position = vec4<f32>(ndc_pos.x, ndc_pos.y, 0.0, 1.0);
45    out.world_position = input.position;
46    out.color = input.color;
47
48    return out;
49}
50
51@fragment
52fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
53    // Simple line rendering
54    return input.color;
55}
56"#;