Skip to content

Commit

Permalink
Fix game of life example constructing one memory allocator per window (
Browse files Browse the repository at this point in the history
  • Loading branch information
marc0246 authored Mar 24, 2023
1 parent d7ac988 commit 28a98f7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 58 deletions.
44 changes: 19 additions & 25 deletions examples/src/bin/multi_window_game_of_life/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use std::{collections::HashMap, sync::Arc};
use vulkano::{
command_buffer::allocator::StandardCommandBufferAllocator,
descriptor_set::allocator::StandardDescriptorSetAllocator, device::Queue, format::Format,
memory::allocator::StandardMemoryAllocator,
};
use vulkano_util::{
context::{VulkanoConfig, VulkanoContext},
Expand All @@ -30,42 +29,24 @@ pub struct RenderPipeline {

impl RenderPipeline {
pub fn new(
app: &App,
compute_queue: Arc<Queue>,
gfx_queue: Arc<Queue>,
size: [u32; 2],
swapchain_format: Format,
) -> RenderPipeline {
let memory_allocator = StandardMemoryAllocator::new_default(gfx_queue.device().clone());
let command_buffer_allocator = Arc::new(StandardCommandBufferAllocator::new(
gfx_queue.device().clone(),
Default::default(),
));
let descriptor_set_allocator = Arc::new(StandardDescriptorSetAllocator::new(
gfx_queue.device().clone(),
));

RenderPipeline {
compute: GameOfLifeComputePipeline::new(
compute_queue,
&memory_allocator,
command_buffer_allocator.clone(),
descriptor_set_allocator.clone(),
size,
),
place_over_frame: RenderPassPlaceOverFrame::new(
gfx_queue,
&memory_allocator,
command_buffer_allocator,
descriptor_set_allocator,
swapchain_format,
),
compute: GameOfLifeComputePipeline::new(app, compute_queue, size),
place_over_frame: RenderPassPlaceOverFrame::new(app, gfx_queue, swapchain_format),
}
}
}

pub struct App {
pub context: VulkanoContext,
pub windows: VulkanoWindows,
pub command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
pub descriptor_set_allocator: Arc<StandardDescriptorSetAllocator>,
pub pipelines: HashMap<WindowId, RenderPipeline>,
}

Expand Down Expand Up @@ -97,6 +78,7 @@ impl App {
self.pipelines.insert(
id1,
RenderPipeline::new(
self,
// Use same queue.. for synchronization.
self.context.graphics_queue().clone(),
self.context.graphics_queue().clone(),
Expand All @@ -113,6 +95,7 @@ impl App {
self.pipelines.insert(
id2,
RenderPipeline::new(
self,
self.context.graphics_queue().clone(),
self.context.graphics_queue().clone(),
[
Expand All @@ -127,9 +110,20 @@ impl App {

impl Default for App {
fn default() -> Self {
let context = VulkanoContext::new(VulkanoConfig::default());
let command_buffer_allocator = Arc::new(StandardCommandBufferAllocator::new(
context.device().clone(),
Default::default(),
));
let descriptor_set_allocator = Arc::new(StandardDescriptorSetAllocator::new(
context.device().clone(),
));

App {
context: VulkanoContext::new(VulkanoConfig::default()),
context,
windows: VulkanoWindows::default(),
command_buffer_allocator,
descriptor_set_allocator,
pipelines: HashMap::new(),
}
}
Expand Down
14 changes: 5 additions & 9 deletions examples/src/bin/multi_window_game_of_life/game_of_life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// notice may not be copied, modified, or distributed except
// according to those terms.

use crate::app::App;
use cgmath::Vector2;
use rand::Rng;
use std::sync::Arc;
Expand Down Expand Up @@ -57,13 +58,8 @@ fn rand_grid(memory_allocator: &impl MemoryAllocator, size: [u32; 2]) -> Subbuff
}

impl GameOfLifeComputePipeline {
pub fn new(
compute_queue: Arc<Queue>,
memory_allocator: &impl MemoryAllocator,
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
descriptor_set_allocator: Arc<StandardDescriptorSetAllocator>,
size: [u32; 2],
) -> GameOfLifeComputePipeline {
pub fn new(app: &App, compute_queue: Arc<Queue>, size: [u32; 2]) -> GameOfLifeComputePipeline {
let memory_allocator = app.context.memory_allocator();
let life_in = rand_grid(memory_allocator, size);
let life_out = rand_grid(memory_allocator, size);

Expand Down Expand Up @@ -91,8 +87,8 @@ impl GameOfLifeComputePipeline {
GameOfLifeComputePipeline {
compute_queue,
compute_life_pipeline,
command_buffer_allocator,
descriptor_set_allocator,
command_buffer_allocator: app.command_buffer_allocator.clone(),
descriptor_set_allocator: app.descriptor_set_allocator.clone(),
life_in,
life_out,
image,
Expand Down
15 changes: 5 additions & 10 deletions examples/src/bin/multi_window_game_of_life/pixels_draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// notice may not be copied, modified, or distributed except
// according to those terms.

use crate::app::App;
use std::sync::Arc;
use vulkano::{
buffer::{Buffer, BufferAllocateInfo, BufferContents, BufferUsage, Subbuffer},
Expand All @@ -19,7 +20,6 @@ use vulkano::{
},
device::Queue,
image::ImageViewAbstract,
memory::allocator::MemoryAllocator,
pipeline::{
graphics::{
input_assembly::InputAssemblyState,
Expand Down Expand Up @@ -78,14 +78,9 @@ pub struct PixelsDrawPipeline {
}

impl PixelsDrawPipeline {
pub fn new(
gfx_queue: Arc<Queue>,
subpass: Subpass,
memory_allocator: &impl MemoryAllocator,
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
descriptor_set_allocator: Arc<StandardDescriptorSetAllocator>,
) -> PixelsDrawPipeline {
pub fn new(app: &App, gfx_queue: Arc<Queue>, subpass: Subpass) -> PixelsDrawPipeline {
let (vertices, indices) = textured_quad(2.0, 2.0);
let memory_allocator = app.context.memory_allocator();
let vertex_buffer = Buffer::from_iter(
memory_allocator,
BufferAllocateInfo {
Expand Down Expand Up @@ -123,8 +118,8 @@ impl PixelsDrawPipeline {
gfx_queue,
subpass,
pipeline,
command_buffer_allocator,
descriptor_set_allocator,
command_buffer_allocator: app.command_buffer_allocator.clone(),
descriptor_set_allocator: app.descriptor_set_allocator.clone(),
vertices: vertex_buffer,
indices: index_buffer,
}
Expand Down
18 changes: 4 additions & 14 deletions examples/src/bin/multi_window_game_of_life/render_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
// notice may not be copied, modified, or distributed except
// according to those terms.

use crate::pixels_draw::PixelsDrawPipeline;
use crate::{app::App, pixels_draw::PixelsDrawPipeline};
use std::sync::Arc;
use vulkano::{
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
RenderPassBeginInfo, SubpassContents,
},
descriptor_set::allocator::StandardDescriptorSetAllocator,
device::Queue,
format::Format,
image::ImageAccess,
memory::allocator::MemoryAllocator,
render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass},
sync::GpuFuture,
};
Expand All @@ -34,10 +32,8 @@ pub struct RenderPassPlaceOverFrame {

impl RenderPassPlaceOverFrame {
pub fn new(
app: &App,
gfx_queue: Arc<Queue>,
memory_allocator: &impl MemoryAllocator,
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
descriptor_set_allocator: Arc<StandardDescriptorSetAllocator>,
output_format: Format,
) -> RenderPassPlaceOverFrame {
let render_pass = vulkano::single_pass_renderpass!(gfx_queue.device().clone(),
Expand All @@ -56,19 +52,13 @@ impl RenderPassPlaceOverFrame {
)
.unwrap();
let subpass = Subpass::from(render_pass.clone(), 0).unwrap();
let pixels_draw_pipeline = PixelsDrawPipeline::new(
gfx_queue.clone(),
subpass,
memory_allocator,
command_buffer_allocator.clone(),
descriptor_set_allocator,
);
let pixels_draw_pipeline = PixelsDrawPipeline::new(app, gfx_queue.clone(), subpass);

RenderPassPlaceOverFrame {
gfx_queue,
render_pass,
pixels_draw_pipeline,
command_buffer_allocator,
command_buffer_allocator: app.command_buffer_allocator.clone(),
}
}

Expand Down

0 comments on commit 28a98f7

Please sign in to comment.