Port triangle example

This commit is contained in:
Tim 2018-07-31 13:22:16 +02:00
parent 253f79eef5
commit dc80b74ea9

View file

@ -27,50 +27,50 @@ fn main() {
vk::AttachmentDescription {
format: base.surface_format.format,
flags: vk::AttachmentDescriptionFlags::empty(),
samples: vk::SAMPLE_COUNT_1_BIT,
load_op: vk::AttachmentLoadOp::Clear,
store_op: vk::AttachmentStoreOp::Store,
stencil_load_op: vk::AttachmentLoadOp::DontCare,
stencil_store_op: vk::AttachmentStoreOp::DontCare,
initial_layout: vk::ImageLayout::Undefined,
final_layout: vk::ImageLayout::PresentSrcKhr,
samples: vk::SampleCountFlags::TYPE_1,
load_op: vk::AttachmentLoadOp::CLEAR,
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,
},
vk::AttachmentDescription {
format: vk::Format::D16Unorm,
format: vk::Format::D16_UNORM,
flags: vk::AttachmentDescriptionFlags::empty(),
samples: vk::SAMPLE_COUNT_1_BIT,
load_op: vk::AttachmentLoadOp::Clear,
store_op: vk::AttachmentStoreOp::DontCare,
stencil_load_op: vk::AttachmentLoadOp::DontCare,
stencil_store_op: vk::AttachmentStoreOp::DontCare,
initial_layout: vk::ImageLayout::DepthStencilAttachmentOptimal,
final_layout: vk::ImageLayout::DepthStencilAttachmentOptimal,
samples: vk::SampleCountFlags::TYPE_1,
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,
},
];
let color_attachment_ref = vk::AttachmentReference {
attachment: 0,
layout: vk::ImageLayout::ColorAttachmentOptimal,
layout: vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL,
};
let depth_attachment_ref = vk::AttachmentReference {
attachment: 1,
layout: vk::ImageLayout::DepthStencilAttachmentOptimal,
layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
};
let dependency = vk::SubpassDependency {
dependency_flags: Default::default(),
src_subpass: vk::VK_SUBPASS_EXTERNAL,
src_subpass: vk::SUBPASS_EXTERNAL,
dst_subpass: Default::default(),
src_stage_mask: vk::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
src_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT,
src_access_mask: Default::default(),
dst_access_mask: vk::ACCESS_COLOR_ATTACHMENT_READ_BIT
| vk::ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
dst_stage_mask: vk::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
dst_access_mask: vk::AccessFlags::COLOR_ATTACHMENT_READ
| vk::AccessFlags::COLOR_ATTACHMENT_WRITE,
dst_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT,
};
let subpass = vk::SubpassDescription {
color_attachment_count: 1,
p_color_attachments: &color_attachment_ref,
p_depth_stencil_attachment: &depth_attachment_ref,
flags: Default::default(),
pipeline_bind_point: vk::PipelineBindPoint::Graphics,
pipeline_bind_point: vk::PipelineBindPoint::GRAPHICS,
input_attachment_count: 0,
p_input_attachments: ptr::null(),
p_resolve_attachments: ptr::null(),
@ -78,7 +78,7 @@ fn main() {
p_preserve_attachments: ptr::null(),
};
let renderpass_create_info = vk::RenderPassCreateInfo {
s_type: vk::StructureType::RenderPassCreateInfo,
s_type: vk::StructureType::RENDER_PASS_CREATE_INFO,
flags: Default::default(),
p_next: ptr::null(),
attachment_count: renderpass_attachments.len() as u32,
@ -96,7 +96,7 @@ fn main() {
.map(|&present_image_view| {
let framebuffer_attachments = [present_image_view, base.depth_image_view];
let frame_buffer_create_info = vk::FramebufferCreateInfo {
s_type: vk::StructureType::FramebufferCreateInfo,
s_type: vk::StructureType::FRAMEBUFFER_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
render_pass: renderpass,
@ -113,12 +113,12 @@ fn main() {
.collect();
let index_buffer_data = [0u32, 1, 2];
let index_buffer_info = vk::BufferCreateInfo {
s_type: vk::StructureType::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,
usage: vk::BUFFER_USAGE_INDEX_BUFFER_BIT,
sharing_mode: vk::SharingMode::Exclusive,
usage: vk::BufferUsageFlags::INDEX_BUFFER,
sharing_mode: vk::SharingMode::EXCLUSIVE,
queue_family_index_count: 0,
p_queue_family_indices: ptr::null(),
};
@ -128,10 +128,10 @@ fn main() {
find_memorytype_index(
&index_buffer_memory_req,
&base.device_memory_properties,
vk::MEMORY_PROPERTY_HOST_VISIBLE_BIT,
vk::MemoryPropertyFlags::HOST_VISIBLE,
).expect("Unable to find suitable memorytype for the index buffer.");
let index_allocate_info = vk::MemoryAllocateInfo {
s_type: vk::StructureType::MemoryAllocateInfo,
s_type: vk::StructureType::MEMORY_ALLOCATE_INFO,
p_next: ptr::null(),
allocation_size: index_buffer_memory_req.size,
memory_type_index: index_buffer_memory_index,
@ -159,12 +159,12 @@ fn main() {
.unwrap();
let vertex_input_buffer_info = vk::BufferCreateInfo {
s_type: vk::StructureType::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,
usage: vk::BUFFER_USAGE_VERTEX_BUFFER_BIT,
sharing_mode: vk::SharingMode::Exclusive,
usage: vk::BufferUsageFlags::VERTEX_BUFFER,
sharing_mode: vk::SharingMode::EXCLUSIVE,
queue_family_index_count: 0,
p_queue_family_indices: ptr::null(),
};
@ -177,11 +177,11 @@ fn main() {
find_memorytype_index(
&vertex_input_buffer_memory_req,
&base.device_memory_properties,
vk::MEMORY_PROPERTY_HOST_VISIBLE_BIT,
vk::MemoryPropertyFlags::HOST_VISIBLE,
).expect("Unable to find suitable memorytype for the vertex buffer.");
let vertex_buffer_allocate_info = vk::MemoryAllocateInfo {
s_type: vk::StructureType::MemoryAllocateInfo,
s_type: vk::StructureType::MEMORY_ALLOCATE_INFO,
p_next: ptr::null(),
allocation_size: vertex_input_buffer_memory_req.size,
memory_type_index: vertex_input_buffer_memory_index,
@ -231,7 +231,7 @@ fn main() {
.filter_map(|byte| byte.ok())
.collect();
let vertex_shader_info = vk::ShaderModuleCreateInfo {
s_type: vk::StructureType::ShaderModuleCreateInfo,
s_type: vk::StructureType::SHADER_MODULE_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
code_size: vertex_bytes.len(),
@ -239,7 +239,7 @@ fn main() {
};
let frag_bytes: Vec<u8> = frag_spv_file.bytes().filter_map(|byte| byte.ok()).collect();
let frag_shader_info = vk::ShaderModuleCreateInfo {
s_type: vk::StructureType::ShaderModuleCreateInfo,
s_type: vk::StructureType::SHADER_MODULE_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
code_size: frag_bytes.len(),
@ -254,7 +254,7 @@ fn main() {
.expect("Fragment shader module error");
let layout_create_info = vk::PipelineLayoutCreateInfo {
s_type: vk::StructureType::PipelineLayoutCreateInfo,
s_type: vk::StructureType::PIPELINE_LAYOUT_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
set_layout_count: 0,
@ -270,47 +270,47 @@ fn main() {
let shader_entry_name = CString::new("main").unwrap();
let shader_stage_create_infos = [
vk::PipelineShaderStageCreateInfo {
s_type: vk::StructureType::PipelineShaderStageCreateInfo,
s_type: vk::StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
module: vertex_shader_module,
p_name: shader_entry_name.as_ptr(),
p_specialization_info: ptr::null(),
stage: vk::SHADER_STAGE_VERTEX_BIT,
stage: vk::ShaderStageFlags::VERTEX,
},
vk::PipelineShaderStageCreateInfo {
s_type: vk::StructureType::PipelineShaderStageCreateInfo,
s_type: vk::StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
module: fragment_shader_module,
p_name: shader_entry_name.as_ptr(),
p_specialization_info: ptr::null(),
stage: vk::SHADER_STAGE_FRAGMENT_BIT,
stage: vk::ShaderStageFlags::FRAGMENT,
},
];
let vertex_input_binding_descriptions = [
vk::VertexInputBindingDescription {
binding: 0,
stride: mem::size_of::<Vertex>() as u32,
input_rate: vk::VertexInputRate::Vertex,
input_rate: vk::VertexInputRate::VERTEX,
},
];
let vertex_input_attribute_descriptions = [
vk::VertexInputAttributeDescription {
location: 0,
binding: 0,
format: vk::Format::R32g32b32a32Sfloat,
format: vk::Format::R32G32B32A32_SFLOAT,
offset: offset_of!(Vertex, pos) as u32,
},
vk::VertexInputAttributeDescription {
location: 1,
binding: 0,
format: vk::Format::R32g32b32a32Sfloat,
format: vk::Format::R32G32B32A32_SFLOAT,
offset: offset_of!(Vertex, color) as u32,
},
];
let vertex_input_state_info = vk::PipelineVertexInputStateCreateInfo {
s_type: vk::StructureType::PipelineVertexInputStateCreateInfo,
s_type: vk::StructureType::PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
vertex_attribute_description_count: vertex_input_attribute_descriptions.len() as u32,
@ -319,11 +319,11 @@ fn main() {
p_vertex_binding_descriptions: vertex_input_binding_descriptions.as_ptr(),
};
let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo {
s_type: vk::StructureType::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::TriangleList,
topology: vk::PrimitiveTopology::TRIANGLE_LIST,
};
let viewports = [
vk::Viewport {
@ -342,7 +342,7 @@ fn main() {
},
];
let viewport_state_info = vk::PipelineViewportStateCreateInfo {
s_type: vk::StructureType::PipelineViewportStateCreateInfo,
s_type: vk::StructureType::PIPELINE_VIEWPORT_STATE_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
scissor_count: scissors.len() as u32,
@ -351,25 +351,25 @@ fn main() {
p_viewports: viewports.as_ptr(),
};
let rasterization_info = vk::PipelineRasterizationStateCreateInfo {
s_type: vk::StructureType::PipelineRasterizationStateCreateInfo,
s_type: vk::StructureType::PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
cull_mode: vk::CULL_MODE_NONE,
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::CounterClockwise,
front_face: vk::FrontFace::COUNTER_CLOCKWISE,
line_width: 1.0,
polygon_mode: vk::PolygonMode::Fill,
polygon_mode: vk::PolygonMode::FILL,
rasterizer_discard_enable: 0,
};
let multisample_state_info = vk::PipelineMultisampleStateCreateInfo {
s_type: vk::StructureType::PipelineMultisampleStateCreateInfo,
s_type: vk::StructureType::PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
flags: Default::default(),
p_next: ptr::null(),
rasterization_samples: vk::SAMPLE_COUNT_1_BIT,
rasterization_samples: vk::SampleCountFlags::TYPE_1,
sample_shading_enable: 0,
min_sample_shading: 0.0,
p_sample_mask: ptr::null(),
@ -377,21 +377,21 @@ fn main() {
alpha_to_coverage_enable: 0,
};
let noop_stencil_state = vk::StencilOpState {
fail_op: vk::StencilOp::Keep,
pass_op: vk::StencilOp::Keep,
depth_fail_op: vk::StencilOp::Keep,
compare_op: vk::CompareOp::Always,
fail_op: vk::StencilOp::KEEP,
pass_op: vk::StencilOp::KEEP,
depth_fail_op: vk::StencilOp::KEEP,
compare_op: vk::CompareOp::ALWAYS,
compare_mask: 0,
write_mask: 0,
reference: 0,
};
let depth_state_info = vk::PipelineDepthStencilStateCreateInfo {
s_type: vk::StructureType::PipelineDepthStencilStateCreateInfo,
s_type: vk::StructureType::PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
depth_test_enable: 1,
depth_write_enable: 1,
depth_compare_op: vk::CompareOp::LessOrEqual,
depth_compare_op: vk::CompareOp::LESS_OR_EQUAL,
depth_bounds_test_enable: 0,
stencil_test_enable: 0,
front: noop_stencil_state.clone(),
@ -402,35 +402,35 @@ fn main() {
let color_blend_attachment_states = [
vk::PipelineColorBlendAttachmentState {
blend_enable: 0,
src_color_blend_factor: vk::BlendFactor::SrcColor,
dst_color_blend_factor: vk::BlendFactor::OneMinusDstColor,
color_blend_op: vk::BlendOp::Add,
src_alpha_blend_factor: vk::BlendFactor::Zero,
dst_alpha_blend_factor: vk::BlendFactor::Zero,
alpha_blend_op: vk::BlendOp::Add,
src_color_blend_factor: vk::BlendFactor::SRC_COLOR,
dst_color_blend_factor: vk::BlendFactor::ONE_MINUS_DST_COLOR,
color_blend_op: vk::BlendOp::ADD,
src_alpha_blend_factor: vk::BlendFactor::ZERO,
dst_alpha_blend_factor: vk::BlendFactor::ZERO,
alpha_blend_op: vk::BlendOp::ADD,
color_write_mask: vk::ColorComponentFlags::all(),
},
];
let color_blend_state = vk::PipelineColorBlendStateCreateInfo {
s_type: vk::StructureType::PipelineColorBlendStateCreateInfo,
s_type: vk::StructureType::PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
logic_op_enable: 0,
logic_op: vk::LogicOp::Clear,
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 {
s_type: vk::StructureType::PipelineDynamicStateCreateInfo,
s_type: vk::StructureType::PIPELINE_DYNAMIC_STATE_CREATE_INFO,
p_next: ptr::null(),
flags: Default::default(),
dynamic_state_count: dynamic_state.len() as u32,
p_dynamic_states: dynamic_state.as_ptr(),
};
let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo {
s_type: vk::StructureType::GraphicsPipelineCreateInfo,
s_type: vk::StructureType::GRAPHICS_PIPELINE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::PipelineCreateFlags::empty(),
stage_count: shader_stage_create_infos.len() as u32,
@ -472,7 +472,7 @@ fn main() {
},
},
vk::ClearValue {
depth: vk::ClearDepthStencilValue {
depth_stencil: vk::ClearDepthStencilValue {
depth: 1.0,
stencil: 0,
},
@ -480,7 +480,7 @@ fn main() {
];
let render_pass_begin_info = vk::RenderPassBeginInfo {
s_type: vk::StructureType::RenderPassBeginInfo,
s_type: vk::StructureType::RENDER_PASS_BEGIN_INFO,
p_next: ptr::null(),
render_pass: renderpass,
framebuffer: framebuffers[present_index as usize],
@ -495,18 +495,18 @@ fn main() {
&base.device,
base.draw_command_buffer,
base.present_queue,
&[vk::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT],
&[vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT],
&[base.present_complete_semaphore],
&[base.rendering_complete_semaphore],
|device, draw_command_buffer| {
device.cmd_begin_render_pass(
draw_command_buffer,
&render_pass_begin_info,
vk::SubpassContents::Inline,
vk::SubpassContents::INLINE,
);
device.cmd_bind_pipeline(
draw_command_buffer,
vk::PipelineBindPoint::Graphics,
vk::PipelineBindPoint::GRAPHICS,
graphic_pipeline,
);
device.cmd_set_viewport(draw_command_buffer, 0, &viewports);
@ -521,7 +521,7 @@ fn main() {
draw_command_buffer,
index_buffer,
0,
vk::IndexType::Uint32,
vk::IndexType::UINT32,
);
device.cmd_draw_indexed(
draw_command_buffer,
@ -538,7 +538,7 @@ fn main() {
);
//let mut present_info_err = mem::uninitialized();
let present_info = vk::PresentInfoKHR {
s_type: vk::StructureType::PresentInfoKhr,
s_type: vk::StructureType::PRESENT_INFO_KHR,
p_next: ptr::null(),
wait_semaphore_count: 1,
p_wait_semaphores: &base.rendering_complete_semaphore,