pub const MESH_VS: &str = r#"
struct VOut {
@builtin(position) position: vec4<f32>,
@location(0) diffuse: vec4<f32>,
@location(1) uv: vec4<f32>,
@location(2) rad_ang: vec2<f32>,
};
@vertex
fn vs_main(@location(0) pos: vec2<f32>, @location(1) diffuse: vec4<f32>,
@location(2) uv: vec4<f32>, @location(3) rad_ang: vec2<f32>) -> VOut {
var o: VOut;
o.position = vec4<f32>(pos, 0.0, 1.0);
o.diffuse = diffuse;
o.uv = uv;
o.rad_ang = rad_ang;
return o;
}
"#;
pub fn warp_vs(group: u32) -> String {
warp_vs_src(group, false)
}
pub fn warp_uv_readback(group: u32) -> String {
warp_vs_src(group, true)
}
fn warp_vs_src(group: u32, probe: bool) -> String {
let (vid_param, position) = if probe {
(
", @builtin(vertex_index) vid: u32",
"let gw = u32(wu.diffuse.x);\n \
o.position = vec4<f32>((f32(vid % gw) + 0.5) / wu.diffuse.x * 2.0 - 1.0, \
1.0 - (f32(vid / gw) + 0.5) / wu.diffuse.y * 2.0, 0.0, 1.0);",
)
} else {
(
"",
"// glm::ortho(-1, 1, 1, -1): y is flipped\n o.position = vec4<f32>(pos.x, -pos.y, 0.0, 1.0);",
)
};
let fs = if probe {
r#"
@fragment
fn fs_probe(in: VOut) -> @location(0) vec4<f32> {
return vec4<f32>(in.uv.xy, 0.0, 0.0);
}
"#
} else {
""
};
format!(
r#"
struct WarpVsUniforms {{
aspect: vec4<f32>, // aspectX, aspectY, invAspectX, invAspectY
timing: vec4<f32>, // warpTime, warpScaleInverse, texelOffset.x, texelOffset.y
factors: vec4<f32>, // warpFactors
diffuse: vec4<f32>, // frag_COLOR (decay or 1)
}};
@group({group}) @binding(0) var<uniform> wu: WarpVsUniforms;
struct VOut {{
@builtin(position) position: vec4<f32>,
@location(0) diffuse: vec4<f32>,
@location(1) uv: vec4<f32>,
@location(2) rad_ang: vec2<f32>,
}};
@vertex
fn vs_main(@location(0) pos: vec2<f32>, @location(1) rad_ang: vec2<f32>, @location(2) transforms: vec4<f32>,
@location(3) warp_center: vec2<f32>, @location(4) warp_distance: vec2<f32>,
@location(5) stretch: vec2<f32>{vid_param}) -> VOut {{
let radius = rad_ang.x;
let zoom = transforms.x;
let zoom_exp = transforms.y;
let rot = transforms.z;
let warp = transforms.w;
let aspect_x = wu.aspect.x;
let aspect_y = wu.aspect.y;
let inv_aspect_x = wu.aspect.z;
let inv_aspect_y = wu.aspect.w;
let warp_time = wu.timing.x;
let warp_scale_inverse = wu.timing.y;
let texel_offset = wu.timing.zw;
let wf = wu.factors;
var o: VOut;
{position}
let zoom2 = pow(zoom, pow(zoom_exp, radius * 2.0 - 1.0));
let zoom2_inverse = 1.0 / zoom2;
var u = pos.x * aspect_x * 0.5 * zoom2_inverse + 0.5;
var v = pos.y * aspect_y * 0.5 * zoom2_inverse + 0.5;
let uv_original = vec2<f32>(pos.x * 0.5 + 0.5 + texel_offset.x,
pos.y * 0.5 + 0.5 + texel_offset.y);
u = (u - warp_center.x) / stretch.x + warp_center.x;
v = (v - warp_center.y) / stretch.y + warp_center.y;
u += warp * 0.0035 * sin(warp_time * 0.333 + warp_scale_inverse * (pos.x * wf.x - pos.y * wf.w));
v += warp * 0.0035 * cos(warp_time * 0.375 - warp_scale_inverse * (pos.x * wf.z + pos.y * wf.y));
u += warp * 0.0035 * cos(warp_time * 0.753 - warp_scale_inverse * (pos.x * wf.y - pos.y * wf.z));
v += warp * 0.0035 * sin(warp_time * 0.825 + warp_scale_inverse * (pos.x * wf.x + pos.y * wf.w));
let u2 = u - warp_center.x;
let v2 = v - warp_center.y;
let cos_rotation = cos(rot);
let sin_rotation = sin(rot);
u = u2 * cos_rotation - v2 * sin_rotation + warp_center.x;
v = u2 * sin_rotation + v2 * cos_rotation + warp_center.y;
u -= warp_distance.x;
v -= warp_distance.y;
u = (u - 0.5) * inv_aspect_x + 0.5;
v = (v - 0.5) * inv_aspect_y + 0.5;
u += texel_offset.x;
v += texel_offset.y;
o.diffuse = wu.diffuse;
o.uv = vec4<f32>(u, v, uv_original);
o.rad_ang = rad_ang;
return o;
}}
{fs}"#
)
}
pub const TEX_MOD_FS: &str = r#"
@group(0) @binding(0) var tex: texture_2d<f32>;
@group(0) @binding(1) var samp: sampler;
struct FIn {
@builtin(position) position: vec4<f32>,
@location(0) diffuse: vec4<f32>,
@location(1) uv: vec4<f32>,
@location(2) rad_ang: vec2<f32>,
};
@fragment
fn fs_main(in: FIn) -> @location(0) vec4<f32> {
let t = textureSample(tex, samp, in.uv.xy);
return vec4<f32>(t.rgb * in.diffuse.rgb, in.diffuse.a);
}
// projectM (`TexturedDrawFragmentShader`): colour = diffuse * texel, alpha included.
@fragment
fn fs_main_ta(in: FIn) -> @location(0) vec4<f32> {
return in.diffuse * textureSample(tex, samp, in.uv.xy);
}
"#;
pub const FLAT_SRC: &str = r#"
struct VOut {
@builtin(position) position: vec4<f32>,
@location(0) color: vec4<f32>,
};
@vertex
fn vs_main(@location(0) pos: vec2<f32>, @location(1) color: vec4<f32>) -> VOut {
var o: VOut;
o.position = vec4<f32>(pos, 0.0, 1.0);
o.color = color;
return o;
}
@fragment
fn fs_main(in: VOut) -> @location(0) vec4<f32> {
return in.color;
}
"#;
pub const BLUR_SRC: &str = r#"
struct BlurParams {
c0: vec4<f32>,
c1: vec4<f32>,
c2: vec4<f32>,
c3: vec4<f32>,
c5: vec4<f32>,
c6: vec4<f32>,
};
@group(0) @binding(0) var<uniform> bp: BlurParams;
@group(0) @binding(1) var tex: texture_2d<f32>;
@group(0) @binding(2) var samp: sampler;
struct VOut {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
};
@vertex
fn vs_main(@builtin(vertex_index) vi: u32) -> VOut {
// full-screen triangle; uv.y = 0 at the top of the target
var p = array<vec2<f32>, 3>(vec2<f32>(-1.0, -1.0), vec2<f32>(3.0, -1.0), vec2<f32>(-1.0, 3.0));
var o: VOut;
let xy = p[vi];
o.position = vec4<f32>(xy, 0.0, 1.0);
o.uv = vec2<f32>(xy.x * 0.5 + 0.5, 0.5 - xy.y * 0.5);
return o;
}
@fragment
fn fs_blur_h(in: VOut) -> @location(0) vec4<f32> {
// LONG HORIZ. PASS 1 (blur1_ps.fx). "+ moves blur UP, LEFT by 1-pixel increments".
let uv2 = in.uv + bp.c0.zw * vec2<f32>(1.0, bp.c3.w);
let dx = bp.c0.z;
var blur =
(textureSampleLevel(tex, samp, uv2 + vec2<f32>( bp.c2.x * dx, 0.0), 0.0).xyz +
textureSampleLevel(tex, samp, uv2 + vec2<f32>(-bp.c2.x * dx, 0.0), 0.0).xyz) * bp.c1.x +
(textureSampleLevel(tex, samp, uv2 + vec2<f32>( bp.c2.y * dx, 0.0), 0.0).xyz +
textureSampleLevel(tex, samp, uv2 + vec2<f32>(-bp.c2.y * dx, 0.0), 0.0).xyz) * bp.c1.y +
(textureSampleLevel(tex, samp, uv2 + vec2<f32>( bp.c2.z * dx, 0.0), 0.0).xyz +
textureSampleLevel(tex, samp, uv2 + vec2<f32>(-bp.c2.z * dx, 0.0), 0.0).xyz) * bp.c1.z +
(textureSampleLevel(tex, samp, uv2 + vec2<f32>( bp.c2.w * dx, 0.0), 0.0).xyz +
textureSampleLevel(tex, samp, uv2 + vec2<f32>(-bp.c2.w * dx, 0.0), 0.0).xyz) * bp.c1.w;
blur = blur * bp.c3.z;
blur = blur * bp.c3.x + bp.c3.y;
return vec4<f32>(blur, 1.0);
}
@fragment
fn fs_blur_v(in: VOut) -> @location(0) vec4<f32> {
// SHORT VERTICAL PASS 2 (blur2_ps.fx)
let uv2 = in.uv;
let dy = bp.c0.w;
var blur =
(textureSampleLevel(tex, samp, uv2 + vec2<f32>(0.0, bp.c5.z * dy), 0.0).xyz +
textureSampleLevel(tex, samp, uv2 + vec2<f32>(0.0, -bp.c5.z * dy), 0.0).xyz) * bp.c5.x +
(textureSampleLevel(tex, samp, uv2 + vec2<f32>(0.0, bp.c5.w * dy), 0.0).xyz +
textureSampleLevel(tex, samp, uv2 + vec2<f32>(0.0, -bp.c5.w * dy), 0.0).xyz) * bp.c5.y;
blur = blur * bp.c6.x;
// tone it down at the edges (only on the 1st vertical pass)
var t = min(min(in.uv.x, in.uv.y), 1.0 - max(in.uv.x, in.uv.y));
t = sqrt(t);
t = bp.c6.y + bp.c6.z * clamp(t * bp.c6.w, 0.0, 1.0);
blur = blur * t;
return vec4<f32>(blur, 1.0);
}
"#;
pub const BLIT_SRC: &str = r#"
struct BlitParams {
// x,y = src size, z,w = dst size
sizes: vec4<f32>,
// x = 1 when the target is an sRGB format
flags: vec4<f32>,
};
@group(0) @binding(0) var<uniform> bp: BlitParams;
@group(0) @binding(1) var tex: texture_2d<f32>;
@vertex
fn vs_main(@builtin(vertex_index) vi: u32) -> @builtin(position) vec4<f32> {
var p = array<vec2<f32>, 3>(vec2<f32>(-1.0, -1.0), vec2<f32>(3.0, -1.0), vec2<f32>(-1.0, 3.0));
return vec4<f32>(p[vi], 0.0, 1.0);
}
fn srgb_to_linear(c: vec3<f32>) -> vec3<f32> {
let lo = c / 12.92;
let hi = pow((c + vec3<f32>(0.055)) / 1.055, vec3<f32>(2.4));
return select(hi, lo, c <= vec3<f32>(0.04045));
}
@fragment
fn fs_main(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> {
let src = vec2<i32>(floor(pos.xy * bp.sizes.xy / bp.sizes.zw));
let c = textureLoad(tex, clamp(src, vec2<i32>(0), vec2<i32>(bp.sizes.xy) - vec2<i32>(1)), 0);
var rgb = c.rgb;
if (bp.flags.x > 0.5) {
rgb = srgb_to_linear(rgb);
}
return vec4<f32>(rgb, 1.0);
}
"#;