refactored texture example to use defaults and builders

This commit is contained in:
colin 2018-12-07 11:46:48 -06:00
parent 0a16b5bd9b
commit 5dfd07098c

View file

@ -49,39 +49,36 @@ fn main() {
load_op: vk::AttachmentLoadOp::CLEAR, load_op: vk::AttachmentLoadOp::CLEAR,
initial_layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 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_refs = [vk::AttachmentReference {
let color_attachment_ref = 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 dependencies = [vk::SubpassDependency {
let dependency = 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() let subpasses = [vk::SubpassDescription::builder()
.color_attachments(&[color_attachment_ref]) .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
@ -94,12 +91,11 @@ fn main() {
.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::builder() let frame_buffer_create_info = vk::FramebufferCreateInfo::builder()
.render_pass(renderpass) .render_pass( renderpass)
.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)
@ -110,7 +106,7 @@ fn main() {
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() .. 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);
@ -223,7 +219,7 @@ fn main() {
size: std::mem::size_of_val(&uniform_color_buffer_data) as u64, size: std::mem::size_of_val(&uniform_color_buffer_data) as u64,
usage: vk::BufferUsageFlags::UNIFORM_BUFFER, usage: vk::BufferUsageFlags::UNIFORM_BUFFER,
sharing_mode: vk::SharingMode::EXCLUSIVE, sharing_mode: vk::SharingMode::EXCLUSIVE,
..Default::default() .. Default::default()
}; };
let uniform_color_buffer = base let uniform_color_buffer = base
.device .device
@ -390,7 +386,9 @@ fn main() {
width: image_dimensions.0, width: image_dimensions.0,
height: image_dimensions.1, height: image_dimensions.1,
depth: 1, depth: 1,
}).build()]; },
.. Default::default()
}];
device.cmd_copy_buffer_to_image( device.cmd_copy_buffer_to_image(
texture_command_buffer, texture_command_buffer,
image_buffer, image_buffer,
@ -432,7 +430,9 @@ fn main() {
address_mode_v: vk::SamplerAddressMode::MIRRORED_REPEAT, address_mode_v: vk::SamplerAddressMode::MIRRORED_REPEAT,
address_mode_w: vk::SamplerAddressMode::MIRRORED_REPEAT, address_mode_w: vk::SamplerAddressMode::MIRRORED_REPEAT,
max_anisotropy: 1.0, max_anisotropy: 1.0,
..Default::default() border_color: vk::BorderColor::FLOAT_OPAQUE_WHITE,
compare_op: vk::CompareOp::NEVER,
.. Default::default()
}; };
let sampler = base.device.create_sampler(&sampler_info, None).unwrap(); let sampler = base.device.create_sampler(&sampler_info, None).unwrap();
@ -453,7 +453,7 @@ fn main() {
..Default::default() ..Default::default()
}, },
image: texture_image, image: texture_image,
..Default::default() .. Default::default()
}; };
let tex_image_view = base let tex_image_view = base
.device .device
@ -471,8 +471,8 @@ fn main() {
]; ];
let descriptor_pool_info = vk::DescriptorPoolCreateInfo::builder() let descriptor_pool_info = vk::DescriptorPoolCreateInfo::builder()
.pool_sizes(&descriptor_sizes) .pool_sizes(&descriptor_sizes)
.max_sets(1) .max_sets( 1);
.build();
let descriptor_pool = base let descriptor_pool = base
.device .device
.create_descriptor_pool(&descriptor_pool_info, None) .create_descriptor_pool(&descriptor_pool_info, None)
@ -493,17 +493,16 @@ fn main() {
}, },
]; ];
let descriptor_info = vk::DescriptorSetLayoutCreateInfo::builder() let descriptor_info = vk::DescriptorSetLayoutCreateInfo::builder()
.bindings(&desc_layout_bindings) .bindings(&desc_layout_bindings);
.build();
let desc_set_layouts = [base let desc_set_layouts = [base
.device .device
.create_descriptor_set_layout(&descriptor_info, None) .create_descriptor_set_layout(&descriptor_info, None)
.unwrap()]; .unwrap()];
let desc_alloc_info = vk::DescriptorSetAllocateInfo::builder() let desc_alloc_info = vk::DescriptorSetAllocateInfo::builder()
.descriptor_pool(descriptor_pool) .descriptor_pool( descriptor_pool)
.set_layouts(&desc_set_layouts) .set_layouts(&desc_set_layouts);
.build();
let descriptor_sets = base let descriptor_sets = base
.device .device
.allocate_descriptor_sets(&desc_alloc_info) .allocate_descriptor_sets(&desc_alloc_info)
@ -527,7 +526,7 @@ fn main() {
descriptor_count: 1, descriptor_count: 1,
descriptor_type: vk::DescriptorType::UNIFORM_BUFFER, descriptor_type: vk::DescriptorType::UNIFORM_BUFFER,
p_buffer_info: &uniform_color_buffer_descriptor, p_buffer_info: &uniform_color_buffer_descriptor,
..Default::default() .. Default::default()
}, },
vk::WriteDescriptorSet { vk::WriteDescriptorSet {
dst_set: descriptor_sets[0], dst_set: descriptor_sets[0],
@ -535,7 +534,7 @@ fn main() {
descriptor_count: 1, descriptor_count: 1,
descriptor_type: vk::DescriptorType::COMBINED_IMAGE_SAMPLER, descriptor_type: vk::DescriptorType::COMBINED_IMAGE_SAMPLER,
p_image_info: &tex_descriptor, p_image_info: &tex_descriptor,
..Default::default() .. Default::default()
}, },
]; ];
base.device.update_descriptor_sets(&write_desc_sets, &[]); base.device.update_descriptor_sets(&write_desc_sets, &[]);
@ -567,8 +566,7 @@ fn main() {
.expect("Fragment shader module error"); .expect("Fragment shader module error");
let layout_create_info = vk::PipelineLayoutCreateInfo::builder() let layout_create_info = vk::PipelineLayoutCreateInfo::builder()
.set_layouts(&desc_set_layouts) .set_layouts(&desc_set_layouts);
.build();
let pipeline_layout = base let pipeline_layout = base
.device .device
@ -581,13 +579,13 @@ fn main() {
module: vertex_shader_module, module: vertex_shader_module,
p_name: shader_entry_name.as_ptr(), p_name: shader_entry_name.as_ptr(),
stage: vk::ShaderStageFlags::VERTEX, stage: vk::ShaderStageFlags::VERTEX,
..Default::default() .. Default::default()
}, },
vk::PipelineShaderStageCreateInfo { vk::PipelineShaderStageCreateInfo {
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,
..Default::default() .. Default::default()
}, },
]; ];
let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription { let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription {
@ -611,13 +609,12 @@ fn main() {
]; ];
let vertex_input_state_info = vk::PipelineVertexInputStateCreateInfo::builder() let vertex_input_state_info = vk::PipelineVertexInputStateCreateInfo::builder()
.vertex_attribute_descriptions(&vertex_input_attribute_descriptions) .vertex_attribute_descriptions(&vertex_input_attribute_descriptions)
.vertex_binding_descriptions(&vertex_input_binding_descriptions) .vertex_binding_descriptions(&vertex_input_binding_descriptions);
.build();
let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo::builder()
.topology(vk::PrimitiveTopology::TRIANGLE_LIST)
.build();
let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo {
topology: vk::PrimitiveTopology::TRIANGLE_LIST,
.. Default::default()
};
let viewports = [vk::Viewport { let viewports = [vk::Viewport {
x: 0.0, x: 0.0,
y: 0.0, y: 0.0,
@ -627,20 +624,22 @@ fn main() {
max_depth: 1.0, max_depth: 1.0,
}]; }];
let scissors = [vk::Rect2D { let scissors = [vk::Rect2D {
offset: vk::Offset2D { x: 0, y: 0 },
extent: base.surface_resolution.clone(), extent: base.surface_resolution.clone(),
.. Default::default()
}]; }];
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,
polygon_mode: vk::PolygonMode::FILL, polygon_mode: vk::PolygonMode::FILL,
..Default::default() .. Default::default()
};
let multisample_state_info = vk::PipelineMultisampleStateCreateInfo {
rasterization_samples: vk::SampleCountFlags::TYPE_1,
.. Default::default()
}; };
let multisample_state_info = vk::PipelineMultisampleStateCreateInfo::builder() let multisample_state_info = vk::PipelineMultisampleStateCreateInfo::builder()
@ -652,7 +651,7 @@ fn main() {
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,
..Default::default() .. Default::default()
}; };
let depth_state_info = vk::PipelineDepthStencilStateCreateInfo { let depth_state_info = vk::PipelineDepthStencilStateCreateInfo {
depth_test_enable: 1, depth_test_enable: 1,
@ -661,7 +660,7 @@ fn main() {
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,
..Default::default() .. Default::default()
}; };
let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState { let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState {
@ -675,27 +674,25 @@ fn main() {
color_write_mask: vk::ColorComponentFlags::all(), color_write_mask: vk::ColorComponentFlags::all(),
}]; }];
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)
.vertex_input_state(&vertex_input_state_info) .vertex_input_state( &vertex_input_state_info)
.input_assembly_state(&vertex_input_assembly_state_info) .input_assembly_state( &vertex_input_assembly_state_info)
.viewport_state(&viewport_state_info) .viewport_state( &viewport_state_info)
.rasterization_state(&rasterization_info) .rasterization_state( &rasterization_info)
.multisample_state(&multisample_state_info) .multisample_state( &multisample_state_info)
.depth_stencil_state(&depth_state_info) .depth_stencil_state( &depth_state_info)
.color_blend_state(&color_blend_state) .color_blend_state( &color_blend_state)
.dynamic_state(&dynamic_state_info) .dynamic_state( &dynamic_state_info)
.layout(pipeline_layout) .layout( pipeline_layout)
.render_pass(renderpass) .render_pass( renderpass)
.build(); .build();
let graphics_pipelines = base let graphics_pipelines = base
@ -731,11 +728,11 @@ fn main() {
let render_pass_begin_info = vk::RenderPassBeginInfo::builder() let render_pass_begin_info = vk::RenderPassBeginInfo::builder()
.render_pass(renderpass) .render_pass(renderpass)
.framebuffer(framebuffers[present_index as usize]) .framebuffer(framebuffers[present_index as usize])
.render_area(vk::Rect2D { .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_values(&clear_values) })
.build(); .clear_values(&clear_values);
record_submit_commandbuffer( record_submit_commandbuffer(
&base.device, &base.device,
@ -791,13 +788,14 @@ 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() wait_semaphore_count: 1,
.wait_semaphores(&[base.rendering_complete_semaphore]) p_wait_semaphores: &base.rendering_complete_semaphore,
.swapchains(&[base.swapchain]) swapchain_count: 1,
.image_indices(&[present_index]) p_swapchains: &base.swapchain,
.build(); p_image_indices: &present_index,
.. Default::default()
};
base.swapchain_loader base.swapchain_loader
.queue_present(base.present_queue, &present_info) .queue_present(base.present_queue, &present_info)
.unwrap(); .unwrap();