From b27234e27bbaf4d7f7f3467de64bd95ff2145c0e Mon Sep 17 00:00:00 2001 From: Tengu712 Date: Sat, 26 Sep 2026 19:56:07 +0900 Subject: make it better --- Cargo.lock | 7 +++++++ Cargo.toml | 2 +- src/graphics.rs | 22 ++++++++++++++-------- src/main.rs | 21 +++++++++++++++++---- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6b6c7fa..0b08c75 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1943,12 +1943,17 @@ dependencies = [ "cfg_aliases", "document-features", "hashbrown 0.17.1", + "js-sys", "log", + "parking_lot", "portable-atomic", "profiling", "raw-window-handle", "smallvec", "static_assertions", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", "wgpu-core", "wgpu-hal", "wgpu-types", @@ -2070,10 +2075,12 @@ checksum = "99dad6f1fbdbbdb4c278a6508b059d44688f5cebddf78d005a46a31340269286" dependencies = [ "bitflags 2.13.2", "bytemuck", + "js-sys", "log", "naga-types", "raw-window-handle", "static_assertions", + "web-sys", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index dadfd1b..c3230a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,4 @@ edition = "2024" [dependencies] winit = "0.30.13" pollster = "1.0.1" -wgpu = { version = "30.0.0", default-features = false, features = [ "wgsl", "dx12", "metal", "vulkan" ] } +wgpu = { version = "30.0.0", default-features = false, features = [ "dx12", "metal", "parking_lot", "std", "vulkan", "wgsl" ] } diff --git a/src/graphics.rs b/src/graphics.rs index d933c8b..6fa13b3 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -2,7 +2,7 @@ use std::{borrow::Cow, sync::Arc}; use wgpu::*; use winit::{dpi::PhysicalSize, event_loop::OwnedDisplayHandle, window::Window}; -pub enum RenderResult { +pub enum AcquireSurfaceTextureResult { Ok(SurfaceTexture), ShouldSkip, Lost, @@ -42,6 +42,9 @@ impl Renderer { .map_err(|e| e.to_string())?; let surface_cap = surface.get_capabilities(&adapter); + if surface_cap.formats.is_empty() { + return Err("no surface format found".to_string()); + } let (surface_format, view_format) = decide_surface_and_view_format(&adapter, &surface_cap); @@ -110,24 +113,25 @@ impl Renderer { Ok(v) } - pub fn get_current_texture(&self, window: &Window) -> RenderResult { + pub fn get_current_texture(&self, window: &Window) -> AcquireSurfaceTextureResult { match self.surface.get_current_texture() { - CurrentSurfaceTexture::Success(v) => RenderResult::Ok(v), + CurrentSurfaceTexture::Success(v) => AcquireSurfaceTextureResult::Ok(v), CurrentSurfaceTexture::Suboptimal(v) => { // NOTE: 取得したテクスチャが生存した状態でサーフェスを再設定すると // `PreviousOutputExists`エラーになるので、 // 破棄した上で再設定し、描画をスキップする。 drop(v); self.configure_surface(window.inner_size()); - RenderResult::ShouldSkip + AcquireSurfaceTextureResult::ShouldSkip } - CurrentSurfaceTexture::Occluded => RenderResult::ShouldSkip, - CurrentSurfaceTexture::Timeout => RenderResult::ShouldSkip, + CurrentSurfaceTexture::Occluded => AcquireSurfaceTextureResult::ShouldSkip, + CurrentSurfaceTexture::Timeout => AcquireSurfaceTextureResult::ShouldSkip, CurrentSurfaceTexture::Outdated => { self.configure_surface(window.inner_size()); - RenderResult::ShouldSkip + AcquireSurfaceTextureResult::ShouldSkip } - CurrentSurfaceTexture::Lost => RenderResult::Lost, + CurrentSurfaceTexture::Lost => AcquireSurfaceTextureResult::Lost, + // TODO: 一応対応する。 CurrentSurfaceTexture::Validation => unreachable!(), } } @@ -158,6 +162,8 @@ impl Renderer { render_pass.set_pipeline(&self.pipeline); + // TODO: 描画 + drop(render_pass); self.queue.submit([encoder.finish()]); diff --git a/src/main.rs b/src/main.rs index 3c7f20d..d78aee7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use winit::{ mod graphics; -use graphics::{RenderResult, Renderer}; +use graphics::{AcquireSurfaceTextureResult, Renderer}; #[derive(Default)] struct App { @@ -30,6 +30,7 @@ impl ApplicationHandler for App { let attributes = Window::default_attributes() .with_title("君は呪われている!") + .with_resizable(false) .with_inner_size(LogicalSize::new(1280, 720)) .with_fullscreen(None); @@ -74,6 +75,17 @@ impl ApplicationHandler for App { WindowEvent::CursorLeft { .. } => { println!("left"); } + WindowEvent::Resized(size) => { + if let Some(renderer) = self.renderer.as_ref() { + renderer.configure_surface(size); + } + } + WindowEvent::ScaleFactorChanged { .. } => { + let (Some(window), Some(renderer)) = (&self.window, &self.renderer) else { + return; + }; + renderer.configure_surface(window.inner_size()); + } WindowEvent::Focused(false) | WindowEvent::Occluded(true) => { println!("focused or occluded"); } @@ -93,13 +105,14 @@ impl ApplicationHandler for App { // NOTE: Presentation modeがFIFOの場合はここでスレッドが停止する。 let current_texture = match renderer.get_current_texture(window) { - RenderResult::Ok(v) => v, - RenderResult::ShouldSkip => { + AcquireSurfaceTextureResult::Ok(v) => v, + AcquireSurfaceTextureResult::ShouldSkip => { event_loop .set_control_flow(ControlFlow::WaitUntil(start + Duration::from_millis(16))); return; } - RenderResult::Lost => panic!("fatal: device or surface lost."), + // TODO: 一応対応する。 + AcquireSurfaceTextureResult::Lost => panic!("fatal: device or surface lost."), }; renderer.render(window, current_texture); -- cgit v1.3.1