Skip to content

Commit

Permalink
vk: annotate debug scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Sep 23, 2024
1 parent fdc80c0 commit a4215a2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
67 changes: 55 additions & 12 deletions blade-graphics/src/vulkan/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,16 @@ fn map_render_target(rt: &crate::RenderTarget) -> vk::RenderingAttachmentInfo<'s
vk_info
}

fn end_pass(device: &super::Device, cmd_buf: vk::CommandBuffer) {
if device.toggles.command_scopes {
unsafe {
device.debug_utils.cmd_end_debug_utils_label(cmd_buf);
}
}
}

impl super::CommandEncoder {
pub fn mark(&mut self, marker: &str) {
fn add_marker(&mut self, marker: &str) {
if let Some(ref mut ch) = self.crash_handler {
let id = ch.add_marker(marker);
unsafe {
Expand All @@ -217,10 +225,30 @@ impl super::CommandEncoder {
}
}

fn begin_pass(&mut self, label: &str) {
self.barrier();
self.add_marker(label);
if self.device.toggles.command_scopes {
self.temp_label.clear();
self.temp_label.extend_from_slice(label.as_bytes());
self.temp_label.push(0);
unsafe {
self.device.debug_utils.cmd_begin_debug_utils_label(
self.buffers[0].raw,
&vk::DebugUtilsLabelEXT {
p_label_name: self.temp_label.as_ptr() as *const _,
..Default::default()
},
)
}
}
}

pub fn start(&mut self) {
self.buffers.rotate_left(1);
let cmd_buf = self.buffers.first_mut().unwrap();
self.device
.reset_descriptor_pool(&mut self.buffers[0].descriptor_pool);
.reset_descriptor_pool(&mut cmd_buf.descriptor_pool);

let vk_info = vk::CommandBufferBeginInfo {
flags: vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT,
Expand All @@ -229,14 +257,14 @@ impl super::CommandEncoder {
unsafe {
self.device
.core
.begin_command_buffer(self.buffers[0].raw, &vk_info)
.begin_command_buffer(cmd_buf.raw, &vk_info)
.unwrap();
}
}

pub(super) fn finish(&mut self) -> vk::CommandBuffer {
self.barrier();
self.mark("finish");
self.add_marker("finish");
let raw = self.buffers[0].raw;
unsafe { self.device.core.end_command_buffer(raw).unwrap() }
raw
Expand Down Expand Up @@ -331,8 +359,7 @@ impl super::CommandEncoder {
}

pub fn transfer(&mut self, label: &str) -> super::TransferCommandEncoder {
self.barrier();
self.mark(label);
self.begin_pass(label);
super::TransferCommandEncoder {
raw: self.buffers[0].raw,
device: &self.device,
Expand All @@ -343,17 +370,15 @@ impl super::CommandEncoder {
&mut self,
label: &str,
) -> super::AccelerationStructureCommandEncoder {
self.barrier();
self.mark(label);
self.begin_pass(label);
super::AccelerationStructureCommandEncoder {
raw: self.buffers[0].raw,
device: &self.device,
}
}

pub fn compute(&mut self, label: &str) -> super::ComputeCommandEncoder {
self.barrier();
self.mark(label);
self.begin_pass(label);
super::ComputeCommandEncoder {
cmd_buf: self.buffers.first_mut().unwrap(),
device: &self.device,
Expand All @@ -366,8 +391,7 @@ impl super::CommandEncoder {
label: &str,
targets: crate::RenderTargetSet,
) -> super::RenderCommandEncoder {
self.barrier();
self.mark(label);
self.begin_pass(label);

let mut target_size = [0u16; 2];
let mut color_attachments = Vec::with_capacity(targets.colors.len());
Expand Down Expand Up @@ -551,6 +575,12 @@ impl crate::traits::TransferEncoder for super::TransferCommandEncoder<'_> {
}
}

impl Drop for super::TransferCommandEncoder<'_> {
fn drop(&mut self) {
end_pass(self.device, self.raw);
}
}

#[hidden_trait::expose]
impl crate::traits::AccelerationStructureEncoder
for super::AccelerationStructureCommandEncoder<'_>
Expand Down Expand Up @@ -635,6 +665,12 @@ impl crate::traits::AccelerationStructureEncoder
}
}

impl Drop for super::AccelerationStructureCommandEncoder<'_> {
fn drop(&mut self) {
end_pass(self.device, self.raw);
}
}

impl<'a> super::ComputeCommandEncoder<'a> {
pub fn with<'b, 'p>(
&'b mut self,
Expand All @@ -651,6 +687,12 @@ impl<'a> super::ComputeCommandEncoder<'a> {
}
}

impl Drop for super::ComputeCommandEncoder<'_> {
fn drop(&mut self) {
end_pass(self.device, self.cmd_buf.raw);
}
}

impl<'a> super::RenderCommandEncoder<'a> {
pub fn set_scissor_rect(&mut self, rect: &crate::ScissorRect) {
let vk_scissor = vk::Rect2D {
Expand Down Expand Up @@ -692,6 +734,7 @@ impl Drop for super::RenderCommandEncoder<'_> {
.dynamic_rendering
.cmd_end_rendering(self.cmd_buf.raw)
};
end_pass(self.device, self.cmd_buf.raw);
}
}

Expand Down
3 changes: 3 additions & 0 deletions blade-graphics/src/vulkan/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,9 @@ impl super::Context {
},
core: device_core,
device_information: capabilities.device_information,
toggles: super::Toggles {
command_scopes: desc.capture,
},
//TODO: detect GPU family
workarounds: super::Workarounds {
extra_sync_src_access: vk::AccessFlags::TRANSFER_WRITE,
Expand Down
8 changes: 8 additions & 0 deletions blade-graphics/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ struct RayTracingDevice {
acceleration_structure: khr::acceleration_structure::Device,
}

#[derive(Clone, Default)]
struct Toggles {
command_scopes: bool,
}

#[derive(Clone)]
struct Workarounds {
extra_sync_src_access: vk::AccessFlags,
Expand All @@ -38,6 +43,7 @@ struct Device {
buffer_marker: Option<ash::amd::buffer_marker::Device>,
shader_info: Option<ash::amd::shader_info::Device>,
full_screen_exclusive: Option<ash::ext::full_screen_exclusive::Device>,
toggles: Toggles,
workarounds: Workarounds,
}

Expand Down Expand Up @@ -239,6 +245,7 @@ pub struct CommandEncoder {
update_data: Vec<u8>,
present: Option<Presentation>,
crash_handler: Option<CrashHandler>,
temp_label: Vec<u8>,
}
pub struct TransferCommandEncoder<'a> {
raw: vk::CommandBuffer,
Expand Down Expand Up @@ -367,6 +374,7 @@ impl crate::traits::CommandDevice for Context {
update_data: Vec::new(),
present: None,
crash_handler,
temp_label: Vec::new(),
}
}

Expand Down

0 comments on commit a4215a2

Please sign in to comment.