Draw unlit cube when DEM is loaded

Reconfigure the pipeline to draw an unlit (solid colour silhouette) cube
when a DEM is loaded. The fragment shader of this cube will be what
raytraces the DEM.
This commit is contained in:
Matthew Gordon 2024-11-14 12:51:24 -04:00
parent 71b05c31a2
commit b870e03812
5 changed files with 431 additions and 209 deletions

282
Cargo.lock generated
View File

@ -157,11 +157,11 @@ checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b"
[[package]]
name = "ash"
version = "0.37.3+1.3.251"
version = "0.38.0+1.3.281"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e9c3835d686b0a6084ab4234fcd1b07dbf6e4767dce60874b12356a25ecd4a"
checksum = "0bb44936d800fea8f016d7f2311c6a4f97aebd5dc86f09906139ec848cf3a46f"
dependencies = [
"libloading 0.7.4",
"libloading",
]
[[package]]
@ -178,18 +178,18 @@ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "bit-set"
version = "0.5.3"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1"
checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3"
dependencies = [
"bit-vec",
]
[[package]]
name = "bit-vec"
version = "0.6.3"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7"
[[package]]
name = "bitflags"
@ -229,6 +229,20 @@ name = "bytemuck"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8334215b81e418a0a7bdb8ef0849474f40bb10c8b71f1c4ed315cff49f32494d"
dependencies = [
"bytemuck_derive",
]
[[package]]
name = "bytemuck_derive"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcfcc3cd946cb52f0bbfdbbcfa2f4e24f75ebb6c0e1002f7c25904fada18b9ec"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "bytes"
@ -313,37 +327,6 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "com"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e17887fd17353b65b1b2ef1c526c83e26cd72e74f598a8dc1bee13a48f3d9f6"
dependencies = [
"com_macros",
]
[[package]]
name = "com_macros"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d375883580a668c7481ea6631fc1a8863e33cc335bf56bfad8d7e6d4b04b13a5"
dependencies = [
"com_macros_support",
"proc-macro2",
"syn 1.0.109",
]
[[package]]
name = "com_macros_support"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad899a1087a9296d5644792d7cb72b8e34c1bec8e7d4fbc002230169a6e8710c"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "combine"
version = "4.6.7"
@ -444,17 +427,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991"
[[package]]
name = "d3d12"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b28bfe653d79bd16c77f659305b195b82bb5ce0c0eb2a4846b82ddbd77586813"
dependencies = [
"bitflags 2.6.0",
"libloading 0.8.5",
"winapi",
]
[[package]]
name = "dispatch"
version = "0.2.0"
@ -467,7 +439,7 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "330c60081dcc4c72131f8eb70510f1ac07223e5d4163db481a04a0befcffa412"
dependencies = [
"libloading 0.8.5",
"libloading",
]
[[package]]
@ -558,7 +530,7 @@ checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn",
]
[[package]]
@ -623,7 +595,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn",
]
[[package]]
@ -689,10 +661,16 @@ dependencies = [
]
[[package]]
name = "glow"
version = "0.13.1"
name = "glam"
version = "0.29.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd348e04c43b32574f2de31c8bb397d96c9fcfa1371bd4ca6d8bdc464ab121b1"
checksum = "dc46dd3ec48fdd8e693a98d2b8bafae273a2d54c1de02a2a7e3d57d501f39677"
[[package]]
name = "glow"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51fa363f025f5c111e03f13eda21162faeacb6911fe8caa0c0349f9cf0c4483"
dependencies = [
"js-sys",
"slotmap",
@ -702,9 +680,9 @@ dependencies = [
[[package]]
name = "glutin_wgl_sys"
version = "0.5.0"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8098adac955faa2d31079b65dc48841251f69efd3ac25477903fc424362ead"
checksum = "0a4e1951bbd9434a81aa496fe59ccc2235af3820d27b85f9314e279609211e2c"
dependencies = [
"gl_generator",
]
@ -730,14 +708,13 @@ dependencies = [
[[package]]
name = "gpu-allocator"
version = "0.25.0"
version = "0.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f56f6318968d03c18e1bcf4857ff88c61157e9da8e47c5f29055d60e1228884"
checksum = "c151a2a5ef800297b4e79efa4f4bec035c5f51d5ae587287c9b952bdf734cacd"
dependencies = [
"log",
"presser",
"thiserror",
"winapi",
"windows",
]
@ -777,21 +754,6 @@ version = "0.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3"
[[package]]
name = "hassle-rs"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af2a7e73e1f34c48da31fb668a907f250794837e08faa144fd24f0b8b741e890"
dependencies = [
"bitflags 2.6.0",
"com",
"libc",
"libloading 0.8.5",
"thiserror",
"widestring",
"winapi",
]
[[package]]
name = "hermit-abi"
version = "0.4.0"
@ -879,7 +841,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6aae1df220ece3c0ada96b8153459b67eebe9ae9212258bb0134ae60416fdf76"
dependencies = [
"libc",
"libloading 0.8.5",
"libloading",
"pkg-config",
]
@ -895,16 +857,6 @@ version = "0.2.162"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398"
[[package]]
name = "libloading"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f"
dependencies = [
"cfg-if",
"winapi",
]
[[package]]
name = "libloading"
version = "0.8.5"
@ -980,9 +932,9 @@ dependencies = [
[[package]]
name = "metal"
version = "0.28.0"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5637e166ea14be6063a3f8ba5ccb9a4159df7d8f6d61c02fc3d480b1f90dcfcb"
checksum = "7ecfd3296f8c56b7c1f6fbac3c71cefa9d78ce009850c45000015f206dc7fa21"
dependencies = [
"bitflags 2.6.0",
"block",
@ -1014,18 +966,18 @@ dependencies = [
[[package]]
name = "naga"
version = "0.20.0"
version = "23.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e536ae46fcab0876853bd4a632ede5df4b1c2527a58f6c5a4150fe86be858231"
checksum = "3d5941e45a15b53aad4375eedf02033adb7a28931eedc31117faffa52e6a857e"
dependencies = [
"arrayvec",
"bit-set",
"bitflags 2.6.0",
"cfg_aliases 0.1.1",
"codespan-reporting",
"hexf-parse",
"indexmap",
"log",
"num-traits",
"rustc-hash",
"spirv",
"termcolor",
@ -1072,15 +1024,6 @@ dependencies = [
"jni-sys",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "num_enum"
version = "0.7.3"
@ -1099,7 +1042,7 @@ dependencies = [
"proc-macro-crate",
"proc-macro2",
"quote",
"syn 2.0.87",
"syn",
]
[[package]]
@ -1390,7 +1333,7 @@ checksum = "3c0f5fad0874fc7abcd4d750e76917eaebbecaa2c20bde22e1dbeeba8beb758c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn",
]
[[package]]
@ -1460,10 +1403,12 @@ checksum = "afbdc74edc00b6f6a218ca6a5364d6226a259d4b8ea1af4a0ea063f27e179f4d"
name = "pteropus"
version = "0.1.0"
dependencies = [
"bytemuck",
"console_error_panic_hook",
"console_log",
"env_logger",
"futures",
"glam",
"log",
"tiff",
"wasm-bindgen",
@ -1627,7 +1572,7 @@ checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn",
]
[[package]]
@ -1715,17 +1660,6 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731"
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.87"
@ -1763,7 +1697,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn",
]
[[package]]
@ -1915,7 +1849,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.87",
"syn",
"wasm-bindgen-shared",
]
@ -1949,7 +1883,7 @@ checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -1983,7 +1917,7 @@ checksum = "c97b2ef2c8d627381e51c071c2ab328eac606d3f69dd82bcbca20a9e389d95f0"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn",
]
[[package]]
@ -2123,12 +2057,11 @@ checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082"
[[package]]
name = "wgpu"
version = "0.20.1"
version = "23.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e37c7b9921b75dfd26dd973fdcbce36f13dfa6e2dc82aece584e0ed48c355c"
checksum = "76ab52f2d3d18b70d5ab8dd270a1cff3ebe6dbe4a7d13c1cc2557138a9777fdc"
dependencies = [
"arrayvec",
"cfg-if",
"cfg_aliases 0.1.1",
"document-features",
"js-sys",
@ -2149,15 +2082,14 @@ dependencies = [
[[package]]
name = "wgpu-core"
version = "0.21.1"
version = "23.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d50819ab545b867d8a454d1d756b90cd5f15da1f2943334ca314af10583c9d39"
checksum = "0e0c68e7b6322a03ee5b83fcd92caeac5c2a932f6457818179f4652ad2a9c065"
dependencies = [
"arrayvec",
"bit-vec",
"bitflags 2.6.0",
"cfg_aliases 0.1.1",
"codespan-reporting",
"document-features",
"indexmap",
"log",
@ -2169,16 +2101,15 @@ dependencies = [
"rustc-hash",
"smallvec",
"thiserror",
"web-sys",
"wgpu-hal",
"wgpu-types",
]
[[package]]
name = "wgpu-hal"
version = "0.21.1"
version = "23.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "172e490a87295564f3fcc0f165798d87386f6231b04d4548bca458cbbfd63222"
checksum = "de6e7266b869de56c7e3ed72a954899f71d14fec6cc81c102b7530b92947601b"
dependencies = [
"android_system_properties",
"arrayvec",
@ -2186,19 +2117,18 @@ dependencies = [
"bit-set",
"bitflags 2.6.0",
"block",
"bytemuck",
"cfg_aliases 0.1.1",
"core-graphics-types",
"d3d12",
"glow",
"glutin_wgl_sys",
"gpu-alloc",
"gpu-allocator",
"gpu-descriptor",
"hassle-rs",
"js-sys",
"khronos-egl",
"libc",
"libloading 0.8.5",
"libloading",
"log",
"metal",
"naga",
@ -2216,42 +2146,21 @@ dependencies = [
"wasm-bindgen",
"web-sys",
"wgpu-types",
"winapi",
"windows",
"windows-core",
]
[[package]]
name = "wgpu-types"
version = "0.20.0"
version = "23.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1353d9a46bff7f955a680577f34c69122628cc2076e1d6f3a9be6ef00ae793ef"
checksum = "610f6ff27778148c31093f3b03abc4840f9636d58d597ca2f5977433acfe0068"
dependencies = [
"bitflags 2.6.0",
"js-sys",
"web-sys",
]
[[package]]
name = "widestring"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7219d36b6eac893fa81e84ebe06485e7dcbb616177469b142df14f1f4deb1311"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.9"
@ -2261,17 +2170,11 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.52.0"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be"
checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6"
dependencies = [
"windows-core",
"windows-targets 0.52.6",
@ -2279,10 +2182,55 @@ dependencies = [
[[package]]
name = "windows-core"
version = "0.52.0"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99"
dependencies = [
"windows-implement",
"windows-interface",
"windows-result",
"windows-strings",
"windows-targets 0.52.6",
]
[[package]]
name = "windows-implement"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-result"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e"
dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-strings"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10"
dependencies = [
"windows-result",
"windows-targets 0.52.6",
]
@ -2572,7 +2520,7 @@ dependencies = [
"as-raw-xcb-connection",
"gethostname",
"libc",
"libloading 0.8.5",
"libloading",
"once_cell",
"rustix",
"x11rb-protocol",
@ -2632,5 +2580,5 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"syn",
]

View File

@ -7,17 +7,19 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]
[dependencies]
tiff = "0.9.1"
bytemuck = { version = "1.19.0", features = ["derive"] }
glam = "0.29.2"
log = "0.4.22"
tiff = "0.9.1"
[target.'cfg(target_arch = "x86_64")'.dependencies]
winit = { version = "0.30.3", features = ["rwh_06"] }
wgpu = "0.20.1"
wgpu = "23.0.0"
env_logger = "0.11.3"
futures = { version = "0.3.30", features = ["executor"] }
[target.'cfg(target_arch = "wasm32")'.dependencies]
wgpu = { version = "0.20.1", features = ["webgl"]}
wgpu = { version = "23.0.0", features = ["webgl"]}
wasm-bindgen = "0.2.84"
wasm-bindgen-futures = "0.4.42"
console_log = "1.0"

236
src/app/dem_renderer.rs Normal file
View File

@ -0,0 +1,236 @@
use super::raster::Dem;
use {
bytemuck::{Pod, Zeroable},
std::{borrow::Cow, rc::Rc},
wgpu::util::DeviceExt,
};
pub struct DemRenderer {
source: Rc<Dem>,
pipeline: wgpu::RenderPipeline,
bind_group: wgpu::BindGroup,
vertex_buffer: wgpu::Buffer,
index_buffer: wgpu::Buffer,
index_count: usize,
}
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct Vertex {
position: [f32; 4],
}
impl DemRenderer {
pub fn new(
source: Rc<Dem>,
device: &wgpu::Device,
surface_config: &wgpu::SurfaceConfiguration,
) -> Self {
let (vertex_data, index_data) = create_vertices();
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("DemRenderer Vertex Buffer"),
contents: bytemuck::cast_slice(&vertex_data),
usage: wgpu::BufferUsages::VERTEX,
});
let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("DemRenderer Index Buffer"),
contents: bytemuck::cast_slice(&index_data),
usage: wgpu::BufferUsages::INDEX,
});
let index_count = index_data.len();
let uniform_buf = {
let mx_total =
generate_matrix(surface_config.width as f32 / surface_config.height as f32);
let mx_ref: &[f32; 16] = mx_total.as_ref();
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(mx_ref),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
})
};
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(64),
},
count: None,
},
/*wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
sample_type: wgpu::TextureSampleType::Uint,
view_dimension: wgpu::TextureViewDimension::D2,
},
count: None,
},*/
],
});
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buf.as_entire_binding(),
}],
label: Some("DemRendererBindGroup"),
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});
let vertex_buffers = [wgpu::VertexBufferLayout {
array_stride: size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x4,
offset: 0,
shader_location: 0,
}],
}];
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("dem_renderer.wgsl"))),
});
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("DemRendererPipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("vs_main"),
compilation_options: Default::default(),
buffers: &vertex_buffers,
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs_solid"),
compilation_options: Default::default(),
targets: &[Some(surface_config.view_formats[0].into())],
}),
primitive: wgpu::PrimitiveState {
cull_mode: Some(wgpu::Face::Back),
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
multiview: None,
cache: None,
});
DemRenderer {
source,
pipeline,
bind_group,
vertex_buffer,
index_buffer,
index_count,
}
}
pub fn render(&mut self, view: &wgpu::TextureView, device: &wgpu::Device, queue: &wgpu::Queue) {
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("DemRendererCommandEncoder"),
});
{
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("DemRendererRenderPass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});
rpass.set_pipeline(&self.pipeline);
rpass.set_bind_group(0, &self.bind_group, &[]);
rpass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
rpass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
rpass.draw_indexed(0..self.index_count as u32, 0, 0..1);
}
queue.submit(Some(encoder.finish()));
}
}
fn vertex(pos: [i8; 3]) -> Vertex {
Vertex {
position: [pos[0] as f32, pos[1] as f32, pos[2] as f32, 1.0],
}
}
fn create_vertices() -> (Vec<Vertex>, Vec<u16>) {
let vertex_data = [
// top (0, 0, 1)
vertex([-1, -1, 1]),
vertex([1, -1, 1]),
vertex([1, 1, 1]),
vertex([-1, 1, 1]),
// bottom (0, 0, -1)
vertex([-1, 1, -1]),
vertex([1, 1, -1]),
vertex([1, -1, -1]),
vertex([-1, -1, -1]),
// right (1, 0, 0)
vertex([1, -1, -1]),
vertex([1, 1, -1]),
vertex([1, 1, 1]),
vertex([1, -1, 1]),
// left (-1, 0, 0)
vertex([-1, -1, 1]),
vertex([-1, 1, 1]),
vertex([-1, 1, -1]),
vertex([-1, -1, -1]),
// front (0, 1, 0)
vertex([1, 1, -1]),
vertex([-1, 1, -1]),
vertex([-1, 1, 1]),
vertex([1, 1, 1]),
// back (0, -1, 0)
vertex([1, -1, 1]),
vertex([-1, -1, 1]),
vertex([-1, -1, -1]),
vertex([1, -1, -1]),
];
let index_data: &[u16] = &[
0, 1, 2, 2, 3, 0, // top
4, 5, 6, 6, 7, 4, // bottom
8, 9, 10, 10, 11, 8, // right
12, 13, 14, 14, 15, 12, // left
16, 17, 18, 18, 19, 16, // front
20, 21, 22, 22, 23, 20, // back
];
(vertex_data.to_vec(), index_data.to_vec())
}
fn generate_matrix(aspect_ratio: f32) -> glam::Mat4 {
let projection =
glam::Mat4::perspective_rh(std::f32::consts::FRAC_PI_4, aspect_ratio, 1.0, 10.0);
let view = glam::Mat4::look_at_rh(
glam::Vec3::new(1.5f32, -5.0, 3.0),
glam::Vec3::ZERO,
glam::Vec3::Z,
);
projection * view
}

