vk: redo the hello_triangle
This commit is contained in:
parent
1d07c66239
commit
c8bcc34fc6
BIN
librashader-runtime-vk/shader/triangle_simple/frag.spv
Normal file
BIN
librashader-runtime-vk/shader/triangle_simple/frag.spv
Normal file
Binary file not shown.
|
@ -0,0 +1,9 @@
|
||||||
|
#version 450
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 fragColor;
|
||||||
|
|
||||||
|
layout(location = 0) out vec4 outColor;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
outColor = vec4(fragColor, 1.0);
|
||||||
|
}
|
20
librashader-runtime-vk/shader/triangle_simple/shader.vert
Normal file
20
librashader-runtime-vk/shader/triangle_simple/shader.vert
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#version 450
|
||||||
|
|
||||||
|
layout(location = 0) out vec3 fragColor;
|
||||||
|
|
||||||
|
vec2 positions[3] = vec2[](
|
||||||
|
vec2(0.0, -0.5),
|
||||||
|
vec2(0.5, 0.5),
|
||||||
|
vec2(-0.5, 0.5)
|
||||||
|
);
|
||||||
|
|
||||||
|
vec3 colors[3] = vec3[](
|
||||||
|
vec3(1.0, 0.0, 0.0),
|
||||||
|
vec3(0.0, 1.0, 0.0),
|
||||||
|
vec3(0.0, 0.0, 1.0)
|
||||||
|
);
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
|
||||||
|
fragColor = colors[gl_VertexIndex];
|
||||||
|
}
|
BIN
librashader-runtime-vk/shader/triangle_simple/vert.spv
Normal file
BIN
librashader-runtime-vk/shader/triangle_simple/vert.spv
Normal file
Binary file not shown.
38
librashader-runtime-vk/src/hello_triangle/command.rs
Normal file
38
librashader-runtime-vk/src/hello_triangle/command.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
use ash::prelude::VkResult;
|
||||||
|
use ash::vk;
|
||||||
|
use crate::hello_triangle::physicaldevice::find_queue_family;
|
||||||
|
use crate::hello_triangle::vulkan_base::VulkanBase;
|
||||||
|
|
||||||
|
pub struct VulkanCommandPool {
|
||||||
|
pool: vk::CommandPool,
|
||||||
|
device: ash::Device,
|
||||||
|
pub buffers: Vec<vk::CommandBuffer>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VulkanCommandPool {
|
||||||
|
pub fn new(base: &VulkanBase, frames_in_flight: u32) -> VkResult<VulkanCommandPool> {
|
||||||
|
let indices = find_queue_family(&base.instance, base.physical_device.clone());
|
||||||
|
|
||||||
|
let create_info = vk::CommandPoolCreateInfo::builder()
|
||||||
|
.flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER)
|
||||||
|
.queue_family_index(indices.graphics_family())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
let pool = base.device.create_command_pool(&create_info, None)?;
|
||||||
|
let buffer_info = vk::CommandBufferAllocateInfo::builder()
|
||||||
|
.command_pool(pool)
|
||||||
|
.level(vk::CommandBufferLevel::PRIMARY)
|
||||||
|
.command_buffer_count(frames_in_flight)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let buffers = base.device.allocate_command_buffers(&buffer_info)?;
|
||||||
|
Ok(VulkanCommandPool {
|
||||||
|
pool,
|
||||||
|
device: base.device.clone(),
|
||||||
|
buffers,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
27
librashader-runtime-vk/src/hello_triangle/framebuffer.rs
Normal file
27
librashader-runtime-vk/src/hello_triangle/framebuffer.rs
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
use ash::prelude::VkResult;
|
||||||
|
use ash::vk;
|
||||||
|
use crate::hello_triangle::swapchain::VulkanSwapchain;
|
||||||
|
|
||||||
|
pub struct VulkanFramebuffer {
|
||||||
|
pub framebuffer: vk::Framebuffer,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VulkanFramebuffer {
|
||||||
|
pub fn new(device: &ash::Device, image_view: &vk::ImageView, render_pass: &vk::RenderPass, width: u32, height: u32) -> VkResult<VulkanFramebuffer> {
|
||||||
|
let attachments = &[*image_view];
|
||||||
|
let framebuffer_info = vk::FramebufferCreateInfo::builder()
|
||||||
|
.render_pass(*render_pass)
|
||||||
|
.attachments(attachments)
|
||||||
|
.width(width)
|
||||||
|
.height(height)
|
||||||
|
.layers(1)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
let framebuffer = device.create_framebuffer(&framebuffer_info, None)?;
|
||||||
|
Ok(VulkanFramebuffer {
|
||||||
|
framebuffer
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,13 +3,22 @@ mod debug;
|
||||||
mod physicaldevice;
|
mod physicaldevice;
|
||||||
mod surface;
|
mod surface;
|
||||||
mod swapchain;
|
mod swapchain;
|
||||||
|
mod pipeline;
|
||||||
|
mod framebuffer;
|
||||||
|
mod command;
|
||||||
|
mod syncobjects;
|
||||||
|
|
||||||
|
use ash::vk;
|
||||||
use winit::event::{Event, VirtualKeyCode, ElementState, KeyboardInput, WindowEvent};
|
use winit::event::{Event, VirtualKeyCode, ElementState, KeyboardInput, WindowEvent};
|
||||||
use winit::event_loop::{EventLoop, ControlFlow, EventLoopBuilder};
|
use winit::event_loop::{EventLoop, ControlFlow, EventLoopBuilder};
|
||||||
use winit::platform::windows::EventLoopBuilderExtWindows;
|
use winit::platform::windows::EventLoopBuilderExtWindows;
|
||||||
use crate::filter_chain::{FilterChainVulkan, Vulkan};
|
use crate::filter_chain::{FilterChainVulkan, Vulkan};
|
||||||
|
use crate::hello_triangle::command::VulkanCommandPool;
|
||||||
|
use crate::hello_triangle::framebuffer::VulkanFramebuffer;
|
||||||
|
use crate::hello_triangle::pipeline::VulkanPipeline;
|
||||||
use crate::hello_triangle::surface::VulkanSurface;
|
use crate::hello_triangle::surface::VulkanSurface;
|
||||||
use crate::hello_triangle::swapchain::VulkanSwapchain;
|
use crate::hello_triangle::swapchain::VulkanSwapchain;
|
||||||
|
use crate::hello_triangle::syncobjects::SyncObjects;
|
||||||
use crate::hello_triangle::vulkan_base::VulkanBase;
|
use crate::hello_triangle::vulkan_base::VulkanBase;
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
|
@ -24,11 +33,12 @@ impl VulkanWindow {
|
||||||
winit::window::WindowBuilder::new()
|
winit::window::WindowBuilder::new()
|
||||||
.with_title(WINDOW_TITLE)
|
.with_title(WINDOW_TITLE)
|
||||||
.with_inner_size(winit::dpi::LogicalSize::new(WINDOW_WIDTH, WINDOW_HEIGHT))
|
.with_inner_size(winit::dpi::LogicalSize::new(WINDOW_WIDTH, WINDOW_HEIGHT))
|
||||||
|
.with_resizable(false)
|
||||||
.build(event_loop)
|
.build(event_loop)
|
||||||
.expect("Failed to create window.")
|
.expect("Failed to create window.")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main_loop(event_loop: EventLoop<()>) {
|
pub fn main_loop(event_loop: EventLoop<()>, window: winit::window::Window, vulkan: VulkanDraw, mut filter_chain: FilterChainVulkan) {
|
||||||
event_loop.run(move |event, _, control_flow| {
|
event_loop.run(move |event, _, control_flow| {
|
||||||
match event {
|
match event {
|
||||||
| Event::WindowEvent { event, .. } => {
|
| Event::WindowEvent { event, .. } => {
|
||||||
|
@ -41,7 +51,6 @@ impl VulkanWindow {
|
||||||
| KeyboardInput { virtual_keycode, state, .. } => {
|
| KeyboardInput { virtual_keycode, state, .. } => {
|
||||||
match (virtual_keycode, state) {
|
match (virtual_keycode, state) {
|
||||||
| (Some(VirtualKeyCode::Escape), ElementState::Pressed) => {
|
| (Some(VirtualKeyCode::Escape), ElementState::Pressed) => {
|
||||||
dbg!();
|
|
||||||
*control_flow = ControlFlow::Exit
|
*control_flow = ControlFlow::Exit
|
||||||
},
|
},
|
||||||
| _ => {},
|
| _ => {},
|
||||||
|
@ -52,17 +61,134 @@ impl VulkanWindow {
|
||||||
| _ => {},
|
| _ => {},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
| Event::MainEventsCleared => {
|
||||||
|
window.request_redraw();
|
||||||
|
},
|
||||||
|
| Event::RedrawRequested(_window_id) => {
|
||||||
|
VulkanWindow::draw_frame(&vulkan, &mut filter_chain);
|
||||||
|
},
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe fn record_command_buffer(vulkan: &VulkanDraw, swapchain_index: u32) -> vk::CommandBuffer {
|
||||||
|
let clear_values = [
|
||||||
|
vk::ClearValue {
|
||||||
|
color: vk::ClearColorValue {
|
||||||
|
float32: [0.3, 0.3, 0.5, 0.0],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
let render_pass_begin = vk::RenderPassBeginInfo::builder()
|
||||||
|
.render_pass(vulkan.pipeline.renderpass)
|
||||||
|
.framebuffer(vulkan.framebuffers[swapchain_index as usize].framebuffer)
|
||||||
|
.render_area(vk::Rect2D {
|
||||||
|
extent: vulkan.swapchain.extent,
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.clear_values(&clear_values)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
let cmd = vulkan.command_pool.buffers[swapchain_index as usize];
|
||||||
|
vulkan.base.device.reset_command_buffer(cmd, vk::CommandBufferResetFlags::empty())
|
||||||
|
.expect("could not reset command buffer");
|
||||||
|
|
||||||
|
vulkan.base.device.begin_command_buffer(cmd, &vk::CommandBufferBeginInfo::default())
|
||||||
|
.expect("failed to begin command buffer");
|
||||||
|
|
||||||
|
vulkan.base.device
|
||||||
|
.cmd_begin_render_pass(cmd,
|
||||||
|
&render_pass_begin, vk::SubpassContents::INLINE);
|
||||||
|
|
||||||
|
vulkan.base.device
|
||||||
|
.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, vulkan.pipeline.graphic_pipeline);
|
||||||
|
|
||||||
|
vulkan.base.device
|
||||||
|
.cmd_set_viewport(cmd, 0, &[vk::Viewport {
|
||||||
|
max_depth: 1.0,
|
||||||
|
width: vulkan.swapchain.extent.width as f32,
|
||||||
|
height: vulkan.swapchain.extent.height as f32,
|
||||||
|
..Default::default()
|
||||||
|
}]);
|
||||||
|
|
||||||
|
vulkan.base.device
|
||||||
|
.cmd_set_scissor(cmd, 0, &[vk::Rect2D {
|
||||||
|
offset: Default::default(),
|
||||||
|
extent: vulkan.swapchain.extent
|
||||||
|
}]);
|
||||||
|
|
||||||
|
vulkan.base.device.cmd_draw(cmd, 3, 1, 0, 0);
|
||||||
|
vulkan.base.device.cmd_end_render_pass(cmd);
|
||||||
|
|
||||||
|
vulkan.base.device.end_command_buffer(cmd).expect("failed to record commandbuffer");
|
||||||
|
|
||||||
|
cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_frame(vulkan: &VulkanDraw, filter: &mut FilterChainVulkan) {
|
||||||
|
unsafe {
|
||||||
|
vulkan.base.device.wait_for_fences(&[vulkan.sync.in_flight], true, u64::MAX)
|
||||||
|
.unwrap();
|
||||||
|
vulkan.base.device.reset_fences(&[vulkan.sync.in_flight])
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
|
||||||
|
let (swapchain_index, _) = vulkan.swapchain.loader.acquire_next_image(vulkan.swapchain.swapchain, u64::MAX, vulkan.sync.image_available, vk::Fence::null())
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let cmd = Self::record_command_buffer(vulkan, swapchain_index);
|
||||||
|
|
||||||
|
let submit_info = vk::SubmitInfo::builder()
|
||||||
|
.wait_dst_stage_mask(&[vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT])
|
||||||
|
.wait_semaphores(&[vulkan.sync.image_available])
|
||||||
|
.signal_semaphores(&[vulkan.sync.render_finished])
|
||||||
|
.command_buffers(&[cmd])
|
||||||
|
.build();
|
||||||
|
|
||||||
|
vulkan.base.device.queue_submit(vulkan.base.graphics_queue, &[submit_info], vulkan.sync.in_flight)
|
||||||
|
.expect("failed to submit queue");
|
||||||
|
|
||||||
|
let present_info = vk::PresentInfoKHR::builder()
|
||||||
|
.wait_semaphores(&[vulkan.sync.render_finished])
|
||||||
|
.swapchains(&[vulkan.swapchain.swapchain])
|
||||||
|
.image_indices(&[swapchain_index])
|
||||||
|
.build();
|
||||||
|
|
||||||
|
vulkan.swapchain.loader.queue_present(vulkan.base.graphics_queue, &present_info)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find_memorytype_index(
|
||||||
|
memory_req: &vk::MemoryRequirements,
|
||||||
|
memory_prop: &vk::PhysicalDeviceMemoryProperties,
|
||||||
|
flags: vk::MemoryPropertyFlags,
|
||||||
|
) -> Option<u32> {
|
||||||
|
memory_prop.memory_types[..memory_prop.memory_type_count as _]
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.find(|(index, memory_type)| {
|
||||||
|
(1 << index) & memory_req.memory_type_bits != 0
|
||||||
|
&& memory_type.property_flags & flags == flags
|
||||||
|
})
|
||||||
|
.map(|(index, _memory_type)| index as _)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub struct VulkanDraw {
|
pub struct VulkanDraw {
|
||||||
surface: VulkanSurface,
|
surface: VulkanSurface,
|
||||||
base: VulkanBase,
|
base: VulkanBase,
|
||||||
pub swapchain: VulkanSwapchain,
|
pub swapchain: VulkanSwapchain,
|
||||||
|
pub pipeline: VulkanPipeline,
|
||||||
|
pub framebuffers: Vec<VulkanFramebuffer>,
|
||||||
|
pub command_pool: VulkanCommandPool,
|
||||||
|
pub sync: SyncObjects,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main(vulkan: VulkanBase, filter_chain: FilterChainVulkan) {
|
pub fn main(vulkan: VulkanBase, filter_chain: FilterChainVulkan) {
|
||||||
|
@ -78,11 +204,29 @@ pub fn main(vulkan: VulkanBase, filter_chain: FilterChainVulkan) {
|
||||||
let swapchain = VulkanSwapchain::new(&vulkan, &surface, WINDOW_WIDTH, WINDOW_HEIGHT)
|
let swapchain = VulkanSwapchain::new(&vulkan, &surface, WINDOW_WIDTH, WINDOW_HEIGHT)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
let pipeline = unsafe { VulkanPipeline::new(&vulkan, &swapchain) }
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut framebuffers = vec![];
|
||||||
|
for image in &swapchain.image_views {
|
||||||
|
framebuffers.push(VulkanFramebuffer::new(&vulkan.device, image, &pipeline.renderpass, WINDOW_WIDTH, WINDOW_HEIGHT).unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
let command_pool = VulkanCommandPool::new(&vulkan, 3)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let sync = SyncObjects::new(&vulkan.device)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let vulkan = VulkanDraw {
|
let vulkan = VulkanDraw {
|
||||||
surface,
|
surface,
|
||||||
swapchain,
|
swapchain,
|
||||||
base: vulkan
|
base: vulkan,
|
||||||
|
pipeline,
|
||||||
|
framebuffers,
|
||||||
|
command_pool,
|
||||||
|
sync
|
||||||
};
|
};
|
||||||
|
|
||||||
VulkanWindow::main_loop(event_loop);
|
VulkanWindow::main_loop(event_loop, window, vulkan, filter_chain);
|
||||||
}
|
}
|
349
librashader-runtime-vk/src/hello_triangle/pipeline.rs
Normal file
349
librashader-runtime-vk/src/hello_triangle/pipeline.rs
Normal file
|
@ -0,0 +1,349 @@
|
||||||
|
use std::ffi::CStr;
|
||||||
|
use std::io::Cursor;
|
||||||
|
use std::mem::align_of;
|
||||||
|
use ash::prelude::VkResult;
|
||||||
|
use ash::util::{Align, read_spv};
|
||||||
|
use ash::vk;
|
||||||
|
use bytemuck::offset_of;
|
||||||
|
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;
|
||||||
|
|
||||||
|
#[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,
|
||||||
|
)
|
||||||
|
.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()
|
||||||
|
};
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
.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()
|
||||||
|
};
|
||||||
|
|
||||||
|
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
|
||||||
|
let mut vertex_spv_file = Cursor::new(&include_bytes!("../../shader/triangle_simple/vert.spv")[..]);
|
||||||
|
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)
|
||||||
|
.name(CStr::from_bytes_with_nul_unchecked(b"main\0"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
let mut frag_spv_file = Cursor::new(&include_bytes!("../../shader/triangle_simple/frag.spv")[..]);
|
||||||
|
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)
|
||||||
|
.name(CStr::from_bytes_with_nul_unchecked(b"main\0"))
|
||||||
|
.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();
|
||||||
|
|
||||||
|
|
||||||
|
let dynamic_state = vk::PipelineDynamicStateCreateInfo::builder()
|
||||||
|
.dynamic_states(&[vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR])
|
||||||
|
.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);
|
||||||
|
|
||||||
|
let renderpass_attachments = [
|
||||||
|
vk::AttachmentDescription {
|
||||||
|
format: swapchain.format.format,
|
||||||
|
samples: vk::SampleCountFlags::TYPE_1,
|
||||||
|
load_op: vk::AttachmentLoadOp::CLEAR,
|
||||||
|
store_op: vk::AttachmentStoreOp::STORE,
|
||||||
|
final_layout: vk::ImageLayout::PRESENT_SRC_KHR,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
];
|
||||||
|
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,
|
||||||
|
pipeline_layout
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ShaderModule {
|
||||||
|
pub module: vk::ShaderModule,
|
||||||
|
device: ash::Device,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ShaderModule {
|
||||||
|
pub fn new(device: &ash::Device, spirv: Vec<u32>) -> VkResult<ShaderModule> {
|
||||||
|
let create_info = vk::ShaderModuleCreateInfo::builder()
|
||||||
|
.code(&spirv)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
let module = unsafe {
|
||||||
|
device.create_shader_module(&create_info, None)?
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(ShaderModule {
|
||||||
|
module,
|
||||||
|
device: device.clone()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for ShaderModule {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
unsafe {
|
||||||
|
self.device.destroy_shader_module(self.module, None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -6,11 +6,11 @@ use crate::hello_triangle::vulkan_base::VulkanBase;
|
||||||
pub struct VulkanSwapchain {
|
pub struct VulkanSwapchain {
|
||||||
pub swapchain: vk::SwapchainKHR,
|
pub swapchain: vk::SwapchainKHR,
|
||||||
pub loader: ash::extensions::khr::Swapchain,
|
pub loader: ash::extensions::khr::Swapchain,
|
||||||
format: vk::SurfaceFormatKHR,
|
pub format: vk::SurfaceFormatKHR,
|
||||||
extent: vk::Extent2D,
|
pub extent: vk::Extent2D,
|
||||||
mode: vk::PresentModeKHR,
|
mode: vk::PresentModeKHR,
|
||||||
images: Vec<vk::Image>,
|
images: Vec<vk::Image>,
|
||||||
image_views: Vec<vk::ImageView>,
|
pub image_views: Vec<vk::ImageView>,
|
||||||
device: ash::Device,
|
device: ash::Device,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
24
librashader-runtime-vk/src/hello_triangle/syncobjects.rs
Normal file
24
librashader-runtime-vk/src/hello_triangle/syncobjects.rs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
use ash::prelude::VkResult;
|
||||||
|
use ash::vk;
|
||||||
|
|
||||||
|
pub struct SyncObjects {
|
||||||
|
pub image_available: vk::Semaphore,
|
||||||
|
pub render_finished: vk::Semaphore,
|
||||||
|
pub in_flight: vk::Fence
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SyncObjects {
|
||||||
|
pub fn new(device: &ash::Device) -> VkResult<SyncObjects> {
|
||||||
|
unsafe {
|
||||||
|
let image_available = device.create_semaphore(&vk::SemaphoreCreateInfo::default(), None)?;
|
||||||
|
let render_finished = device.create_semaphore(&vk::SemaphoreCreateInfo::default(), None)?;
|
||||||
|
let in_flight = device.create_fence(&vk::FenceCreateInfo::builder().flags(vk::FenceCreateFlags::SIGNALED).build(), None)?;
|
||||||
|
|
||||||
|
Ok(SyncObjects {
|
||||||
|
image_available,
|
||||||
|
render_finished,
|
||||||
|
in_flight
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue