Skip to content

Commit

Permalink
Task graph [5/10]: the new command buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
marc0246 committed Sep 12, 2024
1 parent d89e5cf commit 10f0d19
Show file tree
Hide file tree
Showing 15 changed files with 3,240 additions and 144 deletions.
94 changes: 47 additions & 47 deletions examples/async-update/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ use std::{
};
use vulkano::{
buffer::{Buffer, BufferContents, BufferCreateInfo, BufferUsage, Subbuffer},
command_buffer::{
sys::RawRecordingCommandBuffer, BufferImageCopy, ClearColorImageInfo,
CopyBufferToImageInfo, RenderPassBeginInfo,
},
command_buffer::RenderPassBeginInfo,
descriptor_set::{
allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet,
},
Expand All @@ -57,7 +54,7 @@ use vulkano::{
image::{
sampler::{Sampler, SamplerCreateInfo},
view::ImageView,
Image, ImageCreateInfo, ImageType, ImageUsage,
Image, ImageAspects, ImageCreateInfo, ImageSubresourceLayers, ImageType, ImageUsage,
},
instance::{Instance, InstanceCreateFlags, InstanceCreateInfo},
memory::allocator::{AllocationCreateInfo, DeviceLayout, MemoryTypeFilter},
Expand All @@ -81,6 +78,9 @@ use vulkano::{
DeviceSize, Validated, VulkanError, VulkanLibrary,
};
use vulkano_taskgraph::{
command_buffer::{
BufferImageCopy, ClearColorImageInfo, CopyBufferToImageInfo, RecordingCommandBuffer,
},
graph::{CompileInfo, ExecuteError, TaskGraph},
resource::{AccessType, Flight, HostAccessType, ImageLayoutType, Resources},
resource_map, Id, QueueFamilyType, Task, TaskContext, TaskResult,
Expand Down Expand Up @@ -224,7 +224,7 @@ fn main() -> Result<(), impl Error> {
{transfer_family_index} for transfers",
);

let resources = Resources::new(device.clone(), Default::default());
let resources = Resources::new(&device, &Default::default());

let graphics_flight_id = resources.create_flight(MAX_FRAMES_IN_FLIGHT).unwrap();
let transfer_flight_id = resources.create_flight(1).unwrap();
Expand Down Expand Up @@ -343,16 +343,18 @@ fn main() -> Result<(), impl Error> {
// Initialize the resources.
unsafe {
vulkano_taskgraph::execute(
graphics_queue.clone(),
resources.clone(),
&graphics_queue,
&resources,
graphics_flight_id,
|cbf, tcx| {
tcx.write_buffer::<[MyVertex]>(vertex_buffer_id, ..)?
.copy_from_slice(&vertices);

for &texture_id in &texture_ids {
let texture = tcx.image(texture_id)?.image();
cbf.clear_color_image(&ClearColorImageInfo::image(texture.clone()))?;
cbf.clear_color_image_unchecked(&ClearColorImageInfo {
image: texture_id,
..Default::default()
});
}

Ok(())
Expand Down Expand Up @@ -504,7 +506,7 @@ fn main() -> Result<(), impl Error> {
framebuffers,
};

let mut task_graph = TaskGraph::new(resources.clone(), 1, 4);
let mut task_graph = TaskGraph::new(&resources, 1, 4);

let virtual_swapchain_id = task_graph.add_swapchain(&SwapchainCreateInfo::default());
let virtual_texture_id = task_graph.add_image(&texture_create_info);
Expand Down Expand Up @@ -543,9 +545,9 @@ fn main() -> Result<(), impl Error> {
);

let task_graph = unsafe {
task_graph.compile(CompileInfo {
queues: vec![graphics_queue.clone()],
present_queue: Some(graphics_queue.clone()),
task_graph.compile(&CompileInfo {
queues: &[&graphics_queue],
present_queue: Some(&graphics_queue),
flight_id: graphics_flight_id,
..Default::default()
})
Expand Down Expand Up @@ -729,7 +731,7 @@ impl Task for RenderTask {

unsafe fn execute(
&self,
cbf: &mut RawRecordingCommandBuffer,
cbf: &mut RecordingCommandBuffer<'_>,
tcx: &mut TaskContext<'_>,
rcx: &Self::World,
) -> TaskResult {
Expand All @@ -755,6 +757,8 @@ impl Task for RenderTask {
},
};

let cbf = cbf.raw_command_buffer();

cbf.begin_render_pass(
&RenderPassBeginInfo {
clear_values: vec![Some([0.0, 0.0, 0.0, 1.0].into())],
Expand Down Expand Up @@ -831,7 +835,7 @@ fn run_worker(
.unwrap()
});

let mut task_graph = TaskGraph::new(resources.clone(), 1, 3);
let mut task_graph = TaskGraph::new(&resources, 1, 3);

let virtual_front_staging_buffer_id = task_graph.add_buffer(&BufferCreateInfo::default());
let virtual_back_staging_buffer_id = task_graph.add_buffer(&BufferCreateInfo::default());
Expand Down Expand Up @@ -861,8 +865,8 @@ fn run_worker(
);

let task_graph = unsafe {
task_graph.compile(CompileInfo {
queues: vec![transfer_queue],
task_graph.compile(&CompileInfo {
queues: &[&transfer_queue],
flight_id: transfer_flight_id,
..Default::default()
})
Expand Down Expand Up @@ -942,7 +946,7 @@ impl Task for UploadTask {

unsafe fn execute(
&self,
cbf: &mut RawRecordingCommandBuffer,
cbf: &mut RecordingCommandBuffer<'_>,
tcx: &mut TaskContext<'_>,
&current_corner: &Self::World,
) -> TaskResult {
Expand All @@ -967,42 +971,38 @@ impl Task for UploadTask {
tcx.write_buffer::<[_]>(self.front_staging_buffer_id, ..)?
.fill(color);

let texture = tcx.image(self.texture_id)?.image();

cbf.copy_buffer_to_image(&CopyBufferToImageInfo {
regions: [BufferImageCopy {
image_subresource: texture.subresource_layers(),
cbf.copy_buffer_to_image_unchecked(&CopyBufferToImageInfo {
src_buffer: self.front_staging_buffer_id,
dst_image: self.texture_id,
regions: &[BufferImageCopy {
image_subresource: ImageSubresourceLayers {
aspects: ImageAspects::COLOR,
mip_level: 0,
array_layers: 0..1,
},
image_offset: CORNER_OFFSETS[current_corner % 4],
image_extent: [TRANSFER_GRANULARITY, TRANSFER_GRANULARITY, 1],
..Default::default()
}]
.into(),
..CopyBufferToImageInfo::buffer_image(
tcx.buffer(self.front_staging_buffer_id)?
.buffer()
.clone()
.into(),
texture.clone(),
)
})?;
}],
..Default::default()
});

if current_corner > 0 {
cbf.copy_buffer_to_image(&CopyBufferToImageInfo {
regions: [BufferImageCopy {
image_subresource: texture.subresource_layers(),
cbf.copy_buffer_to_image_unchecked(&CopyBufferToImageInfo {
src_buffer: self.back_staging_buffer_id,
dst_image: self.texture_id,
regions: &[BufferImageCopy {
image_subresource: ImageSubresourceLayers {
aspects: ImageAspects::COLOR,
mip_level: 0,
array_layers: 0..1,
},
image_offset: CORNER_OFFSETS[(current_corner - 1) % 4],
image_extent: [TRANSFER_GRANULARITY, TRANSFER_GRANULARITY, 1],
..Default::default()
}]
.into(),
..CopyBufferToImageInfo::buffer_image(
tcx.buffer(self.back_staging_buffer_id)?
.buffer()
.clone()
.into(),
texture.clone(),
)
})?;
}],
..Default::default()
});
}

Ok(())
Expand Down
Loading

0 comments on commit 10f0d19

Please sign in to comment.