Refactor app context and render pass manager
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 9m10s
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 9m10s
This commit is contained in:
parent
f1ae54bc73
commit
2c169548b9
12 changed files with 846 additions and 466 deletions
|
@ -1,23 +1,21 @@
|
|||
use std::{error::Error, sync::Arc};
|
||||
use std::error::Error;
|
||||
|
||||
use super::settings_scene::SettingsScene;
|
||||
use crate::core::app::DEPTH_IMAGE_ID;
|
||||
use crate::core::app::context::ApplicationContext;
|
||||
use crate::core::app::user_event::UserEvent;
|
||||
use crate::core::render::primitives::camera::Camera3D;
|
||||
use crate::core::render::primitives::transform::Transform;
|
||||
use crate::core::render::render_pass_manager::{RenderPassConfig, RenderPassManager};
|
||||
use crate::core::render::texture::Texture;
|
||||
use crate::core::scene::Scene;
|
||||
use crate::core::scene::SceneContext;
|
||||
use crate::game::assets::square::Square;
|
||||
use egui_winit_vulkano::{Gui, egui};
|
||||
use egui_winit_vulkano::egui;
|
||||
use glam::EulerRot;
|
||||
use glam::Quat;
|
||||
use glam::Vec3;
|
||||
use vulkano::{
|
||||
command_buffer::{
|
||||
AutoCommandBufferBuilder, CommandBufferUsage, PrimaryCommandBufferAbstract,
|
||||
RenderingAttachmentInfo, RenderingInfo,
|
||||
},
|
||||
pipeline::graphics::viewport::Viewport,
|
||||
render_pass::{AttachmentLoadOp, AttachmentStoreOp},
|
||||
command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage, PrimaryCommandBufferAbstract},
|
||||
sync::GpuFuture,
|
||||
};
|
||||
use winit::window::CursorGrabMode;
|
||||
|
@ -40,12 +38,22 @@ impl Scene for MainScene {
|
|||
self.state.is_some()
|
||||
}
|
||||
|
||||
fn load(&mut self, scene_context: &SceneContext) -> Result<(), Box<dyn Error>> {
|
||||
fn load(
|
||||
&mut self,
|
||||
app_context: &mut ApplicationContext,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let depth_image_view = app_context.with_renderer_mut(|renderer| {
|
||||
renderer.get_additional_image_view(DEPTH_IMAGE_ID).clone()
|
||||
});
|
||||
|
||||
let swapchain_image_view =
|
||||
app_context.with_renderer_mut(|renderer| renderer.swapchain_image_view().clone());
|
||||
|
||||
let square = Square::new(
|
||||
&scene_context.device,
|
||||
&scene_context.memory_allocator,
|
||||
scene_context.swapchain_image_view.format(),
|
||||
scene_context.depth_stencil_image_view.format(),
|
||||
&app_context.device,
|
||||
&app_context.memory_allocator,
|
||||
swapchain_image_view.format(),
|
||||
depth_image_view.format(),
|
||||
)?;
|
||||
|
||||
let num_instances = 100;
|
||||
|
@ -69,29 +77,35 @@ impl Scene for MainScene {
|
|||
})
|
||||
.collect();
|
||||
|
||||
let camera = Camera3D::new(
|
||||
scene_context.aspect_ratio,
|
||||
std::f32::consts::FRAC_PI_2,
|
||||
0.01,
|
||||
1000.0,
|
||||
);
|
||||
let texture = {
|
||||
let mut uploads = AutoCommandBufferBuilder::primary(
|
||||
app_context.command_buffer_allocator.clone(),
|
||||
app_context.graphics_queue.queue_family_index(),
|
||||
CommandBufferUsage::OneTimeSubmit,
|
||||
)?;
|
||||
|
||||
let mut uploads = AutoCommandBufferBuilder::primary(
|
||||
scene_context.command_buffer_allocator.clone(),
|
||||
scene_context.graphics_queue.queue_family_index(),
|
||||
CommandBufferUsage::OneTimeSubmit,
|
||||
)?;
|
||||
let texture = Texture::from_file(
|
||||
&app_context.device,
|
||||
&app_context.memory_allocator,
|
||||
&mut uploads,
|
||||
"res/textures/wooden-crate.jpg",
|
||||
)?;
|
||||
|
||||
let texture = Texture::from_file(
|
||||
&scene_context.device,
|
||||
&scene_context.memory_allocator,
|
||||
&mut uploads,
|
||||
"res/textures/wooden-crate.jpg",
|
||||
)?;
|
||||
let _ = uploads
|
||||
.build()?
|
||||
.execute(app_context.graphics_queue.clone())?;
|
||||
|
||||
let _ = uploads
|
||||
.build()?
|
||||
.execute(scene_context.graphics_queue.clone())?;
|
||||
texture
|
||||
};
|
||||
|
||||
let camera = app_context.with_renderer(|renderer| {
|
||||
Camera3D::new(
|
||||
renderer.aspect_ratio(),
|
||||
std::f32::consts::FRAC_PI_2,
|
||||
0.01,
|
||||
1000.0,
|
||||
)
|
||||
});
|
||||
|
||||
self.state = Some(MainSceneState {
|
||||
square,
|
||||
|
@ -104,44 +118,46 @@ impl Scene for MainScene {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn update(&mut self, scene_context: &SceneContext) -> Result<(), Box<dyn Error>> {
|
||||
let state = self.state.as_mut().ok_or("State not found")?;
|
||||
state.camera.update(
|
||||
&scene_context.input_manager,
|
||||
&scene_context.timer,
|
||||
state.speed,
|
||||
10.0,
|
||||
scene_context.aspect_ratio,
|
||||
);
|
||||
fn update(&mut self, app_context: &mut ApplicationContext) -> Result<(), Box<dyn Error>> {
|
||||
let state = self.state.as_mut().unwrap();
|
||||
app_context.with_input_manager(|input_manager| {
|
||||
app_context.with_timer(|timer| {
|
||||
state.camera.update(
|
||||
input_manager,
|
||||
timer,
|
||||
state.speed,
|
||||
10.0,
|
||||
app_context.get_aspect_ratio(),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
if scene_context
|
||||
.input_manager
|
||||
.get_virtual_input_state("mouse_left")
|
||||
if app_context
|
||||
.with_input_manager(|input_manager| input_manager.get_virtual_input_state("mouse_left"))
|
||||
> 0.0
|
||||
{
|
||||
let _ = scene_context
|
||||
let _ = app_context
|
||||
.event_loop_proxy
|
||||
.send_event(UserEvent::CursorVisible(scene_context.window_id, false));
|
||||
let _ = scene_context
|
||||
.send_event(UserEvent::CursorVisible(app_context.window_id, false));
|
||||
let _ = app_context
|
||||
.event_loop_proxy
|
||||
.send_event(UserEvent::CursorGrabMode(
|
||||
scene_context.window_id,
|
||||
app_context.window_id,
|
||||
CursorGrabMode::Locked,
|
||||
));
|
||||
}
|
||||
|
||||
if scene_context
|
||||
.input_manager
|
||||
.get_virtual_input_state("mouse_right")
|
||||
> 0.0
|
||||
if app_context.with_input_manager(|input_manager| {
|
||||
input_manager.get_virtual_input_state("mouse_right")
|
||||
}) > 0.0
|
||||
{
|
||||
let _ = scene_context
|
||||
let _ = app_context
|
||||
.event_loop_proxy
|
||||
.send_event(UserEvent::CursorVisible(scene_context.window_id, true));
|
||||
let _ = scene_context
|
||||
.send_event(UserEvent::CursorVisible(app_context.window_id, true));
|
||||
let _ = app_context
|
||||
.event_loop_proxy
|
||||
.send_event(UserEvent::CursorGrabMode(
|
||||
scene_context.window_id,
|
||||
app_context.window_id,
|
||||
CursorGrabMode::None,
|
||||
));
|
||||
}
|
||||
|
@ -151,87 +167,112 @@ impl Scene for MainScene {
|
|||
|
||||
fn render(
|
||||
&mut self,
|
||||
acquire_future: Box<dyn GpuFuture>,
|
||||
scene_context: &SceneContext,
|
||||
gui: &mut Gui,
|
||||
before_future: Box<dyn GpuFuture>,
|
||||
app_context: &mut ApplicationContext,
|
||||
) -> Result<Box<dyn GpuFuture>, Box<dyn Error>> {
|
||||
let state = self.state.as_ref().ok_or("State not found")?;
|
||||
let state = self.state.as_ref().ok_or("State not loaded")?;
|
||||
|
||||
let mut builder = AutoCommandBufferBuilder::primary(
|
||||
scene_context.command_buffer_allocator.clone(),
|
||||
scene_context.graphics_queue.queue_family_index(),
|
||||
app_context.command_buffer_allocator.clone(),
|
||||
app_context.graphics_queue.queue_family_index(),
|
||||
CommandBufferUsage::OneTimeSubmit,
|
||||
)?;
|
||||
|
||||
{
|
||||
let viewport = Viewport {
|
||||
offset: [0.0, 0.0],
|
||||
extent: scene_context.window_size,
|
||||
depth_range: 0.0..=1.0,
|
||||
};
|
||||
|
||||
builder
|
||||
.begin_rendering(RenderingInfo {
|
||||
color_attachments: vec![Some(RenderingAttachmentInfo {
|
||||
load_op: AttachmentLoadOp::Clear,
|
||||
store_op: AttachmentStoreOp::Store,
|
||||
clear_value: Some([0.0, 0.0, 0.0, 1.0].into()),
|
||||
..RenderingAttachmentInfo::image_view(
|
||||
scene_context.swapchain_image_view.clone(),
|
||||
)
|
||||
})],
|
||||
depth_attachment: Some(RenderingAttachmentInfo {
|
||||
load_op: AttachmentLoadOp::Clear,
|
||||
store_op: AttachmentStoreOp::DontCare,
|
||||
clear_value: Some([1.0].into()),
|
||||
..RenderingAttachmentInfo::image_view(
|
||||
scene_context.depth_stencil_image_view.clone(),
|
||||
)
|
||||
}),
|
||||
..Default::default()
|
||||
})?
|
||||
.set_viewport(0, [viewport].into_iter().collect())?;
|
||||
let swapchain_image_view =
|
||||
app_context.with_renderer_mut(|renderer| renderer.swapchain_image_view().clone());
|
||||
let depth_image_view = app_context.with_renderer_mut(|renderer| {
|
||||
renderer.get_additional_image_view(DEPTH_IMAGE_ID).clone()
|
||||
});
|
||||
let config = RenderPassConfig::default();
|
||||
RenderPassManager::begin_standard_rendering(
|
||||
&mut builder,
|
||||
&config,
|
||||
swapchain_image_view,
|
||||
Some(depth_image_view),
|
||||
app_context.get_window_size(),
|
||||
)?;
|
||||
}
|
||||
|
||||
let camera_uniform = state
|
||||
.camera
|
||||
.create_buffer(&scene_context.memory_allocator)?;
|
||||
// Create camera uniform using the actual camera
|
||||
let camera_uniform = state.camera.create_buffer(&app_context.memory_allocator)?;
|
||||
|
||||
let transform_uniform =
|
||||
Transform::create_buffer(&scene_context.memory_allocator, &state.instances)?;
|
||||
Transform::create_buffer(&app_context.memory_allocator, &state.instances)?;
|
||||
|
||||
state
|
||||
.square
|
||||
.render(
|
||||
&mut builder,
|
||||
&scene_context.descriptor_set_allocator,
|
||||
&app_context.descriptor_set_allocator,
|
||||
&camera_uniform,
|
||||
&transform_uniform,
|
||||
&state.texture,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
builder.end_rendering()?;
|
||||
RenderPassManager::end_rendering(&mut builder)?;
|
||||
|
||||
let command_buffer = builder.build()?;
|
||||
|
||||
let render_future =
|
||||
acquire_future.then_execute(scene_context.graphics_queue.clone(), command_buffer)?;
|
||||
before_future.then_execute(app_context.graphics_queue.clone(), command_buffer)?;
|
||||
|
||||
gui.immediate_ui(|gui| {
|
||||
let ctx = gui.context();
|
||||
let swapchain_image_view =
|
||||
app_context.with_renderer_mut(|renderer| renderer.swapchain_image_view().clone());
|
||||
let input_manager_status =
|
||||
app_context.with_input_manager(|input_manager| format!("{:#?}", input_manager));
|
||||
let event_loop_proxy = app_context.event_loop_proxy.clone();
|
||||
let delta_time = app_context.get_delta_time();
|
||||
let window_id = app_context.window_id;
|
||||
let window_size = app_context.get_window_size();
|
||||
let render_future = app_context.with_gui_mut(|gui| {
|
||||
gui.immediate_ui(|gui| {
|
||||
let ctx = gui.context();
|
||||
egui::TopBottomPanel::top("top_panel").show(&ctx, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.heading("Vulkan Test - Moteur 3D");
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
if ui.button("Paramètres").clicked() {
|
||||
let _ = event_loop_proxy.send_event(UserEvent::ChangeScene(
|
||||
window_id,
|
||||
Box::new(SettingsScene::default()),
|
||||
));
|
||||
}
|
||||
if ui.button("Quitter").clicked() {
|
||||
let _ = event_loop_proxy.send_event(UserEvent::Exit(window_id));
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
egui::Window::new("Informations")
|
||||
.vscroll(true)
|
||||
.show(&ctx, |ui| {
|
||||
ui.label(format!("Resolution: {:?}", scene_context.window_size));
|
||||
egui::SidePanel::left("side_panel").show(&ctx, |ui| {
|
||||
ui.heading("Informations");
|
||||
|
||||
ui.label(format!("{:#?}", scene_context.input_manager));
|
||||
ui.separator();
|
||||
|
||||
ui.label(format!(
|
||||
"Delta time: {:?}",
|
||||
scene_context.timer.delta_time()
|
||||
));
|
||||
ui.label(format!("Résolution: {:?}", window_size));
|
||||
ui.label(format!("Delta Time: {:.2}ms", delta_time * 1000.0));
|
||||
|
||||
ui.separator();
|
||||
|
||||
ui.label("Position caméra:");
|
||||
let position = state.camera.get_position();
|
||||
ui.label(format!(" X: {:.2}", position[0]));
|
||||
ui.label(format!(" Y: {:.2}", position[1]));
|
||||
ui.label(format!(" Z: {:.2}", position[2]));
|
||||
|
||||
ui.separator();
|
||||
|
||||
ui.label("Rotation caméra:");
|
||||
let rotation = state.camera.get_rotation();
|
||||
ui.label(format!(" Yaw: {:.2}°", rotation.y.to_degrees()));
|
||||
ui.label(format!(" Pitch: {:.2}°", rotation.x.to_degrees()));
|
||||
|
||||
ui.separator();
|
||||
ui.label(input_manager_status);
|
||||
|
||||
ui.label(format!("Delta time: {:?}", delta_time));
|
||||
|
||||
ui.label(format!(
|
||||
"Position: {:?}, Rotation: {:?}",
|
||||
|
@ -239,13 +280,17 @@ impl Scene for MainScene {
|
|||
state.camera.get_rotation()
|
||||
));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
let render_future =
|
||||
gui.draw_on_image(render_future, scene_context.swapchain_image_view.clone());
|
||||
let render_future = gui.draw_on_image(render_future, swapchain_image_view.clone());
|
||||
|
||||
render_future
|
||||
});
|
||||
|
||||
Ok(render_future)
|
||||
}
|
||||
|
||||
fn unload(&mut self) {}
|
||||
fn unload(&mut self) {
|
||||
self.state = None;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue