From 7cbc78588896cdd4d7e98fee9a34dfc796587f21 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Wed, 11 Dec 2024 21:07:47 +0100 Subject: [PATCH] Scene: manage error --- src/renderer/app.rs | 15 ++++--- src/renderer/pipelines/triangle_pipeline.rs | 46 +++++++++++---------- src/renderer/scene.rs | 34 ++++++++------- 3 files changed, 53 insertions(+), 42 deletions(-) diff --git a/src/renderer/app.rs b/src/renderer/app.rs index 784e6ce..8651372 100644 --- a/src/renderer/app.rs +++ b/src/renderer/app.rs @@ -146,11 +146,14 @@ impl ApplicationHandler for App { let surface = Surface::from_window(self.instance.clone(), window.clone()).unwrap(); self.rcx = Some(RenderContext::new(window, surface, &self.device)); - self.scene = Some(Scene::initialize( - &self.device, - &self.rcx.as_ref().unwrap().swapchain, - &self.memory_allocator, - )); + self.scene = Some( + Scene::load( + &self.device, + &self.rcx.as_ref().unwrap().swapchain, + &self.memory_allocator, + ) + .unwrap(), + ); } fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) { @@ -230,7 +233,7 @@ impl ApplicationHandler for App { .unwrap(); if let Some(scene) = self.scene.as_ref() { - scene.render(&mut builder); + scene.render(&mut builder).unwrap(); } builder.end_rendering().unwrap(); diff --git a/src/renderer/pipelines/triangle_pipeline.rs b/src/renderer/pipelines/triangle_pipeline.rs index b5858e2..77d1092 100644 --- a/src/renderer/pipelines/triangle_pipeline.rs +++ b/src/renderer/pipelines/triangle_pipeline.rs @@ -1,3 +1,4 @@ +use std::error::Error; use std::sync::Arc; use vulkano::device::Device; use vulkano::pipeline::graphics::color_blend::{ColorBlendAttachmentState, ColorBlendState}; @@ -12,6 +13,7 @@ use vulkano::pipeline::layout::PipelineDescriptorSetLayoutCreateInfo; use vulkano::pipeline::{ DynamicState, GraphicsPipeline, PipelineLayout, PipelineShaderStageCreateInfo, }; +use vulkano::shader::EntryPoint; use vulkano::swapchain::Swapchain; use crate::renderer::Vertex2D; @@ -35,37 +37,26 @@ mod shaders { pub fn create_triangle_pipeline( device: &Arc, swapchain: &Arc, -) -> Arc { - let vs = shaders::vs::load(device.clone()) - .unwrap() - .entry_point("main") - .unwrap(); - let fs = shaders::fs::load(device.clone()) - .unwrap() - .entry_point("main") - .unwrap(); - - let vertex_input_state = Vertex2D::per_vertex().definition(&vs).unwrap(); +) -> Result, Box> { + let (vs, fs) = load_shaders(device)?; + let vertex_input_state = Vertex2D::per_vertex().definition(&vs)?; let stages = [ PipelineShaderStageCreateInfo::new(vs), PipelineShaderStageCreateInfo::new(fs), ]; - let layout = PipelineLayout::new( - device.clone(), - PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages) - .into_pipeline_layout_create_info(device.clone()) - .unwrap(), - ) - .unwrap(); + let create_info = PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages) + .into_pipeline_layout_create_info(device.clone())?; + + let layout = PipelineLayout::new(device.clone(), create_info)?; let subpass = PipelineRenderingCreateInfo { color_attachment_formats: vec![Some(swapchain.image_format())], ..Default::default() }; - GraphicsPipeline::new( + let pipeline = GraphicsPipeline::new( device.clone(), None, GraphicsPipelineCreateInfo { @@ -83,6 +74,19 @@ pub fn create_triangle_pipeline( subpass: Some(subpass.into()), ..GraphicsPipelineCreateInfo::layout(layout) }, - ) - .unwrap() + )?; + + Ok(pipeline) +} + +fn load_shaders(device: &Arc) -> Result<(EntryPoint, EntryPoint), Box> { + let vs = shaders::vs::load(device.clone())? + .entry_point("main") + .ok_or(format!("Failed find main entry point of vertex shader"))?; + + let fs = shaders::fs::load(device.clone())? + .entry_point("main") + .ok_or(format!("Failed find main entry point of fragment shader"))?; + + Ok((vs, fs)) } diff --git a/src/renderer/scene.rs b/src/renderer/scene.rs index e327f05..c820380 100644 --- a/src/renderer/scene.rs +++ b/src/renderer/scene.rs @@ -1,3 +1,4 @@ +use std::error::Error; use std::sync::Arc; use vulkano::buffer::Subbuffer; use vulkano::command_buffer::{AutoCommandBufferBuilder, PrimaryAutoCommandBuffer}; @@ -69,31 +70,34 @@ pub struct Scene { } impl Scene { - pub fn initialize( + pub fn load( device: &Arc, swapchain: &Arc, memory_allocator: &Arc, - ) -> Scene { - let pipeline = create_triangle_pipeline(device, swapchain); - let vertex_buffer = - Vertex2D::create_buffer(Vec::from_iter(VERTICES), memory_allocator).unwrap(); + ) -> Result> { + let pipeline = create_triangle_pipeline(device, swapchain)?; + let vertex_buffer = Vertex2D::create_buffer(Vec::from_iter(VERTICES), memory_allocator)?; - Scene { + Ok(Scene { pipeline, vertex_buffer, - } + }) } - pub fn render(&self, builder: &mut AutoCommandBufferBuilder) { - builder - .bind_pipeline_graphics(self.pipeline.clone()) - .unwrap() - .bind_vertex_buffers(0, self.vertex_buffer.clone()) - .unwrap(); - + pub fn render( + &self, + builder: &mut AutoCommandBufferBuilder, + ) -> Result<(), Box> { let vertex_count = self.vertex_buffer.len() as u32; let instance_count = vertex_count / 3; - unsafe { builder.draw(vertex_count, instance_count, 0, 0) }.unwrap(); + unsafe { + builder + .bind_pipeline_graphics(self.pipeline.clone())? + .bind_vertex_buffers(0, self.vertex_buffer.clone())? + .draw(vertex_count, instance_count, 0, 0)?; + } + + Ok(()) } }