refactored triangle to use builders and defaults
This commit is contained in:
parent
cc21817445
commit
0a16b5bd9b
|
@ -35,41 +35,44 @@ fn main() {
|
||||||
format: vk::Format::D16_UNORM,
|
format: vk::Format::D16_UNORM,
|
||||||
samples: vk::SampleCountFlags::TYPE_1,
|
samples: vk::SampleCountFlags::TYPE_1,
|
||||||
load_op: vk::AttachmentLoadOp::CLEAR,
|
load_op: vk::AttachmentLoadOp::CLEAR,
|
||||||
|
initial_layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
||||||
final_layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
final_layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
||||||
.. Default::default()
|
.. Default::default()
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
let color_attachment_ref = vk::AttachmentReference {
|
let color_attachment_refs = [vk::AttachmentReference {
|
||||||
attachment: 0,
|
attachment: 0,
|
||||||
layout: vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL,
|
layout: vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL,
|
||||||
};
|
}];
|
||||||
let depth_attachment_ref = vk::AttachmentReference {
|
let depth_attachment_ref = vk::AttachmentReference {
|
||||||
attachment: 1,
|
attachment: 1,
|
||||||
layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
||||||
};
|
};
|
||||||
let dependency = vk::SubpassDependency {
|
let dependencies = [vk::SubpassDependency {
|
||||||
src_subpass: vk::SUBPASS_EXTERNAL,
|
src_subpass: vk::SUBPASS_EXTERNAL,
|
||||||
src_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT,
|
src_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT,
|
||||||
dst_access_mask: vk::AccessFlags::COLOR_ATTACHMENT_READ
|
dst_access_mask: vk::AccessFlags::COLOR_ATTACHMENT_READ
|
||||||
| vk::AccessFlags::COLOR_ATTACHMENT_WRITE,
|
| vk::AccessFlags::COLOR_ATTACHMENT_WRITE,
|
||||||
dst_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT,
|
dst_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT,
|
||||||
.. Default::default()
|
.. Default::default()
|
||||||
};
|
}];
|
||||||
let subpass = vk::SubpassDescription::builder()
|
|
||||||
.color_attachments(&[color_attachment_ref])
|
let subpasses = [vk::SubpassDescription::builder()
|
||||||
|
.color_attachments(&color_attachment_refs)
|
||||||
.depth_stencil_attachment( &depth_attachment_ref)
|
.depth_stencil_attachment( &depth_attachment_ref)
|
||||||
.pipeline_bind_point( vk::PipelineBindPoint::GRAPHICS)
|
.pipeline_bind_point( vk::PipelineBindPoint::GRAPHICS)
|
||||||
.build();
|
.build()];
|
||||||
|
|
||||||
let renderpass_create_info = vk::RenderPassCreateInfo::builder()
|
let renderpass_create_info = vk::RenderPassCreateInfo::builder()
|
||||||
.attachments( &renderpass_attachments)
|
.attachments( &renderpass_attachments)
|
||||||
.subpasses(&[subpass])
|
.subpasses( &subpasses)
|
||||||
.dependencies(&[dependency])
|
.dependencies( &dependencies);
|
||||||
.build();
|
|
||||||
let renderpass = base
|
let renderpass = base
|
||||||
.device
|
.device
|
||||||
.create_render_pass(&renderpass_create_info, None)
|
.create_render_pass(&renderpass_create_info, None)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let framebuffers: Vec<vk::Framebuffer> = base
|
let framebuffers: Vec<vk::Framebuffer> = base
|
||||||
.present_image_views
|
.present_image_views
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -80,19 +83,19 @@ fn main() {
|
||||||
.attachments( &framebuffer_attachments)
|
.attachments( &framebuffer_attachments)
|
||||||
.width( base.surface_resolution.width)
|
.width( base.surface_resolution.width)
|
||||||
.height( base.surface_resolution.height)
|
.height( base.surface_resolution.height)
|
||||||
.layers(1)
|
.layers(1);
|
||||||
.build();
|
|
||||||
base.device
|
base.device
|
||||||
.create_framebuffer(&frame_buffer_create_info, None)
|
.create_framebuffer(&frame_buffer_create_info, None)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
let index_buffer_data = [0u32, 1, 2];
|
let index_buffer_data = [0u32, 1, 2];
|
||||||
let index_buffer_info = vk::BufferCreateInfo {
|
let index_buffer_info = vk::BufferCreateInfo::builder()
|
||||||
size: std::mem::size_of_val(&index_buffer_data) as u64,
|
.size( std::mem::size_of_val(&index_buffer_data) as u64)
|
||||||
usage: vk::BufferUsageFlags::INDEX_BUFFER,
|
.usage( vk::BufferUsageFlags::INDEX_BUFFER)
|
||||||
sharing_mode: vk::SharingMode::EXCLUSIVE,
|
.sharing_mode(vk::SharingMode::EXCLUSIVE);
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let index_buffer = base.device.create_buffer(&index_buffer_info, None).unwrap();
|
let index_buffer = base.device.create_buffer(&index_buffer_info, None).unwrap();
|
||||||
let index_buffer_memory_req = base.device.get_buffer_memory_requirements(index_buffer);
|
let index_buffer_memory_req = base.device.get_buffer_memory_requirements(index_buffer);
|
||||||
let index_buffer_memory_index = find_memorytype_index(
|
let index_buffer_memory_index = find_memorytype_index(
|
||||||
|
@ -100,6 +103,7 @@ fn main() {
|
||||||
&base.device_memory_properties,
|
&base.device_memory_properties,
|
||||||
vk::MemoryPropertyFlags::HOST_VISIBLE,
|
vk::MemoryPropertyFlags::HOST_VISIBLE,
|
||||||
).expect("Unable to find suitable memorytype for the index buffer.");
|
).expect("Unable to find suitable memorytype for the index buffer.");
|
||||||
|
|
||||||
let index_allocate_info = vk::MemoryAllocateInfo {
|
let index_allocate_info = vk::MemoryAllocateInfo {
|
||||||
allocation_size: index_buffer_memory_req.size,
|
allocation_size: index_buffer_memory_req.size,
|
||||||
memory_type_index: index_buffer_memory_index,
|
memory_type_index: index_buffer_memory_index,
|
||||||
|
@ -134,13 +138,16 @@ fn main() {
|
||||||
sharing_mode: vk::SharingMode::EXCLUSIVE,
|
sharing_mode: vk::SharingMode::EXCLUSIVE,
|
||||||
.. Default::default()
|
.. Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let vertex_input_buffer = base
|
let vertex_input_buffer = base
|
||||||
.device
|
.device
|
||||||
.create_buffer(&vertex_input_buffer_info, None)
|
.create_buffer(&vertex_input_buffer_info, None)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let vertex_input_buffer_memory_req = base
|
let vertex_input_buffer_memory_req = base
|
||||||
.device
|
.device
|
||||||
.get_buffer_memory_requirements(vertex_input_buffer);
|
.get_buffer_memory_requirements(vertex_input_buffer);
|
||||||
|
|
||||||
let vertex_input_buffer_memory_index = find_memorytype_index(
|
let vertex_input_buffer_memory_index = find_memorytype_index(
|
||||||
&vertex_input_buffer_memory_req,
|
&vertex_input_buffer_memory_req,
|
||||||
&base.device_memory_properties,
|
&base.device_memory_properties,
|
||||||
|
@ -152,10 +159,12 @@ fn main() {
|
||||||
memory_type_index: vertex_input_buffer_memory_index,
|
memory_type_index: vertex_input_buffer_memory_index,
|
||||||
.. Default::default()
|
.. Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let vertex_input_buffer_memory = base
|
let vertex_input_buffer_memory = base
|
||||||
.device
|
.device
|
||||||
.allocate_memory(&vertex_buffer_allocate_info, None)
|
.allocate_memory(&vertex_buffer_allocate_info, None)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let vertices = [
|
let vertices = [
|
||||||
Vertex {
|
Vertex {
|
||||||
pos: [-1.0, 1.0, 0.0, 1.0],
|
pos: [-1.0, 1.0, 0.0, 1.0],
|
||||||
|
@ -170,6 +179,7 @@ fn main() {
|
||||||
color: [1.0, 0.0, 0.0, 1.0],
|
color: [1.0, 0.0, 0.0, 1.0],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
let vert_ptr = base
|
let vert_ptr = base
|
||||||
.device
|
.device
|
||||||
.map_memory(
|
.map_memory(
|
||||||
|
@ -178,6 +188,7 @@ fn main() {
|
||||||
vertex_input_buffer_memory_req.size,
|
vertex_input_buffer_memory_req.size,
|
||||||
vk::MemoryMapFlags::empty(),
|
vk::MemoryMapFlags::empty(),
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
let mut vert_align = Align::new(
|
let mut vert_align = Align::new(
|
||||||
vert_ptr,
|
vert_ptr,
|
||||||
align_of::<Vertex>() as u64,
|
align_of::<Vertex>() as u64,
|
||||||
|
@ -231,6 +242,7 @@ fn main() {
|
||||||
.. Default::default()
|
.. Default::default()
|
||||||
},
|
},
|
||||||
vk::PipelineShaderStageCreateInfo {
|
vk::PipelineShaderStageCreateInfo {
|
||||||
|
s_type: vk::StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO,
|
||||||
module: fragment_shader_module,
|
module: fragment_shader_module,
|
||||||
p_name: shader_entry_name.as_ptr(),
|
p_name: shader_entry_name.as_ptr(),
|
||||||
stage: vk::ShaderStageFlags::FRAGMENT,
|
stage: vk::ShaderStageFlags::FRAGMENT,
|
||||||
|
@ -256,14 +268,18 @@ fn main() {
|
||||||
offset: offset_of!(Vertex, color) as u32,
|
offset: offset_of!(Vertex, color) as u32,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
let vertex_input_state_info = vk::PipelineVertexInputStateCreateInfo::builder()
|
|
||||||
.vertex_attribute_descriptions(&vertex_input_attribute_descriptions)
|
|
||||||
.vertex_binding_descriptions(&vertex_input_binding_descriptions)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
|
let vertex_input_state_info = vk::PipelineVertexInputStateCreateInfo {
|
||||||
|
vertex_attribute_description_count: vertex_input_attribute_descriptions.len() as u32,
|
||||||
|
p_vertex_attribute_descriptions: vertex_input_attribute_descriptions.as_ptr(),
|
||||||
|
vertex_binding_description_count: vertex_input_binding_descriptions.len() as u32,
|
||||||
|
p_vertex_binding_descriptions: vertex_input_binding_descriptions.as_ptr(),
|
||||||
|
.. Default::default()
|
||||||
|
};
|
||||||
let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo {
|
let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo {
|
||||||
topology: vk::PrimitiveTopology::TRIANGLE_LIST,
|
topology: vk::PrimitiveTopology::TRIANGLE_LIST,
|
||||||
.. Default::default()
|
.. Default::default()
|
||||||
|
|
||||||
};
|
};
|
||||||
let viewports = [vk::Viewport {
|
let viewports = [vk::Viewport {
|
||||||
x: 0.0,
|
x: 0.0,
|
||||||
|
@ -279,8 +295,8 @@ fn main() {
|
||||||
}];
|
}];
|
||||||
let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder()
|
let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder()
|
||||||
.scissors(&scissors)
|
.scissors(&scissors)
|
||||||
.viewports(&viewports)
|
.viewports(&viewports);
|
||||||
.build();
|
|
||||||
let rasterization_info = vk::PipelineRasterizationStateCreateInfo {
|
let rasterization_info = vk::PipelineRasterizationStateCreateInfo {
|
||||||
front_face: vk::FrontFace::COUNTER_CLOCKWISE,
|
front_face: vk::FrontFace::COUNTER_CLOCKWISE,
|
||||||
line_width: 1.0,
|
line_width: 1.0,
|
||||||
|
@ -319,13 +335,12 @@ fn main() {
|
||||||
}];
|
}];
|
||||||
let color_blend_state = vk::PipelineColorBlendStateCreateInfo::builder()
|
let color_blend_state = vk::PipelineColorBlendStateCreateInfo::builder()
|
||||||
.logic_op( vk::LogicOp::CLEAR)
|
.logic_op( vk::LogicOp::CLEAR)
|
||||||
.attachments(&color_blend_attachment_states)
|
.attachments( &color_blend_attachment_states);
|
||||||
.build();
|
|
||||||
|
|
||||||
let dynamic_state = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR];
|
let dynamic_state = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR];
|
||||||
let dynamic_state_info = vk::PipelineDynamicStateCreateInfo::builder()
|
let dynamic_state_info = vk::PipelineDynamicStateCreateInfo::builder()
|
||||||
.dynamic_states(&dynamic_state)
|
.dynamic_states(&dynamic_state);
|
||||||
.build();
|
|
||||||
|
|
||||||
let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder()
|
let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder()
|
||||||
.stages(&shader_stage_create_infos)
|
.stages(&shader_stage_create_infos)
|
||||||
|
@ -340,6 +355,8 @@ fn main() {
|
||||||
.layout( pipeline_layout)
|
.layout( pipeline_layout)
|
||||||
.render_pass(renderpass)
|
.render_pass(renderpass)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|
||||||
let graphics_pipelines = base
|
let graphics_pipelines = base
|
||||||
.device
|
.device
|
||||||
.create_graphics_pipelines(vk::PipelineCache::null(), &[graphic_pipeline_info], None)
|
.create_graphics_pipelines(vk::PipelineCache::null(), &[graphic_pipeline_info], None)
|
||||||
|
@ -370,18 +387,15 @@ fn main() {
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
let render_pass_begin_info = vk::RenderPassBeginInfo {
|
let render_pass_begin_info = vk::RenderPassBeginInfo::builder()
|
||||||
s_type: vk::StructureType::RENDER_PASS_BEGIN_INFO,
|
.render_pass(renderpass)
|
||||||
p_next: ptr::null(),
|
.framebuffer( framebuffers[present_index as usize])
|
||||||
render_pass: renderpass,
|
.render_area( vk::Rect2D {
|
||||||
framebuffer: framebuffers[present_index as usize],
|
|
||||||
render_area: vk::Rect2D {
|
|
||||||
offset: vk::Offset2D { x: 0, y: 0 },
|
offset: vk::Offset2D { x: 0, y: 0 },
|
||||||
extent: base.surface_resolution.clone(),
|
extent: base.surface_resolution.clone(),
|
||||||
},
|
})
|
||||||
clear_value_count: clear_values.len() as u32,
|
.clear_values( &clear_values);
|
||||||
p_clear_values: clear_values.as_ptr(),
|
|
||||||
};
|
|
||||||
record_submit_commandbuffer(
|
record_submit_commandbuffer(
|
||||||
&base.device,
|
&base.device,
|
||||||
base.draw_command_buffer,
|
base.draw_command_buffer,
|
||||||
|
@ -428,11 +442,14 @@ fn main() {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
//let mut present_info_err = mem::uninitialized();
|
//let mut present_info_err = mem::uninitialized();
|
||||||
|
let wait_semaphors = [base.rendering_complete_semaphore];
|
||||||
|
let swapchains = [base.swapchain];
|
||||||
|
let image_indices = [present_index];
|
||||||
let present_info = vk::PresentInfoKHR::builder()
|
let present_info = vk::PresentInfoKHR::builder()
|
||||||
.wait_semaphores(&[base.rendering_complete_semaphore])
|
.wait_semaphores( &wait_semaphors) // &base.rendering_complete_semaphore)
|
||||||
.swapchains(&[base.swapchain])
|
.swapchains( &swapchains)
|
||||||
.image_indices(&[present_index])
|
.image_indices( &image_indices);
|
||||||
.build();
|
|
||||||
base.swapchain_loader
|
base.swapchain_loader
|
||||||
.queue_present(base.present_queue, &present_info)
|
.queue_present(base.present_queue, &present_info)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
Loading…
Reference in a new issue