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

@ -52,36 +52,33 @@ fn main() {
.. 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
@ -98,8 +95,7 @@ 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)
@ -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,6 +430,8 @@ 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,
border_color: vk::BorderColor::FLOAT_OPAQUE_WHITE,
compare_op: vk::CompareOp::NEVER,
.. Default::default() .. Default::default()
}; };
@ -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)
@ -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
@ -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,14 +624,12 @@ 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,
@ -642,6 +637,10 @@ fn main() {
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()
.rasterization_samples(vk::SampleCountFlags::TYPE_1) .rasterization_samples(vk::SampleCountFlags::TYPE_1)
@ -676,13 +675,11 @@ 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)
@ -734,8 +731,8 @@ fn main() {
.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();