Skip to content

Commit

Permalink
Merge pull request compute-toys#33 from compute-toys/bin
Browse files Browse the repository at this point in the history
Native binary
  • Loading branch information
davidar authored Apr 10, 2023
2 parents 632afde + 20c49ba commit 4f41c0b
Show file tree
Hide file tree
Showing 53 changed files with 9,012 additions and 77 deletions.
1,591 changes: 1,571 additions & 20 deletions Cargo.lock

Large diffs are not rendered by default.

36 changes: 24 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ default = ["console_error_panic_hook"]

[dependencies]
wasm-bindgen = "0.2.84"
wasm-bindgen-futures = "0.4.31"

winit = "0.27.1"
pollster = "0.2.5"
async-executor = "1.4.1"
bytemuck = { version = "1.9.1", features = ["derive"] }
log = "0.4.17"
env_logger = "0.9.0"
Expand All @@ -29,13 +29,6 @@ futures-intrusive = "0.4.0"
async-recursion = "1.0.0"
snailquote = "0.3.1"

console_log = "0.2.0"
js-sys = "0.3.57"
wasm-bindgen-futures = "0.4.31"
web-sys = "0.3.57"
gloo-utils = "0.1.4"
gloo-net = "0.2.1"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
Expand All @@ -49,16 +42,35 @@ console_error_panic_hook = { version = "0.1.7", optional = true }
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.5", optional = true }

[dependencies.wgpu]
#git = "https://github.com/gfx-rs/wgpu"
[target.'cfg(target_arch = "wasm32")'.dependencies]
console_log = "0.2.0"
js-sys = "0.3.57"
web-sys = "0.3.57"
gloo-utils = "0.1.4"
gloo-net = "0.2.1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
reqwest = "0.11.16"
reqwest-middleware = "0.1"
reqwest-middleware-cache = "0.1"
tokio = { version = "1.27.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

# https://github.com/gfx-rs/wgpu/issues/3430
[target.'cfg(target_arch = "wasm32")'.dependencies.wgpu]
version = "0.14.2"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.wgpu]
version = "0.15.1"

[dependencies.naga]
git = "https://github.com/gfx-rs/naga"
rev = "99a7773e65f855ecdefc6fc0fffa6f0aacd3f867"
version = "0.11.0"
features = ["wgsl-in"]

[patch.crates-io.naga]
git = "https://github.com/gfx-rs/naga"
rev = "99a7773e65f855ecdefc6fc0fffa6f0aacd3f867"

[dependencies.image]
version = "0.24.2"
default-features = false
Expand Down
34 changes: 34 additions & 0 deletions examples/assert.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <float>

#include "Dave_Hoskins/hash"

@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, !isinf(col.x));
assert(1, isfinite(col.y));
assert(2, !isnan(col.z));
assert(3, isnormal(col.w));

// Output to screen (linear colour space)
textureStore(screen, int2(id.xy), col);
}
136 changes: 136 additions & 0 deletions examples/cornusammonis/3d-colorspace-projection.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#storage atomic_storage array<atomic<i32>>

alias float4x4 = mat4x4<f32>;

fn rotXW(t: float) -> float4x4 {
return float4x4(
1.0, 0.0, 0.0, 0.0,
0.0, cos(t), sin(t), 0.0,
0.0, - sin(t), cos(t), 0.0,
0.0, 0.0, 0.0, 1.0
);
}
fn rotXY(t: float) -> float4x4 {
return float4x4(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, cos(t), sin(t),
0.0, 0.0, - sin(t), cos(t)
);
}
fn rotXZ(t: float) -> float4x4 {
return float4x4(
1.0, 0.0, 0.0, 0.0,
0.0, cos(t), 0.0, sin(t),
0.0, 0.0, 1.0, 0.0,
0.0, - sin(t), 0.0, cos(t)
);
}
fn rotYZ(t: float) -> float4x4 {
return float4x4(
cos(t), 0.0, 0.0, sin(t),
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
- sin(t), 0.0, 0.0, cos(t)
);
}
fn rotYW(t: float) -> float4x4 {
return float4x4(
cos(t), 0.0, sin(t), 0.0,
0.0, 1.0, 0.0, 0.0,
- sin(t), 0.0, cos(t), 0.0,
0.0, 0.0, 0.0, 1.0
);
}
fn rotZW(t: float) -> float4x4 {
return float4x4(
cos(t), sin(t), 0.0, 0.0,
- sin(t), cos(t), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
);
}

