1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-27 23:46:31 +03:00

add wgpu example

This commit is contained in:
Wez Furlong 2021-05-06 09:04:16 -07:00
parent f4939c510b
commit 058c3f612f
5 changed files with 755 additions and 1 deletions

475
Cargo.lock generated
View File

@ -126,6 +126,15 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
[[package]]
name = "ash"
version = "0.32.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06063a002a77d2734631db74e8f4ce7148b77fe522e6bca46f2ae7774fd48112"
dependencies = [
"libloading 0.7.0",
]
[[package]]
name = "async-channel"
version = "1.6.1"
@ -371,6 +380,21 @@ checksum = "ea52a394d9a85bf9819ca50382187ece2497501dbd6397fd33388400e512a90e"
name = "bintree"
version = "0.1.0"
[[package]]
name = "bit-set"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e11e16035ea35e4e5997b393eacbf6f63983188f7a2ad25bfb13465f5ad59de"
dependencies = [
"bit-vec",
]
[[package]]
name = "bit-vec"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
[[package]]
name = "bitflags"
version = "1.2.1"
@ -547,6 +571,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_aliases"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e"
[[package]]
name = "cgl"
version = "0.3.2"
@ -631,6 +661,21 @@ dependencies = [
"objc",
]
[[package]]
name = "cocoa-foundation"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ade49b65d560ca58c403a479bb396592b155c0185eada742ee323d1d68d6318"
dependencies = [
"bitflags",
"block",
"core-foundation 0.9.1",
"core-graphics-types",
"foreign-types",
"libc",
"objc",
]
[[package]]
name = "codec"
version = "0.1.0"
@ -652,6 +697,16 @@ dependencies = [
"zstd",
]
[[package]]
name = "codespan-reporting"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e"
dependencies = [
"termcolor",
"unicode-width",
]
[[package]]
name = "color_quant"
version = "1.1.0"
@ -719,6 +774,12 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb58b6451e8c2a812ad979ed1d83378caa5e927eef2622017a45f251457c2c9d"
[[package]]
name = "copyless"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2df960f5d869b2dd8532793fde43eb5427cceb126c929747a26823ab0eeb536"
[[package]]
name = "core-foundation"
version = "0.7.0"
@ -894,6 +955,17 @@ dependencies = [
"syn",
]
[[package]]
name = "d3d12"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "091ed1b25fe47c7ff129fc440c23650b6114f36aa00bc7212cc8041879294428"
dependencies = [
"bitflags",
"libloading 0.7.0",
"winapi 0.3.9",
]
[[package]]
name = "deflate"
version = "0.8.6"
@ -1190,6 +1262,12 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "fixedbitset"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37ab347416e802de484e4d03c7316c48f1ecb56574dfd4a46a80f173ce1de04d"
[[package]]
name = "flate2"
version = "1.0.20"
@ -1416,6 +1494,15 @@ dependencies = [
"slab",
]
[[package]]
name = "fxhash"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c"
dependencies = [
"byteorder",
]
[[package]]
name = "generic-array"
version = "0.12.4"
@ -1468,6 +1555,152 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "gfx-auxil"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ccf8711c9994dfa34337466bee3ae1462e172874c432ce4eb120ab2e98d39cf"
dependencies = [
"fxhash",
"gfx-hal",
"spirv_cross",
]
[[package]]
name = "gfx-backend-dx11"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f839f27f8c8a6dc553ccca7f5b35a42009432bc25db9688bba7061cd394161f"
dependencies = [
"arrayvec",
"bitflags",
"gfx-auxil",
"gfx-hal",
"libloading 0.7.0",
"log",
"parking_lot 0.11.1",
"range-alloc",
"raw-window-handle",
"smallvec 1.6.1",
"spirv_cross",
"thunderdome",
"winapi 0.3.9",
"wio",
]
[[package]]
name = "gfx-backend-dx12"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3937738b0da5839bba4e33980d29f9a06dbce184d04a3a08c9a949e7953700e3"
dependencies = [
"arrayvec",
"bit-set",
"bitflags",
"d3d12",
"gfx-auxil",
"gfx-hal",
"log",
"parking_lot 0.11.1",
"range-alloc",
"raw-window-handle",
"smallvec 1.6.1",
"spirv_cross",
"thunderdome",
"winapi 0.3.9",
]
[[package]]
name = "gfx-backend-empty"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ac55ada4bfcd35479b3421eea324d36d7da5f724e2f66ecb36d4efdb7041a5e"
dependencies = [
"gfx-hal",
"log",
"raw-window-handle",
]
[[package]]
name = "gfx-backend-gl"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0caa03d6e0b7b4f202aea1f20c3f3288cfa06d92d24cea9d69c9a7627967244a"
dependencies = [
"arrayvec",
"bitflags",
"fxhash",
"gfx-hal",
"glow",
"js-sys",
"khronos-egl",
"libloading 0.7.0",
"log",
"naga",
"parking_lot 0.11.1",
"raw-window-handle",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "gfx-backend-metal"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96a809b746d8063ade971cfb3f188e2b0d655926979823d80be7590390f4b911"
dependencies = [
"arrayvec",
"bitflags",
"block",
"cocoa-foundation",
"copyless",
"foreign-types",
"fxhash",
"gfx-hal",
"log",
"metal",
"naga",
"objc",
"parking_lot 0.11.1",
"profiling",
"range-alloc",
"raw-window-handle",
"storage-map",
]
[[package]]
name = "gfx-backend-vulkan"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a353fc6fdb42ec646de49bbb74e4870e37a7e680caf33f3ac0615c30b1146d94"
dependencies = [
"arrayvec",
"ash",
"byteorder",
"core-graphics-types",
"gfx-hal",
"inplace_it",
"log",
"naga",
"objc",
"parking_lot 0.11.1",
"raw-window-handle",
"smallvec 1.6.1",
"winapi 0.3.9",
]
[[package]]
name = "gfx-hal"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d285bfd566f6b9134af908446ca350c0a1047495dfb9bbd826e701e8ee1d259"
dependencies = [
"bitflags",
"naga",
"raw-window-handle",
"thiserror",
]
[[package]]
name = "gif"
version = "0.11.2"
@ -1523,12 +1756,63 @@ dependencies = [
"web-sys",
]
[[package]]
name = "glow"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b80b98efaa8a34fce11d60dd2ce2760d5d83c373cbcc73bb87c2a3a84a54108"
dependencies = [
"js-sys",
"slotmap",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "glyph-names"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8050dc1be3880f690a771051d09727d759229d299afffa1488acc3556879039d"
[[package]]
name = "gpu-alloc"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc76088804bb65a6f3b880bea9306fdaeffb25ebb453105fafa691282ee9fdba"
dependencies = [
"bitflags",
"gpu-alloc-types",
]
[[package]]
name = "gpu-alloc-types"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54804d0d6bc9d7f26db4eaec1ad10def69b599315f487d32c334a80d1efe67a5"
dependencies = [
"bitflags",
]
[[package]]
name = "gpu-descriptor"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8a70f1e87a3840ed6a3e99e02c2b861e4dbdf26f0d07e38f42ea5aff46cfce2"
dependencies = [
"bitflags",
"gpu-descriptor-types",
"hashbrown 0.9.1",
]
[[package]]
name = "gpu-descriptor-types"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "363e3677e55ad168fef68cf9de3a4a310b53124c5e784c53a1d70e92d23f2126"
dependencies = [
"bitflags",
]
[[package]]
name = "guillotiere"
version = "0.6.0"
@ -1656,6 +1940,16 @@ dependencies = [
"tiff",
]
[[package]]
name = "indexmap"
version = "1.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3"
dependencies = [
"autocfg",
"hashbrown 0.9.1",
]
[[package]]
name = "inotify"
version = "0.7.1"
@ -1676,6 +1970,12 @@ dependencies = [
"libc",
]
[[package]]
name = "inplace_it"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90953f308a79fe6d62a4643e51f848fbfddcd05975a38e69fdf4ab86a7baf7ca"
[[package]]
name = "instant"
version = "0.1.9"
@ -1772,6 +2072,16 @@ dependencies = [
"winapi-build",
]
[[package]]
name = "khronos-egl"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c2352bd1d0bceb871cb9d40f24360c8133c11d7486b68b5381c1dd1a32015e3"
dependencies = [
"libc",
"libloading 0.7.0",
]
[[package]]
name = "khronos_api"
version = "3.1.0"
@ -2046,6 +2356,20 @@ dependencies = [
"autocfg",
]
[[package]]
name = "metal"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c12e48c737ee9a55e8bb2352bcde588f79ae308d3529ee888f7cc0f469b5777"
dependencies = [
"bitflags",
"block",
"cocoa-foundation",
"foreign-types",
"log",
"objc",
]
[[package]]
name = "metrics"
version = "0.14.2"
@ -2186,6 +2510,23 @@ dependencies = [
"wezterm-term",
]
[[package]]
name = "naga"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a462414ac6a74a8fcc2c6d235d9a92b288f22682c016cf725e75d0c9470fb515"
dependencies = [
"bit-set",
"bitflags",
"codespan-reporting",
"fxhash",
"log",
"num-traits 0.2.14",
"petgraph",
"spirv_headers",
"thiserror",
]
[[package]]
name = "nanorand"
version = "0.5.2"
@ -2434,6 +2775,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1"
dependencies = [
"malloc_buf",
"objc_exception",
]
[[package]]
@ -2447,6 +2789,15 @@ dependencies = [
"objc_id",
]
[[package]]
name = "objc_exception"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad970fb455818ad6cba4c122ad012fae53ae8b4795f86378bce65e4f6bab2ca4"
dependencies = [
"cc",
]
[[package]]
name = "objc_id"
version = "0.1.1"
@ -2695,6 +3046,16 @@ dependencies = [
"sha-1",
]
[[package]]
name = "petgraph"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "467d164a6de56270bd7c4d070df81d07beace25012d5103ced4e9ff08d6afdb7"
dependencies = [
"fixedbitset",
"indexmap",
]
[[package]]
name = "phf"
version = "0.8.0"
@ -2899,6 +3260,12 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "profiling"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3a66d5e88679f2720126c11ee29da07a08f094eac52306b066edd7d393752d6"
[[package]]
name = "promise"
version = "0.2.0"
@ -3029,6 +3396,12 @@ dependencies = [
"rand_core 0.5.1",
]
[[package]]
name = "range-alloc"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63e935c45e09cc6dcf00d2f0b2d630a58f4095320223d47fc68918722f0538b6"
[[package]]
name = "rangeset"
version = "0.1.0"
@ -3510,6 +3883,12 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f173ac3d1a7e3b28003f40de0b5ce7fe2710f9b9dc3fc38664cebee46b3b6527"
[[package]]
name = "slotmap"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c46a3482db8f247956e464d783693ece164ca056e6e67563ee5505bdb86452cd"
[[package]]
name = "smallvec"
version = "0.6.14"
@ -3593,6 +3972,27 @@ dependencies = [
"lock_api 0.4.4",
]
[[package]]
name = "spirv_cross"
version = "0.23.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60647fadbf83c4a72f0d7ea67a7ca3a81835cf442b8deae5c134c3e0055b2e14"
dependencies = [
"cc",
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "spirv_headers"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f5b132530b1ac069df335577e3581765995cba5a13995cdbbdbc8fb057c532c"
dependencies = [
"bitflags",
"num-traits 0.2.14",
]
[[package]]
name = "ssh2"
version = "0.9.1"
@ -3616,6 +4016,15 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "storage-map"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "418bb14643aa55a7841d5303f72cf512cfb323b8cc221d51580500a1ca75206c"
dependencies = [
"lock_api 0.4.4",
]
[[package]]
name = "strip-ansi-escapes"
version = "0.1.0"
@ -3845,6 +4254,12 @@ dependencies = [
"syn",
]
[[package]]
name = "thunderdome"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87b4947742c93ece24a0032141d9caa3d853752e694a57e35029dd2bd08673e0"
[[package]]
name = "tiff"
version = "0.6.1"
@ -4609,6 +5024,65 @@ dependencies = [
"zvariant",
]
[[package]]
name = "wgpu"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f07667388164ad75066dcbab46e295abd8204fd9a6dc057704b8d703837d12a8"
dependencies = [
"arrayvec",
"js-sys",
"log",
"naga",
"parking_lot 0.11.1",
"raw-window-handle",
"smallvec 1.6.1",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu-core",
"wgpu-types",
]
[[package]]
name = "wgpu-core"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59abd59fe91fe502fe2dd8777bcac414069199af22dd2a92a1bb635f9f251425"
dependencies = [
"arrayvec",
"bitflags",
"cfg_aliases",
"copyless",
"fxhash",
"gfx-backend-dx11",
"gfx-backend-dx12",
"gfx-backend-empty",
"gfx-backend-gl",
"gfx-backend-metal",
"gfx-backend-vulkan",
"gfx-hal",
"gpu-alloc",
"gpu-descriptor",
"log",
"naga",
"parking_lot 0.11.1",
"profiling",
"raw-window-handle",
"smallvec 1.6.1",
"thiserror",
"wgpu-types",
]
[[package]]
name = "wgpu-types"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa248d90c8e6832269b8955bf800e8241f942c25e18a235b7752226804d21556"
dependencies = [
"bitflags",
]
[[package]]
name = "which"
version = "4.1.0"
@ -4704,6 +5178,7 @@ dependencies = [
"wayland-client",
"wayland-egl",
"wezterm-input-types",
"wgpu",
"winapi 0.3.9",
"winreg 0.6.2",
"x11",

View File

@ -10,6 +10,7 @@ build = "build.rs"
[dev-dependencies]
pretty_env_logger = "0.4"
wgpu = "0.8"
[build-dependencies]
gl_generator = "0.14"

View File

@ -85,7 +85,7 @@ async fn spawn_window() -> Result<(), Box<dyn std::error::Error>> {
frame.clear_color_srgb(0.25, 0.125, 0.375, 1.0);
win.finish_frame(frame)?;
}
WindowEvent::FocusChanged(_) => {}
WindowEvent::Notification(_) | WindowEvent::FocusChanged(_) => {}
}
}

View File

@ -0,0 +1,13 @@
// vim:ft=rust:
[[stage(vertex)]]
fn vs_main([[builtin(vertex_index)]] in_vertex_index: u32) -> [[builtin(position)]] vec4<f32> {
let x = f32(i32(in_vertex_index) - 1);
let y = f32(i32(in_vertex_index & 1u) * 2 - 1);
return vec4<f32>(x, y, 0.0, 1.0);
}
[[stage(fragment)]]
fn fs_main() -> [[location(0)]] vec4<f32> {
return vec4<f32>(1.0, 0.0, 0.0, 1.0);
}

265
window/examples/wgpu.rs Normal file
View File

@ -0,0 +1,265 @@
use ::window::*;
use anyhow::Context;
use promise::spawn::spawn;
pub struct GpuContext {
pub swap_chain: wgpu::SwapChain,
pub sc_desc: wgpu::SwapChainDescriptor,
pub adapter: wgpu::Adapter,
pub device: wgpu::Device,
pub queue: wgpu::Queue,
pub surface: wgpu::Surface,
}
struct MyWindow {
allow_close: bool,
cursor_pos: Point,
dims: Dimensions,
gpu: Option<GpuContext>,
render_pipeline: Option<wgpu::RenderPipeline>,
}
impl Drop for MyWindow {
fn drop(&mut self) {
eprintln!("MyWindow dropped");
}
}
impl MyWindow {
async fn enable_wgpu(&mut self, win: &Window) -> anyhow::Result<()> {
let instance = wgpu::Instance::new(if cfg!(target_os = "macos") {
wgpu::BackendBit::METAL
} else if cfg!(windows) {
wgpu::BackendBit::DX12
} else {
wgpu::BackendBit::PRIMARY
});
let surface = unsafe { instance.create_surface(win) };
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::LowPower,
compatible_surface: Some(&surface),
})
.await
.ok_or_else(|| anyhow::anyhow!("No suitable GPU adapters found on the system!"))?;
let adapter_info = adapter.get_info();
log::info!("wgpu adapter: {:?}", adapter_info);
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: wgpu::Features::empty(),
limits: wgpu::Limits::default(),
},
None,
)
.await
.context("Unable to find a suitable GPU adapter!")?;
log::info!("wgpu device features: {:?}", device.features());
log::info!("wgpu device limits: {:?}", device.limits());
let format = adapter
.get_swap_chain_preferred_format(&surface)
.ok_or_else(|| anyhow::anyhow!("adapter is not compatible with surface"))?;
let sc_desc = wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
format,
width: self.dims.pixel_width as u32,
height: self.dims.pixel_height as u32,
present_mode: wgpu::PresentMode::Mailbox,
};
let swap_chain = device.create_swap_chain(&surface, &sc_desc);
let gpu = GpuContext {
swap_chain,
sc_desc,
adapter,
device,
queue,
surface,
};
let shader = gpu
.device
.create_shader_module(&wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(include_str!(
"shader.wgsl"
))),
flags: wgpu::ShaderFlags::all(),
});
let pipeline_layout = gpu
.device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[],
push_constant_ranges: &[],
});
self.render_pipeline
.replace(
gpu.device
.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: None,
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
buffers: &[],
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
targets: &[format.into()],
}),
primitive: wgpu::PrimitiveState::default(),
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
}),
);
self.gpu.replace(gpu);
Ok(())
}
fn paint(&mut self) -> anyhow::Result<()> {
if let Some(gpu) = self.gpu.as_mut() {
let frame = match gpu.swap_chain.get_current_frame() {
Ok(frame) => frame,
Err(err) => {
log::info!("get_current_frame: {:#}", err);
gpu.swap_chain = gpu.device.create_swap_chain(&gpu.surface, &gpu.sc_desc);
gpu.swap_chain
.get_current_frame()
.expect("Failed to acquire next swap chain texture!")
}
};
let mut encoder = gpu
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
{
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &frame.output.view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0.,
g: 1.,
b: 0.,
a: 0.5,
}),
store: true,
},
}],
depth_stencil_attachment: None,
});
rpass.set_pipeline(self.render_pipeline.as_ref().unwrap());
rpass.draw(0..3, 0..1);
}
gpu.queue.submit(Some(encoder.finish()));
}
Ok(())
}
fn resize(&mut self, dims: Dimensions) {
if self.dims == dims {
// May just be a move event
return;
}
self.dims = dims;
if let Some(gpu) = self.gpu.as_mut() {
gpu.sc_desc.width = dims.pixel_width as u32;
gpu.sc_desc.height = dims.pixel_height as u32;
gpu.swap_chain = gpu.device.create_swap_chain(&gpu.surface, &gpu.sc_desc);
}
}
}
async fn spawn_window() -> anyhow::Result<()> {
let (win, events) = Window::new_window("myclass", "the title", 800, 600, None).await?;
let mut state = MyWindow {
allow_close: false,
cursor_pos: Point::new(100, 200),
dims: Dimensions {
pixel_width: 800,
pixel_height: 600,
dpi: 0,
},
gpu: None,
render_pipeline: None,
};
eprintln!("before show");
win.show().await?;
state.enable_wgpu(&win).await?;
eprintln!("window is visible, do loop");
while let Ok(event) = events.recv().await {
match event {
WindowEvent::CloseRequested => {
eprintln!("can I close?");
if state.allow_close {
win.close();
} else {
state.allow_close = true;
}
}
WindowEvent::Destroyed => {
eprintln!("destroy was called!");
Connection::get().unwrap().terminate_message_loop();
}
WindowEvent::Resized {
dimensions,
is_full_screen:_,
} => {
state.resize(dimensions);
}
WindowEvent::MouseEvent(event) => {
state.cursor_pos = event.coords;
win.invalidate();
win.set_cursor(Some(MouseCursor::Arrow));
if event.kind == MouseEventKind::Press(MousePress::Left) {
eprintln!("{:?}", event);
}
}
WindowEvent::KeyEvent(key) => {
eprintln!("{:?}", key);
win.set_cursor(Some(MouseCursor::Text));
win.default_key_processing(key);
}
WindowEvent::NeedRepaint => {
state.paint()?;
}
WindowEvent::Notification(_) | WindowEvent::FocusChanged(_) => {}
}
}
Ok(())
}
fn main() -> anyhow::Result<()> {
let _ = pretty_env_logger::formatted_builder()
.filter_level(log::LevelFilter::Info)
.try_init();
let conn = Connection::init()?;
spawn(async {
eprintln!("running this async block");
dbg!(spawn_window().await).ok();
eprintln!("end of async block");
})
.detach();
conn.run_message_loop()
}