Compare commits

...

2 commits

Author SHA1 Message Date
2b9c3ab25a
vscode: Organize imports 2024-12-11 21:08:28 +01:00
7cbc785888
Scene: manage error 2024-12-11 21:07:47 +01:00
4 changed files with 56 additions and 42 deletions

View file

@ -1,5 +1,8 @@
{ {
"editor.formatOnSave": true, "editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "always",
},
"[rust]": { "[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer", "editor.defaultFormatter": "rust-lang.rust-analyzer",
}, },

View file

@ -146,11 +146,14 @@ impl ApplicationHandler for App {
let surface = Surface::from_window(self.instance.clone(), window.clone()).unwrap(); let surface = Surface::from_window(self.instance.clone(), window.clone()).unwrap();
self.rcx = Some(RenderContext::new(window, surface, &self.device)); self.rcx = Some(RenderContext::new(window, surface, &self.device));
self.scene = Some(Scene::initialize( self.scene = Some(
&self.device, Scene::load(
&self.rcx.as_ref().unwrap().swapchain, &self.device,
&self.memory_allocator, &self.rcx.as_ref().unwrap().swapchain,
)); &self.memory_allocator,
)
.unwrap(),
);
} }
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) { fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
@ -230,7 +233,7 @@ impl ApplicationHandler for App {
.unwrap(); .unwrap();
if let Some(scene) = self.scene.as_ref() { if let Some(scene) = self.scene.as_ref() {
scene.render(&mut builder); scene.render(&mut builder).unwrap();
} }
builder.end_rendering().unwrap(); builder.end_rendering().unwrap();

View file

@ -1,3 +1,4 @@
use std::error::Error;
use std::sync::Arc; use std::sync::Arc;
use vulkano::device::Device; use vulkano::device::Device;
use vulkano::pipeline::graphics::color_blend::{ColorBlendAttachmentState, ColorBlendState}; use vulkano::pipeline::graphics::color_blend::{ColorBlendAttachmentState, ColorBlendState};
@ -12,6 +13,7 @@ use vulkano::pipeline::layout::PipelineDescriptorSetLayoutCreateInfo;
use vulkano::pipeline::{ use vulkano::pipeline::{
DynamicState, GraphicsPipeline, PipelineLayout, PipelineShaderStageCreateInfo, DynamicState, GraphicsPipeline, PipelineLayout, PipelineShaderStageCreateInfo,
}; };
use vulkano::shader::EntryPoint;
use vulkano::swapchain::Swapchain; use vulkano::swapchain::Swapchain;
use crate::renderer::Vertex2D; use crate::renderer::Vertex2D;
@ -35,37 +37,26 @@ mod shaders {
pub fn create_triangle_pipeline( pub fn create_triangle_pipeline(
device: &Arc<Device>, device: &Arc<Device>,
swapchain: &Arc<Swapchain>, swapchain: &Arc<Swapchain>,
) -> Arc<GraphicsPipeline> { ) -> Result<Arc<GraphicsPipeline>, Box<dyn Error>> {
let vs = shaders::vs::load(device.clone()) let (vs, fs) = load_shaders(device)?;
.unwrap() let vertex_input_state = Vertex2D::per_vertex().definition(&vs)?;
.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();
let stages = [ let stages = [
PipelineShaderStageCreateInfo::new(vs), PipelineShaderStageCreateInfo::new(vs),
PipelineShaderStageCreateInfo::new(fs), PipelineShaderStageCreateInfo::new(fs),
]; ];
let layout = PipelineLayout::new( let create_info = PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages)
device.clone(), .into_pipeline_layout_create_info(device.clone())?;
PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages)
.into_pipeline_layout_create_info(device.clone()) let layout = PipelineLayout::new(device.clone(), create_info)?;
.unwrap(),
)
.unwrap();
let subpass = PipelineRenderingCreateInfo { let subpass = PipelineRenderingCreateInfo {
color_attachment_formats: vec![Some(swapchain.image_format())], color_attachment_formats: vec![Some(swapchain.image_format())],
..Default::default() ..Default::default()
}; };
GraphicsPipeline::new( let pipeline = GraphicsPipeline::new(
device.clone(), device.clone(),
None, None,
GraphicsPipelineCreateInfo { GraphicsPipelineCreateInfo {
@ -83,6 +74,19 @@ pub fn create_triangle_pipeline(
subpass: Some(subpass.into()), subpass: Some(subpass.into()),
..GraphicsPipelineCreateInfo::layout(layout) ..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 std::sync::Arc;
use vulkano::buffer::Subbuffer; use vulkano::buffer::Subbuffer;
use vulkano::command_buffer::{AutoCommandBufferBuilder, PrimaryAutoCommandBuffer}; use vulkano::command_buffer::{AutoCommandBufferBuilder, PrimaryAutoCommandBuffer};
@ -69,31 +70,34 @@ pub struct Scene {
} }
impl Scene { impl Scene {
pub fn initialize( pub fn load(
device: &Arc<Device>, device: &Arc<Device>,
swapchain: &Arc<Swapchain>, swapchain: &Arc<Swapchain>,
memory_allocator: &Arc<StandardMemoryAllocator>, memory_allocator: &Arc<StandardMemoryAllocator>,
) -> Scene { ) -> Result<Self, Box<dyn Error>> {
let pipeline = create_triangle_pipeline(device, swapchain); let pipeline = create_triangle_pipeline(device, swapchain)?;
let vertex_buffer = let vertex_buffer = Vertex2D::create_buffer(Vec::from_iter(VERTICES), memory_allocator)?;
Vertex2D::create_buffer(Vec::from_iter(VERTICES), memory_allocator).unwrap();
Scene { Ok(Scene {
pipeline, pipeline,
vertex_buffer, vertex_buffer,
} })
} }
pub fn render(&self, builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>) { pub fn render(
builder &self,
.bind_pipeline_graphics(self.pipeline.clone()) builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>,
.unwrap() ) -> Result<(), Box<dyn Error>> {
.bind_vertex_buffers(0, self.vertex_buffer.clone())
.unwrap();
let vertex_count = self.vertex_buffer.len() as u32; let vertex_count = self.vertex_buffer.len() as u32;
let instance_count = vertex_count / 3; 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(())
} }
} }