Skip to content

Commit

Permalink
Simplify acquire_frame()
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Nov 15, 2024
1 parent f847a4e commit 8df3ff4
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 55 deletions.
1 change: 1 addition & 0 deletions blade-graphics/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ struct Swapchain {
}

pub struct Surface {
device: khr::swapchain::Device,
raw: vk::SurfaceKHR,
frames: Vec<InternalFrame>,
next_semaphore: vk::Semaphore,
Expand Down
103 changes: 53 additions & 50 deletions blade-graphics/src/vulkan/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,48 @@ impl super::Surface {
alpha: self.swapchain.alpha,
}
}

unsafe fn deinit_swapchain(&mut self, raw_device: &ash::Device) {
self.device
.destroy_swapchain(mem::take(&mut self.swapchain.raw), None);
for frame in self.frames.drain(..) {
raw_device.destroy_image_view(frame.view, None);
raw_device.destroy_semaphore(frame.acquire_semaphore, None);
}
}

pub fn acquire_frame(&mut self) -> super::Frame {
let acquire_semaphore = self.next_semaphore;
match unsafe {
self.device.acquire_next_image(
self.swapchain.raw,
!0,
acquire_semaphore,
vk::Fence::null(),
)
} {
Ok((index, _suboptimal)) => {
self.next_semaphore = mem::replace(
&mut self.frames[index as usize].acquire_semaphore,
acquire_semaphore,
);
super::Frame {
internal: self.frames[index as usize],
swapchain: self.swapchain,
image_index: index,
}
}
Err(vk::Result::ERROR_OUT_OF_DATE_KHR) => {
log::warn!("Acquire failed because the surface is out of date");
super::Frame {
internal: super::InternalFrame::default(),
swapchain: self.swapchain,
image_index: 0,
}
}
Err(other) => panic!("Aquire image error {}", other),
}
}
}

impl super::Context {
Expand All @@ -18,6 +60,12 @@ impl super::Context {
window: &I,
config: crate::SurfaceConfig,
) -> Result<super::Surface, crate::NotSupportedError> {
let khr_swapchain = self
.device
.swapchain
.clone()
.ok_or(crate::NotSupportedError::NoSupportedDeviceFound)?;

let raw = unsafe {
ash_window::create_surface(
&self.entry,
Expand Down Expand Up @@ -72,6 +120,7 @@ impl super::Context {
};

let mut this = super::Surface {
device: khr_swapchain,
raw,
frames: Vec::new(),
next_semaphore,
Expand All @@ -89,7 +138,7 @@ impl super::Context {

pub fn destroy_surface(&self, surface: &mut super::Surface) {
unsafe {
self.deinit_swapchain(surface);
surface.deinit_swapchain(&self.device.core);
self.device
.core
.destroy_semaphore(surface.next_semaphore, None)
Expand All @@ -99,19 +148,7 @@ impl super::Context {
}
}

unsafe fn deinit_swapchain(&self, surface: &mut super::Surface) {
let khr_swapchain = self.device.swapchain.as_ref().unwrap();
khr_swapchain.destroy_swapchain(mem::take(&mut surface.swapchain.raw), None);
for frame in surface.frames.drain(..) {
self.device.core.destroy_image_view(frame.view, None);
self.device
.core
.destroy_semaphore(frame.acquire_semaphore, None);
}
}

pub fn reconfigure_surface(&self, surface: &mut super::Surface, config: crate::SurfaceConfig) {
let khr_swapchain = self.device.swapchain.as_ref().unwrap();
let khr_surface = self.instance.surface.as_ref().unwrap();

let capabilities = unsafe {
Expand Down Expand Up @@ -298,13 +335,13 @@ impl super::Context {
} else if !config.allow_exclusive_full_screen {
log::info!("Unable to forbid exclusive full screen");
}
let raw_swapchain = unsafe { khr_swapchain.create_swapchain(&create_info, None).unwrap() };
let raw_swapchain = unsafe { surface.device.create_swapchain(&create_info, None).unwrap() };

unsafe {
self.deinit_swapchain(surface);
surface.deinit_swapchain(&self.device.core);
}

let images = unsafe { khr_swapchain.get_swapchain_images(raw_swapchain).unwrap() };
let images = unsafe { surface.device.get_swapchain_images(raw_swapchain).unwrap() };
let target_size = [config.size.width as u16, config.size.height as u16];
let subresource_range = vk::ImageSubresourceRange {
aspect_mask: vk::ImageAspectFlags::COLOR,
Expand Down Expand Up @@ -347,38 +384,4 @@ impl super::Context {
target_size,
};
}

pub fn acquire_frame(&self, surface: &mut super::Surface) -> super::Frame {
let khr_swapchain = self.device.swapchain.as_ref().unwrap();
let acquire_semaphore = surface.next_semaphore;
match unsafe {
khr_swapchain.acquire_next_image(
surface.swapchain.raw,
!0,
acquire_semaphore,
vk::Fence::null(),
)
} {
Ok((index, _suboptimal)) => {
surface.next_semaphore = mem::replace(
&mut surface.frames[index as usize].acquire_semaphore,
acquire_semaphore,
);
super::Frame {
internal: surface.frames[index as usize],
swapchain: surface.swapchain,
image_index: index,
}
}
Err(vk::Result::ERROR_OUT_OF_DATE_KHR) => {
log::warn!("Acquire failed because the surface is out of date");
super::Frame {
internal: super::InternalFrame::default(),
swapchain: surface.swapchain,
image_index: 0,
}
}
Err(other) => panic!("Aquire image error {}", other),
}
}
}
2 changes: 1 addition & 1 deletion examples/bunnymark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl Example {
if self.window_size == Default::default() {
return;
}
let frame = self.context.acquire_frame(&mut self.surface);
let frame = self.surface.acquire_frame();

self.command_encoder.start();
self.command_encoder.init_texture(frame.texture());
Expand Down
2 changes: 1 addition & 1 deletion examples/particle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Example {
gui_textures: &egui::TexturesDelta,
screen_desc: &blade_egui::ScreenDescriptor,
) {
let frame = self.context.acquire_frame(&mut self.surface);
let frame = self.surface.acquire_frame();
self.command_encoder.start();
self.command_encoder.init_texture(frame.texture());

Expand Down
2 changes: 1 addition & 1 deletion examples/ray-query/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl Example {
}
}

let frame = self.context.acquire_frame(&mut self.surface);
let frame = self.surface.acquire_frame();
self.command_encoder.init_texture(frame.texture());

if let mut pass = self.command_encoder.render(
Expand Down
2 changes: 1 addition & 1 deletion examples/scene/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ impl Example {
}
}

let frame = self.context.acquire_frame(&mut self.surface);
let frame = self.surface.acquire_frame();
command_encoder.init_texture(frame.texture());

if let mut pass = command_encoder.render(
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ impl Engine {
}
}

let frame = self.gpu_context.acquire_frame(&mut self.gpu_surface);
let frame = self.gpu_surface.acquire_frame();
command_encoder.init_texture(frame.texture());

if let mut pass = command_encoder.render(
Expand Down

0 comments on commit 8df3ff4

Please sign in to comment.