2023-01-10 11:17:13 +11:00
|
|
|
use crate::hello_triangle::find_memorytype_index;
|
|
|
|
use crate::hello_triangle::surface::VulkanSurface;
|
|
|
|
use crate::hello_triangle::swapchain::VulkanSwapchain;
|
|
|
|
use crate::hello_triangle::vulkan_base::VulkanBase;
|
2023-01-10 14:54:54 +11:00
|
|
|
use ash::prelude::VkResult;
|
|
|
|
use ash::util::{read_spv, Align};
|
|
|
|
use ash::vk;
|
|
|
|
use bytemuck::offset_of;
|
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::io::Cursor;
|
|
|
|
use std::mem::align_of;
|
2023-01-13 16:07:03 +11:00
|
|
|
const ENTRY_POINT: &'static CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"main\0") } ;
|
2023-01-10 11:17:13 +11:00
|
|
|
|
|
|
|
#[derive(Default, Clone, Debug, Copy)]
|
|
|
|
struct Vertex {
|
|
|
|
pos: [f32; 4],
|
|
|
|
color: [f32; 4],
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct VulkanPipeline {
|
|
|
|
pub graphic_pipeline: vk::Pipeline,
|
|
|
|
pub renderpass: vk::RenderPass,
|
|
|
|
pub pipeline_layout: vk::PipelineLayout,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VulkanPipeline {
|
|
|
|
pub unsafe fn new(base: &VulkanBase, swapchain: &VulkanSwapchain) -> VkResult<VulkanPipeline> {
|
|
|
|
// upload buffers
|
|
|
|
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)
|
|
|
|
.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(
|
|
|
|
&index_buffer_memory_req,
|
|
|
|
&base.mem_props,
|
|
|
|
vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT,
|
|
|
|
)
|
2023-01-10 14:54:54 +11:00
|
|
|
.expect("Unable to find suitable memorytype for the index buffer.");
|
2023-01-10 11:17:13 +11:00
|
|
|
|
|
|
|
let index_allocate_info = vk::MemoryAllocateInfo {
|
|
|
|
allocation_size: index_buffer_memory_req.size,
|
|
|
|
memory_type_index: index_buffer_memory_index,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let index_buffer_memory = base
|
|
|
|
.device
|
|
|
|
.allocate_memory(&index_allocate_info, None)
|
|
|
|
.unwrap();
|
|
|
|
let index_ptr = base
|
|
|
|
.device
|
|
|
|
.map_memory(
|
|
|
|
index_buffer_memory,
|
|
|
|
0,
|
|
|
|
index_buffer_memory_req.size,
|
|
|
|
vk::MemoryMapFlags::empty(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let mut index_slice = Align::new(
|
|
|
|
index_ptr,
|
|
|
|
align_of::<u32>() as u64,
|
|
|
|
index_buffer_memory_req.size,
|
|
|
|
);
|
|
|
|
index_slice.copy_from_slice(&index_buffer_data);
|
|
|
|
base.device.unmap_memory(index_buffer_memory);
|
|
|
|
base.device
|
|
|
|
.bind_buffer_memory(index_buffer, index_buffer_memory, 0)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let vertex_input_buffer_info = vk::BufferCreateInfo {
|
|
|
|
size: 3 * std::mem::size_of::<Vertex>() as u64,
|
|
|
|
usage: vk::BufferUsageFlags::VERTEX_BUFFER,
|
|
|
|
sharing_mode: vk::SharingMode::EXCLUSIVE,
|
|
|
|
..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.mem_props,
|
|
|
|
vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT,
|
|
|
|
)
|
2023-01-10 14:54:54 +11:00
|
|
|
.expect("Unable to find suitable memorytype for the vertex buffer.");
|
2023-01-10 11:17:13 +11:00
|
|
|
|
|
|
|
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()
|
|
|
|
};
|
|
|
|
|
|
|
|
let vertex_input_buffer_memory = base
|
|
|
|
.device
|
|
|
|
.allocate_memory(&vertex_buffer_allocate_info, None)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let vertices = [
|
|
|
|
// green
|
|
|
|
Vertex {
|
|
|
|
pos: [0.5, 0.5, 0.0, 1.0],
|
|
|
|
color: [0.0, 1.0, 0.0, 1.0],
|
|
|
|
},
|
|
|
|
// blue
|
|
|
|
Vertex {
|
|
|
|
pos: [-0.5, 0.5, 0.0, 1.0],
|
|
|
|
color: [0.0, 0.0, 1.0, 1.0],
|
|
|
|
},
|
|
|
|
Vertex {
|
|
|
|
pos: [0.0f32, -0.5, 0.0, 1.0],
|
|
|
|
color: [1.0, 0.0, 0.0, 1.0],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let vert_ptr = base
|
|
|
|
.device
|
|
|
|
.map_memory(
|
|
|
|
vertex_input_buffer_memory,
|
|
|
|
0,
|
|
|
|
vertex_input_buffer_memory_req.size,
|
|
|
|
vk::MemoryMapFlags::empty(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut vert_align = Align::new(
|
|
|
|
vert_ptr,
|
|
|
|
align_of::<Vertex>() as u64,
|
|
|
|
vertex_input_buffer_memory_req.size,
|
|
|
|
);
|
|
|
|
vert_align.copy_from_slice(&vertices);
|
|
|
|
base.device.unmap_memory(vertex_input_buffer_memory);
|
|
|
|
base.device
|
|
|
|
.bind_buffer_memory(vertex_input_buffer, vertex_input_buffer_memory, 0)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// create pipeline
|
2023-01-10 14:54:54 +11:00
|
|
|
let mut vertex_spv_file =
|
|
|
|
Cursor::new(&include_bytes!("../../shader/triangle_simple/vert.spv")[..]);
|
2023-01-10 11:17:13 +11:00
|
|
|
let vertex_code =
|
|
|
|
read_spv(&mut vertex_spv_file).expect("Failed to read vertex shader spv file");
|
|
|
|
let vertex_shader_info = ShaderModule::new(&base.device, vertex_code)?;
|
|
|
|
let vertex_stage_info = vk::PipelineShaderStageCreateInfo::builder()
|
|
|
|
.module(vertex_shader_info.module)
|
|
|
|
.stage(vk::ShaderStageFlags::VERTEX)
|
2023-01-13 16:07:03 +11:00
|
|
|
.name(ENTRY_POINT)
|
2023-01-10 11:17:13 +11:00
|
|
|
.build();
|
|
|
|
|
2023-01-10 14:54:54 +11:00
|
|
|
let mut frag_spv_file =
|
|
|
|
Cursor::new(&include_bytes!("../../shader/triangle_simple/frag.spv")[..]);
|
2023-01-10 11:17:13 +11:00
|
|
|
let frag_code =
|
|
|
|
read_spv(&mut frag_spv_file).expect("Failed to read fragment shader spv file");
|
|
|
|
let frag_shader_info = ShaderModule::new(&base.device, frag_code)?;
|
|
|
|
let frag_stage_info = vk::PipelineShaderStageCreateInfo::builder()
|
|
|
|
.module(frag_shader_info.module)
|
|
|
|
.stage(vk::ShaderStageFlags::FRAGMENT)
|
2023-01-13 16:07:03 +11:00
|
|
|
.name(ENTRY_POINT)
|
2023-01-10 11:17:13 +11:00
|
|
|
.build();
|
|
|
|
|
|
|
|
let vertex_input_state_info = vk::PipelineVertexInputStateCreateInfo::builder()
|
|
|
|
.vertex_attribute_descriptions(&[])
|
|
|
|
.vertex_binding_descriptions(&[])
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo::builder()
|
|
|
|
.primitive_restart_enable(false)
|
|
|
|
.topology(vk::PrimitiveTopology::TRIANGLE_LIST)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let viewports = [vk::Viewport {
|
|
|
|
x: 0.0,
|
|
|
|
y: 0.0,
|
|
|
|
width: swapchain.extent.width as f32,
|
|
|
|
height: swapchain.extent.height as f32,
|
|
|
|
min_depth: 0.0,
|
|
|
|
max_depth: 1.0,
|
|
|
|
}];
|
|
|
|
let scissors = [vk::Rect2D {
|
|
|
|
offset: Default::default(),
|
|
|
|
extent: swapchain.extent,
|
|
|
|
}];
|
|
|
|
|
|
|
|
let layout_create_info = vk::PipelineLayoutCreateInfo::default();
|
|
|
|
|
|
|
|
let pipeline_layout = base
|
|
|
|
.device
|
|
|
|
.create_pipeline_layout(&layout_create_info, None)
|
|
|
|
.unwrap();
|
|
|
|
|
2023-01-13 16:07:03 +11:00
|
|
|
let states = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR];
|
2023-01-10 11:17:13 +11:00
|
|
|
let dynamic_state = vk::PipelineDynamicStateCreateInfo::builder()
|
2023-01-13 16:07:03 +11:00
|
|
|
.dynamic_states(&states)
|
2023-01-10 11:17:13 +11:00
|
|
|
.build();
|
|
|
|
|
|
|
|
let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder()
|
|
|
|
.scissors(&scissors)
|
|
|
|
.viewports(&viewports);
|
|
|
|
|
|
|
|
let rs_state_info = vk::PipelineRasterizationStateCreateInfo::builder()
|
|
|
|
.depth_clamp_enable(false)
|
|
|
|
.depth_bias_enable(false)
|
|
|
|
.rasterizer_discard_enable(false)
|
|
|
|
.polygon_mode(vk::PolygonMode::FILL)
|
|
|
|
.line_width(1.0f32)
|
|
|
|
.cull_mode(vk::CullModeFlags::BACK)
|
|
|
|
.front_face(vk::FrontFace::CLOCKWISE)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let multisample = vk::PipelineMultisampleStateCreateInfo::builder()
|
|
|
|
.rasterization_samples(vk::SampleCountFlags::TYPE_1)
|
|
|
|
.min_sample_shading(1.0f32)
|
|
|
|
.sample_shading_enable(false)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let color_blend_attachment = [vk::PipelineColorBlendAttachmentState::builder()
|
|
|
|
.blend_enable(false)
|
|
|
|
.color_write_mask(vk::ColorComponentFlags::RGBA)
|
|
|
|
.src_color_blend_factor(vk::BlendFactor::ONE)
|
|
|
|
.dst_color_blend_factor(vk::BlendFactor::ZERO)
|
|
|
|
.color_blend_op(vk::BlendOp::ADD)
|
|
|
|
.src_alpha_blend_factor(vk::BlendFactor::ONE)
|
|
|
|
.dst_alpha_blend_factor(vk::BlendFactor::ZERO)
|
|
|
|
.alpha_blend_op(vk::BlendOp::ADD)
|
|
|
|
.build()];
|
|
|
|
//
|
|
|
|
// let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState {
|
|
|
|
// blend_enable: 0,
|
|
|
|
// src_color_blend_factor: vk::BlendFactor::SRC_COLOR,
|
|
|
|
// dst_color_blend_factor: vk::BlendFactor::ONE_MINUS_DST_COLOR,
|
|
|
|
// color_blend_op: vk::BlendOp::ADD,
|
|
|
|
// src_alpha_blend_factor: vk::BlendFactor::ZERO,
|
|
|
|
// dst_alpha_blend_factor: vk::BlendFactor::ZERO,
|
|
|
|
// alpha_blend_op: vk::BlendOp::ADD,
|
|
|
|
// color_write_mask: vk::ColorComponentFlags::RGBA,
|
|
|
|
// }];
|
|
|
|
let color_blend_state = vk::PipelineColorBlendStateCreateInfo::builder()
|
|
|
|
.logic_op(vk::LogicOp::COPY)
|
|
|
|
.attachments(&color_blend_attachment);
|
|
|
|
|
2023-01-10 14:54:54 +11:00
|
|
|
let renderpass_attachments = [vk::AttachmentDescription {
|
|
|
|
format: swapchain.format.format,
|
|
|
|
samples: vk::SampleCountFlags::TYPE_1,
|
|
|
|
load_op: vk::AttachmentLoadOp::CLEAR,
|
|
|
|
store_op: vk::AttachmentStoreOp::STORE,
|
2023-01-11 11:35:36 +11:00
|
|
|
initial_layout: vk::ImageLayout::UNDEFINED,
|
|
|
|
final_layout: vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL,
|
2023-01-10 14:54:54 +11:00
|
|
|
..Default::default()
|
|
|
|
}];
|
2023-01-10 11:17:13 +11:00
|
|
|
let color_attachment_refs = [vk::AttachmentReference {
|
|
|
|
attachment: 0,
|
|
|
|
layout: vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL,
|
|
|
|
}];
|
|
|
|
|
|
|
|
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_refs)
|
|
|
|
.pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS);
|
|
|
|
|
|
|
|
let renderpass_create_info = vk::RenderPassCreateInfo::builder()
|
|
|
|
.attachments(&renderpass_attachments)
|
|
|
|
.subpasses(std::slice::from_ref(&subpass))
|
|
|
|
.dependencies(&dependencies);
|
|
|
|
|
|
|
|
let renderpass = base
|
|
|
|
.device
|
|
|
|
.create_render_pass(&renderpass_create_info, None)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let infos = [vertex_stage_info, frag_stage_info];
|
|
|
|
let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::builder()
|
|
|
|
.stages(&infos)
|
|
|
|
.vertex_input_state(&vertex_input_state_info)
|
|
|
|
.input_assembly_state(&vertex_input_assembly_state_info)
|
|
|
|
.viewport_state(&viewport_state_info)
|
|
|
|
.rasterization_state(&rs_state_info)
|
|
|
|
.multisample_state(&multisample)
|
|
|
|
.color_blend_state(&color_blend_state)
|
|
|
|
.dynamic_state(&dynamic_state)
|
|
|
|
.layout(pipeline_layout)
|
|
|
|
.render_pass(renderpass);
|
|
|
|
|
|
|
|
let graphics_pipelines = base
|
|
|
|
.device
|
|
|
|
.create_graphics_pipelines(
|
|
|
|
vk::PipelineCache::null(),
|
|
|
|
&[graphic_pipeline_info.build()],
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("Unable to create graphics pipeline");
|
|
|
|
|
|
|
|
let graphic_pipeline = graphics_pipelines[0];
|
|
|
|
|
|
|
|
Ok(VulkanPipeline {
|
|
|
|
graphic_pipeline,
|
|
|
|
renderpass,
|
2023-01-10 14:54:54 +11:00
|
|
|
pipeline_layout,
|
2023-01-10 11:17:13 +11:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ShaderModule {
|
|
|
|
pub module: vk::ShaderModule,
|
|
|
|
device: ash::Device,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ShaderModule {
|
|
|
|
pub fn new(device: &ash::Device, spirv: Vec<u32>) -> VkResult<ShaderModule> {
|
2023-01-10 14:54:54 +11:00
|
|
|
let create_info = vk::ShaderModuleCreateInfo::builder().code(&spirv).build();
|
2023-01-10 11:17:13 +11:00
|
|
|
|
2023-01-10 14:54:54 +11:00
|
|
|
let module = unsafe { device.create_shader_module(&create_info, None)? };
|
2023-01-10 11:17:13 +11:00
|
|
|
|
|
|
|
Ok(ShaderModule {
|
|
|
|
module,
|
2023-01-10 14:54:54 +11:00
|
|
|
device: device.clone(),
|
2023-01-10 11:17:13 +11:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for ShaderModule {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
self.device.destroy_shader_module(self.module, None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|