Move allocator to App
This commit is contained in:
parent
784e5b90be
commit
f7a3d883c6
4 changed files with 150 additions and 153 deletions
|
@ -3,17 +3,12 @@ use glam::{Mat3, Mat4, Vec3};
|
|||
use std::error::Error;
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
use vulkano::buffer::allocator::{SubbufferAllocator, SubbufferAllocatorCreateInfo};
|
||||
use vulkano::buffer::{BufferUsage, Subbuffer};
|
||||
use vulkano::buffer::Subbuffer;
|
||||
use vulkano::command_buffer::{AutoCommandBufferBuilder, PrimaryAutoCommandBuffer};
|
||||
use vulkano::descriptor_set::allocator::StandardDescriptorSetAllocator;
|
||||
use vulkano::descriptor_set::{DescriptorSet, WriteDescriptorSet};
|
||||
use vulkano::device::Device;
|
||||
use vulkano::memory::allocator::{MemoryTypeFilter, StandardMemoryAllocator};
|
||||
use vulkano::pipeline::{GraphicsPipeline, Pipeline, PipelineBindPoint};
|
||||
use vulkano::swapchain::Swapchain;
|
||||
|
||||
use crate::renderer::{pipelines::triangle_pipeline::create_triangle_pipeline, Vertex2D};
|
||||
use crate::renderer::{pipelines::triangle_pipeline::create_triangle_pipeline, App, Vertex2D};
|
||||
|
||||
const VERTICES: [Vertex2D; 12] = [
|
||||
// Triangle en haut à gauche
|
||||
|
@ -74,57 +69,34 @@ pub struct Scene {
|
|||
pipeline: Arc<GraphicsPipeline>,
|
||||
vertex_buffer: Subbuffer<[Vertex2D]>,
|
||||
|
||||
uniform_buffer_allocator: SubbufferAllocator,
|
||||
rotation_start: Instant,
|
||||
swapchain: Arc<Swapchain>,
|
||||
descriptor_set_allocator: Arc<StandardDescriptorSetAllocator>,
|
||||
}
|
||||
|
||||
impl Scene {
|
||||
pub fn load(
|
||||
device: &Arc<Device>,
|
||||
swapchain: &Arc<Swapchain>,
|
||||
memory_allocator: &Arc<StandardMemoryAllocator>,
|
||||
) -> Result<Self, Box<dyn Error>> {
|
||||
let pipeline = create_triangle_pipeline(device, swapchain)?;
|
||||
let vertex_buffer = Vertex2D::create_buffer(Vec::from_iter(VERTICES), memory_allocator)?;
|
||||
|
||||
let uniform_buffer_allocator = SubbufferAllocator::new(
|
||||
memory_allocator.clone(),
|
||||
SubbufferAllocatorCreateInfo {
|
||||
buffer_usage: BufferUsage::UNIFORM_BUFFER,
|
||||
memory_type_filter: MemoryTypeFilter::PREFER_DEVICE
|
||||
| MemoryTypeFilter::HOST_SEQUENTIAL_WRITE,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
let descriptor_set_allocator = Arc::new(StandardDescriptorSetAllocator::new(
|
||||
device.clone(),
|
||||
Default::default(),
|
||||
));
|
||||
pub fn load(app: &App) -> Result<Self, Box<dyn Error>> {
|
||||
let pipeline = create_triangle_pipeline(&app.device, &app.rcx.as_ref().unwrap().swapchain)?;
|
||||
let vertex_buffer =
|
||||
Vertex2D::create_buffer(Vec::from_iter(VERTICES), &app.memory_allocator)?;
|
||||
|
||||
Ok(Scene {
|
||||
pipeline,
|
||||
vertex_buffer,
|
||||
uniform_buffer_allocator,
|
||||
rotation_start: Instant::now(),
|
||||
swapchain: swapchain.clone(),
|
||||
descriptor_set_allocator,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn render(
|
||||
&self,
|
||||
app: &App,
|
||||
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();
|
||||
let uniform_buffer = self.get_uniform_buffer(app);
|
||||
let layout = &self.pipeline.layout().set_layouts()[0];
|
||||
let descriptor_set = DescriptorSet::new(
|
||||
self.descriptor_set_allocator.clone(),
|
||||
app.descriptor_set_allocator.clone(),
|
||||
layout.clone(),
|
||||
[WriteDescriptorSet::buffer(0, uniform_buffer)],
|
||||
[],
|
||||
|
@ -147,15 +119,15 @@ impl Scene {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn get_uniform_buffer(&self) -> Subbuffer<vs::MVPData> {
|
||||
fn get_uniform_buffer(&self, app: &App) -> Subbuffer<vs::MVPData> {
|
||||
let swapchain = &app.rcx.as_ref().unwrap().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);
|
||||
|
||||
// NOTE: This teapot was meant for OpenGL where the origin is at the lower left
|
||||
// instead the origin is at the upper left in Vulkan, so we reverse the Y axis.
|
||||
let aspect_ratio =
|
||||
self.swapchain.image_extent()[0] as f32 / self.swapchain.image_extent()[1] as f32;
|
||||
let aspect_ratio = swapchain.image_extent()[0] as f32 / swapchain.image_extent()[1] as f32;
|
||||
|
||||
let proj = Mat4::perspective_rh_gl(std::f32::consts::FRAC_PI_2, aspect_ratio, 0.01, 100.0);
|
||||
let view = Mat4::look_at_rh(
|
||||
|
@ -171,7 +143,7 @@ impl Scene {
|
|||
projection: proj.to_cols_array_2d(),
|
||||
};
|
||||
|
||||
let buffer = self.uniform_buffer_allocator.allocate_sized().unwrap();
|
||||
let buffer = app.uniform_buffer_allocator.allocate_sized().unwrap();
|
||||
*buffer.write().unwrap() = uniform_data;
|
||||
|
||||
buffer
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue