Split app, window render context and vulkan context
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 26m52s

This commit is contained in:
Florian RICHER 2025-04-03 19:59:10 +02:00
parent f32db72101
commit 15c273b93d
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
9 changed files with 426 additions and 325 deletions

View file

@ -8,7 +8,10 @@ use vulkano::command_buffer::{AutoCommandBufferBuilder, PrimaryAutoCommandBuffer
use vulkano::descriptor_set::{DescriptorSet, WriteDescriptorSet};
use vulkano::pipeline::{GraphicsPipeline, Pipeline, PipelineBindPoint};
use crate::vulkan::{App, Vertex2D, pipelines::triangle_pipeline::create_triangle_pipeline};
use crate::vulkan::{pipelines::triangle_pipeline::create_triangle_pipeline, vertex::Vertex2D};
use super::vulkan_context::VulkanContext;
use super::window_render_context::WindowRenderContext;
const VERTICES: [Vertex2D; 12] = [
// Triangle en haut à gauche
@ -73,10 +76,14 @@ pub struct Scene {
}
impl Scene {
pub fn load(app: &App) -> Result<Self, Box<dyn Error>> {
let pipeline = create_triangle_pipeline(&app.device, &app.rcx.as_ref().unwrap().swapchain)?;
pub fn load(
vulkan_context: &VulkanContext,
window_render_context: &WindowRenderContext,
) -> Result<Self, Box<dyn Error>> {
let pipeline =
create_triangle_pipeline(&vulkan_context.device, &window_render_context.swapchain)?;
let vertex_buffer =
Vertex2D::create_buffer(Vec::from_iter(VERTICES), &app.memory_allocator)?;
Vertex2D::create_buffer(Vec::from_iter(VERTICES), &vulkan_context.memory_allocator)?;
Ok(Scene {
pipeline,
@ -87,16 +94,17 @@ impl Scene {
pub fn render(
&self,
app: &App,
vulkan_context: &VulkanContext,
window_render_context: &WindowRenderContext,
builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>,
) -> Result<(), Box<dyn Error>> {
let vertex_count = self.vertex_buffer.len() as u32;
let instance_count = vertex_count / 3;
let uniform_buffer = self.get_uniform_buffer(app);
let uniform_buffer = self.get_uniform_buffer(vulkan_context, window_render_context);
let layout = &self.pipeline.layout().set_layouts()[0];
let descriptor_set = DescriptorSet::new(
app.descriptor_set_allocator.clone(),
vulkan_context.descriptor_set_allocator.clone(),
layout.clone(),
[WriteDescriptorSet::buffer(0, uniform_buffer)],
[],
@ -119,8 +127,12 @@ impl Scene {
Ok(())
}
fn get_uniform_buffer(&self, app: &App) -> Subbuffer<vs::MVPData> {
let swapchain = &app.rcx.as_ref().unwrap().swapchain;
fn get_uniform_buffer(
&self,
vulkan_context: &VulkanContext,
window_render_context: &WindowRenderContext,
) -> Subbuffer<vs::MVPData> {
let swapchain = &window_render_context.swapchain;
let elapsed = self.rotation_start.elapsed();
let rotation = elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 / 1_000_000_000.0;
let rotation = Mat3::from_rotation_y(rotation as f32);
@ -143,7 +155,10 @@ impl Scene {
projection: proj.to_cols_array_2d(),
};
let buffer = app.uniform_buffer_allocator.allocate_sized().unwrap();
let buffer = vulkan_context
.uniform_buffer_allocator
.allocate_sized()
.unwrap();
*buffer.write().unwrap() = uniform_data;
buffer