Scene: manage error

This commit is contained in:
Florian RICHER 2024-12-11 21:07:47 +01:00
parent d9f70caec0
commit 7cbc785888
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
3 changed files with 53 additions and 42 deletions

View file

@ -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();

View file

@ -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<Device>,
swapchain: &Arc<Swapchain>,
) -> Arc<GraphicsPipeline> {
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<Arc<GraphicsPipeline>, Box<dyn Error>> {
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<Device>) -> Result<(EntryPoint, EntryPoint), Box<dyn Error>> {
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))
}

View file

@ -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<Device>,
swapchain: &Arc<Swapchain>,
memory_allocator: &Arc<StandardMemoryAllocator>,
) -> Scene {
let pipeline = create_triangle_pipeline(device, swapchain);
let vertex_buffer =
Vertex2D::create_buffer(Vec::from_iter(VERTICES), memory_allocator).unwrap();
) -> Result<Self, Box<dyn Error>> {
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<PrimaryAutoCommandBuffer>) {
builder
.bind_pipeline_graphics(self.pipeline.clone())
.unwrap()
.bind_vertex_buffers(0, self.vertex_buffer.clone())
.unwrap();
pub fn render(
&self,
builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>,
) -> Result<(), Box<dyn Error>> {
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(())
}
}