using builders and defaults in triangle example

This commit is contained in:
colin 2018-12-06 09:33:22 -06:00
parent bac3a5bc21
commit dbbdc8c3c2

View file

@ -25,25 +25,18 @@ fn main() {
let renderpass_attachments = [ let renderpass_attachments = [
vk::AttachmentDescription { vk::AttachmentDescription {
format: base.surface_format.format, format: base.surface_format.format,
flags: vk::AttachmentDescriptionFlags::empty(),
samples: vk::SampleCountFlags::TYPE_1, samples: vk::SampleCountFlags::TYPE_1,
load_op: vk::AttachmentLoadOp::CLEAR, load_op: vk::AttachmentLoadOp::CLEAR,
store_op: vk::AttachmentStoreOp::STORE, store_op: vk::AttachmentStoreOp::STORE,
stencil_load_op: vk::AttachmentLoadOp::DONT_CARE,
stencil_store_op: vk::AttachmentStoreOp::DONT_CARE,
initial_layout: vk::ImageLayout::UNDEFINED,
final_layout: vk::ImageLayout::PRESENT_SRC_KHR, final_layout: vk::ImageLayout::PRESENT_SRC_KHR,
.. Default::default()
}, },
vk::AttachmentDescription { vk::AttachmentDescription {
format: vk::Format::D16_UNORM, format: vk::Format::D16_UNORM,
flags: vk::AttachmentDescriptionFlags::empty(),
samples: vk::SampleCountFlags::TYPE_1, samples: vk::SampleCountFlags::TYPE_1,
load_op: vk::AttachmentLoadOp::CLEAR, load_op: vk::AttachmentLoadOp::CLEAR,
store_op: vk::AttachmentStoreOp::DONT_CARE,
stencil_load_op: vk::AttachmentLoadOp::DONT_CARE,
stencil_store_op: vk::AttachmentStoreOp::DONT_CARE,
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()
}, },
]; ];
let color_attachment_ref = vk::AttachmentReference { let color_attachment_ref = vk::AttachmentReference {
@ -55,38 +48,24 @@ fn main() {
layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL, layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
}; };
let dependency = vk::SubpassDependency { let dependency = vk::SubpassDependency {
dependency_flags: Default::default(),
src_subpass: vk::SUBPASS_EXTERNAL, src_subpass: vk::SUBPASS_EXTERNAL,
dst_subpass: Default::default(),
src_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, src_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT,
src_access_mask: Default::default(),
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()
}; };
let subpass = vk::SubpassDescription { let subpass = vk::SubpassDescription::builder()
color_attachment_count: 1, .color_attachments( &[color_attachment_ref])
p_color_attachments: &color_attachment_ref, .depth_stencil_attachment( &depth_attachment_ref)
p_depth_stencil_attachment: &depth_attachment_ref, .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS)
flags: Default::default(), .build();
pipeline_bind_point: vk::PipelineBindPoint::GRAPHICS,
input_attachment_count: 0, let renderpass_create_info = vk::RenderPassCreateInfo::builder()
p_input_attachments: ptr::null(), .attachments(&renderpass_attachments)
p_resolve_attachments: ptr::null(), .subpasses(&[subpass])
preserve_attachment_count: 0, .dependencies(&[dependency])
p_preserve_attachments: ptr::null(), .build();
};
let renderpass_create_info = vk::RenderPassCreateInfo {
s_type: vk::StructureType::RENDER_PASS_CREATE_INFO,
flags: Default::default(),
p_next: ptr::null(),
attachment_count: renderpass_attachments.len() as u32,
p_attachments: renderpass_attachments.as_ptr(),
subpass_count: 1,
p_subpasses: &subpass,
dependency_count: 1,
p_dependencies: &dependency,
};
let renderpass = base let renderpass = base
.device .device
.create_render_pass(&renderpass_create_info, None) .create_render_pass(&renderpass_create_info, None)
@ -96,31 +75,23 @@ fn main() {
.iter() .iter()
.map(|&present_image_view| { .map(|&present_image_view| {
let framebuffer_attachments = [present_image_view, base.depth_image_view]; let framebuffer_attachments = [present_image_view, base.depth_image_view];
let frame_buffer_create_info = vk::FramebufferCreateInfo { let frame_buffer_create_info = vk::FramebufferCreateInfo::builder()
s_type: vk::StructureType::FRAMEBUFFER_CREATE_INFO, .render_pass(renderpass)
p_next: ptr::null(), .attachments(&framebuffer_attachments)
flags: Default::default(), .width( base.surface_resolution.width)
render_pass: renderpass, .height( base.surface_resolution.height)
attachment_count: framebuffer_attachments.len() as u32, .layers( 1)
p_attachments: framebuffer_attachments.as_ptr(), .build();
width: base.surface_resolution.width,
height: base.surface_resolution.height,
layers: 1,
};
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 {
s_type: vk::StructureType::BUFFER_CREATE_INFO,
p_next: ptr::null(),
flags: vk::BufferCreateFlags::empty(),
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,
queue_family_index_count: 0, .. Default::default()
p_queue_family_indices: ptr::null(),
}; };
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);
@ -130,10 +101,9 @@ fn main() {
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 {
s_type: vk::StructureType::MEMORY_ALLOCATE_INFO,
p_next: ptr::null(),
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,
.. Default::default()
}; };
let index_buffer_memory = base let index_buffer_memory = base
.device .device
@ -159,14 +129,10 @@ fn main() {
.unwrap(); .unwrap();
let vertex_input_buffer_info = vk::BufferCreateInfo { let vertex_input_buffer_info = vk::BufferCreateInfo {
s_type: vk::StructureType::BUFFER_CREATE_INFO,
p_next: ptr::null(),
flags: vk::BufferCreateFlags::empty(),
size: 3 * std::mem::size_of::<Vertex>() as u64, size: 3 * std::mem::size_of::<Vertex>() as u64,
usage: vk::BufferUsageFlags::VERTEX_BUFFER, usage: vk::BufferUsageFlags::VERTEX_BUFFER,
sharing_mode: vk::SharingMode::EXCLUSIVE, sharing_mode: vk::SharingMode::EXCLUSIVE,
queue_family_index_count: 0, .. Default::default()
p_queue_family_indices: ptr::null(),
}; };
let vertex_input_buffer = base let vertex_input_buffer = base
.device .device
@ -182,10 +148,9 @@ fn main() {
).expect("Unable to find suitable memorytype for the vertex buffer."); ).expect("Unable to find suitable memorytype for the vertex buffer.");
let vertex_buffer_allocate_info = vk::MemoryAllocateInfo { let vertex_buffer_allocate_info = vk::MemoryAllocateInfo {
s_type: vk::StructureType::MEMORY_ALLOCATE_INFO,
p_next: ptr::null(),
allocation_size: vertex_input_buffer_memory_req.size, allocation_size: vertex_input_buffer_memory_req.size,
memory_type_index: vertex_input_buffer_memory_index, memory_type_index: vertex_input_buffer_memory_index,
.. Default::default()
}; };
let vertex_input_buffer_memory = base let vertex_input_buffer_memory = base
.device .device
@ -232,21 +197,18 @@ fn main() {
.bytes() .bytes()
.filter_map(|byte| byte.ok()) .filter_map(|byte| byte.ok())
.collect(); .collect();
let vertex_shader_info = vk::ShaderModuleCreateInfo { let vertex_bytes = examples::bytes_to_u32_vec(vertex_bytes.iter().cloned());
s_type: vk::StructureType::SHADER_MODULE_CREATE_INFO,
p_next: ptr::null(), let vertex_shader_info = vk::ShaderModuleCreateInfo::builder()
flags: Default::default(), .code(&vertex_bytes)
code_size: vertex_bytes.len(), .build();
p_code: vertex_bytes.as_ptr() as *const u32,
};
let frag_bytes: Vec<u8> = frag_spv_file.bytes().filter_map(|byte| byte.ok()).collect(); let frag_bytes: Vec<u8> = frag_spv_file.bytes().filter_map(|byte| byte.ok()).collect();
let frag_shader_info = vk::ShaderModuleCreateInfo { let frag_bytes = examples::bytes_to_u32_vec(frag_bytes.iter().cloned());
s_type: vk::StructureType::SHADER_MODULE_CREATE_INFO,
p_next: ptr::null(), let frag_shader_info = vk::ShaderModuleCreateInfo::builder()
flags: Default::default(), .code(&frag_bytes)
code_size: frag_bytes.len(), .build();
p_code: frag_bytes.as_ptr() as *const u32,
};
let vertex_shader_module = base let vertex_shader_module = base
.device .device
.create_shader_module(&vertex_shader_info, None) .create_shader_module(&vertex_shader_info, None)
@ -257,15 +219,7 @@ fn main() {
.create_shader_module(&frag_shader_info, None) .create_shader_module(&frag_shader_info, None)
.expect("Fragment shader module error"); .expect("Fragment shader module error");
let layout_create_info = vk::PipelineLayoutCreateInfo { let layout_create_info = vk::PipelineLayoutCreateInfo::default();
s_type: vk::StructureType::PIPELINE_LAYOUT_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
set_layout_count: 0,
p_set_layouts: ptr::null(),
push_constant_range_count: 0,
p_push_constant_ranges: ptr::null(),
};
let pipeline_layout = base let pipeline_layout = base
.device .device
@ -275,22 +229,16 @@ fn main() {
let shader_entry_name = CString::new("main").unwrap(); let shader_entry_name = CString::new("main").unwrap();
let shader_stage_create_infos = [ let shader_stage_create_infos = [
vk::PipelineShaderStageCreateInfo { vk::PipelineShaderStageCreateInfo {
s_type: vk::StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
module: vertex_shader_module, module: vertex_shader_module,
p_name: shader_entry_name.as_ptr(), p_name: shader_entry_name.as_ptr(),
p_specialization_info: ptr::null(),
stage: vk::ShaderStageFlags::VERTEX, stage: vk::ShaderStageFlags::VERTEX,
.. Default::default()
}, },
vk::PipelineShaderStageCreateInfo { vk::PipelineShaderStageCreateInfo {
s_type: vk::StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
module: fragment_shader_module, module: fragment_shader_module,
p_name: shader_entry_name.as_ptr(), p_name: shader_entry_name.as_ptr(),
p_specialization_info: ptr::null(),
stage: vk::ShaderStageFlags::FRAGMENT, stage: vk::ShaderStageFlags::FRAGMENT,
.. Default::default()
}, },
]; ];
let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription { let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription {
@ -312,21 +260,14 @@ fn main() {
offset: offset_of!(Vertex, color) as u32, offset: offset_of!(Vertex, color) as u32,
}, },
]; ];
let vertex_input_state_info = vk::PipelineVertexInputStateCreateInfo { let vertex_input_state_info = vk::PipelineVertexInputStateCreateInfo::builder()
s_type: vk::StructureType::PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, .vertex_attribute_descriptions(&vertex_input_attribute_descriptions)
p_next: ptr::null(), .vertex_binding_descriptions(&vertex_input_binding_descriptions)
flags: Default::default(), .build();
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(),
};
let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo { let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo {
s_type: vk::StructureType::PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
flags: Default::default(),
p_next: ptr::null(),
primitive_restart_enable: 0,
topology: vk::PrimitiveTopology::TRIANGLE_LIST, topology: vk::PrimitiveTopology::TRIANGLE_LIST,
.. Default::default()
}; };
let viewports = [vk::Viewport { let viewports = [vk::Viewport {
x: 0.0, x: 0.0,
@ -340,63 +281,35 @@ fn main() {
offset: vk::Offset2D { x: 0, y: 0 }, offset: vk::Offset2D { x: 0, y: 0 },
extent: base.surface_resolution.clone(), extent: base.surface_resolution.clone(),
}]; }];
let viewport_state_info = vk::PipelineViewportStateCreateInfo { let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder()
s_type: vk::StructureType::PIPELINE_VIEWPORT_STATE_CREATE_INFO, .scissors(&scissors)
p_next: ptr::null(), .viewports(&viewports)
flags: Default::default(), .build();
scissor_count: scissors.len() as u32,
p_scissors: scissors.as_ptr(),
viewport_count: viewports.len() as u32,
p_viewports: viewports.as_ptr(),
};
let rasterization_info = vk::PipelineRasterizationStateCreateInfo { let rasterization_info = vk::PipelineRasterizationStateCreateInfo {
s_type: vk::StructureType::PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
cull_mode: vk::CullModeFlags::NONE,
depth_bias_clamp: 0.0,
depth_bias_constant_factor: 0.0,
depth_bias_enable: 0,
depth_bias_slope_factor: 0.0,
depth_clamp_enable: 0,
front_face: vk::FrontFace::COUNTER_CLOCKWISE, front_face: vk::FrontFace::COUNTER_CLOCKWISE,
line_width: 1.0, line_width: 1.0,
polygon_mode: vk::PolygonMode::FILL, polygon_mode: vk::PolygonMode::FILL,
rasterizer_discard_enable: 0, .. Default::default()
}; };
let multisample_state_info = vk::PipelineMultisampleStateCreateInfo { let multisample_state_info = vk::PipelineMultisampleStateCreateInfo {
s_type: vk::StructureType::PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
flags: Default::default(),
p_next: ptr::null(),
rasterization_samples: vk::SampleCountFlags::TYPE_1, rasterization_samples: vk::SampleCountFlags::TYPE_1,
sample_shading_enable: 0, .. Default::default()
min_sample_shading: 0.0,
p_sample_mask: ptr::null(),
alpha_to_one_enable: 0,
alpha_to_coverage_enable: 0,
}; };
let noop_stencil_state = vk::StencilOpState { let noop_stencil_state = vk::StencilOpState {
fail_op: vk::StencilOp::KEEP, fail_op: vk::StencilOp::KEEP,
pass_op: vk::StencilOp::KEEP, pass_op: vk::StencilOp::KEEP,
depth_fail_op: vk::StencilOp::KEEP, depth_fail_op: vk::StencilOp::KEEP,
compare_op: vk::CompareOp::ALWAYS, compare_op: vk::CompareOp::ALWAYS,
compare_mask: 0, .. Default::default()
write_mask: 0,
reference: 0,
}; };
let depth_state_info = vk::PipelineDepthStencilStateCreateInfo { let depth_state_info = vk::PipelineDepthStencilStateCreateInfo {
s_type: vk::StructureType::PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
depth_test_enable: 1, depth_test_enable: 1,
depth_write_enable: 1, depth_write_enable: 1,
depth_compare_op: vk::CompareOp::LESS_OR_EQUAL, depth_compare_op: vk::CompareOp::LESS_OR_EQUAL,
depth_bounds_test_enable: 0,
stencil_test_enable: 0,
front: noop_stencil_state.clone(), front: noop_stencil_state.clone(),
back: noop_stencil_state.clone(), back: noop_stencil_state.clone(),
max_depth_bounds: 1.0, max_depth_bounds: 1.0,
min_depth_bounds: 0.0, .. Default::default()
}; };
let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState { let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState {
blend_enable: 0, blend_enable: 0,
@ -408,45 +321,29 @@ fn main() {
alpha_blend_op: vk::BlendOp::ADD, alpha_blend_op: vk::BlendOp::ADD,
color_write_mask: vk::ColorComponentFlags::all(), color_write_mask: vk::ColorComponentFlags::all(),
}]; }];
let color_blend_state = vk::PipelineColorBlendStateCreateInfo { let color_blend_state = vk::PipelineColorBlendStateCreateInfo::builder()
s_type: vk::StructureType::PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, .logic_op( vk::LogicOp::CLEAR)
p_next: ptr::null(), .attachments(&color_blend_attachment_states)
flags: Default::default(), .build();
logic_op_enable: 0,
logic_op: vk::LogicOp::CLEAR,
attachment_count: color_blend_attachment_states.len() as u32,
p_attachments: color_blend_attachment_states.as_ptr(),
blend_constants: [0.0, 0.0, 0.0, 0.0],
};
let dynamic_state = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR]; let dynamic_state = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR];
let dynamic_state_info = vk::PipelineDynamicStateCreateInfo { let dynamic_state_info = vk::PipelineDynamicStateCreateInfo::builder()
s_type: vk::StructureType::PIPELINE_DYNAMIC_STATE_CREATE_INFO, .dynamic_states(&dynamic_state)
p_next: ptr::null(), .build();
flags: Default::default(),
dynamic_state_count: dynamic_state.len() as u32, let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder()
p_dynamic_states: dynamic_state.as_ptr(), .stages(&shader_stage_create_infos)
}; .vertex_input_state( &vertex_input_state_info)
let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo { .input_assembly_state( &vertex_input_assembly_state_info)
s_type: vk::StructureType::GRAPHICS_PIPELINE_CREATE_INFO, .viewport_state( &viewport_state_info)
p_next: ptr::null(), .rasterization_state( &rasterization_info)
flags: vk::PipelineCreateFlags::empty(), .multisample_state( &multisample_state_info)
stage_count: shader_stage_create_infos.len() as u32, .depth_stencil_state( &depth_state_info)
p_stages: shader_stage_create_infos.as_ptr(), .color_blend_state( &color_blend_state)
p_vertex_input_state: &vertex_input_state_info, .dynamic_state( &dynamic_state_info)
p_input_assembly_state: &vertex_input_assembly_state_info, .layout( pipeline_layout)
p_tessellation_state: ptr::null(), .render_pass( renderpass)
p_viewport_state: &viewport_state_info, .build();
p_rasterization_state: &rasterization_info,
p_multisample_state: &multisample_state_info,
p_depth_stencil_state: &depth_state_info,
p_color_blend_state: &color_blend_state,
p_dynamic_state: &dynamic_state_info,
layout: pipeline_layout,
render_pass: renderpass,
subpass: 0,
base_pipeline_handle: vk::Pipeline::null(),
base_pipeline_index: 0,
};
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)
@ -535,16 +432,11 @@ fn main() {
}, },
); );
//let mut present_info_err = mem::uninitialized(); //let mut present_info_err = mem::uninitialized();
let present_info = vk::PresentInfoKHR { let present_info = vk::PresentInfoKHR::builder()
s_type: vk::StructureType::PRESENT_INFO_KHR, .wait_semaphores( &[base.rendering_complete_semaphore])
p_next: ptr::null(), .swapchains( &[base.swapchain])
wait_semaphore_count: 1, .image_indices( &[present_index])
p_wait_semaphores: &base.rendering_complete_semaphore, .build();
swapchain_count: 1,
p_swapchains: &base.swapchain,
p_image_indices: &present_index,
p_results: ptr::null_mut(),
};
base.swapchain_loader base.swapchain_loader
.queue_present_khr(base.present_queue, &present_info) .queue_present_khr(base.present_queue, &present_info)
.unwrap(); .unwrap();