Skip to content

Commit ec46110

Browse files
committed
wgpu: add api choice wgpu
1 parent 3e2e710 commit ec46110

18 files changed

Lines changed: 489 additions & 3 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[workspace]
2+
members = [
3+
"mygraphics",
4+
"mygraphics-shaders"
5+
]
6+
resolver = "3"
7+
8+
[workspace.package]
9+
version = "0.1.0"
10+
authors = ["generated <generated>"]
11+
edition = "2024"
12+
license = "MIT"
13+
repository = ""
14+
15+
[workspace.lints.rust]
16+
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spirv"))'] }
17+
18+
[workspace.dependencies]
19+
cargo-gpu = { git = "https://github.com/Rust-GPU/cargo-gpu", rev = "bf24eb6060e0c7b0013eceddd23b5d7cee68f4cc" }
20+
spirv-std = { git = "https://github.com/Rust-GPU/rust-gpu", rev = "6fb875068d0d35b1a81ae89b73cf7a464f8c60f7" }
21+
22+
glam = { version = "0.30.9", default-features = false }
23+
bytemuck = { version = "1.24.0", features = ["derive"] }
24+
ash = "0.38"
25+
ash-window = "0.13"
26+
raw-window-handle = "0.6.2"
27+
winit = "0.30.0"
28+
cfg-if = "1.0.0"
29+
anyhow = "1.0.98"
30+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "mygraphics-shaders"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lints]
7+
workspace = true
8+
9+
[lib]
10+
crate-type = ["lib", "dylib"]
11+
12+
[dependencies]
13+
spirv-std.workspace = true
14+
glam.workspace = true
15+
bytemuck.workspace = true
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![no_std]
2+
3+
use bytemuck::{Pod, Zeroable};
4+
use core::f32::consts::PI;
5+
use glam::{Vec3, Vec4, vec2, vec3};
6+
#[cfg(target_arch = "spirv")]
7+
use spirv_std::num_traits::Float;
8+
use spirv_std::spirv;
9+
10+
#[derive(Copy, Clone, Pod, Zeroable)]
11+
#[repr(C)]
12+
pub struct ShaderConstants {
13+
pub width: u32,
14+
pub height: u32,
15+
pub time: f32,
16+
}
17+
18+
#[spirv(fragment)]
19+
pub fn main_fs(vtx_color: Vec3, output: &mut Vec4) {
20+
*output = Vec4::from((vtx_color, 1.));
21+
}
22+
23+
#[spirv(vertex)]
24+
pub fn main_vs(
25+
#[spirv(vertex_index)] vert_id: i32,
26+
#[spirv(push_constant)] constants: &ShaderConstants,
27+
#[spirv(position)] vtx_pos: &mut Vec4,
28+
vtx_color: &mut Vec3,
29+
) {
30+
let speed = 0.4;
31+
let time = constants.time * speed + vert_id as f32 * (2. * PI * 120. / 360.);
32+
let position = vec2(f32::sin(time), f32::cos(time));
33+
*vtx_pos = Vec4::from((position, 0.0, 1.0));
34+
35+
*vtx_color = [vec3(1., 0., 0.), vec3(0., 1., 0.), vec3(0., 0., 1.)][vert_id as usize % 3];
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "mygraphics"
3+
publish = false
4+
version.workspace = true
5+
authors.workspace = true
6+
edition.workspace = true
7+
license.workspace = true
8+
repository.workspace = true
9+
10+
[lints]
11+
workspace = true
12+
13+
[dependencies]
14+
mygraphics-shaders = { path = "../mygraphics-shaders" }
15+
16+
ash.workspace = true
17+
ash-window.workspace = true
18+
raw-window-handle.workspace = true
19+
winit.workspace = true
20+
anyhow.workspace = true
21+
bytemuck.workspace = true
22+
23+
[build-dependencies]
24+
cargo-gpu.workspace = true
25+
anyhow.workspace = true
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use cargo_gpu::Install;
2+
use cargo_gpu::spirv_builder::{MetadataPrintout, ShaderPanicStrategy};
3+
use std::path::PathBuf;
4+
5+
pub fn main() -> anyhow::Result<()> {
6+
let manifest_dir = env!("CARGO_MANIFEST_DIR");
7+
let crate_path = [manifest_dir, "..", "mygraphics-shaders"]
8+
.iter()
9+
.copied()
10+
.collect::<PathBuf>();
11+
12+
let install = Install::from_shader_crate(crate_path.clone()).run()?;
13+
let mut builder = install.to_spirv_builder(crate_path, "spirv-unknown-vulkan1.3");
14+
builder.print_metadata = MetadataPrintout::DependencyOnly;
15+
builder.shader_panic_strategy = ShaderPanicStrategy::DebugPrintfThenExit {
16+
print_inputs: true,
17+
print_backtrace: true,
18+
};
19+
20+
let compile_result = builder.build()?;
21+
let spv_path = compile_result.module.unwrap_single();
22+
println!("cargo::rustc-env=SHADER_SPV_PATH={}", spv_path.display());
23+
Ok(())
24+
}

graphics/mygraphics/src/main.rs renamed to generated/graphics/wgpu/cargo-gpu/mygraphics/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@
7070
// crate-specific exceptions:
7171
// #![allow()]
7272

73-
pub mod ash_renderer;
73+
pub mod wgpu_renderer;
7474

7575
pub fn main() -> anyhow::Result<()> {
76-
ash_renderer::main()
76+
wgpu_renderer::main()
7777
}
7878

7979
pub fn enable_debug_layer() -> bool {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub fn main() -> anyhow::Result<()> {
2+
println!("wgpu renderer");
3+
Ok(())
4+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[workspace]
2+
members = [
3+
"mygraphics",
4+
"mygraphics-shaders"
5+
]
6+
resolver = "3"
7+
8+
[workspace.package]
9+
version = "0.1.0"
10+
authors = ["generated <generated>"]
11+
edition = "2024"
12+
license = "MIT"
13+
repository = ""
14+
15+
[workspace.lints.rust]
16+
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spirv"))'] }
17+
18+
[workspace.dependencies]
19+
# The version of the dependencies `spirv-builder` and `spirv-std` must match exactly!
20+
spirv-builder = { git = "https://github.com/Rust-GPU/rust-gpu", rev = "6fb875068d0d35b1a81ae89b73cf7a464f8c60f7" }
21+
spirv-std = { git = "https://github.com/Rust-GPU/rust-gpu", rev = "6fb875068d0d35b1a81ae89b73cf7a464f8c60f7" }
22+
23+
glam = { version = "0.30.9", default-features = false }
24+
bytemuck = { version = "1.24.0", features = ["derive"] }
25+
ash = "0.38"
26+
ash-window = "0.13"
27+
raw-window-handle = "0.6.2"
28+
winit = "0.30.0"
29+
cfg-if = "1.0.0"
30+
anyhow = "1.0.98"
31+
32+
# Optimize build scripts, copied from rust-gpu's repo
33+
# Enable incremental by default in release mode.
34+
[profile.release]
35+
incremental = true
36+
# HACK(eddyb) this is the default but without explicitly specifying it, Cargo
37+
# will treat the identical settings in `[profile.release.build-override]` below
38+
# as different sets of `rustc` flags and will not reuse artifacts between them.
39+
codegen-units = 256
40+
41+
# Compile build-dependencies in release mode with the same settings
42+
# as regular dependencies (including the incremental enabled above).
43+
[profile.release.build-override]
44+
opt-level = 3
45+
incremental = true
46+
codegen-units = 256
47+
48+
# HACK(eddyb) reduce the number of linker exports and/or imports, by avoiding
49+
# inter-CGU linkage, to stay under the 64Ki MSVC limit for `rustc_codegen_spirv`
50+
# when building it in "debug mode" (only relevant to CI for now, realistically),
51+
# i.e. working around this issue: https://github.com/rust-lang/rust/issues/53014.
52+
[profile.dev]
53+
# HACK(eddyb) fewer inter-crate exports/imports (not just inter-CGU), but sadly
54+
# not configurable w/o breaking `Cargo.toml` parsing from non-nightly Cargo.
55+
#
56+
# rustflags = ["-Zshare-generics=off"]
57+
codegen-units = 1
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "mygraphics-shaders"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lints]
7+
workspace = true
8+
9+
[lib]
10+
crate-type = ["lib", "dylib"]
11+
12+
[dependencies]
13+
spirv-std.workspace = true
14+
glam.workspace = true
15+
bytemuck.workspace = true
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![no_std]
2+
3+
use bytemuck::{Pod, Zeroable};
4+
use core::f32::consts::PI;
5+
use glam::{Vec3, Vec4, vec2, vec3};
6+
#[cfg(target_arch = "spirv")]
7+
use spirv_std::num_traits::Float;
8+
use spirv_std::spirv;
9+
10+
#[derive(Copy, Clone, Pod, Zeroable)]
11+
#[repr(C)]
12+
pub struct ShaderConstants {
13+
pub width: u32,
14+
pub height: u32,
15+
pub time: f32,
16+
}
17+
18+
#[spirv(fragment)]
19+
pub fn main_fs(vtx_color: Vec3, output: &mut Vec4) {
20+
*output = Vec4::from((vtx_color, 1.));
21+
}
22+
23+
#[spirv(vertex)]
24+
pub fn main_vs(
25+
#[spirv(vertex_index)] vert_id: i32,
26+
#[spirv(push_constant)] constants: &ShaderConstants,
27+
#[spirv(position)] vtx_pos: &mut Vec4,
28+
vtx_color: &mut Vec3,
29+
) {
30+
let speed = 0.4;
31+
let time = constants.time * speed + vert_id as f32 * (2. * PI * 120. / 360.);
32+
let position = vec2(f32::sin(time), f32::cos(time));
33+
*vtx_pos = Vec4::from((position, 0.0, 1.0));
34+
35+
*vtx_color = [vec3(1., 0., 0.), vec3(0., 1., 0.), vec3(0., 0., 1.)][vert_id as usize % 3];
36+
}

0 commit comments

Comments
 (0)