added pipeline for cuboid

This commit is contained in:
zomseffen 2024-09-14 19:49:51 +02:00
parent c5bcd148ca
commit 4494b68c26
4 changed files with 98 additions and 33 deletions

View file

@ -19,7 +19,8 @@ pub struct AppData {
pub descriptor_set_layout: vk::DescriptorSetLayout, pub descriptor_set_layout: vk::DescriptorSetLayout,
pub pipeline_layout: vk::PipelineLayout, pub pipeline_layout: vk::PipelineLayout,
pub render_pass: vk::RenderPass, pub render_pass: vk::RenderPass,
pub pipeline: vk::Pipeline, pub pipeline_cube: vk::Pipeline,
pub pipeline_cuboid: vk::Pipeline,
pub framebuffers: Vec<vk::Framebuffer>, pub framebuffers: Vec<vk::Framebuffer>,
pub command_pool: vk::CommandPool, pub command_pool: vk::CommandPool,
pub command_buffers: Vec<vk::CommandBuffer>, pub command_buffers: Vec<vk::CommandBuffer>,

View file

@ -68,7 +68,7 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa
*command_buffer, &info, vk::SubpassContents::INLINE); *command_buffer, &info, vk::SubpassContents::INLINE);
device.cmd_bind_pipeline( device.cmd_bind_pipeline(
*command_buffer, vk::PipelineBindPoint::GRAPHICS, data.pipeline); *command_buffer, vk::PipelineBindPoint::GRAPHICS, data.pipeline_cube);
device.cmd_bind_vertex_buffers(*command_buffer, 0, &[scene_handler.vertex_buffer], &[0]); device.cmd_bind_vertex_buffers(*command_buffer, 0, &[scene_handler.vertex_buffer], &[0]);
device.cmd_bind_index_buffer(*command_buffer, scene_handler.index_buffer, 0, vk::IndexType::UINT32); device.cmd_bind_index_buffer(*command_buffer, scene_handler.index_buffer, 0, vk::IndexType::UINT32);

View file

