Fix errors
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 0s

This commit is contained in:
Florian RICHER 2024-11-20 20:08:31 +01:00
parent 1dc9da0d61
commit 2590db0a06
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
8 changed files with 123 additions and 88 deletions

View file

@ -29,7 +29,9 @@ mod vk_semaphore;
pub(self) use vk_semaphore::VkSemaphore; pub(self) use vk_semaphore::VkSemaphore;
mod vk_command_pool; mod vk_command_pool;
pub(self) use vk_command_pool::VkCommandPool;
mod vk_framebuffer; mod vk_framebuffer;
pub(self) use vk_framebuffer::VkFramebuffer;
mod utils; mod utils;

View file

@ -10,14 +10,18 @@ pub struct VkCommandPool {
} }
impl VkCommandPool { impl VkCommandPool {
pub fn new(device: Arc<VkDevice>) -> VkResult<Self> { pub fn new(device: &Arc<VkDevice>) -> VkResult<Self> {
let command_pool_info = vk::CommandPoolCreateInfo::default() let command_pool_info =
.queue_family_index(device.queue_family_index); vk::CommandPoolCreateInfo::default().queue_family_index(device.queue_family_index);
let command_pool = unsafe { device.handle.create_command_pool(&command_pool_info, None)? }; let command_pool = unsafe {
device
.handle
.create_command_pool(&command_pool_info, None)?
};
log::debug!("Command pool created ({command_pool:?})"); log::debug!("Command pool created ({command_pool:?})");
Ok(Self { Ok(Self {
device, device: device.clone(),
handle: command_pool, handle: command_pool,
}) })
} }
@ -28,4 +32,4 @@ impl Drop for VkCommandPool {
unsafe { self.device.handle.destroy_command_pool(self.handle, None) }; unsafe { self.device.handle.destroy_command_pool(self.handle, None) };
log::debug!("Command pool destroyed ({:?})", self.handle); log::debug!("Command pool destroyed ({:?})", self.handle);
} }
} }

View file

