building blocks for future showcases

pull/126/head
Ben Hansen 4 years ago
parent 28c8637d73
commit 4fa1e1a504

130
Cargo.lock generated

@ -270,6 +270,11 @@ name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "cgmath"
version = "0.17.0"
@ -394,13 +399,18 @@ dependencies = [
"glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"image 0.23.7 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"shaderc 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tobj 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"wgpu 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit 0.22.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "const_fn"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "copyless"
version = "0.1.5"
@ -555,6 +565,15 @@ dependencies = [
"maybe-uninit 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-utils 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "crossbeam-deque"
version = "0.7.3"
@ -565,6 +584,16 @@ dependencies = [
"maybe-uninit 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-epoch 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-utils 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "crossbeam-epoch"
version = "0.8.2"
@ -579,6 +608,19 @@ dependencies = [
"scopeguard 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"const_fn 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-utils 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"memoffset 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
"scopeguard 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "crossbeam-utils"
version = "0.7.2"
@ -589,6 +631,17 @@ dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cfg-if 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"const_fn 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "d3d12"
version = "0.3.1"
@ -750,7 +803,9 @@ dependencies = [
"glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"image 0.23.7 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"shaderc 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
"thiserror 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
"tobj 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"wgpu 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit 0.22.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1211,7 +1266,7 @@ dependencies = [
"crossbeam-channel 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-deque 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"linked-hash-map 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-hash 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1292,6 +1347,15 @@ dependencies = [
"tiff 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "imgui"
version = "0.1.0"
dependencies = [
"framework 0.1.0",
"imgui-wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"wgpu 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "imgui"
version = "0.4.0"
@ -1303,6 +1367,17 @@ dependencies = [
"parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "imgui"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"imgui-sys 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "imgui-sys"
version = "0.4.0"
@ -1311,6 +1386,14 @@ dependencies = [
"cc 1.0.58 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "imgui-sys"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cc 1.0.58 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "imgui-wgpu"
version = "0.7.0"
@ -1321,6 +1404,18 @@ dependencies = [
"wgpu 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "imgui-wgpu"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bytemuck 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"imgui 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"smallvec 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"wgpu 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "inflate"
version = "0.4.5"
@ -1377,7 +1472,7 @@ version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -2272,23 +2367,23 @@ dependencies = [
[[package]]
name = "rayon"
version = "1.4.0"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-deque 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-deque 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon-core 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon-core 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rayon-core"
version = "1.8.1"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"crossbeam-channel 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-deque 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-channel 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-deque 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-utils 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -2773,7 +2868,7 @@ dependencies = [
"glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"image 0.23.7 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"shaderc 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tobj 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"wgpu 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -3549,6 +3644,7 @@ dependencies = [
"checksum cc 1.0.58 (registry+https://github.com/rust-lang/crates.io-index)" = "f9a06fb2e53271d7c279ec1efea6ab691c35a2ae67ec0d91d7acec0caf13b518"
"checksum cexpr 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f4aedb84272dbe89af497cf81375129abda4fc0a9e7c5d317498c15cc30c0d27"
"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
"checksum cfg-if 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
"checksum cgmath 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "283944cdecc44bf0b8dd010ec9af888d3b4f142844fdbe026c20ef68148d6fe7"
"checksum clang-sys 0.29.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fe6837df1d5cba2397b835c8530f51723267e16abbf83892e9e5af4f0e5dd10a"
"checksum claxon 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f86c952727a495bda7abaf09bafdee1a939194dd793d9a8e26281df55ac43b00"
@ -3560,6 +3656,7 @@ dependencies = [
"checksum cocoa 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c54201c07dcf3a5ca33fececb8042aed767ee4bfd5a0235a8ceabcda956044b2"
"checksum cocoa-foundation 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7ade49b65d560ca58c403a479bb396592b155c0185eada742ee323d1d68d6318"
"checksum color_quant 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0dbbb57365263e881e805dc77d94697c9118fd94d8da011240555aa7b23445bd"
"checksum const_fn 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c478836e029dcef17fb47c89023448c64f781a046e0300e257ad8225ae59afab"
"checksum copyless 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a2df960f5d869b2dd8532793fde43eb5427cceb126c929747a26823ab0eeb536"
"checksum core-foundation 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "25b9e03f145fd4f2bf705e07b900cd41fc636598fe5dc452fd0db1441c3f496d"
"checksum core-foundation 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "57d24c7a13c43e870e37c1556b74555437870a04514f7685f5b354e090567171"
@ -3577,9 +3674,13 @@ dependencies = [
"checksum cpal 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6b55d55d69f403f62a95bd3c04b431e0aedf5120c70f15d07a8edd234443dd59"
"checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
"checksum crossbeam-channel 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b153fe7cbef478c567df0f972e02e6d736db11affe43dfc9c56a9374d1adfb87"
"checksum crossbeam-channel 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775"
"checksum crossbeam-deque 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285"
"checksum crossbeam-deque 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9"
"checksum crossbeam-epoch 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace"
"checksum crossbeam-epoch 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ec0f606a85340376eef0d6d8fec399e6d4a544d648386c6645eb6d0653b27d9f"
"checksum crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
"checksum crossbeam-utils 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ec91540d98355f690a86367e566ecad2e9e579f230230eb7c21398372be73ea5"
"checksum d3d12 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c1324bc4eae34f03b0ced586da5ae2b1ab46acfdae68b5b26d2e23dadae376a2"
"checksum darling 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858"
"checksum darling_core 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b"
@ -3645,7 +3746,10 @@ dependencies = [
"checksum image 0.22.5 (registry+https://github.com/rust-lang/crates.io-index)" = "08ed2ada878397b045454ac7cfb011d73132c59f31a955d230bd1f1c2e68eb4a"
"checksum image 0.23.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a2397fc43bd5648b7117aabb3c5e62d0e62c194826ec77b0b4d0c41e62744635"
"checksum imgui 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9fdb2bcc7e498e78137ce28705ae836d69e36ee2eac89d0d926cfabfcf550570"
"checksum imgui 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a714f73a3f08446d92c47a38b536a03deba500aa6bb6fa805e8c0b6f90945e4f"
"checksum imgui-sys 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "72be9671d64dd0ed26bb708cd10060a431262ac90ae70cf7c5912feefe6849da"
"checksum imgui-sys 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0522b693da8a64322afbb32c63c04f39d9b9435cc75199d630207eee48886fc1"
"checksum imgui-wgpu 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8017f74f8880049c1c3fc055868486a610178ab93ad18eadaf5b0bb52f738365"
"checksum imgui-wgpu 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b641250a21c77c91144566ee4f98d81c853e89c99bb718796344da87e8804345"
"checksum inflate 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1cdb29978cc5797bd8dcc8e5bf7de604891df2a8dc576973d71a281e916db2ff"
"checksum inplace_it 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd01a2a73f2f399df96b22dc88ea687ef4d76226284e7531ae3c7ee1dc5cb534"
@ -3751,8 +3855,8 @@ dependencies = [
"checksum range-alloc 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a871f1e45a3a3f0c73fb60343c811238bb5143a81642e27c2ac7aac27ff01a63"
"checksum raw-window-handle 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "af3d3b2e1053b3ff2171efc29a8bff3439ce6b2ce6a0432695134bc1c7ff8e87"
"checksum raw-window-handle 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0a441a7a6c80ad6473bd4b74ec1c9a4c951794285bf941c2126f607c72e48211"
"checksum rayon 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cfd016f0c045ad38b5251be2c9c0ab806917f82da4d36b2a327e5166adad9270"
"checksum rayon-core 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e8c4fec834fb6e6d2dd5eece3c7b432a52f0ba887cf40e595190c4107edc08bf"
"checksum rayon 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674"
"checksum rayon-core 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a"
"checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
"checksum redox_syscall 0.1.57 (registry+https://github.com/rust-lang/crates.io-index)" = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
"checksum regex 1.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6"

@ -6,6 +6,7 @@ edition = "2018"
[dependencies]
anyhow = "1.0"
thiserror = "1.0"
bytemuck = { version = "1.4", features = [ "derive" ] }
cgmath = "0.17"
env_logger = "0.7"
@ -20,4 +21,5 @@ winit = "0.22"
anyhow = "1.0"
fs_extra = "1.2"
glob = "0.3"
shaderc = "0.6"
shaderc = "0.6"
rayon = "1.5"

@ -0,0 +1,92 @@
use anyhow::*;
use fs_extra::copy_items;
use fs_extra::dir::CopyOptions;
use glob::glob;
use rayon::prelude::*;
use std::env;
use std::fs::{read_to_string, write};
use std::path::PathBuf;
struct ShaderData {
src: String,
src_path: PathBuf,
spv_path: PathBuf,
kind: shaderc::ShaderKind,
}
impl ShaderData {
pub fn load(src_path: PathBuf) -> Result<Self> {
let extension = src_path
.extension()
.context("File has no extension")?
.to_str()
.context("Extension cannot be converted to &str")?;
let kind = match extension {
"vert" => shaderc::ShaderKind::Vertex,
"frag" => shaderc::ShaderKind::Fragment,
"comp" => shaderc::ShaderKind::Compute,
_ => bail!("Unsupported shader: {}", src_path.display()),
};
let src = read_to_string(src_path.clone())?;
let spv_path = src_path.with_extension(format!("{}.spv", extension));
Ok(Self {
src,
src_path,
spv_path,
kind,
})
}
}
fn main() -> Result<()> {
// This tells cargo to rerun this script if something in /src/ changes.
println!("cargo:rerun-if-changed=src/*");
// Collect all shaders recursively within /src/
// UDPATED!
let mut shader_paths = Vec::new();
shader_paths.extend(glob("./src/**/*.vert")?);
shader_paths.extend(glob("./src/**/*.frag")?);
shader_paths.extend(glob("./src/**/*.comp")?);
// UPDATED!
// This is parallelized
let shaders = shader_paths
.into_par_iter()
.map(|glob_result| ShaderData::load(glob_result?))
.collect::<Vec<Result<_>>>()
.into_iter()
.collect::<Result<Vec<_>>>();
let mut compiler = shaderc::Compiler::new().context("Unable to create shader compiler")?;
// This can't be parallelized. The [shaderc::Compiler] is not
// thread safe. Also, it creates a lot of resources. You could
// spawn multiple processes to handle this, but it would probably
// be better just to only compile shaders that have been changed
// recently.
for shader in shaders? {
let compiled = compiler.compile_into_spirv(
&shader.src,
shader.kind,
&shader.src_path.to_str().unwrap(),
"main",
None,
)?;
write(shader.spv_path, compiled.as_binary_u8())?;
}
// This tells cargo to rerun this script if something in /res/ changes.
// println!("cargo:rerun-if-changed=res/*");
// let out_dir = env::var("OUT_DIR")?;
// let mut copy_options = CopyOptions::new();
// copy_options.overwrite = true;
// let mut paths_to_copy = Vec::new();
// paths_to_copy.push("res/");
// copy_items(&paths_to_copy, out_dir, &copy_options)?;
Ok(())
}

@ -5,6 +5,7 @@ mod model;
mod pipeline;
pub mod prelude;
mod texture;
mod shader_canvas;
pub use buffer::*;
pub use camera::*;
@ -12,6 +13,7 @@ pub use light::*;
pub use model::*;
pub use pipeline::*;
pub use texture::*;
pub use shader_canvas::*;
use anyhow::*;
use cgmath::*;

@ -0,0 +1,18 @@
#version 450
layout(location=0) in vec3 clip_coords;
layout(location=0) out vec4 frag_color;
layout(set=0, binding=0) uniform SimulationData {
float time;
float delta_time;
vec2 mouse_pos;
vec2 canvas_size;
vec4 clear_color;
};
void main() {
vec3 uv = clip_coords * 0.5 + 0.5;
vec4 col = mix(vec4(uv, 1.0), clear_color, sin(time));
frag_color = col;
}

@ -0,0 +1,228 @@
//! Features
//! - [ ] Support fullscreen drawing
//! - [ ] Data struct for basic uniforms (time, mousePos, etc.)
//! - [ ] Lambda support for other bind groups
//! - [ ] Drawing to texture (maybe have the render pass decide this?)
//! - [ ] Saving to file
use std::time::Instant;
use thiserror::Error;
use wgpu::util::{DeviceExt, BufferInitDescriptor};
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct SimulationData {
time: f32,
delta_time: f32,
mouse_pos: [f32; 2],
canvas_size: [f32; 2],
clear_color: [f32; 4],
}
#[derive(Error, Debug)]
pub enum ShaderBuildError {
#[error("Please supply a valid vertex shader")]
InvalidVertexShader,
#[error("Please supply a valid fragment shader")]
InvalidFragmentShader,
#[error("Please supply a valid display format")]
InvalidDisplayFormat,
}
pub struct ShaderCanvas {
pipeline: wgpu::RenderPipeline,
start_time: Option<Instant>,
last_time: Option<Instant>,
simulation_data: SimulationData,
simulation_data_buffer: wgpu::Buffer,
simulation_bind_group: wgpu::BindGroup,
}
impl ShaderCanvas {
pub fn input(&mut self, mouse_x: f32, mouse_y: f32) {
self.simulation_data.mouse_pos[0] = mouse_x;
self.simulation_data.mouse_pos[1] = mouse_y;
}
pub fn render(
&mut self,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
frame: &wgpu::TextureView,
width: f32,
height: f32,
) {
let current_time = Instant::now();
let start_time = match self.start_time {
Some(t) => t,
None => {
let t = current_time;
self.start_time = Some(t);
t
},
};
let last_time = self.last_time.unwrap_or(current_time);
self.last_time = Some(current_time);
self.simulation_data.time = (current_time - start_time).as_secs_f32();
self.simulation_data.delta_time = (current_time - last_time).as_secs_f32();
self.simulation_data.canvas_size[0] = width;
self.simulation_data.canvas_size[1] = height;
queue.write_buffer(&self.simulation_data_buffer, 0, bytemuck::cast_slice(&[self.simulation_data]));
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
color_attachments: &[
wgpu::RenderPassColorAttachmentDescriptor {
attachment: frame,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: true,
}
},
],
depth_stencil_attachment: None,
});
pass.set_bind_group(0, &self.simulation_bind_group, &[]);
pass.set_pipeline(&self.pipeline);
pass.draw(0..6, 0..1);
}
}
pub struct ShaderCanvasBuilder<'a> {
canvas_size: [f32; 2],
clear_color: [f32; 4],
label: Option<&'a str>,
display_format: Option<wgpu::TextureFormat>,
frag_code: Option<wgpu::ShaderModuleSource<'a>>,
vert_code: Option<wgpu::ShaderModuleSource<'a>>,
}
impl<'a> ShaderCanvasBuilder<'a> {
pub fn new() -> Self {
Self {
canvas_size: [256.0; 2],
clear_color: [0.0, 0.0, 0.0, 1.0],
label: None,
display_format: None,
frag_code: Some(wgpu::include_spirv!("shader_canvas.frag.spv")),
vert_code: Some(wgpu::include_spirv!("shader_canvas.vert.spv")),
}
}
pub fn canvas_size(&mut self, width: f32, height: f32) -> &mut Self {
self.canvas_size = [width, height];
self
}
pub fn display_format(&mut self, format: wgpu::TextureFormat) -> &mut Self {
self.display_format = Some(format);
self
}
pub fn use_swap_chain_desc(&mut self, sc_desc: &wgpu::SwapChainDescriptor) -> &mut Self {
self.display_format(sc_desc.format);
self.canvas_size(sc_desc.width as f32, sc_desc.height as f32)
}
pub fn fragment_shader(&mut self, code: wgpu::ShaderModuleSource<'a>) -> &mut Self {
self.frag_code = Some(code);
self
}
pub fn vertex_shader(&mut self, code: wgpu::ShaderModuleSource<'a>) -> &mut Self {
self.vert_code = Some(code);
self
}
pub fn build(&mut self, device: &wgpu::Device) -> Result<ShaderCanvas, ShaderBuildError> {
let display_format = self.display_format.ok_or(ShaderBuildError::InvalidDisplayFormat)?;
let frag_code = self.frag_code.take().ok_or(ShaderBuildError::InvalidFragmentShader)?;
let vert_code = self.frag_code.take().ok_or(ShaderBuildError::InvalidVertexShader)?;
let simulation_data = SimulationData {
time: 0.0,
delta_time: 0.0,
mouse_pos: [0.0; 2],
canvas_size: self.canvas_size,
clear_color: self.clear_color,
};
let simulation_data_buffer = device.create_buffer_init(&BufferInitDescriptor {
label: self.label,
contents: bytemuck::cast_slice(&[simulation_data]),
usage: wgpu::BufferUsage::UNIFORM,
});
let simulation_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: self.label,
entries: &[
// SimulationData
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStage::FRAGMENT,
count: None,
ty: wgpu::BindingType::UniformBuffer {
dynamic: false,
min_binding_size: None,
}
}
],
});
let simulation_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &simulation_bind_group_layout,
label: self.label,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(simulation_data_buffer.slice(..))
}
]
});
let vert_module = device.create_shader_module(vert_code);
let frag_module = device.create_shader_module(frag_code);
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: self.label,
bind_group_layouts: &[&simulation_bind_group_layout],
push_constant_ranges: &[],
});
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: self.label,
layout: Some(&pipeline_layout),
vertex_stage: wgpu::ProgrammableStageDescriptor {
entry_point: "main",
module: &vert_module,
},
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
entry_point: "main",
module: &frag_module,
}),
color_states: &[wgpu::ColorStateDescriptor {
format: display_format,
alpha_blend: wgpu::BlendDescriptor::REPLACE,
color_blend: wgpu::BlendDescriptor::REPLACE,
write_mask: wgpu::ColorWrite::ALL,
}],
rasterization_state: None,
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
depth_stencil_state: None,
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
vertex_state: wgpu::VertexStateDescriptor {
index_format: wgpu::IndexFormat::Uint16,
vertex_buffers: &[],
},
});
Ok(ShaderCanvas {
pipeline,
start_time: None,
last_time: None,
simulation_data,
simulation_data_buffer,
simulation_bind_group,
})
}
}

@ -0,0 +1,20 @@
#version 450
layout(location=0) out vec3 clip_coords;
const vec3 positions[6] = vec3[6](
// Bottom left triangle
vec3(-1.0, 1.0, 0.0),
vec3(-1.0, -1.0, 0.0),
vec3(1.0, -1.0, 0.0),
// Top right triangle
vec3(-1.0, 1.0, 0.0),
vec3(1.0, -1.0, 0.0),
vec3(1.0, 1.0, 0.0)
);
void main() {
gl_Position = vec4(positions[gl_VertexIndex], 1.0);
clip_coords = gl_Position.xyz;
}

@ -90,7 +90,7 @@ fn main() -> Result<()> {
paths_to_copy.push("res/");
match copy_items(&paths_to_copy, out_dir, &copy_options) {
Ok(_) => {}
Err(e) => eprintln!("{}", e),
Err(e) => println!("{}", e),
}
Ok(())

@ -17,10 +17,7 @@ uniform Uniforms {
mat4 u_view_proj;
};
layout(set=2, binding=0)
buffer Instances {
mat4 model_matrix[];
};
layout(location=5) in mat4 model_matrix;
// NEW!
layout(set=1, binding=0) uniform Light {

Loading…
Cancel
Save