Skip to content

Commit ca3919c

Browse files
committed
Cleanup some redundant acquire/release barrier code.
1 parent 5bac73d commit ca3919c

File tree

4 files changed

+42
-93
lines changed

4 files changed

+42
-93
lines changed

tests/external_objects.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ static bool run_test(Device &producer, Device &consumer)
143143
VK_PIPELINE_STAGE_TRANSFER_BIT, 0,
144144
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT);
145145
fill_cmd->clear_image(*write_image, clear_value);
146-
fill_cmd->release_external_buffer_barrier(*write_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT);
147-
fill_cmd->release_external_image_barrier(*write_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
148-
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT);
146+
fill_cmd->release_buffer_barrier(*write_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT);
147+
fill_cmd->release_image_barrier(*write_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
148+
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT);
149149
producer.submit(fill_cmd);
150150
auto external = producer.request_semaphore_external(
151151
VK_SEMAPHORE_TYPE_BINARY, ExternalHandle::get_opaque_semaphore_handle_type());
@@ -167,9 +167,9 @@ static bool run_test(Device &producer, Device &consumer)
167167
consumer.add_wait_semaphore(CommandBuffer::Type::AsyncTransfer, import, VK_PIPELINE_STAGE_TRANSFER_BIT, true);
168168
auto copy_cmd = consumer.request_command_buffer(CommandBuffer::Type::AsyncTransfer);
169169
const VkBufferCopy copy = { 0, 2 * i * sizeof(uint32_t), sizeof(uint32_t) };
170-
copy_cmd->acquire_external_buffer_barrier(*read_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT);
171-
copy_cmd->acquire_external_image_barrier(*read_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
172-
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT);
170+
copy_cmd->acquire_buffer_barrier(*read_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT);
171+
copy_cmd->acquire_image_barrier(*read_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
172+
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT);
173173
copy_cmd->copy_buffer(*readback_buffer, *read_buffer, &copy, 1);
174174
copy_cmd->copy_image_to_buffer(*readback_buffer, *read_image,
175175
(2 * i + 1) * sizeof(uint32_t), {}, {1, 1, 1}, 0, 0, { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 });

vulkan/command_buffer.cpp

+23-67
Original file line numberDiff line numberDiff line change
@@ -536,10 +536,11 @@ static uint32_t deduce_acquire_release_family_index(Device &device, const Image
536536
return family;
537537
}
538538

539-
void CommandBuffer::release_external_image_barrier(
539+
void CommandBuffer::release_image_barrier(
540540
const Image &image,
541541
VkImageLayout old_layout, VkImageLayout new_layout,
542-
VkPipelineStageFlags2 src_stage, VkAccessFlags2 src_access)
542+
VkPipelineStageFlags2 src_stage, VkAccessFlags2 src_access,
543+
uint32_t dst_queue_family)
543544
{
544545
VkImageMemoryBarrier2 barrier = { VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2 };
545546
uint32_t family_index = device->get_queue_info().family_indices[device->get_physical_queue_type(type)];
@@ -552,19 +553,21 @@ void CommandBuffer::release_external_image_barrier(
552553
};
553554
barrier.oldLayout = old_layout;
554555
barrier.newLayout = new_layout;
555-
barrier.srcAccessMask = src_access;
556556

557557
barrier.srcQueueFamilyIndex = deduce_acquire_release_family_index(*device, image, family_index);
558-
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_EXTERNAL;
558+
barrier.dstQueueFamilyIndex = dst_queue_family;
559+
560+
barrier.srcAccessMask = src_access;
559561
barrier.srcStageMask = src_stage;
560562

561563
image_barriers(1, &barrier);
562564
}
563565

564-
void CommandBuffer::acquire_external_image_barrier(
566+
void CommandBuffer::acquire_image_barrier(
565567
const Image &image,
566568
VkImageLayout old_layout, VkImageLayout new_layout,
567-
VkPipelineStageFlags2 dst_stage, VkAccessFlags2 dst_access)
569+
VkPipelineStageFlags2 dst_stage, VkAccessFlags2 dst_access,
570+
uint32_t src_queue_family)
568571
{
569572
VkImageMemoryBarrier2 b = { VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2 };
570573
uint32_t family_index = device->get_queue_info().family_indices[device->get_physical_queue_type(type)];
@@ -577,90 +580,43 @@ void CommandBuffer::acquire_external_image_barrier(
577580
};
578581
b.oldLayout = old_layout;
579582
b.newLayout = new_layout;
580-
b.dstAccessMask = dst_access;
581-
b.srcQueueFamilyIndex = VK_QUEUE_FAMILY_EXTERNAL;
583+
b.srcQueueFamilyIndex = src_queue_family;
582584
b.dstQueueFamilyIndex = deduce_acquire_release_family_index(*device, image, family_index);
583-
b.srcStageMask = VK_PIPELINE_STAGE_NONE;
585+
584586
b.dstStageMask = dst_stage;
587+
b.dstAccessMask = dst_access;
588+
585589
image_barriers(1, &b);
586590
}
587591

