Use vulkano_util instead
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 6m30s

This commit is contained in:
Florian RICHER 2025-05-25 19:48:59 +02:00
parent f486486be3
commit a4a6c0c60a
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
9 changed files with 137 additions and 415 deletions

View file

@ -1,28 +1,32 @@
use crate::render::scene::Scene;
use crate::render::vulkan_context::VulkanContext;
use crate::render::window_render_context::WindowRenderContext;
use std::sync::Arc;
use vulkano::command_buffer::{RenderingAttachmentInfo, RenderingInfo};
use vulkano::command_buffer::{
AutoCommandBufferBuilder, CommandBufferUsage, RenderingAttachmentInfo, RenderingInfo,
};
use vulkano::pipeline::graphics::viewport::Viewport;
use vulkano::render_pass::{AttachmentLoadOp, AttachmentStoreOp};
use vulkano::swapchain::{SwapchainPresentInfo, acquire_next_image};
use vulkano::swapchain::{PresentMode, SwapchainPresentInfo};
use vulkano::sync::GpuFuture;
use vulkano::{Validated, VulkanError, sync};
use vulkano_util::context::VulkanoContext;
use vulkano_util::window::{VulkanoWindows, WindowDescriptor};
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::ActiveEventLoop;
use winit::window::WindowId;
use super::vulkan_context::VulkanContext;
pub struct App {
vulkan_context: VulkanContext,
window_render_context: Option<WindowRenderContext>,
vulkano_windows: VulkanoWindows,
scene: Option<Scene>,
}
impl From<VulkanContext> for App {
fn from(vulkan_context: VulkanContext) -> Self {
impl From<VulkanoContext> for App {
fn from(vulkano_context: VulkanoContext) -> Self {
Self {
vulkan_context,
window_render_context: None,
vulkan_context: VulkanContext::new(vulkano_context),
vulkano_windows: VulkanoWindows::default(),
scene: None,
}
}
@ -30,26 +34,23 @@ impl From<VulkanContext> for App {
impl ApplicationHandler for App {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let window_attributes = winit::window::Window::default_attributes()
.with_title("Rust ASH Test")
.with_inner_size(winit::dpi::PhysicalSize::new(
f64::from(800),
f64::from(600),
));
self.vulkano_windows.create_window(
event_loop,
self.vulkan_context.vulkano_context(),
&WindowDescriptor {
title: "Rust ASH Test".to_string(),
width: 800.0,
height: 600.0,
present_mode: PresentMode::Fifo,
..Default::default()
},
|_| {},
);
let window = Arc::new(event_loop.create_window(window_attributes).unwrap());
let surface = self.vulkan_context.create_surface(window.clone());
self.window_render_context = Some(WindowRenderContext::new(
window,
surface,
&self.vulkan_context.device,
));
self.scene = Some(
Scene::load(
&self.vulkan_context,
self.window_render_context.as_ref().unwrap(),
&self.vulkano_windows.get_primary_renderer_mut().unwrap(),
)
.unwrap(),
);
@ -61,45 +62,27 @@ impl ApplicationHandler for App {
log::debug!("The close button was pressed; stopping");
event_loop.exit();
}
WindowEvent::Resized(_) => {
let rcx = self.window_render_context.as_mut().unwrap();
rcx.recreate_swapchain = true;
}
WindowEvent::RedrawRequested => {
let (image_index, acquire_future) = {
let rcx = self.window_render_context.as_mut().unwrap();
let window_size = rcx.window.inner_size();
let renderer = self.vulkano_windows.get_primary_renderer_mut().unwrap();
let acquire_future = renderer.acquire(None, |_| {}).unwrap();
if window_size.width == 0 || window_size.height == 0 {
return;
}
rcx.previous_frame_end.as_mut().unwrap().cleanup_finished();
rcx.update_swapchain().unwrap();
let (image_index, suboptimal, acquire_future) =
match acquire_next_image(rcx.swapchain.clone(), None)
.map_err(Validated::unwrap)
{
Ok(r) => r,
Err(VulkanError::OutOfDate) => {
rcx.recreate_swapchain = true;
return;
}
Err(e) => panic!("failed to acquire next image: {e}"),
};
if suboptimal {
rcx.recreate_swapchain = true;
}
(image_index, acquire_future)
};
let mut builder = self.vulkan_context.create_render_builder();
let mut builder = AutoCommandBufferBuilder::primary(
self.vulkan_context.command_buffer_allocator().clone(),
self.vulkan_context
.vulkano_context()
.graphics_queue()
.queue_family_index(),
CommandBufferUsage::OneTimeSubmit,
)
.unwrap();
{
let rcx = self.window_render_context.as_ref().unwrap();
let viewport = Viewport {
offset: [0.0, 0.0],
extent: renderer.resolution(),
depth_range: 0.0..=1.0,
};
builder
.begin_rendering(RenderingInfo {
color_attachments: vec![Some(RenderingAttachmentInfo {
@ -107,23 +90,19 @@ impl ApplicationHandler for App {
store_op: AttachmentStoreOp::Store,
clear_value: Some([0.0, 0.0, 0.0, 1.0].into()),
..RenderingAttachmentInfo::image_view(
rcx.attachment_image_views[image_index as usize].clone(),
renderer.swapchain_image_view().clone(),
)
})],
..Default::default()
})
.unwrap()
.set_viewport(0, [rcx.viewport.clone()].into_iter().collect())
.set_viewport(0, [viewport].into_iter().collect())
.unwrap();
}
if let Some(scene) = self.scene.as_ref() {
scene
.render(
&self.vulkan_context,
&self.window_render_context.as_ref().unwrap(),
&mut builder,
)
.render(&self.vulkan_context, &renderer, &mut builder)
.unwrap();
}
@ -131,48 +110,24 @@ impl ApplicationHandler for App {
let command_buffer = builder.build().unwrap();
{
let rcx = self.window_render_context.as_mut().unwrap();
let future = acquire_future
.then_execute(
self.vulkan_context
.vulkano_context()
.graphics_queue()
.clone(),
command_buffer,
)
.unwrap();
let future = rcx
.previous_frame_end
.take()
.unwrap()
.join(acquire_future)
.then_execute(self.vulkan_context.graphics_queue.clone(), command_buffer)
.unwrap()
.then_swapchain_present(
self.vulkan_context.graphics_queue.clone(),
SwapchainPresentInfo::swapchain_image_index(
rcx.swapchain.clone(),
image_index,
),
)
.then_signal_fence_and_flush();
match future.map_err(Validated::unwrap) {
Ok(future) => {
rcx.previous_frame_end = Some(future.boxed());
}
Err(VulkanError::OutOfDate) => {
rcx.recreate_swapchain = true;
rcx.previous_frame_end =
Some(sync::now(self.vulkan_context.device.clone()).boxed());
}
Err(e) => {
println!("failed to flush future: {e}");
rcx.previous_frame_end =
Some(sync::now(self.vulkan_context.device.clone()).boxed());
}
}
}
renderer.present(future.boxed(), true);
}
_ => {}
}
}
fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
let rcx = self.window_render_context.as_mut().unwrap();
rcx.window.request_redraw();
let window = self.vulkano_windows.get_primary_window().unwrap();
window.request_redraw();
}
}