@ -16,7 +16,7 @@ pub struct VkDevice {
impl VkDevice { impl VkDevice {
pub(super) fn new_graphics_device( pub(super) fn new_graphics_device(
instance: Arc<VkInstance>, instance: &Arc<VkInstance>,
physical_device: &VkPhysicalDevice, physical_device: &VkPhysicalDevice,
queue_family_index: u32, queue_family_index: u32,
) -> anyhow::Result<Self> { ) -> anyhow::Result<Self> {
@ -56,7 +56,7 @@ impl VkDevice {
let swapchain_loader = ash::khr::swapchain::Device::new(&instance.handle, &device); let swapchain_loader = ash::khr::swapchain::Device::new(&instance.handle, &device);
Ok(Self { Ok(Self {
instance, instance: instance.clone(),
handle: device, handle: device,
swapchain_loader, swapchain_loader,
queue_family_index, queue_family_index,

View file

@ -1,30 +1,24 @@
use crate::vulkan::vk_render_pass::VkRenderPass; use crate::vulkan::vk_render_pass::VkRenderPass;
use crate::vulkan::{VkDevice, VkSwapchain}; use crate::vulkan::{VkDevice, VkSwapchain};
use ash::prelude::VkResult;
use ash::vk; use ash::vk;
use std::sync::Arc; use std::sync::Arc;
pub struct VkSwapchainFramebuffer { pub struct VkFramebuffer {
device: Arc<VkDevice>, device: Arc<VkDevice>,
swapchain: Arc<VkSwapchain>, image_view: Arc<vk::ImageView>,
render_pass: Arc<VkRenderPass>, render_pass: Arc<VkRenderPass>,
image_view_index: usize,
pub(super) handle: vk::Framebuffer, pub(super) handle: vk::Framebuffer,
} }
impl VkSwapchainFramebuffer { impl VkFramebuffer {
pub fn new( pub fn from_swapchain_image_view(
device: Arc<VkDevice>, device: &Arc<VkDevice>,
swapchain: Arc<VkSwapchain>, render_pass: &Arc<VkRenderPass>,
render_pass: Arc<VkRenderPass>, image_view: &Arc<vk::ImageView>,
image_view_index: usize, swapchain: &VkSwapchain,
) -> VkResult<Self> { ) -> anyhow::Result<Self> {
let present_image_view = swapchain let attachments = [*image_view.as_ref()];
.present_image_views
.as_ref()
.unwrap()[image_view_index];
let attachments = [present_image_view];
let framebuffer_info = vk::FramebufferCreateInfo::default() let framebuffer_info = vk::FramebufferCreateInfo::default()
.render_pass(render_pass.handle) .render_pass(render_pass.handle)
.width(swapchain.surface_resolution.width) .width(swapchain.surface_resolution.width)
@ -35,18 +29,17 @@ impl VkSwapchainFramebuffer {
let framebuffer = unsafe { device.handle.create_framebuffer(&framebuffer_info, None)? }; let framebuffer = unsafe { device.handle.create_framebuffer(&framebuffer_info, None)? };
Ok(Self { Ok(Self {
device, device: device.clone(),
swapchain, render_pass: render_pass.clone(),
render_pass, image_view: image_view.clone(),
image_view_index,
handle: framebuffer, handle: framebuffer,
}) })
} }
} }
impl Drop for VkSwapchainFramebuffer { impl Drop for VkFramebuffer {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { self.device.handle.destroy_framebuffer(self.handle, None) }; unsafe { self.device.handle.destroy_framebuffer(self.handle, None) };
} }
} }

View file

@ -17,9 +17,9 @@ pub struct VkGraphicsPipeline {
impl VkGraphicsPipeline { impl VkGraphicsPipeline {
pub fn new( pub fn new(
device: Arc<VkDevice>, device: &Arc<VkDevice>,
swapchain: Arc<VkSwapchain>, swapchain: &Arc<VkSwapchain>,
render_pass: Arc<VkRenderPass>, render_pass: &Arc<VkRenderPass>,
) -> anyhow::Result<Self> { ) -> anyhow::Result<Self> {
let shader_entry_name = CStr::from_bytes_with_nul(b"main\0")?; let shader_entry_name = CStr::from_bytes_with_nul(b"main\0")?;
@ -107,9 +107,9 @@ impl VkGraphicsPipeline {
log::debug!("Pipeline created ({pipeline_layout:?})"); log::debug!("Pipeline created ({pipeline_layout:?})");
Ok(Self { Ok(Self {
device, device: device.clone(),
swapchain, swapchain: swapchain.clone(),
render_pass, render_pass: render_pass.clone(),
pipeline_layout, pipeline_layout,
pipeline, pipeline,
vertex_shader: vert_shader_module, vertex_shader: vert_shader_module,
@ -124,8 +124,10 @@ impl Drop for VkGraphicsPipeline {
self.device.handle.destroy_pipeline(self.pipeline, None); self.device.handle.destroy_pipeline(self.pipeline, None);
log::debug!("Pipeline destroyed ({:?})", self.pipeline); log::debug!("Pipeline destroyed ({:?})", self.pipeline);
self.device.handle.destroy_pipeline_layout(self.pipeline_layout, None); self.device
.handle
.destroy_pipeline_layout(self.pipeline_layout, None);
log::debug!("Pipeline layout destroyed ({:?})", self.pipeline_layout); log::debug!("Pipeline layout destroyed ({:?})", self.pipeline_layout);
} }
} }
} }

View file

@ -1,7 +1,8 @@
use crate::vulkan::vk_command_pool::VkCommandPool; use crate::vulkan::{
use crate::vulkan::{VkDevice, VkGraphicsPipeline, VkInstance, VkPhysicalDevice, VkRenderPass, VkSemaphore, VkSurface, VkSwapchain}; VkCommandPool, VkDevice, VkFramebuffer, VkGraphicsPipeline, VkInstance, VkPhysicalDevice,
VkRenderPass, VkSemaphore, VkSurface, VkSwapchain,
};
use ash::vk; use ash::vk;
use std::mem::swap;
use std::sync::Arc; use std::sync::Arc;
pub struct VkRenderContext { pub struct VkRenderContext {
@ -11,6 +12,7 @@ pub struct VkRenderContext {
swapchain: Arc<VkSwapchain>, swapchain: Arc<VkSwapchain>,
render_pass: Arc<VkRenderPass>, render_pass: Arc<VkRenderPass>,
framebuffers: Vec<Arc<VkFramebuffer>>,
pipeline: Arc<VkGraphicsPipeline>, pipeline: Arc<VkGraphicsPipeline>,
command_pool: VkCommandPool, command_pool: VkCommandPool,
command_buffers: Vec<vk::CommandBuffer>, command_buffers: Vec<vk::CommandBuffer>,
@ -34,66 +36,81 @@ impl VkRenderContext {
Some(vk::QueueFlags::GRAPHICS), Some(vk::QueueFlags::GRAPHICS),
Some(&surface), Some(&surface),
) )
.ok_or_else(|| anyhow::anyhow!("Unable to find physical device"))?; .ok_or_else(|| anyhow::anyhow!("Unable to find physical device"))?;
log::debug!( log::debug!(
"Selected queue {properties:#?} for physical device {:?}", "Selected queue {properties:#?} for physical device {:?}",
physical_device.properties.device_name_as_c_str() physical_device.properties.device_name_as_c_str()
); );
let device = Arc::new(VkDevice::new_graphics_device( let device = Arc::new(VkDevice::new_graphics_device(
instance.clone(), &instance,
&physical_device, &physical_device,
queue_family_index, queue_family_index,
)?); )?);
let swapchain = Arc::new(VkSwapchain::new( let swapchain = Arc::new(VkSwapchain::new(
&window, &window,
surface.clone(), &surface,
device.clone(), &device,
&physical_device, &physical_device,
)?); )?);
let render_pass = Arc::new(VkRenderPass::new( let render_pass = Arc::new(VkRenderPass::new(&device, &swapchain)?);
device.clone(),
swapchain.clone(),
)?);
let framebuffers = swapchain.create_framebuffers(render_pass.clone()) let framebuffers = swapchain
.create_framebuffers(&render_pass)
.ok_or_else(|| anyhow::anyhow!("Failed to get framebuffers"))?; .ok_or_else(|| anyhow::anyhow!("Failed to get framebuffers"))?;
let pipeline = Arc::new(VkGraphicsPipeline::new( let pipeline = Arc::new(VkGraphicsPipeline::new(&device, &swapchain, &render_pass)?);
device.clone(),
swapchain.clone(),
render_pass.clone(),
)?);
let command_pool = VkCommandPool::new(device.clone())?; let command_pool = VkCommandPool::new(&device)?;
let command_buffer_info = vk::CommandBufferAllocateInfo::default() let command_buffer_info = vk::CommandBufferAllocateInfo::default()
.command_pool(command_pool.handle) .command_pool(command_pool.handle)
.level(vk::CommandBufferLevel::PRIMARY) .level(vk::CommandBufferLevel::PRIMARY)
.command_buffer_count(framebuffers.as_ref().unwrap().len() as u32); .command_buffer_count(framebuffers.len() as u32);
// Destroyed with command pool // Destroyed with command pool
let command_buffers = unsafe { device.handle.allocate_command_buffers(&command_buffer_info)? }; let command_buffers = unsafe {
device
.handle
.allocate_command_buffers(&command_buffer_info)?
};
// Same in VkGraphicsPipeline (TODO: Refactor this) // Same in VkGraphicsPipeline (TODO: Refactor this)
let render_area = vk::Rect2D::default().extent(swapchain.surface_resolution); let render_area = vk::Rect2D::default().extent(swapchain.surface_resolution);
let clear_value = vk::ClearValue::default(); let clear_value = vk::ClearValue::default();
for (index, command_buffer) in command_buffers.iter().enumerate() { for (index, command_buffer) in command_buffers.iter().enumerate() {
let command_buffer_begin_info = vk::CommandBufferBeginInfo::default(); let command_buffer_begin_info = vk::CommandBufferBeginInfo::default();
unsafe { device.handle.begin_command_buffer(*command_buffer, &command_buffer_begin_info)? }; unsafe {
device
.handle
.begin_command_buffer(*command_buffer, &command_buffer_begin_info)?
};
let clear_values = [clear_value]; let clear_values = [clear_value];
let framebuffer = framebuffers[index].as_ref();
let render_pass_begin_info = vk::RenderPassBeginInfo::default() let render_pass_begin_info = vk::RenderPassBeginInfo::default()
.render_pass(render_pass.handle) .render_pass(render_pass.handle)
.framebuffer(framebuffers[index]) .framebuffer(framebuffer.handle)
.render_area(render_area) .render_area(render_area)
.clear_values(&clear_values); .clear_values(&clear_values);
unsafe { device.handle.cmd_begin_render_pass(*command_buffer, &render_pass_begin_info, vk::SubpassContents::INLINE); }; unsafe {
device.handle.cmd_begin_render_pass(
*command_buffer,
&render_pass_begin_info,
vk::SubpassContents::INLINE,
);
};
unsafe { device.handle.cmd_bind_pipeline(*command_buffer, vk::PipelineBindPoint::GRAPHICS, pipeline.pipeline) }; unsafe {
device.handle.cmd_bind_pipeline(
*command_buffer,
vk::PipelineBindPoint::GRAPHICS,
pipeline.pipeline,
)
};
unsafe { device.handle.cmd_draw(*command_buffer, 3, 1, 0, 0) }; unsafe { device.handle.cmd_draw(*command_buffer, 3, 1, 0, 0) };
@ -112,6 +129,7 @@ impl VkRenderContext {
swapchain, swapchain,
render_pass, render_pass,
framebuffers,
pipeline, pipeline,
command_pool, command_pool,
@ -123,10 +141,14 @@ impl VkRenderContext {
} }
pub fn render(&mut self) -> anyhow::Result<()> { pub fn render(&mut self) -> anyhow::Result<()> {
let queue = self.device.get_device_queue(0) let queue = self
.device
.get_device_queue(0)
.ok_or_else(|| anyhow::anyhow!("Failed to get a queue"))?; .ok_or_else(|| anyhow::anyhow!("Failed to get a queue"))?;
let (index, _) = self.swapchain.acquire_next_image(&self.image_available_semaphore)?; let (index, _) = self
.swapchain
.acquire_next_image(&self.image_available_semaphore)?;
let wait_semaphores = [self.image_available_semaphore.handle]; let wait_semaphores = [self.image_available_semaphore.handle];
let signal_semaphores = [self.render_finished_semaphore.handle]; let signal_semaphores = [self.render_finished_semaphore.handle];
@ -138,7 +160,11 @@ impl VkRenderContext {
.command_buffers(&command_buffers_to_submit) .command_buffers(&command_buffers_to_submit)
.signal_semaphores(&signal_semaphores); .signal_semaphores(&signal_semaphores);
unsafe { self.device.handle.queue_submit(*queue, &[submit_info], vk::Fence::null())? }; unsafe {
self.device
.handle
.queue_submit(*queue, &[submit_info], vk::Fence::null())?
};
Ok(()) Ok(())
} }

View file

@ -11,10 +11,7 @@ pub struct VkRenderPass {
} }
impl VkRenderPass { impl VkRenderPass {
pub fn new( pub fn new(device: &Arc<VkDevice>, swapchain: &Arc<VkSwapchain>) -> VkResult<Self> {
device: Arc<VkDevice>,
swapchain: Arc<VkSwapchain>,
) -> VkResult<Self> {
let color_attachment = vk::AttachmentDescription::default() let color_attachment = vk::AttachmentDescription::default()
.format(swapchain.surface_format.format) .format(swapchain.surface_format.format)
.samples(vk::SampleCountFlags::TYPE_1) .samples(vk::SampleCountFlags::TYPE_1)
@ -40,14 +37,12 @@ impl VkRenderPass {
.attachments(&attachments) .attachments(&attachments)
.subpasses(&subpasses); .subpasses(&subpasses);
let render_pass = unsafe { let render_pass = unsafe { device.handle.create_render_pass(&render_pass_info, None)? };
device.handle.create_render_pass(&render_pass_info, None)?
};
log::debug!("Render pass created ({render_pass:?})"); log::debug!("Render pass created ({render_pass:?})");
Ok(Self { Ok(Self {
device, device: device.clone(),
swapchain, swapchain: swapchain.clone(),
handle: render_pass, handle: render_pass,
}) })
} }
@ -60,4 +55,4 @@ impl Drop for VkRenderPass {
log::debug!("Render pass destroyed ({:?})", self.handle); log::debug!("Render pass destroyed ({:?})", self.handle);
} }
} }
} }

View file

@ -1,9 +1,8 @@
use crate::display::Window; use crate::display::Window;
use crate::vulkan::vk_framebuffer::VkSwapchainFramebuffer;
use crate::vulkan::vk_render_pass::VkRenderPass; use crate::vulkan::vk_render_pass::VkRenderPass;
use crate::vulkan::vk_semaphore::VkSemaphore; use crate::vulkan::vk_semaphore::VkSemaphore;
use crate::vulkan::vk_surface::SwapchainSupportDetails; use crate::vulkan::vk_surface::SwapchainSupportDetails;
use crate::vulkan::{VkDevice, VkPhysicalDevice, VkSurface}; use crate::vulkan::{VkDevice, VkFramebuffer, VkPhysicalDevice, VkSurface};
use ash::prelude::VkResult; use ash::prelude::VkResult;
use ash::vk; use ash::vk;
use std::sync::Arc; use std::sync::Arc;
@ -22,14 +21,14 @@ pub struct VkSwapchain {
pub(super) pre_transform: vk::SurfaceTransformFlagsKHR, pub(super) pre_transform: vk::SurfaceTransformFlagsKHR,
pub(super) present_images: Option<Vec<vk::Image>>, pub(super) present_images: Option<Vec<vk::Image>>,
pub(super) present_image_views: Option<Vec<vk::ImageView>>, pub(super) present_image_views: Option<Vec<Arc<vk::ImageView>>>,
} }
impl VkSwapchain { impl VkSwapchain {
pub(super) fn new( pub(super) fn new(
window: &Window, window: &Window,
surface: Arc<VkSurface>, surface: &Arc<VkSurface>,
device: Arc<VkDevice>, device: &Arc<VkDevice>,
physical_device: &VkPhysicalDevice, physical_device: &VkPhysicalDevice,
) -> anyhow::Result<Self> { ) -> anyhow::Result<Self> {
log::debug!("Creating swapchain"); log::debug!("Creating swapchain");
@ -61,8 +60,8 @@ impl VkSwapchain {
let present_mode = Self::choose_present_mode(present_modes); let present_mode = Self::choose_present_mode(present_modes);
let mut swapchain = Self { let mut swapchain = Self {
surface, surface: surface.clone(),
device, device: device.clone(),
swapchain: None, swapchain: None,
swapchain_support_details, swapchain_support_details,
@ -104,6 +103,7 @@ impl VkSwapchain {
self.create_present_image_view(*i) self.create_present_image_view(*i)
.expect("Failed to create image view") .expect("Failed to create image view")
}) })
.map(|i| Arc::new(i))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
if log::log_enabled!(log::Level::Debug) { if log::log_enabled!(log::Level::Debug) {
@ -121,14 +121,27 @@ impl VkSwapchain {
Ok(()) Ok(())
} }
pub(super) fn create_framebuffers(&self, render_pass: Arc<VkRenderPass>) -> Option<Vec<VkSwapchainFramebuffer>> { pub(super) fn create_framebuffers(
&self,
render_pass: &Arc<VkRenderPass>,
) -> Option<Vec<Arc<VkFramebuffer>>> {
let present_image_views = self.present_image_views.as_ref()?; let present_image_views = self.present_image_views.as_ref()?;
present_image_views.iter().enumerate() Some(
.map(|present_image_view| { present_image_views
VkSwapchainFramebuffer::new() .iter()
}) .map(|image_view| {
.collect::<Vec<_>>() VkFramebuffer::from_swapchain_image_view(
&self.device,
&render_pass,
&image_view,
&self,
)
.unwrap()
})
.map(|framebuffer| Arc::new(framebuffer))
.collect::<Vec<_>>(),
)
} }
pub(super) fn update_resolution(&mut self, width: u32, height: u32) -> VkResult<()> { pub(super) fn update_resolution(&mut self, width: u32, height: u32) -> VkResult<()> {