From 5afd5b0c1eeaa27f31039609b8aa77609321f6aa Mon Sep 17 00:00:00 2001 From: Chance Date: Mon, 5 May 2025 17:22:02 -0400 Subject: [PATCH] fix(err): Improve error handling to reduce crashes --- Cargo.lock | 1 + Cargo.toml | 4 ++-- shaders/shader.frag | 7 ++++--- shaders/shader.vert | 28 +++++++++++++++++----------- src/main.rs | 41 +++++++++++++++++++++++++++-------------- src/model/mod.rs | 9 ++++++--- 6 files changed, 57 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 77318c1..baaf280 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3707,6 +3707,7 @@ checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" name = "zenyx" version = "0.1.0" dependencies = [ + "ahash", "allocator-api2", "build-print", "bytemuck", diff --git a/Cargo.toml b/Cargo.toml index 9cb8778..0f99368 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,7 @@ cgmath = "0.18.0" image = "0.25.6" smol = "2.0.2" -winit = { version = "0.30.9" } +winit = "0.30.9" terminator = "0.3.2" thiserror = "2.0.12" tobj = "4.0.3" @@ -55,10 +55,10 @@ vulkano = "0.35.1" wgpu = { version = "25.0.0", features = ["spirv"] } zlog.workspace = true allocator-api2 = "0.2.21" +ahash = "0.8.11" [target.aarch64-linux-android.dependencies] winit = { version = "0.30.9", features = ["android-native-activity"] } - [build-dependencies] build-print = "0.1.1" bytemuck = "1.22.0" diff --git a/shaders/shader.frag b/shaders/shader.frag index 72736f8..cee14db 100644 --- a/shaders/shader.frag +++ b/shaders/shader.frag @@ -1,7 +1,7 @@ #version 450 layout(location = 0) in vec2 tex_coords; -layout(location = 1) in vec3 normal; +layout(location = 1) in vec4 normal; layout(set = 1, binding = 0) uniform texture2D t_diffuse; layout(set = 1, binding = 1) uniform sampler s_diffuse; layout(location = 0) out vec4 out_color; @@ -9,9 +9,10 @@ layout(location = 0) out vec4 out_color; void main() { float ambient = 0.2; vec3 light_dir = normalize(vec3(0.5, 1.0, 0.5)); - float diffuse = clamp(dot(normalize(normal), light_dir), 0.0, 1.0); + float diffuse = clamp(dot(normalize(vec3(normal.x, normal.y, normal.z)), light_dir), 0.0, 1.0); float brightness = ambient + (1.0 - ambient) * diffuse; - + // out_color = vec4(normal.x, normal.y, normal.z, 1.0); + // out_color = vec3(1.0,.0.0) out_color = texture(sampler2D(t_diffuse, s_diffuse), tex_coords); out_color.r = clamp(out_color.r * 0.6 * brightness, 0.0, 1.0); out_color.g = clamp(out_color.g * 0.6 * brightness, 0.0, 1.0); diff --git a/shaders/shader.vert b/shaders/shader.vert index 7c1fe3d..620b7a3 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -5,7 +5,7 @@ layout(location = 1) in vec3 color; layout(location = 2) in vec3 normal_in; layout(location = 3) in vec2 tex_coords; layout(location = 0) out vec2 tex_coord; -layout(location = 1) out vec3 normal; +layout(location = 1) out vec4 normal; layout(push_constant) uniform float TIME; layout(set = 0, binding = 0) uniform UniformBufferObject { mat4x4 projection; @@ -16,16 +16,22 @@ mat4 rotation(float lerp) { 0., 0., 1., 0., 0., 0., 0., 1.); } + void main() { float sum_val = sin(TIME / 5); - gl_Position = view.projection * transpose(mat4( - 1.0, 0.0, 0.0, sum_val, - 0.0, 1.0, 0.0, sum_val, - 0.0, 0.0, 1.0, sum_val, - 0.0, 0.0, 0.0, 1.0) * mat4((sin(TIME) + 1.0) / 2, 0.0, 0.0, 0.0, - 0.0, (sin(TIME) + 1.0) / 2, 0.0, 0.0, - 0.0, 0.0, (sin(TIME) + 1.0) / 2, 0.0, - 0.0, 0.0, 0.0, 1) * rotation(TIME * 2.5)) * vec4(position, 1.0); - tex_coord = tex_coords; - normal = normal_in; + + mat4 model = transpose(mat4( + 1.0, 0.0, 0.0, sum_val, + 0.0, 1.0, 0.0, sum_val, + 0.0, 0.0, 1.0, sum_val, + 0.0, 0.0, 0.0, 1.0) * mat4((sin(TIME) + 1.0) / 2, 0.0, 0.0, 0.0, + 0.0, (sin(TIME) + 1.0) / 2, 0.0, 0.0, + 0.0, 0.0, (sin(TIME) + 1.0) / 2, 0.0, + 0.0, 0.0, 0.0, 1) * rotation(TIME * 2.5)); + + gl_Position = view.projection * model * vec4(position, 1.0); + tex_coord = tex_coords * abs(sin(TIME / 4)); + // tex_coord.x = tex_coord.x * sin(TIME / 2); + // tex_coord.y = tex_coord.y * cos(TIME / 2); + normal = model * vec4(normal_in, 1.0); } diff --git a/src/main.rs b/src/main.rs index c10c95f..e0fdb9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,14 +3,14 @@ use std::collections::BTreeMap; use std::io::BufReader; use std::sync::Arc; use std::time::Instant; -use tracing::info; +use tracing::{error, info}; use wgpu::util::DeviceExt; use wgpu::{ Backends, Features, FragmentState, IndexFormat, Instance, InstanceDescriptor, Limits, PipelineCompilationOptions, PushConstantRange, ShaderStages, }; use winit::application::ApplicationHandler; -use winit::event::{ElementState, MouseButton}; +use winit::event::{ElementState, MouseButton, WindowEvent}; use winit::event_loop::{ActiveEventLoop, EventLoop}; #[cfg(target_os = "android")] use winit::platform::android::activity::AndroidApp; @@ -110,7 +110,7 @@ impl WgpuRenderer<'_> { self.queue.submit(Some(encoder.finish())); let elapsed_time = std::time::Instant::elapsed(&self.start_time); self.time_elapsed = elapsed_time.as_secs_f32(); - info!("{}", self.time_elapsed); + // info!("{}", self.time_elapsed); let delta_time = std::time::Instant::now() - self.last_frame_time; self.delta = delta_time.as_secs_f32(); // info!("{}", self.delta); @@ -165,7 +165,12 @@ impl WgpuState { } async fn create_renderer<'surface>(&self, window: Arc) -> WgpuRenderer<'surface> { - let surface = self.instance.create_surface(window.clone()).unwrap(); + let surface = match self.instance.create_surface(window.clone()) { + Ok(surface) => surface, + Err(e) => { + panic!("{e}") + } + }; let adapter = self .instance .request_adapter(&wgpu::RequestAdapterOptions { @@ -322,10 +327,10 @@ impl WgpuState { usage: wgpu::TextureUsages::RENDER_ATTACHMENT, desired_maximum_frame_latency: 3, }; - + static PUMPKIN: &[u8] = include_bytes!("../Pumpkin.obj"); surface.configure(&device, &surface_config); let pumpkin = model::Model::load_obj( - &mut BufReader::new(std::fs::File::open("Pumpkin.obj").unwrap()), + &mut BufReader::new(PUMPKIN), &device, &queue, &texture_bind_group_layout, @@ -453,16 +458,15 @@ impl ApplicationHandler for App<'_> { event: winit::event::WindowEvent, ) { match event { - winit::event::WindowEvent::RedrawRequested => { - let window_ctx = self.windows.get_mut(&window_id).unwrap(); - window_ctx.renderer.draw(); - window_ctx.request_redraw(); - } + winit::event::WindowEvent::RedrawRequested => match self.windows.get_mut(&window_id) { + Some(window_ctx) => { + window_ctx.renderer.draw(); + window_ctx.request_redraw(); + } + None => self.resumed(event_loop), + }, winit::event::WindowEvent::CloseRequested => { let _ = self.windows.remove(&window_id); - if self.windows.is_empty() { - event_loop.exit(); - } } winit::event::WindowEvent::MouseInput { state, button, .. } => { if button == MouseButton::Left && state == ElementState::Pressed { @@ -501,6 +505,11 @@ impl ApplicationHandler for App<'_> { .configure(&window_ctx.renderer.device, &new_config) } } + WindowEvent::Destroyed => { + if self.windows.is_empty() { + event_loop.exit(); + } + } _ => (), } } @@ -554,12 +563,16 @@ pub fn run_app(event_loop: winit::event_loop::EventLoop<()>) -> Result<(), termi #[unsafe(no_mangle)] #[cfg(target_os = "android")] extern "C" fn android_main(app: AndroidApp) { + use android_logger::Config; + use android_logger::FilterBuilder; + use tracing::level_filters::LevelFilter; use winit::event_loop::EventLoopBuilder; use winit::platform::android::EventLoopBuilderExtAndroid; let event_loop = EventLoopBuilder::default() .with_android_app(app) .build() .unwrap(); + android_logger::init_once(Config::default().with_tag("Zenyx")); run_app(event_loop).unwrap() } diff --git a/src/model/mod.rs b/src/model/mod.rs index 23bb69b..ea2b72b 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -1,8 +1,11 @@ -use std::io::{BufRead, BufReader}; +use std::{ + collections::HashMap, + io::{BufRead, BufReader}, +}; use crate::texture::Texture; use cgmath::{Vector2, Vector3, Zero}; -use tobj::Model as tModel; +use tobj::{MTLLoadResult, Model as tModel}; use wgpu::util::DeviceExt; pub struct Model { @@ -38,7 +41,7 @@ impl Model { single_index: true, ..Default::default() }, - |p| tobj::load_mtl_buf(&mut BufReader::new(std::fs::File::open(p).unwrap())), + |_| MTLLoadResult::Ok((vec![], ahash::AHashMap::new())), ) .unwrap();