From 5759f769b0be423efd4f0e32c2fe56dccb7537e8 Mon Sep 17 00:00:00 2001 From: grovesNL Date: Sat, 22 Sep 2018 16:54:19 -0600 Subject: [PATCH 1/8] Add initial bindings --- Cargo.lock | 262 ++++++++++++++++-- Cargo.toml | 2 + examples/Cargo.toml | 21 ++ .../hello_triangle/main.rs | 4 +- .../shaders}/hello_triangle.frag | 0 .../shaders}/hello_triangle.frag.spv | Bin .../shaders}/hello_triangle.vert | 0 .../shaders}/hello_triangle.vert.spv | Bin wgpu-bindings/Cargo.toml | 13 + wgpu-bindings/bindings.h | 54 ++++ wgpu-bindings/src/main.rs | 14 + wgpu-native/Cargo.toml | 7 + wgpu-native/src/binding_model.rs | 10 +- wgpu-native/src/command/compute.rs | 19 +- wgpu-native/src/command/mod.rs | 26 +- wgpu-native/src/command/render.rs | 26 +- wgpu-native/src/device.rs | 34 +-- wgpu-native/src/handle.rs | 113 -------- wgpu-native/src/instance.rs | 35 ++- wgpu-native/src/lib.rs | 49 ++-- wgpu-native/src/pipeline.rs | 10 +- wgpu-native/src/registry.rs | 43 +++ 22 files changed, 481 insertions(+), 261 deletions(-) create mode 100644 examples/Cargo.toml rename wgpu-native/examples/hello_triangle.rs => examples/hello_triangle/main.rs (81%) rename {wgpu-native/examples/data => examples/hello_triangle/shaders}/hello_triangle.frag (100%) rename {wgpu-native/examples/data => examples/hello_triangle/shaders}/hello_triangle.frag.spv (100%) rename {wgpu-native/examples/data => examples/hello_triangle/shaders}/hello_triangle.vert (100%) rename {wgpu-native/examples/data => examples/hello_triangle/shaders}/hello_triangle.vert.spv (100%) create mode 100644 wgpu-bindings/Cargo.toml create mode 100644 wgpu-bindings/bindings.h create mode 100644 wgpu-bindings/src/main.rs delete mode 100644 wgpu-native/src/handle.rs create mode 100644 wgpu-native/src/registry.rs diff --git a/Cargo.lock b/Cargo.lock index c5914c520..64e3e5d6e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,6 +3,14 @@ name = "android_glue" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ash" version = "0.24.4" @@ -13,6 +21,16 @@ dependencies = [ "shared_library 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "atty" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "backtrace" version = "0.3.9" @@ -22,7 +40,7 @@ dependencies = [ "cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -49,6 +67,21 @@ name = "byteorder" version = "1.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "cbindgen" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)", + "tempfile 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cc" version = "1.0.25" @@ -59,6 +92,20 @@ name = "cfg-if" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "clap" +version = "2.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cloudabi" version = "0.0.3" @@ -86,7 +133,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "block 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "core-graphics 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", + "core-graphics 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", "objc 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -118,7 +165,7 @@ dependencies = [ [[package]] name = "core-graphics" -version = "0.17.0" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -155,6 +202,13 @@ name = "either" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "examples" +version = "0.1.0" +dependencies = [ + "wgpu-native 0.1.0", +] + [[package]] name = "failure" version = "0.1.2" @@ -226,7 +280,7 @@ dependencies = [ "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "spirv_cross 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "winit 0.17.2 (registry+https://github.com/rust-lang/crates.io-index)", "wio 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -247,7 +301,7 @@ dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "block 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "cocoa 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", - "core-graphics 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", + "core-graphics 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)", "foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "gfx-hal 0.1.0 (git+https://github.com/gfx-rs/gfx?rev=a435a05)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -255,7 +309,7 @@ dependencies = [ "objc 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", "smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", - "spirv_cross 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)", + "spirv_cross 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)", "storage-map 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "winit 0.17.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -271,7 +325,7 @@ dependencies = [ "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "winit 0.17.2 (registry+https://github.com/rust-lang/crates.io-index)", "x11 2.18.1 (registry+https://github.com/rust-lang/crates.io-index)", "xcb 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -306,6 +360,11 @@ dependencies = [ "either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "itoa" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "lazy_static" version = "0.2.11" @@ -330,7 +389,7 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -364,7 +423,7 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -375,7 +434,7 @@ dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "block 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "cocoa 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", - "core-graphics 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", + "core-graphics 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)", "foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -457,7 +516,7 @@ dependencies = [ "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -500,7 +559,7 @@ dependencies = [ "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -508,11 +567,32 @@ name = "rand_core" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "redox_syscall" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_termios" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "relevant" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "remove_dir_all" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rustc-demangle" version = "0.1.9" @@ -526,6 +606,11 @@ dependencies = [ "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ryu" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "scopeguard" version = "0.3.3" @@ -544,6 +629,43 @@ name = "semver-parser" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "serde" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive_internals 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive_internals" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_json" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", + "ryu 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "shared_library" version = "0.1.9" @@ -587,7 +709,7 @@ dependencies = [ [[package]] name = "spirv_cross" -version = "0.11.1" +version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", @@ -606,6 +728,11 @@ dependencies = [ "lock_api 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "strsim" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "syn" version = "0.10.8" @@ -615,6 +742,16 @@ dependencies = [ "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "syn" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "syn" version = "0.14.9" @@ -625,6 +762,14 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "synom" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "synstructure" version = "0.9.0" @@ -636,6 +781,50 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tempfile" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "termion" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "textwrap" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "toml" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-width" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-xid" version = "0.0.4" @@ -654,6 +843,11 @@ dependencies = [ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "vec_map" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "version_check" version = "0.1.4" @@ -714,6 +908,13 @@ dependencies = [ "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "wgpu-bindings" +version = "0.1.0" +dependencies = [ + "cbindgen 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "wgpu-native" version = "0.1.0" @@ -725,11 +926,12 @@ dependencies = [ "gfx-backend-vulkan 0.1.0 (git+https://github.com/gfx-rs/gfx?rev=a435a05)", "gfx-hal 0.1.0 (git+https://github.com/gfx-rs/gfx?rev=a435a05)", "gfx-memory 0.1.0 (git+https://github.com/gfx-rs/gfx-memory?rev=483d64d)", + "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "winapi" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -763,7 +965,7 @@ dependencies = [ "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "smithay-client-toolkit 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "wayland-client 0.20.12 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "x11-dl 2.18.3 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -772,7 +974,7 @@ name = "wio" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -813,21 +1015,25 @@ dependencies = [ [metadata] "checksum android_glue 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "000444226fcff248f2bc4c7625be32c63caccfecc2723a2b9f78a7487a49c407" +"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" "checksum ash 0.24.4 (registry+https://github.com/rust-lang/crates.io-index)" = "11f080bc0414ee1b6b959442cb36478d56c6e6b9bb2b04079a5048d9acc91a30" +"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "89a47830402e9981c5c41223151efcced65a0510c13097c769cede7efb34782a" "checksum backtrace-sys 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)" = "c66d56ac8dabd07f6aacdaf633f4b8262f5b3601a810a0dcddffd5c22c69daa0" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" "checksum block 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" "checksum byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "90492c5858dd7d2e78691cfb89f90d273a2800fc11d98f60786e5d87e2f83781" +"checksum cbindgen 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f1f920442b99c6eb6208905a2b71b055e4c8f170287fbe7c7ceb9b6cd206878d" "checksum cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)" = "f159dfd43363c4d08055a07703eb7a3406b0dac4d0584d96965a3262db3c9d16" "checksum cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e7bb64a8ebb0d856483e1e682ea3422f883c5f5615a90d51a2c82fe87fdd3" +"checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum cocoa 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f5cd1afb83b2de9c41e5dfedb2bcccb779d433b958404876009ae4b01746ff23" "checksum cocoa 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)" = "53a840785348e998a1433d1f9d0b350fd83e91711fae8507c76ce510afc77e72" "checksum core-foundation 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cc3532ec724375c7cb7ff0a097b714fde180bb1f6ed2ab27cfcd99ffca873cd2" "checksum core-foundation-sys 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a3fb15cdbdd9cf8b82d97d0296bb5cd3631bba58d6e31650a002a8e7fb5721f9" "checksum core-graphics 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "92801c908ea6301ae619ed842a72e01098085fc321b9c2f3f833dad555bba055" -"checksum core-graphics 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de51fc45f0de4bc07e7ecdb172f0559e0f19ca016a0059577a149b11a2f05324" +"checksum core-graphics 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)" = "62ceafe1622ffc9a332199096841d0ff9912ec8cf8f9cde01e254a7d5217cd10" "checksum derivative 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "67b3d6d0e84e53a5bdc263cc59340541877bb541706a191d762bfac6a481bdde" "checksum dlib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "77e51249a9d823a4cb79e3eca6dcd756153e8ed0157b6c04775d04bf1b13b76a" "checksum downcast-rs 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "18df8ce4470c189d18aa926022da57544f31e154631eb4cfe796aea97051fe6c" @@ -847,6 +1053,7 @@ dependencies = [ "checksum gfx-hal 0.1.0 (git+https://github.com/gfx-rs/gfx?rev=a435a05)" = "" "checksum gfx-memory 0.1.0 (git+https://github.com/gfx-rs/gfx-memory?rev=483d64d)" = "" "checksum itertools 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4833d6978da405305126af4ac88569b5d71ff758581ce5a987dbfa3755f694fc" +"checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" "checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" "checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7" "checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" @@ -871,25 +1078,42 @@ dependencies = [ "checksum quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "dd636425967c33af890042c483632d33fa7a18f19ad1d7ea72e8998c6ef8dea5" "checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c" "checksum rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "edecf0f94da5551fc9b492093e30b041a891657db7940ee221f9d2f66e82eef2" +"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" +"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum relevant 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c2b087c662aa6d36c6bb22c44a97911196ffd4a1df324c1fabdbcb80d18b0302" +"checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" "checksum rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "bcfe5b13211b4d78e5c2cadfebd7769197d95c639c35a50057eb4c05de811395" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +"checksum ryu 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7153dd96dade874ab973e098cb62fcdbb89a03682e46b144fd09550998d4a4a7" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +"checksum serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)" = "84257ccd054dc351472528c8587b4de2dbf0dc0fe2e634030c1a90bfdacebaa9" +"checksum serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)" = "652bc323d694dc925829725ec6c890156d8e70ae5202919869cb00fe2eff3788" +"checksum serde_derive_internals 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "32f1926285523b2db55df263d2aa4eb69ddcfa7a7eade6430323637866b513ab" +"checksum serde_json 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "d30ec34ac923489285d24688c7a9c0898d16edff27fc1f1bd854edeff6ca3b7f" "checksum shared_library 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5a9e7e0f2bfae24d8a5b5a66c5b257a83c7412304311512a0c054cd5e619da11" "checksum smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "153ffa32fd170e9944f7e0838edf824a754ec4c1fc64746fcc9fe1f8fa602e5d" "checksum smithay-client-toolkit 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f1609083d6bca3991a3c648d80ae16e1764d70881c3321bee1c915149073d605" "checksum spirv_cross 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1c2c0be48967fec392de4749ac34a2da758aaa359ca648abaa47b90b36845013" -"checksum spirv_cross 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e1b0b4e01f61e50a6e223fde32934883df4086004174a1fc522fc97bda2ce938" +"checksum spirv_cross 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2cc3aef2f7822a4fdd4174f547700f208bbc0f0871c59b754573717c92fd29f4" "checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" "checksum storage-map 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "cb94f167ccba0941876c8e722e888be8b4c05511ffdacc8cfcd4c647adfd424d" +"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum syn 0.10.8 (registry+https://github.com/rust-lang/crates.io-index)" = "58fd09df59565db3399efbba34ba8a2fec1307511ebd245d0061ff9d42691673" +"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)" = "261ae9ecaa397c42b960649561949d69311f08eeaea86a65696e6e46517cf741" +"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum synstructure 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "85bb9b7550d063ea184027c9b8c20ac167cd36d3e06b3a40bceb9d746dc1a7b7" +"checksum tempfile 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "55c1195ef8513f3273d55ff59fe5da6940287a0d7a98331254397f464833675b" +"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" +"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" +"checksum toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a0263c6c02c4db6c8f7681f9fd35e90de799ebd4cfdeab77a38f4ff6b3d8c0d9" +"checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" +"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "checksum version_check 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7716c242968ee87e5542f8021178248f267f295a5c4803beae8b8b7fd9bc6051" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" "checksum wayland-client 0.20.12 (registry+https://github.com/rust-lang/crates.io-index)" = "e7516a23419a55bd2e6d466c75a6a52c85718e5013660603289c2b8bee794b12" @@ -897,7 +1121,7 @@ dependencies = [ "checksum wayland-protocols 0.20.12 (registry+https://github.com/rust-lang/crates.io-index)" = "bd4d31a96be6ecdbaddbf35200f5af2daee01be592afecd8feaf443d417e9230" "checksum wayland-scanner 0.20.12 (registry+https://github.com/rust-lang/crates.io-index)" = "e674d85ae9c67cbbc590374d8f2e20a7a02fff87ce3a31fc52213afece8d05ad" "checksum wayland-sys 0.20.12 (registry+https://github.com/rust-lang/crates.io-index)" = "87c82ee658aa657fdfd7061f22e442030d921cfefc5bad68bcf41973e67922f7" -"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd" +"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum winit 0.17.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ba44cf306b981badc781894ab5d6fda54764a0512cbbf8db4685d329014143fa" diff --git a/Cargo.toml b/Cargo.toml index d34dc82b6..b54bc44fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,6 @@ [workspace] members = [ "wgpu-native", + "wgpu-bindings", + "examples", ] diff --git a/examples/Cargo.toml b/examples/Cargo.toml new file mode 100644 index 000000000..ae80337c4 --- /dev/null +++ b/examples/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "examples" +version = "0.1.0" +authors = [ + "Dzmitry Malyshau ", + "Joshua Groves ", +] +publish = false + +[[bin]] +name = "hello_triangle" +path = "hello_triangle/main.rs" + +[features] +default = [] +metal = ["wgpu-native/metal"] +dx12 = ["wgpu-native/dx12"] +vulkan = ["wgpu-native/vulkan"] + +[dependencies] +wgpu-native = { path = "../wgpu-native" } diff --git a/wgpu-native/examples/hello_triangle.rs b/examples/hello_triangle/main.rs similarity index 81% rename from wgpu-native/examples/hello_triangle.rs rename to examples/hello_triangle/main.rs index 7eaa1bf35..8ebcc108f 100644 --- a/wgpu-native/examples/hello_triangle.rs +++ b/examples/hello_triangle/main.rs @@ -11,9 +11,9 @@ fn main() { }, }); let _vs = wgn::device_create_shader_module(device, wgn::ShaderModuleDescriptor { - code: include_bytes!("./data/hello_triangle.vert.spv"), + code: include_bytes!("./shaders/hello_triangle.vert.spv"), }); let _fs = wgn::device_create_shader_module(device, wgn::ShaderModuleDescriptor { - code: include_bytes!("./data/hello_triangle.frag.spv"), + code: include_bytes!("./shaders/hello_triangle.frag.spv"), }); } diff --git a/wgpu-native/examples/data/hello_triangle.frag b/examples/hello_triangle/shaders/hello_triangle.frag similarity index 100% rename from wgpu-native/examples/data/hello_triangle.frag rename to examples/hello_triangle/shaders/hello_triangle.frag diff --git a/wgpu-native/examples/data/hello_triangle.frag.spv b/examples/hello_triangle/shaders/hello_triangle.frag.spv similarity index 100% rename from wgpu-native/examples/data/hello_triangle.frag.spv rename to examples/hello_triangle/shaders/hello_triangle.frag.spv diff --git a/wgpu-native/examples/data/hello_triangle.vert b/examples/hello_triangle/shaders/hello_triangle.vert similarity index 100% rename from wgpu-native/examples/data/hello_triangle.vert rename to examples/hello_triangle/shaders/hello_triangle.vert diff --git a/wgpu-native/examples/data/hello_triangle.vert.spv b/examples/hello_triangle/shaders/hello_triangle.vert.spv similarity index 100% rename from wgpu-native/examples/data/hello_triangle.vert.spv rename to examples/hello_triangle/shaders/hello_triangle.vert.spv diff --git a/wgpu-bindings/Cargo.toml b/wgpu-bindings/Cargo.toml new file mode 100644 index 000000000..7ab66cfdb --- /dev/null +++ b/wgpu-bindings/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "wgpu-bindings" +version = "0.1.0" +authors = [ + "Dzmitry Malyshau ", + "Joshua Groves ", +] + +[features] +default = [] + +[dependencies] +cbindgen = "0.6.3" diff --git a/wgpu-bindings/bindings.h b/wgpu-bindings/bindings.h new file mode 100644 index 000000000..30143c2a7 --- /dev/null +++ b/wgpu-bindings/bindings.h @@ -0,0 +1,54 @@ +#include +#include + +enum class PowerPreference { + Default = 0, + LowPower = 1, + HighPerformance = 2, +}; + +struct ShaderModuleDescriptor; + +using Id = uint32_t; + +using DeviceId = Id; + +using AdapterId = Id; + +struct Extensions { + bool anisotropic_filtering; +}; + +struct DeviceDescriptor { + Extensions extensions; +}; + +using ComputePassId = Id; + +using RenderPassId = Id; + +using CommandBufferId = Id; + +using InstanceId = Id; + +using ShaderModuleId = Id; + +struct AdapterDescriptor { + PowerPreference power_preference; +}; + +extern "C" { + +DeviceId adapter_create_device(AdapterId adapter_id, DeviceDescriptor desc); + +ComputePassId command_buffer_begin_compute_pass(); + +RenderPassId command_buffer_begin_render_pass(CommandBufferId command_buffer); + +InstanceId create_instance(); + +ShaderModuleId device_create_shader_module(DeviceId device_id, ShaderModuleDescriptor desc); + +AdapterId instance_get_adapter(InstanceId instance_id, AdapterDescriptor desc); + +} // extern "C" diff --git a/wgpu-bindings/src/main.rs b/wgpu-bindings/src/main.rs new file mode 100644 index 000000000..20a94bb3e --- /dev/null +++ b/wgpu-bindings/src/main.rs @@ -0,0 +1,14 @@ +extern crate cbindgen; + +use std::path::PathBuf; + +fn main() { + let mut crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + crate_dir.push("../wgpu-native"); + + cbindgen::Builder::new() + .with_crate(crate_dir) + .generate() + .expect("Unable to generate bindings") + .write_to_file("bindings.h"); +} diff --git a/wgpu-native/Cargo.toml b/wgpu-native/Cargo.toml index 3f6d853c4..0200a92b9 100644 --- a/wgpu-native/Cargo.toml +++ b/wgpu-native/Cargo.toml @@ -6,11 +6,18 @@ authors = [ "Joshua Groves ", ] +[lib] +crate-type = ["lib", "cdylib", "staticlib"] + [features] default = [] +metal = ["gfx-backend-metal"] +dx12 = ["gfx-backend-dx12"] +vulkan = ["gfx-backend-vulkan"] [dependencies] bitflags = "1.0" +lazy_static = "1.1.0" gfx-hal = { git = "https://github.com/gfx-rs/gfx", rev = "a435a05" } # required by gfx-memory gfx-backend-empty = { git = "https://github.com/gfx-rs/gfx", rev = "a435a05" } gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "a435a05", optional = true } diff --git a/wgpu-native/src/binding_model.rs b/wgpu-native/src/binding_model.rs index 577dcf43a..5bbc5a007 100644 --- a/wgpu-native/src/binding_model.rs +++ b/wgpu-native/src/binding_model.rs @@ -1,6 +1,6 @@ use hal; -use {BindGroupLayoutHandle, BufferHandle, SamplerHandle, TextureViewHandle}; +use {BindGroupLayoutId, BufferId, SamplerId, TextureViewId}; bitflags! { #[repr(transparent)] @@ -38,7 +38,7 @@ pub struct BindGroupLayout { #[repr(C)] pub struct PipelineLayoutDescriptor<'a> { - pub bind_group_layouts: &'a [BindGroupLayoutHandle], + pub bind_group_layouts: &'a [BindGroupLayoutId], } pub struct PipelineLayout { @@ -47,7 +47,7 @@ pub struct PipelineLayout { #[repr(C)] pub struct BufferBinding { - pub buffer: BufferHandle, + pub buffer: BufferId, pub offset: u32, pub size: u32, } @@ -55,8 +55,8 @@ pub struct BufferBinding { #[repr(C)] pub enum BindingResource { Buffer(BufferBinding), - Sampler(SamplerHandle), - TextureView(TextureViewHandle), + Sampler(SamplerId), + TextureView(TextureViewId), } #[repr(C)] diff --git a/wgpu-native/src/command/compute.rs b/wgpu-native/src/command/compute.rs index df2f0c01a..10fef286b 100644 --- a/wgpu-native/src/command/compute.rs +++ b/wgpu-native/src/command/compute.rs @@ -1,25 +1,8 @@ use hal; -use {CommandBuffer, CommandBufferHandle, ComputePassHandle}; +use {CommandBuffer, CommandBufferId, ComputePassId}; pub struct ComputePass { raw: B::CommandBuffer, } - -pub extern "C" -fn compute_pass_dispatch( - pass: ComputePassHandle, groups_x: u32, groups_y: u32, groups_z: u32 -) { - unimplemented!() -} - -pub extern "C" -fn compute_pass_end(pass: ComputePassHandle) -> CommandBufferHandle { - match pass.unbox() { - Some(pass) => CommandBufferHandle::new(CommandBuffer { - raw: pass.raw, - }), - None => CommandBufferHandle::null(), - } -} diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index ea31b64a6..d096df1fa 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -6,8 +6,8 @@ pub use self::render::*; use hal; -use {BufferHandle, Color, CommandBufferHandle, ComputePassHandle, Origin3d, - RenderPassHandle, TextureViewHandle, TextureHandle}; +use {BufferId, Color, CommandBufferId, ComputePassId, Origin3d, + RenderPassId, TextureViewId, TextureId}; #[repr(C)] @@ -23,7 +23,7 @@ pub enum StoreOp { #[repr(C)] pub struct RenderPassColorAttachmentDescriptor { - pub attachment: TextureViewHandle, + pub attachment: TextureViewId, pub load_op: LoadOp, pub store_op: StoreOp, pub clear_color: Color, @@ -31,7 +31,7 @@ pub struct RenderPassColorAttachmentDescriptor { #[repr(C)] pub struct RenderPassDepthStencilAttachmentDescriptor { - pub attachment: TextureViewHandle, + pub attachment: TextureViewId, pub depth_load_op: LoadOp, pub depth_store_op: StoreOp, pub clear_depth: f32, @@ -48,7 +48,7 @@ pub struct RenderPassDescriptor<'a> { #[repr(C)] pub struct BufferCopyView { - pub buffer: BufferHandle, + pub buffer: BufferId, pub offset: u32, pub row_pitch: u32, pub image_height: u32, @@ -56,7 +56,7 @@ pub struct BufferCopyView { #[repr(C)] pub struct TextureCopyView { - pub texture: TextureHandle, + pub texture: TextureId, pub level: u32, pub slice: u32, pub origin: Origin3d, @@ -70,15 +70,15 @@ pub struct CommandBuffer { #[repr(C)] pub struct CommandBufferDescriptor; -pub extern "C" -fn command_buffer_begin_render_pass( - command_buffer: CommandBufferHandle -) -> RenderPassHandle { +#[no_mangle] +pub extern "C" fn command_buffer_begin_render_pass( + command_buffer: CommandBufferId +) -> RenderPassId { unimplemented!() } -pub extern "C" -fn command_buffer_begin_compute_pass( -) -> ComputePassHandle { +#[no_mangle] +pub extern "C" fn command_buffer_begin_compute_pass( +) -> ComputePassId { unimplemented!() } diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index a6c1e19a5..98b936542 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -1,32 +1,8 @@ use hal; -use {CommandBuffer, CommandBufferHandle, RenderPassHandle}; +use {CommandBuffer, CommandBufferId, RenderPassId}; pub struct RenderPass { raw: B::CommandBuffer, } - -pub extern "C" -fn render_pass_draw( - pass: RenderPassHandle, vertex_count: u32, instance_count: u32, first_vertex: u32, first_instance: u32 -) { - unimplemented!() -} - -pub extern "C" -fn render_pass_draw_indexed( - pass: RenderPassHandle, index_count: u32, instance_count: u32, first_index: u32, vertex_offset: i32, first_instance: u32 -) { - unimplemented!() -} - -pub extern "C" -fn render_pass_end(pass: RenderPassHandle) -> CommandBufferHandle { - match pass.unbox() { - Some(pass) => CommandBufferHandle::new(CommandBuffer { - raw: pass.raw, - }), - None => CommandBufferHandle::null(), - } -} diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index bb4c310af..60c2d731e 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -1,7 +1,8 @@ use hal::{self, Device as _Device, QueueGroup}; use {conv, memory, pipeline, resource}; -use {BufferHandle, CommandBufferHandle, DeviceHandle, ShaderModuleHandle}; +use registry; +use {BufferId, CommandBufferId, DeviceId, ShaderModuleId}; #[repr(C)] @@ -28,35 +29,18 @@ impl Device { } } -pub extern "C" -fn device_create_buffer( - device: DeviceHandle, desc: resource::BufferDescriptor -) -> BufferHandle { - let (usage, memory_properties) = conv::map_buffer_usage(desc.usage); - let buffer = device.device.create_buffer(desc.size as u64, usage).unwrap(); - BufferHandle::new(resource::Buffer { - raw: buffer, - memory_properties, - }) -} - pub struct ShaderModule { pub raw: B::ShaderModule, } -pub extern "C" -fn device_create_shader_module( - device: DeviceHandle, desc: pipeline::ShaderModuleDescriptor -) -> ShaderModuleHandle { +#[no_mangle] +pub extern "C" fn device_create_shader_module( + device_id: DeviceId, desc: pipeline::ShaderModuleDescriptor +) -> ShaderModuleId { + let device_registry = registry::DEVICE_REGISTRY.lock().unwrap(); + let device = device_registry.get(device_id).unwrap(); let shader = device.device.create_shader_module(desc.code).unwrap(); - ShaderModuleHandle::new(ShaderModule { + registry::SHADER_MODULE_REGISTRY.lock().unwrap().register(ShaderModule { raw: shader, }) } - -pub extern "C" -fn device_create_command_buffer( - device: DeviceHandle, desc: CommandBufferDescriptor -) -> CommandBufferHandle { - unimplemented!() -} diff --git a/wgpu-native/src/handle.rs b/wgpu-native/src/handle.rs deleted file mode 100644 index 6716ff5a7..000000000 --- a/wgpu-native/src/handle.rs +++ /dev/null @@ -1,113 +0,0 @@ -use std::{borrow, cmp, fmt, ops, ptr}; -#[cfg(feature = "nightly")] -use std::sync::{Arc, Mutex}; - -#[cfg(feature = "nightly")] -use hal::backend::FastHashMap; - -#[cfg(feature = "nightly")] -lazy_static! { - static ref REGISTRY: Arc>> = Arc::new(Mutex::new(FastHashMap::default())); -} - -#[repr(C)] -pub struct Handle(*mut T); - -#[cfg(feature = "nightly")] -impl Handle<()> { - pub fn report_leaks() { - println!("Leaked handles:"); - let mut map = REGISTRY.lock().unwrap(); - for (_, type_id) in map.drain() { - println!("\t{:?}", type_id); - } - } -} - -impl Handle { - pub fn new(value: T) -> Self { - let ptr = Box::into_raw(Box::new(value)); - #[cfg(feature = "nightly")] - { - use std::intrinsics::type_name; - let name = unsafe { type_name::() }; - REGISTRY.lock().unwrap().insert(ptr as _, name); - } - Handle(ptr) - } - - pub fn null() -> Self { - Handle(ptr::null_mut()) - } - - pub fn unbox(self) -> Option { - if self.0.is_null() { - None - } else { - #[cfg(feature = "nightly")] - { - REGISTRY.lock().unwrap().remove(&(self.0 as _)).unwrap(); - } - Some(*unsafe { Box::from_raw(self.0) }) - } - } - - pub fn as_ref(&self) -> Option<&T> { - unsafe { self.0.as_ref() } - } -} - -impl Handle { - #[cfg(feature = "nightly")] - #[inline] - fn check(&self) { - assert!(REGISTRY.lock().unwrap().contains_key(&(self.0 as _))); - } - #[cfg(not(feature = "nightly"))] - #[inline] - fn check(&self) { - debug_assert!(!self.0.is_null()); - } -} - -impl Clone for Handle { - fn clone(&self) -> Self { - Handle(self.0) - } -} - -impl Copy for Handle {} - -impl ops::Deref for Handle { - type Target = T; - fn deref(&self) -> &T { - self.check(); - unsafe { &*self.0 } - } -} - -impl ops::DerefMut for Handle { - fn deref_mut(&mut self) -> &mut T { - self.check(); - unsafe { &mut *self.0 } - } -} - -impl borrow::Borrow for Handle { - fn borrow(&self) -> &T { - self.check(); - unsafe { &*self.0 } - } -} - -impl cmp::PartialEq for Handle { - fn eq(&self, other: &Self) -> bool { - self.0.eq(&other.0) - } -} - -impl fmt::Debug for Handle { - fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "Handle({:p})", self.0) - } -} diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index de11a2bab..3415a900b 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -1,6 +1,7 @@ use hal::{self, Instance as _Instance, PhysicalDevice as _PhysicalDevice}; -use {AdapterHandle, Device, DeviceHandle, InstanceHandle}; +use registry; +use {AdapterId, Device, DeviceId, InstanceId}; #[repr(C)] @@ -25,12 +26,13 @@ pub struct DeviceDescriptor { pub extensions: Extensions, } -pub extern "C" -fn create_instance() -> InstanceHandle { +#[no_mangle] +pub extern "C" fn create_instance() -> InstanceId { #[cfg(any(feature = "gfx-backend-vulkan", feature = "gfx-backend-dx12", feature = "gfx-backend-metal"))] { + let mut registry = registry::INSTANCE_REGISTRY.lock().unwrap(); let inst = ::back::Instance::create("wgpu", 1); - InstanceHandle::new(inst) + registry.register(inst) } #[cfg(not(any(feature = "gfx-backend-vulkan", feature = "gfx-backend-dx12", feature = "gfx-backend-metal")))] { @@ -38,10 +40,13 @@ fn create_instance() -> InstanceHandle { } } -pub extern "C" -fn instance_get_adapter( - instance: InstanceHandle, desc: AdapterDescriptor -) -> AdapterHandle { + +#[no_mangle] +pub extern "C" fn instance_get_adapter( + instance_id: InstanceId, desc: AdapterDescriptor +) -> AdapterId { + let instance_registry = registry::INSTANCE_REGISTRY.lock().unwrap(); + let instance = instance_registry.get(instance_id).unwrap(); let (mut low, mut high, mut other) = (None, None, None); for adapter in instance.enumerate_adapters() { match adapter.info.device_type { @@ -56,14 +61,16 @@ fn instance_get_adapter( PowerPreference::HighPerformance | PowerPreference::Default => high.or(low), }; - AdapterHandle::new(some.or(other).unwrap()) + registry::ADAPTER_REGISTRY.lock().unwrap().register(some.or(other).unwrap()) } -pub extern "C" -fn adapter_create_device( - mut adapter: AdapterHandle, desc: DeviceDescriptor -) -> DeviceHandle { +#[no_mangle] +pub extern "C" fn adapter_create_device( + adapter_id: AdapterId, desc: DeviceDescriptor +) -> DeviceId { + let mut adapter_registry = registry::ADAPTER_REGISTRY.lock().unwrap(); + let adapter = adapter_registry.get_mut(adapter_id).unwrap(); let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap(); let mem_props = adapter.physical_device.memory_properties(); - DeviceHandle::new(Device::new(device, queue_group, mem_props)) + registry::DEVICE_REGISTRY.lock().unwrap().register(Device::new(device, queue_group, mem_props)) } diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index 55a7b6353..e56160fa4 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -1,4 +1,5 @@ #[macro_use] extern crate bitflags; +#[macro_use] extern crate lazy_static; #[cfg(feature = "gfx-backend-vulkan")] extern crate gfx_backend_vulkan as back; @@ -13,12 +14,12 @@ extern crate gfx_hal as hal; extern crate gfx_memory as memory; mod binding_model; -mod conv; mod command; +mod conv; mod device; -mod handle; mod instance; mod pipeline; +mod registry; mod resource; pub use self::binding_model::*; @@ -29,7 +30,7 @@ pub use self::pipeline::*; pub use self::resource::*; use back::Backend as B; -use handle::Handle; +use registry::Id; #[repr(C)] pub struct Color { @@ -53,29 +54,33 @@ pub struct Extent3d { pub depth: f32, } -pub type InstanceHandle = Handle; -pub type AdapterHandle = Handle>; -pub type DeviceHandle = Handle>; -pub type BufferHandle = Handle>; +pub type InstanceId = Id; +pub(crate) type InstanceHandle = back::Instance; +pub type AdapterId = Id; +pub(crate) type AdapterHandle = hal::Adapter; +pub type DeviceId = Id; +pub(crate) type DeviceHandle = Device; +pub type BufferId = Id; // Resource -pub type TextureViewHandle = Handle; -pub type TextureHandle = Handle; -pub type SamplerHandle = Handle; +pub type TextureViewId = Id; +pub type TextureId = Id; +pub type SamplerId = Id; // Binding model -pub type BindGroupLayoutHandle = Handle; -pub type PipelineLayoutHandle = Handle>; +pub type BindGroupLayoutId = Id; +pub type PipelineLayoutId = Id; // Pipeline -pub type BlendStateHandle = Handle; -pub type DepthStencilStateHandle = Handle; -pub type InputStateHandle = Handle; -pub type ShaderModuleHandle = Handle>; -pub type AttachmentStateHandle = Handle; -pub type ComputePipelineHandle = Handle; -pub type RenderPipelineHandle = Handle; +pub type BlendStateId = Id; +pub type DepthStencilStateId = Id; +pub type InputStateId = Id; +pub type ShaderModuleId = Id; +pub(crate) type ShaderModuleHandle = ShaderModule; +pub type AttachmentStateId = Id; +pub type ComputePipelineId = Id; +pub type RenderPipelineId = Id; -pub type CommandBufferHandle = Handle>; -pub type RenderPassHandle = Handle>; -pub type ComputePassHandle = Handle>; +pub type CommandBufferId = Id; +pub type RenderPassId = Id; +pub type ComputePassId = Id; diff --git a/wgpu-native/src/pipeline.rs b/wgpu-native/src/pipeline.rs index 976c93f22..9757b950f 100644 --- a/wgpu-native/src/pipeline.rs +++ b/wgpu-native/src/pipeline.rs @@ -1,7 +1,7 @@ use hal; use resource; -use {BlendStateHandle, DepthStencilStateHandle, PipelineLayoutHandle}; +use {BlendStateId, DepthStencilStateId, PipelineLayoutId}; #[repr(C)] @@ -171,7 +171,7 @@ pub struct PipelineStageDescriptor<'a> { #[repr(C)] pub struct ComputePipelineDescriptor<'a> { - pub layout: PipelineLayoutHandle, + pub layout: PipelineLayoutId, pub stages: &'a [PipelineStageDescriptor<'a>], } @@ -190,11 +190,11 @@ pub enum PrimitiveTopology { #[repr(C)] pub struct RenderPipelineDescriptor<'a> { - pub layout: PipelineLayoutHandle, + pub layout: PipelineLayoutId, pub stages: &'a [PipelineStageDescriptor<'a>], pub primitive_topology: PrimitiveTopology, - pub blend_state: &'a [BlendStateHandle], - pub depth_stencil_state: DepthStencilStateHandle, + pub blend_state: &'a [BlendStateId], + pub depth_stencil_state: DepthStencilStateId, pub attachment_state: AttachmentState, } diff --git a/wgpu-native/src/registry.rs b/wgpu-native/src/registry.rs new file mode 100644 index 000000000..e0bb48e82 --- /dev/null +++ b/wgpu-native/src/registry.rs @@ -0,0 +1,43 @@ +use std::{borrow, cmp, fmt, ops, ptr}; +use std::sync::{Arc, Mutex}; + +use hal::backend::FastHashMap; +use {AdapterHandle, DeviceHandle, InstanceHandle, ShaderModuleHandle}; + +pub(crate) type Id = u32; + +pub(crate) struct Registry { + next_id: Id, + tracked: FastHashMap, +} + +impl Registry { + fn new() -> Self { + Registry { + next_id: 0, + tracked: FastHashMap::default(), + } + } + + pub(crate) fn register(&mut self, handle: T) -> Id { + let id = self.next_id; + self.tracked.insert(id, handle); + self.next_id += 1; + id + } + + pub(crate) fn get(&self, id: Id) -> Option<&T> { + self.tracked.get(&id) + } + + pub(crate) fn get_mut(&mut self, id: Id) -> Option<&mut T> { + self.tracked.get_mut(&id) + } +} + +lazy_static! { + pub(crate) static ref ADAPTER_REGISTRY: Arc>> = Arc::new(Mutex::new(Registry::new())); + pub(crate) static ref DEVICE_REGISTRY: Arc>> = Arc::new(Mutex::new(Registry::new())); + pub(crate) static ref INSTANCE_REGISTRY: Arc>> = Arc::new(Mutex::new(Registry::new())); + pub(crate) static ref SHADER_MODULE_REGISTRY: Arc>> = Arc::new(Mutex::new(Registry::new())); +} From 105a71d19a298a0290b8011032c56d938bdc801f Mon Sep 17 00:00:00 2001 From: grovesNL Date: Sat, 22 Sep 2018 16:55:01 -0600 Subject: [PATCH 2/8] Format all files --- examples/hello_triangle/main.rs | 38 ++++++++++++++++++++---------- wgpu-native/src/binding_model.rs | 2 +- wgpu-native/src/command/compute.rs | 1 - wgpu-native/src/command/mod.rs | 12 +++++----- wgpu-native/src/command/render.rs | 1 - wgpu-native/src/conv.rs | 7 +++--- wgpu-native/src/device.rs | 14 +++++------ wgpu-native/src/instance.rs | 34 ++++++++++++++++---------- wgpu-native/src/lib.rs | 18 +++++++++----- wgpu-native/src/pipeline.rs | 1 - wgpu-native/src/registry.rs | 14 +++++++---- wgpu-native/src/resource.rs | 3 +-- 12 files changed, 87 insertions(+), 58 deletions(-) diff --git a/examples/hello_triangle/main.rs b/examples/hello_triangle/main.rs index 8ebcc108f..3025ea6f8 100644 --- a/examples/hello_triangle/main.rs +++ b/examples/hello_triangle/main.rs @@ -2,18 +2,30 @@ extern crate wgpu_native as wgn; fn main() { let instance = wgn::create_instance(); - let adapter = wgn::instance_get_adapter(instance, wgn::AdapterDescriptor { - power_preference: wgn::PowerPreference::LowPower, - }); - let device = wgn::adapter_create_device(adapter, wgn::DeviceDescriptor { - extensions: wgn::Extensions { - anisotropic_filtering: false, + let adapter = wgn::instance_get_adapter( + instance, + wgn::AdapterDescriptor { + power_preference: wgn::PowerPreference::LowPower, }, - }); - let _vs = wgn::device_create_shader_module(device, wgn::ShaderModuleDescriptor { - code: include_bytes!("./shaders/hello_triangle.vert.spv"), - }); - let _fs = wgn::device_create_shader_module(device, wgn::ShaderModuleDescriptor { - code: include_bytes!("./shaders/hello_triangle.frag.spv"), - }); + ); + let device = wgn::adapter_create_device( + adapter, + wgn::DeviceDescriptor { + extensions: wgn::Extensions { + anisotropic_filtering: false, + }, + }, + ); + let _vs = wgn::device_create_shader_module( + device, + wgn::ShaderModuleDescriptor { + code: include_bytes!("./shaders/hello_triangle.vert.spv"), + }, + ); + let _fs = wgn::device_create_shader_module( + device, + wgn::ShaderModuleDescriptor { + code: include_bytes!("./shaders/hello_triangle.frag.spv"), + }, + ); } diff --git a/wgpu-native/src/binding_model.rs b/wgpu-native/src/binding_model.rs index 5bbc5a007..ca8df9052 100644 --- a/wgpu-native/src/binding_model.rs +++ b/wgpu-native/src/binding_model.rs @@ -13,7 +13,7 @@ bitflags! { } #[repr(C)] -pub enum BindingType { +pub enum BindingType { UniformBuffer = 0, Sampler = 1, SampledTexture = 2, diff --git a/wgpu-native/src/command/compute.rs b/wgpu-native/src/command/compute.rs index 10fef286b..daff85ff3 100644 --- a/wgpu-native/src/command/compute.rs +++ b/wgpu-native/src/command/compute.rs @@ -2,7 +2,6 @@ use hal; use {CommandBuffer, CommandBufferId, ComputePassId}; - pub struct ComputePass { raw: B::CommandBuffer, } diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index d096df1fa..5a90af140 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -6,9 +6,10 @@ pub use self::render::*; use hal; -use {BufferId, Color, CommandBufferId, ComputePassId, Origin3d, - RenderPassId, TextureViewId, TextureId}; - +use { + BufferId, Color, CommandBufferId, ComputePassId, Origin3d, RenderPassId, TextureId, + TextureViewId, +}; #[repr(C)] pub enum LoadOp { @@ -72,13 +73,12 @@ pub struct CommandBufferDescriptor; #[no_mangle] pub extern "C" fn command_buffer_begin_render_pass( - command_buffer: CommandBufferId + command_buffer: CommandBufferId, ) -> RenderPassId { unimplemented!() } #[no_mangle] -pub extern "C" fn command_buffer_begin_compute_pass( -) -> ComputePassId { +pub extern "C" fn command_buffer_begin_compute_pass() -> ComputePassId { unimplemented!() } diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index 98b936542..f77d1edcd 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -2,7 +2,6 @@ use hal; use {CommandBuffer, CommandBufferId, RenderPassId}; - pub struct RenderPass { raw: B::CommandBuffer, } diff --git a/wgpu-native/src/conv.rs b/wgpu-native/src/conv.rs index 39d55b2ca..238d6a8f0 100644 --- a/wgpu-native/src/conv.rs +++ b/wgpu-native/src/conv.rs @@ -2,11 +2,12 @@ use hal; use resource; - -pub(crate) fn map_buffer_usage(usage: resource::BufferUsageFlags) -> (hal::buffer::Usage, hal::memory::Properties) { - use resource::BufferUsageFlags as W; +pub(crate) fn map_buffer_usage( + usage: resource::BufferUsageFlags, +) -> (hal::buffer::Usage, hal::memory::Properties) { use hal::buffer::Usage as U; use hal::memory::Properties as P; + use resource::BufferUsageFlags as W; let mut hal_memory = P::empty(); if usage.contains(W::MAP_READ) { diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 60c2d731e..37ef45d64 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -4,10 +4,8 @@ use {conv, memory, pipeline, resource}; use registry; use {BufferId, CommandBufferId, DeviceId, ShaderModuleId}; - #[repr(C)] -pub struct CommandBufferDescriptor { -} +pub struct CommandBufferDescriptor {} pub struct Device { device: B::Device, @@ -35,12 +33,14 @@ pub struct ShaderModule { #[no_mangle] pub extern "C" fn device_create_shader_module( - device_id: DeviceId, desc: pipeline::ShaderModuleDescriptor + device_id: DeviceId, + desc: pipeline::ShaderModuleDescriptor, ) -> ShaderModuleId { let device_registry = registry::DEVICE_REGISTRY.lock().unwrap(); let device = device_registry.get(device_id).unwrap(); let shader = device.device.create_shader_module(desc.code).unwrap(); - registry::SHADER_MODULE_REGISTRY.lock().unwrap().register(ShaderModule { - raw: shader, - }) + registry::SHADER_MODULE_REGISTRY + .lock() + .unwrap() + .register(ShaderModule { raw: shader }) } diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 3415a900b..d9943958e 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -3,7 +3,6 @@ use hal::{self, Instance as _Instance, PhysicalDevice as _PhysicalDevice}; use registry; use {AdapterId, Device, DeviceId, InstanceId}; - #[repr(C)] pub enum PowerPreference { Default = 0, @@ -28,22 +27,30 @@ pub struct DeviceDescriptor { #[no_mangle] pub extern "C" fn create_instance() -> InstanceId { - #[cfg(any(feature = "gfx-backend-vulkan", feature = "gfx-backend-dx12", feature = "gfx-backend-metal"))] + #[cfg(any( + feature = "gfx-backend-vulkan", + feature = "gfx-backend-dx12", + feature = "gfx-backend-metal" + ))] { let mut registry = registry::INSTANCE_REGISTRY.lock().unwrap(); let inst = ::back::Instance::create("wgpu", 1); registry.register(inst) } - #[cfg(not(any(feature = "gfx-backend-vulkan", feature = "gfx-backend-dx12", feature = "gfx-backend-metal")))] + #[cfg(not(any( + feature = "gfx-backend-vulkan", + feature = "gfx-backend-dx12", + feature = "gfx-backend-metal" + )))] { unimplemented!() } } - #[no_mangle] pub extern "C" fn instance_get_adapter( - instance_id: InstanceId, desc: AdapterDescriptor + instance_id: InstanceId, + desc: AdapterDescriptor, ) -> AdapterId { let instance_registry = registry::INSTANCE_REGISTRY.lock().unwrap(); let instance = instance_registry.get(instance_id).unwrap(); @@ -58,19 +65,22 @@ pub extern "C" fn instance_get_adapter( let some = match desc.power_preference { PowerPreference::LowPower => low.or(high), - PowerPreference::HighPerformance | - PowerPreference::Default => high.or(low), + PowerPreference::HighPerformance | PowerPreference::Default => high.or(low), }; - registry::ADAPTER_REGISTRY.lock().unwrap().register(some.or(other).unwrap()) + registry::ADAPTER_REGISTRY + .lock() + .unwrap() + .register(some.or(other).unwrap()) } #[no_mangle] -pub extern "C" fn adapter_create_device( - adapter_id: AdapterId, desc: DeviceDescriptor -) -> DeviceId { +pub extern "C" fn adapter_create_device(adapter_id: AdapterId, desc: DeviceDescriptor) -> DeviceId { let mut adapter_registry = registry::ADAPTER_REGISTRY.lock().unwrap(); let adapter = adapter_registry.get_mut(adapter_id).unwrap(); let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap(); let mem_props = adapter.physical_device.memory_properties(); - registry::DEVICE_REGISTRY.lock().unwrap().register(Device::new(device, queue_group, mem_props)) + registry::DEVICE_REGISTRY + .lock() + .unwrap() + .register(Device::new(device, queue_group, mem_props)) } diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index e56160fa4..d53af324c 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -1,14 +1,20 @@ -#[macro_use] extern crate bitflags; -#[macro_use] extern crate lazy_static; +#[macro_use] +extern crate bitflags; +#[macro_use] +extern crate lazy_static; -#[cfg(feature = "gfx-backend-vulkan")] -extern crate gfx_backend_vulkan as back; #[cfg(feature = "gfx-backend-dx12")] extern crate gfx_backend_dx12 as back; +#[cfg(not(any( + feature = "gfx-backend-vulkan", + feature = "gfx-backend-dx12", + feature = "gfx-backend-metal" +)))] +extern crate gfx_backend_empty as back; #[cfg(feature = "gfx-backend-metal")] extern crate gfx_backend_metal as back; -#[cfg(not(any(feature = "gfx-backend-vulkan", feature = "gfx-backend-dx12", feature = "gfx-backend-metal")))] -extern crate gfx_backend_empty as back; +#[cfg(feature = "gfx-backend-vulkan")] +extern crate gfx_backend_vulkan as back; extern crate gfx_hal as hal; extern crate gfx_memory as memory; diff --git a/wgpu-native/src/pipeline.rs b/wgpu-native/src/pipeline.rs index 9757b950f..54d3626ff 100644 --- a/wgpu-native/src/pipeline.rs +++ b/wgpu-native/src/pipeline.rs @@ -3,7 +3,6 @@ use resource; use {BlendStateId, DepthStencilStateId, PipelineLayoutId}; - #[repr(C)] pub enum BlendFactor { Zero = 0, diff --git a/wgpu-native/src/registry.rs b/wgpu-native/src/registry.rs index e0bb48e82..c418c2827 100644 --- a/wgpu-native/src/registry.rs +++ b/wgpu-native/src/registry.rs @@ -1,5 +1,5 @@ -use std::{borrow, cmp, fmt, ops, ptr}; use std::sync::{Arc, Mutex}; +use std::{borrow, cmp, fmt, ops, ptr}; use hal::backend::FastHashMap; use {AdapterHandle, DeviceHandle, InstanceHandle, ShaderModuleHandle}; @@ -36,8 +36,12 @@ impl Registry { } lazy_static! { - pub(crate) static ref ADAPTER_REGISTRY: Arc>> = Arc::new(Mutex::new(Registry::new())); - pub(crate) static ref DEVICE_REGISTRY: Arc>> = Arc::new(Mutex::new(Registry::new())); - pub(crate) static ref INSTANCE_REGISTRY: Arc>> = Arc::new(Mutex::new(Registry::new())); - pub(crate) static ref SHADER_MODULE_REGISTRY: Arc>> = Arc::new(Mutex::new(Registry::new())); + pub(crate) static ref ADAPTER_REGISTRY: Arc>> = + Arc::new(Mutex::new(Registry::new())); + pub(crate) static ref DEVICE_REGISTRY: Arc>> = + Arc::new(Mutex::new(Registry::new())); + pub(crate) static ref INSTANCE_REGISTRY: Arc>> = + Arc::new(Mutex::new(Registry::new())); + pub(crate) static ref SHADER_MODULE_REGISTRY: Arc>> = + Arc::new(Mutex::new(Registry::new())); } diff --git a/wgpu-native/src/resource.rs b/wgpu-native/src/resource.rs index defc23431..6913af100 100644 --- a/wgpu-native/src/resource.rs +++ b/wgpu-native/src/resource.rs @@ -1,7 +1,6 @@ use hal; -use {Extent3d}; - +use Extent3d; bitflags! { #[repr(transparent)] From d94d45cd801b6de34786d862a7e4c35320422b69 Mon Sep 17 00:00:00 2001 From: grovesNL Date: Sat, 22 Sep 2018 20:54:42 -0600 Subject: [PATCH 3/8] Target C for header generation --- wgpu-bindings/bindings.h | 47 ++++++++++++++++++--------------------- wgpu-bindings/src/main.rs | 1 + 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/wgpu-bindings/bindings.h b/wgpu-bindings/bindings.h index 30143c2a7..fcf9468ec 100644 --- a/wgpu-bindings/bindings.h +++ b/wgpu-bindings/bindings.h @@ -1,54 +1,51 @@ -#include -#include +#include +#include +#include -enum class PowerPreference { +typedef enum { Default = 0, LowPower = 1, HighPerformance = 2, -}; +} PowerPreference; -struct ShaderModuleDescriptor; +typedef struct ShaderModuleDescriptor ShaderModuleDescriptor; -using Id = uint32_t; +typedef uint32_t Id; -using DeviceId = Id; +typedef Id DeviceId; -using AdapterId = Id; +typedef Id AdapterId; -struct Extensions { +typedef struct { bool anisotropic_filtering; -}; +} Extensions; -struct DeviceDescriptor { +typedef struct { Extensions extensions; -}; +} DeviceDescriptor; -using ComputePassId = Id; +typedef Id ComputePassId; -using RenderPassId = Id; +typedef Id RenderPassId; -using CommandBufferId = Id; +typedef Id CommandBufferId; -using InstanceId = Id; +typedef Id InstanceId; -using ShaderModuleId = Id; +typedef Id ShaderModuleId; -struct AdapterDescriptor { +typedef struct { PowerPreference power_preference; -}; - -extern "C" { +} AdapterDescriptor; DeviceId adapter_create_device(AdapterId adapter_id, DeviceDescriptor desc); -ComputePassId command_buffer_begin_compute_pass(); +ComputePassId command_buffer_begin_compute_pass(void); RenderPassId command_buffer_begin_render_pass(CommandBufferId command_buffer); -InstanceId create_instance(); +InstanceId create_instance(void); ShaderModuleId device_create_shader_module(DeviceId device_id, ShaderModuleDescriptor desc); AdapterId instance_get_adapter(InstanceId instance_id, AdapterDescriptor desc); - -} // extern "C" diff --git a/wgpu-bindings/src/main.rs b/wgpu-bindings/src/main.rs index 20a94bb3e..627f0aeaf 100644 --- a/wgpu-bindings/src/main.rs +++ b/wgpu-bindings/src/main.rs @@ -8,6 +8,7 @@ fn main() { cbindgen::Builder::new() .with_crate(crate_dir) + .with_language(cbindgen::Language::C) .generate() .expect("Unable to generate bindings") .write_to_file("bindings.h"); From 08ad0f40ed2bf46ea73b3a828f40ec28d2c79dbe Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Sun, 23 Sep 2018 13:25:05 -0600 Subject: [PATCH 4/8] Create C example --- .gitignore | 1 + examples/Cargo.toml | 2 +- examples/Makefile | 14 +++++ .../shaders => data}/hello_triangle.frag | 0 .../shaders => data}/hello_triangle.frag.spv | Bin .../shaders => data}/hello_triangle.vert | 0 .../shaders => data}/hello_triangle.vert.spv | Bin examples/hello_triangle/main.rs | 31 ----------- examples/hello_triangle_c/main.c | 23 ++++++++ examples/hello_triangle_rust/main.rs | 32 +++++++++++ wgpu-bindings/bindings.h | 51 ----------------- wgpu-bindings/src/main.rs | 12 +++- wgpu-bindings/wgpu.h | 52 ++++++++++++++++++ wgpu-native/src/command/mod.rs | 4 +- wgpu-native/src/device.rs | 2 +- wgpu-native/src/instance.rs | 6 +- 16 files changed, 140 insertions(+), 90 deletions(-) create mode 100644 examples/Makefile rename examples/{hello_triangle/shaders => data}/hello_triangle.frag (100%) rename examples/{hello_triangle/shaders => data}/hello_triangle.frag.spv (100%) rename examples/{hello_triangle/shaders => data}/hello_triangle.vert (100%) rename examples/{hello_triangle/shaders => data}/hello_triangle.vert.spv (100%) delete mode 100644 examples/hello_triangle/main.rs create mode 100644 examples/hello_triangle_c/main.c create mode 100644 examples/hello_triangle_rust/main.rs delete mode 100644 wgpu-bindings/bindings.h create mode 100644 wgpu-bindings/wgpu.h diff --git a/.gitignore b/.gitignore index db4a59b65..621da21d8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ **/*.rs.bk #Cargo.lock .vscode +build diff --git a/examples/Cargo.toml b/examples/Cargo.toml index ae80337c4..12f60412d 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -9,7 +9,7 @@ publish = false [[bin]] name = "hello_triangle" -path = "hello_triangle/main.rs" +path = "hello_triangle_rust/main.rs" [features] default = [] diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 000000000..c78b661b4 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,14 @@ +all: hello_world_c + +CC=gcc +CFLAGS=-I. +DEPS=./../wgpu-bindings/wgpu.h +OUTDIR=./build +LINK_ARGS=-L ./../target/debug -lwgpu_native + +%.o: %.c $(DEPS) + $(CC) $(LINK_ARGS) -c -o $(OUTDIR)/$@ $< $(CFLAGS) + +hello_world_c: hello_triangle_c/main.c + mkdir -p $(OUTDIR) + $(CC) $(LINK_ARGS) -o $(OUTDIR)/$@ $^ $(CFLAGS) diff --git a/examples/hello_triangle/shaders/hello_triangle.frag b/examples/data/hello_triangle.frag similarity index 100% rename from examples/hello_triangle/shaders/hello_triangle.frag rename to examples/data/hello_triangle.frag diff --git a/examples/hello_triangle/shaders/hello_triangle.frag.spv b/examples/data/hello_triangle.frag.spv similarity index 100% rename from examples/hello_triangle/shaders/hello_triangle.frag.spv rename to examples/data/hello_triangle.frag.spv diff --git a/examples/hello_triangle/shaders/hello_triangle.vert b/examples/data/hello_triangle.vert similarity index 100% rename from examples/hello_triangle/shaders/hello_triangle.vert rename to examples/data/hello_triangle.vert diff --git a/examples/hello_triangle/shaders/hello_triangle.vert.spv b/examples/data/hello_triangle.vert.spv similarity index 100% rename from examples/hello_triangle/shaders/hello_triangle.vert.spv rename to examples/data/hello_triangle.vert.spv diff --git a/examples/hello_triangle/main.rs b/examples/hello_triangle/main.rs deleted file mode 100644 index 3025ea6f8..000000000 --- a/examples/hello_triangle/main.rs +++ /dev/null @@ -1,31 +0,0 @@ -extern crate wgpu_native as wgn; - -fn main() { - let instance = wgn::create_instance(); - let adapter = wgn::instance_get_adapter( - instance, - wgn::AdapterDescriptor { - power_preference: wgn::PowerPreference::LowPower, - }, - ); - let device = wgn::adapter_create_device( - adapter, - wgn::DeviceDescriptor { - extensions: wgn::Extensions { - anisotropic_filtering: false, - }, - }, - ); - let _vs = wgn::device_create_shader_module( - device, - wgn::ShaderModuleDescriptor { - code: include_bytes!("./shaders/hello_triangle.vert.spv"), - }, - ); - let _fs = wgn::device_create_shader_module( - device, - wgn::ShaderModuleDescriptor { - code: include_bytes!("./shaders/hello_triangle.frag.spv"), - }, - ); -} diff --git a/examples/hello_triangle_c/main.c b/examples/hello_triangle_c/main.c new file mode 100644 index 000000000..7e06a3a9a --- /dev/null +++ b/examples/hello_triangle_c/main.c @@ -0,0 +1,23 @@ +#include +#include "./../../wgpu-bindings/wgpu.h" + +int main() +{ + WGPUInstanceId instance = wgpu_create_instance(); + WGPUAdapterDescriptor adapter_desc = { + .power_preference = WGPUPowerPreference_LowPower, + }; + WGPUAdapterId adapter = wgpu_instance_get_adapter(instance, adapter_desc); + WGPUDeviceDescriptor device_desc = { + .extensions = { + .anisotropic_filtering = false, + }, + }; + WGPUDeviceId device = wgpu_adapter_create_device(adapter, device_desc); + /*WGPUShaderModuleDescriptor vs_desc = { + .code = "", + }; + WGPUShaderModuleId _vs = wgpu_device_create_shader_module(device, vs_desc); + */ + return 0; +} diff --git a/examples/hello_triangle_rust/main.rs b/examples/hello_triangle_rust/main.rs new file mode 100644 index 000000000..63b0b7ab8 --- /dev/null +++ b/examples/hello_triangle_rust/main.rs @@ -0,0 +1,32 @@ +extern crate wgpu_native; +use wgpu_native::*; + +fn main() { + let instance = wgpu_create_instance(); + let adapter = wgpu_instance_get_adapter( + instance, + AdapterDescriptor { + power_preference: PowerPreference::LowPower, + }, + ); + let device = wgpu_adapter_create_device( + adapter, + DeviceDescriptor { + extensions: Extensions { + anisotropic_filtering: false, + }, + }, + ); + let _vs = wgpu_device_create_shader_module( + device, + ShaderModuleDescriptor { + code: include_bytes!("./../data/hello_triangle.vert.spv"), + }, + ); + let _fs = wgpu_device_create_shader_module( + device, + ShaderModuleDescriptor { + code: include_bytes!("./../data/hello_triangle.frag.spv"), + }, + ); +} diff --git a/wgpu-bindings/bindings.h b/wgpu-bindings/bindings.h deleted file mode 100644 index fcf9468ec..000000000 --- a/wgpu-bindings/bindings.h +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -#include - -typedef enum { - Default = 0, - LowPower = 1, - HighPerformance = 2, -} PowerPreference; - -typedef struct ShaderModuleDescriptor ShaderModuleDescriptor; - -typedef uint32_t Id; - -typedef Id DeviceId; - -typedef Id AdapterId; - -typedef struct { - bool anisotropic_filtering; -} Extensions; - -typedef struct { - Extensions extensions; -} DeviceDescriptor; - -typedef Id ComputePassId; - -typedef Id RenderPassId; - -typedef Id CommandBufferId; - -typedef Id InstanceId; - -typedef Id ShaderModuleId; - -typedef struct { - PowerPreference power_preference; -} AdapterDescriptor; - -DeviceId adapter_create_device(AdapterId adapter_id, DeviceDescriptor desc); - -ComputePassId command_buffer_begin_compute_pass(void); - -RenderPassId command_buffer_begin_render_pass(CommandBufferId command_buffer); - -InstanceId create_instance(void); - -ShaderModuleId device_create_shader_module(DeviceId device_id, ShaderModuleDescriptor desc); - -AdapterId instance_get_adapter(InstanceId instance_id, AdapterDescriptor desc); diff --git a/wgpu-bindings/src/main.rs b/wgpu-bindings/src/main.rs index 627f0aeaf..9d31ef501 100644 --- a/wgpu-bindings/src/main.rs +++ b/wgpu-bindings/src/main.rs @@ -6,10 +6,20 @@ fn main() { let mut crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); crate_dir.push("../wgpu-native"); + let config = cbindgen::Config { + enumeration: cbindgen::EnumConfig { + prefix_with_name: true, + ..Default::default() + }, + ..Default::default() + }; + cbindgen::Builder::new() .with_crate(crate_dir) + .with_config(config) .with_language(cbindgen::Language::C) + .with_item_prefix("WGPU") .generate() .expect("Unable to generate bindings") - .write_to_file("bindings.h"); + .write_to_file("wgpu.h"); } diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h new file mode 100644 index 000000000..802227d9e --- /dev/null +++ b/wgpu-bindings/wgpu.h @@ -0,0 +1,52 @@ +#include +#include +#include + +typedef enum { + WGPUPowerPreference_Default = 0, + WGPUPowerPreference_LowPower = 1, + WGPUPowerPreference_HighPerformance = 2, +} WGPUPowerPreference; + +typedef struct WGPUShaderModuleDescriptor WGPUShaderModuleDescriptor; + +typedef uint32_t WGPUId; + +typedef WGPUId WGPUDeviceId; + +typedef WGPUId WGPUAdapterId; + +typedef struct { + bool anisotropic_filtering; +} WGPUExtensions; + +typedef struct { + WGPUExtensions extensions; +} WGPUDeviceDescriptor; + +typedef WGPUId WGPUComputePassId; + +typedef WGPUId WGPURenderPassId; + +typedef WGPUId WGPUCommandBufferId; + +typedef WGPUId WGPUInstanceId; + +typedef WGPUId WGPUShaderModuleId; + +typedef struct { + WGPUPowerPreference power_preference; +} WGPUAdapterDescriptor; + +WGPUDeviceId wgpu_adapter_create_device(WGPUAdapterId adapter_id, WGPUDeviceDescriptor desc); + +WGPUComputePassId wgpu_command_buffer_begin_compute_pass(void); + +WGPURenderPassId wgpu_command_buffer_begin_render_pass(WGPUCommandBufferId command_buffer); + +WGPUInstanceId wgpu_create_instance(void); + +WGPUShaderModuleId wgpu_device_create_shader_module(WGPUDeviceId device_id, + WGPUShaderModuleDescriptor desc); + +WGPUAdapterId wgpu_instance_get_adapter(WGPUInstanceId instance_id, WGPUAdapterDescriptor desc); diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index 5a90af140..e70a017c1 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -72,13 +72,13 @@ pub struct CommandBuffer { pub struct CommandBufferDescriptor; #[no_mangle] -pub extern "C" fn command_buffer_begin_render_pass( +pub extern "C" fn wgpu_command_buffer_begin_render_pass( command_buffer: CommandBufferId, ) -> RenderPassId { unimplemented!() } #[no_mangle] -pub extern "C" fn command_buffer_begin_compute_pass() -> ComputePassId { +pub extern "C" fn wgpu_command_buffer_begin_compute_pass() -> ComputePassId { unimplemented!() } diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 37ef45d64..22c418bc7 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -32,7 +32,7 @@ pub struct ShaderModule { } #[no_mangle] -pub extern "C" fn device_create_shader_module( +pub extern "C" fn wgpu_device_create_shader_module( device_id: DeviceId, desc: pipeline::ShaderModuleDescriptor, ) -> ShaderModuleId { diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index d9943958e..8b8dee710 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -26,7 +26,7 @@ pub struct DeviceDescriptor { } #[no_mangle] -pub extern "C" fn create_instance() -> InstanceId { +pub extern "C" fn wgpu_create_instance() -> InstanceId { #[cfg(any( feature = "gfx-backend-vulkan", feature = "gfx-backend-dx12", @@ -48,7 +48,7 @@ pub extern "C" fn create_instance() -> InstanceId { } #[no_mangle] -pub extern "C" fn instance_get_adapter( +pub extern "C" fn wgpu_instance_get_adapter( instance_id: InstanceId, desc: AdapterDescriptor, ) -> AdapterId { @@ -74,7 +74,7 @@ pub extern "C" fn instance_get_adapter( } #[no_mangle] -pub extern "C" fn adapter_create_device(adapter_id: AdapterId, desc: DeviceDescriptor) -> DeviceId { +pub extern "C" fn wgpu_adapter_create_device(adapter_id: AdapterId, desc: DeviceDescriptor) -> DeviceId { let mut adapter_registry = registry::ADAPTER_REGISTRY.lock().unwrap(); let adapter = adapter_registry.get_mut(adapter_id).unwrap(); let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap(); From 211189a090ea7144788796a311762ae86f8224ae Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Sun, 23 Sep 2018 19:29:44 -0600 Subject: [PATCH 5/8] Create shader modules from C --- examples/hello_triangle_c/main.c | 25 ++++++++++++++++++++++--- examples/hello_triangle_rust/main.rs | 12 ++++++++++-- wgpu-bindings/wgpu.h | 11 +++++++++-- wgpu-native/src/device.rs | 6 +++++- wgpu-native/src/instance.rs | 5 ++++- wgpu-native/src/lib.rs | 6 ++++++ wgpu-native/src/pipeline.rs | 14 +++++++------- 7 files changed, 63 insertions(+), 16 deletions(-) diff --git a/examples/hello_triangle_c/main.c b/examples/hello_triangle_c/main.c index 7e06a3a9a..ef451a0b4 100644 --- a/examples/hello_triangle_c/main.c +++ b/examples/hello_triangle_c/main.c @@ -1,6 +1,22 @@ #include #include "./../../wgpu-bindings/wgpu.h" +WGPUByteArray read_file(const char *name) +{ + FILE *file = fopen(name, "rb"); + fseek(file, 0, SEEK_END); + long length = ftell(file); + unsigned char *bytes = malloc(length); + fseek(file, 0, SEEK_SET); + fread(bytes, 1, length, file); + fclose(file); + WGPUByteArray ret = { + .bytes = bytes, + .length = length, + }; + return ret; +} + int main() { WGPUInstanceId instance = wgpu_create_instance(); @@ -14,10 +30,13 @@ int main() }, }; WGPUDeviceId device = wgpu_adapter_create_device(adapter, device_desc); - /*WGPUShaderModuleDescriptor vs_desc = { - .code = "", + WGPUShaderModuleDescriptor vs_desc = { + .code = read_file("./../data/hello_triangle.vert.spv"), }; WGPUShaderModuleId _vs = wgpu_device_create_shader_module(device, vs_desc); - */ + WGPUShaderModuleDescriptor fs_desc = { + .code = read_file("./../data/hello_triangle.frag.spv"), + }; + WGPUShaderModuleId _fs = wgpu_device_create_shader_module(device, fs_desc); return 0; } diff --git a/examples/hello_triangle_rust/main.rs b/examples/hello_triangle_rust/main.rs index 63b0b7ab8..313e68029 100644 --- a/examples/hello_triangle_rust/main.rs +++ b/examples/hello_triangle_rust/main.rs @@ -17,16 +17,24 @@ fn main() { }, }, ); + let vs_bytes = include_bytes!("./../data/hello_triangle.vert.spv"); let _vs = wgpu_device_create_shader_module( device, ShaderModuleDescriptor { - code: include_bytes!("./../data/hello_triangle.vert.spv"), + code: ByteArray { + bytes: vs_bytes.as_ptr(), + length: vs_bytes.len(), + }, }, ); + let fs_bytes = include_bytes!("./../data/hello_triangle.frag.spv"); let _fs = wgpu_device_create_shader_module( device, ShaderModuleDescriptor { - code: include_bytes!("./../data/hello_triangle.frag.spv"), + code: ByteArray { + bytes: fs_bytes.as_ptr(), + length: fs_bytes.len(), + }, }, ); } diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index 802227d9e..93499b6c8 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -8,8 +8,6 @@ typedef enum { WGPUPowerPreference_HighPerformance = 2, } WGPUPowerPreference; -typedef struct WGPUShaderModuleDescriptor WGPUShaderModuleDescriptor; - typedef uint32_t WGPUId; typedef WGPUId WGPUDeviceId; @@ -34,6 +32,15 @@ typedef WGPUId WGPUInstanceId; typedef WGPUId WGPUShaderModuleId; +typedef struct { + const uint8_t *bytes; + uintptr_t length; +} WGPUByteArray; + +typedef struct { + WGPUByteArray code; +} WGPUShaderModuleDescriptor; + typedef struct { WGPUPowerPreference power_preference; } WGPUAdapterDescriptor; diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 22c418bc7..c630725c7 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -38,7 +38,11 @@ pub extern "C" fn wgpu_device_create_shader_module( ) -> ShaderModuleId { let device_registry = registry::DEVICE_REGISTRY.lock().unwrap(); let device = device_registry.get(device_id).unwrap(); - let shader = device.device.create_shader_module(desc.code).unwrap(); + let shader = device + .device + .create_shader_module(unsafe { + ::std::slice::from_raw_parts(desc.code.bytes, desc.code.length) + }).unwrap(); registry::SHADER_MODULE_REGISTRY .lock() .unwrap() diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 8b8dee710..5c3255fa0 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -74,7 +74,10 @@ pub extern "C" fn wgpu_instance_get_adapter( } #[no_mangle] -pub extern "C" fn wgpu_adapter_create_device(adapter_id: AdapterId, desc: DeviceDescriptor) -> DeviceId { +pub extern "C" fn wgpu_adapter_create_device( + adapter_id: AdapterId, + desc: DeviceDescriptor, +) -> DeviceId { let mut adapter_registry = registry::ADAPTER_REGISTRY.lock().unwrap(); let adapter = adapter_registry.get_mut(adapter_id).unwrap(); let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap(); diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index d53af324c..86f8c111a 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -60,6 +60,12 @@ pub struct Extent3d { pub depth: f32, } +#[repr(C)] +pub struct ByteArray { + pub bytes: *const u8, + pub length: usize, +} + pub type InstanceId = Id; pub(crate) type InstanceHandle = back::Instance; pub type AdapterId = Id; diff --git a/wgpu-native/src/pipeline.rs b/wgpu-native/src/pipeline.rs index 54d3626ff..0d94aa2ac 100644 --- a/wgpu-native/src/pipeline.rs +++ b/wgpu-native/src/pipeline.rs @@ -1,7 +1,7 @@ use hal; use resource; -use {BlendStateId, DepthStencilStateId, PipelineLayoutId}; +use {BlendStateId, ByteArray, DepthStencilStateId, PipelineLayoutId}; #[repr(C)] pub enum BlendFactor { @@ -141,8 +141,8 @@ pub struct InputState { } #[repr(C)] -pub struct ShaderModuleDescriptor<'a> { - pub code: &'a [u8], +pub struct ShaderModuleDescriptor { + pub code: ByteArray, } #[repr(C)] @@ -162,8 +162,8 @@ pub enum ShaderStage { } #[repr(C)] -pub struct PipelineStageDescriptor<'a> { - pub module: ShaderModuleDescriptor<'a>, +pub struct PipelineStageDescriptor { + pub module: ShaderModuleDescriptor, pub stage: ShaderStage, pub entry_point: *const ::std::os::raw::c_char, } @@ -171,7 +171,7 @@ pub struct PipelineStageDescriptor<'a> { #[repr(C)] pub struct ComputePipelineDescriptor<'a> { pub layout: PipelineLayoutId, - pub stages: &'a [PipelineStageDescriptor<'a>], + pub stages: &'a [PipelineStageDescriptor], } pub struct ComputePipeline { @@ -190,7 +190,7 @@ pub enum PrimitiveTopology { #[repr(C)] pub struct RenderPipelineDescriptor<'a> { pub layout: PipelineLayoutId, - pub stages: &'a [PipelineStageDescriptor<'a>], + pub stages: &'a [PipelineStageDescriptor], pub primitive_topology: PrimitiveTopology, pub blend_state: &'a [BlendStateId], pub depth_stencil_state: DepthStencilStateId, From 7d35607ec1efe216c2a57560a90f5fe264275c6a Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Sun, 23 Sep 2018 23:05:12 -0600 Subject: [PATCH 6/8] Add remote feature to control ID type --- examples/Cargo.toml | 1 + wgpu-bindings/src/main.rs | 14 +++++-- wgpu-bindings/wgpu.h | 8 +++- wgpu-native/Cargo.toml | 1 + wgpu-native/src/device.rs | 2 +- wgpu-native/src/instance.rs | 2 +- wgpu-native/src/registry.rs | 81 ++++++++++++++++++++++++++++++------- 7 files changed, 88 insertions(+), 21 deletions(-) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 12f60412d..e6b9f6d4d 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -13,6 +13,7 @@ path = "hello_triangle_rust/main.rs" [features] default = [] +remote = ["wgpu-native/remote"] metal = ["wgpu-native/metal"] dx12 = ["wgpu-native/dx12"] vulkan = ["wgpu-native/vulkan"] diff --git a/wgpu-bindings/src/main.rs b/wgpu-bindings/src/main.rs index 9d31ef501..a1d14669b 100644 --- a/wgpu-bindings/src/main.rs +++ b/wgpu-bindings/src/main.rs @@ -7,19 +7,27 @@ fn main() { crate_dir.push("../wgpu-native"); let config = cbindgen::Config { + header: Some(String::from("#ifdef WGPU_REMOTE\n typedef uint32_t WGPUId;\n#else\n typedef void *WGPUId;\n#endif")), enumeration: cbindgen::EnumConfig { prefix_with_name: true, ..Default::default() }, + export: cbindgen::ExportConfig { + prefix: Some(String::from("WGPU")), + exclude: vec![ + // We manually define `Id` is with an `#ifdef`, so exclude it here + String::from("Id"), + ], + ..Default::default() + }, + language: cbindgen::Language::C, ..Default::default() }; cbindgen::Builder::new() .with_crate(crate_dir) .with_config(config) - .with_language(cbindgen::Language::C) - .with_item_prefix("WGPU") .generate() - .expect("Unable to generate bindings") + .unwrap() .write_to_file("wgpu.h"); } diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index 93499b6c8..4c776af9e 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -1,3 +1,9 @@ +#ifdef WGPU_REMOTE + typedef uint32_t WGPUId; +#else + typedef void *WGPUId; +#endif + #include #include #include @@ -8,8 +14,6 @@ typedef enum { WGPUPowerPreference_HighPerformance = 2, } WGPUPowerPreference; -typedef uint32_t WGPUId; - typedef WGPUId WGPUDeviceId; typedef WGPUId WGPUAdapterId; diff --git a/wgpu-native/Cargo.toml b/wgpu-native/Cargo.toml index 0200a92b9..58694cb0f 100644 --- a/wgpu-native/Cargo.toml +++ b/wgpu-native/Cargo.toml @@ -11,6 +11,7 @@ crate-type = ["lib", "cdylib", "staticlib"] [features] default = [] +remote = [] metal = ["gfx-backend-metal"] dx12 = ["gfx-backend-dx12"] vulkan = ["gfx-backend-vulkan"] diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index c630725c7..c11acbfb4 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -1,7 +1,7 @@ use hal::{self, Device as _Device, QueueGroup}; use {conv, memory, pipeline, resource}; -use registry; +use registry::{self, Registry}; use {BufferId, CommandBufferId, DeviceId, ShaderModuleId}; #[repr(C)] diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 5c3255fa0..3308defcd 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -1,6 +1,6 @@ use hal::{self, Instance as _Instance, PhysicalDevice as _PhysicalDevice}; -use registry; +use registry::{self, Registry}; use {AdapterId, Device, DeviceId, InstanceId}; #[repr(C)] diff --git a/wgpu-native/src/registry.rs b/wgpu-native/src/registry.rs index c418c2827..8d482c246 100644 --- a/wgpu-native/src/registry.rs +++ b/wgpu-native/src/registry.rs @@ -1,47 +1,100 @@ +use std::marker::PhantomData; +use std::os::raw::c_void; use std::sync::{Arc, Mutex}; use std::{borrow, cmp, fmt, ops, ptr}; use hal::backend::FastHashMap; use {AdapterHandle, DeviceHandle, InstanceHandle, ShaderModuleHandle}; +#[cfg(not(feature = "remote"))] +pub(crate) type Id = *mut c_void; +#[cfg(feature = "remote")] pub(crate) type Id = u32; -pub(crate) struct Registry { +pub(crate) trait Registry { + fn new() -> Self; + fn register(&mut self, handle: T) -> Id; + fn get(&self, id: Id) -> Option<&T>; + fn get_mut(&mut self, id: Id) -> Option<&mut T>; +} + +#[cfg(not(feature = "remote"))] +pub(crate) struct LocalRegistry { + marker: PhantomData, +} + +#[cfg(not(feature = "remote"))] +impl Registry for LocalRegistry { + fn new() -> Self { + LocalRegistry { + marker: PhantomData, + } + } + + fn register(&mut self, handle: T) -> Id { + ::std::boxed::Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void + } + + fn get(&self, id: Id) -> Option<&T> { + unsafe { (id as *const T).as_ref() } + } + + fn get_mut(&mut self, id: Id) -> Option<&mut T> { + unsafe { (id as *mut T).as_mut() } + } +} + +#[cfg(feature = "remote")] +pub(crate) struct RemoteRegistry { next_id: Id, tracked: FastHashMap, } -impl Registry { +#[cfg(feature = "remote")] +impl Registry for RemoteRegistry { fn new() -> Self { - Registry { + RemoteRegistry { next_id: 0, tracked: FastHashMap::default(), } } - pub(crate) fn register(&mut self, handle: T) -> Id { + fn register(&mut self, handle: T) -> Id { let id = self.next_id; self.tracked.insert(id, handle); self.next_id += 1; id } - pub(crate) fn get(&self, id: Id) -> Option<&T> { + fn get(&self, id: Id) -> Option<&T> { self.tracked.get(&id) } - pub(crate) fn get_mut(&mut self, id: Id) -> Option<&mut T> { + fn get_mut(&mut self, id: Id) -> Option<&mut T> { self.tracked.get_mut(&id) } } +#[cfg(not(feature = "remote"))] lazy_static! { - pub(crate) static ref ADAPTER_REGISTRY: Arc>> = - Arc::new(Mutex::new(Registry::new())); - pub(crate) static ref DEVICE_REGISTRY: Arc>> = - Arc::new(Mutex::new(Registry::new())); - pub(crate) static ref INSTANCE_REGISTRY: Arc>> = - Arc::new(Mutex::new(Registry::new())); - pub(crate) static ref SHADER_MODULE_REGISTRY: Arc>> = - Arc::new(Mutex::new(Registry::new())); + pub(crate) static ref ADAPTER_REGISTRY: Mutex> = + Mutex::new(LocalRegistry::new()); + pub(crate) static ref DEVICE_REGISTRY: Mutex> = + Mutex::new(LocalRegistry::new()); + pub(crate) static ref INSTANCE_REGISTRY: Mutex> = + Mutex::new(LocalRegistry::new()); + pub(crate) static ref SHADER_MODULE_REGISTRY: Mutex> = + Mutex::new(LocalRegistry::new()); +} + +#[cfg(feature = "remote")] +lazy_static! { + pub(crate) static ref ADAPTER_REGISTRY: Arc>> = + Arc::new(Mutex::new(RemoteRegistry::new())); + pub(crate) static ref DEVICE_REGISTRY: Arc>> = + Arc::new(Mutex::new(RemoteRegistry::new())); + pub(crate) static ref INSTANCE_REGISTRY: Arc>> = + Arc::new(Mutex::new(RemoteRegistry::new())); + pub(crate) static ref SHADER_MODULE_REGISTRY: Arc>> = + Arc::new(Mutex::new(RemoteRegistry::new())); } From 7e6765108b98e958b70e8d582350a7c462073022 Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Mon, 24 Sep 2018 12:39:49 -0600 Subject: [PATCH 7/8] Move mutex inside registry --- wgpu-bindings/src/main.rs | 12 +++++-- wgpu-native/src/device.rs | 8 ++--- wgpu-native/src/instance.rs | 19 +++------- wgpu-native/src/registry.rs | 69 +++++++++++++++++++++---------------- 4 files changed, 56 insertions(+), 52 deletions(-) diff --git a/wgpu-bindings/src/main.rs b/wgpu-bindings/src/main.rs index a1d14669b..42a5d402f 100644 --- a/wgpu-bindings/src/main.rs +++ b/wgpu-bindings/src/main.rs @@ -2,12 +2,20 @@ extern crate cbindgen; use std::path::PathBuf; +const HEADER: &str = " +#ifdef WGPU_REMOTE + typedef uint32_t WGPUId; +#else + typedef void *WGPUId; +#endif +"; + fn main() { let mut crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); crate_dir.push("../wgpu-native"); let config = cbindgen::Config { - header: Some(String::from("#ifdef WGPU_REMOTE\n typedef uint32_t WGPUId;\n#else\n typedef void *WGPUId;\n#endif")), + header: Some(String::from(HEADER.trim())), enumeration: cbindgen::EnumConfig { prefix_with_name: true, ..Default::default() @@ -15,7 +23,7 @@ fn main() { export: cbindgen::ExportConfig { prefix: Some(String::from("WGPU")), exclude: vec![ - // We manually define `Id` is with an `#ifdef`, so exclude it here + // We manually define `Id` is within the header, so exclude it here String::from("Id"), ], ..Default::default() diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index c11acbfb4..8579d1dff 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -36,15 +36,11 @@ pub extern "C" fn wgpu_device_create_shader_module( device_id: DeviceId, desc: pipeline::ShaderModuleDescriptor, ) -> ShaderModuleId { - let device_registry = registry::DEVICE_REGISTRY.lock().unwrap(); - let device = device_registry.get(device_id).unwrap(); + let device = registry::DEVICE_REGISTRY.get(device_id).unwrap(); let shader = device .device .create_shader_module(unsafe { ::std::slice::from_raw_parts(desc.code.bytes, desc.code.length) }).unwrap(); - registry::SHADER_MODULE_REGISTRY - .lock() - .unwrap() - .register(ShaderModule { raw: shader }) + registry::SHADER_MODULE_REGISTRY.register(ShaderModule { raw: shader }) } diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 3308defcd..c3832f884 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -33,9 +33,8 @@ pub extern "C" fn wgpu_create_instance() -> InstanceId { feature = "gfx-backend-metal" ))] { - let mut registry = registry::INSTANCE_REGISTRY.lock().unwrap(); let inst = ::back::Instance::create("wgpu", 1); - registry.register(inst) + registry::INSTANCE_REGISTRY.register(inst) } #[cfg(not(any( feature = "gfx-backend-vulkan", @@ -52,8 +51,7 @@ pub extern "C" fn wgpu_instance_get_adapter( instance_id: InstanceId, desc: AdapterDescriptor, ) -> AdapterId { - let instance_registry = registry::INSTANCE_REGISTRY.lock().unwrap(); - let instance = instance_registry.get(instance_id).unwrap(); + let instance = registry::INSTANCE_REGISTRY.get(instance_id).unwrap(); let (mut low, mut high, mut other) = (None, None, None); for adapter in instance.enumerate_adapters() { match adapter.info.device_type { @@ -67,10 +65,7 @@ pub extern "C" fn wgpu_instance_get_adapter( PowerPreference::LowPower => low.or(high), PowerPreference::HighPerformance | PowerPreference::Default => high.or(low), }; - registry::ADAPTER_REGISTRY - .lock() - .unwrap() - .register(some.or(other).unwrap()) + registry::ADAPTER_REGISTRY.register(some.or(other).unwrap()) } #[no_mangle] @@ -78,12 +73,8 @@ pub extern "C" fn wgpu_adapter_create_device( adapter_id: AdapterId, desc: DeviceDescriptor, ) -> DeviceId { - let mut adapter_registry = registry::ADAPTER_REGISTRY.lock().unwrap(); - let adapter = adapter_registry.get_mut(adapter_id).unwrap(); + let adapter = registry::ADAPTER_REGISTRY.get_mut(adapter_id).unwrap(); let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap(); let mem_props = adapter.physical_device.memory_properties(); - registry::DEVICE_REGISTRY - .lock() - .unwrap() - .register(Device::new(device, queue_group, mem_props)) + registry::DEVICE_REGISTRY.register(Device::new(device, queue_group, mem_props)) } diff --git a/wgpu-native/src/registry.rs b/wgpu-native/src/registry.rs index 8d482c246..f67ec3293 100644 --- a/wgpu-native/src/registry.rs +++ b/wgpu-native/src/registry.rs @@ -13,9 +13,9 @@ pub(crate) type Id = u32; pub(crate) trait Registry { fn new() -> Self; - fn register(&mut self, handle: T) -> Id; + fn register(&self, handle: T) -> Id; fn get(&self, id: Id) -> Option<&T>; - fn get_mut(&mut self, id: Id) -> Option<&mut T>; + fn get_mut(&self, id: Id) -> Option<&mut T>; } #[cfg(not(feature = "remote"))] @@ -31,7 +31,7 @@ impl Registry for LocalRegistry { } } - fn register(&mut self, handle: T) -> Id { + fn register(&self, handle: T) -> Id { ::std::boxed::Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void } @@ -39,62 +39,71 @@ impl Registry for LocalRegistry { unsafe { (id as *const T).as_ref() } } - fn get_mut(&mut self, id: Id) -> Option<&mut T> { + fn get_mut(&self, id: Id) -> Option<&mut T> { unsafe { (id as *mut T).as_mut() } } } #[cfg(feature = "remote")] -pub(crate) struct RemoteRegistry { +struct Registrations { next_id: Id, tracked: FastHashMap, } +#[cfg(feature = "remote")] +impl Registrations { + fn new() -> Self { + Registrations { + next_id: 0, + tracked: FastHashMap::default(), + } + } +} + +#[cfg(feature = "remote")] +pub(crate) struct RemoteRegistry { + registrations: Arc>>, +} + #[cfg(feature = "remote")] impl Registry for RemoteRegistry { fn new() -> Self { RemoteRegistry { - next_id: 0, - tracked: FastHashMap::default(), + registrations: Arc::new(Mutex::new(Registrations::new())), } } - fn register(&mut self, handle: T) -> Id { - let id = self.next_id; - self.tracked.insert(id, handle); - self.next_id += 1; + fn register(&self, handle: T) -> Id { + let mut registrations = self.registrations.lock().unwrap(); + let id = registrations.next_id; + registrations.tracked.insert(id, handle); + registrations.next_id += 1; id } fn get(&self, id: Id) -> Option<&T> { - self.tracked.get(&id) + let registrations = self.registrations.lock().unwrap(); + registrations.tracked.get(&id) } - fn get_mut(&mut self, id: Id) -> Option<&mut T> { - self.tracked.get_mut(&id) + fn get_mut(&self, id: Id) -> Option<&mut T> { + let registrations = self.registrations.lock().unwrap(); + registrations.tracked.get_mut(&id) } } #[cfg(not(feature = "remote"))] lazy_static! { - pub(crate) static ref ADAPTER_REGISTRY: Mutex> = - Mutex::new(LocalRegistry::new()); - pub(crate) static ref DEVICE_REGISTRY: Mutex> = - Mutex::new(LocalRegistry::new()); - pub(crate) static ref INSTANCE_REGISTRY: Mutex> = - Mutex::new(LocalRegistry::new()); - pub(crate) static ref SHADER_MODULE_REGISTRY: Mutex> = - Mutex::new(LocalRegistry::new()); + pub(crate) static ref ADAPTER_REGISTRY: LocalRegistry = LocalRegistry::new(); + pub(crate) static ref DEVICE_REGISTRY: LocalRegistry = LocalRegistry::new(); + pub(crate) static ref INSTANCE_REGISTRY: LocalRegistry = LocalRegistry::new(); + pub(crate) static ref SHADER_MODULE_REGISTRY: LocalRegistry = LocalRegistry::new(); } #[cfg(feature = "remote")] lazy_static! { - pub(crate) static ref ADAPTER_REGISTRY: Arc>> = - Arc::new(Mutex::new(RemoteRegistry::new())); - pub(crate) static ref DEVICE_REGISTRY: Arc>> = - Arc::new(Mutex::new(RemoteRegistry::new())); - pub(crate) static ref INSTANCE_REGISTRY: Arc>> = - Arc::new(Mutex::new(RemoteRegistry::new())); - pub(crate) static ref SHADER_MODULE_REGISTRY: Arc>> = - Arc::new(Mutex::new(RemoteRegistry::new())); + pub(crate) static ref ADAPTER_REGISTRY: RemoteRegistry = RemoteRegistry::new(); + pub(crate) static ref DEVICE_REGISTRY: RemoteRegistry = RemoteRegistry::new(); + pub(crate) static ref INSTANCE_REGISTRY: RemoteRegistry = RemoteRegistry::new(); + pub(crate) static ref SHADER_MODULE_REGISTRY: RemoteRegistry = RemoteRegistry::new(); } From 19ddb63e11cf1db7b0ec3c2635726c8edfc5f8a1 Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Mon, 24 Sep 2018 21:20:03 -0600 Subject: [PATCH 8/8] Fix remote `get_mut` --- Cargo.lock | 1 + wgpu-native/Cargo.toml | 3 ++- wgpu-native/src/device.rs | 2 +- wgpu-native/src/instance.rs | 4 +-- wgpu-native/src/lib.rs | 2 ++ wgpu-native/src/registry.rs | 51 ++++++++++++++++--------------------- 6 files changed, 30 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 64e3e5d6e..42272bac2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -927,6 +927,7 @@ dependencies = [ "gfx-hal 0.1.0 (git+https://github.com/gfx-rs/gfx?rev=a435a05)", "gfx-memory 0.1.0 (git+https://github.com/gfx-rs/gfx-memory?rev=483d64d)", "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/wgpu-native/Cargo.toml b/wgpu-native/Cargo.toml index 58694cb0f..1ddbec910 100644 --- a/wgpu-native/Cargo.toml +++ b/wgpu-native/Cargo.toml @@ -11,7 +11,7 @@ crate-type = ["lib", "cdylib", "staticlib"] [features] default = [] -remote = [] +remote = ["parking_lot"] metal = ["gfx-backend-metal"] dx12 = ["gfx-backend-dx12"] vulkan = ["gfx-backend-vulkan"] @@ -19,6 +19,7 @@ vulkan = ["gfx-backend-vulkan"] [dependencies] bitflags = "1.0" lazy_static = "1.1.0" +parking_lot = { version = "0.6", optional = true } gfx-hal = { git = "https://github.com/gfx-rs/gfx", rev = "a435a05" } # required by gfx-memory gfx-backend-empty = { git = "https://github.com/gfx-rs/gfx", rev = "a435a05" } gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "a435a05", optional = true } diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 8579d1dff..15bc10085 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -36,7 +36,7 @@ pub extern "C" fn wgpu_device_create_shader_module( device_id: DeviceId, desc: pipeline::ShaderModuleDescriptor, ) -> ShaderModuleId { - let device = registry::DEVICE_REGISTRY.get(device_id).unwrap(); + let device = registry::DEVICE_REGISTRY.get_mut(device_id); let shader = device .device .create_shader_module(unsafe { diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index c3832f884..a6a43f288 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -51,7 +51,7 @@ pub extern "C" fn wgpu_instance_get_adapter( instance_id: InstanceId, desc: AdapterDescriptor, ) -> AdapterId { - let instance = registry::INSTANCE_REGISTRY.get(instance_id).unwrap(); + let instance = registry::INSTANCE_REGISTRY.get_mut(instance_id); let (mut low, mut high, mut other) = (None, None, None); for adapter in instance.enumerate_adapters() { match adapter.info.device_type { @@ -73,7 +73,7 @@ pub extern "C" fn wgpu_adapter_create_device( adapter_id: AdapterId, desc: DeviceDescriptor, ) -> DeviceId { - let adapter = registry::ADAPTER_REGISTRY.get_mut(adapter_id).unwrap(); + let mut adapter = registry::ADAPTER_REGISTRY.get_mut(adapter_id); let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap(); let mem_props = adapter.physical_device.memory_properties(); registry::DEVICE_REGISTRY.register(Device::new(device, queue_group, mem_props)) diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index 86f8c111a..db405f85a 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -2,6 +2,8 @@ extern crate bitflags; #[macro_use] extern crate lazy_static; +#[cfg(feature = "remote")] +extern crate parking_lot; #[cfg(feature = "gfx-backend-dx12")] extern crate gfx_backend_dx12 as back; diff --git a/wgpu-native/src/registry.rs b/wgpu-native/src/registry.rs index f67ec3293..08a072ec9 100644 --- a/wgpu-native/src/registry.rs +++ b/wgpu-native/src/registry.rs @@ -1,6 +1,9 @@ use std::marker::PhantomData; use std::os::raw::c_void; -use std::sync::{Arc, Mutex}; +#[cfg(feature = "remote")] +use std::sync::Arc; +#[cfg(feature = "remote")] +use parking_lot::{Mutex, MutexGuard, MappedMutexGuard}; use std::{borrow, cmp, fmt, ops, ptr}; use hal::backend::FastHashMap; @@ -11,11 +14,15 @@ pub(crate) type Id = *mut c_void; #[cfg(feature = "remote")] pub(crate) type Id = u32; +#[cfg(not(feature = "remote"))] +type RegistryItem<'a, T> = &'a mut T; +#[cfg(feature = "remote")] +type RegistryItem<'a, T> = MappedMutexGuard<'a, T>; + pub(crate) trait Registry { fn new() -> Self; fn register(&self, handle: T) -> Id; - fn get(&self, id: Id) -> Option<&T>; - fn get_mut(&self, id: Id) -> Option<&mut T>; + fn get_mut(&self, id: Id) -> RegistryItem; } #[cfg(not(feature = "remote"))] @@ -35,12 +42,8 @@ impl Registry for LocalRegistry { ::std::boxed::Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void } - fn get(&self, id: Id) -> Option<&T> { - unsafe { (id as *const T).as_ref() } - } - - fn get_mut(&self, id: Id) -> Option<&mut T> { - unsafe { (id as *mut T).as_mut() } + fn get_mut(&self, id: Id) -> RegistryItem { + unsafe { (id as *mut T).as_mut() }.unwrap() } } @@ -74,36 +77,26 @@ impl Registry for RemoteRegistry { } fn register(&self, handle: T) -> Id { - let mut registrations = self.registrations.lock().unwrap(); + let mut registrations = self.registrations.lock(); let id = registrations.next_id; registrations.tracked.insert(id, handle); registrations.next_id += 1; id } - fn get(&self, id: Id) -> Option<&T> { - let registrations = self.registrations.lock().unwrap(); - registrations.tracked.get(&id) - } - - fn get_mut(&self, id: Id) -> Option<&mut T> { - let registrations = self.registrations.lock().unwrap(); - registrations.tracked.get_mut(&id) + fn get_mut(&self, id: Id) -> RegistryItem { + MutexGuard::map(self.registrations.lock(), |r| r.tracked.get_mut(&id).unwrap()) } } #[cfg(not(feature = "remote"))] -lazy_static! { - pub(crate) static ref ADAPTER_REGISTRY: LocalRegistry = LocalRegistry::new(); - pub(crate) static ref DEVICE_REGISTRY: LocalRegistry = LocalRegistry::new(); - pub(crate) static ref INSTANCE_REGISTRY: LocalRegistry = LocalRegistry::new(); - pub(crate) static ref SHADER_MODULE_REGISTRY: LocalRegistry = LocalRegistry::new(); -} - +type ConcreteRegistry = LocalRegistry; #[cfg(feature = "remote")] +type ConcreteRegistry = RemoteRegistry; + lazy_static! { - pub(crate) static ref ADAPTER_REGISTRY: RemoteRegistry = RemoteRegistry::new(); - pub(crate) static ref DEVICE_REGISTRY: RemoteRegistry = RemoteRegistry::new(); - pub(crate) static ref INSTANCE_REGISTRY: RemoteRegistry = RemoteRegistry::new(); - pub(crate) static ref SHADER_MODULE_REGISTRY: RemoteRegistry = RemoteRegistry::new(); + pub(crate) static ref ADAPTER_REGISTRY: ConcreteRegistry = ConcreteRegistry::new(); + pub(crate) static ref DEVICE_REGISTRY: ConcreteRegistry = ConcreteRegistry::new(); + pub(crate) static ref INSTANCE_REGISTRY: ConcreteRegistry = ConcreteRegistry::new(); + pub(crate) static ref SHADER_MODULE_REGISTRY: ConcreteRegistry = ConcreteRegistry::new(); }