diff --git a/.travis.yml b/.travis.yml index ab52d84226..d5fa7ea9e0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -57,10 +57,8 @@ before_install: script: - cargo test - - (cd wgpu-native && cargo check --features local,glutin) - # TODO: Temporarily only test building the gl backend, properly test when it is usable from C - - if [[ $TRAVIS_OS_NAME == "windows" ]]; then (cd wgpu-native && cargo check --features gfx-backend-dx11); fi - - if [[ $TRAVIS_OS_NAME == "windows" ]]; then (cd wgpu-native && cargo check --features gfx-backend-dx12); fi + # TODO: enable GL backend + #- (cd wgpu-native && cargo check --features local,glutin) - if [[ $TRAVIS_OS_NAME == "linux" ]]; then cargo check --release; fi - if [[ $TRAVIS_RUST_VERSION == "nightly" ]]; then cargo +nightly install cbindgen; fi - if [[ $TRAVIS_RUST_VERSION == "nightly" ]] && [[ $TRAVIS_OS_NAME == "windows" ]]; then diff --git a/Cargo.lock b/Cargo.lock index 607eadd172..521a1960cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -162,6 +162,15 @@ dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "d3d12" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "derivative" version = "1.0.2" @@ -242,6 +251,39 @@ name = "gcc" version = "0.3.55" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "gfx-backend-dx11" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "derivative 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "gfx-hal 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "range-alloc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", + "spirv_cross 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "wio 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "gfx-backend-dx12" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "d3d12 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "derivative 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "gfx-hal 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "range-alloc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", + "spirv_cross 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "gfx-backend-empty" version = "0.3.0" @@ -362,6 +404,15 @@ name = "libc" version = "0.2.55" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "lock_api" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "lock_api" version = "0.3.1" @@ -480,6 +531,23 @@ dependencies = [ "objc 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "owning_ref" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "parking_lot" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "parking_lot" version = "0.9.0" @@ -490,6 +558,18 @@ dependencies = [ "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "parking_lot_core" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.6.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.9 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "parking_lot_core" version = "0.6.1" @@ -707,6 +787,11 @@ dependencies = [ "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "scopeguard" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "scopeguard" version = "1.0.0" @@ -762,6 +847,16 @@ name = "smallvec" version = "0.6.9" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "spirv_cross" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", + "js-sys 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "spirv_cross" version = "0.15.0" @@ -772,6 +867,11 @@ dependencies = [ "wasm-bindgen 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "stable_deref_trait" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "storage-map" version = "0.2.0" @@ -887,6 +987,8 @@ dependencies = [ "arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "copyless 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "gfx-backend-dx11 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gfx-backend-dx12 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "gfx-backend-empty 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "gfx-backend-metal 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "gfx-backend-vulkan 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -941,6 +1043,14 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "wio" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ws2_32-sys" version = "0.2.1" @@ -982,6 +1092,7 @@ dependencies = [ "checksum core-graphics 0.17.3 (registry+https://github.com/rust-lang/crates.io-index)" = "56790968ab1c8a1202a102e6de05fc6e1ec87da99e4e93e9a7d13efbfc1e95a9" "checksum crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "0f0ed1a4de2235cabda8558ff5840bffb97fcb64c97827f354a451307df5f72b" "checksum crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f8306fcef4a7b563b76b7dd949ca48f52bc1141aa067d2ea09565f3e2652aa5c" +"checksum d3d12 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4fda5547c55c93b070d59108464bbfd7d9da9563b2ce78fceefc6430e972a420" "checksum derivative 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6073e9676dbebdddeabaeb63e3b7cefd23c86f5c41d381ee1237cc77b1079898" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" "checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" @@ -993,6 +1104,8 @@ dependencies = [ "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum fxhash 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" "checksum gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)" = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" +"checksum gfx-backend-dx11 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "79b2bfb8b9c8d020062ab8c43e5bc48a049b780a638069cd9c261291ce8c83da" +"checksum gfx-backend-dx12 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "42a7ad96014d4b610d6846edf97dd70479374c38f5a64908abeb86c99b4c4fab" "checksum gfx-backend-empty 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "19ee69c0aaa5b96b0995914e7136676ba8a9b5bd8a87d8b21f26641e72aa309b" "checksum gfx-backend-metal 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a11d55ddb58c69f179bbcef317ffd7d8bac80251fd19be6f980ace2348ac31" "checksum gfx-backend-vulkan 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "057ee275c9802b98aa68777844dc986f5693204f6e93830b5302379eaaa93fb7" @@ -1004,6 +1117,7 @@ dependencies = [ "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" "checksum libc 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)" = "42914d39aad277d9e176efbdad68acb1d5443ab65afe0e0e4f0d49352a950880" +"checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c" "checksum lock_api 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f8912e782533a93a167888781b836336a6ca5da6175c05944c86cf28c31104dc" "checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" "checksum malloc_buf 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" @@ -1016,7 +1130,10 @@ dependencies = [ "checksum objc-foundation 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9" "checksum objc_exception 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "098cd29a2fa3c230d3463ae069cecccc3fdfd64c0d2496ab5b96f82dab6a00dc" "checksum objc_id 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b" +"checksum owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13" +"checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337" "checksum parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252" +"checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9" "checksum parking_lot_core 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1a7bbaa05312363e0480e1efee133fff1a09ef4a6406b65e226b9a793c223a32" "checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" "checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" @@ -1041,6 +1158,7 @@ dependencies = [ "checksum rendy-memory 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "557ed61c95042de292b4347124e9e77ec76954618cb6eeb50cec1a98d3adf51f" "checksum rustc-demangle 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "ccc78bfd5acd7bf3e89cffcf899e5cb1a52d6fafa8dec2739ad70c9577a57288" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b42e15e59b18a828bbf5c58ea01debb36b9b096346de35d941dcb89009f24a0d" "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" @@ -1049,7 +1167,9 @@ dependencies = [ "checksum shared_library 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5a9e7e0f2bfae24d8a5b5a66c5b257a83c7412304311512a0c054cd5e619da11" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c4488ae950c49d403731982257768f48fada354a5203fe81f9bb6f43ca9002be" +"checksum spirv_cross 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b03b5986fa339f976b69dc363b4facb9df5683f40ec48c9716c38b42a4cb10cc" "checksum spirv_cross 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cd72403fee8f7ec2f4f453dfbaf39ccdd4c6d0157d67d6e65513cd26cc49289f" +"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" "checksum storage-map 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fd0a4829a5c591dc24a944a736d6b1e4053e51339a79fd5d4702c4c999a9c45e" "checksum syn 0.15.34 (registry+https://github.com/rust-lang/crates.io-index)" = "a1393e4a97a19c01e900df2aec855a29f71cf02c402e2f443b8d2747c25c5dbe" "checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f" @@ -1067,5 +1187,6 @@ dependencies = [ "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" "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 wio 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5d129932f4644ac2396cb456385cbf9e63b5b30c6e8dc4820bdca4eb082037a5" "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" "checksum x11 2.18.1 (registry+https://github.com/rust-lang/crates.io-index)" = "39697e3123f715483d311b5826e254b6f3cfebdd83cf7ef3358f579c3d68e235" diff --git a/examples/compute/main.c b/examples/compute/main.c index 879396554e..35d6a0f09d 100644 --- a/examples/compute/main.c +++ b/examples/compute/main.c @@ -17,7 +17,7 @@ int main( char *argv[]) { if (argc != 5) { - printf("You must pass 4 positive integers!"); + printf("You must pass 4 positive integers!\n"); return 0; } diff --git a/examples/framework.c b/examples/framework.c index 82a09e93cb..46f73dd1d3 100644 --- a/examples/framework.c +++ b/examples/framework.c @@ -8,6 +8,10 @@ WGPUU32Array read_file(const char *name) { FILE *file = fopen(name, "rb"); + if (!file) { + printf("Unable to open %s\n", name); + exit(1); + } fseek(file, 0, SEEK_END); long length = ftell(file); unsigned char *bytes = malloc(length); @@ -27,7 +31,7 @@ void read_buffer_map( (void)userdata; if (status == WGPUBufferMapAsyncStatus_Success) { uint32_t *times = (uint32_t *) data; - printf("Times: [%d, %d, %d, %d]", + printf("Times: [%d, %d, %d, %d]\n", times[0], times[1], times[2], diff --git a/examples/triangle/main.c b/examples/triangle/main.c index 36b1536155..0de9812627 100644 --- a/examples/triangle/main.c +++ b/examples/triangle/main.c @@ -36,6 +36,7 @@ int main() { WGPUAdapterId adapter = wgpu_request_adapter( &(WGPURequestAdapterOptions){ .power_preference = WGPUPowerPreference_LowPower, + .backends = 2 | 4 | 8, }); WGPUDeviceId device = wgpu_adapter_request_device(adapter, @@ -155,7 +156,7 @@ int main() { [ns_window.contentView setWantsLayer:YES]; metal_layer = [CAMetalLayer layer]; [ns_window.contentView setLayer:metal_layer]; - surface = wgpu_create_surface_from_macos_layer(metal_layer); + surface = wgpu_create_surface_from_metal_layer(metal_layer); } #elif WGPU_TARGET == WGPU_TARGET_LINUX { diff --git a/wgpu-native/Cargo.toml b/wgpu-native/Cargo.toml index 760ba0bffe..c44fc6daaf 100644 --- a/wgpu-native/Cargo.toml +++ b/wgpu-native/Cargo.toml @@ -41,7 +41,11 @@ vec_map = "0.8" gfx-backend-vulkan = { version = "0.3.0", features = ["x11"] } [target.'cfg(not(unix))'.dependencies] -gfx-backend-vulkan = "0.3.0" +gfx-backend-vulkan = "0.3" [target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies] gfx-backend-metal = "0.3" + +[target.'cfg(windows)'.dependencies] +gfx-backend-dx12 = "0.3" +gfx-backend-dx11 = "0.3" diff --git a/wgpu-native/src/hub.rs b/wgpu-native/src/hub.rs index 5ba8624d64..ca42563bc3 100644 --- a/wgpu-native/src/hub.rs +++ b/wgpu-native/src/hub.rs @@ -1,4 +1,5 @@ use crate::{ + backend, id::{Input, Output}, Adapter, AdapterId, @@ -156,10 +157,8 @@ pub trait Access {} pub enum Root {} //TODO: establish an order instead of declaring all the pairs. -#[cfg(not(feature = "gfx-backend-gl"))] impl Access for Root {} impl Access for Root {} -#[cfg(not(feature = "gfx-backend-gl"))] impl Access for Instance {} impl Access> for Root {} impl Access> for Surface {} @@ -296,7 +295,7 @@ impl Registry { #[cfg(feature = "remote")] pub fn new_identity(&self, id_in: Input) -> (I, Output) { - //debug_assert_eq!(self.backend, id_in.backend()); + //TODO: debug_assert_eq!(self.backend, id_in.backend()); (id_in, PhantomData) } @@ -379,9 +378,13 @@ impl Default for Hub { #[derive(Debug, Default)] pub struct Hubs { - vulkan: Hub, + vulkan: Hub, #[cfg(any(target_os = "ios", target_os = "macos"))] - metal: Hub, + metal: Hub, + #[cfg(windows)] + dx12: Hub, + #[cfg(windows)] + dx11: Hub, } #[derive(Debug)] @@ -405,7 +408,7 @@ pub trait GfxBackend: hal::Backend { fn get_surface_mut(surface: &mut Surface) -> &mut Self::Surface; } -impl GfxBackend for gfx_backend_vulkan::Backend { +impl GfxBackend for backend::Vulkan { const VARIANT: Backend = Backend::Vulkan; fn hub() -> &'static Hub { &GLOBAL.hubs.vulkan @@ -416,7 +419,7 @@ impl GfxBackend for gfx_backend_vulkan::Backend { } #[cfg(any(target_os = "ios", target_os = "macos"))] -impl GfxBackend for gfx_backend_metal::Backend { +impl GfxBackend for backend::Metal { const VARIANT: Backend = Backend::Metal; fn hub() -> &'static Hub { &GLOBAL.hubs.metal @@ -425,3 +428,25 @@ impl GfxBackend for gfx_backend_metal::Backend { &mut surface.metal } } + +#[cfg(windows)] +impl GfxBackend for backend::Dx12 { + const VARIANT: Backend = Backend::Dx12; + fn hub() -> &'static Hub { + &GLOBAL.hubs.dx12 + } + fn get_surface_mut(surface: &mut Surface) -> &mut Self::Surface { + surface.dx12.as_mut().unwrap() + } +} + +#[cfg(windows)] +impl GfxBackend for backend::Dx11 { + const VARIANT: Backend = Backend::Dx11; + fn hub() -> &'static Hub { + &GLOBAL.hubs.dx11 + } + fn get_surface_mut(surface: &mut Surface) -> &mut Self::Surface { + &mut surface.dx11 + } +} diff --git a/wgpu-native/src/id.rs b/wgpu-native/src/id.rs index b7e28f6342..90aa67fbc5 100644 --- a/wgpu-native/src/id.rs +++ b/wgpu-native/src/id.rs @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize}; use std::{fmt, marker::PhantomData}; const BACKEND_BITS: usize = 3; +const EPOCH_MASK: u32 = (1 << (32 - BACKEND_BITS)) - 1; type Dummy = crate::backend::Empty; #[repr(transparent)] @@ -15,10 +16,10 @@ impl Id { match self.0 >> (64 - BACKEND_BITS) as u8 { 0 => Backend::Empty, 1 => Backend::Vulkan, - 2 => Backend::Gl, - 3 => Backend::Metal, - 4 => Backend::Dx12, - 5 => Backend::Dx11, + 2 => Backend::Metal, + 3 => Backend::Dx12, + 4 => Backend::Dx11, + 5 => Backend::Gl, _ => unreachable!(), } } @@ -57,13 +58,17 @@ pub trait TypedId { impl TypedId for Id { fn zip(index: Index, epoch: Epoch, backend: Backend) -> Self { - assert_eq!(0, epoch >> 32 - BACKEND_BITS); + assert_eq!(0, epoch >> (32 - BACKEND_BITS)); let v = index as u64 | ((epoch as u64) << 32) | ((backend as u64) << (64 - BACKEND_BITS)); Id(v, PhantomData) } fn unzip(self) -> (Index, Epoch, Backend) { - (self.0 as u32, (self.0 >> 32) as u32, self.backend()) + ( + self.0 as u32, + (self.0 >> 32) as u32 & EPOCH_MASK, + self.backend(), + ) } } @@ -103,3 +108,18 @@ pub type ComputePassId = Id>; // Swap chain pub type SurfaceId = Id; pub type SwapChainId = Id>; + +#[test] +fn test_id_backend() { + for &b in &[ + Backend::Empty, + Backend::Vulkan, + Backend::Metal, + Backend::Dx12, + Backend::Dx11, + Backend::Gl, + ] { + let id: Id<()> = Id::zip(0, 0, b); + assert_eq!(id.backend(), b); + } +} diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 263d49da95..3e106bb31c 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -1,4 +1,5 @@ use crate::{ + backend, binding_model::MAX_BIND_GROUPS, device::BIND_BUFFER_ALIGNMENT, hub::{GfxBackend, Token, GLOBAL}, @@ -23,22 +24,32 @@ use hal::{self, Instance as _, PhysicalDevice as _}; use std::marker::PhantomData; -#[derive(Debug, Default)] +#[derive(Debug)] pub struct Instance { vulkan: Option, #[cfg(any(target_os = "ios", target_os = "macos"))] metal: gfx_backend_metal::Instance, + #[cfg(windows)] + dx12: Option, + #[cfg(windows)] + dx11: gfx_backend_dx11::Instance, } impl Instance { pub(crate) fn new(name: &str, version: u32) -> Self { Instance { - #[cfg(any(unix, windows))] - vulkan: Some(gfx_backend_vulkan::Instance::create(name, version)), - #[cfg(not(any(unix, windows)))] - vulkan: None, + //TODO: reconsider once `create` returns a `Result` + vulkan: if cfg!(all(unix, not(target_os = "ios"), not(target_os = "macos"))) { + Some(gfx_backend_vulkan::Instance::create(name, version)) + } else { + None + }, #[cfg(any(target_os = "ios", target_os = "macos"))] metal: gfx_backend_metal::Instance::create(name, version), + #[cfg(windows)] + dx12: Some(gfx_backend_dx12::Instance::create(name, version)), + #[cfg(windows)] + dx11: gfx_backend_dx11::Instance::create(name, version), } } } @@ -48,9 +59,13 @@ type GfxSurface = ::Surface; #[derive(Debug)] pub struct Surface { pub(crate) swap_chain: Option, - pub(crate) vulkan: Option>, + pub(crate) vulkan: Option>, #[cfg(any(target_os = "ios", target_os = "macos"))] - pub(crate) metal: GfxSurface, + pub(crate) metal: GfxSurface, + #[cfg(windows)] + pub(crate) dx12: Option>, + #[cfg(windows)] + pub(crate) dx11: GfxSurface, } #[derive(Debug)] @@ -138,15 +153,6 @@ pub struct DeviceDescriptor { pub limits: Limits, } -#[cfg(all(not(feature = "remote"), feature = "glutin"))] -pub fn wgpu_create_gl_surface( - windowed_context: back::glutin::RawContext, -) -> SurfaceId { - let raw = back::Surface::from_context(windowed_context); - let surface = SurfaceHandle::new(raw); - GLOBAL.surfaces.register_local(surface, &mut Token::root()) -} - #[cfg(not(feature = "remote"))] pub fn wgpu_create_surface(raw_handle: raw_window_handle::RawWindowHandle) -> SurfaceId { use raw_window_handle::RawWindowHandle as Rwh; @@ -172,7 +178,7 @@ pub fn wgpu_create_surface(raw_handle: raw_window_handle::RawWindowHandle) -> Su .metal .create_surface_from_nsview(h.ns_view, cfg!(debug_assertions)), }, - #[cfg(unix)] + #[cfg(all(unix, not(target_os = "ios"), not(target_os = "macos")))] Rwh::X11(h) => Surface { swap_chain: None, vulkan: instance @@ -180,7 +186,7 @@ pub fn wgpu_create_surface(raw_handle: raw_window_handle::RawWindowHandle) -> Su .as_ref() .map(|inst| inst.create_surface_from_xlib(h.display as _, h.window as _)), }, - #[cfg(unix)] + #[cfg(all(unix, not(target_os = "ios"), not(target_os = "macos")))] Rwh::Wayland(h) => Surface { swap_chain: None, vulkan: instance @@ -195,10 +201,11 @@ pub fn wgpu_create_surface(raw_handle: raw_window_handle::RawWindowHandle) -> Su .vulkan .as_ref() .map(|inst| inst.create_surface_from_hwnd(std::ptr::null_mut(), h.hwnd)), - //dx11: instance.dx11.create_surface_from_hwnd(h.hwnd), - //dx12: instance.dx11 - // .as_ref() - // .map(|inst| inst.create_surface_from_hwnd(h.hwnd), + dx12: instance + .dx12 + .as_ref() + .map(|inst| inst.create_surface_from_hwnd(h.hwnd)), + dx11: instance.dx11.create_surface_from_hwnd(h.hwnd), }, _ => panic!("Unsupported window handle"), }; @@ -209,7 +216,7 @@ pub fn wgpu_create_surface(raw_handle: raw_window_handle::RawWindowHandle) -> Su .register_identity(PhantomData, surface, &mut token) } -#[cfg(all(not(feature = "remote"), unix))] +#[cfg(all(not(feature = "remote"), unix, not(target_os = "ios"), not(target_os = "macos")))] #[no_mangle] pub extern "C" fn wgpu_create_surface_from_xlib( display: *mut *const std::ffi::c_void, @@ -228,15 +235,11 @@ pub extern "C" fn wgpu_create_surface_from_xlib( pub extern "C" fn wgpu_create_surface_from_metal_layer(layer: *mut std::ffi::c_void) -> SurfaceId { let surface = Surface { swap_chain: None, - vulkan: GLOBAL - .instance - .vulkan - .as_ref() - .map(|inst| inst.create_surface_from_layer(h.ns_view)), + vulkan: None, //TODO: currently requires `NSView` metal: GLOBAL .instance .metal - .create_surface_from_nsview(h.ns_view, cfg!(debug_assertions)), + .create_surface_from_layer(layer as *mut _, cfg!(debug_assertions)), }; GLOBAL @@ -250,20 +253,15 @@ pub extern "C" fn wgpu_create_surface_from_windows_hwnd( _hinstance: *mut std::ffi::c_void, hwnd: *mut std::ffi::c_void, ) -> SurfaceId { - use raw_window_handle::windows::Handle; + use raw_window_handle::windows::WindowsHandle; wgpu_create_surface(raw_window_handle::RawWindowHandle::Windows( - raw_window_handle::windows::Handle { + raw_window_handle::windows::WindowsHandle { hwnd, - ..Handle::empty() + ..WindowsHandle::empty() }, )) } -#[cfg(all(not(feature = "remote"), feature = "gfx-backend-gl"))] -pub fn wgpu_get_gl_surface() -> SurfaceId { - instance_id -} - pub fn request_adapter( desc: &RequestAdapterOptions, input_ids: &[Input], @@ -285,51 +283,67 @@ pub fn request_adapter( let id_vulkan = find_input(Backend::Vulkan); let id_metal = find_input(Backend::Metal); + let id_dx12 = find_input(Backend::Dx12); + let id_dx11 = find_input(Backend::Dx11); let mut adapters_vk = match instance.vulkan { - Some(ref inst) if id_vulkan.is_some() => inst.enumerate_adapters(), + Some(ref inst) if id_vulkan.is_some() => { + let adapters = inst.enumerate_adapters(); + device_types.extend(adapters.iter().map(|ad| ad.info.device_type.clone())); + adapters + } _ => Vec::new(), }; - device_types.extend(adapters_vk.iter().map(|ad| ad.info.device_type.clone())); #[cfg(any(target_os = "ios", target_os = "macos"))] - let mut adaptres_mtl = if id_metal.is_some() { + let mut adapters_mtl = if id_metal.is_some() { let adapters = instance.metal.enumerate_adapters(); device_types.extend(adapters.iter().map(|ad| ad.info.device_type.clone())); adapters } else { Vec::new() }; + #[cfg(windows)] + let mut adapters_dx12 = match instance.dx12 { + Some(ref inst) if id_dx12.is_some() => { + let adapters = inst.enumerate_adapters(); + device_types.extend(adapters.iter().map(|ad| ad.info.device_type.clone())); + adapters + } + _ => Vec::new(), + }; + #[cfg(windows)] + let mut adapters_dx11 = if id_dx11.is_some() { + let adapters = instance.dx11.enumerate_adapters(); + device_types.extend(adapters.iter().map(|ad| ad.info.device_type.clone())); + adapters + } else { + Vec::new() + }; if device_types.is_empty() { panic!("No adapters are available!"); } - let (mut integrated_first, mut discrete_first, mut discrete_last) = (None, None, None); + let (mut integrated, mut discrete, mut other) = (None, None, None); - // On Windows > 1803, dx12 enumerate_adapters returns the adapters in order from highest to - // lowest performance. Therefore, the first found adapter in each category is selected. - // - // TODO: move power/performance policy querying into gfx, which has more context into - // performance policy than wgpu for (i, ty) in device_types.into_iter().enumerate() { match ty { hal::adapter::DeviceType::IntegratedGpu => { - integrated_first = integrated_first.or(Some(i)); + integrated = integrated.or(Some(i)); } hal::adapter::DeviceType::DiscreteGpu => { - discrete_first = discrete_first.or(Some(i)); - discrete_last = Some(i); + discrete = discrete.or(Some(i)); + } + _ => { + other = other.or(Some(i)); } - _ => {} } } let preferred_gpu = match desc.power_preference { - // If `LowPower`, prefer lowest power `DiscreteGPU` - PowerPreference::LowPower => integrated_first.or(discrete_last), - PowerPreference::HighPerformance | PowerPreference::Default => { - discrete_first.or(integrated_first) - } + PowerPreference::Default => integrated.or(discrete).or(other), + PowerPreference::LowPower => integrated.or(other).or(discrete), + PowerPreference::HighPerformance => discrete.or(other).or(integrated), }; let mut token = Token::root(); @@ -340,9 +354,11 @@ pub fn request_adapter( raw: adapters_vk.swap_remove(selected), }; info!("Adapter Vulkan {:?}", adapter.raw.info); - let id_out = gfx_backend_vulkan::Backend::hub() - .adapters - .register_identity(id_vulkan.unwrap(), adapter, &mut token); + let id_out = backend::Vulkan::hub().adapters.register_identity( + id_vulkan.unwrap(), + adapter, + &mut token, + ); return Some(id_out); } selected -= adapters_vk.len(); @@ -354,14 +370,45 @@ pub fn request_adapter( raw: adapters_mtl.swap_remove(selected), }; info!("Adapter Metal {:?}", adapter.raw.info); - let id_out = gfx_backend_metal::Backend::hub() - .adapters - .register_identity(id_metal.unwrap(), adapter, &mut token); + let id_out = backend::Metal::hub().adapters.register_identity( + id_metal.unwrap(), + adapter, + &mut token, + ); return Some(id_out); } selected -= adapters_mtl.len(); } - let _ = (selected, id_metal); + #[cfg(windows)] + { + if selected < adapters_dx12.len() { + let adapter = Adapter { + raw: adapters_dx12.swap_remove(selected), + }; + info!("Adapter Dx12 {:?}", adapter.raw.info); + let id_out = backend::Dx12::hub().adapters.register_identity( + id_dx12.unwrap(), + adapter, + &mut token, + ); + return Some(id_out); + } + selected -= adapters_dx12.len(); + if selected < adapters_dx11.len() { + let adapter = Adapter { + raw: adapters_dx11.swap_remove(selected), + }; + info!("Adapter Dx11 {:?}", adapter.raw.info); + let id_out = backend::Dx11::hub().adapters.register_identity( + id_dx11.unwrap(), + adapter, + &mut token, + ); + return Some(id_out); + } + selected -= adapters_dx11.len(); + } + let _ = (selected, id_metal, id_dx12, id_dx11); None } diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index 4abeaff9bd..de10e2a27f 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -1,4 +1,8 @@ pub mod backend { + #[cfg(windows)] + pub use gfx_backend_dx11::Backend as Dx11; + #[cfg(windows)] + pub use gfx_backend_dx12::Backend as Dx12; pub use gfx_backend_empty::Backend as Empty; #[cfg(any(target_os = "ios", target_os = "macos"))] pub use gfx_backend_metal::Backend as Metal; @@ -29,9 +33,6 @@ pub use self::resource::*; pub use self::swap_chain::*; pub use hal::pso::read_spirv; -#[cfg(feature = "glutin")] -pub use back::glutin; - use std::{ os::raw::c_char, ptr, @@ -47,10 +48,10 @@ type Epoch = u32; pub enum Backend { Empty = 0, Vulkan = 1, - Gl = 2, - Metal = 3, - Dx12 = 4, - Dx11 = 5, + Metal = 2, + Dx12 = 3, + Dx11 = 4, + Gl = 5, } pub type BufferAddress = u64; @@ -204,6 +205,10 @@ macro_rules! gfx_select { $crate::Backend::Vulkan => $function::<$crate::backend::Vulkan>( $($param),+ ), #[cfg(any(target_os = "ios", target_os = "macos"))] $crate::Backend::Metal => $function::<$crate::backend::Metal>( $($param),+ ), + #[cfg(windows)] + $crate::Backend::Dx12 => $function::<$crate::backend::Dx12>( $($param),+ ), + #[cfg(windows)] + $crate::Backend::Dx11 => $function::<$crate::backend::Dx11>( $($param),+ ), _ => unreachable!() } }; diff --git a/wgpu-remote/src/lib.rs b/wgpu-remote/src/lib.rs index 14c5174b32..a157677f33 100644 --- a/wgpu-remote/src/lib.rs +++ b/wgpu-remote/src/lib.rs @@ -61,6 +61,8 @@ struct Identities { vulkan: IdentityHub, #[cfg(any(target_os = "ios", target_os = "macos"))] metal: IdentityHub, + #[cfg(windows)] + dx12: IdentityHub, } impl Identities { @@ -70,6 +72,8 @@ impl Identities { vulkan: IdentityHub::new(Backend::Vulkan), #[cfg(any(target_os = "ios", target_os = "macos"))] metal: IdentityHub::new(Backend::Metal), + #[cfg(windows)] + dx12: IdentityHub::new(Backend::Dx12), } } @@ -78,6 +82,8 @@ impl Identities { Backend::Vulkan => &mut self.vulkan, #[cfg(any(target_os = "ios", target_os = "macos"))] Backend::Metal => &mut self.metal, + #[cfg(windows)] + Backend::Dx12 => &mut self.dx12, _ => panic!("Unexpected backend: {:?}", backend), } } @@ -141,6 +147,8 @@ pub extern "C" fn wgpu_client_request_adapter( identities.vulkan.adapters.alloc(), #[cfg(any(target_os = "ios", target_os = "macos"))] identities.metal.adapters.alloc(), + #[cfg(windows)] + identities.dx12.adapters.alloc(), ]; let msg = GlobalMessage::RequestAdapter(desc.clone(), ids); client.channel.send(msg).unwrap();