vk: fix hello triangle to use render to texture then blit

This commit is contained in:
chyyran 2023-01-09 22:53:28 -05:00
parent c8bcc34fc6
commit 47b5625095
12 changed files with 324 additions and 1221 deletions

View file

@ -87,6 +87,42 @@ impl From<Size<u32>> for vk::Extent3D {
}
}
}
impl From<vk::Extent3D> for Size<u32> {
fn from(value: vk::Extent3D) -> Self {
Size {
width: value.width,
height: value.height,
}
}
}
impl From<vk::Extent2D> for Size<u32> {
fn from(value: vk::Extent2D) -> Self {
Size {
width: value.width,
height: value.height,
}
}
}
impl From<vk::Viewport> for Size<u32> {
fn from(value: vk::Viewport) -> Self {
Size {
width: value.width as u32,
height: value.height as u32,
}
}
}
impl From<&vk::Viewport> for Size<u32> {
fn from(value: &vk::Viewport) -> Self {
Size {
width: value.width as u32,
height: value.height as u32,
}
}
}
impl From<FilterMode> for vk::Filter {
fn from(value: FilterMode) -> Self {

View file

@ -67,6 +67,7 @@ impl TryFrom<VulkanInfo<'_>> for Vulkan {
device.create_pipeline_cache(&vk::PipelineCacheCreateInfo::default(), None)?
};
// the queue is only used for lut loading.. we may want to select our own queue.
let command_pool = unsafe {
device.create_command_pool(
&vk::CommandPoolCreateInfo::builder()
@ -373,8 +374,7 @@ impl FilterChainVulkan {
viewport: &vk::Viewport,
input: &VulkanImage,
options: Option<()>,
wait: vk::Semaphore,
signal: vk::Semaphore,
command_buffer: vk::CommandBuffer
) -> error::Result<()> {
// limit number of passes to those enabled.
let passes = &mut self.passes[0..self.common.config.passes_enabled];
@ -384,14 +384,14 @@ impl FilterChainVulkan {
// pass.draw(index, &self.common, count as u32, 0, viewport, &Default::default(), &Texture {}, &Texture {})
// }
unsafe {
self.vulkan.device.queue_submit(self.vulkan.queue, &[vk::SubmitInfo::builder()
.wait_semaphores(&[wait])
.wait_dst_stage_mask(&[vk::PipelineStageFlags::ALL_COMMANDS],)
.signal_semaphores(&[signal])
.command_buffers(&[])
.build()], vk::Fence::null())?
}
// unsafe {
// self.vulkan.device.queue_submit(self.vulkan.queue, &[vk::SubmitInfo::builder()
// .wait_semaphores(&[wait])
// .wait_dst_stage_mask(&[vk::PipelineStageFlags::ALL_COMMANDS],)
// .signal_semaphores(&[signal])
// .command_buffers(&[])
// .build()], vk::Fence::null())?
// }
Ok(())

View file

@ -58,6 +58,7 @@ impl FilterPass {
pub(crate) fn draw(
&mut self,
cmd: vk::CommandBuffer,
pass_index: usize,
parent: &FilterCommon,
frame_count: u32,
@ -69,14 +70,13 @@ impl FilterPass {
) -> error::Result<()> {
let descriptor = *&self.graphics_pipeline.layout.descriptor_sets[0];
self.build_semantics(pass_index, parent, &output.mvp, frame_count, frame_direction, Size::new(100, 100),
Size::new(100,100),&descriptor, original, source);
self.build_semantics(pass_index, parent, &output.mvp, frame_count, frame_direction, output.output.size,
viewport.into(),&descriptor, original, source);
if let Some(ubo) = &self.reflection.ubo {
// shader_vulkan: 2554 (ra uses uses one big buffer)
// itll be simpler for us if we just use a RingBuffer<vk::Buffer> tbh.
self.ubo_ring.bind_for_frame(descriptor, ubo.binding, &self.uniform_storage)?;
self.ubo_ring.bind_to_descriptor_set(descriptor, ubo.binding, &self.uniform_storage)?;
}
Ok(())

View file

@ -67,75 +67,84 @@ impl OwnedFramebuffer {
}
}
pub struct OutputFramebuffer<'a> {
device: ash::Device,
render_pass: &'a VulkanRenderPass,
pub handle: vk::Framebuffer,
#[derive(Debug, Clone)]
pub(crate) struct OutputFramebuffer {
pub framebuffer: vk::Framebuffer,
pub size: Size<u32>,
pub image: vk::Image,
pub image_view: vk::ImageView,
pub viewport: vk::Viewport,
}
impl<'a> OutputFramebuffer<'a> {
pub fn new(vulkan: &Vulkan, render_pass: &'a VulkanRenderPass, image: vk::Image, size: Size<u32>) -> error::Result<OutputFramebuffer<'a>> {
let image_subresource = vk::ImageSubresourceRange::builder()
.base_mip_level(0)
.base_array_layer(0)
.level_count(1)
.layer_count(1)
.aspect_mask(ImageAspectFlags::COLOR)
.build();
let swizzle_components = vk::ComponentMapping::builder()
.r(vk::ComponentSwizzle::R)
.g(vk::ComponentSwizzle::G)
.b(vk::ComponentSwizzle::B)
.a(vk::ComponentSwizzle::A)
.build();
let mut view_info = vk::ImageViewCreateInfo::builder()
.view_type(ImageViewType::TYPE_2D)
.format(render_pass.format.into())
.image(image.clone())
.subresource_range(image_subresource)
.components(swizzle_components)
.build();
let image_view = unsafe { vulkan.device.create_image_view(&view_info, None)? };
let framebuffer = unsafe {
vulkan.device.create_framebuffer(
&vk::FramebufferCreateInfo::builder()
.render_pass(render_pass.handle)
.attachments(&[image_view])
.width(size.width)
.height(size.height)
.layers(1)
.build(),
None,
)?
};
Ok(OutputFramebuffer {
device: vulkan.device.clone(),
image,
image_view,
render_pass,
size,
handle: framebuffer,
})
}
pub fn get_renderpass_begin_info(&self, area: vk::Rect2D, clear: Option<&[vk::ClearValue]>) -> vk::RenderPassBeginInfo {
let mut builder = vk::RenderPassBeginInfo::builder()
.render_pass(self.render_pass.handle)
.framebuffer(self.handle)
.render_area(area);
if let Some(clear) = clear {
builder = builder.clear_values(clear)
}
builder.build()
}
}
//
// pub struct OutputFramebuffer<'a> {
// device: ash::Device,
// render_pass: &'a VulkanRenderPass,
// pub handle: vk::Framebuffer,
// pub size: Size<u32>,
// pub image: vk::Image,
// pub image_view: vk::ImageView,
// }
//
// impl<'a> OutputFramebuffer<'a> {
// pub fn new(vulkan: &Vulkan, render_pass: &'a VulkanRenderPass, image: vk::Image, size: Size<u32>) -> error::Result<OutputFramebuffer<'a>> {
// let image_subresource = vk::ImageSubresourceRange::builder()
// .base_mip_level(0)
// .base_array_layer(0)
// .level_count(1)
// .layer_count(1)
// .aspect_mask(ImageAspectFlags::COLOR)
// .build();
//
// let swizzle_components = vk::ComponentMapping::builder()
// .r(vk::ComponentSwizzle::R)
// .g(vk::ComponentSwizzle::G)
// .b(vk::ComponentSwizzle::B)
// .a(vk::ComponentSwizzle::A)
// .build();
//
// let mut view_info = vk::ImageViewCreateInfo::builder()
// .view_type(ImageViewType::TYPE_2D)
// .format(render_pass.format.into())
// .image(image.clone())
// .subresource_range(image_subresource)
// .components(swizzle_components)
// .build();
//
// let image_view = unsafe { vulkan.device.create_image_view(&view_info, None)? };
//
// let framebuffer = unsafe {
// vulkan.device.create_framebuffer(
// &vk::FramebufferCreateInfo::builder()
// .render_pass(render_pass.handle)
// .attachments(&[image_view])
// .width(size.width)
// .height(size.height)
// .layers(1)
// .build(),
// None,
// )?
// };
//
// Ok(OutputFramebuffer {
// device: vulkan.device.clone(),
// image,
// image_view,
// render_pass,
// size,
// handle: framebuffer,
// })
// }
//
// pub fn get_renderpass_begin_info(&self, area: vk::Rect2D, clear: Option<&[vk::ClearValue]>) -> vk::RenderPassBeginInfo {
// let mut builder = vk::RenderPassBeginInfo::builder()
// .render_pass(self.render_pass.handle)
// .framebuffer(self.handle)
// .render_area(area);
//
// if let Some(clear) = clear {
// builder = builder.clear_values(clear)
// }
//
// builder.build()
// }
// }

View file

@ -9,6 +9,7 @@ mod command;
mod syncobjects;
use ash::vk;
use ash::vk::RenderingInfo;
use winit::event::{Event, VirtualKeyCode, ElementState, KeyboardInput, WindowEvent};
use winit::event_loop::{EventLoop, ControlFlow, EventLoopBuilder};
use winit::platform::windows::EventLoopBuilderExtWindows;
@ -20,6 +21,7 @@ use crate::hello_triangle::surface::VulkanSurface;
use crate::hello_triangle::swapchain::VulkanSwapchain;
use crate::hello_triangle::syncobjects::SyncObjects;
use crate::hello_triangle::vulkan_base::VulkanBase;
use crate::util;
// Constants
const WINDOW_TITLE: &'static str = "librashader Vulkan";
@ -73,7 +75,7 @@ impl VulkanWindow {
})
}
unsafe fn record_command_buffer(vulkan: &VulkanDraw, swapchain_index: u32) -> vk::CommandBuffer {
unsafe fn record_command_buffer(vulkan: &VulkanDraw, framebuffer: vk::Framebuffer, cmd: vk::CommandBuffer) {
let clear_values = [
vk::ClearValue {
color: vk::ClearColorValue {
@ -84,7 +86,7 @@ impl VulkanWindow {
let render_pass_begin = vk::RenderPassBeginInfo::builder()
.render_pass(vulkan.pipeline.renderpass)
.framebuffer(vulkan.framebuffers[swapchain_index as usize].framebuffer)
.framebuffer(framebuffer)
.render_area(vk::Rect2D {
extent: vulkan.swapchain.extent,
..Default::default()
@ -93,7 +95,6 @@ impl VulkanWindow {
.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");
@ -124,9 +125,9 @@ impl VulkanWindow {
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) {
@ -140,7 +141,77 @@ impl VulkanWindow {
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 cmd = vulkan.render_command_pool.buffers[swapchain_index as usize];
let framebuffer = vulkan.render_framebuffers[swapchain_index as usize].framebuffer;
let framebuffer_image = vulkan.swapchain.render_images[swapchain_index as usize].0;
let swapchain_image = vulkan.swapchain.swapchain_images[swapchain_index as usize];
Self::record_command_buffer(vulkan, framebuffer, cmd);
util::vulkan_image_layout_transition_levels(&vulkan.base.device,
cmd,
framebuffer_image,
1,
vk::ImageLayout::UNDEFINED,
vk::ImageLayout::TRANSFER_SRC_OPTIMAL,
vk::AccessFlags::COLOR_ATTACHMENT_WRITE,
vk::AccessFlags::TRANSFER_READ,
vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT,
vk::PipelineStageFlags::TRANSFER,
vk::QUEUE_FAMILY_IGNORED,
vk::QUEUE_FAMILY_IGNORED
);
let blit_subresource =vk::ImageSubresourceLayers::builder()
.layer_count(1)
.aspect_mask(vk::ImageAspectFlags::COLOR)
.build();
let src_offsets = [
vk::Offset3D { x: 0, y: 0, z: 0 },
vk::Offset3D {
x: vulkan.swapchain.extent.width as i32,
y: vulkan.swapchain.extent.height as i32,
z: 1,
},
];
let dst_offsets = [
vk::Offset3D { x: 0, y: 0, z: 0 },
vk::Offset3D {
x: vulkan.swapchain.extent.width as i32,
y: vulkan.swapchain.extent.height as i32,
z: 1,
},
];
vulkan.base.device.cmd_blit_image(cmd, framebuffer_image, vk::ImageLayout::TRANSFER_SRC_OPTIMAL,
swapchain_image,
vk::ImageLayout::TRANSFER_DST_OPTIMAL,
&[vk::ImageBlit {
src_subresource: blit_subresource,
src_offsets,
dst_subresource: blit_subresource,
dst_offsets,
}],
vk::Filter::LINEAR
);
util::vulkan_image_layout_transition_levels(&vulkan.base.device,
cmd,
swapchain_image,
1,
vk::ImageLayout::UNDEFINED,
vk::ImageLayout::PRESENT_SRC_KHR,
vk::AccessFlags::empty(),
vk::AccessFlags::TRANSFER_READ,
vk::PipelineStageFlags::TRANSFER,
vk::PipelineStageFlags::TRANSFER,
vk::QUEUE_FAMILY_IGNORED,
vk::QUEUE_FAMILY_IGNORED
);
vulkan.base.device.end_command_buffer(cmd).expect("failed to record commandbuffer");
let submit_info = vk::SubmitInfo::builder()
.wait_dst_stage_mask(&[vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT])
@ -186,8 +257,10 @@ pub struct VulkanDraw {
base: VulkanBase,
pub swapchain: VulkanSwapchain,
pub pipeline: VulkanPipeline,
pub framebuffers: Vec<VulkanFramebuffer>,
pub command_pool: VulkanCommandPool,
pub swapchain_framebuffers: Vec<VulkanFramebuffer>,
pub swapchain_command_pool: VulkanCommandPool,
pub render_command_pool: VulkanCommandPool,
pub render_framebuffers: Vec<VulkanFramebuffer>,
pub sync: SyncObjects,
}
@ -207,12 +280,19 @@ pub fn main(vulkan: VulkanBase, filter_chain: FilterChainVulkan) {
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 mut swapchain_framebuffers = vec![];
for image in &swapchain.swapchain_image_views {
swapchain_framebuffers.push(VulkanFramebuffer::new(&vulkan.device, image, &pipeline.renderpass, WINDOW_WIDTH, WINDOW_HEIGHT).unwrap())
}
let command_pool = VulkanCommandPool::new(&vulkan, 3)
let mut render_framebuffers = vec![];
for image in &swapchain.render_image_views {
render_framebuffers.push(VulkanFramebuffer::new(&vulkan.device, image, &pipeline.renderpass, WINDOW_WIDTH, WINDOW_HEIGHT).unwrap())
}
let swapchain_command_pool = VulkanCommandPool::new(&vulkan, 3)
.unwrap();
let render_command_pool = VulkanCommandPool::new(&vulkan, 3)
.unwrap();
let sync = SyncObjects::new(&vulkan.device)
@ -223,9 +303,11 @@ pub fn main(vulkan: VulkanBase, filter_chain: FilterChainVulkan) {
swapchain,
base: vulkan,
pipeline,
framebuffers,
command_pool,
sync
swapchain_framebuffers,
swapchain_command_pool,
sync,
render_command_pool,
render_framebuffers,
};
VulkanWindow::main_loop(event_loop, window, vulkan, filter_chain);

View file

@ -1,7 +1,10 @@
use ash::prelude::VkResult;
use ash::vk;
use ash::vk::Extent3D;
use crate::hello_triangle::surface::VulkanSurface;
use crate::hello_triangle::vulkan_base::VulkanBase;
use crate::util::find_vulkan_memory_type;
use crate::vulkan_primitives::VulkanImageMemory;
pub struct VulkanSwapchain {
pub swapchain: vk::SwapchainKHR,
@ -9,8 +12,11 @@ pub struct VulkanSwapchain {
pub format: vk::SurfaceFormatKHR,
pub extent: vk::Extent2D,
mode: vk::PresentModeKHR,
images: Vec<vk::Image>,
pub image_views: Vec<vk::ImageView>,
pub swapchain_images: Vec<vk::Image>,
pub render_images: Vec<(vk::Image, VulkanImageMemory)>,
pub swapchain_image_views: Vec<vk::ImageView>,
pub render_image_views: Vec<vk::ImageView>,
device: ash::Device,
}
@ -45,7 +51,7 @@ impl VulkanSwapchain {
.clipped(true)
.image_array_layers(1)
// todo: switch to IMAGE_USAGE_TRANSFER_DST
.image_usage(vk::ImageUsageFlags::COLOR_ATTACHMENT)
.image_usage(vk::ImageUsageFlags::TRANSFER_DST)
.build();
let loader = ash::extensions::khr::Swapchain::new(&base.instance, &base.device);
@ -54,9 +60,84 @@ impl VulkanSwapchain {
loader.create_swapchain(&create_info, None)?
};
let images = unsafe { loader.get_swapchain_images(swapchain)? };
let swapchain_images = unsafe { loader.get_swapchain_images(swapchain)? };
let image_views: VkResult<Vec<vk::ImageView>> = images.iter().map(|image| {
let mut render_images = vec![];
for _ in 0..swapchain_images.len() {
let create_info = vk::ImageCreateInfo::builder()
.extent(Extent3D {
width,
height,
depth: 1,
})
.format(format.format)
.image_type(vk::ImageType::TYPE_2D)
.sharing_mode(vk::SharingMode::EXCLUSIVE)
.samples(vk::SampleCountFlags::TYPE_1)
.tiling(vk::ImageTiling::OPTIMAL)
.array_layers(1)
.mip_levels(1)
.usage(
vk::ImageUsageFlags::SAMPLED
| vk::ImageUsageFlags::COLOR_ATTACHMENT
)
.initial_layout(vk::ImageLayout::UNDEFINED);
unsafe {
let image = base.device.create_image(&create_info, None)?;
let mem_reqs = unsafe { base.device.get_image_memory_requirements(image.clone()) };
let alloc_info = vk::MemoryAllocateInfo::builder()
.allocation_size(mem_reqs.size)
.memory_type_index(find_vulkan_memory_type(
&base.mem_props,
mem_reqs.memory_type_bits,
vk::MemoryPropertyFlags::DEVICE_LOCAL,
))
.build();
// todo: optimize by reusing existing memory.
let memory = VulkanImageMemory::new(&base.device, &alloc_info)
.unwrap();
memory.bind(&image)
.unwrap();
render_images.push((image, memory))
}
}
let image_views: VkResult<Vec<vk::ImageView>> = swapchain_images.iter().map(|image| {
let create_info = vk::ImageViewCreateInfo::builder()
.view_type(vk::ImageViewType::TYPE_2D)
.format(format.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::COLOR,
base_mip_level: 0,
level_count: 1,
base_array_layer: 0,
layer_count: 1,
})
.image(*image)
.build();
let view = unsafe {
base.device.create_image_view(&create_info, None)?
};
Ok(view)
}).collect();
let render_image_views: VkResult<Vec<vk::ImageView>> = render_images.iter().map(|(image, _)| {
let create_info = vk::ImageViewCreateInfo::builder()
.view_type(vk::ImageViewType::TYPE_2D)
.format(format.format)
@ -89,8 +170,10 @@ impl VulkanSwapchain {
format,
extent,
mode,
images,
image_views: image_views?,
swapchain_images,
render_images,
swapchain_image_views: image_views?,
render_image_views: render_image_views?,
device: base.device.clone()
})
}
@ -99,7 +182,7 @@ impl VulkanSwapchain {
impl Drop for VulkanSwapchain {
fn drop(&mut self) {
unsafe {
for view in &self.image_views {
for view in &self.swapchain_image_views {
self.device.destroy_image_view(*view, None)
}
self.loader.destroy_swapchain(self.swapchain, None)

View file

@ -1,627 +0,0 @@
extern crate ash;
extern crate winit;
use ash::extensions::{
ext::DebugUtils,
khr::{Surface, Swapchain},
};
use ash::{vk, Entry};
pub use ash::{Device, Instance};
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
use std::borrow::Cow;
use std::cell::RefCell;
use std::default::Default;
use std::ffi::CStr;
use std::ops::Drop;
use std::os::raw::c_char;
use winit::platform::windows::EventLoopBuilderExtWindows;
#[cfg(any(target_os = "macos", target_os = "ios"))]
use ash::vk::{
KhrGetPhysicalDeviceProperties2Fn, KhrPortabilityEnumerationFn, KhrPortabilitySubsetFn,
};
use winit::event_loop::EventLoopBuilder;
use winit::{
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop},
platform::run_return::EventLoopExtRunReturn,
window::WindowBuilder,
};
// Simple offset_of macro akin to C++ offsetof
#[macro_export]
macro_rules! offset_of {
($base:path, $field:ident) => {{
#[allow(unused_unsafe)]
unsafe {
let b: $base = mem::zeroed();
std::ptr::addr_of!(b.$field) as isize - std::ptr::addr_of!(b) as isize
}
}};
}
/// Helper function for submitting command buffers. Immediately waits for the fence before the command buffer
/// is executed. That way we can delay the waiting for the fences by 1 frame which is good for performance.
/// Make sure to create the fence in a signaled state on the first use.
#[allow(clippy::too_many_arguments)]
pub fn record_submit_commandbuffer<F: FnOnce(&Device, vk::CommandBuffer)>(
device: &Device,
command_buffer: vk::CommandBuffer,
command_buffer_reuse_fence: vk::Fence,
submit_queue: vk::Queue,
wait_mask: &[vk::PipelineStageFlags],
wait_semaphores: &[vk::Semaphore],
signal_semaphores: &[vk::Semaphore],
f: F,
) {
unsafe {
device
.wait_for_fences(&[command_buffer_reuse_fence], true, std::u64::MAX)
.expect("Wait for fence failed.");
device
.reset_fences(&[command_buffer_reuse_fence])
.expect("Reset fences failed.");
device
.reset_command_buffer(
command_buffer,
vk::CommandBufferResetFlags::RELEASE_RESOURCES,
)
.expect("Reset command buffer failed.");
let command_buffer_begin_info = vk::CommandBufferBeginInfo::builder()
.flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT);
device
.begin_command_buffer(command_buffer, &command_buffer_begin_info)
.expect("Begin commandbuffer");
f(device, command_buffer);
device
.end_command_buffer(command_buffer)
.expect("End commandbuffer");
let command_buffers = vec![command_buffer];
let submit_info = vk::SubmitInfo::builder()
.wait_semaphores(wait_semaphores)
.wait_dst_stage_mask(wait_mask)
.command_buffers(&command_buffers)
.signal_semaphores(signal_semaphores);
device
.queue_submit(
submit_queue,
&[submit_info.build()],
command_buffer_reuse_fence,
)
.expect("queue submit failed.");
}
}
unsafe extern "system" fn vulkan_debug_callback(
message_severity: vk::DebugUtilsMessageSeverityFlagsEXT,
message_type: vk::DebugUtilsMessageTypeFlagsEXT,
p_callback_data: *const vk::DebugUtilsMessengerCallbackDataEXT,
_user_data: *mut std::os::raw::c_void,
) -> vk::Bool32 {
let callback_data = *p_callback_data;
let message_id_number: i32 = callback_data.message_id_number as i32;
let message_id_name = if callback_data.p_message_id_name.is_null() {
Cow::from("")
} else {
CStr::from_ptr(callback_data.p_message_id_name).to_string_lossy()
};
let message = if callback_data.p_message.is_null() {
Cow::from("")
} else {
CStr::from_ptr(callback_data.p_message).to_string_lossy()
};
println!(
"{:?}:\n{:?} [{} ({})] : {}\n",
message_severity,
message_type,
message_id_name,
&message_id_number.to_string(),
message,
);
vk::FALSE
}
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 ExampleBase {
pub entry: Entry,
pub instance: Instance,
pub device: Device,
pub surface_loader: Surface,
pub swapchain_loader: Swapchain,
pub debug_utils_loader: DebugUtils,
pub window: winit::window::Window,
pub event_loop: RefCell<EventLoop<()>>,
pub debug_call_back: vk::DebugUtilsMessengerEXT,
pub pdevice: vk::PhysicalDevice,
pub device_memory_properties: vk::PhysicalDeviceMemoryProperties,
pub queue_family_index: u32,
pub present_queue: vk::Queue,
pub surface: vk::SurfaceKHR,
pub surface_format: vk::SurfaceFormatKHR,
pub surface_resolution: vk::Extent2D,
pub swapchain: vk::SwapchainKHR,
pub present_images: Vec<vk::Image>,
pub present_image_views: Vec<vk::ImageView>,
pub pool: vk::CommandPool,
pub draw_command_buffer: vk::CommandBuffer,
pub setup_command_buffer: vk::CommandBuffer,
pub depth_image: vk::Image,
pub depth_image_view: vk::ImageView,
pub depth_image_memory: vk::DeviceMemory,
pub present_complete_semaphore: vk::Semaphore,
pub rendering_complete_semaphore: vk::Semaphore,
pub postprocess_complete_semaphore: vk::Semaphore,
pub draw_commands_reuse_fence: vk::Fence,
pub setup_commands_reuse_fence: vk::Fence,
}
impl ExampleBase {
pub fn render_loop<F: FnMut()>(&self, mut f: F) {
self.event_loop
.borrow_mut()
.run_return(|event, _, control_flow| {
*control_flow = ControlFlow::Poll;
match event {
Event::WindowEvent {
event:
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Escape),
..
},
..
},
..
} => *control_flow = ControlFlow::Exit,
Event::MainEventsCleared => f(),
_ => (),
}
});
}
pub fn new(window_width: u32, window_height: u32) -> Self {
unsafe {
let mut event_loop = EventLoopBuilder::new().with_any_thread(true).build();
let window = WindowBuilder::new()
.with_title("Ash - Example")
.with_inner_size(winit::dpi::LogicalSize::new(
f64::from(window_width),
f64::from(window_height),
))
.with_resizable(false)
.build(&event_loop)
.unwrap();
let entry = Entry::linked();
let app_name = CStr::from_bytes_with_nul_unchecked(b"VulkanTriangle\0");
let layer_names = [CStr::from_bytes_with_nul_unchecked(
b"VK_LAYER_KHRONOS_validation\0",
)];
let layers_names_raw: Vec<*const c_char> = layer_names
.iter()
.map(|raw_name| raw_name.as_ptr())
.collect();
let mut extension_names =
ash_window::enumerate_required_extensions(window.raw_display_handle())
.unwrap()
.to_vec();
extension_names.push(DebugUtils::name().as_ptr());
#[cfg(any(target_os = "macos", target_os = "ios"))]
{
extension_names.push(KhrPortabilityEnumerationFn::name().as_ptr());
// Enabling this extension is a requirement when using `VK_KHR_portability_subset`
extension_names.push(KhrGetPhysicalDeviceProperties2Fn::name().as_ptr());
}
let appinfo = vk::ApplicationInfo::builder()
.application_name(app_name)
.application_version(0)
.engine_name(app_name)
.engine_version(0)
.api_version(vk::make_api_version(0, 1, 3, 0));
let create_flags = if cfg!(any(target_os = "macos", target_os = "ios")) {
vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR
} else {
vk::InstanceCreateFlags::default()
};
let create_info = vk::InstanceCreateInfo::builder()
.application_info(&appinfo)
.enabled_layer_names(&layers_names_raw)
.enabled_extension_names(&extension_names)
.flags(create_flags);
let instance: Instance = entry
.create_instance(&create_info, None)
.expect("Instance creation error");
let debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder()
.message_severity(
vk::DebugUtilsMessageSeverityFlagsEXT::ERROR
| vk::DebugUtilsMessageSeverityFlagsEXT::WARNING
| vk::DebugUtilsMessageSeverityFlagsEXT::INFO,
)
.message_type(
vk::DebugUtilsMessageTypeFlagsEXT::GENERAL
| vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION
| vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE,
)
.pfn_user_callback(Some(vulkan_debug_callback));
let debug_utils_loader = DebugUtils::new(&entry, &instance);
let debug_call_back = debug_utils_loader
.create_debug_utils_messenger(&debug_info, None)
.unwrap();
let surface = ash_window::create_surface(
&entry,
&instance,
window.raw_display_handle(),
window.raw_window_handle(),
None,
)
.unwrap();
let pdevices = instance
.enumerate_physical_devices()
.expect("Physical device error");
let surface_loader = Surface::new(&entry, &instance);
let (pdevice, queue_family_index) = pdevices
.iter()
.find_map(|pdevice| {
instance
.get_physical_device_queue_family_properties(*pdevice)
.iter()
.enumerate()
.find_map(|(index, info)| {
let supports_graphic_and_surface =
info.queue_flags.contains(vk::QueueFlags::GRAPHICS)
&& surface_loader
.get_physical_device_surface_support(
*pdevice,
index as u32,
surface,
)
.unwrap();
if supports_graphic_and_surface {
Some((*pdevice, index))
} else {
None
}
})
})
.expect("Couldn't find suitable device.");
let queue_family_index = queue_family_index as u32;
let device_extension_names_raw = [
Swapchain::name().as_ptr(),
#[cfg(any(target_os = "macos", target_os = "ios"))]
KhrPortabilitySubsetFn::name().as_ptr(),
];
let features = vk::PhysicalDeviceFeatures {
shader_clip_distance: 1,
..Default::default()
};
let priorities = [1.0];
let queue_info = vk::DeviceQueueCreateInfo::builder()
.queue_family_index(queue_family_index)
.queue_priorities(&priorities);
let device_create_info = vk::DeviceCreateInfo::builder()
.queue_create_infos(std::slice::from_ref(&queue_info))
.enabled_extension_names(&device_extension_names_raw)
.enabled_features(&features);
let device: Device = instance
.create_device(pdevice, &device_create_info, None)
.unwrap();
let present_queue = device.get_device_queue(queue_family_index as u32, 0);
let surface_format = surface_loader
.get_physical_device_surface_formats(pdevice, surface)
.unwrap()[0];
let surface_capabilities = surface_loader
.get_physical_device_surface_capabilities(pdevice, surface)
.unwrap();
let mut desired_image_count = surface_capabilities.min_image_count + 1;
if surface_capabilities.max_image_count > 0
&& desired_image_count > surface_capabilities.max_image_count
{
desired_image_count = surface_capabilities.max_image_count;
}
let surface_resolution = match surface_capabilities.current_extent.width {
std::u32::MAX => vk::Extent2D {
width: window_width,
height: window_height,
},
_ => surface_capabilities.current_extent,
};
let pre_transform = if surface_capabilities
.supported_transforms
.contains(vk::SurfaceTransformFlagsKHR::IDENTITY)
{
vk::SurfaceTransformFlagsKHR::IDENTITY
} else {
surface_capabilities.current_transform
};
let present_modes = surface_loader
.get_physical_device_surface_present_modes(pdevice, surface)
.unwrap();
let present_mode = present_modes
.iter()
.cloned()
.find(|&mode| mode == vk::PresentModeKHR::MAILBOX)
.unwrap_or(vk::PresentModeKHR::FIFO);
let swapchain_loader = Swapchain::new(&instance, &device);
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)
.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()
.flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER)
.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);
let command_buffers = device
.allocate_command_buffers(&command_buffer_allocate_info)
.unwrap();
let setup_command_buffer = command_buffers[0];
let draw_command_buffer = command_buffers[1];
let present_images = swapchain_loader.get_swapchain_images(swapchain).unwrap();
let present_image_views: Vec<vk::ImageView> = present_images
.iter()
.map(|&image| {
let create_view_info = vk::ImageViewCreateInfo::builder()
.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);
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(surface_resolution.into())
.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);
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(
&depth_image_memory_req,
&device_memory_properties,
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);
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");
let fence_create_info =
vk::FenceCreateInfo::builder().flags(vk::FenceCreateFlags::SIGNALED);
let draw_commands_reuse_fence = device
.create_fence(&fence_create_info, None)
.expect("Create fence failed.");
let setup_commands_reuse_fence = device
.create_fence(&fence_create_info, None)
.expect("Create fence failed.");
record_submit_commandbuffer(
&device,
setup_command_buffer,
setup_commands_reuse_fence,
present_queue,
&[],
&[],
&[],
|device, setup_command_buffer| {
let layout_transition_barriers = 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(),
);
device.cmd_pipeline_barrier(
setup_command_buffer,
vk::PipelineStageFlags::BOTTOM_OF_PIPE,
vk::PipelineStageFlags::LATE_FRAGMENT_TESTS,
vk::DependencyFlags::empty(),
&[],
&[],
&[layout_transition_barriers.build()],
);
},
);
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);
let depth_image_view = device
.create_image_view(&depth_image_view_info, None)
.unwrap();
let semaphore_create_info = vk::SemaphoreCreateInfo::default();
let present_complete_semaphore = device
.create_semaphore(&semaphore_create_info, None)
.unwrap();
let rendering_complete_semaphore = device
.create_semaphore(&semaphore_create_info, None)
.unwrap();
let postprocess_complete_semaphore = device
.create_semaphore(&semaphore_create_info, None)
.unwrap();
ExampleBase {
event_loop: RefCell::new(event_loop),
entry,
instance,
device,
queue_family_index,
pdevice,
device_memory_properties,
window,
surface_loader,
surface_format,
present_queue,
surface_resolution,
swapchain_loader,
swapchain,
present_images,
present_image_views,
pool,
draw_command_buffer,
setup_command_buffer,
depth_image,
depth_image_view,
present_complete_semaphore,
rendering_complete_semaphore,
draw_commands_reuse_fence,
setup_commands_reuse_fence,
surface,
debug_call_back,
debug_utils_loader,
depth_image_memory,
postprocess_complete_semaphore
}
}
}
}
impl Drop for ExampleBase {
fn drop(&mut self) {
unsafe {
self.device.device_wait_idle().unwrap();
self.device
.destroy_semaphore(self.present_complete_semaphore, None);
self.device
.destroy_semaphore(self.rendering_complete_semaphore, None);
self.device
.destroy_semaphore(self.postprocess_complete_semaphore, None);
self.device
.destroy_fence(self.draw_commands_reuse_fence, None);
self.device
.destroy_fence(self.setup_commands_reuse_fence, None);
self.device.free_memory(self.depth_image_memory, None);
self.device.destroy_image_view(self.depth_image_view, None);
self.device.destroy_image(self.depth_image, None);
for &image_view in self.present_image_views.iter() {
self.device.destroy_image_view(image_view, None);
}
self.device.destroy_command_pool(self.pool, None);
self.swapchain_loader
.destroy_swapchain(self.swapchain, None);
self.device.destroy_device(None);
self.surface_loader.destroy_surface(self.surface, None);
self.debug_utils_loader
.destroy_debug_utils_messenger(self.debug_call_back, None);
self.instance.destroy_instance(None);
}
}
}

View file

@ -1,476 +0,0 @@
use crate::offset_of;
use ash::util::*;
use ash::vk;
use std::default::Default;
use std::ffi::CStr;
use std::io::Cursor;
use std::mem;
use std::mem::align_of;
mod base;
pub use base::*;
use crate::filter_chain::FilterChainVulkan;
use crate::texture::VulkanImage;
#[derive(Clone, Debug, Copy)]
struct Vertex {
pos: [f32; 4],
color: [f32; 4],
}
pub(crate) fn main(base: ExampleBase, mut filter_chain: FilterChainVulkan) {
unsafe {
let renderpass_attachments = [
vk::AttachmentDescription {
format: base.surface_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()
},
vk::AttachmentDescription {
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()
},
];
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 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)
.depth_stencil_attachment(&depth_attachment_ref)
.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 framebuffers: Vec<vk::Framebuffer> = 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);
base.device
.create_framebuffer(&frame_buffer_create_info, None)
.unwrap()
})
.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)
.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.device_memory_properties,
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.device_memory_properties,
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();
let mut vertex_spv_file = Cursor::new(&include_bytes!("../../shader/triangle/vert.spv")[..]);
let mut frag_spv_file = Cursor::new(&include_bytes!("../../shader/triangle/frag.spv")[..]);
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_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code);
let vertex_shader_module = base
.device
.create_shader_module(&vertex_shader_info, None)
.expect("Vertex shader module error");
let fragment_shader_module = base
.device
.create_shader_module(&frag_shader_info, None)
.expect("Fragment shader module error");
let layout_create_info = vk::PipelineLayoutCreateInfo::default();
let pipeline_layout = base
.device
.create_pipeline_layout(&layout_create_info, None)
.unwrap();
let shader_entry_name = CStr::from_bytes_with_nul_unchecked(b"main\0");
let shader_stage_create_infos = [
vk::PipelineShaderStageCreateInfo {
module: vertex_shader_module,
p_name: shader_entry_name.as_ptr(),
stage: vk::ShaderStageFlags::VERTEX,
..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()
},
];
let vertex_input_binding_descriptions = [vk::VertexInputBindingDescription {
binding: 0,
stride: mem::size_of::<Vertex>() as u32,
input_rate: vk::VertexInputRate::VERTEX,
}];
let vertex_input_attribute_descriptions = [
vk::VertexInputAttributeDescription {
location: 0,
binding: 0,
format: vk::Format::R32G32B32A32_SFLOAT,
offset: offset_of!(Vertex, pos) as u32,
},
vk::VertexInputAttributeDescription {
location: 1,
binding: 0,
format: vk::Format::R32G32B32A32_SFLOAT,
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);
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,
width: base.surface_resolution.width as f32,
height: base.surface_resolution.height as f32,
min_depth: 0.0,
max_depth: 1.0,
}];
let scissors = [base.surface_resolution.into()];
let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder()
.scissors(&scissors)
.viewports(&viewports);
let rasterization_info = vk::PipelineRasterizationStateCreateInfo {
front_face: vk::FrontFace::COUNTER_CLOCKWISE,
line_width: 1.0,
polygon_mode: vk::PolygonMode::FILL,
..Default::default()
};
let multisample_state_info = vk::PipelineMultisampleStateCreateInfo {
rasterization_samples: vk::SampleCountFlags::TYPE_1,
..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()
};
let depth_state_info = vk::PipelineDepthStencilStateCreateInfo {
depth_test_enable: 1,
depth_write_enable: 1,
depth_compare_op: vk::CompareOp::LESS_OR_EQUAL,
front: noop_stencil_state,
back: noop_stencil_state,
max_depth_bounds: 1.0,
..Default::default()
};
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::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 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);
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];
base.render_loop(|| {
let (present_index, _) = base
.swapchain_loader
.acquire_next_image(
base.swapchain,
std::u64::MAX,
base.present_complete_semaphore,
vk::Fence::null(),
)
.unwrap();
let clear_values = [
vk::ClearValue {
color: vk::ClearColorValue {
float32: [0.3, 0.3, 0.5, 0.0],
},
},
vk::ClearValue {
depth_stencil: vk::ClearDepthStencilValue {
depth: 1.0,
stencil: 0,
},
},
];
let render_pass_begin_info = vk::RenderPassBeginInfo::builder()
.render_pass(renderpass)
.framebuffer(framebuffers[present_index as usize])
.render_area(base.surface_resolution.into())
.clear_values(&clear_values);
record_submit_commandbuffer(
&base.device,
base.draw_command_buffer,
base.draw_commands_reuse_fence,
base.present_queue,
&[vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT],
&[base.present_complete_semaphore],
&[base.rendering_complete_semaphore],
|device, draw_command_buffer| {
device.cmd_begin_render_pass(
draw_command_buffer,
&render_pass_begin_info,
vk::SubpassContents::INLINE,
);
device.cmd_bind_pipeline(
draw_command_buffer,
vk::PipelineBindPoint::GRAPHICS,
graphic_pipeline,
);
device.cmd_set_viewport(draw_command_buffer, 0, &viewports);
device.cmd_set_scissor(draw_command_buffer, 0, &scissors);
device.cmd_bind_vertex_buffers(
draw_command_buffer,
0,
&[vertex_input_buffer],
&[0],
);
device.cmd_bind_index_buffer(
draw_command_buffer,
index_buffer,
0,
vk::IndexType::UINT32,
);
device.cmd_draw_indexed(
draw_command_buffer,
index_buffer_data.len() as u32,
1,
0,
0,
1,
);
// Or draw without the index buffer
// device.cmd_draw(draw_command_buffer, 3, 1, 0, 0);
device.cmd_end_render_pass(draw_command_buffer);
},
);
filter_chain.frame(0, &viewports[0], &VulkanImage {
size: Default::default(),
image: Default::default(),
format: Default::default(),
}, None, base.rendering_complete_semaphore, base.postprocess_complete_semaphore)
.unwrap();
//let mut present_info_err = mem::zeroed();
let wait_semaphors = [base.postprocess_complete_semaphore];
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);
base.swapchain_loader
.queue_present(base.present_queue, &present_info)
.unwrap();
});
base.device.device_wait_idle().unwrap();
for pipeline in graphics_pipelines {
base.device.destroy_pipeline(pipeline, None);
}
base.device.destroy_pipeline_layout(pipeline_layout, None);
base.device
.destroy_shader_module(vertex_shader_module, None);
base.device
.destroy_shader_module(fragment_shader_module, None);
base.device.free_memory(index_buffer_memory, None);
base.device.destroy_buffer(index_buffer, None);
base.device.free_memory(vertex_input_buffer_memory, None);
base.device.destroy_buffer(vertex_input_buffer, None);
for framebuffer in framebuffers {
base.device.destroy_framebuffer(framebuffer, None);
}
base.device.destroy_render_pass(renderpass, None);
}
}

View file

@ -7,7 +7,6 @@ mod error;
mod filter_chain;
mod filter_pass;
mod framebuffer;
mod hello_triangle_old;
mod luts;
mod renderpass;
mod util;

View file

@ -2,9 +2,6 @@ use crate::filter_chain::Vulkan;
use crate::vulkan_primitives::{VulkanBuffer, VulkanImageMemory};
use crate::{error, util};
use ash::vk;
use ash::vk::ImageSubresourceLayers;
use glfw::Key::P;
use librashader_common::{FilterMode, WrapMode};
use librashader_presets::TextureConfig;
use librashader_runtime::image::{Image, BGRA8};
use librashader_runtime::scaling::MipmapSize;
@ -53,7 +50,7 @@ impl LutTexture {
mem_reqs.memory_type_bits,
vk::MemoryPropertyFlags::DEVICE_LOCAL,
);
crate::vulkan_primitives::VulkanImageMemory::new(
VulkanImageMemory::new(
&vulkan.device,
&vk::MemoryAllocateInfo::builder()
.memory_type_index(mem_type)
@ -162,14 +159,14 @@ impl LutTexture {
z: 1,
},
];
let src_subresource = ImageSubresourceLayers::builder()
let src_subresource = vk::ImageSubresourceLayers::builder()
.aspect_mask(vk::ImageAspectFlags::COLOR)
.mip_level(level - 1)
.base_array_layer(0)
.layer_count(1)
.build();
let dst_subresource = ImageSubresourceLayers::builder()
let dst_subresource = vk::ImageSubresourceLayers::builder()
.aspect_mask(vk::ImageAspectFlags::COLOR)
.mip_level(level)
.base_array_layer(0)

View file

@ -1,8 +1,8 @@
use ash::vk;
use crate::framebuffer::VulkanFramebuffer;
use crate::framebuffer::{OutputFramebuffer,};
pub struct RenderTarget {
pub mvp: [f32; 16],
pub image: vk::Image,
pub framebuffer: VulkanFramebuffer
#[derive(Debug, Clone)]
pub(crate) struct RenderTarget<'a> {
pub mvp: &'a [f32; 16],
pub output: OutputFramebuffer,
}

View file

@ -22,7 +22,7 @@ impl VkUboRing {
})
}
pub fn bind_for_frame(&mut self, descriptor_set: vk::DescriptorSet, binding: u32, storage: &impl UniformStorageAccess) -> error::Result<()>{
pub fn bind_to_descriptor_set(&mut self, descriptor_set: vk::DescriptorSet, binding: u32, storage: &impl UniformStorageAccess) -> error::Result<()>{
let mut buffer = self.ring.current_mut();
// todo: write directly to allocated buffer.
unsafe {