Skip to content

Instantly share code, notes, and snippets.

@davidar
Last active May 13, 2022 07:07
Show Gist options
  • Save davidar/24429fb91484c0bace7c402f1ac1d1bd to your computer and use it in GitHub Desktop.
Save davidar/24429fb91484c0bace7c402f1ac1d1bd to your computer and use it in GitHub Desktop.
fn isfinite(x: f32) -> bool {
return clamp(x, -3.4e38, 3.4e38) == x;
}
fn hash12(p: float2) -> float {
var p3 = fract(float3(p.xyx) * .1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
@stage(compute) @workgroup_size(16, 16)
fn main_image(@builtin(global_invocation_id) id: uint3) {
// Viewport resolution (in pixels)
let screen_size = uint2(textureDimensions(screen));
// Prevent overdraw for workgroups on the edge of the viewport
if (id.x >= screen_size.x || id.y >= screen_size.y) { return; }
// Pixel coordinates (centre of pixel, origin at bottom left)
let fragCoord = float2(float(id.x) + .5, float(screen_size.y - id.y) - .5);
// Normalised pixel coordinates (from 0 to 1)
let uv = fragCoord / float2(screen_size);
// Time varying pixel colour
var col = .5 + .5 * cos(time.elapsed + uv.xyxx + float4(0.,2.,4.,0.));
col.x /= floor(3. * hash12(fragCoord));
col.z -= hash12(fragCoord);
col.z = log(col.z);
if (time.frame % 199u == 0u) { col.w = col.x; }
assert(0, isfinite(col.x)); // inf
assert(1, isfinite(col.y));
assert(2, isfinite(col.z)); // nan
assert(3, isfinite(col.w)); // rare
// Output to screen (linear colour space)
textureStore(screen, int2(id.xy), col);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment