Trying to fix rendering

This commit is contained in:
hodasemi 2023-12-04 12:10:49 +01:00
parent e860b2413a
commit bd8d255492
6 changed files with 94 additions and 131 deletions

View file

@ -33,7 +33,7 @@ pub struct LeaderBoardEntry {
} }
impl LeaderBoardEntry { impl LeaderBoardEntry {
const ENTRY: &str = include_str!("leaderboard_entry.xml"); const ENTRY: &'static str = include_str!("leaderboard_entry.xml");
pub fn create_snippet(gui_handler: &Arc<GuiHandler>) -> Result<Arc<GuiSnippet>> { pub fn create_snippet(gui_handler: &Arc<GuiHandler>) -> Result<Arc<GuiSnippet>> {
GuiSnippet::from_str(gui_handler, Self::ENTRY) GuiSnippet::from_str(gui_handler, Self::ENTRY)

View file

@ -58,8 +58,8 @@ pub struct LeaderBoard {
} }
impl LeaderBoard { impl LeaderBoard {
const LEADERBOARD: &str = include_str!("leaderboard_grid.xml"); const LEADERBOARD: &'static str = include_str!("leaderboard_grid.xml");
const DELTABOARD: &str = include_str!("deltaboard_grid.xml"); const DELTABOARD: &'static str = include_str!("deltaboard_grid.xml");
pub fn new( pub fn new(
gui_handler: &Arc<GuiHandler>, gui_handler: &Arc<GuiHandler>,

View file

@ -178,7 +178,7 @@ impl Overlay {
panic!(); panic!();
} }
fn device_collection_mut(&mut self, device: &Arc<Device>) -> &mut DeviceCollection { pub fn device_collection_mut(&mut self, device: &Arc<Device>) -> &mut DeviceCollection {
if let Some(instance_collection) = self if let Some(instance_collection) = self
.instances .instances
.get_mut(&device.physical_device().instance().vk_handle()) .get_mut(&device.physical_device().instance().vk_handle())
@ -208,25 +208,6 @@ impl Overlay {
.insert(handle, queue); .insert(handle, queue);
} }
pub fn queue(&self, device: VkDevice, queue: VkQueue) -> Arc<Mutex<Queue>> {
match self.device_collection(device).queues.get(&queue) {
Some(queue) => queue.clone(),
None => {
write_log!(format!("queue ({queue:?} not found)"));
panic!();
}
}
}
pub fn device_queue(&self, device: VkDevice) -> Arc<Mutex<Queue>> {
self.device_collection(device)
.queues
.values()
.next()
.expect(&format!("no queue for device ({device:?}) present"))
.clone()
}
pub fn insert_swapchain(&mut self, swapchain: Arc<Swapchain>) { pub fn insert_swapchain(&mut self, swapchain: Arc<Swapchain>) {
self.device_collection_mut(swapchain.device()) self.device_collection_mut(swapchain.device())
.swapchains .swapchains
@ -385,7 +366,7 @@ impl Overlay {
Ok(()) Ok(())
} }
pub fn render(&mut self) -> Result<()> { pub fn render(&mut self, wait_semaphores: &[VkSemaphore]) -> Result<&Arc<Semaphore>> {
if self.rfactor_data.is_none() { if self.rfactor_data.is_none() {
self.rfactor_data = RFactorData::new().ok(); self.rfactor_data = RFactorData::new().ok();
@ -402,7 +383,7 @@ impl Overlay {
rfactor.update()?; rfactor.update()?;
} }
self.rendering.as_ref().unwrap().render() self.rendering.as_ref().unwrap().render(wait_semaphores)
} }
} }

View file

@ -1,10 +1,7 @@
use anyhow::Result; use anyhow::Result;
use vulkan_rs::prelude::*; use vulkan_rs::prelude::*;
use std::{ use std::sync::{Arc, Mutex};
sync::{Arc, Mutex},
time::Duration,
};
use crate::write_log; use crate::write_log;
@ -14,6 +11,8 @@ pub struct Rendering {
queue: Arc<Mutex<Queue>>, queue: Arc<Mutex<Queue>>,
signal_semaphore: Arc<Semaphore>,
render_callbacks: Vec<Box<dyn Fn(u32) -> Result<Arc<CommandBuffer>> + Send + Sync>>, render_callbacks: Vec<Box<dyn Fn(u32) -> Result<Arc<CommandBuffer>> + Send + Sync>>,
} }
@ -45,6 +44,8 @@ impl Rendering {
)); ));
Ok(Self { Ok(Self {
signal_semaphore: Semaphore::new(swapchain.device().clone())?,
swapchain, swapchain,
images, images,
@ -65,7 +66,7 @@ impl Rendering {
self.render_callbacks.push(Box::new(f)); self.render_callbacks.push(Box::new(f));
} }
pub fn render(&self) -> Result<()> { pub fn render(&self, wait_semaphores: &[VkSemaphore]) -> Result<&Arc<Semaphore>> {
let image_index = self.swapchain.current_index(); let image_index = self.swapchain.current_index();
let command_buffers: Vec<Arc<CommandBuffer>> = self let command_buffers: Vec<Arc<CommandBuffer>> = self
@ -74,15 +75,30 @@ impl Rendering {
.map(|c| c(image_index)) .map(|c| c(image_index))
.collect::<Result<Vec<Arc<CommandBuffer>>>>()?; .collect::<Result<Vec<Arc<CommandBuffer>>>>()?;
let mut submit = SubmitInfo::default().add_signal_semaphore(&self.signal_semaphore);
for command_buffer in command_buffers.iter() {
submit = submit.add_command_buffer(command_buffer);
}
for wait_semaphore in wait_semaphores {
submit = submit.add_wait_semaphore_vk(*wait_semaphore);
}
write_log!(format!( write_log!(format!(
"submitting {} commandbuffer(s)", "submitting {} commandbuffer(s)",
command_buffers.len() command_buffers.len()
)); ));
write_log!(format!(
"submitting {} wait semaphore(s)",
wait_semaphores.len()
));
let queue = self.queue.lock().unwrap(); self.queue.lock().unwrap().submit(None, &[submit])?;
queue.minimal_submit(Duration::from_secs(10), &command_buffers)?;
Ok(()) write_log!("submitted layer queue");
Ok(&self.signal_semaphore)
} }
pub fn images(&self) -> &Vec<Arc<Image>> { pub fn images(&self) -> &Vec<Arc<Image>> {

View file

@ -65,6 +65,6 @@ create_functions_enum!(
[CreateDevice, create_device], [CreateDevice, create_device],
[DestroyDevice, destroy_device], [DestroyDevice, destroy_device],
[CreateSwapchainKHR, create_swapchain], [CreateSwapchainKHR, create_swapchain],
[AcquireNextImageKHR, acquire_next_image], [DestroySwapchainKHR, destroy_swapchain],
[QueuePresentKHR, present_queue], [QueuePresentKHR, present_queue],
); );

View file

@ -8,10 +8,7 @@ use structs::*;
use vk_handles::*; use vk_handles::*;
use vulkan_rs::prelude::*; use vulkan_rs::prelude::*;
use std::{ffi::c_void, mem, os::raw::c_char, ptr}; use std::{mem, os::raw::c_char, ptr};
static mut ACQUIRE_NEXT_IMAGE: PFN_vkAcquireNextImageKHR =
unsafe { mem::transmute(vkVoidFunction as *const c_void) };
use crate::{get_config, logging, overlay, write_log}; use crate::{get_config, logging, overlay, write_log};
@ -267,15 +264,6 @@ pub(crate) extern "system" fn create_device(
} }
vk_handles_mut().load_device_functions(unsafe { *device }, proc_addr); vk_handles_mut().load_device_functions(unsafe { *device }, proc_addr);
unsafe {
ACQUIRE_NEXT_IMAGE = match vk_handles().handle("vkAcquireNextImageKHR") {
Some(acquire_next_image) => mem::transmute(acquire_next_image),
None => {
write_log!("failed querying vkAcquireNextImageKHR");
return VK_ERROR_INITIALIZATION_FAILED;
}
};
};
let mut overlay = overlay(); let mut overlay = overlay();
@ -426,104 +414,60 @@ pub(crate) extern "system" fn create_swapchain(
VK_SUCCESS VK_SUCCESS
} }
pub(crate) extern "system" fn acquire_next_image( pub(crate) extern "system" fn destroy_swapchain(
device: VkDevice, device: VkDevice,
swapchain: VkSwapchainKHR, swapchain: VkSwapchainKHR,
timeout: u64, allocator: *const VkAllocationCallbacks,
semaphore: VkSemaphore, ) -> () {
fence: VkFence, write_log!(" ================== vulkan layer destroy swapchain ==================");
image_index: *mut u32,
) -> VkResult {
// unsafe {
// match OVERLAY.swapchain(swapchain) {
// Some(sc) => {
// let device = OVERLAY.device();
// let res = device.acquire_next_image( unsafe {
// sc.vk_handle(), if let Some(vk_fn) = vk_handles().handle("vkDestroySwapchainKHR") {
// timeout, let destroy_swapchain: PFN_vkDestroySwapchainKHR = mem::transmute(vk_fn);
// Some(semaphore),
// Some(fence),
// );
// write_log!(format!( destroy_swapchain(device, swapchain, allocator);
// "acquire (swapchain: {:?}) res: {:?}", }
// sc.vk_handle(),
// res
// ));
// match res { let mut overlay = overlay();
// Ok(res) => match res { let device = overlay.device(device);
// OutOfDate::Ok(index) => {
// sc.set_image_index(index);
// *image_index = index;
// VK_SUCCESS
// }
// OutOfDate::OutOfDate => VK_ERROR_OUT_OF_DATE_KHR,
// OutOfDate::TimeOut => VK_TIMEOUT,
// },
// Err(err) => {
// write_log!(format!("failed acquiring next image {:?}", err));
// VK_ERROR_DEVICE_LOST
// }
// }
// }
// None => {
// write_log!("acquired other swapchain image");
// ACQUIRE_NEXT_IMAGE(device, swapchain, timeout, semaphore, fence, image_index)
// }
// }
// }
let res = overlay
unsafe { ACQUIRE_NEXT_IMAGE(device, swapchain, timeout, semaphore, fence, image_index) }; .device_collection_mut(&device)
.swapchains
if res != VK_SUCCESS { .remove(&swapchain);
write_log!(" ================== vulkan layer acquire next image ==================");
write_log!(format!("image index: {}", unsafe { *image_index }));
write_log!(format!("result: {:?}", VkResult::from(res)));
} }
res
} }
pub(crate) extern "system" fn present_queue( pub(crate) extern "system" fn present_queue(
queue: VkQueue, queue: VkQueue,
present_info: *const VkPresentInfoKHR, present_info: *const VkPresentInfoKHR,
) -> VkResult { ) -> VkResult {
// if logging() { if logging() {
// write_log!(" ================== vulkan layer queue present =================="); write_log!(" ================== vulkan layer queue present ==================");
// write_log!(format!("iq: {:?}, cq: {:?}", queue, unsafe { unsafe {
// OVERLAY.queue().lock().unwrap().vk_handle() let swapchains = std::slice::from_raw_parts(
// })); (*present_info).pSwapchains,
// unsafe { (*present_info).swapchainCount as usize,
// let swapchains = std::slice::from_raw_parts( );
// (*present_info).pSwapchains,
// (*present_info).swapchainCount as usize,
// );
// write_log!(format!("present {} swapchain(s)", swapchains.len())); write_log!(format!("present {} swapchain(s)", swapchains.len()));
// for swapchain in swapchains { for swapchain in swapchains {
// write_log!(format!("present swapchain: {:?}", swapchain)); write_log!(format!("present swapchain: {:?}", swapchain));
// if let Some(swch) = OVERLAY.swapchain(*swapchain) { if let Some(swch) = overlay().swapchain(*swapchain) {
// write_log!(format!( write_log!(format!(
// " -> internal swapchain found! ({:?})", " -> internal swapchain found! ({:?})",
// swch.vk_handle() swch.vk_handle()
// )); ));
// } }
// } }
// } }
// } }
// match unsafe { OVERLAY.render() } { let mut overlay = overlay();
// Ok(_) => (), let wait_semaphores = unsafe { (*present_info).wait_semaphores() };
// Err(err) => { let mut res = VK_SUCCESS;
// write_log!(format!("overlay rendering failed: {:?}", err));
// return VK_ERROR_DEVICE_LOST;
// }
// };
let pfn: PFN_vkQueuePresentKHR = match vk_handles().handle("vkQueuePresentKHR") { let pfn: PFN_vkQueuePresentKHR = match vk_handles().handle("vkQueuePresentKHR") {
Some(pfn) => unsafe { mem::transmute(pfn) }, Some(pfn) => unsafe { mem::transmute(pfn) },
@ -533,10 +477,32 @@ pub(crate) extern "system" fn present_queue(
} }
}; };
let res = pfn(queue, present_info); for swapchain_info in unsafe { (*present_info).swapchain_info() } {
if let Some(swapchain) = overlay.swapchain(swapchain_info.swapchain) {
unsafe { swapchain.set_image_index(swapchain_info.index) };
if res != VK_SUCCESS { match overlay.render(wait_semaphores) {
write_log!(format!("queue present failed: {:?}", VkResult::from(res))); Ok(signal_semaphore) => {
let new_present_info = present_info as *mut VkPresentInfoKHR;
unsafe {
(*new_present_info).waitSemaphoreCount = 1;
(*new_present_info).pWaitSemaphores =
[signal_semaphore.vk_handle()].as_ptr();
}
res = pfn(queue, new_present_info);
if res != VK_SUCCESS {
write_log!(format!("queue present failed: {:?}", res));
}
}
Err(err) => {
write_log!(format!("overlay rendering failed: {:?}", err));
return VK_ERROR_DEVICE_LOST;
}
}
}
} }
res res