588-
void CommandBuffer::release_external_buffer_barrier(
592+
void CommandBuffer::release_buffer_barrier(
589593
const Buffer &buffer,
590-
VkPipelineStageFlags2 src_stage, VkAccessFlags2 src_access)
594+
VkPipelineStageFlags2 src_stage, VkAccessFlags2 src_access,
595+
uint32_t dst_queue_family)
591596
{
592597
VkBufferMemoryBarrier2 b = { VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER_2 };
593598
b.buffer = buffer.get_buffer();
594599
b.size = buffer.get_create_info().size;
595-
b.srcAccessMask = src_access;
596600
b.srcQueueFamilyIndex = deduce_acquire_release_family_index(*device);
597-
b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_EXTERNAL;
601+
b.dstQueueFamilyIndex = dst_queue_family;
598602
b.srcStageMask = src_stage;
599-
b.dstStageMask = VK_PIPELINE_STAGE_NONE;
603+
b.srcAccessMask = src_access;
600604
buffer_barriers(1, &b);
601605
}
602606

603-
void CommandBuffer::acquire_external_buffer_barrier(
607+
void CommandBuffer::acquire_buffer_barrier(
604608
const Buffer &buffer,
605-
VkPipelineStageFlags2 dst_stage, VkAccessFlags2 dst_access)
609+
VkPipelineStageFlags2 dst_stage, VkAccessFlags2 dst_access,
610+
uint32_t src_queue_family)
606611
{
607612
VkBufferMemoryBarrier2 b = { VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER_2 };
608613
b.buffer = buffer.get_buffer();
609614
b.size = buffer.get_create_info().size;
610-
b.dstAccessMask = dst_access;
611-
b.srcQueueFamilyIndex = VK_QUEUE_FAMILY_EXTERNAL;
615+
b.srcQueueFamilyIndex = src_queue_family;
612616
b.dstQueueFamilyIndex = deduce_acquire_release_family_index(*device);
613-
b.srcStageMask = VK_PIPELINE_STAGE_NONE;
614617
b.dstStageMask = dst_stage;
615-
buffer_barriers(1, &b);
616-
}
617-
618-
void CommandBuffer::image_barrier_acquire(const Vulkan::Image &image,
619-
VkImageLayout old_layout, VkImageLayout new_layout,
620-
uint32_t src_queue_family,
621-
VkPipelineStageFlags2 dst_stage, VkAccessFlags2 dst_access)
622-
{
623-
VK_ASSERT(!actual_render_pass);
624-
VK_ASSERT(!framebuffer);
625-
VK_ASSERT(image.get_create_info().domain != ImageDomain::Transient);
626-
627-
VkImageMemoryBarrier2 b = { VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2 };
628-
b.oldLayout = old_layout;
629-
b.newLayout = new_layout;
630-
b.image = image.get_image();
631-
b.subresourceRange.aspectMask = format_to_aspect_mask(image.get_create_info().format);
632-
b.subresourceRange.levelCount = image.get_create_info().levels;
633-
b.subresourceRange.layerCount = image.get_create_info().layers;
634-
b.srcQueueFamilyIndex = src_queue_family;
635-
b.dstQueueFamilyIndex = device->get_queue_info().family_indices[device->get_physical_queue_type(type)];
636618
b.dstAccessMask = dst_access;
637-
b.dstStageMask = dst_stage;
638-
639-
image_barriers(1, &b);
640-
}
641-
642-
void CommandBuffer::image_barrier_release(const Vulkan::Image &image,
643-
VkImageLayout old_layout, VkImageLayout new_layout,
644-
VkPipelineStageFlags2 src_stage, VkAccessFlags2 src_access,
645-
uint32_t dst_queue_family)
646-
{
647-
VK_ASSERT(!actual_render_pass);
648-
VK_ASSERT(!framebuffer);
649-
VK_ASSERT(image.get_create_info().domain != ImageDomain::Transient);
650-
651-
VkImageMemoryBarrier2 b = { VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2 };
652-
b.oldLayout = old_layout;
653-
b.newLayout = new_layout;
654-
b.image = image.get_image();
655-
b.subresourceRange.aspectMask = format_to_aspect_mask(image.get_create_info().format);
656-
b.subresourceRange.levelCount = image.get_create_info().levels;
657-
b.subresourceRange.layerCount = image.get_create_info().layers;
658-
b.srcQueueFamilyIndex = device->get_queue_info().family_indices[device->get_physical_queue_type(type)];
659-
b.dstQueueFamilyIndex = dst_queue_family;
660-
b.srcAccessMask = src_access;
661-
b.srcStageMask = src_stage;
662-
663-
image_barriers(1, &b);
619+
buffer_barriers(1, &b);
664620
}
665621

