Skip to main content

runmat_plot/gpu/shaders/
errorbar.rs

1pub const F32: &str = r#"
2const WORKGROUP_SIZE: u32 = {{WORKGROUP_SIZE}}u;
3
4struct VertexRaw { data: array<f32, 12u>, };
5struct Params { color: vec4<f32>, count: u32, line_style: u32, cap_half_width: f32, orientation: u32, };
6
7@group(0) @binding(0) var<storage, read> buf_x: array<f32>;
8@group(0) @binding(1) var<storage, read> buf_y: array<f32>;
9@group(0) @binding(2) var<storage, read> buf_x_neg: array<f32>;
10@group(0) @binding(3) var<storage, read> buf_x_pos: array<f32>;
11@group(0) @binding(4) var<storage, read> buf_y_neg: array<f32>;
12@group(0) @binding(5) var<storage, read> buf_y_pos: array<f32>;
13@group(0) @binding(6) var<storage, read_write> out_vertices: array<VertexRaw>;
14@group(0) @binding(7) var<uniform> params: Params;
15
16fn write_vertex(index: u32, pos: vec3<f32>, color: vec4<f32>) {
17  var v: VertexRaw;
18  v.data[0u] = pos.x; v.data[1u] = pos.y; v.data[2u] = 0.0;
19  v.data[3u] = color.x; v.data[4u] = color.y; v.data[5u] = color.z; v.data[6u] = color.w;
20  v.data[7u] = 0.0; v.data[8u] = 0.0; v.data[9u] = 1.0; v.data[10u] = 0.0; v.data[11u] = 0.0;
21  out_vertices[index] = v;
22}
23
24fn include_segment(i:u32, style:u32)->bool { switch(style){ case 0u:{return true;} case 1u:{return (i%4u)<2u;} case 2u:{return (i%4u)==0u;} case 3u:{let m=i%6u; return (m<2u)||(m==3u);} default:{return true;} } }
25
26@compute @workgroup_size(WORKGROUP_SIZE)
27fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
28  let i = gid.x;
29  if (i >= params.count) { return; }
30  if (!include_segment(i, params.line_style)) { return; }
31  let x = buf_x[i];
32  let y = buf_y[i];
33  let y0 = y - buf_y_neg[i];
34  let y1 = y + buf_y_pos[i];
35  let x0 = x - buf_x_neg[i];
36  let x1 = x + buf_x_pos[i];
37  let base = i * (select(6u, 12u, params.orientation == 2u));
38  if (params.orientation != 1u) {
39    write_vertex(base + 0u, vec3<f32>(x, y0, 0.0), params.color);
40    write_vertex(base + 1u, vec3<f32>(x, y1, 0.0), params.color);
41    write_vertex(base + 2u, vec3<f32>(x - params.cap_half_width, y0, 0.0), params.color);
42    write_vertex(base + 3u, vec3<f32>(x + params.cap_half_width, y0, 0.0), params.color);
43    write_vertex(base + 4u, vec3<f32>(x - params.cap_half_width, y1, 0.0), params.color);
44    write_vertex(base + 5u, vec3<f32>(x + params.cap_half_width, y1, 0.0), params.color);
45  }
46  if (params.orientation != 0u) {
47    let off = select(0u, 6u, params.orientation == 2u);
48    write_vertex(base + off + 0u, vec3<f32>(x0, y, 0.0), params.color);
49    write_vertex(base + off + 1u, vec3<f32>(x1, y, 0.0), params.color);
50    write_vertex(base + off + 2u, vec3<f32>(x0, y - params.cap_half_width, 0.0), params.color);
51    write_vertex(base + off + 3u, vec3<f32>(x0, y + params.cap_half_width, 0.0), params.color);
52    write_vertex(base + off + 4u, vec3<f32>(x1, y - params.cap_half_width, 0.0), params.color);
53    write_vertex(base + off + 5u, vec3<f32>(x1, y + params.cap_half_width, 0.0), params.color);
54  }
55}
56"#;
57
58pub const F64: &str = r#"
59const WORKGROUP_SIZE: u32 = {{WORKGROUP_SIZE}}u;
60
61struct VertexRaw { data: array<f32, 12u>, };
62struct Params { color: vec4<f32>, count: u32, line_style: u32, cap_half_width: f32, orientation: u32, };
63
64@group(0) @binding(0) var<storage, read> buf_x: array<f64>;
65@group(0) @binding(1) var<storage, read> buf_y: array<f64>;
66@group(0) @binding(2) var<storage, read> buf_x_neg: array<f64>;
67@group(0) @binding(3) var<storage, read> buf_x_pos: array<f64>;
68@group(0) @binding(4) var<storage, read> buf_y_neg: array<f64>;
69@group(0) @binding(5) var<storage, read> buf_y_pos: array<f64>;
70@group(0) @binding(6) var<storage, read_write> out_vertices: array<VertexRaw>;
71@group(0) @binding(7) var<uniform> params: Params;
72
73fn write_vertex(index: u32, pos: vec3<f32>, color: vec4<f32>) {
74  var v: VertexRaw;
75  v.data[0u] = pos.x; v.data[1u] = pos.y; v.data[2u] = 0.0;
76  v.data[3u] = color.x; v.data[4u] = color.y; v.data[5u] = color.z; v.data[6u] = color.w;
77  v.data[7u] = 0.0; v.data[8u] = 0.0; v.data[9u] = 1.0; v.data[10u] = 0.0; v.data[11u] = 0.0;
78  out_vertices[index] = v;
79}
80
81fn include_segment(i:u32, style:u32)->bool { switch(style){ case 0u:{return true;} case 1u:{return (i%4u)<2u;} case 2u:{return (i%4u)==0u;} case 3u:{let m=i%6u; return (m<2u)||(m==3u);} default:{return true;} } }
82
83@compute @workgroup_size(WORKGROUP_SIZE)
84fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
85  let i = gid.x;
86  if (i >= params.count) { return; }
87  if (!include_segment(i, params.line_style)) { return; }
88  let x = f32(buf_x[i]);
89  let y = f32(buf_y[i]);
90  let y0 = y - f32(buf_y_neg[i]);
91  let y1 = y + f32(buf_y_pos[i]);
92  let x0 = x - f32(buf_x_neg[i]);
93  let x1 = x + f32(buf_x_pos[i]);
94  let base = i * (select(6u, 12u, params.orientation == 2u));
95  if (params.orientation != 1u) {
96    write_vertex(base + 0u, vec3<f32>(x, y0, 0.0), params.color);
97    write_vertex(base + 1u, vec3<f32>(x, y1, 0.0), params.color);
98    write_vertex(base + 2u, vec3<f32>(x - params.cap_half_width, y0, 0.0), params.color);
99    write_vertex(base + 3u, vec3<f32>(x + params.cap_half_width, y0, 0.0), params.color);
100    write_vertex(base + 4u, vec3<f32>(x - params.cap_half_width, y1, 0.0), params.color);
101    write_vertex(base + 5u, vec3<f32>(x + params.cap_half_width, y1, 0.0), params.color);
102  }
103  if (params.orientation != 0u) {
104    let off = select(0u, 6u, params.orientation == 2u);
105    write_vertex(base + off + 0u, vec3<f32>(x0, y, 0.0), params.color);
106    write_vertex(base + off + 1u, vec3<f32>(x1, y, 0.0), params.color);
107    write_vertex(base + off + 2u, vec3<f32>(x0, y - params.cap_half_width, 0.0), params.color);
108    write_vertex(base + off + 3u, vec3<f32>(x0, y + params.cap_half_width, 0.0), params.color);
109    write_vertex(base + off + 4u, vec3<f32>(x1, y - params.cap_half_width, 0.0), params.color);
110    write_vertex(base + off + 5u, vec3<f32>(x1, y + params.cap_half_width, 0.0), params.color);
111  }
112}
113"#;