From bac3a5bc21d3daf62ba53bf31e6d5087e64108d3 Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 6 Dec 2018 08:58:12 -0600 Subject: [PATCH 01/19] using builders and Defaults in texture example --- examples/src/bin/texture.rs | 476 +++++++++++++----------------------- examples/src/lib.rs | 387 ++++++++++++++--------------- 2 files changed, 348 insertions(+), 515 deletions(-) diff --git a/examples/src/bin/texture.rs b/examples/src/bin/texture.rs index dc68c8d..331e875 100644 --- a/examples/src/bin/texture.rs +++ b/examples/src/bin/texture.rs @@ -33,30 +33,27 @@ pub struct Vector3 { fn main() { unsafe { let base = ExampleBase::new(1920, 1080); + let renderpass_attachments = [ vk::AttachmentDescription { format: base.surface_format.format, - flags: vk::AttachmentDescriptionFlags::empty(), 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, + .. Default::default() }, vk::AttachmentDescription { format: vk::Format::D16_UNORM, - flags: vk::AttachmentDescriptionFlags::empty(), 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, + .. Default::default() }, ]; + + let color_attachment_ref = vk::AttachmentReference { attachment: 0, layout: vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL, @@ -65,73 +62,59 @@ fn main() { attachment: 1, layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL, }; + let dependency = vk::SubpassDependency { - dependency_flags: Default::default(), src_subpass: vk::SUBPASS_EXTERNAL, - dst_subpass: Default::default(), src_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, - src_access_mask: Default::default(), dst_access_mask: vk::AccessFlags::COLOR_ATTACHMENT_READ | vk::AccessFlags::COLOR_ATTACHMENT_WRITE, dst_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, + .. Default::default() }; - 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, - input_attachment_count: 0, - p_input_attachments: ptr::null(), - p_resolve_attachments: ptr::null(), - preserve_attachment_count: 0, - p_preserve_attachments: ptr::null(), - }; - 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 subpass = vk::SubpassDescription::builder() + .color_attachments(&[color_attachment_ref]) + .depth_stencil_attachment(&depth_attachment_ref) + .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS) + .build(); + + + let renderpass_create_info = vk::RenderPassCreateInfo::builder() + .attachments(&renderpass_attachments) + .subpasses( &[subpass]) + .dependencies(&[dependency]) + .build(); + let renderpass = base .device .create_render_pass(&renderpass_create_info, None) .unwrap(); + let framebuffers: Vec = base .present_image_views .iter() .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::FRAMEBUFFER_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - render_pass: renderpass, - attachment_count: framebuffer_attachments.len() as u32, - p_attachments: framebuffer_attachments.as_ptr(), - width: base.surface_resolution.width, - height: base.surface_resolution.height, - layers: 1, - }; + let frame_buffer_create_info = vk::FramebufferCreateInfo::builder() + .render_pass(renderpass) + .attachments(&framebuffer_attachments) + .width( base.surface_resolution.width) + .height( base.surface_resolution.height) + .layers( 1) + .build(); + base.device .create_framebuffer(&frame_buffer_create_info, None) .unwrap() }).collect(); let index_buffer_data = [0u32, 1, 2, 2, 3, 0]; 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, usage: vk::BufferUsageFlags::INDEX_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - queue_family_index_count: 0, - p_queue_family_indices: ptr::null(), + .. Default::default() }; 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); @@ -141,10 +124,9 @@ fn main() { vk::MemoryPropertyFlags::HOST_VISIBLE, ).expect("Unable to find suitable memorytype for the index buffer."); let index_allocate_info = vk::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, + .. Default::default() }; let index_buffer_memory = base .device @@ -188,14 +170,10 @@ fn main() { }, ]; let vertex_input_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(&vertices) as u64, usage: vk::BufferUsageFlags::VERTEX_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - queue_family_index_count: 0, - p_queue_family_indices: ptr::null(), + .. Default::default() }; let vertex_input_buffer = base .device @@ -211,15 +189,15 @@ fn main() { ).expect("Unable to find suitable memorytype for the vertex buffer."); 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, memory_type_index: vertex_input_buffer_memory_index, + .. Default::default() }; let vertex_input_buffer_memory = base .device .allocate_memory(&vertex_buffer_allocate_info, None) .unwrap(); + let vert_ptr = base .device .map_memory( @@ -246,14 +224,10 @@ fn main() { _pad: 0.0, }; let uniform_color_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(&uniform_color_buffer_data) as u64, usage: vk::BufferUsageFlags::UNIFORM_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - queue_family_index_count: 0, - p_queue_family_indices: ptr::null(), + .. Default::default() }; let uniform_color_buffer = base .device @@ -269,10 +243,9 @@ fn main() { ).expect("Unable to find suitable memorytype for the vertex buffer."); let uniform_color_buffer_allocate_info = vk::MemoryAllocateInfo { - s_type: vk::StructureType::MEMORY_ALLOCATE_INFO, - p_next: ptr::null(), allocation_size: uniform_color_buffer_memory_req.size, memory_type_index: uniform_color_buffer_memory_index, + .. Default::default() }; let uniform_color_buffer_memory = base .device @@ -301,14 +274,10 @@ fn main() { let image_dimensions = image.dimensions(); let image_data = image.into_raw(); let image_buffer_info = vk::BufferCreateInfo { - s_type: vk::StructureType::BUFFER_CREATE_INFO, - p_next: ptr::null(), - flags: vk::BufferCreateFlags::empty(), size: (std::mem::size_of::() * image_data.len()) as u64, usage: vk::BufferUsageFlags::TRANSFER_SRC, sharing_mode: vk::SharingMode::EXCLUSIVE, - queue_family_index_count: 0, - p_queue_family_indices: ptr::null(), + .. Default::default() }; let image_buffer = base.device.create_buffer(&image_buffer_info, None).unwrap(); let image_buffer_memory_req = base.device.get_buffer_memory_requirements(image_buffer); @@ -319,10 +288,9 @@ fn main() { ).expect("Unable to find suitable memorytype for the vertex buffer."); let image_buffer_allocate_info = vk::MemoryAllocateInfo { - s_type: vk::StructureType::MEMORY_ALLOCATE_INFO, - p_next: ptr::null(), allocation_size: image_buffer_memory_req.size, memory_type_index: image_buffer_memory_index, + .. Default::default() }; let image_buffer_memory = base .device @@ -348,9 +316,6 @@ fn main() { .unwrap(); let texture_create_info = vk::ImageCreateInfo { - s_type: vk::StructureType::IMAGE_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), image_type: vk::ImageType::TYPE_2D, format: vk::Format::R8G8B8A8_UNORM, extent: vk::Extent3D { @@ -364,9 +329,7 @@ fn main() { tiling: vk::ImageTiling::OPTIMAL, usage: vk::ImageUsageFlags::TRANSFER_DST | vk::ImageUsageFlags::SAMPLED, sharing_mode: vk::SharingMode::EXCLUSIVE, - queue_family_index_count: 0, - p_queue_family_indices: ptr::null(), - initial_layout: vk::ImageLayout::UNDEFINED, + .. Default::default() }; let texture_image = base .device @@ -380,10 +343,9 @@ fn main() { ).expect("Unable to find suitable memory index for depth image."); let texture_allocate_info = vk::MemoryAllocateInfo { - s_type: vk::StructureType::MEMORY_ALLOCATE_INFO, - p_next: ptr::null(), allocation_size: texture_memory_req.size, memory_type_index: texture_memory_index, + .. Default::default() }; let texture_memory = base .device @@ -397,27 +359,21 @@ fn main() { &base.device, base.setup_command_buffer, base.present_queue, - &[vk::PipelineStageFlags::TOP_OF_PIPE], + &[], &[], &[], |device, texture_command_buffer| { let texture_barrier = vk::ImageMemoryBarrier { - s_type: vk::StructureType::IMAGE_MEMORY_BARRIER, - p_next: ptr::null(), - src_access_mask: Default::default(), dst_access_mask: vk::AccessFlags::TRANSFER_WRITE, - old_layout: vk::ImageLayout::UNDEFINED, new_layout: vk::ImageLayout::TRANSFER_DST_OPTIMAL, - src_queue_family_index: vk::QUEUE_FAMILY_IGNORED, - dst_queue_family_index: vk::QUEUE_FAMILY_IGNORED, image: texture_image, subresource_range: vk::ImageSubresourceRange { aspect_mask: vk::ImageAspectFlags::COLOR, - base_mip_level: 0, level_count: 1, - base_array_layer: 0, layer_count: 1, + .. Default::default() }, + .. Default::default() }; device.cmd_pipeline_barrier( texture_command_buffer, @@ -428,24 +384,22 @@ fn main() { &[], &[texture_barrier], ); - let buffer_copy_regions = [vk::BufferImageCopy { - image_subresource: vk::ImageSubresourceLayers { - aspect_mask: vk::ImageAspectFlags::COLOR, - mip_level: 0, - base_array_layer: 0, - layer_count: 1, - }, - image_extent: vk::Extent3D { - width: image_dimensions.0, - height: image_dimensions.1, - depth: 1, - }, - buffer_offset: 0, - // FIX ME - buffer_image_height: 0, - buffer_row_length: 0, - image_offset: vk::Offset3D { x: 0, y: 0, z: 0 }, - }]; + let buffer_copy_regions = [ + vk::BufferImageCopy::builder() + .image_subresource( + vk::ImageSubresourceLayers::builder() + .aspect_mask( vk::ImageAspectFlags::COLOR) + .layer_count(1) + .build() + ) + .image_extent( + vk::Extent3D { + width: image_dimensions.0, + height: image_dimensions.1, + depth: 1, + }) + .build() + ]; device.cmd_copy_buffer_to_image( texture_command_buffer, image_buffer, @@ -454,22 +408,18 @@ fn main() { &buffer_copy_regions, ); let texture_barrier_end = vk::ImageMemoryBarrier { - s_type: vk::StructureType::IMAGE_MEMORY_BARRIER, - p_next: ptr::null(), src_access_mask: vk::AccessFlags::TRANSFER_WRITE, dst_access_mask: vk::AccessFlags::SHADER_READ, old_layout: vk::ImageLayout::TRANSFER_DST_OPTIMAL, new_layout: vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL, - src_queue_family_index: vk::QUEUE_FAMILY_IGNORED, - dst_queue_family_index: vk::QUEUE_FAMILY_IGNORED, image: texture_image, subresource_range: vk::ImageSubresourceRange { aspect_mask: vk::ImageAspectFlags::COLOR, - base_mip_level: 0, level_count: 1, - base_array_layer: 0, layer_count: 1, + .. Default::default() }, + .. Default::default() }; device.cmd_pipeline_barrier( texture_command_buffer, @@ -484,32 +434,19 @@ fn main() { ); let sampler_info = vk::SamplerCreateInfo { - s_type: vk::StructureType::SAMPLER_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), mag_filter: vk::Filter::LINEAR, min_filter: vk::Filter::LINEAR, mipmap_mode: vk::SamplerMipmapMode::LINEAR, address_mode_u: vk::SamplerAddressMode::MIRRORED_REPEAT, address_mode_v: vk::SamplerAddressMode::MIRRORED_REPEAT, address_mode_w: vk::SamplerAddressMode::MIRRORED_REPEAT, - mip_lod_bias: 0.0, - min_lod: 0.0, - max_lod: 0.0, - anisotropy_enable: 0, max_anisotropy: 1.0, - border_color: vk::BorderColor::FLOAT_OPAQUE_WHITE, - compare_enable: 0, - compare_op: vk::CompareOp::NEVER, - unnormalized_coordinates: 0, + .. Default::default() }; let sampler = base.device.create_sampler(&sampler_info, None).unwrap(); let tex_image_view_info = vk::ImageViewCreateInfo { - s_type: vk::StructureType::IMAGE_VIEW_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), view_type: vk::ImageViewType::TYPE_2D, format: texture_create_info.format, components: vk::ComponentMapping { @@ -520,12 +457,12 @@ fn main() { }, subresource_range: vk::ImageSubresourceRange { aspect_mask: vk::ImageAspectFlags::COLOR, - base_mip_level: 0, level_count: 1, - base_array_layer: 0, layer_count: 1, + .. Default::default() }, image: texture_image, + .. Default::default() }; let tex_image_view = base .device @@ -541,53 +478,41 @@ fn main() { descriptor_count: 1, }, ]; - let descriptor_pool_info = vk::DescriptorPoolCreateInfo { - s_type: vk::StructureType::DESCRIPTOR_POOL_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - pool_size_count: descriptor_sizes.len() as u32, - p_pool_sizes: descriptor_sizes.as_ptr(), - max_sets: 1, - }; + let descriptor_pool_info = vk::DescriptorPoolCreateInfo::builder() + .pool_sizes(&descriptor_sizes) + .max_sets(1) + .build(); let descriptor_pool = base .device .create_descriptor_pool(&descriptor_pool_info, None) .unwrap(); let desc_layout_bindings = [ vk::DescriptorSetLayoutBinding { - binding: 0, descriptor_type: vk::DescriptorType::UNIFORM_BUFFER, descriptor_count: 1, stage_flags: vk::ShaderStageFlags::FRAGMENT, - p_immutable_samplers: ptr::null(), + .. Default::default() }, vk::DescriptorSetLayoutBinding { binding: 1, descriptor_type: vk::DescriptorType::COMBINED_IMAGE_SAMPLER, descriptor_count: 1, stage_flags: vk::ShaderStageFlags::FRAGMENT, - p_immutable_samplers: ptr::null(), + .. Default::default() }, ]; - let descriptor_info = vk::DescriptorSetLayoutCreateInfo { - s_type: vk::StructureType::DESCRIPTOR_SET_LAYOUT_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - binding_count: desc_layout_bindings.len() as u32, - p_bindings: desc_layout_bindings.as_ptr(), - }; + let descriptor_info = vk::DescriptorSetLayoutCreateInfo::builder() + .bindings(&desc_layout_bindings) + .build(); let desc_set_layouts = [base .device .create_descriptor_set_layout(&descriptor_info, None) .unwrap()]; - let desc_alloc_info = vk::DescriptorSetAllocateInfo { - s_type: vk::StructureType::DESCRIPTOR_SET_ALLOCATE_INFO, - p_next: ptr::null(), - descriptor_pool: descriptor_pool, - descriptor_set_count: desc_set_layouts.len() as u32, - p_set_layouts: desc_set_layouts.as_ptr(), - }; + let desc_alloc_info = vk::DescriptorSetAllocateInfo::builder() + .descriptor_pool(descriptor_pool) + .set_layouts(&desc_set_layouts) + .build(); let descriptor_sets = base .device .allocate_descriptor_sets(&desc_alloc_info) @@ -607,28 +532,19 @@ fn main() { let write_desc_sets = [ vk::WriteDescriptorSet { - s_type: vk::StructureType::WRITE_DESCRIPTOR_SET, - p_next: ptr::null(), dst_set: descriptor_sets[0], - dst_binding: 0, - dst_array_element: 0, descriptor_count: 1, descriptor_type: vk::DescriptorType::UNIFORM_BUFFER, - p_image_info: ptr::null(), p_buffer_info: &uniform_color_buffer_descriptor, - p_texel_buffer_view: ptr::null(), + .. Default::default() }, vk::WriteDescriptorSet { - s_type: vk::StructureType::WRITE_DESCRIPTOR_SET, - p_next: ptr::null(), dst_set: descriptor_sets[0], dst_binding: 1, - dst_array_element: 0, descriptor_count: 1, descriptor_type: vk::DescriptorType::COMBINED_IMAGE_SAMPLER, p_image_info: &tex_descriptor, - p_buffer_info: ptr::null(), - p_texel_buffer_view: ptr::null(), + .. Default::default() }, ]; base.device.update_descriptor_sets(&write_desc_sets, &[]); @@ -642,21 +558,22 @@ fn main() { .bytes() .filter_map(|byte| byte.ok()) .collect(); - let vertex_shader_info = vk::ShaderModuleCreateInfo { - s_type: vk::StructureType::SHADER_MODULE_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - code_size: vertex_bytes.len(), - p_code: vertex_bytes.as_ptr() as *const u32, - }; - let frag_bytes: Vec = frag_spv_file.bytes().filter_map(|byte| byte.ok()).collect(); - let frag_shader_info = vk::ShaderModuleCreateInfo { - s_type: vk::StructureType::SHADER_MODULE_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - code_size: frag_bytes.len(), - p_code: frag_bytes.as_ptr() as *const u32, - }; + let vertex_bytes = examples::bytes_to_u32_vec(vertex_bytes.iter().cloned()); + + let vertex_shader_info = vk::ShaderModuleCreateInfo::builder() + .code(&vertex_bytes) + .build(); + + let frag_bytes: Vec = frag_spv_file.bytes() + .filter_map(|byte| byte.ok()) + .collect(); + + let frag_bytes = examples::bytes_to_u32_vec(frag_bytes.iter().cloned()); + + let frag_shader_info = vk::ShaderModuleCreateInfo::builder() + .code(&frag_bytes) + .build(); + let vertex_shader_module = base .device .create_shader_module(&vertex_shader_info, None) @@ -667,15 +584,9 @@ fn main() { .create_shader_module(&frag_shader_info, None) .expect("Fragment shader module error"); - let layout_create_info = vk::PipelineLayoutCreateInfo { - s_type: vk::StructureType::PIPELINE_LAYOUT_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - set_layout_count: desc_set_layouts.len() as u32, - p_set_layouts: desc_set_layouts.as_ptr(), - push_constant_range_count: 0, - p_push_constant_ranges: ptr::null(), - }; + let layout_create_info = vk::PipelineLayoutCreateInfo::builder() + .set_layouts(&desc_set_layouts) + .build(); let pipeline_layout = base .device @@ -685,22 +596,16 @@ fn main() { let shader_entry_name = CString::new("main").unwrap(); let shader_stage_create_infos = [ vk::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::ShaderStageFlags::VERTEX, + .. Default::default() }, vk::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::ShaderStageFlags::FRAGMENT, + .. Default::default() }, ]; let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription { @@ -722,22 +627,15 @@ fn main() { offset: offset_of!(Vertex, uv) as u32, }, ]; - let vertex_input_state_info = vk::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, - 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 { - 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, - }; + 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_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo::builder() + .topology(vk::PrimitiveTopology::TRIANGLE_LIST) + .build(); + let viewports = [vk::Viewport { x: 0.0, y: 0.0, @@ -750,64 +648,40 @@ fn main() { offset: vk::Offset2D { x: 0, y: 0 }, extent: base.surface_resolution.clone(), }]; - let viewport_state_info = vk::PipelineViewportStateCreateInfo { - s_type: vk::StructureType::PIPELINE_VIEWPORT_STATE_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - scissor_count: scissors.len() as u32, - p_scissors: scissors.as_ptr(), - viewport_count: viewports.len() as u32, - p_viewports: viewports.as_ptr(), - }; + + let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder() + .scissors(&scissors) + .viewports(&viewports) + .build(); + 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, line_width: 1.0, polygon_mode: vk::PolygonMode::FILL, - rasterizer_discard_enable: 0, - }; - 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, - sample_shading_enable: 0, - min_sample_shading: 0.0, - p_sample_mask: ptr::null(), - alpha_to_one_enable: 0, - alpha_to_coverage_enable: 0, + .. Default::default() }; + + let multisample_state_info = vk::PipelineMultisampleStateCreateInfo::builder() + .rasterization_samples( vk::SampleCountFlags::TYPE_1) + .build(); + 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, - compare_mask: 0, - write_mask: 0, - reference: 0, + .. Default::default() }; 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_write_enable: 1, depth_compare_op: vk::CompareOp::LESS_OR_EQUAL, - depth_bounds_test_enable: 0, - stencil_test_enable: 0, front: noop_stencil_state.clone(), back: noop_stencil_state.clone(), max_depth_bounds: 1.0, - min_depth_bounds: 0.0, + .. Default::default() }; + let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState { blend_enable: 0, src_color_blend_factor: vk::BlendFactor::SRC_COLOR, @@ -818,45 +692,30 @@ fn main() { alpha_blend_op: vk::BlendOp::ADD, color_write_mask: vk::ColorComponentFlags::all(), }]; - let color_blend_state = vk::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, - 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 color_blend_state = vk::PipelineColorBlendStateCreateInfo::builder() + .logic_op(vk::LogicOp::CLEAR) + .attachments(&color_blend_attachment_states) + .build(); + let dynamic_state = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR]; - let dynamic_state_info = vk::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::GRAPHICS_PIPELINE_CREATE_INFO, - p_next: ptr::null(), - flags: vk::PipelineCreateFlags::empty(), - stage_count: shader_stage_create_infos.len() as u32, - p_stages: shader_stage_create_infos.as_ptr(), - p_vertex_input_state: &vertex_input_state_info, - p_input_assembly_state: &vertex_input_assembly_state_info, - p_tessellation_state: ptr::null(), - p_viewport_state: &viewport_state_info, - 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 dynamic_state_info = vk::PipelineDynamicStateCreateInfo::builder() + .dynamic_states(&dynamic_state) + .build(); + + let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder() + .stages(&shader_stage_create_infos) + .vertex_input_state(&vertex_input_state_info) + .input_assembly_state(&vertex_input_assembly_state_info) + .viewport_state(&viewport_state_info) + .rasterization_state(&rasterization_info) + .multisample_state(&multisample_state_info) + .depth_stencil_state(&depth_state_info) + .color_blend_state(&color_blend_state) + .dynamic_state(&dynamic_state_info) + .layout(pipeline_layout) + .render_pass(renderpass) + .build(); + let graphics_pipelines = base .device .create_graphics_pipelines(vk::PipelineCache::null(), &[graphic_pipeline_info], None) @@ -887,18 +746,16 @@ fn main() { }, ]; - let render_pass_begin_info = vk::RenderPassBeginInfo { - s_type: vk::StructureType::RENDER_PASS_BEGIN_INFO, - p_next: ptr::null(), - render_pass: renderpass, - framebuffer: framebuffers[present_index as usize], - render_area: vk::Rect2D { + let render_pass_begin_info = vk::RenderPassBeginInfo::builder() + .render_pass(renderpass) + .framebuffer(framebuffers[present_index as usize]) + .render_area(vk::Rect2D { offset: vk::Offset2D { x: 0, y: 0 }, extent: base.surface_resolution.clone(), - }, - clear_value_count: clear_values.len() as u32, - p_clear_values: clear_values.as_ptr(), - }; + }) + .clear_values(&clear_values) + .build(); + record_submit_commandbuffer( &base.device, base.draw_command_buffer, @@ -953,16 +810,13 @@ fn main() { }, ); //let mut present_info_err = mem::uninitialized(); - let present_info = vk::PresentInfoKHR { - s_type: vk::StructureType::PRESENT_INFO_KHR, - p_next: ptr::null(), - wait_semaphore_count: 1, - p_wait_semaphores: &base.rendering_complete_semaphore, - swapchain_count: 1, - p_swapchains: &base.swapchain, - p_image_indices: &present_index, - p_results: ptr::null_mut(), - }; + + let present_info = vk::PresentInfoKHR::builder() + .wait_semaphores(&[base.rendering_complete_semaphore]) + .swapchains( &[base.swapchain]) + .image_indices( &[present_index]) + .build(); + base.swapchain_loader .queue_present_khr(base.present_queue, &present_info) .unwrap(); diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 195903c..4570f2a 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -2,6 +2,7 @@ extern crate ash; #[cfg(target_os = "windows")] extern crate winapi; + extern crate winit; #[cfg(target_os = "macos")] @@ -38,7 +39,7 @@ use std::default::Default; use std::ffi::{CStr, CString}; use std::ops::Drop; use std::os::raw::{c_char, c_void}; -use std::ptr; + // Simple offset_of macro akin to C++ offsetof #[macro_export] @@ -52,6 +53,34 @@ macro_rules! offset_of { }}; } +fn u32_from_bytes(bytes: &[u8; 4]) -> u32 { + unsafe { + std::mem::transmute(*bytes) + } +} + +pub fn bytes_to_u32_vec(bytes:B) -> Vec +where B: Iterator +{ + let mut output = vec![]; + let mut buffer: [u8;4] = [0,0,0,0]; + for (i,b) in bytes.enumerate() { + match i % 4 { + 3 => { + buffer[3] = b; + output.push(u32_from_bytes(&buffer)); + buffer = [0,0,0,0]; + }, + idx => buffer[idx] = b + } + } + if buffer != [0,0,0,0] { + output.push(u32_from_bytes(&buffer)); + } + output +} + + pub fn record_submit_commandbuffer( device: &D, command_buffer: vk::CommandBuffer, @@ -67,12 +96,9 @@ pub fn record_submit_commandbuffer( use winit::os::unix::WindowExt; let x11_display = window.get_xlib_display().unwrap(); let x11_window = window.get_xlib_window().unwrap(); - let x11_create_info = vk::XlibSurfaceCreateInfoKHR { - s_type: vk::StructureType::XLIB_SURFACE_CREATE_INFO_KHR, - p_next: ptr::null(), - flags: Default::default(), - window: x11_window as vk::Window, - dpy: x11_display as *mut vk::Display, - }; + let x11_create_info = vk::XlibSurfaceCreateInfoKHR::builder() + .window(x11_window as vk::Window) + .dpy(x11_display as *mut vk::Display) + .build(); let xlib_surface_loader = XlibSurface::new(entry, instance); xlib_surface_loader.create_xlib_surface_khr(&x11_create_info, None) } @@ -333,7 +351,6 @@ impl ExampleBase { .unwrap(); let entry = Entry::new().unwrap(); let app_name = CString::new("VulkanTriangle").unwrap(); - let raw_name = app_name.as_ptr(); let layer_names = [CString::new("VK_LAYER_LUNARG_standard_validation").unwrap()]; let layers_names_raw: Vec<*const i8> = layer_names @@ -342,37 +359,34 @@ impl ExampleBase { .collect(); let extension_names_raw = extension_names(); - let appinfo = vk::ApplicationInfo { - p_application_name: raw_name, - s_type: vk::StructureType::APPLICATION_INFO, - p_next: ptr::null(), - application_version: 0, - p_engine_name: raw_name, - engine_version: 0, - api_version: vk_make_version!(1, 0, 36), - }; - let create_info = vk::InstanceCreateInfo { - s_type: vk::StructureType::INSTANCE_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - p_application_info: &appinfo, - pp_enabled_layer_names: layers_names_raw.as_ptr(), - enabled_layer_count: layers_names_raw.len() as u32, - pp_enabled_extension_names: extension_names_raw.as_ptr(), - enabled_extension_count: extension_names_raw.len() as u32, - }; + + let appinfo = vk::ApplicationInfo::builder() + .application_name(&app_name ) + .application_version(0) + .engine_name(&app_name) + .engine_version(0) + .api_version(vk_make_version!(1,0,36)) + .build(); + + let create_info = vk::InstanceCreateInfo::builder() + .application_info(&appinfo) + .enabled_layer_names(&layers_names_raw) + .enabled_extension_names(&extension_names_raw ) + .build(); + let instance: Instance = entry .create_instance(&create_info, None) .expect("Instance creation error"); - let debug_info = vk::DebugReportCallbackCreateInfoEXT { - s_type: vk::StructureType::DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT, - p_next: ptr::null(), - flags: vk::DebugReportFlagsEXT::ERROR - | vk::DebugReportFlagsEXT::WARNING - | vk::DebugReportFlagsEXT::PERFORMANCE_WARNING, - pfn_callback: Some(vulkan_debug_callback), - p_user_data: ptr::null_mut(), - }; + + let debug_info = vk::DebugReportCallbackCreateInfoEXT::builder() + .pfn_callback(Some(vulkan_debug_callback)) + .flags( + vk::DebugReportFlagsEXT::ERROR + | vk::DebugReportFlagsEXT::WARNING + | vk::DebugReportFlagsEXT::PERFORMANCE_WARNING + ) + .build(); + let debug_report_loader = DebugReport::new(&entry, &instance); let debug_call_back = debug_report_loader .create_debug_report_callback_ext(&debug_info, None) @@ -412,26 +426,18 @@ impl ExampleBase { ..Default::default() }; let priorities = [1.0]; - let queue_info = vk::DeviceQueueCreateInfo { - s_type: vk::StructureType::DEVICE_QUEUE_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - queue_family_index: queue_family_index as u32, - p_queue_priorities: priorities.as_ptr(), - queue_count: priorities.len() as u32, - }; - let device_create_info = vk::DeviceCreateInfo { - s_type: vk::StructureType::DEVICE_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - queue_create_info_count: 1, - p_queue_create_infos: &queue_info, - enabled_layer_count: 0, - pp_enabled_layer_names: ptr::null(), - enabled_extension_count: device_extension_names_raw.len() as u32, - pp_enabled_extension_names: device_extension_names_raw.as_ptr(), - p_enabled_features: &features, - }; + + let queue_info = vk::DeviceQueueCreateInfo::builder() + .queue_family_index(queue_family_index as u32 ) + .queue_priorities(&priorities) + .build(); + + let device_create_info = vk::DeviceCreateInfo::builder() + .queue_create_infos(&[queue_info]) + .enabled_extension_names(&device_extension_names_raw) + .enabled_features(&features) + .build(); + let device: Device = instance .create_device(pdevice, &device_create_info, None) .unwrap(); @@ -483,43 +489,38 @@ impl ExampleBase { .find(|&mode| mode == vk::PresentModeKHR::MAILBOX) .unwrap_or(vk::PresentModeKHR::FIFO); let swapchain_loader = Swapchain::new(&instance, &device); - let swapchain_create_info = vk::SwapchainCreateInfoKHR { - s_type: vk::StructureType::SWAPCHAIN_CREATE_INFO_KHR, - p_next: ptr::null(), - flags: Default::default(), - surface: surface, - min_image_count: desired_image_count, - image_color_space: surface_format.color_space, - image_format: surface_format.format, - image_extent: surface_resolution.clone(), - image_usage: vk::ImageUsageFlags::COLOR_ATTACHMENT, - image_sharing_mode: vk::SharingMode::EXCLUSIVE, - pre_transform: pre_transform, - composite_alpha: vk::CompositeAlphaFlagsKHR::OPAQUE, - present_mode: present_mode, - clipped: 1, - old_swapchain: vk::SwapchainKHR::null(), - image_array_layers: 1, - p_queue_family_indices: ptr::null(), - queue_family_index_count: 0, - }; + + let swapchain_create_info = vk::SwapchainCreateInfoKHR::builder() + .surface(surface) + .min_image_count(desired_image_count) + .image_color_space(surface_format.color_space) + .image_format(surface_format.format) + .image_extent(surface_resolution.clone()) + .image_usage(vk::ImageUsageFlags::COLOR_ATTACHMENT) + .pre_transform(pre_transform) + .composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE) + .present_mode(present_mode) +// .clipped(true) + .image_array_layers(1) + .build(); let swapchain = swapchain_loader .create_swapchain_khr(&swapchain_create_info, None) .unwrap(); - let pool_create_info = vk::CommandPoolCreateInfo { - s_type: vk::StructureType::COMMAND_POOL_CREATE_INFO, - p_next: ptr::null(), - flags: vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER, - queue_family_index: queue_family_index, - }; + + let pool_create_info = vk::CommandPoolCreateInfo::builder() + .queue_family_index(queue_family_index) + .flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER) + .build(); + + let pool = device.create_command_pool(&pool_create_info, None).unwrap(); - let command_buffer_allocate_info = vk::CommandBufferAllocateInfo { - s_type: vk::StructureType::COMMAND_BUFFER_ALLOCATE_INFO, - p_next: ptr::null(), - command_buffer_count: 2, - command_pool: pool, - level: vk::CommandBufferLevel::PRIMARY, - }; + + let command_buffer_allocate_info = vk::CommandBufferAllocateInfo::builder() + .command_buffer_count(2) + .command_pool(pool) + .level(vk::CommandBufferLevel::PRIMARY) + .build(); + let command_buffers = device .allocate_command_buffers(&command_buffer_allocate_info) .unwrap(); @@ -532,51 +533,37 @@ impl ExampleBase { let present_image_views: Vec = present_images .iter() .map(|&image| { - let create_view_info = vk::ImageViewCreateInfo { - s_type: vk::StructureType::IMAGE_VIEW_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - view_type: vk::ImageViewType::TYPE_2D, - format: surface_format.format, - components: vk::ComponentMapping { - r: vk::ComponentSwizzle::R, - g: vk::ComponentSwizzle::G, - b: vk::ComponentSwizzle::B, - a: vk::ComponentSwizzle::A, - }, - subresource_range: vk::ImageSubresourceRange { - aspect_mask: vk::ImageAspectFlags::COLOR, - base_mip_level: 0, - level_count: 1, - base_array_layer: 0, - layer_count: 1, - }, - image: image, - }; + let create_view_info = vk::ImageViewCreateInfo::builder() + .subresource_range( + vk::ImageSubresourceRange::builder() + .aspect_mask(vk::ImageAspectFlags::COLOR) + .level_count(1) + .layer_count(1) + .build() + ) + .image(image) + .format(surface_format.format) + .view_type(vk::ImageViewType::TYPE_2D) + .build(); + device.create_image_view(&create_view_info, None).unwrap() }).collect(); let device_memory_properties = instance.get_physical_device_memory_properties(pdevice); - let depth_image_create_info = vk::ImageCreateInfo { - s_type: vk::StructureType::IMAGE_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - image_type: vk::ImageType::TYPE_2D, - format: vk::Format::D16_UNORM, - extent: vk::Extent3D { + + let depth_image_create_info = vk::ImageCreateInfo::builder() + .image_type(vk::ImageType::TYPE_2D) + .format(vk::Format::D16_UNORM) + .extent(vk::Extent3D { width: surface_resolution.width, height: surface_resolution.height, - depth: 1, - }, - mip_levels: 1, - array_layers: 1, - samples: vk::SampleCountFlags::TYPE_1, - tiling: vk::ImageTiling::OPTIMAL, - usage: vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT, - sharing_mode: vk::SharingMode::EXCLUSIVE, - queue_family_index_count: 0, - p_queue_family_indices: ptr::null(), - initial_layout: vk::ImageLayout::UNDEFINED, - }; + depth: 1 + }) + .mip_levels(1) + .array_layers(1) + .samples(vk::SampleCountFlags::TYPE_1) + .usage(vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT) + .build(); + let depth_image = device.create_image(&depth_image_create_info, None).unwrap(); let depth_image_memory_req = device.get_image_memory_requirements(depth_image); let depth_image_memory_index = find_memorytype_index( @@ -585,45 +572,45 @@ impl ExampleBase { vk::MemoryPropertyFlags::DEVICE_LOCAL, ).expect("Unable to find suitable memory index for depth image."); - let depth_image_allocate_info = vk::MemoryAllocateInfo { - s_type: vk::StructureType::MEMORY_ALLOCATE_INFO, - p_next: ptr::null(), - allocation_size: depth_image_memory_req.size, - memory_type_index: depth_image_memory_index, - }; + + let depth_image_allocate_info = vk::MemoryAllocateInfo::builder() + .allocation_size(depth_image_memory_req.size) + .memory_type_index(depth_image_memory_index) + .build(); + let depth_image_memory = device .allocate_memory(&depth_image_allocate_info, None) .unwrap(); + device .bind_image_memory(depth_image, depth_image_memory, 0) .expect("Unable to bind depth image memory"); + record_submit_commandbuffer( &device, setup_command_buffer, present_queue, - &[vk::PipelineStageFlags::BOTTOM_OF_PIPE], + &[], &[], &[], |device, setup_command_buffer| { - let layout_transition_barrier = vk::ImageMemoryBarrier { - s_type: vk::StructureType::IMAGE_MEMORY_BARRIER, - p_next: ptr::null(), - src_access_mask: Default::default(), - dst_access_mask: vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_READ - | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_WRITE, - old_layout: vk::ImageLayout::UNDEFINED, - new_layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL, - src_queue_family_index: vk::QUEUE_FAMILY_IGNORED, - dst_queue_family_index: vk::QUEUE_FAMILY_IGNORED, - image: depth_image, - subresource_range: vk::ImageSubresourceRange { - aspect_mask: vk::ImageAspectFlags::DEPTH, - base_mip_level: 0, - level_count: 1, - base_array_layer: 0, - layer_count: 1, - }, - }; + let layout_transition_barrier = vk::ImageMemoryBarrier::builder() + .image(depth_image) + .dst_access_mask( + vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_READ + | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_WRITE + ) + .new_layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL) + .old_layout(vk::ImageLayout::UNDEFINED) + .subresource_range( + vk::ImageSubresourceRange::builder() + .aspect_mask(vk::ImageAspectFlags::DEPTH) + .layer_count(1) + .level_count(1) + .build() + ) + .build(); + device.cmd_pipeline_barrier( setup_command_buffer, vk::PipelineStageFlags::BOTTOM_OF_PIPE, @@ -635,35 +622,27 @@ impl ExampleBase { ); }, ); - let depth_image_view_info = vk::ImageViewCreateInfo { - s_type: vk::StructureType::IMAGE_VIEW_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - view_type: vk::ImageViewType::TYPE_2D, - format: depth_image_create_info.format, - components: vk::ComponentMapping { - r: vk::ComponentSwizzle::IDENTITY, - g: vk::ComponentSwizzle::IDENTITY, - b: vk::ComponentSwizzle::IDENTITY, - a: vk::ComponentSwizzle::IDENTITY, - }, - subresource_range: vk::ImageSubresourceRange { - aspect_mask: vk::ImageAspectFlags::DEPTH, - base_mip_level: 0, - level_count: 1, - base_array_layer: 0, - layer_count: 1, - }, - image: depth_image, - }; + + let depth_image_view_info = vk::ImageViewCreateInfo::builder() + .subresource_range( + vk::ImageSubresourceRange::builder() + .aspect_mask(vk::ImageAspectFlags::DEPTH) + .level_count(1) + .layer_count(1) + .build() + ) + .image(depth_image) + .format(depth_image_create_info.format) + .view_type(vk::ImageViewType::TYPE_2D) + .build(); + let depth_image_view = device .create_image_view(&depth_image_view_info, None) .unwrap(); - let semaphore_create_info = vk::SemaphoreCreateInfo { - s_type: vk::StructureType::SEMAPHORE_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - }; + + + let semaphore_create_info = vk::SemaphoreCreateInfo::default(); + let present_complete_semaphore = device .create_semaphore(&semaphore_create_info, None) .unwrap(); From dbbdc8c3c28080b2c14339a3718903836a1fe361 Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 6 Dec 2018 09:33:22 -0600 Subject: [PATCH 02/19] using builders and defaults in triangle example --- examples/src/bin/triangle.rs | 268 +++++++++++------------------------ 1 file changed, 80 insertions(+), 188 deletions(-) diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index 213127c..39590bf 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -25,25 +25,18 @@ fn main() { let renderpass_attachments = [ vk::AttachmentDescription { format: base.surface_format.format, - flags: vk::AttachmentDescriptionFlags::empty(), 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, + .. Default::default() }, vk::AttachmentDescription { format: vk::Format::D16_UNORM, - flags: vk::AttachmentDescriptionFlags::empty(), 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, + .. Default::default() }, ]; let color_attachment_ref = vk::AttachmentReference { @@ -55,38 +48,24 @@ fn main() { layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL, }; let dependency = vk::SubpassDependency { - dependency_flags: Default::default(), src_subpass: vk::SUBPASS_EXTERNAL, - dst_subpass: Default::default(), src_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, - src_access_mask: Default::default(), dst_access_mask: vk::AccessFlags::COLOR_ATTACHMENT_READ | vk::AccessFlags::COLOR_ATTACHMENT_WRITE, dst_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, + .. Default::default() }; - 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, - input_attachment_count: 0, - p_input_attachments: ptr::null(), - p_resolve_attachments: ptr::null(), - preserve_attachment_count: 0, - p_preserve_attachments: ptr::null(), - }; - 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 subpass = vk::SubpassDescription::builder() + .color_attachments( &[color_attachment_ref]) + .depth_stencil_attachment( &depth_attachment_ref) + .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS) + .build(); + + let renderpass_create_info = vk::RenderPassCreateInfo::builder() + .attachments(&renderpass_attachments) + .subpasses(&[subpass]) + .dependencies(&[dependency]) + .build(); let renderpass = base .device .create_render_pass(&renderpass_create_info, None) @@ -96,31 +75,23 @@ fn main() { .iter() .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::FRAMEBUFFER_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - render_pass: renderpass, - attachment_count: framebuffer_attachments.len() as u32, - p_attachments: framebuffer_attachments.as_ptr(), - width: base.surface_resolution.width, - height: base.surface_resolution.height, - layers: 1, - }; + let frame_buffer_create_info = vk::FramebufferCreateInfo::builder() + .render_pass(renderpass) + .attachments(&framebuffer_attachments) + .width( base.surface_resolution.width) + .height( base.surface_resolution.height) + .layers( 1) + .build(); base.device .create_framebuffer(&frame_buffer_create_info, None) .unwrap() }).collect(); let index_buffer_data = [0u32, 1, 2]; 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, usage: vk::BufferUsageFlags::INDEX_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - queue_family_index_count: 0, - p_queue_family_indices: ptr::null(), + .. Default::default() }; 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); @@ -130,10 +101,9 @@ fn main() { vk::MemoryPropertyFlags::HOST_VISIBLE, ).expect("Unable to find suitable memorytype for the index buffer."); let index_allocate_info = vk::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, + .. Default::default() }; let index_buffer_memory = base .device @@ -159,14 +129,10 @@ fn main() { .unwrap(); 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::() as u64, usage: vk::BufferUsageFlags::VERTEX_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - queue_family_index_count: 0, - p_queue_family_indices: ptr::null(), + .. Default::default() }; let vertex_input_buffer = base .device @@ -182,10 +148,9 @@ fn main() { ).expect("Unable to find suitable memorytype for the vertex buffer."); 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, memory_type_index: vertex_input_buffer_memory_index, + .. Default::default() }; let vertex_input_buffer_memory = base .device @@ -232,21 +197,18 @@ fn main() { .bytes() .filter_map(|byte| byte.ok()) .collect(); - let vertex_shader_info = vk::ShaderModuleCreateInfo { - s_type: vk::StructureType::SHADER_MODULE_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - code_size: vertex_bytes.len(), - p_code: vertex_bytes.as_ptr() as *const u32, - }; + let vertex_bytes = examples::bytes_to_u32_vec(vertex_bytes.iter().cloned()); + + let vertex_shader_info = vk::ShaderModuleCreateInfo::builder() + .code(&vertex_bytes) + .build(); + let frag_bytes: Vec = frag_spv_file.bytes().filter_map(|byte| byte.ok()).collect(); - let frag_shader_info = vk::ShaderModuleCreateInfo { - s_type: vk::StructureType::SHADER_MODULE_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - code_size: frag_bytes.len(), - p_code: frag_bytes.as_ptr() as *const u32, - }; + let frag_bytes = examples::bytes_to_u32_vec(frag_bytes.iter().cloned()); + + let frag_shader_info = vk::ShaderModuleCreateInfo::builder() + .code(&frag_bytes) + .build(); let vertex_shader_module = base .device .create_shader_module(&vertex_shader_info, None) @@ -257,15 +219,7 @@ fn main() { .create_shader_module(&frag_shader_info, None) .expect("Fragment shader module error"); - let layout_create_info = vk::PipelineLayoutCreateInfo { - 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 layout_create_info = vk::PipelineLayoutCreateInfo::default(); let pipeline_layout = base .device @@ -275,22 +229,16 @@ fn main() { let shader_entry_name = CString::new("main").unwrap(); let shader_stage_create_infos = [ vk::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::ShaderStageFlags::VERTEX, + .. Default::default() }, vk::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::ShaderStageFlags::FRAGMENT, + .. Default::default() }, ]; let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription { @@ -312,21 +260,14 @@ fn main() { offset: offset_of!(Vertex, color) as u32, }, ]; - let vertex_input_state_info = vk::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, - 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_state_info = vk::PipelineVertexInputStateCreateInfo::builder() + .vertex_attribute_descriptions(&vertex_input_attribute_descriptions) + .vertex_binding_descriptions(&vertex_input_binding_descriptions) + .build(); + 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, + .. Default::default() }; let viewports = [vk::Viewport { x: 0.0, @@ -340,63 +281,35 @@ fn main() { offset: vk::Offset2D { x: 0, y: 0 }, extent: base.surface_resolution.clone(), }]; - let viewport_state_info = vk::PipelineViewportStateCreateInfo { - s_type: vk::StructureType::PIPELINE_VIEWPORT_STATE_CREATE_INFO, - p_next: ptr::null(), - flags: Default::default(), - scissor_count: scissors.len() as u32, - p_scissors: scissors.as_ptr(), - viewport_count: viewports.len() as u32, - p_viewports: viewports.as_ptr(), - }; + let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder() + .scissors(&scissors) + .viewports(&viewports) + .build(); 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, line_width: 1.0, polygon_mode: vk::PolygonMode::FILL, - rasterizer_discard_enable: 0, + .. Default::default() }; 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, - sample_shading_enable: 0, - min_sample_shading: 0.0, - p_sample_mask: ptr::null(), - alpha_to_one_enable: 0, - alpha_to_coverage_enable: 0, + .. Default::default() }; 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, - compare_mask: 0, - write_mask: 0, - reference: 0, + .. Default::default() }; 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_write_enable: 1, depth_compare_op: vk::CompareOp::LESS_OR_EQUAL, - depth_bounds_test_enable: 0, - stencil_test_enable: 0, front: noop_stencil_state.clone(), back: noop_stencil_state.clone(), max_depth_bounds: 1.0, - min_depth_bounds: 0.0, + .. Default::default() }; let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState { blend_enable: 0, @@ -408,45 +321,29 @@ fn main() { alpha_blend_op: vk::BlendOp::ADD, color_write_mask: vk::ColorComponentFlags::all(), }]; - let color_blend_state = vk::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, - 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 color_blend_state = vk::PipelineColorBlendStateCreateInfo::builder() + .logic_op( vk::LogicOp::CLEAR) + .attachments(&color_blend_attachment_states) + .build(); + let dynamic_state = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR]; - let dynamic_state_info = vk::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::GRAPHICS_PIPELINE_CREATE_INFO, - p_next: ptr::null(), - flags: vk::PipelineCreateFlags::empty(), - stage_count: shader_stage_create_infos.len() as u32, - p_stages: shader_stage_create_infos.as_ptr(), - p_vertex_input_state: &vertex_input_state_info, - p_input_assembly_state: &vertex_input_assembly_state_info, - p_tessellation_state: ptr::null(), - p_viewport_state: &viewport_state_info, - 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 dynamic_state_info = vk::PipelineDynamicStateCreateInfo::builder() + .dynamic_states(&dynamic_state) + .build(); + + let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder() + .stages(&shader_stage_create_infos) + .vertex_input_state( &vertex_input_state_info) + .input_assembly_state( &vertex_input_assembly_state_info) + .viewport_state( &viewport_state_info) + .rasterization_state( &rasterization_info) + .multisample_state( &multisample_state_info) + .depth_stencil_state( &depth_state_info) + .color_blend_state( &color_blend_state) + .dynamic_state( &dynamic_state_info) + .layout( pipeline_layout) + .render_pass( renderpass) + .build(); let graphics_pipelines = base .device .create_graphics_pipelines(vk::PipelineCache::null(), &[graphic_pipeline_info], None) @@ -535,16 +432,11 @@ fn main() { }, ); //let mut present_info_err = mem::uninitialized(); - let present_info = vk::PresentInfoKHR { - s_type: vk::StructureType::PRESENT_INFO_KHR, - p_next: ptr::null(), - wait_semaphore_count: 1, - p_wait_semaphores: &base.rendering_complete_semaphore, - swapchain_count: 1, - p_swapchains: &base.swapchain, - p_image_indices: &present_index, - p_results: ptr::null_mut(), - }; + let present_info = vk::PresentInfoKHR::builder() + .wait_semaphores( &[base.rendering_complete_semaphore]) + .swapchains( &[base.swapchain]) + .image_indices( &[present_index]) + .build(); base.swapchain_loader .queue_present_khr(base.present_queue, &present_info) .unwrap(); From 2855192145c16f2cb0cc9bafeb84ff5e47999748 Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 6 Dec 2018 12:58:31 -0600 Subject: [PATCH 03/19] added std::ptr back in import to satisfy build checks --- examples/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 4570f2a..60b38ba 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -39,7 +39,7 @@ use std::default::Default; use std::ffi::{CStr, CString}; use std::ops::Drop; use std::os::raw::{c_char, c_void}; - +use std::ptr; // Simple offset_of macro akin to C++ offsetof #[macro_export] From 046989ad75f4371ff72ae64b598559a99141b03b Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 6 Dec 2018 13:18:37 -0600 Subject: [PATCH 04/19] reformatted with fmt --- examples/src/bin/texture.rs | 110 ++++++++++++++++------------------- examples/src/bin/triangle.rs | 66 ++++++++++----------- examples/src/lib.rs | 65 +++++++++------------ 3 files changed, 109 insertions(+), 132 deletions(-) diff --git a/examples/src/bin/texture.rs b/examples/src/bin/texture.rs index 331e875..1362dcd 100644 --- a/examples/src/bin/texture.rs +++ b/examples/src/bin/texture.rs @@ -41,7 +41,7 @@ fn main() { load_op: vk::AttachmentLoadOp::CLEAR, store_op: vk::AttachmentStoreOp::STORE, final_layout: vk::ImageLayout::PRESENT_SRC_KHR, - .. Default::default() + ..Default::default() }, vk::AttachmentDescription { format: vk::Format::D16_UNORM, @@ -49,11 +49,10 @@ fn main() { load_op: vk::AttachmentLoadOp::CLEAR, initial_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 { attachment: 0, layout: vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL, @@ -69,21 +68,18 @@ fn main() { dst_access_mask: vk::AccessFlags::COLOR_ATTACHMENT_READ | vk::AccessFlags::COLOR_ATTACHMENT_WRITE, dst_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, - .. Default::default() + ..Default::default() }; - - let subpass = vk::SubpassDescription::builder() .color_attachments(&[color_attachment_ref]) .depth_stencil_attachment(&depth_attachment_ref) .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS) .build(); - let renderpass_create_info = vk::RenderPassCreateInfo::builder() .attachments(&renderpass_attachments) - .subpasses( &[subpass]) + .subpasses(&[subpass]) .dependencies(&[dependency]) .build(); @@ -100,9 +96,9 @@ fn main() { let frame_buffer_create_info = vk::FramebufferCreateInfo::builder() .render_pass(renderpass) .attachments(&framebuffer_attachments) - .width( base.surface_resolution.width) - .height( base.surface_resolution.height) - .layers( 1) + .width(base.surface_resolution.width) + .height(base.surface_resolution.height) + .layers(1) .build(); base.device @@ -114,7 +110,7 @@ fn main() { size: std::mem::size_of_val(&index_buffer_data) as u64, usage: vk::BufferUsageFlags::INDEX_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - .. Default::default() + ..Default::default() }; 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); @@ -126,7 +122,7 @@ fn main() { let index_allocate_info = vk::MemoryAllocateInfo { allocation_size: index_buffer_memory_req.size, memory_type_index: index_buffer_memory_index, - .. Default::default() + ..Default::default() }; let index_buffer_memory = base .device @@ -173,7 +169,7 @@ fn main() { size: std::mem::size_of_val(&vertices) as u64, usage: vk::BufferUsageFlags::VERTEX_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - .. Default::default() + ..Default::default() }; let vertex_input_buffer = base .device @@ -191,7 +187,7 @@ fn main() { let vertex_buffer_allocate_info = vk::MemoryAllocateInfo { allocation_size: vertex_input_buffer_memory_req.size, memory_type_index: vertex_input_buffer_memory_index, - .. Default::default() + ..Default::default() }; let vertex_input_buffer_memory = base .device @@ -227,7 +223,7 @@ fn main() { size: std::mem::size_of_val(&uniform_color_buffer_data) as u64, usage: vk::BufferUsageFlags::UNIFORM_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - .. Default::default() + ..Default::default() }; let uniform_color_buffer = base .device @@ -245,7 +241,7 @@ fn main() { let uniform_color_buffer_allocate_info = vk::MemoryAllocateInfo { allocation_size: uniform_color_buffer_memory_req.size, memory_type_index: uniform_color_buffer_memory_index, - .. Default::default() + ..Default::default() }; let uniform_color_buffer_memory = base .device @@ -277,7 +273,7 @@ fn main() { size: (std::mem::size_of::() * image_data.len()) as u64, usage: vk::BufferUsageFlags::TRANSFER_SRC, sharing_mode: vk::SharingMode::EXCLUSIVE, - .. Default::default() + ..Default::default() }; let image_buffer = base.device.create_buffer(&image_buffer_info, None).unwrap(); let image_buffer_memory_req = base.device.get_buffer_memory_requirements(image_buffer); @@ -290,7 +286,7 @@ fn main() { let image_buffer_allocate_info = vk::MemoryAllocateInfo { allocation_size: image_buffer_memory_req.size, memory_type_index: image_buffer_memory_index, - .. Default::default() + ..Default::default() }; let image_buffer_memory = base .device @@ -329,7 +325,7 @@ fn main() { tiling: vk::ImageTiling::OPTIMAL, usage: vk::ImageUsageFlags::TRANSFER_DST | vk::ImageUsageFlags::SAMPLED, sharing_mode: vk::SharingMode::EXCLUSIVE, - .. Default::default() + ..Default::default() }; let texture_image = base .device @@ -345,7 +341,7 @@ fn main() { let texture_allocate_info = vk::MemoryAllocateInfo { allocation_size: texture_memory_req.size, memory_type_index: texture_memory_index, - .. Default::default() + ..Default::default() }; let texture_memory = base .device @@ -371,9 +367,9 @@ fn main() { aspect_mask: vk::ImageAspectFlags::COLOR, level_count: 1, layer_count: 1, - .. Default::default() + ..Default::default() }, - .. Default::default() + ..Default::default() }; device.cmd_pipeline_barrier( texture_command_buffer, @@ -384,22 +380,17 @@ fn main() { &[], &[texture_barrier], ); - let buffer_copy_regions = [ - vk::BufferImageCopy::builder() - .image_subresource( - vk::ImageSubresourceLayers::builder() - .aspect_mask( vk::ImageAspectFlags::COLOR) - .layer_count(1) - .build() - ) - .image_extent( - vk::Extent3D { - width: image_dimensions.0, - height: image_dimensions.1, - depth: 1, - }) - .build() - ]; + let buffer_copy_regions = [vk::BufferImageCopy::builder() + .image_subresource( + vk::ImageSubresourceLayers::builder() + .aspect_mask(vk::ImageAspectFlags::COLOR) + .layer_count(1) + .build(), + ).image_extent(vk::Extent3D { + width: image_dimensions.0, + height: image_dimensions.1, + depth: 1, + }).build()]; device.cmd_copy_buffer_to_image( texture_command_buffer, image_buffer, @@ -417,9 +408,9 @@ fn main() { aspect_mask: vk::ImageAspectFlags::COLOR, level_count: 1, layer_count: 1, - .. Default::default() + ..Default::default() }, - .. Default::default() + ..Default::default() }; device.cmd_pipeline_barrier( texture_command_buffer, @@ -441,7 +432,7 @@ fn main() { address_mode_v: vk::SamplerAddressMode::MIRRORED_REPEAT, address_mode_w: vk::SamplerAddressMode::MIRRORED_REPEAT, max_anisotropy: 1.0, - .. Default::default() + ..Default::default() }; let sampler = base.device.create_sampler(&sampler_info, None).unwrap(); @@ -459,10 +450,10 @@ fn main() { aspect_mask: vk::ImageAspectFlags::COLOR, level_count: 1, layer_count: 1, - .. Default::default() + ..Default::default() }, image: texture_image, - .. Default::default() + ..Default::default() }; let tex_image_view = base .device @@ -491,14 +482,14 @@ fn main() { descriptor_type: vk::DescriptorType::UNIFORM_BUFFER, descriptor_count: 1, stage_flags: vk::ShaderStageFlags::FRAGMENT, - .. Default::default() + ..Default::default() }, vk::DescriptorSetLayoutBinding { binding: 1, descriptor_type: vk::DescriptorType::COMBINED_IMAGE_SAMPLER, descriptor_count: 1, stage_flags: vk::ShaderStageFlags::FRAGMENT, - .. Default::default() + ..Default::default() }, ]; let descriptor_info = vk::DescriptorSetLayoutCreateInfo::builder() @@ -536,7 +527,7 @@ fn main() { descriptor_count: 1, descriptor_type: vk::DescriptorType::UNIFORM_BUFFER, p_buffer_info: &uniform_color_buffer_descriptor, - .. Default::default() + ..Default::default() }, vk::WriteDescriptorSet { dst_set: descriptor_sets[0], @@ -544,7 +535,7 @@ fn main() { descriptor_count: 1, descriptor_type: vk::DescriptorType::COMBINED_IMAGE_SAMPLER, p_image_info: &tex_descriptor, - .. Default::default() + ..Default::default() }, ]; base.device.update_descriptor_sets(&write_desc_sets, &[]); @@ -564,9 +555,7 @@ fn main() { .code(&vertex_bytes) .build(); - let frag_bytes: Vec = frag_spv_file.bytes() - .filter_map(|byte| byte.ok()) - .collect(); + let frag_bytes: Vec = frag_spv_file.bytes().filter_map(|byte| byte.ok()).collect(); let frag_bytes = examples::bytes_to_u32_vec(frag_bytes.iter().cloned()); @@ -599,13 +588,13 @@ fn main() { module: vertex_shader_module, p_name: shader_entry_name.as_ptr(), stage: vk::ShaderStageFlags::VERTEX, - .. Default::default() + ..Default::default() }, vk::PipelineShaderStageCreateInfo { module: fragment_shader_module, p_name: shader_entry_name.as_ptr(), stage: vk::ShaderStageFlags::FRAGMENT, - .. Default::default() + ..Default::default() }, ]; let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription { @@ -658,11 +647,11 @@ fn main() { front_face: vk::FrontFace::COUNTER_CLOCKWISE, line_width: 1.0, polygon_mode: vk::PolygonMode::FILL, - .. Default::default() + ..Default::default() }; let multisample_state_info = vk::PipelineMultisampleStateCreateInfo::builder() - .rasterization_samples( vk::SampleCountFlags::TYPE_1) + .rasterization_samples(vk::SampleCountFlags::TYPE_1) .build(); let noop_stencil_state = vk::StencilOpState { @@ -670,7 +659,7 @@ fn main() { pass_op: vk::StencilOp::KEEP, depth_fail_op: vk::StencilOp::KEEP, compare_op: vk::CompareOp::ALWAYS, - .. Default::default() + ..Default::default() }; let depth_state_info = vk::PipelineDepthStencilStateCreateInfo { depth_test_enable: 1, @@ -679,7 +668,7 @@ fn main() { front: noop_stencil_state.clone(), back: noop_stencil_state.clone(), max_depth_bounds: 1.0, - .. Default::default() + ..Default::default() }; let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState { @@ -752,8 +741,7 @@ fn main() { .render_area(vk::Rect2D { offset: vk::Offset2D { x: 0, y: 0 }, extent: base.surface_resolution.clone(), - }) - .clear_values(&clear_values) + }).clear_values(&clear_values) .build(); record_submit_commandbuffer( @@ -813,8 +801,8 @@ fn main() { let present_info = vk::PresentInfoKHR::builder() .wait_semaphores(&[base.rendering_complete_semaphore]) - .swapchains( &[base.swapchain]) - .image_indices( &[present_index]) + .swapchains(&[base.swapchain]) + .image_indices(&[present_index]) .build(); base.swapchain_loader diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index 39590bf..5e80c3b 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -29,14 +29,14 @@ fn main() { load_op: vk::AttachmentLoadOp::CLEAR, store_op: vk::AttachmentStoreOp::STORE, final_layout: vk::ImageLayout::PRESENT_SRC_KHR, - .. Default::default() + ..Default::default() }, vk::AttachmentDescription { format: vk::Format::D16_UNORM, samples: vk::SampleCountFlags::TYPE_1, load_op: vk::AttachmentLoadOp::CLEAR, final_layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL, - .. Default::default() + ..Default::default() }, ]; let color_attachment_ref = vk::AttachmentReference { @@ -53,11 +53,11 @@ fn main() { dst_access_mask: vk::AccessFlags::COLOR_ATTACHMENT_READ | vk::AccessFlags::COLOR_ATTACHMENT_WRITE, dst_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, - .. Default::default() + ..Default::default() }; let subpass = vk::SubpassDescription::builder() - .color_attachments( &[color_attachment_ref]) - .depth_stencil_attachment( &depth_attachment_ref) + .color_attachments(&[color_attachment_ref]) + .depth_stencil_attachment(&depth_attachment_ref) .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS) .build(); @@ -78,9 +78,9 @@ fn main() { let frame_buffer_create_info = vk::FramebufferCreateInfo::builder() .render_pass(renderpass) .attachments(&framebuffer_attachments) - .width( base.surface_resolution.width) - .height( base.surface_resolution.height) - .layers( 1) + .width(base.surface_resolution.width) + .height(base.surface_resolution.height) + .layers(1) .build(); base.device .create_framebuffer(&frame_buffer_create_info, None) @@ -91,7 +91,7 @@ fn main() { size: std::mem::size_of_val(&index_buffer_data) as u64, usage: vk::BufferUsageFlags::INDEX_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - .. Default::default() + ..Default::default() }; 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); @@ -103,7 +103,7 @@ fn main() { let index_allocate_info = vk::MemoryAllocateInfo { allocation_size: index_buffer_memory_req.size, memory_type_index: index_buffer_memory_index, - .. Default::default() + ..Default::default() }; let index_buffer_memory = base .device @@ -132,7 +132,7 @@ fn main() { size: 3 * std::mem::size_of::() as u64, usage: vk::BufferUsageFlags::VERTEX_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - .. Default::default() + ..Default::default() }; let vertex_input_buffer = base .device @@ -150,7 +150,7 @@ fn main() { let vertex_buffer_allocate_info = vk::MemoryAllocateInfo { allocation_size: vertex_input_buffer_memory_req.size, memory_type_index: vertex_input_buffer_memory_index, - .. Default::default() + ..Default::default() }; let vertex_input_buffer_memory = base .device @@ -232,13 +232,13 @@ fn main() { module: vertex_shader_module, p_name: shader_entry_name.as_ptr(), stage: vk::ShaderStageFlags::VERTEX, - .. Default::default() + ..Default::default() }, vk::PipelineShaderStageCreateInfo { module: fragment_shader_module, p_name: shader_entry_name.as_ptr(), stage: vk::ShaderStageFlags::FRAGMENT, - .. Default::default() + ..Default::default() }, ]; let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription { @@ -267,7 +267,7 @@ fn main() { let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo { topology: vk::PrimitiveTopology::TRIANGLE_LIST, - .. Default::default() + ..Default::default() }; let viewports = [vk::Viewport { x: 0.0, @@ -289,18 +289,18 @@ fn main() { front_face: vk::FrontFace::COUNTER_CLOCKWISE, line_width: 1.0, polygon_mode: vk::PolygonMode::FILL, - .. Default::default() + ..Default::default() }; let multisample_state_info = vk::PipelineMultisampleStateCreateInfo { rasterization_samples: vk::SampleCountFlags::TYPE_1, - .. Default::default() + ..Default::default() }; 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, - .. Default::default() + ..Default::default() }; let depth_state_info = vk::PipelineDepthStencilStateCreateInfo { depth_test_enable: 1, @@ -309,7 +309,7 @@ fn main() { front: noop_stencil_state.clone(), back: noop_stencil_state.clone(), max_depth_bounds: 1.0, - .. Default::default() + ..Default::default() }; let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState { blend_enable: 0, @@ -322,7 +322,7 @@ fn main() { color_write_mask: vk::ColorComponentFlags::all(), }]; let color_blend_state = vk::PipelineColorBlendStateCreateInfo::builder() - .logic_op( vk::LogicOp::CLEAR) + .logic_op(vk::LogicOp::CLEAR) .attachments(&color_blend_attachment_states) .build(); @@ -333,16 +333,16 @@ fn main() { let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder() .stages(&shader_stage_create_infos) - .vertex_input_state( &vertex_input_state_info) - .input_assembly_state( &vertex_input_assembly_state_info) - .viewport_state( &viewport_state_info) - .rasterization_state( &rasterization_info) - .multisample_state( &multisample_state_info) - .depth_stencil_state( &depth_state_info) - .color_blend_state( &color_blend_state) - .dynamic_state( &dynamic_state_info) - .layout( pipeline_layout) - .render_pass( renderpass) + .vertex_input_state(&vertex_input_state_info) + .input_assembly_state(&vertex_input_assembly_state_info) + .viewport_state(&viewport_state_info) + .rasterization_state(&rasterization_info) + .multisample_state(&multisample_state_info) + .depth_stencil_state(&depth_state_info) + .color_blend_state(&color_blend_state) + .dynamic_state(&dynamic_state_info) + .layout(pipeline_layout) + .render_pass(renderpass) .build(); let graphics_pipelines = base .device @@ -433,9 +433,9 @@ fn main() { ); //let mut present_info_err = mem::uninitialized(); let present_info = vk::PresentInfoKHR::builder() - .wait_semaphores( &[base.rendering_complete_semaphore]) - .swapchains( &[base.swapchain]) - .image_indices( &[present_index]) + .wait_semaphores(&[base.rendering_complete_semaphore]) + .swapchains(&[base.swapchain]) + .image_indices(&[present_index]) .build(); base.swapchain_loader .queue_present_khr(base.present_queue, &present_info) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 60b38ba..7b150e6 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -54,33 +54,31 @@ macro_rules! offset_of { } fn u32_from_bytes(bytes: &[u8; 4]) -> u32 { - unsafe { - std::mem::transmute(*bytes) - } + unsafe { std::mem::transmute(*bytes) } } -pub fn bytes_to_u32_vec(bytes:B) -> Vec -where B: Iterator +pub fn bytes_to_u32_vec(bytes: B) -> Vec +where + B: Iterator, { let mut output = vec![]; - let mut buffer: [u8;4] = [0,0,0,0]; - for (i,b) in bytes.enumerate() { + let mut buffer: [u8; 4] = [0, 0, 0, 0]; + for (i, b) in bytes.enumerate() { match i % 4 { 3 => { buffer[3] = b; output.push(u32_from_bytes(&buffer)); - buffer = [0,0,0,0]; - }, - idx => buffer[idx] = b + buffer = [0, 0, 0, 0]; + } + idx => buffer[idx] = b, } } - if buffer != [0,0,0,0] { + if buffer != [0, 0, 0, 0] { output.push(u32_from_bytes(&buffer)); } output } - pub fn record_submit_commandbuffer( device: &D, command_buffer: vk::CommandBuffer, @@ -361,17 +359,17 @@ impl ExampleBase { let extension_names_raw = extension_names(); let appinfo = vk::ApplicationInfo::builder() - .application_name(&app_name ) + .application_name(&app_name) .application_version(0) .engine_name(&app_name) .engine_version(0) - .api_version(vk_make_version!(1,0,36)) + .api_version(vk_make_version!(1, 0, 36)) .build(); let create_info = vk::InstanceCreateInfo::builder() .application_info(&appinfo) .enabled_layer_names(&layers_names_raw) - .enabled_extension_names(&extension_names_raw ) + .enabled_extension_names(&extension_names_raw) .build(); let instance: Instance = entry @@ -383,9 +381,8 @@ impl ExampleBase { .flags( vk::DebugReportFlagsEXT::ERROR | vk::DebugReportFlagsEXT::WARNING - | vk::DebugReportFlagsEXT::PERFORMANCE_WARNING - ) - .build(); + | vk::DebugReportFlagsEXT::PERFORMANCE_WARNING, + ).build(); let debug_report_loader = DebugReport::new(&entry, &instance); let debug_call_back = debug_report_loader @@ -428,7 +425,7 @@ impl ExampleBase { let priorities = [1.0]; let queue_info = vk::DeviceQueueCreateInfo::builder() - .queue_family_index(queue_family_index as u32 ) + .queue_family_index(queue_family_index as u32) .queue_priorities(&priorities) .build(); @@ -500,7 +497,7 @@ impl ExampleBase { .pre_transform(pre_transform) .composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE) .present_mode(present_mode) -// .clipped(true) + // .clipped(true) .image_array_layers(1) .build(); let swapchain = swapchain_loader @@ -512,7 +509,6 @@ impl ExampleBase { .flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER) .build(); - let pool = device.create_command_pool(&pool_create_info, None).unwrap(); let command_buffer_allocate_info = vk::CommandBufferAllocateInfo::builder() @@ -539,9 +535,8 @@ impl ExampleBase { .aspect_mask(vk::ImageAspectFlags::COLOR) .level_count(1) .layer_count(1) - .build() - ) - .image(image) + .build(), + ).image(image) .format(surface_format.format) .view_type(vk::ImageViewType::TYPE_2D) .build(); @@ -556,9 +551,8 @@ impl ExampleBase { .extent(vk::Extent3D { width: surface_resolution.width, height: surface_resolution.height, - depth: 1 - }) - .mip_levels(1) + depth: 1, + }).mip_levels(1) .array_layers(1) .samples(vk::SampleCountFlags::TYPE_1) .usage(vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT) @@ -572,7 +566,6 @@ impl ExampleBase { vk::MemoryPropertyFlags::DEVICE_LOCAL, ).expect("Unable to find suitable memory index for depth image."); - let depth_image_allocate_info = vk::MemoryAllocateInfo::builder() .allocation_size(depth_image_memory_req.size) .memory_type_index(depth_image_memory_index) @@ -598,18 +591,16 @@ impl ExampleBase { .image(depth_image) .dst_access_mask( vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_READ - | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_WRITE - ) - .new_layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL) + | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_WRITE, + ).new_layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL) .old_layout(vk::ImageLayout::UNDEFINED) .subresource_range( vk::ImageSubresourceRange::builder() .aspect_mask(vk::ImageAspectFlags::DEPTH) .layer_count(1) .level_count(1) - .build() - ) - .build(); + .build(), + ).build(); device.cmd_pipeline_barrier( setup_command_buffer, @@ -629,9 +620,8 @@ impl ExampleBase { .aspect_mask(vk::ImageAspectFlags::DEPTH) .level_count(1) .layer_count(1) - .build() - ) - .image(depth_image) + .build(), + ).image(depth_image) .format(depth_image_create_info.format) .view_type(vk::ImageViewType::TYPE_2D) .build(); @@ -640,7 +630,6 @@ impl ExampleBase { .create_image_view(&depth_image_view_info, None) .unwrap(); - let semaphore_create_info = vk::SemaphoreCreateInfo::default(); let present_complete_semaphore = device From 2bb85a8d50e125698734701a8e790dd4062132c7 Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 6 Dec 2018 13:36:05 -0600 Subject: [PATCH 05/19] fixed logic bug in bytes_to_u32_vec --- examples/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 7b150e6..7870442 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -63,7 +63,9 @@ where { let mut output = vec![]; let mut buffer: [u8; 4] = [0, 0, 0, 0]; + let mut l: usize = 0; for (i, b) in bytes.enumerate() { + l = i; match i % 4 { 3 => { buffer[3] = b; @@ -73,7 +75,7 @@ where idx => buffer[idx] = b, } } - if buffer != [0, 0, 0, 0] { + if (l+1) % 4 != 0 { output.push(u32_from_bytes(&buffer)); } output From 4858f4bcebc5e7cc3f53dce4f10fa303312499b6 Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 6 Dec 2018 13:40:25 -0600 Subject: [PATCH 06/19] fixed logic error in bytes_to_u32_vec --- examples/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 7870442..6825b2f 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -63,9 +63,9 @@ where { let mut output = vec![]; let mut buffer: [u8; 4] = [0, 0, 0, 0]; - let mut l: usize = 0; + let mut byte_len: usize = 0; for (i, b) in bytes.enumerate() { - l = i; + byte_len = i; match i % 4 { 3 => { buffer[3] = b; @@ -75,7 +75,7 @@ where idx => buffer[idx] = b, } } - if (l+1) % 4 != 0 { + if (byte_len + 1) % 4 != 0 { output.push(u32_from_bytes(&buffer)); } output From cf60417ecc7a873ad3334be4fb6e76584441aee2 Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 6 Dec 2018 16:07:53 -0600 Subject: [PATCH 07/19] added bytes_to_u32_vec function using byteorder crate --- examples/Cargo.toml | 1 + examples/src/bin/texture.rs | 15 ++++----------- examples/src/bin/triangle.rs | 14 +++++--------- examples/src/lib.rs | 20 ++++++++++++++++++++ 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 1ff2abf..7cfb9ec 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -7,6 +7,7 @@ authors = ["maik klein "] winit = "0.16" image = "0.10.4" ash = { path = "../ash" } +byteorder = "1.2.7" [target.'cfg(windows)'.dependencies] winapi = { version = "0.3.4", features = ["windef", "winuser"] } diff --git a/examples/src/bin/texture.rs b/examples/src/bin/texture.rs index f8bc221..6201180 100644 --- a/examples/src/bin/texture.rs +++ b/examples/src/bin/texture.rs @@ -549,19 +549,12 @@ fn main() { .bytes() .filter_map(|byte| byte.ok()) .collect(); - let vertex_bytes = examples::bytes_to_u32_vec(vertex_bytes.iter().cloned()); - - let vertex_shader_info = vk::ShaderModuleCreateInfo::builder() - .code(&vertex_bytes) - .build(); + let vertex_code = bytes_to_u32_vec(&vertex_bytes); + let vertex_shader_info = vk::ShaderModuleCreateInfo::builder().code(&vertex_code); let frag_bytes: Vec = frag_spv_file.bytes().filter_map(|byte| byte.ok()).collect(); - - let frag_bytes = examples::bytes_to_u32_vec(frag_bytes.iter().cloned()); - - let frag_shader_info = vk::ShaderModuleCreateInfo::builder() - .code(&frag_bytes) - .build(); + let frag_code = bytes_to_u32_vec(&frag_bytes); + let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code); let vertex_shader_module = base .device diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index 612f664..7c79c0f 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -197,18 +197,14 @@ fn main() { .bytes() .filter_map(|byte| byte.ok()) .collect(); - let vertex_bytes = examples::bytes_to_u32_vec(vertex_bytes.iter().cloned()); - let vertex_shader_info = vk::ShaderModuleCreateInfo::builder() - .code(&vertex_bytes) - .build(); + let vertex_code = bytes_to_u32_vec(&vertex_bytes); + + let vertex_shader_info = vk::ShaderModuleCreateInfo::builder().code(&vertex_code); let frag_bytes: Vec = frag_spv_file.bytes().filter_map(|byte| byte.ok()).collect(); - let frag_bytes = examples::bytes_to_u32_vec(frag_bytes.iter().cloned()); - - let frag_shader_info = vk::ShaderModuleCreateInfo::builder() - .code(&frag_bytes) - .build(); + let frag_code = bytes_to_u32_vec(&frag_bytes); + let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code); let vertex_shader_module = base .device .create_shader_module(&vertex_shader_info, None) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 4c494cd..643421c 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -1,5 +1,6 @@ #[macro_use] extern crate ash; +extern crate byteorder; #[cfg(target_os = "windows")] extern crate winapi; @@ -35,13 +36,32 @@ use ash::extensions::khr::Win32Surface; use ash::extensions::mvk::MacOSSurface; pub use ash::version::{DeviceV1_0, EntryV1_0, InstanceV1_0}; use ash::{vk, Device, Entry, Instance}; +use byteorder::{LittleEndian, ReadBytesExt}; use std::cell::RefCell; use std::default::Default; use std::ffi::{CStr, CString}; +use std::io::Cursor; use std::ops::Drop; use std::os::raw::{c_char, c_void}; use std::ptr; +pub fn bytes_to_u32_vec(bytes: &[u8]) -> Vec { + let mut output = vec![]; + let mut buffer: [u8; 4] = [0, 0, 0, 0]; + for (i, b) in bytes.iter().enumerate() { + let idx = i % 4; + buffer[idx] = *b; + if idx == 3 { + output.push(Cursor::new(buffer).read_u32::().unwrap()); + buffer = [0, 0, 0, 0]; + } + } + if bytes.len() % 4 != 0 { + output.push(Cursor::new(buffer).read_u32::().unwrap()); + } + output +} + // Simple offset_of macro akin to C++ offsetof #[macro_export] macro_rules! offset_of { From 8c2424c836a7000a05b12f3107099cd053afa0cc Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 6 Dec 2018 21:53:53 -0600 Subject: [PATCH 08/19] refactored examples/src/lib.rs to use builders --- examples/src/lib.rs | 104 +++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 50 deletions(-) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 643421c..51be5b6 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -117,8 +117,8 @@ pub fn record_submit_commandbuffer( let x11_display = window.get_xlib_display().unwrap(); let x11_window = window.get_xlib_window().unwrap(); let x11_create_info = vk::XlibSurfaceCreateInfoKHR::builder() - .window(x11_window as vk::Window) - .dpy(x11_display as *mut vk::Display) - .build(); + .window(x11_window) + .dpy(x11_display as *mut vk::Display); + let xlib_surface_loader = XlibSurface::new(entry, instance); xlib_surface_loader.create_xlib_surface(&x11_create_info, None) } @@ -400,12 +401,11 @@ impl ExampleBase { .expect("Instance creation error"); let debug_info = vk::DebugReportCallbackCreateInfoEXT::builder() - .pfn_callback(Some(vulkan_debug_callback)) .flags( vk::DebugReportFlagsEXT::ERROR | vk::DebugReportFlagsEXT::WARNING - | vk::DebugReportFlagsEXT::PERFORMANCE_WARNING, - ).build(); + | vk::DebugReportFlagsEXT::PERFORMANCE_WARNING) + .pfn_callback(Some(vulkan_debug_callback)); let debug_report_loader = DebugReport::new(&entry, &instance); let debug_call_back = debug_report_loader @@ -447,16 +447,15 @@ impl ExampleBase { }; let priorities = [1.0]; - let queue_info = vk::DeviceQueueCreateInfo::builder() - .queue_family_index(queue_family_index as u32) - .queue_priorities(&priorities) - .build(); + let queue_info = [vk::DeviceQueueCreateInfo::builder() + .queue_family_index(queue_family_index) + .queue_priorities(&priorities) + .build()]; let device_create_info = vk::DeviceCreateInfo::builder() - .queue_create_infos(&[queue_info]) + .queue_create_infos(&queue_info) .enabled_extension_names(&device_extension_names_raw) - .enabled_features(&features) - .build(); + .enabled_features(&features); let device: Device = instance .create_device(pdevice, &device_create_info, None) @@ -512,33 +511,32 @@ impl ExampleBase { let swapchain_create_info = vk::SwapchainCreateInfoKHR::builder() .surface(surface) - .min_image_count(desired_image_count) - .image_color_space(surface_format.color_space) - .image_format(surface_format.format) - .image_extent(surface_resolution.clone()) - .image_usage(vk::ImageUsageFlags::COLOR_ATTACHMENT) - .pre_transform(pre_transform) - .composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE) - .present_mode(present_mode) - // .clipped(true) - .image_array_layers(1) - .build(); + .min_image_count( desired_image_count) + .image_color_space( surface_format.color_space) + .image_format( surface_format.format) + .image_extent( surface_resolution.clone()) + .image_usage( vk::ImageUsageFlags::COLOR_ATTACHMENT) + .image_sharing_mode(vk::SharingMode::EXCLUSIVE) + .pre_transform( pre_transform) + .composite_alpha( vk::CompositeAlphaFlagsKHR::OPAQUE) + .present_mode( present_mode) + .clipped(true) + .image_array_layers( 1); + let swapchain = swapchain_loader .create_swapchain(&swapchain_create_info, None) .unwrap(); let pool_create_info = vk::CommandPoolCreateInfo::builder() - .queue_family_index(queue_family_index) .flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER) - .build(); + .queue_family_index(queue_family_index); let pool = device.create_command_pool(&pool_create_info, None).unwrap(); let command_buffer_allocate_info = vk::CommandBufferAllocateInfo::builder() .command_buffer_count(2) .command_pool(pool) - .level(vk::CommandBufferLevel::PRIMARY) - .build(); + .level(vk::CommandBufferLevel::PRIMARY); let command_buffers = device .allocate_command_buffers(&command_buffer_allocate_info) @@ -551,33 +549,39 @@ impl ExampleBase { .iter() .map(|&image| { let create_view_info = vk::ImageViewCreateInfo::builder() - .subresource_range( - vk::ImageSubresourceRange::builder() - .aspect_mask(vk::ImageAspectFlags::COLOR) - .level_count(1) - .layer_count(1) - .build(), - ).image(image) + .view_type( vk::ImageViewType::TYPE_2D) .format(surface_format.format) - .view_type(vk::ImageViewType::TYPE_2D) - .build(); - + .components( vk::ComponentMapping { + r: vk::ComponentSwizzle::R, + g: vk::ComponentSwizzle::G, + b: vk::ComponentSwizzle::B, + a: vk::ComponentSwizzle::A, + }) + .subresource_range( vk::ImageSubresourceRange { + aspect_mask: vk::ImageAspectFlags::COLOR, + base_mip_level: 0, + level_count: 1, + base_array_layer: 0, + layer_count: 1, + }) + .image( image); device.create_image_view(&create_view_info, None).unwrap() }).collect(); let device_memory_properties = instance.get_physical_device_memory_properties(pdevice); - let depth_image_create_info = vk::ImageCreateInfo::builder() - .image_type(vk::ImageType::TYPE_2D) - .format(vk::Format::D16_UNORM) - .extent(vk::Extent3D { + .image_type( vk::ImageType::TYPE_2D) + .format( vk::Format::D16_UNORM) + .extent( vk::Extent3D { width: surface_resolution.width, height: surface_resolution.height, depth: 1, - }).mip_levels(1) - .array_layers(1) - .samples(vk::SampleCountFlags::TYPE_1) + }) + .mip_levels( 1) + .array_layers( 1) + .samples( vk::SampleCountFlags::TYPE_1) + .tiling( vk::ImageTiling::OPTIMAL) .usage(vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT) - .build(); + .sharing_mode( vk::SharingMode::EXCLUSIVE); let depth_image = device.create_image(&depth_image_create_info, None).unwrap(); let depth_image_memory_req = device.get_image_memory_requirements(depth_image); From cc218174450b6153498085461387d6618cf17bdb Mon Sep 17 00:00:00 2001 From: colin Date: Fri, 7 Dec 2018 10:04:55 -0600 Subject: [PATCH 09/19] attempt to detect endianness in bytes_to_u32_vec --- examples/src/lib.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 51be5b6..69c7196 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -36,7 +36,7 @@ use ash::extensions::khr::Win32Surface; use ash::extensions::mvk::MacOSSurface; pub use ash::version::{DeviceV1_0, EntryV1_0, InstanceV1_0}; use ash::{vk, Device, Entry, Instance}; -use byteorder::{LittleEndian, ReadBytesExt}; +use byteorder::{LittleEndian, BigEndian, ReadBytesExt}; use std::cell::RefCell; use std::default::Default; use std::ffi::{CStr, CString}; @@ -45,14 +45,29 @@ use std::ops::Drop; use std::os::raw::{c_char, c_void}; use std::ptr; + +const MAGIC_NUMBER: u32 = 0x07230203; + +fn spirv_is_little_endian(bytes: &[u8] ) -> bool { + let buffer: [u8;4] = [bytes[0], bytes[1],bytes[2],bytes[3]]; + let number : u32 = Cursor::new(buffer).read_u32::().unwrap(); + number == MAGIC_NUMBER +} + pub fn bytes_to_u32_vec(bytes: &[u8]) -> Vec { let mut output = vec![]; let mut buffer: [u8; 4] = [0, 0, 0, 0]; + let is_little_endian = spirv_is_little_endian(bytes); for (i, b) in bytes.iter().enumerate() { let idx = i % 4; buffer[idx] = *b; if idx == 3 { - output.push(Cursor::new(buffer).read_u32::().unwrap()); + let new_word = if is_little_endian { + Cursor::new(buffer).read_u32::().unwrap() + } else { + Cursor::new(buffer).read_u32::().unwrap() + }; + output.push(new_word); buffer = [0, 0, 0, 0]; } } From 0a16b5bd9b8bdece2cb43f139947b020c63fbd61 Mon Sep 17 00:00:00 2001 From: colin Date: Fri, 7 Dec 2018 11:01:26 -0600 Subject: [PATCH 10/19] refactored triangle to use builders and defaults --- examples/src/bin/triangle.rs | 155 +++++++++++++++++++---------------- 1 file changed, 86 insertions(+), 69 deletions(-) diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index 7c79c0f..60a6efd 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -35,64 +35,67 @@ fn main() { format: vk::Format::D16_UNORM, samples: vk::SampleCountFlags::TYPE_1, load_op: vk::AttachmentLoadOp::CLEAR, + initial_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, layout: vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL, - }; + }]; let depth_attachment_ref = vk::AttachmentReference { attachment: 1, layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL, }; - let dependency = vk::SubpassDependency { + let dependencies = [vk::SubpassDependency { src_subpass: vk::SUBPASS_EXTERNAL, src_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, dst_access_mask: vk::AccessFlags::COLOR_ATTACHMENT_READ | vk::AccessFlags::COLOR_ATTACHMENT_WRITE, dst_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, - ..Default::default() - }; - let subpass = vk::SubpassDescription::builder() - .color_attachments(&[color_attachment_ref]) - .depth_stencil_attachment(&depth_attachment_ref) - .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS) - .build(); + .. Default::default() + }]; + + let subpasses = [vk::SubpassDescription::builder() + .color_attachments(&color_attachment_refs) + .depth_stencil_attachment( &depth_attachment_ref) + .pipeline_bind_point( vk::PipelineBindPoint::GRAPHICS) + .build()]; let renderpass_create_info = vk::RenderPassCreateInfo::builder() - .attachments(&renderpass_attachments) - .subpasses(&[subpass]) - .dependencies(&[dependency]) - .build(); + .attachments( &renderpass_attachments) + .subpasses( &subpasses) + .dependencies( &dependencies); + let renderpass = base .device .create_render_pass(&renderpass_create_info, None) .unwrap(); + let framebuffers: Vec = base .present_image_views .iter() .map(|&present_image_view| { let framebuffer_attachments = [present_image_view, base.depth_image_view]; let frame_buffer_create_info = vk::FramebufferCreateInfo::builder() - .render_pass(renderpass) - .attachments(&framebuffer_attachments) - .width(base.surface_resolution.width) - .height(base.surface_resolution.height) - .layers(1) - .build(); + .render_pass( renderpass) + .attachments( &framebuffer_attachments) + .width( base.surface_resolution.width) + .height( base.surface_resolution.height) + .layers(1); + base.device .create_framebuffer(&frame_buffer_create_info, None) .unwrap() }).collect(); + let index_buffer_data = [0u32, 1, 2]; - let index_buffer_info = vk::BufferCreateInfo { - size: std::mem::size_of_val(&index_buffer_data) as u64, - usage: vk::BufferUsageFlags::INDEX_BUFFER, - sharing_mode: vk::SharingMode::EXCLUSIVE, - ..Default::default() - }; + let index_buffer_info = vk::BufferCreateInfo::builder() + .size( std::mem::size_of_val(&index_buffer_data) as u64) + .usage( vk::BufferUsageFlags::INDEX_BUFFER) + .sharing_mode(vk::SharingMode::EXCLUSIVE); + 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_index = find_memorytype_index( @@ -100,10 +103,11 @@ fn main() { &base.device_memory_properties, vk::MemoryPropertyFlags::HOST_VISIBLE, ).expect("Unable to find suitable memorytype for the index buffer."); + let index_allocate_info = vk::MemoryAllocateInfo { allocation_size: index_buffer_memory_req.size, memory_type_index: index_buffer_memory_index, - ..Default::default() + .. Default::default() }; let index_buffer_memory = base .device @@ -132,15 +136,18 @@ fn main() { size: 3 * std::mem::size_of::() as u64, usage: vk::BufferUsageFlags::VERTEX_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - ..Default::default() + .. Default::default() }; + let vertex_input_buffer = base .device .create_buffer(&vertex_input_buffer_info, None) .unwrap(); + let vertex_input_buffer_memory_req = base .device .get_buffer_memory_requirements(vertex_input_buffer); + let vertex_input_buffer_memory_index = find_memorytype_index( &vertex_input_buffer_memory_req, &base.device_memory_properties, @@ -150,12 +157,14 @@ fn main() { let vertex_buffer_allocate_info = vk::MemoryAllocateInfo { allocation_size: vertex_input_buffer_memory_req.size, memory_type_index: vertex_input_buffer_memory_index, - ..Default::default() + .. Default::default() }; + let vertex_input_buffer_memory = base .device .allocate_memory(&vertex_buffer_allocate_info, None) .unwrap(); + let vertices = [ Vertex { pos: [-1.0, 1.0, 0.0, 1.0], @@ -170,6 +179,7 @@ fn main() { color: [1.0, 0.0, 0.0, 1.0], }, ]; + let vert_ptr = base .device .map_memory( @@ -178,6 +188,7 @@ fn main() { vertex_input_buffer_memory_req.size, vk::MemoryMapFlags::empty(), ).unwrap(); + let mut vert_align = Align::new( vert_ptr, align_of::() as u64, @@ -228,13 +239,14 @@ fn main() { module: vertex_shader_module, p_name: shader_entry_name.as_ptr(), stage: vk::ShaderStageFlags::VERTEX, - ..Default::default() + .. Default::default() }, vk::PipelineShaderStageCreateInfo { + s_type: vk::StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO, module: fragment_shader_module, p_name: shader_entry_name.as_ptr(), stage: vk::ShaderStageFlags::FRAGMENT, - ..Default::default() + .. Default::default() }, ]; let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription { @@ -256,14 +268,18 @@ fn main() { 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 { topology: vk::PrimitiveTopology::TRIANGLE_LIST, - ..Default::default() + .. Default::default() + }; let viewports = [vk::Viewport { x: 0.0, @@ -279,24 +295,24 @@ fn main() { }]; let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder() .scissors(&scissors) - .viewports(&viewports) - .build(); + .viewports(&viewports); + let rasterization_info = vk::PipelineRasterizationStateCreateInfo { front_face: vk::FrontFace::COUNTER_CLOCKWISE, line_width: 1.0, polygon_mode: vk::PolygonMode::FILL, - ..Default::default() + .. Default::default() }; let multisample_state_info = vk::PipelineMultisampleStateCreateInfo { rasterization_samples: vk::SampleCountFlags::TYPE_1, - ..Default::default() + .. Default::default() }; 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, - ..Default::default() + .. Default::default() }; let depth_state_info = vk::PipelineDepthStencilStateCreateInfo { depth_test_enable: 1, @@ -305,7 +321,7 @@ fn main() { front: noop_stencil_state.clone(), back: noop_stencil_state.clone(), max_depth_bounds: 1.0, - ..Default::default() + .. Default::default() }; let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState { blend_enable: 0, @@ -318,28 +334,29 @@ fn main() { color_write_mask: vk::ColorComponentFlags::all(), }]; let color_blend_state = vk::PipelineColorBlendStateCreateInfo::builder() - .logic_op(vk::LogicOp::CLEAR) - .attachments(&color_blend_attachment_states) - .build(); + .logic_op( vk::LogicOp::CLEAR) + .attachments( &color_blend_attachment_states); let dynamic_state = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR]; let dynamic_state_info = vk::PipelineDynamicStateCreateInfo::builder() - .dynamic_states(&dynamic_state) - .build(); + .dynamic_states(&dynamic_state); + let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder() .stages(&shader_stage_create_infos) - .vertex_input_state(&vertex_input_state_info) - .input_assembly_state(&vertex_input_assembly_state_info) + .vertex_input_state( &vertex_input_state_info) + .input_assembly_state( &vertex_input_assembly_state_info) .viewport_state(&viewport_state_info) - .rasterization_state(&rasterization_info) - .multisample_state(&multisample_state_info) + .rasterization_state( &rasterization_info) + .multisample_state( &multisample_state_info) .depth_stencil_state(&depth_state_info) - .color_blend_state(&color_blend_state) - .dynamic_state(&dynamic_state_info) - .layout(pipeline_layout) + .color_blend_state( &color_blend_state) + .dynamic_state( &dynamic_state_info) + .layout( pipeline_layout) .render_pass(renderpass) .build(); + + let graphics_pipelines = base .device .create_graphics_pipelines(vk::PipelineCache::null(), &[graphic_pipeline_info], None) @@ -370,18 +387,15 @@ fn main() { }, ]; - let render_pass_begin_info = vk::RenderPassBeginInfo { - s_type: vk::StructureType::RENDER_PASS_BEGIN_INFO, - p_next: ptr::null(), - render_pass: renderpass, - framebuffer: framebuffers[present_index as usize], - render_area: vk::Rect2D { + let render_pass_begin_info = vk::RenderPassBeginInfo::builder() + .render_pass(renderpass) + .framebuffer( framebuffers[present_index as usize]) + .render_area( vk::Rect2D { offset: vk::Offset2D { x: 0, y: 0 }, extent: base.surface_resolution.clone(), - }, - clear_value_count: clear_values.len() as u32, - p_clear_values: clear_values.as_ptr(), - }; + }) + .clear_values( &clear_values); + record_submit_commandbuffer( &base.device, base.draw_command_buffer, @@ -428,11 +442,14 @@ fn main() { }, ); //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() - .wait_semaphores(&[base.rendering_complete_semaphore]) - .swapchains(&[base.swapchain]) - .image_indices(&[present_index]) - .build(); + .wait_semaphores( &wait_semaphors) // &base.rendering_complete_semaphore) + .swapchains( &swapchains) + .image_indices( &image_indices); + base.swapchain_loader .queue_present(base.present_queue, &present_info) .unwrap(); From 5dfd07098c13ff4b80a92b270a5ad6fa03d19c6c Mon Sep 17 00:00:00 2001 From: colin Date: Fri, 7 Dec 2018 11:46:48 -0600 Subject: [PATCH 11/19] refactored texture example to use defaults and builders --- examples/src/bin/texture.rs | 150 ++++++++++++++++++------------------ 1 file changed, 74 insertions(+), 76 deletions(-) diff --git a/examples/src/bin/texture.rs b/examples/src/bin/texture.rs index 6201180..822e70e 100644 --- a/examples/src/bin/texture.rs +++ b/examples/src/bin/texture.rs @@ -49,39 +49,36 @@ fn main() { load_op: vk::AttachmentLoadOp::CLEAR, initial_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, layout: vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL, - }; + }]; let depth_attachment_ref = vk::AttachmentReference { attachment: 1, layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL, }; - - let dependency = vk::SubpassDependency { + let dependencies = [vk::SubpassDependency { src_subpass: vk::SUBPASS_EXTERNAL, src_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, dst_access_mask: vk::AccessFlags::COLOR_ATTACHMENT_READ | vk::AccessFlags::COLOR_ATTACHMENT_WRITE, dst_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, - ..Default::default() - }; + .. Default::default() + }]; - let subpass = vk::SubpassDescription::builder() - .color_attachments(&[color_attachment_ref]) - .depth_stencil_attachment(&depth_attachment_ref) - .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS) - .build(); + let subpasses = [vk::SubpassDescription::builder() + .color_attachments( &color_attachment_refs) + .depth_stencil_attachment( &depth_attachment_ref) + .pipeline_bind_point( vk::PipelineBindPoint::GRAPHICS) + .build()]; let renderpass_create_info = vk::RenderPassCreateInfo::builder() .attachments(&renderpass_attachments) - .subpasses(&[subpass]) - .dependencies(&[dependency]) - .build(); + .subpasses( &subpasses) + .dependencies( &dependencies); let renderpass = base .device @@ -94,12 +91,11 @@ fn main() { .map(|&present_image_view| { let framebuffer_attachments = [present_image_view, base.depth_image_view]; let frame_buffer_create_info = vk::FramebufferCreateInfo::builder() - .render_pass(renderpass) + .render_pass( renderpass) .attachments(&framebuffer_attachments) .width(base.surface_resolution.width) - .height(base.surface_resolution.height) - .layers(1) - .build(); + .height( base.surface_resolution.height) + .layers(1); base.device .create_framebuffer(&frame_buffer_create_info, None) @@ -110,7 +106,7 @@ fn main() { size: std::mem::size_of_val(&index_buffer_data) as u64, usage: vk::BufferUsageFlags::INDEX_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - ..Default::default() + .. Default::default() }; 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); @@ -223,7 +219,7 @@ fn main() { size: std::mem::size_of_val(&uniform_color_buffer_data) as u64, usage: vk::BufferUsageFlags::UNIFORM_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - ..Default::default() + .. Default::default() }; let uniform_color_buffer = base .device @@ -390,7 +386,9 @@ fn main() { width: image_dimensions.0, height: image_dimensions.1, depth: 1, - }).build()]; + }, + .. Default::default() + }]; device.cmd_copy_buffer_to_image( texture_command_buffer, image_buffer, @@ -432,7 +430,9 @@ fn main() { address_mode_v: vk::SamplerAddressMode::MIRRORED_REPEAT, address_mode_w: vk::SamplerAddressMode::MIRRORED_REPEAT, 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(); @@ -453,7 +453,7 @@ fn main() { ..Default::default() }, image: texture_image, - ..Default::default() + .. Default::default() }; let tex_image_view = base .device @@ -471,8 +471,8 @@ fn main() { ]; let descriptor_pool_info = vk::DescriptorPoolCreateInfo::builder() .pool_sizes(&descriptor_sizes) - .max_sets(1) - .build(); + .max_sets( 1); + let descriptor_pool = base .device .create_descriptor_pool(&descriptor_pool_info, None) @@ -493,17 +493,16 @@ fn main() { }, ]; let descriptor_info = vk::DescriptorSetLayoutCreateInfo::builder() - .bindings(&desc_layout_bindings) - .build(); + .bindings(&desc_layout_bindings); let desc_set_layouts = [base .device .create_descriptor_set_layout(&descriptor_info, None) .unwrap()]; + let desc_alloc_info = vk::DescriptorSetAllocateInfo::builder() - .descriptor_pool(descriptor_pool) - .set_layouts(&desc_set_layouts) - .build(); + .descriptor_pool( descriptor_pool) + .set_layouts(&desc_set_layouts); let descriptor_sets = base .device .allocate_descriptor_sets(&desc_alloc_info) @@ -527,7 +526,7 @@ fn main() { descriptor_count: 1, descriptor_type: vk::DescriptorType::UNIFORM_BUFFER, p_buffer_info: &uniform_color_buffer_descriptor, - ..Default::default() + .. Default::default() }, vk::WriteDescriptorSet { dst_set: descriptor_sets[0], @@ -535,7 +534,7 @@ fn main() { descriptor_count: 1, descriptor_type: vk::DescriptorType::COMBINED_IMAGE_SAMPLER, p_image_info: &tex_descriptor, - ..Default::default() + .. Default::default() }, ]; base.device.update_descriptor_sets(&write_desc_sets, &[]); @@ -567,8 +566,7 @@ fn main() { .expect("Fragment shader module error"); let layout_create_info = vk::PipelineLayoutCreateInfo::builder() - .set_layouts(&desc_set_layouts) - .build(); + .set_layouts(&desc_set_layouts); let pipeline_layout = base .device @@ -581,13 +579,13 @@ fn main() { module: vertex_shader_module, p_name: shader_entry_name.as_ptr(), stage: vk::ShaderStageFlags::VERTEX, - ..Default::default() + .. Default::default() }, vk::PipelineShaderStageCreateInfo { module: fragment_shader_module, p_name: shader_entry_name.as_ptr(), stage: vk::ShaderStageFlags::FRAGMENT, - ..Default::default() + .. Default::default() }, ]; let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription { @@ -611,13 +609,12 @@ fn main() { ]; 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_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo::builder() - .topology(vk::PrimitiveTopology::TRIANGLE_LIST) - .build(); + .vertex_binding_descriptions(&vertex_input_binding_descriptions); + let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo { + topology: vk::PrimitiveTopology::TRIANGLE_LIST, + .. Default::default() + }; let viewports = [vk::Viewport { x: 0.0, y: 0.0, @@ -627,20 +624,22 @@ fn main() { max_depth: 1.0, }]; let scissors = [vk::Rect2D { - offset: vk::Offset2D { x: 0, y: 0 }, extent: base.surface_resolution.clone(), + .. Default::default() }]; - let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder() .scissors(&scissors) - .viewports(&viewports) - .build(); + .viewports(&viewports); let rasterization_info = vk::PipelineRasterizationStateCreateInfo { front_face: vk::FrontFace::COUNTER_CLOCKWISE, line_width: 1.0, 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() @@ -652,7 +651,7 @@ fn main() { pass_op: vk::StencilOp::KEEP, depth_fail_op: vk::StencilOp::KEEP, compare_op: vk::CompareOp::ALWAYS, - ..Default::default() + .. Default::default() }; let depth_state_info = vk::PipelineDepthStencilStateCreateInfo { depth_test_enable: 1, @@ -661,7 +660,7 @@ fn main() { front: noop_stencil_state.clone(), back: noop_stencil_state.clone(), max_depth_bounds: 1.0, - ..Default::default() + .. Default::default() }; let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState { @@ -675,27 +674,25 @@ fn main() { color_write_mask: vk::ColorComponentFlags::all(), }]; let color_blend_state = vk::PipelineColorBlendStateCreateInfo::builder() - .logic_op(vk::LogicOp::CLEAR) - .attachments(&color_blend_attachment_states) - .build(); + .logic_op( vk::LogicOp::CLEAR) + .attachments( &color_blend_attachment_states); let dynamic_state = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR]; let dynamic_state_info = vk::PipelineDynamicStateCreateInfo::builder() - .dynamic_states(&dynamic_state) - .build(); + .dynamic_states(&dynamic_state); let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder() .stages(&shader_stage_create_infos) - .vertex_input_state(&vertex_input_state_info) - .input_assembly_state(&vertex_input_assembly_state_info) - .viewport_state(&viewport_state_info) - .rasterization_state(&rasterization_info) - .multisample_state(&multisample_state_info) - .depth_stencil_state(&depth_state_info) - .color_blend_state(&color_blend_state) - .dynamic_state(&dynamic_state_info) - .layout(pipeline_layout) - .render_pass(renderpass) + .vertex_input_state( &vertex_input_state_info) + .input_assembly_state( &vertex_input_assembly_state_info) + .viewport_state( &viewport_state_info) + .rasterization_state( &rasterization_info) + .multisample_state( &multisample_state_info) + .depth_stencil_state( &depth_state_info) + .color_blend_state( &color_blend_state) + .dynamic_state( &dynamic_state_info) + .layout( pipeline_layout) + .render_pass( renderpass) .build(); let graphics_pipelines = base @@ -731,11 +728,11 @@ fn main() { let render_pass_begin_info = vk::RenderPassBeginInfo::builder() .render_pass(renderpass) .framebuffer(framebuffers[present_index as usize]) - .render_area(vk::Rect2D { + .render_area( vk::Rect2D { offset: vk::Offset2D { x: 0, y: 0 }, extent: base.surface_resolution.clone(), - }).clear_values(&clear_values) - .build(); + }) + .clear_values(&clear_values); record_submit_commandbuffer( &base.device, @@ -791,13 +788,14 @@ fn main() { }, ); //let mut present_info_err = mem::uninitialized(); - - let present_info = vk::PresentInfoKHR::builder() - .wait_semaphores(&[base.rendering_complete_semaphore]) - .swapchains(&[base.swapchain]) - .image_indices(&[present_index]) - .build(); - + let present_info = vk::PresentInfoKHR { + wait_semaphore_count: 1, + p_wait_semaphores: &base.rendering_complete_semaphore, + swapchain_count: 1, + p_swapchains: &base.swapchain, + p_image_indices: &present_index, + .. Default::default() + }; base.swapchain_loader .queue_present(base.present_queue, &present_info) .unwrap(); From 962f1ba8304530f6352a06806dff7e95b96ad677 Mon Sep 17 00:00:00 2001 From: colin Date: Fri, 7 Dec 2018 11:47:57 -0600 Subject: [PATCH 12/19] reformatting via rust fmt --- examples/src/bin/texture.rs | 127 +++++++++++++++++++---------------- examples/src/bin/triangle.rs | 107 +++++++++++++++-------------- examples/src/lib.rs | 84 ++++++++++++----------- 3 files changed, 170 insertions(+), 148 deletions(-) diff --git a/examples/src/bin/texture.rs b/examples/src/bin/texture.rs index 822e70e..e3cdfd9 100644 --- a/examples/src/bin/texture.rs +++ b/examples/src/bin/texture.rs @@ -49,7 +49,7 @@ fn main() { load_op: vk::AttachmentLoadOp::CLEAR, initial_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 { @@ -66,19 +66,19 @@ fn main() { dst_access_mask: vk::AccessFlags::COLOR_ATTACHMENT_READ | vk::AccessFlags::COLOR_ATTACHMENT_WRITE, dst_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, - .. Default::default() + ..Default::default() }]; let subpasses = [vk::SubpassDescription::builder() - .color_attachments( &color_attachment_refs) - .depth_stencil_attachment( &depth_attachment_ref) - .pipeline_bind_point( vk::PipelineBindPoint::GRAPHICS) - .build()]; + .color_attachments(&color_attachment_refs) + .depth_stencil_attachment(&depth_attachment_ref) + .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS) + .build()]; let renderpass_create_info = vk::RenderPassCreateInfo::builder() .attachments(&renderpass_attachments) - .subpasses( &subpasses) - .dependencies( &dependencies); + .subpasses(&subpasses) + .dependencies(&dependencies); let renderpass = base .device @@ -91,22 +91,23 @@ fn main() { .map(|&present_image_view| { let framebuffer_attachments = [present_image_view, base.depth_image_view]; let frame_buffer_create_info = vk::FramebufferCreateInfo::builder() - .render_pass( renderpass) + .render_pass(renderpass) .attachments(&framebuffer_attachments) .width(base.surface_resolution.width) - .height( base.surface_resolution.height) + .height(base.surface_resolution.height) .layers(1); base.device .create_framebuffer(&frame_buffer_create_info, None) .unwrap() - }).collect(); + }) + .collect(); let index_buffer_data = [0u32, 1, 2, 2, 3, 0]; let index_buffer_info = vk::BufferCreateInfo { size: std::mem::size_of_val(&index_buffer_data) as u64, usage: vk::BufferUsageFlags::INDEX_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - .. Default::default() + ..Default::default() }; 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); @@ -114,7 +115,8 @@ fn main() { &index_buffer_memory_req, &base.device_memory_properties, 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 { allocation_size: index_buffer_memory_req.size, memory_type_index: index_buffer_memory_index, @@ -131,7 +133,8 @@ fn main() { 0, index_buffer_memory_req.size, vk::MemoryMapFlags::empty(), - ).unwrap(); + ) + .unwrap(); let mut index_slice = Align::new( index_ptr, align_of::() as u64, @@ -178,7 +181,8 @@ fn main() { &vertex_input_buffer_memory_req, &base.device_memory_properties, vk::MemoryPropertyFlags::HOST_VISIBLE, - ).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 { allocation_size: vertex_input_buffer_memory_req.size, @@ -197,7 +201,8 @@ fn main() { 0, vertex_input_buffer_memory_req.size, vk::MemoryMapFlags::empty(), - ).unwrap(); + ) + .unwrap(); let mut slice = Align::new( vert_ptr, align_of::() as u64, @@ -219,7 +224,7 @@ fn main() { size: std::mem::size_of_val(&uniform_color_buffer_data) as u64, usage: vk::BufferUsageFlags::UNIFORM_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - .. Default::default() + ..Default::default() }; let uniform_color_buffer = base .device @@ -232,7 +237,8 @@ fn main() { &uniform_color_buffer_memory_req, &base.device_memory_properties, vk::MemoryPropertyFlags::HOST_VISIBLE, - ).expect("Unable to find suitable memorytype for the vertex buffer."); + ) + .expect("Unable to find suitable memorytype for the vertex buffer."); let uniform_color_buffer_allocate_info = vk::MemoryAllocateInfo { allocation_size: uniform_color_buffer_memory_req.size, @@ -250,7 +256,8 @@ fn main() { 0, uniform_color_buffer_memory_req.size, vk::MemoryMapFlags::empty(), - ).unwrap(); + ) + .unwrap(); let mut uniform_aligned_slice = Align::new( uniform_ptr, align_of::() as u64, @@ -277,7 +284,8 @@ fn main() { &image_buffer_memory_req, &base.device_memory_properties, vk::MemoryPropertyFlags::HOST_VISIBLE, - ).expect("Unable to find suitable memorytype for the vertex buffer."); + ) + .expect("Unable to find suitable memorytype for the vertex buffer."); let image_buffer_allocate_info = vk::MemoryAllocateInfo { allocation_size: image_buffer_memory_req.size, @@ -295,7 +303,8 @@ fn main() { 0, image_buffer_memory_req.size, vk::MemoryMapFlags::empty(), - ).unwrap(); + ) + .unwrap(); let mut image_slice = Align::new( image_ptr, std::mem::align_of::() as u64, @@ -332,7 +341,8 @@ fn main() { &texture_memory_req, &base.device_memory_properties, vk::MemoryPropertyFlags::DEVICE_LOCAL, - ).expect("Unable to find suitable memory index for depth image."); + ) + .expect("Unable to find suitable memory index for depth image."); let texture_allocate_info = vk::MemoryAllocateInfo { allocation_size: texture_memory_req.size, @@ -387,7 +397,7 @@ fn main() { height: image_dimensions.1, depth: 1, }, - .. Default::default() + ..Default::default() }]; device.cmd_copy_buffer_to_image( texture_command_buffer, @@ -432,7 +442,7 @@ fn main() { max_anisotropy: 1.0, border_color: vk::BorderColor::FLOAT_OPAQUE_WHITE, compare_op: vk::CompareOp::NEVER, - .. Default::default() + ..Default::default() }; let sampler = base.device.create_sampler(&sampler_info, None).unwrap(); @@ -453,7 +463,7 @@ fn main() { ..Default::default() }, image: texture_image, - .. Default::default() + ..Default::default() }; let tex_image_view = base .device @@ -471,7 +481,7 @@ fn main() { ]; let descriptor_pool_info = vk::DescriptorPoolCreateInfo::builder() .pool_sizes(&descriptor_sizes) - .max_sets( 1); + .max_sets(1); let descriptor_pool = base .device @@ -492,8 +502,8 @@ fn main() { ..Default::default() }, ]; - let descriptor_info = vk::DescriptorSetLayoutCreateInfo::builder() - .bindings(&desc_layout_bindings); + let descriptor_info = + vk::DescriptorSetLayoutCreateInfo::builder().bindings(&desc_layout_bindings); let desc_set_layouts = [base .device @@ -501,7 +511,7 @@ fn main() { .unwrap()]; let desc_alloc_info = vk::DescriptorSetAllocateInfo::builder() - .descriptor_pool( descriptor_pool) + .descriptor_pool(descriptor_pool) .set_layouts(&desc_set_layouts); let descriptor_sets = base .device @@ -526,7 +536,7 @@ fn main() { descriptor_count: 1, descriptor_type: vk::DescriptorType::UNIFORM_BUFFER, p_buffer_info: &uniform_color_buffer_descriptor, - .. Default::default() + ..Default::default() }, vk::WriteDescriptorSet { dst_set: descriptor_sets[0], @@ -534,7 +544,7 @@ fn main() { descriptor_count: 1, descriptor_type: vk::DescriptorType::COMBINED_IMAGE_SAMPLER, p_image_info: &tex_descriptor, - .. Default::default() + ..Default::default() }, ]; base.device.update_descriptor_sets(&write_desc_sets, &[]); @@ -565,8 +575,8 @@ fn main() { .create_shader_module(&frag_shader_info, None) .expect("Fragment shader module error"); - let layout_create_info = vk::PipelineLayoutCreateInfo::builder() - .set_layouts(&desc_set_layouts); + let layout_create_info = + vk::PipelineLayoutCreateInfo::builder().set_layouts(&desc_set_layouts); let pipeline_layout = base .device @@ -579,13 +589,13 @@ fn main() { module: vertex_shader_module, p_name: shader_entry_name.as_ptr(), stage: vk::ShaderStageFlags::VERTEX, - .. Default::default() + ..Default::default() }, vk::PipelineShaderStageCreateInfo { module: fragment_shader_module, p_name: shader_entry_name.as_ptr(), stage: vk::ShaderStageFlags::FRAGMENT, - .. Default::default() + ..Default::default() }, ]; let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription { @@ -613,7 +623,7 @@ fn main() { let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo { topology: vk::PrimitiveTopology::TRIANGLE_LIST, - .. Default::default() + ..Default::default() }; let viewports = [vk::Viewport { x: 0.0, @@ -625,7 +635,7 @@ fn main() { }]; let scissors = [vk::Rect2D { extent: base.surface_resolution.clone(), - .. Default::default() + ..Default::default() }]; let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder() .scissors(&scissors) @@ -635,11 +645,11 @@ fn main() { front_face: vk::FrontFace::COUNTER_CLOCKWISE, line_width: 1.0, polygon_mode: vk::PolygonMode::FILL, - .. Default::default() + ..Default::default() }; let multisample_state_info = vk::PipelineMultisampleStateCreateInfo { rasterization_samples: vk::SampleCountFlags::TYPE_1, - .. Default::default() + ..Default::default() }; let multisample_state_info = vk::PipelineMultisampleStateCreateInfo::builder() @@ -651,7 +661,7 @@ fn main() { pass_op: vk::StencilOp::KEEP, depth_fail_op: vk::StencilOp::KEEP, compare_op: vk::CompareOp::ALWAYS, - .. Default::default() + ..Default::default() }; let depth_state_info = vk::PipelineDepthStencilStateCreateInfo { depth_test_enable: 1, @@ -660,7 +670,7 @@ fn main() { front: noop_stencil_state.clone(), back: noop_stencil_state.clone(), max_depth_bounds: 1.0, - .. Default::default() + ..Default::default() }; let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState { @@ -674,25 +684,25 @@ fn main() { color_write_mask: vk::ColorComponentFlags::all(), }]; let color_blend_state = vk::PipelineColorBlendStateCreateInfo::builder() - .logic_op( vk::LogicOp::CLEAR) - .attachments( &color_blend_attachment_states); + .logic_op(vk::LogicOp::CLEAR) + .attachments(&color_blend_attachment_states); let dynamic_state = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR]; - let dynamic_state_info = vk::PipelineDynamicStateCreateInfo::builder() - .dynamic_states(&dynamic_state); + let dynamic_state_info = + vk::PipelineDynamicStateCreateInfo::builder().dynamic_states(&dynamic_state); let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder() .stages(&shader_stage_create_infos) - .vertex_input_state( &vertex_input_state_info) - .input_assembly_state( &vertex_input_assembly_state_info) - .viewport_state( &viewport_state_info) - .rasterization_state( &rasterization_info) - .multisample_state( &multisample_state_info) - .depth_stencil_state( &depth_state_info) - .color_blend_state( &color_blend_state) - .dynamic_state( &dynamic_state_info) - .layout( pipeline_layout) - .render_pass( renderpass) + .vertex_input_state(&vertex_input_state_info) + .input_assembly_state(&vertex_input_assembly_state_info) + .viewport_state(&viewport_state_info) + .rasterization_state(&rasterization_info) + .multisample_state(&multisample_state_info) + .depth_stencil_state(&depth_state_info) + .color_blend_state(&color_blend_state) + .dynamic_state(&dynamic_state_info) + .layout(pipeline_layout) + .render_pass(renderpass) .build(); let graphics_pipelines = base @@ -710,7 +720,8 @@ fn main() { std::u64::MAX, base.present_complete_semaphore, vk::Fence::null(), - ).unwrap(); + ) + .unwrap(); let clear_values = [ vk::ClearValue { color: vk::ClearColorValue { @@ -728,7 +739,7 @@ fn main() { let render_pass_begin_info = vk::RenderPassBeginInfo::builder() .render_pass(renderpass) .framebuffer(framebuffers[present_index as usize]) - .render_area( vk::Rect2D { + .render_area(vk::Rect2D { offset: vk::Offset2D { x: 0, y: 0 }, extent: base.surface_resolution.clone(), }) @@ -794,7 +805,7 @@ fn main() { swapchain_count: 1, p_swapchains: &base.swapchain, p_image_indices: &present_index, - .. Default::default() + ..Default::default() }; base.swapchain_loader .queue_present(base.present_queue, &present_info) diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index 60a6efd..13f1d84 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -37,7 +37,7 @@ fn main() { load_op: vk::AttachmentLoadOp::CLEAR, initial_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 { @@ -54,19 +54,19 @@ fn main() { dst_access_mask: vk::AccessFlags::COLOR_ATTACHMENT_READ | vk::AccessFlags::COLOR_ATTACHMENT_WRITE, dst_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT, - .. Default::default() + ..Default::default() }]; let subpasses = [vk::SubpassDescription::builder() - .color_attachments(&color_attachment_refs) - .depth_stencil_attachment( &depth_attachment_ref) - .pipeline_bind_point( vk::PipelineBindPoint::GRAPHICS) - .build()]; + .color_attachments(&color_attachment_refs) + .depth_stencil_attachment(&depth_attachment_ref) + .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS) + .build()]; let renderpass_create_info = vk::RenderPassCreateInfo::builder() - .attachments( &renderpass_attachments) - .subpasses( &subpasses) - .dependencies( &dependencies); + .attachments(&renderpass_attachments) + .subpasses(&subpasses) + .dependencies(&dependencies); let renderpass = base .device @@ -79,21 +79,22 @@ fn main() { .map(|&present_image_view| { let framebuffer_attachments = [present_image_view, base.depth_image_view]; let frame_buffer_create_info = vk::FramebufferCreateInfo::builder() - .render_pass( renderpass) - .attachments( &framebuffer_attachments) - .width( base.surface_resolution.width) - .height( base.surface_resolution.height) + .render_pass(renderpass) + .attachments(&framebuffer_attachments) + .width(base.surface_resolution.width) + .height(base.surface_resolution.height) .layers(1); base.device .create_framebuffer(&frame_buffer_create_info, None) .unwrap() - }).collect(); + }) + .collect(); let index_buffer_data = [0u32, 1, 2]; let index_buffer_info = vk::BufferCreateInfo::builder() - .size( std::mem::size_of_val(&index_buffer_data) as u64) - .usage( vk::BufferUsageFlags::INDEX_BUFFER) + .size(std::mem::size_of_val(&index_buffer_data) as u64) + .usage(vk::BufferUsageFlags::INDEX_BUFFER) .sharing_mode(vk::SharingMode::EXCLUSIVE); let index_buffer = base.device.create_buffer(&index_buffer_info, None).unwrap(); @@ -102,12 +103,13 @@ fn main() { &index_buffer_memory_req, &base.device_memory_properties, 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 { allocation_size: index_buffer_memory_req.size, memory_type_index: index_buffer_memory_index, - .. Default::default() + ..Default::default() }; let index_buffer_memory = base .device @@ -120,7 +122,8 @@ fn main() { 0, index_buffer_memory_req.size, vk::MemoryMapFlags::empty(), - ).unwrap(); + ) + .unwrap(); let mut index_slice = Align::new( index_ptr, align_of::() as u64, @@ -136,7 +139,7 @@ fn main() { size: 3 * std::mem::size_of::() as u64, usage: vk::BufferUsageFlags::VERTEX_BUFFER, sharing_mode: vk::SharingMode::EXCLUSIVE, - .. Default::default() + ..Default::default() }; let vertex_input_buffer = base @@ -152,12 +155,13 @@ fn main() { &vertex_input_buffer_memory_req, &base.device_memory_properties, vk::MemoryPropertyFlags::HOST_VISIBLE, - ).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 { allocation_size: vertex_input_buffer_memory_req.size, memory_type_index: vertex_input_buffer_memory_index, - .. Default::default() + ..Default::default() }; let vertex_input_buffer_memory = base @@ -187,7 +191,8 @@ fn main() { 0, vertex_input_buffer_memory_req.size, vk::MemoryMapFlags::empty(), - ).unwrap(); + ) + .unwrap(); let mut vert_align = Align::new( vert_ptr, @@ -239,14 +244,14 @@ fn main() { module: vertex_shader_module, p_name: shader_entry_name.as_ptr(), stage: vk::ShaderStageFlags::VERTEX, - .. Default::default() + ..Default::default() }, vk::PipelineShaderStageCreateInfo { s_type: vk::StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO, module: fragment_shader_module, p_name: shader_entry_name.as_ptr(), stage: vk::ShaderStageFlags::FRAGMENT, - .. Default::default() + ..Default::default() }, ]; let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription { @@ -274,12 +279,11 @@ fn main() { 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() + ..Default::default() }; let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo { topology: vk::PrimitiveTopology::TRIANGLE_LIST, - .. Default::default() - + ..Default::default() }; let viewports = [vk::Viewport { x: 0.0, @@ -301,18 +305,18 @@ fn main() { front_face: vk::FrontFace::COUNTER_CLOCKWISE, line_width: 1.0, polygon_mode: vk::PolygonMode::FILL, - .. Default::default() + ..Default::default() }; let multisample_state_info = vk::PipelineMultisampleStateCreateInfo { rasterization_samples: vk::SampleCountFlags::TYPE_1, - .. Default::default() + ..Default::default() }; 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, - .. Default::default() + ..Default::default() }; let depth_state_info = vk::PipelineDepthStencilStateCreateInfo { depth_test_enable: 1, @@ -321,7 +325,7 @@ fn main() { front: noop_stencil_state.clone(), back: noop_stencil_state.clone(), max_depth_bounds: 1.0, - .. Default::default() + ..Default::default() }; let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState { blend_enable: 0, @@ -334,29 +338,27 @@ fn main() { color_write_mask: vk::ColorComponentFlags::all(), }]; let color_blend_state = vk::PipelineColorBlendStateCreateInfo::builder() - .logic_op( vk::LogicOp::CLEAR) - .attachments( &color_blend_attachment_states); + .logic_op(vk::LogicOp::CLEAR) + .attachments(&color_blend_attachment_states); let dynamic_state = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR]; - let dynamic_state_info = vk::PipelineDynamicStateCreateInfo::builder() - .dynamic_states(&dynamic_state); - + let dynamic_state_info = + vk::PipelineDynamicStateCreateInfo::builder().dynamic_states(&dynamic_state); let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder() .stages(&shader_stage_create_infos) - .vertex_input_state( &vertex_input_state_info) - .input_assembly_state( &vertex_input_assembly_state_info) + .vertex_input_state(&vertex_input_state_info) + .input_assembly_state(&vertex_input_assembly_state_info) .viewport_state(&viewport_state_info) - .rasterization_state( &rasterization_info) - .multisample_state( &multisample_state_info) + .rasterization_state(&rasterization_info) + .multisample_state(&multisample_state_info) .depth_stencil_state(&depth_state_info) - .color_blend_state( &color_blend_state) - .dynamic_state( &dynamic_state_info) - .layout( pipeline_layout) + .color_blend_state(&color_blend_state) + .dynamic_state(&dynamic_state_info) + .layout(pipeline_layout) .render_pass(renderpass) .build(); - let graphics_pipelines = base .device .create_graphics_pipelines(vk::PipelineCache::null(), &[graphic_pipeline_info], None) @@ -372,7 +374,8 @@ fn main() { std::u64::MAX, base.present_complete_semaphore, vk::Fence::null(), - ).unwrap(); + ) + .unwrap(); let clear_values = [ vk::ClearValue { color: vk::ClearColorValue { @@ -389,12 +392,12 @@ fn main() { let render_pass_begin_info = vk::RenderPassBeginInfo::builder() .render_pass(renderpass) - .framebuffer( framebuffers[present_index as usize]) - .render_area( vk::Rect2D { + .framebuffer(framebuffers[present_index as usize]) + .render_area(vk::Rect2D { offset: vk::Offset2D { x: 0, y: 0 }, extent: base.surface_resolution.clone(), }) - .clear_values( &clear_values); + .clear_values(&clear_values); record_submit_commandbuffer( &base.device, @@ -446,9 +449,9 @@ fn main() { let swapchains = [base.swapchain]; let image_indices = [present_index]; let present_info = vk::PresentInfoKHR::builder() - .wait_semaphores( &wait_semaphors) // &base.rendering_complete_semaphore) - .swapchains( &swapchains) - .image_indices( &image_indices); + .wait_semaphores(&wait_semaphors) // &base.rendering_complete_semaphore) + .swapchains(&swapchains) + .image_indices(&image_indices); base.swapchain_loader .queue_present(base.present_queue, &present_info) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 69c7196..7a96d3e 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -36,7 +36,7 @@ use ash::extensions::khr::Win32Surface; use ash::extensions::mvk::MacOSSurface; pub use ash::version::{DeviceV1_0, EntryV1_0, InstanceV1_0}; use ash::{vk, Device, Entry, Instance}; -use byteorder::{LittleEndian, BigEndian, ReadBytesExt}; +use byteorder::{BigEndian, LittleEndian, ReadBytesExt}; use std::cell::RefCell; use std::default::Default; use std::ffi::{CStr, CString}; @@ -45,12 +45,11 @@ use std::ops::Drop; use std::os::raw::{c_char, c_void}; use std::ptr; - const MAGIC_NUMBER: u32 = 0x07230203; -fn spirv_is_little_endian(bytes: &[u8] ) -> bool { - let buffer: [u8;4] = [bytes[0], bytes[1],bytes[2],bytes[3]]; - let number : u32 = Cursor::new(buffer).read_u32::().unwrap(); +fn spirv_is_little_endian(bytes: &[u8]) -> bool { + let buffer: [u8; 4] = [bytes[0], bytes[1], bytes[2], bytes[3]]; + let number: u32 = Cursor::new(buffer).read_u32::().unwrap(); number == MAGIC_NUMBER } @@ -62,7 +61,7 @@ pub fn bytes_to_u32_vec(bytes: &[u8]) -> Vec { let idx = i % 4; buffer[idx] = *b; if idx == 3 { - let new_word = if is_little_endian { + let new_word = if is_little_endian { Cursor::new(buffer).read_u32::().unwrap() } else { Cursor::new(buffer).read_u32::().unwrap() @@ -131,7 +130,8 @@ pub fn record_submit_commandbuffer Some((*pdevice, index)), _ => None, } - }).nth(0) - }).filter_map(|v| v) + }) + .nth(0) + }) + .filter_map(|v| v) .nth(0) .expect("Couldn't find suitable device."); let queue_family_index = queue_family_index as u32; @@ -463,9 +467,9 @@ impl ExampleBase { let priorities = [1.0]; let queue_info = [vk::DeviceQueueCreateInfo::builder() - .queue_family_index(queue_family_index) - .queue_priorities(&priorities) - .build()]; + .queue_family_index(queue_family_index) + .queue_priorities(&priorities) + .build()]; let device_create_info = vk::DeviceCreateInfo::builder() .queue_create_infos(&queue_info) @@ -488,7 +492,8 @@ impl ExampleBase { color_space: sfmt.color_space, }, _ => sfmt.clone(), - }).nth(0) + }) + .nth(0) .expect("Unable to find suitable surface format."); let surface_capabilities = surface_loader .get_physical_device_surface_capabilities(pdevice, surface) @@ -526,17 +531,17 @@ impl ExampleBase { let swapchain_create_info = vk::SwapchainCreateInfoKHR::builder() .surface(surface) - .min_image_count( desired_image_count) - .image_color_space( surface_format.color_space) - .image_format( surface_format.format) - .image_extent( surface_resolution.clone()) - .image_usage( vk::ImageUsageFlags::COLOR_ATTACHMENT) + .min_image_count(desired_image_count) + .image_color_space(surface_format.color_space) + .image_format(surface_format.format) + .image_extent(surface_resolution.clone()) + .image_usage(vk::ImageUsageFlags::COLOR_ATTACHMENT) .image_sharing_mode(vk::SharingMode::EXCLUSIVE) - .pre_transform( pre_transform) - .composite_alpha( vk::CompositeAlphaFlagsKHR::OPAQUE) - .present_mode( present_mode) + .pre_transform(pre_transform) + .composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE) + .present_mode(present_mode) .clipped(true) - .image_array_layers( 1); + .image_array_layers(1); let swapchain = swapchain_loader .create_swapchain(&swapchain_create_info, None) @@ -564,39 +569,40 @@ impl ExampleBase { .iter() .map(|&image| { let create_view_info = vk::ImageViewCreateInfo::builder() - .view_type( vk::ImageViewType::TYPE_2D) + .view_type(vk::ImageViewType::TYPE_2D) .format(surface_format.format) - .components( vk::ComponentMapping { + .components(vk::ComponentMapping { r: vk::ComponentSwizzle::R, g: vk::ComponentSwizzle::G, b: vk::ComponentSwizzle::B, a: vk::ComponentSwizzle::A, }) - .subresource_range( vk::ImageSubresourceRange { + .subresource_range(vk::ImageSubresourceRange { aspect_mask: vk::ImageAspectFlags::COLOR, base_mip_level: 0, level_count: 1, base_array_layer: 0, layer_count: 1, }) - .image( image); + .image(image); device.create_image_view(&create_view_info, None).unwrap() - }).collect(); + }) + .collect(); let device_memory_properties = instance.get_physical_device_memory_properties(pdevice); let depth_image_create_info = vk::ImageCreateInfo::builder() - .image_type( vk::ImageType::TYPE_2D) - .format( vk::Format::D16_UNORM) - .extent( vk::Extent3D { + .image_type(vk::ImageType::TYPE_2D) + .format(vk::Format::D16_UNORM) + .extent(vk::Extent3D { width: surface_resolution.width, height: surface_resolution.height, depth: 1, }) - .mip_levels( 1) - .array_layers( 1) - .samples( vk::SampleCountFlags::TYPE_1) - .tiling( vk::ImageTiling::OPTIMAL) + .mip_levels(1) + .array_layers(1) + .samples(vk::SampleCountFlags::TYPE_1) + .tiling(vk::ImageTiling::OPTIMAL) .usage(vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT) - .sharing_mode( vk::SharingMode::EXCLUSIVE); + .sharing_mode(vk::SharingMode::EXCLUSIVE); let depth_image = device.create_image(&depth_image_create_info, None).unwrap(); let depth_image_memory_req = device.get_image_memory_requirements(depth_image); @@ -604,7 +610,8 @@ impl ExampleBase { &depth_image_memory_req, &device_memory_properties, vk::MemoryPropertyFlags::DEVICE_LOCAL, - ).expect("Unable to find suitable memory index for depth image."); + ) + .expect("Unable to find suitable memory index for depth image."); let depth_image_allocate_info = vk::MemoryAllocateInfo::builder() .allocation_size(depth_image_memory_req.size) @@ -624,6 +631,7 @@ impl ExampleBase { setup_command_buffer, present_queue, &[], + // &[vk::PipelineStageFlags::BOTTOM_OF_PIPE], &[], &[], |device, setup_command_buffer| { From 31880de8ab1d00e6097bdae8d394eab8a4c59812 Mon Sep 17 00:00:00 2001 From: colin Date: Fri, 7 Dec 2018 13:00:27 -0600 Subject: [PATCH 13/19] refactored examples --- examples/src/bin/texture.rs | 9 ++------- examples/src/lib.rs | 29 +---------------------------- 2 files changed, 3 insertions(+), 35 deletions(-) diff --git a/examples/src/bin/texture.rs b/examples/src/bin/texture.rs index e3cdfd9..89df311 100644 --- a/examples/src/bin/texture.rs +++ b/examples/src/bin/texture.rs @@ -396,9 +396,8 @@ fn main() { width: image_dimensions.0, height: image_dimensions.1, depth: 1, - }, - ..Default::default() - }]; + }).build()]; + device.cmd_copy_buffer_to_image( texture_command_buffer, image_buffer, @@ -647,10 +646,6 @@ fn main() { polygon_mode: vk::PolygonMode::FILL, ..Default::default() }; - let multisample_state_info = vk::PipelineMultisampleStateCreateInfo { - rasterization_samples: vk::SampleCountFlags::TYPE_1, - ..Default::default() - }; let multisample_state_info = vk::PipelineMultisampleStateCreateInfo::builder() .rasterization_samples(vk::SampleCountFlags::TYPE_1) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 7a96d3e..7431fc3 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -45,6 +45,7 @@ use std::ops::Drop; use std::os::raw::{c_char, c_void}; use std::ptr; + const MAGIC_NUMBER: u32 = 0x07230203; fn spirv_is_little_endian(bytes: &[u8]) -> bool { @@ -88,34 +89,6 @@ macro_rules! offset_of { }}; } -fn u32_from_bytes(bytes: &[u8; 4]) -> u32 { - unsafe { std::mem::transmute(*bytes) } -} - -pub fn bytes_to_u32_vec(bytes: B) -> Vec -where - B: Iterator, -{ - let mut output = vec![]; - let mut buffer: [u8; 4] = [0, 0, 0, 0]; - let mut byte_len: usize = 0; - for (i, b) in bytes.enumerate() { - byte_len = i; - match i % 4 { - 3 => { - buffer[3] = b; - output.push(u32_from_bytes(&buffer)); - buffer = [0, 0, 0, 0]; - } - idx => buffer[idx] = b, - } - } - if (byte_len + 1) % 4 != 0 { - output.push(u32_from_bytes(&buffer)); - } - output -} - pub fn record_submit_commandbuffer( device: &D, command_buffer: vk::CommandBuffer, From bb54ae4c5a039b3ae4120d78a7674fe69fc6e41b Mon Sep 17 00:00:00 2001 From: colin Date: Sun, 9 Dec 2018 08:00:55 -0600 Subject: [PATCH 14/19] refactored to use util::read_spv --- examples/Cargo.toml | 1 - examples/src/bin/texture.rs | 15 ++++----------- examples/src/bin/triangle.rs | 18 +++++------------- examples/src/lib.rs | 35 ----------------------------------- 4 files changed, 9 insertions(+), 60 deletions(-) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 7cfb9ec..1ff2abf 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -7,7 +7,6 @@ authors = ["maik klein "] winit = "0.16" image = "0.10.4" ash = { path = "../ash" } -byteorder = "1.2.7" [target.'cfg(windows)'.dependencies] winapi = { version = "0.3.4", features = ["windef", "winuser"] } diff --git a/examples/src/bin/texture.rs b/examples/src/bin/texture.rs index 89df311..ba1a6f6 100644 --- a/examples/src/bin/texture.rs +++ b/examples/src/bin/texture.rs @@ -5,11 +5,9 @@ extern crate image; use std::default::Default; use std::ffi::CString; use std::fs::File; -use std::io::Read; use std::mem::{self, align_of}; use std::os::raw::c_void; use std::path::Path; -use std::ptr; use ash::util::*; use ash::vk; @@ -548,20 +546,15 @@ fn main() { ]; base.device.update_descriptor_sets(&write_desc_sets, &[]); - let vertex_spv_file = + let mut vertex_spv_file = File::open(Path::new("shader/texture/vert.spv")).expect("Could not find vert.spv."); - let frag_spv_file = + let mut frag_spv_file = File::open(Path::new("shader/texture/frag.spv")).expect("Could not find frag.spv."); - let vertex_bytes: Vec = vertex_spv_file - .bytes() - .filter_map(|byte| byte.ok()) - .collect(); - let vertex_code = bytes_to_u32_vec(&vertex_bytes); + let vertex_code = read_spv(&mut vertex_spv_file).expect("Failed to read vertex shader spv file"); let vertex_shader_info = vk::ShaderModuleCreateInfo::builder().code(&vertex_code); - let frag_bytes: Vec = frag_spv_file.bytes().filter_map(|byte| byte.ok()).collect(); - let frag_code = bytes_to_u32_vec(&frag_bytes); + let frag_code = read_spv(&mut frag_spv_file).expect("Failed to read fragment shader spv file"); let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code); let vertex_shader_module = base diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index 13f1d84..09a4eb2 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -7,11 +7,9 @@ use examples::*; use std::default::Default; use std::ffi::CString; use std::fs::File; -use std::io::Read; use std::mem; use std::mem::align_of; use std::path::Path; -use std::ptr; #[derive(Clone, Debug, Copy)] struct Vertex { @@ -204,23 +202,17 @@ fn main() { base.device .bind_buffer_memory(vertex_input_buffer, vertex_input_buffer_memory, 0) .unwrap(); - let vertex_spv_file = + let mut vertex_spv_file = File::open(Path::new("shader/triangle/vert.spv")).expect("Could not find vert.spv."); - let frag_spv_file = + let mut frag_spv_file = File::open(Path::new("shader/triangle/frag.spv")).expect("Could not find frag.spv."); - let vertex_bytes: Vec = vertex_spv_file - .bytes() - .filter_map(|byte| byte.ok()) - .collect(); - - let vertex_code = bytes_to_u32_vec(&vertex_bytes); - + let vertex_code = read_spv(&mut vertex_spv_file).expect("Failed to read vertex shader spv file"); let vertex_shader_info = vk::ShaderModuleCreateInfo::builder().code(&vertex_code); - let frag_bytes: Vec = frag_spv_file.bytes().filter_map(|byte| byte.ok()).collect(); - let frag_code = bytes_to_u32_vec(&frag_bytes); + let frag_code = read_spv(&mut frag_spv_file).expect("Failed to read fragment shader spv file"); let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code); + let vertex_shader_module = base .device .create_shader_module(&vertex_shader_info, None) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 7b4d165..daa1ba1 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -1,6 +1,5 @@ #[macro_use] extern crate ash; -extern crate byteorder; #[cfg(target_os = "windows")] extern crate winapi; @@ -36,47 +35,13 @@ use ash::extensions::khr::Win32Surface; use ash::extensions::mvk::MacOSSurface; pub use ash::version::{DeviceV1_0, EntryV1_0, InstanceV1_0}; use ash::{vk, Device, Entry, Instance}; -use byteorder::{BigEndian, LittleEndian, ReadBytesExt}; use std::cell::RefCell; use std::default::Default; use std::ffi::{CStr, CString}; -use std::io::Cursor; use std::ops::Drop; use std::os::raw::{c_char, c_void}; use std::ptr; - -const MAGIC_NUMBER: u32 = 0x07230203; - -fn spirv_is_little_endian(bytes: &[u8]) -> bool { - let buffer: [u8; 4] = [bytes[0], bytes[1], bytes[2], bytes[3]]; - let number: u32 = Cursor::new(buffer).read_u32::().unwrap(); - number == MAGIC_NUMBER -} - -pub fn bytes_to_u32_vec(bytes: &[u8]) -> Vec { - let mut output = vec![]; - let mut buffer: [u8; 4] = [0, 0, 0, 0]; - let is_little_endian = spirv_is_little_endian(bytes); - for (i, b) in bytes.iter().enumerate() { - let idx = i % 4; - buffer[idx] = *b; - if idx == 3 { - let new_word = if is_little_endian { - Cursor::new(buffer).read_u32::().unwrap() - } else { - Cursor::new(buffer).read_u32::().unwrap() - }; - output.push(new_word); - buffer = [0, 0, 0, 0]; - } - } - if bytes.len() % 4 != 0 { - output.push(Cursor::new(buffer).read_u32::().unwrap()); - } - output -} - // Simple offset_of macro akin to C++ offsetof #[macro_export] macro_rules! offset_of { From ca651f07cb38acbbe2cf67af7a2a535bf7567dd0 Mon Sep 17 00:00:00 2001 From: colin Date: Sun, 9 Dec 2018 08:01:13 -0600 Subject: [PATCH 15/19] rustfmt --- examples/src/bin/texture.rs | 12 ++++++++---- examples/src/bin/triangle.rs | 6 ++++-- examples/src/lib.rs | 9 ++++++--- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/examples/src/bin/texture.rs b/examples/src/bin/texture.rs index ba1a6f6..87749c5 100644 --- a/examples/src/bin/texture.rs +++ b/examples/src/bin/texture.rs @@ -390,11 +390,13 @@ fn main() { .aspect_mask(vk::ImageAspectFlags::COLOR) .layer_count(1) .build(), - ).image_extent(vk::Extent3D { + ) + .image_extent(vk::Extent3D { width: image_dimensions.0, height: image_dimensions.1, depth: 1, - }).build()]; + }) + .build()]; device.cmd_copy_buffer_to_image( texture_command_buffer, @@ -551,10 +553,12 @@ fn main() { let mut frag_spv_file = File::open(Path::new("shader/texture/frag.spv")).expect("Could not find frag.spv."); - let vertex_code = read_spv(&mut vertex_spv_file).expect("Failed to read vertex shader spv file"); + let vertex_code = + read_spv(&mut vertex_spv_file).expect("Failed to read vertex shader spv file"); let vertex_shader_info = vk::ShaderModuleCreateInfo::builder().code(&vertex_code); - let frag_code = read_spv(&mut frag_spv_file).expect("Failed to read fragment shader spv file"); + let frag_code = + read_spv(&mut frag_spv_file).expect("Failed to read fragment shader spv file"); let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code); let vertex_shader_module = base diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index 09a4eb2..8e18738 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -207,10 +207,12 @@ fn main() { let mut frag_spv_file = File::open(Path::new("shader/triangle/frag.spv")).expect("Could not find frag.spv."); - let vertex_code = read_spv(&mut vertex_spv_file).expect("Failed to read vertex shader spv file"); + let vertex_code = + read_spv(&mut vertex_spv_file).expect("Failed to read vertex shader spv file"); let vertex_shader_info = vk::ShaderModuleCreateInfo::builder().code(&vertex_code); - let frag_code = read_spv(&mut frag_spv_file).expect("Failed to read fragment shader spv file"); + let frag_code = + read_spv(&mut frag_spv_file).expect("Failed to read fragment shader spv file"); let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code); let vertex_shader_module = base diff --git a/examples/src/lib.rs b/examples/src/lib.rs index daa1ba1..362e047 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -579,7 +579,8 @@ impl ExampleBase { .dst_access_mask( vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_READ | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_WRITE, - ).new_layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL) + ) + .new_layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL) .old_layout(vk::ImageLayout::UNDEFINED) .subresource_range( vk::ImageSubresourceRange::builder() @@ -587,7 +588,8 @@ impl ExampleBase { .layer_count(1) .level_count(1) .build(), - ).build(); + ) + .build(); device.cmd_pipeline_barrier( setup_command_buffer, @@ -608,7 +610,8 @@ impl ExampleBase { .level_count(1) .layer_count(1) .build(), - ).image(depth_image) + ) + .image(depth_image) .format(depth_image_create_info.format) .view_type(vk::ImageViewType::TYPE_2D) .build(); From 752fc1f1eee0e1ae60dc3ee53dca46ca2c629bc2 Mon Sep 17 00:00:00 2001 From: colin Date: Sun, 9 Dec 2018 13:35:48 -0600 Subject: [PATCH 16/19] missed some premature .build() calls! --- examples/src/bin/texture.rs | 31 ++++++++--------- examples/src/bin/triangle.rs | 28 +++++++-------- examples/src/lib.rs | 67 ++++++++++++++++-------------------- 3 files changed, 58 insertions(+), 68 deletions(-) diff --git a/examples/src/bin/texture.rs b/examples/src/bin/texture.rs index 87749c5..66ef3cc 100644 --- a/examples/src/bin/texture.rs +++ b/examples/src/bin/texture.rs @@ -645,8 +645,7 @@ fn main() { }; let multisample_state_info = vk::PipelineMultisampleStateCreateInfo::builder() - .rasterization_samples(vk::SampleCountFlags::TYPE_1) - .build(); + .rasterization_samples(vk::SampleCountFlags::TYPE_1); let noop_stencil_state = vk::StencilOpState { fail_op: vk::StencilOp::KEEP, @@ -683,23 +682,23 @@ fn main() { let dynamic_state_info = vk::PipelineDynamicStateCreateInfo::builder().dynamic_states(&dynamic_state); - let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder() - .stages(&shader_stage_create_infos) - .vertex_input_state(&vertex_input_state_info) - .input_assembly_state(&vertex_input_assembly_state_info) - .viewport_state(&viewport_state_info) - .rasterization_state(&rasterization_info) - .multisample_state(&multisample_state_info) - .depth_stencil_state(&depth_state_info) - .color_blend_state(&color_blend_state) - .dynamic_state(&dynamic_state_info) - .layout(pipeline_layout) - .render_pass(renderpass) - .build(); + let graphic_pipeline_infos = [vk::GraphicsPipelineCreateInfo::builder() + .stages(&shader_stage_create_infos) + .vertex_input_state(&vertex_input_state_info) + .input_assembly_state(&vertex_input_assembly_state_info) + .viewport_state(&viewport_state_info) + .rasterization_state(&rasterization_info) + .multisample_state(&multisample_state_info) + .depth_stencil_state(&depth_state_info) + .color_blend_state(&color_blend_state) + .dynamic_state(&dynamic_state_info) + .layout(pipeline_layout) + .render_pass(renderpass). + build()]; let graphics_pipelines = base .device - .create_graphics_pipelines(vk::PipelineCache::null(), &[graphic_pipeline_info], None) + .create_graphics_pipelines(vk::PipelineCache::null(), &graphic_pipeline_infos, None) .unwrap(); let graphic_pipeline = graphics_pipelines[0]; diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index 8e18738..72ac78c 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -339,23 +339,23 @@ fn main() { let dynamic_state_info = vk::PipelineDynamicStateCreateInfo::builder().dynamic_states(&dynamic_state); - let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder() - .stages(&shader_stage_create_infos) - .vertex_input_state(&vertex_input_state_info) - .input_assembly_state(&vertex_input_assembly_state_info) - .viewport_state(&viewport_state_info) - .rasterization_state(&rasterization_info) - .multisample_state(&multisample_state_info) - .depth_stencil_state(&depth_state_info) - .color_blend_state(&color_blend_state) - .dynamic_state(&dynamic_state_info) - .layout(pipeline_layout) - .render_pass(renderpass) - .build(); + let graphic_pipeline_info = [vk::GraphicsPipelineCreateInfo::builder() + .stages(&shader_stage_create_infos) + .vertex_input_state(&vertex_input_state_info) + .input_assembly_state(&vertex_input_assembly_state_info) + .viewport_state(&viewport_state_info) + .rasterization_state(&rasterization_info) + .multisample_state(&multisample_state_info) + .depth_stencil_state(&depth_state_info) + .color_blend_state(&color_blend_state) + .dynamic_state(&dynamic_state_info) + .layout(pipeline_layout) + .render_pass(renderpass) + .build()]; let graphics_pipelines = base .device - .create_graphics_pipelines(vk::PipelineCache::null(), &[graphic_pipeline_info], None) + .create_graphics_pipelines(vk::PipelineCache::null(), &graphic_pipeline_info, None) .expect("Unable to create graphics pipeline"); let graphic_pipeline = graphics_pipelines[0]; diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 362e047..d4fd991 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -40,7 +40,6 @@ use std::default::Default; use std::ffi::{CStr, CString}; use std::ops::Drop; use std::os::raw::{c_char, c_void}; -use std::ptr; // Simple offset_of macro akin to C++ offsetof #[macro_export] @@ -82,22 +81,19 @@ pub fn record_submit_commandbuffer Date: Sun, 9 Dec 2018 13:41:23 -0600 Subject: [PATCH 17/19] rust fmt --- examples/src/bin/texture.rs | 24 +++++++++++----------- examples/src/bin/triangle.rs | 24 +++++++++++----------- examples/src/lib.rs | 40 ++++++++++++++++++------------------ 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/examples/src/bin/texture.rs b/examples/src/bin/texture.rs index 66ef3cc..8faca40 100644 --- a/examples/src/bin/texture.rs +++ b/examples/src/bin/texture.rs @@ -683,18 +683,18 @@ fn main() { vk::PipelineDynamicStateCreateInfo::builder().dynamic_states(&dynamic_state); let graphic_pipeline_infos = [vk::GraphicsPipelineCreateInfo::builder() - .stages(&shader_stage_create_infos) - .vertex_input_state(&vertex_input_state_info) - .input_assembly_state(&vertex_input_assembly_state_info) - .viewport_state(&viewport_state_info) - .rasterization_state(&rasterization_info) - .multisample_state(&multisample_state_info) - .depth_stencil_state(&depth_state_info) - .color_blend_state(&color_blend_state) - .dynamic_state(&dynamic_state_info) - .layout(pipeline_layout) - .render_pass(renderpass). - build()]; + .stages(&shader_stage_create_infos) + .vertex_input_state(&vertex_input_state_info) + .input_assembly_state(&vertex_input_assembly_state_info) + .viewport_state(&viewport_state_info) + .rasterization_state(&rasterization_info) + .multisample_state(&multisample_state_info) + .depth_stencil_state(&depth_state_info) + .color_blend_state(&color_blend_state) + .dynamic_state(&dynamic_state_info) + .layout(pipeline_layout) + .render_pass(renderpass) + .build()]; let graphics_pipelines = base .device diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index 72ac78c..f13f626 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -340,18 +340,18 @@ fn main() { vk::PipelineDynamicStateCreateInfo::builder().dynamic_states(&dynamic_state); let graphic_pipeline_info = [vk::GraphicsPipelineCreateInfo::builder() - .stages(&shader_stage_create_infos) - .vertex_input_state(&vertex_input_state_info) - .input_assembly_state(&vertex_input_assembly_state_info) - .viewport_state(&viewport_state_info) - .rasterization_state(&rasterization_info) - .multisample_state(&multisample_state_info) - .depth_stencil_state(&depth_state_info) - .color_blend_state(&color_blend_state) - .dynamic_state(&dynamic_state_info) - .layout(pipeline_layout) - .render_pass(renderpass) - .build()]; + .stages(&shader_stage_create_infos) + .vertex_input_state(&vertex_input_state_info) + .input_assembly_state(&vertex_input_assembly_state_info) + .viewport_state(&viewport_state_info) + .rasterization_state(&rasterization_info) + .multisample_state(&multisample_state_info) + .depth_stencil_state(&depth_state_info) + .color_blend_state(&color_blend_state) + .dynamic_state(&dynamic_state_info) + .layout(pipeline_layout) + .render_pass(renderpass) + .build()]; let graphics_pipelines = base .device diff --git a/examples/src/lib.rs b/examples/src/lib.rs index d4fd991..6a952c4 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -87,11 +87,11 @@ pub fn record_submit_commandbuffer Date: Sun, 9 Dec 2018 13:52:03 -0600 Subject: [PATCH 18/19] again, added `use std::ptr;` to lib.rs --- examples/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 6a952c4..69a259e 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -40,6 +40,7 @@ use std::default::Default; use std::ffi::{CStr, CString}; use std::ops::Drop; use std::os::raw::{c_char, c_void}; +use std::ptr; // Simple offset_of macro akin to C++ offsetof #[macro_export] From f0251c157a55e33f209832f45d3b7e79d9d80230 Mon Sep 17 00:00:00 2001 From: colin Date: Sun, 9 Dec 2018 14:33:21 -0600 Subject: [PATCH 19/19] inlining slices when on Device methods --- examples/src/bin/texture.rs | 18 ++++++++++-------- examples/src/bin/triangle.rs | 11 +++++++---- examples/src/lib.rs | 16 ++++++++-------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/examples/src/bin/texture.rs b/examples/src/bin/texture.rs index 8faca40..62d6bea 100644 --- a/examples/src/bin/texture.rs +++ b/examples/src/bin/texture.rs @@ -384,7 +384,7 @@ fn main() { &[], &[texture_barrier], ); - let buffer_copy_regions = [vk::BufferImageCopy::builder() + let buffer_copy_regions = vk::BufferImageCopy::builder() .image_subresource( vk::ImageSubresourceLayers::builder() .aspect_mask(vk::ImageAspectFlags::COLOR) @@ -395,15 +395,14 @@ fn main() { width: image_dimensions.0, height: image_dimensions.1, depth: 1, - }) - .build()]; + }); device.cmd_copy_buffer_to_image( texture_command_buffer, image_buffer, texture_image, vk::ImageLayout::TRANSFER_DST_OPTIMAL, - &buffer_copy_regions, + &[buffer_copy_regions.build()], ); let texture_barrier_end = vk::ImageMemoryBarrier { src_access_mask: vk::AccessFlags::TRANSFER_WRITE, @@ -682,7 +681,7 @@ fn main() { let dynamic_state_info = vk::PipelineDynamicStateCreateInfo::builder().dynamic_states(&dynamic_state); - let graphic_pipeline_infos = [vk::GraphicsPipelineCreateInfo::builder() + let graphic_pipeline_infos = vk::GraphicsPipelineCreateInfo::builder() .stages(&shader_stage_create_infos) .vertex_input_state(&vertex_input_state_info) .input_assembly_state(&vertex_input_assembly_state_info) @@ -693,12 +692,15 @@ fn main() { .color_blend_state(&color_blend_state) .dynamic_state(&dynamic_state_info) .layout(pipeline_layout) - .render_pass(renderpass) - .build()]; + .render_pass(renderpass); let graphics_pipelines = base .device - .create_graphics_pipelines(vk::PipelineCache::null(), &graphic_pipeline_infos, None) + .create_graphics_pipelines( + vk::PipelineCache::null(), + &[graphic_pipeline_infos.build()], + None, + ) .unwrap(); let graphic_pipeline = graphics_pipelines[0]; diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index f13f626..267c315 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -339,7 +339,7 @@ fn main() { let dynamic_state_info = vk::PipelineDynamicStateCreateInfo::builder().dynamic_states(&dynamic_state); - let graphic_pipeline_info = [vk::GraphicsPipelineCreateInfo::builder() + let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder() .stages(&shader_stage_create_infos) .vertex_input_state(&vertex_input_state_info) .input_assembly_state(&vertex_input_assembly_state_info) @@ -350,12 +350,15 @@ fn main() { .color_blend_state(&color_blend_state) .dynamic_state(&dynamic_state_info) .layout(pipeline_layout) - .render_pass(renderpass) - .build()]; + .render_pass(renderpass); let graphics_pipelines = base .device - .create_graphics_pipelines(vk::PipelineCache::null(), &graphic_pipeline_info, None) + .create_graphics_pipelines( + vk::PipelineCache::null(), + &[graphic_pipeline_info.build()], + None, + ) .expect("Unable to create graphics pipeline"); let graphic_pipeline = graphics_pipelines[0]; diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 69a259e..f25f514 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -87,14 +87,15 @@ pub fn record_submit_commandbuffer