From fe18434bff923e2fbb06716c54bff30d0d483676 Mon Sep 17 00:00:00 2001 From: Johan Andersson Date: Mon, 26 Oct 2020 15:49:01 +0100 Subject: [PATCH] Add example to evaluate sky shader on CPU (#141) --- .github/workflows/ci.yaml | 3 + Cargo.lock | 494 ++++++++++++++++++++++-- Cargo.toml | 1 + deny.toml | 10 +- examples/example-runner-cpu/Cargo.toml | 17 + examples/example-runner-cpu/src/main.rs | 57 +++ examples/example-shader/src/lib.rs | 46 ++- spirv-std/src/math/vec2.rs | 2 +- 8 files changed, 593 insertions(+), 37 deletions(-) create mode 100644 examples/example-runner-cpu/Cargo.toml create mode 100644 examples/example-runner-cpu/src/main.rs diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index bbfe4b00e8..03aca8cf6b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -17,6 +17,8 @@ jobs: spirv_tools_version: "20200928" steps: - uses: actions/checkout@v2 + - if: ${{ runner.os == 'Linux' }} + run: sudo apt install libwayland-cursor0 libxkbcommon-dev libwayland-dev - if: ${{ runner.os != 'Windows' }} run: brew install spirv-tools # Currently SPIR-V tools aren't available in any package manager @@ -43,6 +45,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - run: sudo apt install libwayland-cursor0 libxkbcommon-dev libwayland-dev - run: rustup component add rustfmt clippy rust-src rustc-dev llvm-tools-preview - run: cargo fmt --all -- --check - run: cargo clippy --workspace --exclude example-runner --all-targets -- -D warnings diff --git a/Cargo.lock b/Cargo.lock index 7a58455a61..d8a3de90de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -77,6 +77,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "autocfg" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" + [[package]] name = "autocfg" version = "1.0.1" @@ -114,7 +120,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b036167e76041694579972c28cf4877b4f92da222560ddb49008937b6a6727c" dependencies = [ "log", - "nix", + "nix 0.18.0", ] [[package]] @@ -150,6 +156,15 @@ dependencies = [ "vec_map", ] +[[package]] +name = "cloudabi" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +dependencies = [ + "bitflags", +] + [[package]] name = "cloudabi" version = "0.1.0" @@ -205,6 +220,12 @@ dependencies = [ "objc", ] +[[package]] +name = "const_fn" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce90df4c658c62f12d78f7508cf92f9173e5184a539c10bfe54a3107b3ffd0f2" + [[package]] name = "core-foundation" version = "0.7.0" @@ -287,6 +308,53 @@ dependencies = [ "objc", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0f606a85340376eef0d6d8fec399e6d4a544d648386c6645eb6d0653b27d9f" +dependencies = [ + "cfg-if 1.0.0", + "const_fn", + "crossbeam-utils", + "lazy_static", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec91540d98355f690a86367e566ecad2e9e579f230230eb7c21398372be73ea5" +dependencies = [ + "autocfg 1.0.1", + "cfg-if 1.0.0", + "const_fn", + "lazy_static", +] + [[package]] name = "ctor" version = "0.1.16" @@ -384,6 +452,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + [[package]] name = "example-runner" version = "0.1.0" @@ -397,6 +471,16 @@ dependencies = [ "winit", ] +[[package]] +name = "example-runner-cpu" +version = "0.1.0" +dependencies = [ + "example-shader", + "minifb", + "rayon", + "spirv-std", +] + [[package]] name = "example-shader" version = "0.1.0" @@ -437,6 +521,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + [[package]] name = "fuchsia-zircon" version = "0.3.3" @@ -623,6 +713,34 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "memoffset" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "043175f069eda7b85febe4a74abbaeff828d9f8b448515d3151a14a3542811aa" +dependencies = [ + "autocfg 1.0.1", +] + +[[package]] +name = "minifb" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eb411a297b8f714800f085846cbda55fbd2ae17ecd381f1b4652af0e5bebe63" +dependencies = [ + "cc", + "orbclient", + "raw-window-handle", + "tempfile", + "wayland-client 0.27.0", + "wayland-cursor 0.27.0", + "wayland-protocols 0.27.0", + "winapi 0.3.9", + "x11-dl", + "xkb", + "xkbcommon-sys", +] + [[package]] name = "mio" version = "0.6.22" @@ -722,6 +840,19 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "nix" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50e4785f2c3b7589a0d0c1dd60285e1188adac4006e8abd6dd578e1567027363" +dependencies = [ + "bitflags", + "cc", + "cfg-if 0.1.10", + "libc", + "void", +] + [[package]] name = "nix" version = "0.18.0" @@ -744,6 +875,38 @@ dependencies = [ "version_check", ] +[[package]] +name = "num" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e" +dependencies = [ + "num-integer", + "num-iter", + "num-traits 0.2.12", +] + +[[package]] +name = "num-integer" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b" +dependencies = [ + "autocfg 1.0.1", + "num-traits 0.2.12", +] + +[[package]] +name = "num-iter" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6e6b7c748f995c4c29c5f5ae0248536e04a5739927c74ec0fa564805094b9f" +dependencies = [ + "autocfg 1.0.1", + "num-integer", + "num-traits 0.2.12", +] + [[package]] name = "num-traits" version = "0.1.43" @@ -759,7 +922,17 @@ version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611" dependencies = [ - "autocfg", + "autocfg 1.0.1", +] + +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", ] [[package]] @@ -799,6 +972,16 @@ version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad" +[[package]] +name = "orbclient" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8b18f57ab94fbd058e30aa57f712ec423c0bb7403f8493a6c58eef0c36d9402" +dependencies = [ + "redox_syscall", + "sdl2", +] + [[package]] name = "output_vt100" version = "0.1.2" @@ -835,7 +1018,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c361aa727dd08437f2f1447be8b59a33b0edd15e0fcee698f935613d9efbca9b" dependencies = [ "cfg-if 0.1.10", - "cloudabi", + "cloudabi 0.1.0", "instant", "libc", "redox_syscall", @@ -942,6 +1125,25 @@ dependencies = [ "proc-macro2 1.0.24", ] +[[package]] +name = "rand" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" +dependencies = [ + "autocfg 0.1.7", + "libc", + "rand_chacha 0.1.1", + "rand_core 0.4.2", + "rand_hc 0.1.0", + "rand_isaac", + "rand_jitter", + "rand_os", + "rand_pcg", + "rand_xorshift", + "winapi 0.3.9", +] + [[package]] name = "rand" version = "0.7.3" @@ -950,9 +1152,19 @@ checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" dependencies = [ "getrandom", "libc", - "rand_chacha", - "rand_core", - "rand_hc", + "rand_chacha 0.2.2", + "rand_core 0.5.1", + "rand_hc 0.2.0", +] + +[[package]] +name = "rand_chacha" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" +dependencies = [ + "autocfg 0.1.7", + "rand_core 0.3.1", ] [[package]] @@ -962,9 +1174,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.5.1", ] +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + [[package]] name = "rand_core" version = "0.5.1" @@ -974,13 +1201,75 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rand_hc" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4" +dependencies = [ + "rand_core 0.3.1", +] + [[package]] name = "rand_hc" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" dependencies = [ - "rand_core", + "rand_core 0.5.1", +] + +[[package]] +name = "rand_isaac" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" +dependencies = [ + "rand_core 0.3.1", +] + +[[package]] +name = "rand_jitter" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" +dependencies = [ + "libc", + "rand_core 0.4.2", + "winapi 0.3.9", +] + +[[package]] +name = "rand_os" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" +dependencies = [ + "cloudabi 0.0.3", + "fuchsia-cprng", + "libc", + "rand_core 0.4.2", + "rdrand", + "winapi 0.3.9", +] + +[[package]] +name = "rand_pcg" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" +dependencies = [ + "autocfg 0.1.7", + "rand_core 0.4.2", +] + +[[package]] +name = "rand_xorshift" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" +dependencies = [ + "rand_core 0.3.1", ] [[package]] @@ -1010,6 +1299,40 @@ dependencies = [ "raw-window-handle", ] +[[package]] +name = "rayon" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674" +dependencies = [ + "autocfg 1.0.1", + "crossbeam-deque", + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "lazy_static", + "num_cpus", +] + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + [[package]] name = "redox_syscall" version = "0.1.57" @@ -1113,6 +1436,30 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "sdl2" +version = "0.32.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d051a07231e303f5f719da78cb6f7394f6d5b54f733aef5b0b447804a83edd7b" +dependencies = [ + "bitflags", + "lazy_static", + "libc", + "num", + "rand 0.6.5", + "sdl2-sys", +] + +[[package]] +name = "sdl2-sys" +version = "0.32.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34e71125077d297d57e4c1acfe8981b5bdfbf5a20e7b589abfdcb33bf1127f86" +dependencies = [ + "cfg-if 0.1.10", + "libc", +] + [[package]] name = "semver" version = "0.9.0" @@ -1185,10 +1532,10 @@ dependencies = [ "lazy_static", "log", "memmap", - "nix", - "wayland-client", - "wayland-cursor", - "wayland-protocols", + "nix 0.18.0", + "wayland-client 0.28.1", + "wayland-cursor 0.28.1", + "wayland-protocols 0.28.1", ] [[package]] @@ -1294,7 +1641,7 @@ checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" dependencies = [ "cfg-if 0.1.10", "libc", - "rand", + "rand 0.7.3", "redox_syscall", "remove_dir_all", "winapi 0.3.9", @@ -1395,6 +1742,12 @@ version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + [[package]] name = "walkdir" version = "2.3.1" @@ -1412,6 +1765,21 @@ version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +[[package]] +name = "wayland-client" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab702fefbcd6d6f67fb5816e3a89a3b5a42a94290abbc015311c9a30d1068ae4" +dependencies = [ + "bitflags", + "downcast-rs", + "libc", + "nix 0.17.0", + "wayland-commons 0.27.0", + "wayland-scanner 0.27.0", + "wayland-sys 0.27.0", +] + [[package]] name = "wayland-client" version = "0.28.1" @@ -1421,11 +1789,23 @@ dependencies = [ "bitflags", "downcast-rs", "libc", - "nix", + "nix 0.18.0", "scoped-tls", - "wayland-commons", - "wayland-scanner", - "wayland-sys", + "wayland-commons 0.28.1", + "wayland-scanner 0.28.1", + "wayland-sys 0.28.1", +] + +[[package]] +name = "wayland-commons" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e972e9336ad5a9dd861b4e21ff35ad71d3e5c6b4803d65c39913612f851b95f1" +dependencies = [ + "nix 0.17.0", + "once_cell", + "smallvec", + "wayland-sys 0.27.0", ] [[package]] @@ -1434,10 +1814,21 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7602d75560fe6f02cac723609cce658042fe60541b5107999818d29d4dab7cfa" dependencies = [ - "nix", + "nix 0.18.0", "once_cell", "smallvec", - "wayland-sys", + "wayland-sys 0.28.1", +] + +[[package]] +name = "wayland-cursor" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "539f346e1a3f706f38c8ccbe1196001e2fb1c9b3e6b605c27d665db2f5b60d41" +dependencies = [ + "nix 0.17.0", + "wayland-client 0.27.0", + "xcursor", ] [[package]] @@ -1446,11 +1837,23 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0446b959c5b5b4b2c11f63112fc7cbeb50ecd9f2c340d2b0ea632875685baf04" dependencies = [ - "nix", - "wayland-client", + "nix 0.18.0", + "wayland-client 0.28.1", "xcursor", ] +[[package]] +name = "wayland-protocols" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3d6fc54b17b98b5083bc21ae3a30e6d75cb4b01647360e4c3a04648bcf8781d" +dependencies = [ + "bitflags", + "wayland-client 0.27.0", + "wayland-commons 0.27.0", + "wayland-scanner 0.27.0", +] + [[package]] name = "wayland-protocols" version = "0.28.1" @@ -1458,9 +1861,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d419585bbdb150fb541579cff205c6095a86cd874530e41838d1f18a9569a08" dependencies = [ "bitflags", - "wayland-client", - "wayland-commons", - "wayland-scanner", + "wayland-client 0.28.1", + "wayland-commons 0.28.1", + "wayland-scanner 0.28.1", +] + +[[package]] +name = "wayland-scanner" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "030f56009d932bd9400bb472764fea8109be1b0fc482d9cd75496c943ac30328" +dependencies = [ + "proc-macro2 1.0.24", + "quote 1.0.7", + "xml-rs", ] [[package]] @@ -1474,6 +1888,15 @@ dependencies = [ "xml-rs", ] +[[package]] +name = "wayland-sys" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bdeffbbb474477dfa2acb45ac7479e5fe8f741c64ab032c5d11b94d07edc269" +dependencies = [ + "pkg-config", +] + [[package]] name = "wayland-sys" version = "0.28.1" @@ -1554,7 +1977,7 @@ dependencies = [ "percent-encoding", "raw-window-handle", "smithay-client-toolkit", - "wayland-client", + "wayland-client 0.28.1", "winapi 0.3.9", "x11-dl", ] @@ -1605,6 +2028,27 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57" +[[package]] +name = "xkb" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aec02bc5de902aa579f3d2f2c522edaf40fa42963cbaffe645b058ddcc68fdb2" +dependencies = [ + "bitflags", + "libc", + "xkbcommon-sys", +] + +[[package]] +name = "xkbcommon-sys" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa434980dca02ebf28795d71e570dbb78316d095a228707efd6117bf8246d78b" +dependencies = [ + "libc", + "pkg-config", +] + [[package]] name = "xml-rs" version = "0.8.3" diff --git a/Cargo.toml b/Cargo.toml index da2ab8c82c..6cb154e86f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ + "examples/example-runner-cpu", "examples/example-runner", "examples/example-shader", "rustc_codegen_spirv", diff --git a/deny.toml b/deny.toml index a13ce6d7db..fdcf518841 100644 --- a/deny.toml +++ b/deny.toml @@ -38,6 +38,7 @@ skip = [ skip-tree = [ # we don't really care if our example brings in some duplicate dependencies, for now { name = "example-runner", version = "*", depth = 20 }, + { name = "example-runner-cpu", version = "*", depth = 20 }, ] @@ -53,13 +54,16 @@ allow = [ "Apache-2.0", "Apache-2.0 WITH LLVM-exception", "BSD-2-Clause", - "BSD-3-Clause" + "BSD-3-Clause", + "ISC" ] # Allow 1 or more licenses on a per-crate basis, so that particular licenses # aren't accepted for every possible crate as with the normal allow list exceptions = [ - { allow = ["ISC"], name = "libloading" }, - + # allow permissive but non-standard and non-ideal license, only used in one of our examples, not in our own code + # https://tldrlegal.com/license/do-what-the-f*ck-you-want-to-public-license-(wtfpl) + { allow = ["WTFPL"], name = "xkb" }, + { allow = ["WTFPL"], name = "xkbcommon-sys" }, ] copyleft = "deny" diff --git a/examples/example-runner-cpu/Cargo.toml b/examples/example-runner-cpu/Cargo.toml new file mode 100644 index 0000000000..cc7897c435 --- /dev/null +++ b/examples/example-runner-cpu/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "example-runner-cpu" +version = "0.1.0" +authors = ["Embark "] +edition = "2018" +license = "MIT OR Apache-2.0" + +[dependencies] +minifb = "0.19.1" +# bring in the shader as natively compiled code +example-shader = { path = "../example-shader" } +# for math types, likely get from glam directly later instead +spirv-std = { path = "../../spirv-std" } + +# for parallelism, not really needed though +rayon = "1.5" + diff --git a/examples/example-runner-cpu/src/main.rs b/examples/example-runner-cpu/src/main.rs new file mode 100644 index 0000000000..16e112837f --- /dev/null +++ b/examples/example-runner-cpu/src/main.rs @@ -0,0 +1,57 @@ +use minifb::{Key, Window, WindowOptions}; +use rayon::prelude::*; +use spirv_std::{vec2, Vec4}; +use std::time::Instant; + +fn color_u32_from_vec4(v: Vec4) -> u32 { + let convert = |f: f32| -> u32 { (f.min(1.0).max(0.0) * 255.0).round() as u32 }; + + // this later will also do linear -> sRGB gamma correction for output, + // but currently our shader output is in sRGB so no conversion needed + + convert(v.z()) | convert(v.y()) << 8 | convert(v.x()) << 16 | convert(v.w()) << 24 +} + +fn main() { + const WIDTH: usize = 1280; + const HEIGHT: usize = 720; + + let mut window = Window::new( + "Rust GPU - CPU shader evaluation", + WIDTH, + HEIGHT, + WindowOptions::default(), + ) + .expect("Window creation failed"); + + // Limit to max ~60 fps update rate + window.limit_update_rate(Some(std::time::Duration::from_micros(16600))); + + let start_time = Instant::now(); + + let buffer = (0..WIDTH * HEIGHT) + .into_par_iter() + .map(|i| { + let screen_pos = vec2( + (i % WIDTH) as f32 / WIDTH as f32 * 2.0 - 1.0, + (i / WIDTH) as f32 / HEIGHT as f32 * 2.0 - 1.0, + ); + + // evaluate the fragment shader for the specific pixel + let color = example_shader::fs(screen_pos); + + color_u32_from_vec4(color) + }) + .collect::>(); + + println!( + "Evaluating {} pixels took {} ms", + buffer.len(), + start_time.elapsed().as_millis() + ); + + while window.is_open() && !window.is_key_down(Key::Escape) { + // We unwrap here as we want this code to exit if it fails. Real applications may want to handle this in a different way + window.update_with_buffer(&buffer, WIDTH, HEIGHT).unwrap(); + } +} diff --git a/examples/example-shader/src/lib.rs b/examples/example-shader/src/lib.rs index 9b346b9ba3..44cb4efcb7 100644 --- a/examples/example-shader/src/lib.rs +++ b/examples/example-shader/src/lib.rs @@ -6,7 +6,7 @@ #![register_attr(spirv)] use core::f32::consts::PI; -use spirv_std::{Input, Mat4, MathExt, Output, Vec3, Vec4}; +use spirv_std::{Input, Mat4, MathExt, Output, Vec2, Vec3, Vec4}; const DEPOLARIZATION_FACTOR: f32 = 0.035; const LUMINANCE: f32 = 1.0; @@ -143,11 +143,7 @@ fn sky(dir: Vec3, sun_position: Vec3) -> Vec3 { color.pow(1.0 / (1.2 + (1.2 * sunfade))) } -#[allow(unused_attributes)] -#[spirv(entry = "fragment")] -pub fn main_fs(input: Input, mut output: Output) { - let dir: Vec3 = input.load().truncate(); - +pub fn main_fs_test(screen_pos: Vec2) -> Vec4 { // hard-code information because we can't bind buffers at the moment let eye_pos = Vec3(0.0, 0.0997, 0.2); let sun_pos = Vec3::new(0.0, 75.0, -1000.0); @@ -158,7 +154,7 @@ pub fn main_fs(input: Input, mut output: Output) { w_axis: Vec4(0.0, -0.14834046, -0.98893654, 0.0), }; - let cs_pos = Vec4(dir.0, -dir.1, 1.0, 1.0); + let cs_pos = Vec4(screen_pos.0, -screen_pos.1, 1.0, 1.0); let ws_pos = { let p = clip_to_world.mul_vec4(cs_pos); p.truncate() / p.3 @@ -168,7 +164,41 @@ pub fn main_fs(input: Input, mut output: Output) { // evaluate Preetham sky model let color = sky(dir, sun_pos); - output.store(color.extend(0.0)) + // let color = dir; + + color.extend(1.0) +} + +pub fn fs(screen_pos: Vec2) -> Vec4 { + // hard-code information because we can't bind buffers at the moment + let eye_pos = Vec3(0.0, 0.0997, 0.2); + let sun_pos = Vec3::new(0.0, 75.0, -1000.0); + let clip_to_world = Mat4 { + x_axis: Vec4(-0.5522849, 0.0, 0.0, 0.0), + y_axis: Vec4(0.0, 0.4096309, -0.061444636, 0.0), + z_axis: Vec4(0.0, 99.99999, 199.99998, 999.99994), + w_axis: Vec4(0.0, -0.14834046, -0.98893654, 0.0), + }; + + let cs_pos = Vec4(screen_pos.0, -screen_pos.1, 1.0, 1.0); + let ws_pos = { + let p = clip_to_world.mul_vec4(cs_pos); + p.truncate() / p.3 + }; + let dir = (ws_pos - eye_pos).normalize(); + + // evaluate Preetham sky model + let color = sky(dir, sun_pos); + + color.extend(1.0) +} + +#[allow(unused_attributes)] +#[spirv(entry = "fragment")] +pub fn main_fs(input: Input, mut output: Output) { + let v = input.load(); + let color = fs(Vec2::new(v.0, v.1)); + output.store(color) } #[allow(unused_attributes)] diff --git a/spirv-std/src/math/vec2.rs b/spirv-std/src/math/vec2.rs index f26a4567b8..03e79a7188 100644 --- a/spirv-std/src/math/vec2.rs +++ b/spirv-std/src/math/vec2.rs @@ -5,7 +5,7 @@ use core::{f32, ops::*}; /// A 2-dimensional vector. #[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Default)] #[repr(simd)] -pub struct Vec2(pub(crate) f32, pub(crate) f32); +pub struct Vec2(pub f32, pub f32); /// Creates a `Vec2`. #[inline]