666622
void CommandBuffer::image_barrier(const Image &image,

vulkan/command_buffer.hpp

+11-17
Original file line numberDiff line numberDiff line change
@@ -390,26 +390,20 @@ class CommandBuffer : public Util::IntrusivePtrEnabled<CommandBuffer, CommandBuf
390390
VkPipelineStageFlags2 src_stage, VkAccessFlags2 src_access,
391391
VkPipelineStageFlags2 dst_stage, VkAccessFlags2 dst_access);
392392

393-
void image_barrier_acquire(const Image &image,
394-
VkImageLayout old_layout, VkImageLayout new_layout,
395-
uint32_t src_queue_family,
396-
VkPipelineStageFlags2 dst_stage, VkAccessFlags2 dst_access);
397-
398-
void image_barrier_release(const Image &image,
399-
VkImageLayout old_layout, VkImageLayout new_layout,
400-
VkPipelineStageFlags2 src_stage, VkAccessFlags2 src_access,
401-
uint32_t dst_queue_family);
402-
403393
void buffer_barriers(uint32_t buffer_barriers, const VkBufferMemoryBarrier2 *buffers);
404394
void image_barriers(uint32_t image_barriers, const VkImageMemoryBarrier2 *images);
405395

406-
void release_external_buffer_barrier(const Buffer &buffer, VkPipelineStageFlags2 src_stage, VkAccessFlags2 src_access);
407-
void acquire_external_buffer_barrier(const Buffer &buffer, VkPipelineStageFlags2 dst_stage, VkAccessFlags2 dst_access);
408-
void release_external_image_barrier(const Image &image,
409-
VkImageLayout old_layout, VkImageLayout new_layout,
410-
VkPipelineStageFlags2 src_stage, VkAccessFlags2 src_access);
411-
void acquire_external_image_barrier(const Image &image, VkImageLayout old_layout, VkImageLayout new_layout,
412-
VkPipelineStageFlags2 dst_stage, VkAccessFlags2 dst_access);
396+
void release_buffer_barrier(const Buffer &buffer, VkPipelineStageFlags2 src_stage, VkAccessFlags2 src_access,
397+
uint32_t dst_queue_family = VK_QUEUE_FAMILY_EXTERNAL);
398+
void acquire_buffer_barrier(const Buffer &buffer, VkPipelineStageFlags2 dst_stage, VkAccessFlags2 dst_access,
399+
uint32_t src_queue_family = VK_QUEUE_FAMILY_EXTERNAL);
400+
void release_image_barrier(const Image &image,
401+
VkImageLayout old_layout, VkImageLayout new_layout,
402+
VkPipelineStageFlags2 src_stage, VkAccessFlags2 src_access,
403+
uint32_t dst_queue_family = VK_QUEUE_FAMILY_EXTERNAL);
404+
void acquire_image_barrier(const Image &image, VkImageLayout old_layout, VkImageLayout new_layout,
405+
VkPipelineStageFlags2 dst_stage, VkAccessFlags2 dst_access,
406+
uint32_t src_queue_family = VK_QUEUE_FAMILY_EXTERNAL);
413407

414408
void blit_image(const Image &dst,
415409
const Image &src,

vulkan/wsi_dxgi.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,9 @@ bool DXGIInteropSwapchain::present(Vulkan::Semaphore release_semaphore, bool vsy
470470
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, true);
471471

472472
auto cmd = vk_device->request_command_buffer();
473-
cmd->image_barrier_release(*per_frame.vulkan_backbuffer,
473+
cmd->release_image_barrier(*per_frame.vulkan_backbuffer,
474474
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_IMAGE_LAYOUT_GENERAL,
475-
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0,
476-
VK_QUEUE_FAMILY_EXTERNAL);
475+
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0);
477476

478477
vk_device->submit(cmd);
479478
auto timeline_signal = vk_device->request_timeline_semaphore_as_binary(*vk_fence, ++fence_value);

0 commit comments

Comments
 (0)