Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ vendor/wgpu/
/shard.lock

# Example Artifacts
/examples/compute
/examples/compute.app
/examples/headless
/examples/headless.app
/examples/red.png
Expand Down
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,21 @@ else
env LD_LIBRARY_PATH=${CWD}/bin/libs examples/triangle
endif
.PHONY: example-triangle

example-compute: vendor-libs
crystal build examples/compute.cr -o examples/compute
ifeq (${OS},Darwin)
@echo "Fixing up libwgpu_native dylib path…"
@install_name_tool -change /Users/runner/work/wgpu-native/wgpu-native/target/debug/deps/libwgpu_native.dylib @executable_path/../../Frameworks/libwgpu_native.dylib examples/compute
@otool -L examples/compute | grep wgpu
@rm -rf "examples/compute.app"
@scripts/appify.sh examples/compute
@mkdir -p "compute.app/Frameworks"
@cp bin/libs/libwgpu_native.dylib "compute.app/Frameworks"
@cp examples/Info.plist "compute.app/Contents"
@mv -f "compute.app" examples
@./examples/compute.app/Contents/MacOS/compute
else
env LD_LIBRARY_PATH=${CWD}/bin/libs examples/compute
endif
.PHONY: example-compute
36 changes: 36 additions & 0 deletions examples/assets/compute/shader.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[[block]]
struct PrimeIndices {
data: [[stride(4)]] array<u32>;
}; // this is used as both input and output for convenience

[[group(0), binding(0)]]
var<storage> v_indices: [[access(read_write)]] PrimeIndices;

// The Collatz Conjecture states that for any integer n:
// If n is even, n = n/2
// If n is odd, n = 3n+1
// And repeat this process for each new n, you will always eventually reach 1.
// Though the conjecture has not been proven, no counterexample has ever been found.
// This function returns how many times this recurrence needs to be applied to reach 1.
fn collatz_iterations(n_base: u32) -> u32{
var n: u32 = n_base;
var i: u32 = 0u;
loop {
if (n <= 1u) {
break;
}
if (n % 2u == 0u) {
n = n / 2u;
}
else {
n = 3u * n + 1u;
}
i = i + 1u;
}
return i;
}

[[stage(compute), workgroup_size(1)]]
fn main([[builtin(global_invocation_id)]] global_id: vec3<u32>) {
v_indices.data[global_id.x] = collatz_iterations(v_indices.data[global_id.x]);
}
30 changes: 30 additions & 0 deletions examples/compute.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "baked_file_system"

require "./../src/wgpu"
require "./framework.cr"

class Assets
extend BakedFileSystem
bake_folder "./assets/compute"

def self.shader
Assets.get("/shader.wgsl").gets_to_end
end
end

puts "WebGPU Compute"

Signal::INT.trap { exit }

WGPU.set_log_level WGPU::LogLevel::Warning
WGPU.set_log_callback(->(level : WGPU::LogLevel, message : String) {
puts "#{level}: #{message}"
})

adapter = WGPU::Adapter.request
pp adapter.get.info
device = WGPU::Device.request(adapter.get).get

# TODO: https://github.com/gfx-rs/wgpu-native/tree/v0.9.2.2/examples/compute

numbers = [1, 2, 3, 4]