Compare commits

..

No commits in common. "2b9c3ab25a7ac1b99c3f60714d01c20d6403cd59" and "d9f70caec0f604ebf833a6adf93db3b2f5439745" have entirely different histories.

4 changed files with 42 additions and 56 deletions

View file

@ -1,8 +1,5 @@
{ {
"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,14 +146,11 @@ 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( self.scene = Some(Scene::initialize(
Scene::load(
&self.device, &self.device,
&self.rcx.as_ref().unwrap().swapchain, &self.rcx.as_ref().unwrap().swapchain,
&self.memory_allocator, &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) {
@ -233,7 +230,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).unwrap(); scene.render(&mut builder);
} }
builder.end_rendering().unwrap(); builder.end_rendering().unwrap();

View file

@ -1,4 +1,3 @@
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};
@ -13,7 +12,6 @@ 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;
@ -37,26 +35,37 @@ mod shaders {
pub fn create_triangle_pipeline( pub fn create_triangle_pipeline(
device: &Arc<Device>, device: &Arc<Device>,
swapchain: &Arc<Swapchain>, swapchain: &Arc<Swapchain>,
) -> Result<Arc<GraphicsPipeline>, Box<dyn Error>> { ) -> Arc<GraphicsPipeline> {
let (vs, fs) = load_shaders(device)?; let vs = shaders::vs::load(device.clone())
let vertex_input_state = Vertex2D::per_vertex().definition(&vs)?; .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();
let stages = [ let stages = [
PipelineShaderStageCreateInfo::new(vs), PipelineShaderStageCreateInfo::new(vs),
PipelineShaderStageCreateInfo::new(fs), PipelineShaderStageCreateInfo::new(fs),
]; ];
let create_info = PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages) let layout = PipelineLayout::new(
.into_pipeline_layout_create_info(device.clone())?; device.clone(),
PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages)
let layout = PipelineLayout::new(device.clone(), create_info)?; .into_pipeline_layout_create_info(device.clone())
.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()
}; };
let pipeline = GraphicsPipeline::new( GraphicsPipeline::new(
device.clone(), device.clone(),
None, None,
GraphicsPipelineCreateInfo { GraphicsPipelineCreateInfo {
@ -74,19 +83,6 @@ 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,4 +1,3 @@
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};
@ -70,34 +69,31 @@ pub struct Scene {
} }
impl Scene { impl Scene {
pub fn load( pub fn initialize(
device: &Arc<Device>, device: &Arc<Device>,
swapchain: &Arc<Swapchain>, swapchain: &Arc<Swapchain>,
memory_allocator: &Arc<StandardMemoryAllocator>, memory_allocator: &Arc<StandardMemoryAllocator>,
) -> Result<Self, Box<dyn Error>> { ) -> Scene {
let pipeline = create_triangle_pipeline(device, swapchain)?; let pipeline = create_triangle_pipeline(device, swapchain);
let vertex_buffer = Vertex2D::create_buffer(Vec::from_iter(VERTICES), memory_allocator)?; let vertex_buffer =
Vertex2D::create_buffer(Vec::from_iter(VERTICES), memory_allocator).unwrap();
Ok(Scene { Scene {
pipeline, pipeline,
vertex_buffer, vertex_buffer,
}) }
} }
pub fn render( pub fn render(&self, builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>) {
&self, builder
builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>, .bind_pipeline_graphics(self.pipeline.clone())
) -> Result<(), Box<dyn Error>> { .unwrap()
.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 { unsafe { builder.draw(vertex_count, instance_count, 0, 0) }.unwrap();
builder
.bind_pipeline_graphics(self.pipeline.clone())?
.bind_vertex_buffers(0, self.vertex_buffer.clone())?
.draw(vertex_count, instance_count, 0, 0)?;
}
Ok(())
} }
} }