2024-10-10 10:16:14 +00:00
|
|
|
use crate::App;
|
2024-02-21 08:30:15 +00:00
|
|
|
use glam::IVec2;
|
2022-02-02 16:21:32 +00:00
|
|
|
use rand::Rng;
|
2022-10-27 18:59:47 +00:00
|
|
|
use std::sync::Arc;
|
2022-03-06 19:30:49 +00:00
|
|
|
use vulkano::{
|
2023-04-02 13:07:16 +00:00
|
|
|
buffer::{Buffer, BufferCreateInfo, BufferUsage, Subbuffer},
|
2023-03-05 18:56:35 +00:00
|
|
|
command_buffer::{
|
2023-12-20 12:04:01 +00:00
|
|
|
allocator::StandardCommandBufferAllocator, CommandBufferBeginInfo, CommandBufferLevel,
|
|
|
|
CommandBufferUsage, RecordingCommandBuffer,
|
2023-03-05 18:56:35 +00:00
|
|
|
},
|
|
|
|
descriptor_set::{
|
2023-11-12 16:17:37 +00:00
|
|
|
allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet,
|
2023-03-05 18:56:35 +00:00
|
|
|
},
|
2022-03-06 19:30:49 +00:00
|
|
|
device::Queue,
|
|
|
|
format::Format,
|
2023-07-06 08:52:11 +00:00
|
|
|
image::{view::ImageView, Image, ImageCreateInfo, ImageType, ImageUsage},
|
2023-09-03 11:09:07 +00:00
|
|
|
memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator},
|
2023-04-22 18:46:22 +00:00
|
|
|
pipeline::{
|
|
|
|
compute::ComputePipelineCreateInfo, layout::PipelineDescriptorSetLayoutCreateInfo,
|
|
|
|
ComputePipeline, Pipeline, PipelineBindPoint, PipelineLayout,
|
2023-06-25 18:08:27 +00:00
|
|
|
PipelineShaderStageCreateInfo,
|
2023-04-22 18:46:22 +00:00
|
|
|
},
|
2022-03-06 19:30:49 +00:00
|
|
|
sync::GpuFuture,
|
|
|
|
};
|
2022-02-02 16:21:32 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Pipeline holding double buffered grid & color image. Grids are used to calculate the state, and
|
|
|
|
/// color image is used to show the output. Because on each step we determine state in parallel, we
|
|
|
|
/// need to write the output to another grid. Otherwise the state would not be correctly determined
|
|
|
|
/// as one shader invocation might read data that was just written by another shader invocation.
|
2022-02-02 16:21:32 +00:00
|
|
|
pub struct GameOfLifeComputePipeline {
|
|
|
|
compute_queue: Arc<Queue>,
|
|
|
|
compute_life_pipeline: Arc<ComputePipeline>,
|
2022-10-27 18:59:47 +00:00
|
|
|
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
|
|
|
|
descriptor_set_allocator: Arc<StandardDescriptorSetAllocator>,
|
2023-01-12 12:56:10 +00:00
|
|
|
life_in: Subbuffer<[u32]>,
|
|
|
|
life_out: Subbuffer<[u32]>,
|
2023-07-03 20:37:29 +00:00
|
|
|
image: Arc<ImageView>,
|
2022-02-02 16:21:32 +00:00
|
|
|
}
|
|
|
|
|
2023-09-03 11:09:07 +00:00
|
|
|
fn rand_grid(memory_allocator: Arc<StandardMemoryAllocator>, size: [u32; 2]) -> Subbuffer<[u32]> {
|
2023-01-12 12:56:10 +00:00
|
|
|
Buffer::from_iter(
|
2022-10-26 14:25:01 +00:00
|
|
|
memory_allocator,
|
2023-04-02 13:07:16 +00:00
|
|
|
BufferCreateInfo {
|
|
|
|
usage: BufferUsage::STORAGE_BUFFER,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
AllocationCreateInfo {
|
2023-07-19 09:11:17 +00:00
|
|
|
memory_type_filter: MemoryTypeFilter::PREFER_DEVICE
|
|
|
|
| MemoryTypeFilter::HOST_SEQUENTIAL_WRITE,
|
2023-01-12 12:56:10 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2023-07-03 20:37:29 +00:00
|
|
|
(0..(size[0] * size[1])).map(|_| rand::thread_rng().gen_range(0u32..=1)),
|
2022-02-02 16:21:32 +00:00
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GameOfLifeComputePipeline {
|
2023-03-24 13:03:49 +00:00
|
|
|
pub fn new(app: &App, compute_queue: Arc<Queue>, size: [u32; 2]) -> GameOfLifeComputePipeline {
|
|
|
|
let memory_allocator = app.context.memory_allocator();
|
2023-09-03 11:09:07 +00:00
|
|
|
let life_in = rand_grid(memory_allocator.clone(), size);
|
|
|
|
let life_out = rand_grid(memory_allocator.clone(), size);
|
2022-02-02 16:21:32 +00:00
|
|
|
|
|
|
|
let compute_life_pipeline = {
|
2023-04-22 18:46:22 +00:00
|
|
|
let device = compute_queue.device();
|
|
|
|
let cs = compute_life_cs::load(device.clone())
|
2023-04-18 18:53:08 +00:00
|
|
|
.unwrap()
|
|
|
|
.entry_point("main")
|
|
|
|
.unwrap();
|
2023-06-25 18:08:27 +00:00
|
|
|
let stage = PipelineShaderStageCreateInfo::new(cs);
|
2023-04-22 18:46:22 +00:00
|
|
|
let layout = PipelineLayout::new(
|
|
|
|
device.clone(),
|
|
|
|
PipelineDescriptorSetLayoutCreateInfo::from_stages([&stage])
|
|
|
|
.into_pipeline_layout_create_info(device.clone())
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-03 20:37:29 +00:00
|
|
|
|
2022-02-02 16:21:32 +00:00
|
|
|
ComputePipeline::new(
|
2023-04-22 18:46:22 +00:00
|
|
|
device.clone(),
|
2022-02-02 16:21:32 +00:00
|
|
|
None,
|
2023-04-22 18:46:22 +00:00
|
|
|
ComputePipelineCreateInfo::stage_layout(stage, layout),
|
2022-02-02 16:21:32 +00:00
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
2023-07-03 20:37:29 +00:00
|
|
|
let image = ImageView::new_default(
|
|
|
|
Image::new(
|
2023-09-03 11:09:07 +00:00
|
|
|
memory_allocator.clone(),
|
2023-07-03 20:37:29 +00:00
|
|
|
ImageCreateInfo {
|
2023-07-06 08:52:11 +00:00
|
|
|
image_type: ImageType::Dim2d,
|
2023-07-14 14:44:34 +00:00
|
|
|
format: Format::R8G8B8A8_UNORM,
|
2023-07-06 08:52:11 +00:00
|
|
|
extent: [size[0], size[1], 1],
|
2023-07-03 20:37:29 +00:00
|
|
|
usage: ImageUsage::TRANSFER_DST | ImageUsage::SAMPLED | ImageUsage::STORAGE,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
AllocationCreateInfo::default(),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-06-24 15:27:33 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-10-05 09:09:26 +00:00
|
|
|
|
2022-02-02 16:21:32 +00:00
|
|
|
GameOfLifeComputePipeline {
|
|
|
|
compute_queue,
|
|
|
|
compute_life_pipeline,
|
2023-03-24 13:03:49 +00:00
|
|
|
command_buffer_allocator: app.command_buffer_allocator.clone(),
|
|
|
|
descriptor_set_allocator: app.descriptor_set_allocator.clone(),
|
2022-02-02 16:21:32 +00:00
|
|
|
life_in,
|
|
|
|
life_out,
|
|
|
|
image,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 20:37:29 +00:00
|
|
|
pub fn color_image(&self) -> Arc<ImageView> {
|
2022-02-02 16:21:32 +00:00
|
|
|
self.image.clone()
|
|
|
|
}
|
|
|
|
|
2024-02-21 08:30:15 +00:00
|
|
|
pub fn draw_life(&self, pos: IVec2) {
|
2022-02-02 16:21:32 +00:00
|
|
|
let mut life_in = self.life_in.write().unwrap();
|
2023-07-06 08:52:11 +00:00
|
|
|
let extent = self.image.image().extent();
|
|
|
|
if pos.y < 0 || pos.y >= extent[1] as i32 || pos.x < 0 || pos.x >= extent[0] as i32 {
|
2022-02-02 16:21:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-07-06 08:52:11 +00:00
|
|
|
let index = (pos.y * extent[0] as i32 + pos.x) as usize;
|
2022-02-02 16:21:32 +00:00
|
|
|
life_in[index] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compute(
|
|
|
|
&mut self,
|
|
|
|
before_future: Box<dyn GpuFuture>,
|
|
|
|
life_color: [f32; 4],
|
|
|
|
dead_color: [f32; 4],
|
|
|
|
) -> Box<dyn GpuFuture> {
|
2023-12-20 12:04:01 +00:00
|
|
|
let mut builder = RecordingCommandBuffer::new(
|
2023-11-14 16:57:43 +00:00
|
|
|
self.command_buffer_allocator.clone(),
|
2022-09-10 06:00:08 +00:00
|
|
|
self.compute_queue.queue_family_index(),
|
2023-12-20 12:04:01 +00:00
|
|
|
CommandBufferLevel::Primary,
|
|
|
|
CommandBufferBeginInfo {
|
|
|
|
usage: CommandBufferUsage::OneTimeSubmit,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-02-02 16:21:32 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
// Dispatch will mutate the builder adding commands which won't be sent before we build the
|
|
|
|
// command buffer after dispatches. This will minimize the commands we send to the GPU. For
|
|
|
|
// example, we could be doing tens of dispatches here depending on our needs. Maybe we
|
|
|
|
// wanted to simulate 10 steps at a time...
|
2022-02-02 16:21:32 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
// First compute the next state.
|
2022-02-02 16:21:32 +00:00
|
|
|
self.dispatch(&mut builder, life_color, dead_color, 0);
|
2023-03-05 18:56:35 +00:00
|
|
|
|
|
|
|
// Then color based on the next state.
|
2022-02-02 16:21:32 +00:00
|
|
|
self.dispatch(&mut builder, life_color, dead_color, 1);
|
|
|
|
|
2023-12-09 11:24:52 +00:00
|
|
|
let command_buffer = builder.end().unwrap();
|
2022-02-02 16:21:32 +00:00
|
|
|
let finished = before_future
|
|
|
|
.then_execute(self.compute_queue.clone(), command_buffer)
|
|
|
|
.unwrap();
|
|
|
|
let after_pipeline = finished.then_signal_fence_and_flush().unwrap().boxed();
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
// Swap input and output so the output becomes the input for next frame.
|
2022-02-02 16:21:32 +00:00
|
|
|
std::mem::swap(&mut self.life_in, &mut self.life_out);
|
|
|
|
|
|
|
|
after_pipeline
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Builds the command for a dispatch.
|
2022-02-02 16:21:32 +00:00
|
|
|
fn dispatch(
|
2022-10-05 09:09:26 +00:00
|
|
|
&self,
|
2023-12-20 12:04:01 +00:00
|
|
|
builder: &mut RecordingCommandBuffer,
|
2022-02-02 16:21:32 +00:00
|
|
|
life_color: [f32; 4],
|
|
|
|
dead_color: [f32; 4],
|
2023-03-05 18:56:35 +00:00
|
|
|
// Step determines whether we color or compute life (see branch in the shader)s.
|
2022-02-02 16:21:32 +00:00
|
|
|
step: i32,
|
|
|
|
) {
|
2023-03-05 18:56:35 +00:00
|
|
|
// Resize image if needed.
|
2023-07-06 08:52:11 +00:00
|
|
|
let image_extent = self.image.image().extent();
|
2024-10-10 10:16:14 +00:00
|
|
|
let layout = &self.compute_life_pipeline.layout().set_layouts()[0];
|
|
|
|
let descriptor_set = DescriptorSet::new(
|
2023-11-12 14:10:22 +00:00
|
|
|
self.descriptor_set_allocator.clone(),
|
2024-10-10 10:16:14 +00:00
|
|
|
layout.clone(),
|
2022-02-02 16:21:32 +00:00
|
|
|
[
|
|
|
|
WriteDescriptorSet::image_view(0, self.image.clone()),
|
|
|
|
WriteDescriptorSet::buffer(1, self.life_in.clone()),
|
|
|
|
WriteDescriptorSet::buffer(2, self.life_out.clone()),
|
|
|
|
],
|
2023-06-03 12:56:27 +00:00
|
|
|
[],
|
2022-02-02 16:21:32 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
let push_constants = compute_life_cs::PushConstants {
|
2022-02-02 16:21:32 +00:00
|
|
|
life_color,
|
|
|
|
dead_color,
|
|
|
|
step,
|
|
|
|
};
|
|
|
|
builder
|
|
|
|
.bind_pipeline_compute(self.compute_life_pipeline.clone())
|
2023-08-04 18:55:16 +00:00
|
|
|
.unwrap()
|
2024-10-10 10:16:14 +00:00
|
|
|
.bind_descriptor_sets(
|
|
|
|
PipelineBindPoint::Compute,
|
|
|
|
self.compute_life_pipeline.layout().clone(),
|
|
|
|
0,
|
|
|
|
descriptor_set,
|
|
|
|
)
|
2023-08-04 18:55:16 +00:00
|
|
|
.unwrap()
|
2024-10-10 10:16:14 +00:00
|
|
|
.push_constants(
|
|
|
|
self.compute_life_pipeline.layout().clone(),
|
|
|
|
0,
|
|
|
|
push_constants,
|
|
|
|
)
|
2022-02-02 16:21:32 +00:00
|
|
|
.unwrap();
|
2023-12-25 03:01:16 +00:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
builder
|
|
|
|
.dispatch([image_extent[0] / 8, image_extent[1] / 8, 1])
|
|
|
|
.unwrap();
|
|
|
|
}
|
2022-02-02 16:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod compute_life_cs {
|
|
|
|
vulkano_shaders::shader! {
|
|
|
|
ty: "compute",
|
2023-03-05 18:56:35 +00:00
|
|
|
src: r"
|
|
|
|
#version 450
|
2022-02-02 16:21:32 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
|
2022-02-02 16:21:32 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
layout(set = 0, binding = 0, rgba8) uniform writeonly image2D img;
|
|
|
|
layout(set = 0, binding = 1) buffer LifeInBuffer { uint life_in[]; };
|
|
|
|
layout(set = 0, binding = 2) buffer LifeOutBuffer { uint life_out[]; };
|
2022-02-02 16:21:32 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
layout(push_constant) uniform PushConstants {
|
|
|
|
vec4 life_color;
|
|
|
|
vec4 dead_color;
|
|
|
|
int step;
|
|
|
|
} push_constants;
|
2022-02-02 16:21:32 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
int get_index(ivec2 pos) {
|
|
|
|
ivec2 dims = ivec2(imageSize(img));
|
|
|
|
return pos.y * dims.x + pos.x;
|
|
|
|
}
|
2022-10-05 09:09:26 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
// https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
|
|
|
|
void compute_life() {
|
|
|
|
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
|
|
|
|
int index = get_index(pos);
|
2022-02-02 16:21:32 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
ivec2 up_left = pos + ivec2(-1, 1);
|
|
|
|
ivec2 up = pos + ivec2(0, 1);
|
|
|
|
ivec2 up_right = pos + ivec2(1, 1);
|
|
|
|
ivec2 right = pos + ivec2(1, 0);
|
|
|
|
ivec2 down_right = pos + ivec2(1, -1);
|
|
|
|
ivec2 down = pos + ivec2(0, -1);
|
|
|
|
ivec2 down_left = pos + ivec2(-1, -1);
|
|
|
|
ivec2 left = pos + ivec2(-1, 0);
|
2022-02-02 16:21:32 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
int alive_count = 0;
|
2023-05-03 09:05:28 +00:00
|
|
|
if (life_in[get_index(up_left)] == 1) { alive_count += 1; }
|
|
|
|
if (life_in[get_index(up)] == 1) { alive_count += 1; }
|
|
|
|
if (life_in[get_index(up_right)] == 1) { alive_count += 1; }
|
|
|
|
if (life_in[get_index(right)] == 1) { alive_count += 1; }
|
|
|
|
if (life_in[get_index(down_right)] == 1) { alive_count += 1; }
|
|
|
|
if (life_in[get_index(down)] == 1) { alive_count += 1; }
|
|
|
|
if (life_in[get_index(down_left)] == 1) { alive_count += 1; }
|
|
|
|
if (life_in[get_index(left)] == 1) { alive_count += 1; }
|
2022-10-05 09:09:26 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
// Dead becomes alive.
|
2023-05-03 09:05:28 +00:00
|
|
|
if (life_in[index] == 0 && alive_count == 3) {
|
2023-03-05 18:56:35 +00:00
|
|
|
life_out[index] = 1;
|
|
|
|
}
|
|
|
|
// Becomes dead.
|
2023-05-03 09:05:28 +00:00
|
|
|
else if (life_in[index] == 1 && alive_count < 2 || alive_count > 3) {
|
2023-03-05 18:56:35 +00:00
|
|
|
life_out[index] = 0;
|
|
|
|
}
|
|
|
|
// Else do nothing.
|
|
|
|
else {
|
|
|
|
life_out[index] = life_in[index];
|
|
|
|
}
|
|
|
|
}
|
2022-02-02 16:21:32 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
void compute_color() {
|
|
|
|
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
|
|
|
|
int index = get_index(pos);
|
|
|
|
if (life_out[index] == 1) {
|
|
|
|
imageStore(img, pos, push_constants.life_color);
|
|
|
|
} else {
|
|
|
|
imageStore(img, pos, push_constants.dead_color);
|
|
|
|
}
|
|
|
|
}
|
2022-02-02 16:21:32 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
void main() {
|
|
|
|
if (push_constants.step == 0) {
|
|
|
|
compute_life();
|
|
|
|
} else {
|
|
|
|
compute_color();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
2022-02-02 16:21:32 +00:00
|
|
|
}
|
|
|
|
}
|