fn matrot(p: float4) -> float4 {
let t = float(time.frame)/240.;
let m = rotZW(t) * rotXW(t*1.3) * rotYW(t*1.6);
return m * p;
}

fn toScreen(p : float4, R : float2) -> float2 {
let q = matrot(p - 0.5).xy;
return (0.5*q+0.5) * (R) * float2(R.y/R.x,1.) + float2(0.25*R.x,0.);
}

@compute @workgroup_size(16, 16)
fn clear(@builtin(global_invocation_id) id: uint3) {
let screen_size = int2(textureDimensions(screen));
let idx0 = int(id.x) + int(screen_size.x * int(id.y));

atomicStore(&atomic_storage[idx0*4+0], 0);
atomicStore(&atomic_storage[idx0*4+1], 0);
atomicStore(&atomic_storage[idx0*4+2], 0);
atomicStore(&atomic_storage[idx0*4+3], 0);
}

@compute @workgroup_size(16, 16)
fn project(@builtin(global_invocation_id) id: uint3) {
// Viewport resolution (in pixels)
let screen_size = int2(textureDimensions(screen));
let screen_size_f = float2(screen_size);

// Prevent overdraw for workgroups on the edge of the viewport
if (id.x >= uint(screen_size.x) || id.y >= uint(screen_size.y)) { return; }

// Pixel coordinates (centre of pixel, origin at bottom left)
let fragCoord = float2(float(id.x) + .5, float(id.y) + .5);

// Normalised pixel coordinates (from 0 to 1)
let uv = fragCoord / screen_size_f;

let col = textureSampleLevel(channel0, bilinear, uv, 0.).xyz;
let pos = sqrt(col);

let screenCoord = clamp(int2(toScreen(float4(pos,0.), screen_size_f)),int2(0,0),screen_size);

let idx1 = int(screenCoord.x) + int(screen_size.x * screenCoord.y);

atomicAdd(&atomic_storage[idx1*4+0], int(256. * col.x));
atomicAdd(&atomic_storage[idx1*4+1], int(256. * col.y));
atomicAdd(&atomic_storage[idx1*4+2], int(256. * col.z));
atomicAdd(&atomic_storage[idx1*4+3], 1);
}

@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(id.y) + .5);

let idx = int(id.x) + int(screen_size.x * id.y);

let count = atomicLoad(&atomic_storage[idx*4+3]);
let x = float(atomicLoad(&atomic_storage[idx*4+0]))/(float(count)*256.0);
let y = float(atomicLoad(&atomic_storage[idx*4+1]))/(float(count)*256.0);
let z = float(atomicLoad(&atomic_storage[idx*4+2]))/(float(count)*256.0);


let projectCol = float3(x,y,z);

// Normalised pixel coordinates (from 0 to 1)
let uv = fragCoord / float2(screen_size);

let col = textureSampleLevel(channel0, bilinear, uv, 0.).xyz;

let result = select(col, projectCol, count > 0);

//let result = col;

// Output to screen (linear colour space)
textureStore(screen, int2(id.xy), float4(result, 1.));
}
11 changes: 11 additions & 0 deletions examples/cornusammonis/3d-colorspace-projection.wgsl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"uniforms": [],
"textures": [
{
"img": "/textures/london.jpg"
},
{
"img": "/textures/blank.png"
}
]
}
Loading

0 comments on commit 4f41c0b

Please sign in to comment.