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 {
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>> {
GuiSnippet::from_str(gui_handler, Self::ENTRY)

View file

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

View file

@ -178,7 +178,7 @@ impl Overlay {
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
.instances
.get_mut(&device.physical_device().instance().vk_handle())
@ -208,25 +208,6 @@ impl Overlay {
.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>) {
self.device_collection_mut(swapchain.device())
.swapchains
@ -385,7 +366,7 @@ impl Overlay {
Ok(())
}
pub fn render(&mut self) -> Result<()> {
pub fn render(&mut self, wait_semaphores: &[VkSemaphore]) -> Result<&Arc<Semaphore>> {
if self.rfactor_data.is_none() {
self.rfactor_data = RFactorData::new().ok();
@ -402,7 +383,7 @@ impl Overlay {
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 vulkan_rs::prelude::*;
use std::{
sync::{Arc, Mutex},
time::Duration,
};
use std::sync::{Arc, Mutex};
use crate::write_log;
@ -14,6 +11,8 @@ pub struct Rendering {
queue: Arc<Mutex<Queue>>,
signal_semaphore: Arc<Semaphore>,
render_callbacks: Vec<Box<dyn Fn(u32) -> Result<Arc<CommandBuffer>> + Send + Sync>>,
}
@ -45,6 +44,8 @@ impl Rendering {
));
Ok(Self {
signal_semaphore: Semaphore::new(swapchain.device().clone())?,
swapchain,
images,
@ -65,7 +66,7 @@ impl Rendering {
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 command_buffers: Vec<Arc<CommandBuffer>> = self
@ -74,15 +75,30 @@ impl Rendering {
.map(|c| c(image_index))
.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!(
"submitting {} commandbuffer(s)",
command_buffers.len()
));
write_log!(format!(
"submitting {} wait semaphore(s)",
wait_semaphores.len()
));
let queue = self.queue.lock().unwrap();
queue.minimal_submit(Duration::from_secs(10), &command_buffers)?;
self.queue.lock().unwrap().submit(None, &[submit])?;
Ok(())
write_log!("submitted layer queue");
Ok(&self.signal_semaphore)
}
pub fn images(&self) -> &Vec<Arc<Image>> {

View file

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

View file

@ -8,10 +8,7 @@ use structs::*;
use vk_handles::*;
use vulkan_rs::prelude::*;
use std::{ffi::c_void, mem, os::raw::c_char, ptr};
static mut ACQUIRE_NEXT_IMAGE: PFN_vkAcquireNextImageKHR =
unsafe { mem::transmute(vkVoidFunction as *const c_void) };
use std::{mem, os::raw::c_char, ptr};
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);
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();
@ -426,104 +414,60 @@ pub(crate) extern "system" fn create_swapchain(
VK_SUCCESS
}
pub(crate) extern "system" fn acquire_next_image(
pub(crate) extern "system" fn destroy_swapchain(
device: VkDevice,
swapchain: VkSwapchainKHR,
timeout: u64,
semaphore: VkSemaphore,
fence: VkFence,
image_index: *mut u32,
) -> VkResult {
// unsafe {
// match OVERLAY.swapchain(swapchain) {
// Some(sc) => {
// let device = OVERLAY.device();
allocator: *const VkAllocationCallbacks,
) -> () {
write_log!(" ================== vulkan layer destroy swapchain ==================");
// let res = device.acquire_next_image(
// sc.vk_handle(),
// timeout,
// Some(semaphore),
// Some(fence),
// );
unsafe {
if let Some(vk_fn) = vk_handles().handle("vkDestroySwapchainKHR") {
let destroy_swapchain: PFN_vkDestroySwapchainKHR = mem::transmute(vk_fn);
// write_log!(format!(
// "acquire (swapchain: {:?}) res: {:?}",
// sc.vk_handle(),
// res
// ));
// match res {
// Ok(res) => match res {
// 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 =
unsafe { ACQUIRE_NEXT_IMAGE(device, swapchain, timeout, semaphore, fence, image_index) };
if res != VK_SUCCESS {
write_log!(" ================== vulkan layer acquire next image ==================");
write_log!(format!("image index: {}", unsafe { *image_index }));
write_log!(format!("result: {:?}", VkResult::from(res)));
destroy_swapchain(device, swapchain, allocator);
}
res
let mut overlay = overlay();
let device = overlay.device(device);
overlay
.device_collection_mut(&device)
.swapchains
.remove(&swapchain);
}
}
pub(crate) extern "system" fn present_queue(
queue: VkQueue,
present_info: *const VkPresentInfoKHR,
) -> VkResult {
// if logging() {
// write_log!(" ================== vulkan layer queue present ==================");
// write_log!(format!("iq: {:?}, cq: {:?}", queue, unsafe {
// OVERLAY.queue().lock().unwrap().vk_handle()
// }));
// unsafe {
// let swapchains = std::slice::from_raw_parts(
// (*present_info).pSwapchains,
// (*present_info).swapchainCount as usize,
// );
if logging() {
write_log!(" ================== vulkan layer queue present ==================");
unsafe {
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 {
// write_log!(format!("present swapchain: {:?}", swapchain));
for swapchain in swapchains {
write_log!(format!("present swapchain: {:?}", swapchain));
// if let Some(swch) = OVERLAY.swapchain(*swapchain) {
// write_log!(format!(
// " -> internal swapchain found! ({:?})",
// swch.vk_handle()
// ));
// }
// }
// }
// }
if let Some(swch) = overlay().swapchain(*swapchain) {
write_log!(format!(
" -> internal swapchain found! ({:?})",
swch.vk_handle()
));
}
}
}
}
// match unsafe { OVERLAY.render() } {
// Ok(_) => (),
// Err(err) => {
// write_log!(format!("overlay rendering failed: {:?}", err));
// return VK_ERROR_DEVICE_LOST;
// }
// };
let mut overlay = overlay();
let wait_semaphores = unsafe { (*present_info).wait_semaphores() };
let mut res = VK_SUCCESS;
let pfn: PFN_vkQueuePresentKHR = match vk_handles().handle("vkQueuePresentKHR") {
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) };
match overlay.render(wait_semaphores) {
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: {:?}", VkResult::from(res)));
write_log!(format!("queue present failed: {:?}", res));
}
}
Err(err) => {
write_log!(format!("overlay rendering failed: {:?}", err));
return VK_ERROR_DEVICE_LOST;
}
}
}
}
res