start work on compositing, effects

This commit is contained in:
Skyler Lehmkuhl
2025-12-08 09:30:22 -05:00
parent 420f3bf7b9
commit 7eb61ab0a8
31 changed files with 3172 additions and 150 deletions
@@ -0,0 +1,73 @@
// Gaussian Blur Effect Shader
// Simple box blur approximation (real Gaussian would need multiple passes)
struct Uniforms {
// params packed as vec4s for proper 16-byte alignment
params0: vec4<f32>,
params1: vec4<f32>,
params2: vec4<f32>,
params3: vec4<f32>,
texture_width: f32,
texture_height: f32,
time: f32,
mix: f32,
}
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
}
@group(0) @binding(0) var source_tex: texture_2d<f32>;
@group(0) @binding(1) var source_sampler: sampler;
@group(0) @binding(2) var<uniform> uniforms: Uniforms;
@vertex
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
var out: VertexOutput;
let x = f32((vertex_index & 1u) << 1u);
let y = f32(vertex_index & 2u);
out.position = vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
out.uv = vec2<f32>(x, y);
return out;
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let src = textureSample(source_tex, source_sampler, in.uv);
let radius = uniforms.params0.x; // Blur radius in pixels
let quality = uniforms.params0.y; // Quality (0-1, affects sample count)
if (radius < 0.5) {
return vec4<f32>(mix(src.rgb, src.rgb, uniforms.mix), src.a);
}
let pixel_size = vec2<f32>(1.0 / uniforms.texture_width, 1.0 / uniforms.texture_height);
// Sample count based on quality (5-13 samples per direction)
let samples = i32(5.0 + quality * 8.0);
let half_samples = samples / 2;
var color = vec3<f32>(0.0);
var total_weight = 0.0;
// Simple box blur with gaussian-like weighting
for (var y = -half_samples; y <= half_samples; y++) {
for (var x = -half_samples; x <= half_samples; x++) {
let offset = vec2<f32>(f32(x), f32(y)) * pixel_size * radius / f32(half_samples);
let sample_pos = in.uv + offset;
// Gaussian-like weight based on distance
let dist = length(vec2<f32>(f32(x), f32(y))) / f32(half_samples);
let weight = exp(-dist * dist * 2.0);
color += textureSample(source_tex, source_sampler, sample_pos).rgb * weight;
total_weight += weight;
}
}
color /= total_weight;
let result = mix(src.rgb, color, uniforms.mix);
return vec4<f32>(result, src.a);
}
@@ -0,0 +1,51 @@
// Brightness/Contrast Effect Shader
struct Uniforms {
// params packed as vec4s for proper 16-byte alignment
params0: vec4<f32>,
params1: vec4<f32>,
params2: vec4<f32>,
params3: vec4<f32>,
texture_width: f32,
texture_height: f32,
time: f32,
mix: f32,
}
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
}
@group(0) @binding(0) var source_tex: texture_2d<f32>;
@group(0) @binding(1) var source_sampler: sampler;
@group(0) @binding(2) var<uniform> uniforms: Uniforms;
@vertex
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
var out: VertexOutput;
let x = f32((vertex_index & 1u) << 1u);
let y = f32(vertex_index & 2u);
out.position = vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
out.uv = vec2<f32>(x, y);
return out;
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let src = textureSample(source_tex, source_sampler, in.uv);
let brightness = uniforms.params0.x; // -1 to 1
let contrast = uniforms.params0.y; // 0 to 3
// Apply brightness (additive)
var color = src.rgb + vec3<f32>(brightness);
// Apply contrast (multiply around midpoint 0.5)
color = (color - vec3<f32>(0.5)) * contrast + vec3<f32>(0.5);
// Clamp to valid range
color = clamp(color, vec3<f32>(0.0), vec3<f32>(1.0));
let result = mix(src.rgb, color, uniforms.mix);
return vec4<f32>(result, src.a);
}
@@ -0,0 +1,47 @@
// Color Tint Effect Shader
struct Uniforms {
// params packed as vec4s for proper 16-byte alignment
params0: vec4<f32>,
params1: vec4<f32>,
params2: vec4<f32>,
params3: vec4<f32>,
texture_width: f32,
texture_height: f32,
time: f32,
mix: f32,
}
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
}
@group(0) @binding(0) var source_tex: texture_2d<f32>;
@group(0) @binding(1) var source_sampler: sampler;
@group(0) @binding(2) var<uniform> uniforms: Uniforms;
@vertex
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
var out: VertexOutput;
let x = f32((vertex_index & 1u) << 1u);
let y = f32(vertex_index & 2u);
out.position = vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
out.uv = vec2<f32>(x, y);
return out;
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let src = textureSample(source_tex, source_sampler, in.uv);
// Tint color from params (RGBA in params0)
let tint = uniforms.params0.xyz;
let amount = uniforms.params1.x; // Amount parameter (params[4])
// Apply tint as multiplicative blend
let tinted = src.rgb * mix(vec3<f32>(1.0), tint, amount);
let result = mix(src.rgb, tinted, uniforms.mix);
return vec4<f32>(result, src.a);
}
@@ -0,0 +1,47 @@
// Grayscale Effect Shader
// Converts image to grayscale using luminance weights
struct Uniforms {
// params packed as vec4s for proper 16-byte alignment
params0: vec4<f32>,
params1: vec4<f32>,
params2: vec4<f32>,
params3: vec4<f32>,
texture_width: f32,
texture_height: f32,
time: f32,
mix: f32,
}
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
}
@group(0) @binding(0) var source_tex: texture_2d<f32>;
@group(0) @binding(1) var source_sampler: sampler;
@group(0) @binding(2) var<uniform> uniforms: Uniforms;
// Fullscreen triangle vertex shader
@vertex
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
var out: VertexOutput;
let x = f32((vertex_index & 1u) << 1u);
let y = f32(vertex_index & 2u);
out.position = vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
out.uv = vec2<f32>(x, y);
return out;
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let src = textureSample(source_tex, source_sampler, in.uv);
let amount = uniforms.params0.x; // grayscale amount
// ITU-R BT.709 luminance coefficients
let luminance = dot(src.rgb, vec3<f32>(0.2126, 0.7152, 0.0722));
let gray = vec3<f32>(luminance, luminance, luminance);
let result = mix(src.rgb, gray, amount * uniforms.mix);
return vec4<f32>(result, src.a);
}
@@ -0,0 +1,107 @@
// Hue/Saturation/Lightness Effect Shader
struct Uniforms {
// params packed as vec4s for proper 16-byte alignment
params0: vec4<f32>,
params1: vec4<f32>,
params2: vec4<f32>,
params3: vec4<f32>,
texture_width: f32,
texture_height: f32,
time: f32,
mix: f32,
}
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
}
@group(0) @binding(0) var source_tex: texture_2d<f32>;
@group(0) @binding(1) var source_sampler: sampler;
@group(0) @binding(2) var<uniform> uniforms: Uniforms;
@vertex
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
var out: VertexOutput;
let x = f32((vertex_index & 1u) << 1u);
let y = f32(vertex_index & 2u);
out.position = vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
out.uv = vec2<f32>(x, y);
return out;
}
// Convert RGB to HSL
fn rgb_to_hsl(c: vec3<f32>) -> vec3<f32> {
let cmax = max(max(c.r, c.g), c.b);
let cmin = min(min(c.r, c.g), c.b);
let delta = cmax - cmin;
var h = 0.0;
var s = 0.0;
let l = (cmax + cmin) / 2.0;
if (delta > 0.0) {
s = select(delta / (2.0 - cmax - cmin), delta / (cmax + cmin), l < 0.5);
if (cmax == c.r) {
h = (c.g - c.b) / delta + select(6.0, 0.0, c.g >= c.b);
} else if (cmax == c.g) {
h = (c.b - c.r) / delta + 2.0;
} else {
h = (c.r - c.g) / delta + 4.0;
}
h /= 6.0;
}
return vec3<f32>(h, s, l);
}
// Helper function for HSL to RGB
fn hue_to_rgb(p: f32, q: f32, t: f32) -> f32 {
var tt = t;
if (tt < 0.0) { tt += 1.0; }
if (tt > 1.0) { tt -= 1.0; }
if (tt < 1.0/6.0) { return p + (q - p) * 6.0 * tt; }
if (tt < 1.0/2.0) { return q; }
if (tt < 2.0/3.0) { return p + (q - p) * (2.0/3.0 - tt) * 6.0; }
return p;
}
// Convert HSL to RGB
fn hsl_to_rgb(hsl: vec3<f32>) -> vec3<f32> {
if (hsl.y == 0.0) {
return vec3<f32>(hsl.z, hsl.z, hsl.z);
}
let q = select(hsl.z + hsl.y - hsl.z * hsl.y, hsl.z * (1.0 + hsl.y), hsl.z < 0.5);
let p = 2.0 * hsl.z - q;
return vec3<f32>(
hue_to_rgb(p, q, hsl.x + 1.0/3.0),
hue_to_rgb(p, q, hsl.x),
hue_to_rgb(p, q, hsl.x - 1.0/3.0)
);
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let src = textureSample(source_tex, source_sampler, in.uv);
let hue_shift = uniforms.params0.x / 360.0; // Convert degrees to 0-1 range
let saturation = uniforms.params0.y; // Multiplier (1.0 = no change)
let lightness = uniforms.params0.z; // Additive (-1 to 1)
// Convert to HSL
var hsl = rgb_to_hsl(src.rgb);
// Apply adjustments
hsl.x = fract(hsl.x + hue_shift); // Shift hue (wrapping)
hsl.y = clamp(hsl.y * saturation, 0.0, 1.0); // Multiply saturation
hsl.z = clamp(hsl.z + lightness, 0.0, 1.0); // Add lightness
// Convert back to RGB
let adjusted = hsl_to_rgb(hsl);
let result = mix(src.rgb, adjusted, uniforms.mix);
return vec4<f32>(result, src.a);
}
@@ -0,0 +1,45 @@
// Invert Effect Shader
// Inverts color values
struct Uniforms {
// params packed as vec4s for proper 16-byte alignment
// params[0-3] in params0, params[4-7] in params1, etc.
params0: vec4<f32>,
params1: vec4<f32>,
params2: vec4<f32>,
params3: vec4<f32>,
texture_width: f32,
texture_height: f32,
time: f32,
mix: f32,
}
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
}
@group(0) @binding(0) var source_tex: texture_2d<f32>;
@group(0) @binding(1) var source_sampler: sampler;
@group(0) @binding(2) var<uniform> uniforms: Uniforms;
@vertex
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
var out: VertexOutput;
let x = f32((vertex_index & 1u) << 1u);
let y = f32(vertex_index & 2u);
out.position = vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
out.uv = vec2<f32>(x, y);
return out;
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let src = textureSample(source_tex, source_sampler, in.uv);
let amount = uniforms.params0.x; // params[0]
let inverted = vec3<f32>(1.0) - src.rgb;
let result = mix(src.rgb, inverted, amount * uniforms.mix);
return vec4<f32>(result, src.a);
}
@@ -0,0 +1,60 @@
// Sharpen Effect Shader
// Unsharp mask style sharpening
struct Uniforms {
// params packed as vec4s for proper 16-byte alignment
params0: vec4<f32>,
params1: vec4<f32>,
params2: vec4<f32>,
params3: vec4<f32>,
texture_width: f32,
texture_height: f32,
time: f32,
mix: f32,
}
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
}
@group(0) @binding(0) var source_tex: texture_2d<f32>;
@group(0) @binding(1) var source_sampler: sampler;
@group(0) @binding(2) var<uniform> uniforms: Uniforms;
@vertex
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
var out: VertexOutput;
let x = f32((vertex_index & 1u) << 1u);
let y = f32(vertex_index & 2u);
out.position = vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
out.uv = vec2<f32>(x, y);
return out;
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let src = textureSample(source_tex, source_sampler, in.uv);
let amount = uniforms.params0.x; // Sharpen amount (0-3)
let radius = uniforms.params0.y; // Sample radius (0.5-5)
let pixel_size = vec2<f32>(1.0 / uniforms.texture_width, 1.0 / uniforms.texture_height) * radius;
// Sample neighbors for edge detection
let left = textureSample(source_tex, source_sampler, in.uv - vec2<f32>(pixel_size.x, 0.0)).rgb;
let right = textureSample(source_tex, source_sampler, in.uv + vec2<f32>(pixel_size.x, 0.0)).rgb;
let top = textureSample(source_tex, source_sampler, in.uv - vec2<f32>(0.0, pixel_size.y)).rgb;
let bottom = textureSample(source_tex, source_sampler, in.uv + vec2<f32>(0.0, pixel_size.y)).rgb;
// Average of neighbors (blur)
let blur = (left + right + top + bottom) * 0.25;
// Unsharp mask: original + (original - blur) * amount
let sharpened = src.rgb + (src.rgb - blur) * amount;
// Clamp to valid range
let clamped = clamp(sharpened, vec3<f32>(0.0), vec3<f32>(1.0));
let result = mix(src.rgb, clamped, uniforms.mix);
return vec4<f32>(result, src.a);
}
@@ -0,0 +1,55 @@
// Vignette Effect Shader
// Darkens edges of the image
struct Uniforms {
// params packed as vec4s for proper 16-byte alignment
params0: vec4<f32>,
params1: vec4<f32>,
params2: vec4<f32>,
params3: vec4<f32>,
texture_width: f32,
texture_height: f32,
time: f32,
mix: f32,
}
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
}
@group(0) @binding(0) var source_tex: texture_2d<f32>;
@group(0) @binding(1) var source_sampler: sampler;
@group(0) @binding(2) var<uniform> uniforms: Uniforms;
@vertex
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
var out: VertexOutput;
let x = f32((vertex_index & 1u) << 1u);
let y = f32(vertex_index & 2u);
out.position = vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
out.uv = vec2<f32>(x, y);
return out;
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let src = textureSample(source_tex, source_sampler, in.uv);
let radius = uniforms.params0.x; // Vignette radius (0.5 = normal)
let softness = uniforms.params0.y; // Edge softness (0-1)
let amount = uniforms.params0.z; // Darkness amount (0-1)
// Calculate distance from center (normalized to -1 to 1)
let center = vec2<f32>(0.5, 0.5);
let dist = distance(in.uv, center);
// Create vignette factor with smooth falloff
let inner = radius;
let outer = radius + softness;
let vignette = 1.0 - smoothstep(inner, outer, dist) * amount;
let vignetted = src.rgb * vignette;
let result = mix(src.rgb, vignetted, uniforms.mix);
return vec4<f32>(result, src.a);
}