diff --git a/src/app_data.rs b/src/app_data.rs index 6393ff9..5825fe5 100644 --- a/src/app_data.rs +++ b/src/app_data.rs @@ -19,7 +19,8 @@ pub struct AppData { pub descriptor_set_layout: vk::DescriptorSetLayout, pub pipeline_layout: vk::PipelineLayout, pub render_pass: vk::RenderPass, - pub pipeline: vk::Pipeline, + pub pipeline_cube: vk::Pipeline, + pub pipeline_cuboid: vk::Pipeline, pub framebuffers: Vec, pub command_pool: vk::CommandPool, pub command_buffers: Vec, diff --git a/src/command_buffer.rs b/src/command_buffer.rs index 01412b1..8d7c005 100644 --- a/src/command_buffer.rs +++ b/src/command_buffer.rs @@ -68,7 +68,7 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa *command_buffer, &info, vk::SubpassContents::INLINE); 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_index_buffer(*command_buffer, scene_handler.index_buffer, 0, vk::IndexType::UINT32); diff --git a/src/main.rs b/src/main.rs index be21161..74ca914 100644 --- a/src/main.rs +++ b/src/main.rs @@ -349,7 +349,8 @@ impl App { .iter() .for_each(|f| self.device.destroy_framebuffer(*f, None)); 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_render_pass(self.data.render_pass, None); 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<()> { - let vert = include_bytes!("../shaders/vert_cube.spv"); - let geo = include_bytes!("../shaders/geo_cube.spv"); - let frag = include_bytes!("../shaders/frag_cube.spv"); + let vert_cube = include_bytes!("../shaders/vert_cube.spv"); + let geo_cube = include_bytes!("../shaders/geo_cube.spv"); + let frag_cube = include_bytes!("../shaders/frag_cube.spv"); - let vert_shader_module = create_shader_module(device, &vert[..])?; - let geo_shader_module = create_shader_module(device, &geo[..])?; - let frag_shader_module = create_shader_module(device, &frag[..])?; + let vert_shader_module_cube = create_shader_module(device, &vert_cube[..])?; + let geo_shader_module_cube = create_shader_module(device, &geo_cube[..])?; + 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) - .module(vert_shader_module) + .module(vert_shader_module_cube) .name(b"main\0"); - let geo_stage = vk::PipelineShaderStageCreateInfo::builder() + let geo_stage_cube = vk::PipelineShaderStageCreateInfo::builder() .stage(vk::ShaderStageFlags::GEOMETRY) - .module(geo_shader_module) + .module(geo_shader_module_cube) .name(b"main\0"); - let frag_stage = vk::PipelineShaderStageCreateInfo::builder() + let frag_stage_cube = vk::PipelineShaderStageCreateInfo::builder() .stage(vk::ShaderStageFlags::FRAGMENT) - .module(frag_shader_module) + .module(frag_shader_module_cube) .name(b"main\0"); - let binding_descriptions = &[vertex::Vertex::binding_description()]; - let attribute_descriptions = vertex::Vertex::attribute_descriptions(); - let vertex_input_state = vk::PipelineVertexInputStateCreateInfo::builder() - .vertex_binding_descriptions(binding_descriptions) - .vertex_attribute_descriptions(&attribute_descriptions); + let binding_descriptions_cube = &[vertex::Vertex::binding_description()]; + let attribute_descriptions_cube = vertex::Vertex::attribute_descriptions(); + let vertex_input_state_cube = vk::PipelineVertexInputStateCreateInfo::builder() + .vertex_binding_descriptions(binding_descriptions_cube) + .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; 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)?; - let stages = &[vert_stage, frag_stage]; - let stages_geom = &[vert_stage, geo_stage,frag_stage]; + let stages_cube = &[vert_stage_cube, frag_stage_cube]; + let stages_geom_cube = &[vert_stage_cube, geo_stage_cube,frag_stage_cube]; - let mut info = vk::GraphicsPipelineCreateInfo::builder() - .vertex_input_state(&vertex_input_state) + let mut info_cube = vk::GraphicsPipelineCreateInfo::builder() + .vertex_input_state(&vertex_input_state_cube) .input_assembly_state(&input_assembly_state) .viewport_state(&viewport_state) .rasterization_state(&rasterization_state) @@ -760,18 +790,46 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu .subpass(0); if data.use_geometry_shader { - info = info.stages(stages_geom); + info_cube = info_cube.stages(stages_geom_cube); } else { - info = info.stages(stages); + info_cube = info_cube.stages(stages_cube); } - data.pipeline = device.create_graphics_pipelines( - vk::PipelineCache::null(), &[info], None)?.0[0]; + let stages_cuboid = &[vert_stage_cuboid, frag_stage_cuboid]; + let stages_geom_cuboid = &[vert_stage_cuboid, geo_stage_cuboid,frag_stage_cuboid]; - device.destroy_shader_module(vert_shader_module, None); - device.destroy_shader_module(geo_shader_module, None); - device.destroy_shader_module(frag_shader_module, None); + let mut info_cuboid = vk::GraphicsPipelineCreateInfo::builder() + .vertex_input_state(&vertex_input_state_cuboid) + .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(()) } diff --git a/src/vertex.rs b/src/vertex.rs index 6be4883..d51ac03 100644 --- a/src/vertex.rs +++ b/src/vertex.rs @@ -100,7 +100,7 @@ impl SizedVertex { .build() } - pub fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 3] { + pub fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 4] { let pos = vk::VertexInputAttributeDescription::builder() .binding(0) .location(0) @@ -121,7 +121,13 @@ impl SizedVertex { .format(vk::Format::R32G32_SFLOAT) .offset((size_of::() + size_of::()) as u32) .build(); - [pos, color, tex_coord] + let size = vk::VertexInputAttributeDescription::builder() + .binding(0) + .location(3) + .format(vk::Format::R32G32B32_SFLOAT) + .offset((size_of::() + size_of::() + size_of::()) as u32) + .build(); + [pos, color, tex_coord, size] } }