@ -349,7 +349,8 @@ impl App {
.iter() .iter()
.for_each(|f| self.device.destroy_framebuffer(*f, None)); .for_each(|f| self.device.destroy_framebuffer(*f, None));
self.device.free_command_buffers(self.data.command_pool, &self.data.command_buffers); self.device.free_command_buffers(self.data.command_pool, &self.data.command_buffers);
self.device.destroy_pipeline(self.data.pipeline, None); self.device.destroy_pipeline(self.data.pipeline_cube, None);
self.device.destroy_pipeline(self.data.pipeline_cuboid, None);
self.device.destroy_pipeline_layout(self.data.pipeline_layout, None); self.device.destroy_pipeline_layout(self.data.pipeline_layout, None);
self.device.destroy_render_pass(self.data.render_pass, None); self.device.destroy_render_pass(self.data.render_pass, None);
self.data.swapchain_image_views self.data.swapchain_image_views
@ -642,34 +643,63 @@ unsafe fn create_logical_device(
} }
unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Result<()> { unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Result<()> {
let vert = include_bytes!("../shaders/vert_cube.spv"); let vert_cube = include_bytes!("../shaders/vert_cube.spv");
let geo = include_bytes!("../shaders/geo_cube.spv"); let geo_cube = include_bytes!("../shaders/geo_cube.spv");
let frag = include_bytes!("../shaders/frag_cube.spv"); let frag_cube = include_bytes!("../shaders/frag_cube.spv");
let vert_shader_module = create_shader_module(device, &vert[..])?; let vert_shader_module_cube = create_shader_module(device, &vert_cube[..])?;
let geo_shader_module = create_shader_module(device, &geo[..])?; let geo_shader_module_cube = create_shader_module(device, &geo_cube[..])?;
let frag_shader_module = create_shader_module(device, &frag[..])?; let frag_shader_module_cube = create_shader_module(device, &frag_cube[..])?;
let vert_stage = vk::PipelineShaderStageCreateInfo::builder() let vert_stage_cube = vk::PipelineShaderStageCreateInfo::builder()
.stage(vk::ShaderStageFlags::VERTEX) .stage(vk::ShaderStageFlags::VERTEX)
.module(vert_shader_module) .module(vert_shader_module_cube)
.name(b"main\0"); .name(b"main\0");
let geo_stage = vk::PipelineShaderStageCreateInfo::builder() let geo_stage_cube = vk::PipelineShaderStageCreateInfo::builder()
.stage(vk::ShaderStageFlags::GEOMETRY) .stage(vk::ShaderStageFlags::GEOMETRY)
.module(geo_shader_module) .module(geo_shader_module_cube)
.name(b"main\0"); .name(b"main\0");
let frag_stage = vk::PipelineShaderStageCreateInfo::builder() let frag_stage_cube = vk::PipelineShaderStageCreateInfo::builder()
.stage(vk::ShaderStageFlags::FRAGMENT) .stage(vk::ShaderStageFlags::FRAGMENT)
.module(frag_shader_module) .module(frag_shader_module_cube)
.name(b"main\0"); .name(b"main\0");
let binding_descriptions = &[vertex::Vertex::binding_description()]; let binding_descriptions_cube = &[vertex::Vertex::binding_description()];
let attribute_descriptions = vertex::Vertex::attribute_descriptions(); let attribute_descriptions_cube = vertex::Vertex::attribute_descriptions();
let vertex_input_state = vk::PipelineVertexInputStateCreateInfo::builder() let vertex_input_state_cube = vk::PipelineVertexInputStateCreateInfo::builder()
.vertex_binding_descriptions(binding_descriptions) .vertex_binding_descriptions(binding_descriptions_cube)
.vertex_attribute_descriptions(&attribute_descriptions); .vertex_attribute_descriptions(&attribute_descriptions_cube);
let vert_cuboid = include_bytes!("../shaders/vert_cuboid.spv");
let geo_cuboid = include_bytes!("../shaders/geo_cuboid.spv");
let frag_cuboid = include_bytes!("../shaders/frag_cuboid.spv");
let vert_shader_module_cuboid = create_shader_module(device, &vert_cuboid[..])?;
let geo_shader_module_cuboid = create_shader_module(device, &geo_cuboid[..])?;
let frag_shader_module_cuboid = create_shader_module(device, &frag_cuboid[..])?;
let vert_stage_cuboid = vk::PipelineShaderStageCreateInfo::builder()
.stage(vk::ShaderStageFlags::VERTEX)
.module(vert_shader_module_cuboid)
.name(b"main\0");
let geo_stage_cuboid = vk::PipelineShaderStageCreateInfo::builder()
.stage(vk::ShaderStageFlags::GEOMETRY)
.module(geo_shader_module_cuboid)
.name(b"main\0");
let frag_stage_cuboid = vk::PipelineShaderStageCreateInfo::builder()
.stage(vk::ShaderStageFlags::FRAGMENT)
.module(frag_shader_module_cuboid)
.name(b"main\0");
let binding_descriptions_cuboid = &[vertex::SizedVertex::binding_description()];
let attribute_descriptions_cuboid = vertex::SizedVertex::attribute_descriptions();
let vertex_input_state_cuboid = vk::PipelineVertexInputStateCreateInfo::builder()
.vertex_binding_descriptions(binding_descriptions_cuboid)
.vertex_attribute_descriptions(&attribute_descriptions_cuboid);
let mut topology = vk::PrimitiveTopology::TRIANGLE_LIST; let mut topology = vk::PrimitiveTopology::TRIANGLE_LIST;
if data.use_geometry_shader { if data.use_geometry_shader {
@ -744,11 +774,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu
data.pipeline_layout = device.create_pipeline_layout(&layout_info, None)?; data.pipeline_layout = device.create_pipeline_layout(&layout_info, None)?;
let stages = &[vert_stage, frag_stage]; let stages_cube = &[vert_stage_cube, frag_stage_cube];
let stages_geom = &[vert_stage, geo_stage,frag_stage]; let stages_geom_cube = &[vert_stage_cube, geo_stage_cube,frag_stage_cube];
let mut info = vk::GraphicsPipelineCreateInfo::builder() let mut info_cube = vk::GraphicsPipelineCreateInfo::builder()
.vertex_input_state(&vertex_input_state) .vertex_input_state(&vertex_input_state_cube)
.input_assembly_state(&input_assembly_state) .input_assembly_state(&input_assembly_state)
.viewport_state(&viewport_state) .viewport_state(&viewport_state)
.rasterization_state(&rasterization_state) .rasterization_state(&rasterization_state)
@ -760,18 +790,46 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu
.subpass(0); .subpass(0);
if data.use_geometry_shader { if data.use_geometry_shader {
info = info.stages(stages_geom); info_cube = info_cube.stages(stages_geom_cube);
} }
else { else {
info = info.stages(stages); info_cube = info_cube.stages(stages_cube);
} }
data.pipeline = device.create_graphics_pipelines( let stages_cuboid = &[vert_stage_cuboid, frag_stage_cuboid];
vk::PipelineCache::null(), &[info], None)?.0[0]; let stages_geom_cuboid = &[vert_stage_cuboid, geo_stage_cuboid,frag_stage_cuboid];
device.destroy_shader_module(vert_shader_module, None); let mut info_cuboid = vk::GraphicsPipelineCreateInfo::builder()
device.destroy_shader_module(geo_shader_module, None); .vertex_input_state(&vertex_input_state_cuboid)
device.destroy_shader_module(frag_shader_module, None); .input_assembly_state(&input_assembly_state)
.viewport_state(&viewport_state)
.rasterization_state(&rasterization_state)
.multisample_state(&multisample_state)
.depth_stencil_state(&depth_stencil_state)
.color_blend_state(&color_blend_state)
.layout(data.pipeline_layout)
.render_pass(data.render_pass)
.subpass(0);
if data.use_geometry_shader {
info_cuboid = info_cuboid.stages(stages_geom_cuboid);
}
else {
info_cuboid = info_cuboid.stages(stages_cuboid);
}
let pipelines = device.create_graphics_pipelines(vk::PipelineCache::null(), &[info_cube, info_cuboid], None)?.0;
data.pipeline_cube = pipelines[0];
data.pipeline_cuboid = pipelines[1];
device.destroy_shader_module(vert_shader_module_cube, None);
device.destroy_shader_module(geo_shader_module_cube, None);
device.destroy_shader_module(frag_shader_module_cube, None);
device.destroy_shader_module(vert_shader_module_cuboid, None);
device.destroy_shader_module(geo_shader_module_cuboid, None);
device.destroy_shader_module(frag_shader_module_cuboid, None);
Ok(()) Ok(())
} }

View file

@ -100,7 +100,7 @@ impl SizedVertex {
.build() .build()
} }
pub fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 3] { pub fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 4] {
let pos = vk::VertexInputAttributeDescription::builder() let pos = vk::VertexInputAttributeDescription::builder()
.binding(0) .binding(0)
.location(0) .location(0)
@ -121,7 +121,13 @@ impl SizedVertex {
.format(vk::Format::R32G32_SFLOAT) .format(vk::Format::R32G32_SFLOAT)
.offset((size_of::<Vec3>() + size_of::<Vec3>()) as u32) .offset((size_of::<Vec3>() + size_of::<Vec3>()) as u32)
.build(); .build();
[pos, color, tex_coord] let size = vk::VertexInputAttributeDescription::builder()
.binding(0)
.location(3)
.format(vk::Format::R32G32B32_SFLOAT)
.offset((size_of::<Vec3>() + size_of::<Vec3>() + size_of::<Vec2>()) as u32)
.build();
[pos, color, tex_coord, size]
} }
} }