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    viewport_px: vec2<f32>,
12    log_flags: vec2<u32>,
13    _pad: vec2<u32>,
14}
15
16@group(0) @binding(0)
17var<uniform> uniforms: Uniforms;
18
19struct VertexInput {
20    @location(0) position: vec3<f32>,
21    @location(1) color: vec4<f32>,
22    @location(2) normal: vec3<f32>,
23    @location(3) tex_coords: vec2<f32>,
24}
25
26struct VertexOutput {
27    @builtin(position) clip_position: vec4<f32>,
28    @location(0) color: vec4<f32>,
29    @location(1) world_position: vec3<f32>,
30}
31
32fn transform_axis(value: f32, is_log: u32) -> f32 {
33    if (is_log != 0u) {
34        if (value <= 0.0) {
35            return 1e30;
36        }
37        return log(value) / log(10.0);
38    }
39    return value;
40}
41
42@vertex
43fn vs_main(input: VertexInput) -> VertexOutput {
44    var out: VertexOutput;
45
46    // Transform data coordinates to normalized device coordinates within viewport bounds
47    let data_min = vec2<f32>(
48        transform_axis(uniforms.data_min.x, uniforms.log_flags.x),
49        transform_axis(uniforms.data_min.y, uniforms.log_flags.y)
50    );
51    let data_max = vec2<f32>(
52        transform_axis(uniforms.data_max.x, uniforms.log_flags.x),
53        transform_axis(uniforms.data_max.y, uniforms.log_flags.y)
54    );
55    let position = vec2<f32>(
56        transform_axis(input.position.x, uniforms.log_flags.x),
57        transform_axis(input.position.y, uniforms.log_flags.y)
58    );
59    let data_range = data_max - data_min;
60    let viewport_range = uniforms.viewport_max - uniforms.viewport_min;
61
62    // Normalize data position to [0, 1] within data bounds
63    let normalized_pos = (position - data_min) / data_range;
64
65    // Map to viewport NDC range
66    let ndc_pos = uniforms.viewport_min + normalized_pos * viewport_range;
67
68    // Create final clip position
69    out.clip_position = vec4<f32>(ndc_pos.x, ndc_pos.y, 0.0, 1.0);
70    out.world_position = input.position;
71    out.color = input.color;
72
73    return out;
74}
75
76@fragment
77fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
78    // Simple line rendering
79    return input.color;
80}
81"#;