21
src/app/dem_renderer.wgsl Normal file
View File

@ -0,0 +1,21 @@
struct VertexOutput {
@builtin(position) position: vec4<f32>,
};
@group(0)
@binding(0)
var<uniform> transform: mat4x4<f32>;
@vertex
fn vs_main(
@location(0) position: vec4<f32>,
) -> VertexOutput {
var result: VertexOutput;
result.position = transform * position;
return result;
}
@fragment
fn fs_solid(vertex: VertexOutput) -> @location(0) vec4<f32> {
return vec4<f32>(1.0, 1.0, 1.0, 1.0);
}

View File

@ -7,11 +7,13 @@ use {
wgpu::{Device, Instance, Queue, RenderPipeline, Surface, SurfaceConfiguration},
};
mod dem_renderer;
mod raster;
use dem_renderer::DemRenderer;
#[derive(Clone)]
pub struct Model {
dem: Option<raster::Dem>,
dem: Option<Rc<raster::Dem>>,
}
impl Model {
@ -26,6 +28,7 @@ struct Context {
device: Device,
render_pipeline: RenderPipeline,
queue: Queue,
scene_data: Option<DemRenderer>,
}
#[derive(Default)]
@ -57,6 +60,7 @@ impl MvuApp<Model> for App {
required_features: wgpu::Features::empty(),
required_limits: wgpu::Limits::downlevel_webgl2_defaults()
.using_resolution(adapter.limits()),
memory_hints: wgpu::MemoryHints::MemoryUsage,
},
None,
)
@ -82,13 +86,13 @@ impl MvuApp<Model> for App {
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
entry_point: Some("vs_main"),
buffers: &[],
compilation_options: Default::default(),
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
entry_point: Some("fs_main"),
compilation_options: Default::default(),
targets: &[Some(swapchain_format.into())],
}),
@ -96,11 +100,13 @@ impl MvuApp<Model> for App {
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
multiview: None,
cache: None,
});
let config = surface
let mut config = surface
.get_default_config(&adapter, size.width, size.height)
.unwrap();
config.view_formats.push(config.format);
surface.configure(&device, &config);
self.context = Some(Context {
@ -109,6 +115,7 @@ impl MvuApp<Model> for App {
device,
render_pipeline,
queue,
scene_data: None,
});
info!("Initialized {}x{}.", size.width, size.height);
@ -119,11 +126,10 @@ impl MvuApp<Model> for App {
config,
surface,
device,
render_pipeline: _,
queue: _,
..
}) = &mut self.context
{
config.width = dbg!(new_size).width.max(1);
config.width = new_size.width.max(1);
config.height = new_size.height.max(1);
surface.configure(device, config);
@ -138,23 +144,31 @@ impl MvuApp<Model> for App {
}
}
async fn view(&mut self, _model: Rc<Model>) -> Result<(), Box<dyn std::error::Error>> {
if let Some(Context {
config: _,
surface,
device,
render_pipeline,
queue,
}) = &self.context
{
let frame = surface
async fn view(&mut self, model: Rc<Model>) -> Result<(), Box<dyn std::error::Error>> {
if let Some(context) = &mut self.context {
if context.scene_data.is_none() {
if let Some(dem) = &model.dem {
context.scene_data = Some(DemRenderer::new(
dem.clone(),
&context.device,
&context.config,
))
}
}
let frame = context
.surface
.get_current_texture()
.expect("Failed to acquire next swap chain texture");
let view = frame
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
if let Some(scene_data) = &mut context.scene_data {
scene_data.render(&view, &context.device, &context.queue);
} else {
let mut encoder = context
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
{
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
@ -170,11 +184,12 @@ impl MvuApp<Model> for App {
timestamp_writes: None,
occlusion_query_set: None,
});
rpass.set_pipeline(render_pipeline);
rpass.set_pipeline(&context.render_pipeline);
rpass.draw(0..3, 0..1);
}
queue.submit(Some(encoder.finish()));
context.queue.submit(Some(encoder.finish()));
}
frame.present();
}
Ok(())
@ -182,6 +197,6 @@ impl MvuApp<Model> for App {
}
fn open_test_file(file_path: PathBuf, model: Rc<Model>) -> Rc<Model> {
let dem = Some(raster::Dem::load_from_image(&file_path));
let dem = Some(Rc::new(raster::Dem::load_from_image(&file_path)));
Rc::new(Model { dem })
}