Skip to content

Compute Shaders + ExecutionMode's #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 10, 2020
42 changes: 41 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ members = [
"examples/wgpu-example-runner",
"examples/example-shader",
"examples/wgpu-example-shader",
"examples/wgpu-example-compute-runner",
"examples/wgpu-example-compute-shader",
"rustc_codegen_spirv",
"spirv-builder",
"spirv-std",
Expand Down
25 changes: 25 additions & 0 deletions examples/wgpu-example-compute-runner/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "wgpu-example-compute-runner"
version = "0.1.0"
authors = ["Embark <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"

[dependencies]
wgpu = "0.6.0"
futures = { version = "0.3", default-features = false, features = ["std", "executor"] }
rspirv = "0.7.0"
bytemuck = "1.4.1"
#winit = { version = "0.22.1", features = ["web-sys"] }

[build-dependencies]
spirv-builder = { path = "../../spirv-builder" }

#[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
#wgpu-subscriber = "0.1.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = "=0.3.39"
console_error_panic_hook = "0.1.6"
console_log = "0.2.0"
wasm-bindgen-futures = "0.4.18"
8 changes: 8 additions & 0 deletions examples/wgpu-example-compute-runner/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use spirv_builder::SpirvBuilder;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
// This will set the env var `wgpu-example-compute-shader.spv` to a spir-v file that can be include!()'d
SpirvBuilder::new("../wgpu-example-compute-shader").build()?;
Ok(())
}
74 changes: 74 additions & 0 deletions examples/wgpu-example-compute-runner/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

async fn create_device_queue() -> (wgpu::Device, wgpu::Queue) {
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
compatible_surface: None,
})
.await
.expect("Failed to find an appropriate adapter");

adapter
.request_device(
&wgpu::DeviceDescriptor {
features: wgpu::Features::empty(),
limits: wgpu::Limits::default(),
shader_validation: true,
},
None,
)
.await
.expect("Failed to create device")
}

fn main() {
let (device, queue) = {
#[cfg(not(target_arch = "wasm32"))]
{ futures::executor::block_on(create_device_queue()) }
#[cfg(target_arch = "wasm32")]
{ wasm_bindgen_futures::spawn_local(create_device_queue()) }
};

// Load the shaders from disk
let module = device.create_shader_module(wgpu::include_spirv!(env!("wgpu_example_compute_shader.spv")));

let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
entries: &[],
});

let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});

let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: None,
layout: Some(&pipeline_layout),
compute_stage: wgpu::ProgrammableStageDescriptor {
module: &module,
entry_point: "main_cs",
},
});

let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &bind_group_layout,
entries: &[],
});

let mut encoder = device.create_command_encoder(
&wgpu::CommandEncoderDescriptor { label: None }
);

{
let mut cpass = encoder.begin_compute_pass();
cpass.set_bind_group(0, &bind_group, &[]);
cpass.set_pipeline(&compute_pipeline);
cpass.dispatch(1, 1, 1);
}

queue.submit(Some(encoder.finish()));
}
15 changes: 15 additions & 0 deletions examples/wgpu-example-compute-shader/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "wgpu-example-compute-shader"
version = "0.1.0"
authors = ["Embark <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"

[lib]
crate-type = ["dylib"]
# Note: Don't include these two lines in your own crates, these are just included here because our repo setup is weird.
doctest = false
test = false

[dependencies]
spirv-std = { path = "../../spirv-std" }
20 changes: 20 additions & 0 deletions examples/wgpu-example-compute-shader/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![cfg_attr(target_arch = "spirv", no_std)]
#![feature(lang_items)]
#![feature(register_attr)]
#![register_attr(spirv)]

#[cfg(not(test))]
use core::panic::PanicInfo;
extern crate spirv_std;

#[spirv(gl_compute(local_size_x=64))]
pub fn main_cs() {

}

#[cfg(not(test))]
#[panic_handler]
fn panic(_: &PanicInfo) -> ! {
loop {}
}

4 changes: 2 additions & 2 deletions rustc_codegen_spirv/src/codegen_cx/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ impl<'tcx> PreDefineMethods<'tcx> for CodegenCx<'tcx> {

for attr in parse_attrs(self, self.tcx.get_attrs(instance.def_id())) {
match attr {
SpirvAttribute::Entry(execution_model) => self.entry_stub(
SpirvAttribute::Entry(entry) => self.entry_stub(
&instance,
&fn_abi,
declared,
human_name.clone(),
execution_model,
entry,
),
SpirvAttribute::ReallyUnsafeIgnoreBitcasts => {
self.really_unsafe_ignore_bitcasts
Expand Down
Loading