2022-06-15 00:09:51 +00:00
|
|
|
#include "vulkan_swapchain.hpp"
|
|
|
|
|
|
|
|
namespace Vulkan
|
|
|
|
{
|
|
|
|
|
|
|
|
Swapchain::Swapchain(vk::Device device_, vk::PhysicalDevice physical_device_, vk::Queue queue_, vk::SurfaceKHR surface_, vk::CommandPool command_pool_)
|
|
|
|
: surface(surface_),
|
|
|
|
command_pool(command_pool_),
|
|
|
|
physical_device(physical_device_),
|
|
|
|
queue(queue_)
|
|
|
|
{
|
|
|
|
device = device_;
|
|
|
|
create_render_pass();
|
2023-04-14 20:37:38 +00:00
|
|
|
end_render_pass_function = nullptr;
|
2022-06-15 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Swapchain::~Swapchain()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-06-13 21:16:20 +00:00
|
|
|
void Swapchain::set_vsync(bool new_setting)
|
2022-06-15 00:09:51 +00:00
|
|
|
{
|
2023-02-10 22:03:05 +00:00
|
|
|
vsync = new_setting;
|
2022-06-15 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 20:37:38 +00:00
|
|
|
void Swapchain::on_render_pass_end(std::function<void ()> function)
|
|
|
|
{
|
|
|
|
end_render_pass_function = function;
|
|
|
|
}
|
|
|
|
|
2022-06-15 00:09:51 +00:00
|
|
|
void Swapchain::create_render_pass()
|
|
|
|
{
|
|
|
|
auto attachment_description = vk::AttachmentDescription{}
|
|
|
|
.setFormat(vk::Format::eB8G8R8A8Unorm)
|
|
|
|
.setSamples(vk::SampleCountFlagBits::e1)
|
|
|
|
.setLoadOp(vk::AttachmentLoadOp::eClear)
|
|
|
|
.setStoreOp(vk::AttachmentStoreOp::eStore)
|
|
|
|
.setStencilLoadOp(vk::AttachmentLoadOp::eDontCare)
|
|
|
|
.setStencilStoreOp(vk::AttachmentStoreOp::eStore)
|
|
|
|
.setInitialLayout(vk::ImageLayout::eUndefined)
|
|
|
|
.setFinalLayout(vk::ImageLayout::ePresentSrcKHR);
|
|
|
|
|
|
|
|
auto attachment_reference = vk::AttachmentReference{}
|
|
|
|
.setAttachment(0)
|
|
|
|
.setLayout(vk::ImageLayout::eColorAttachmentOptimal);
|
|
|
|
|
|
|
|
std::array<vk::SubpassDependency, 2> subpass_dependency{};
|
|
|
|
subpass_dependency[0]
|
|
|
|
.setSrcSubpass(VK_SUBPASS_EXTERNAL)
|
|
|
|
.setDstSubpass(0)
|
|
|
|
.setSrcStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput)
|
|
|
|
.setSrcAccessMask(vk::AccessFlagBits(0))
|
|
|
|
.setDstStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput)
|
|
|
|
.setDstAccessMask(vk::AccessFlagBits::eColorAttachmentWrite);
|
|
|
|
subpass_dependency[1]
|
|
|
|
.setSrcSubpass(VK_SUBPASS_EXTERNAL)
|
|
|
|
.setDstSubpass(0)
|
|
|
|
.setSrcStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput)
|
|
|
|
.setSrcAccessMask(vk::AccessFlagBits::eColorAttachmentWrite)
|
|
|
|
.setDstStageMask(vk::PipelineStageFlagBits::eFragmentShader)
|
|
|
|
.setDstAccessMask(vk::AccessFlagBits::eShaderRead);
|
|
|
|
|
|
|
|
|
|
|
|
auto subpass_description = vk::SubpassDescription{}
|
|
|
|
.setColorAttachments(attachment_reference)
|
|
|
|
.setPipelineBindPoint(vk::PipelineBindPoint::eGraphics);
|
|
|
|
|
|
|
|
auto render_pass_create_info = vk::RenderPassCreateInfo{}
|
|
|
|
.setSubpasses(subpass_description)
|
|
|
|
.setDependencies(subpass_dependency)
|
|
|
|
.setAttachments(attachment_description);
|
|
|
|
|
2024-04-12 23:47:16 +00:00
|
|
|
render_pass = device.createRenderPassUnique(render_pass_create_info).value;
|
2022-06-15 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
2023-01-24 23:33:46 +00:00
|
|
|
bool Swapchain::recreate(int new_width, int new_height)
|
2022-06-15 00:09:51 +00:00
|
|
|
{
|
2024-06-12 21:51:12 +00:00
|
|
|
if (swapchain_object)
|
2024-06-13 16:16:36 +00:00
|
|
|
{
|
2024-06-13 19:39:36 +00:00
|
|
|
device.waitIdle();
|
2024-06-12 21:51:12 +00:00
|
|
|
wait_on_frames();
|
2024-06-13 16:16:36 +00:00
|
|
|
}
|
2024-06-12 21:51:12 +00:00
|
|
|
|
2023-01-25 20:03:21 +00:00
|
|
|
return create(num_swapchain_images, new_width, new_height);
|
2022-06-15 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vk::Image Swapchain::get_image()
|
|
|
|
{
|
2024-06-13 16:16:36 +00:00
|
|
|
return image_data[current_swapchain_image].image;
|
2022-06-15 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
2024-06-13 16:37:00 +00:00
|
|
|
template<typename T>
|
|
|
|
static bool vector_find(std::vector<T> haystack, T&& needle)
|
|
|
|
{
|
|
|
|
for (auto &elem : haystack)
|
|
|
|
if (elem == needle)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
vk::PresentModeKHR Swapchain::get_present_mode() {
|
|
|
|
auto present_mode = vk::PresentModeKHR::eFifo;
|
2024-06-13 21:16:20 +00:00
|
|
|
|
2024-06-13 16:37:00 +00:00
|
|
|
if (!vsync) {
|
|
|
|
if (supports_mailbox)
|
|
|
|
present_mode = vk::PresentModeKHR::eMailbox;
|
|
|
|
if (supports_immediate)
|
|
|
|
present_mode = vk::PresentModeKHR::eImmediate;
|
|
|
|
}
|
2024-06-13 21:16:20 +00:00
|
|
|
|
2024-06-13 16:37:00 +00:00
|
|
|
return present_mode;
|
|
|
|
}
|
|
|
|
|
2023-07-16 00:14:44 +00:00
|
|
|
bool Swapchain::check_and_resize(int width, int height)
|
|
|
|
{
|
|
|
|
vk::SurfaceCapabilitiesKHR surface_capabilities;
|
|
|
|
|
|
|
|
if (width == -1 && height == -1)
|
|
|
|
{
|
2024-04-12 23:47:16 +00:00
|
|
|
surface_capabilities = physical_device.getSurfaceCapabilitiesKHR(surface).value;
|
2023-07-16 00:14:44 +00:00
|
|
|
width = surface_capabilities.currentExtent.width;
|
|
|
|
height = surface_capabilities.currentExtent.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width < 1 || height < 1)
|
|
|
|
return false;
|
|
|
|
|
2023-10-11 00:32:31 +00:00
|
|
|
if (extents.width != (uint32_t)width || extents.height != (uint32_t)height)
|
2023-07-16 00:14:44 +00:00
|
|
|
{
|
|
|
|
recreate(width, height);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-25 20:03:21 +00:00
|
|
|
bool Swapchain::create(unsigned int desired_num_swapchain_images, int new_width, int new_height)
|
2022-06-15 00:09:51 +00:00
|
|
|
{
|
|
|
|
frames.clear();
|
2024-06-13 16:16:36 +00:00
|
|
|
image_data.clear();
|
2022-06-15 00:09:51 +00:00
|
|
|
|
2024-04-12 23:47:16 +00:00
|
|
|
auto surface_capabilities = physical_device.getSurfaceCapabilitiesKHR(surface).value;
|
2022-06-15 00:09:51 +00:00
|
|
|
|
2023-01-25 20:03:21 +00:00
|
|
|
if (surface_capabilities.minImageCount > desired_num_swapchain_images)
|
|
|
|
num_swapchain_images = surface_capabilities.minImageCount;
|
2022-06-15 00:09:51 +00:00
|
|
|
else
|
2023-01-25 20:03:21 +00:00
|
|
|
num_swapchain_images = desired_num_swapchain_images;
|
2022-06-15 00:09:51 +00:00
|
|
|
|
2024-02-08 00:41:18 +00:00
|
|
|
// If extents aren't reported (Wayland), we have to rely on Wayland to report
|
|
|
|
// the size, so keep current extent.
|
2024-05-04 20:43:54 +00:00
|
|
|
if (surface_capabilities.currentExtent.width != UINT32_MAX)
|
2024-02-08 00:41:18 +00:00
|
|
|
extents = surface_capabilities.currentExtent;
|
2022-06-15 00:09:51 +00:00
|
|
|
|
2023-09-04 18:45:16 +00:00
|
|
|
uint32_t graphics_queue_index = 0;
|
|
|
|
auto queue_properties = physical_device.getQueueFamilyProperties();
|
|
|
|
for (size_t i = 0; i < queue_properties.size(); i++)
|
|
|
|
{
|
|
|
|
if (queue_properties[i].queueFlags & vk::QueueFlagBits::eGraphics)
|
|
|
|
{
|
|
|
|
graphics_queue_index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 23:33:46 +00:00
|
|
|
if (new_width > 0 && new_height > 0)
|
|
|
|
{
|
|
|
|
// No buffer is allocated for surface yet
|
|
|
|
extents.width = new_width;
|
|
|
|
extents.height = new_height;
|
|
|
|
}
|
|
|
|
else if (extents.width < 1 || extents.height < 1)
|
2022-06-15 00:09:51 +00:00
|
|
|
{
|
|
|
|
// Surface is likely hidden
|
|
|
|
printf("Extents too small.\n");
|
2023-01-24 23:33:46 +00:00
|
|
|
swapchain_object.reset();
|
2022-06-15 00:09:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-09-04 16:56:10 +00:00
|
|
|
|
|
|
|
if (extents.width > surface_capabilities.maxImageExtent.width)
|
|
|
|
extents.width = surface_capabilities.maxImageExtent.width;
|
|
|
|
if (extents.height > surface_capabilities.maxImageExtent.height)
|
|
|
|
extents.height = surface_capabilities.maxImageExtent.height;
|
|
|
|
if (extents.width < surface_capabilities.minImageExtent.width)
|
|
|
|
extents.width = surface_capabilities.minImageExtent.width;
|
|
|
|
if (extents.height < surface_capabilities.minImageExtent.height)
|
|
|
|
extents.height = surface_capabilities.minImageExtent.height;
|
2022-06-15 00:09:51 +00:00
|
|
|
|
2024-04-12 23:47:16 +00:00
|
|
|
auto present_modes = physical_device.getSurfacePresentModesKHR(surface).value;
|
2024-06-13 16:37:00 +00:00
|
|
|
supports_mailbox = vector_find(present_modes, vk::PresentModeKHR::eMailbox);
|
|
|
|
supports_immediate = vector_find(present_modes, vk::PresentModeKHR::eImmediate);
|
|
|
|
supports_relaxed = vector_find(present_modes, vk::PresentModeKHR::eFifoRelaxed);
|
2024-06-11 21:06:30 +00:00
|
|
|
|
2024-06-12 21:51:12 +00:00
|
|
|
auto swapchain_maintenance_info = vk::SwapchainPresentModesCreateInfoEXT{}
|
|
|
|
.setPresentModes(present_modes);
|
2023-09-04 16:37:28 +00:00
|
|
|
|
2022-06-15 00:09:51 +00:00
|
|
|
auto swapchain_create_info = vk::SwapchainCreateInfoKHR{}
|
2023-01-25 20:03:21 +00:00
|
|
|
.setMinImageCount(num_swapchain_images)
|
2022-06-15 00:09:51 +00:00
|
|
|
.setImageFormat(vk::Format::eB8G8R8A8Unorm)
|
|
|
|
.setImageExtent(extents)
|
|
|
|
.setImageColorSpace(vk::ColorSpaceKHR::eSrgbNonlinear)
|
|
|
|
.setImageSharingMode(vk::SharingMode::eExclusive)
|
|
|
|
.setImageUsage(vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eTransferSrc)
|
|
|
|
.setCompositeAlpha(vk::CompositeAlphaFlagBitsKHR::eOpaque)
|
|
|
|
.setClipped(true)
|
2024-06-13 16:37:00 +00:00
|
|
|
.setPresentMode(get_present_mode())
|
2022-06-15 00:09:51 +00:00
|
|
|
.setSurface(surface)
|
2023-07-03 22:42:45 +00:00
|
|
|
.setPreTransform(vk::SurfaceTransformFlagBitsKHR::eIdentity)
|
2023-09-04 18:45:16 +00:00
|
|
|
.setImageArrayLayers(1)
|
2024-06-12 21:51:12 +00:00
|
|
|
.setQueueFamilyIndices(graphics_queue_index)
|
|
|
|
.setPNext(&swapchain_maintenance_info);
|
2022-06-15 00:09:51 +00:00
|
|
|
|
2024-06-11 21:06:30 +00:00
|
|
|
swapchain_object.reset();
|
|
|
|
auto resval = device.createSwapchainKHRUnique(swapchain_create_info);
|
|
|
|
if (resval.result != vk::Result::eSuccess && resval.result != vk::Result::eSuboptimalKHR)
|
|
|
|
{
|
2023-09-04 16:37:28 +00:00
|
|
|
swapchain_object.reset();
|
2023-01-24 23:33:46 +00:00
|
|
|
return false;
|
2024-06-11 21:06:30 +00:00
|
|
|
}
|
|
|
|
swapchain_object = std::move(resval.value);
|
2022-06-15 00:09:51 +00:00
|
|
|
|
2024-06-12 21:51:12 +00:00
|
|
|
create_resources();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Swapchain::create_resources()
|
|
|
|
{
|
2024-04-12 23:47:16 +00:00
|
|
|
auto swapchain_images = device.getSwapchainImagesKHR(swapchain_object.get()).value;
|
2024-06-12 21:51:12 +00:00
|
|
|
if (swapchain_images.size() > num_swapchain_images)
|
|
|
|
num_swapchain_images = swapchain_images.size();
|
2022-06-15 00:09:51 +00:00
|
|
|
|
2024-06-12 21:51:12 +00:00
|
|
|
vk::CommandBufferAllocateInfo command_buffer_allocate_info(command_pool, vk::CommandBufferLevel::ePrimary, num_swapchain_images);
|
|
|
|
auto command_buffers = device.allocateCommandBuffersUnique(command_buffer_allocate_info).value;
|
2022-06-15 00:09:51 +00:00
|
|
|
|
2023-07-03 22:19:54 +00:00
|
|
|
frames.resize(num_swapchain_images);
|
2024-06-13 16:16:36 +00:00
|
|
|
image_data.resize(num_swapchain_images);
|
2022-06-15 00:09:51 +00:00
|
|
|
|
|
|
|
vk::FenceCreateInfo fence_create_info(vk::FenceCreateFlagBits::eSignaled);
|
|
|
|
|
2023-10-11 00:32:31 +00:00
|
|
|
for (unsigned int i = 0; i < num_swapchain_images; i++)
|
2022-06-15 00:09:51 +00:00
|
|
|
{
|
|
|
|
// Create frame queue resources
|
|
|
|
auto &frame = frames[i];
|
|
|
|
frame.command_buffer = std::move(command_buffers[i]);
|
2024-04-12 23:47:16 +00:00
|
|
|
frame.fence = device.createFenceUnique(fence_create_info).value;
|
|
|
|
frame.acquire = device.createSemaphoreUnique({}).value;
|
|
|
|
frame.complete = device.createSemaphoreUnique({}).value;
|
2022-06-15 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 20:03:21 +00:00
|
|
|
for (unsigned int i = 0; i < num_swapchain_images; i++)
|
2022-06-15 00:09:51 +00:00
|
|
|
{
|
|
|
|
// Create resources associated with swapchain images
|
2024-06-13 16:16:36 +00:00
|
|
|
auto &image = image_data[i];
|
2022-06-15 00:09:51 +00:00
|
|
|
image.image = swapchain_images[i];
|
|
|
|
auto image_view_create_info = vk::ImageViewCreateInfo{}
|
|
|
|
.setImage(swapchain_images[i])
|
|
|
|
.setViewType(vk::ImageViewType::e2D)
|
|
|
|
.setFormat(vk::Format::eB8G8R8A8Unorm)
|
|
|
|
.setComponents(vk::ComponentMapping())
|
|
|
|
.setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1));
|
2024-04-12 23:47:16 +00:00
|
|
|
image.image_view = device.createImageViewUnique(image_view_create_info).value;
|
2022-06-15 00:09:51 +00:00
|
|
|
|
|
|
|
auto framebuffer_create_info = vk::FramebufferCreateInfo{}
|
|
|
|
.setAttachments(image.image_view.get())
|
|
|
|
.setWidth(extents.width)
|
|
|
|
.setHeight(extents.height)
|
|
|
|
.setLayers(1)
|
|
|
|
.setRenderPass(render_pass.get());
|
2024-04-12 23:47:16 +00:00
|
|
|
image.framebuffer = device.createFramebufferUnique(framebuffer_create_info).value;
|
2024-06-13 16:16:36 +00:00
|
|
|
|
|
|
|
image.fence = device.createFenceUnique(fence_create_info).value;
|
2022-06-15 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
current_swapchain_image = 0;
|
2024-06-12 21:51:12 +00:00
|
|
|
current_frame = 0;
|
2022-06-15 00:09:51 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Swapchain::begin_frame()
|
|
|
|
{
|
2023-01-24 23:33:46 +00:00
|
|
|
if (!swapchain_object || extents.width < 1 || extents.height < 1)
|
2022-06-15 00:09:51 +00:00
|
|
|
{
|
|
|
|
printf ("Extents too small\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &frame = frames[current_frame];
|
|
|
|
|
2024-06-13 16:16:36 +00:00
|
|
|
auto result = device.waitForFences({ frame.fence.get() }, true, 33333333);
|
2022-06-15 00:09:51 +00:00
|
|
|
if (result != vk::Result::eSuccess)
|
|
|
|
{
|
2023-06-27 20:39:30 +00:00
|
|
|
printf("Timed out waiting for fence.\n");
|
2022-06-15 00:09:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-04 23:57:35 +00:00
|
|
|
vk::ResultValue<uint32_t> result_value(vk::Result::eSuccess, 0);
|
2024-04-12 23:47:16 +00:00
|
|
|
result_value = device.acquireNextImageKHR(swapchain_object.get(), UINT64_MAX, frame.acquire.get());
|
2024-06-11 21:06:30 +00:00
|
|
|
|
2022-06-15 00:09:51 +00:00
|
|
|
if (result_value.result == vk::Result::eErrorOutOfDateKHR ||
|
|
|
|
result_value.result == vk::Result::eSuboptimalKHR)
|
|
|
|
{
|
|
|
|
recreate();
|
|
|
|
return begin_frame();
|
|
|
|
}
|
|
|
|
|
2023-06-27 20:39:30 +00:00
|
|
|
if (result_value.result == vk::Result::eTimeout)
|
|
|
|
{
|
|
|
|
printf("Timed out waiting for swapchain.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-15 00:09:51 +00:00
|
|
|
if (result_value.result != vk::Result::eSuccess)
|
|
|
|
{
|
2023-02-23 23:20:35 +00:00
|
|
|
printf("Unable to acquire swapchain image: %s\n", vk::to_string(result_value.result).c_str());
|
2022-06-15 00:09:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_swapchain_image = result_value.value;
|
|
|
|
|
2023-01-25 20:03:21 +00:00
|
|
|
device.resetFences(frame.fence.get());
|
|
|
|
|
2022-06-15 00:09:51 +00:00
|
|
|
vk::CommandBufferBeginInfo command_buffer_begin_info(vk::CommandBufferUsageFlags{vk::CommandBufferUsageFlagBits::eOneTimeSubmit});
|
|
|
|
frame.command_buffer->begin(command_buffer_begin_info);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-23 21:24:01 +00:00
|
|
|
void Swapchain::end_frame_without_swap()
|
2022-06-15 00:09:51 +00:00
|
|
|
{
|
|
|
|
auto &frame = frames[current_frame];
|
|
|
|
frame.command_buffer->end();
|
|
|
|
|
|
|
|
vk::PipelineStageFlags flags = vk::PipelineStageFlagBits::eColorAttachmentOutput;
|
|
|
|
vk::SubmitInfo submit_info(
|
|
|
|
frame.acquire.get(),
|
|
|
|
flags,
|
|
|
|
frame.command_buffer.get(),
|
|
|
|
frame.complete.get());
|
|
|
|
|
|
|
|
queue.submit(submit_info, frame.fence.get());
|
2023-02-23 21:24:01 +00:00
|
|
|
}
|
2022-06-15 00:09:51 +00:00
|
|
|
|
2023-02-23 21:24:01 +00:00
|
|
|
bool Swapchain::swap()
|
|
|
|
{
|
2022-06-15 00:09:51 +00:00
|
|
|
auto present_info = vk::PresentInfoKHR{}
|
|
|
|
.setWaitSemaphores(frames[current_frame].complete.get())
|
2023-01-24 23:33:46 +00:00
|
|
|
.setSwapchains(swapchain_object.get())
|
2022-06-15 00:09:51 +00:00
|
|
|
.setImageIndices(current_swapchain_image);
|
|
|
|
|
2024-06-12 21:51:12 +00:00
|
|
|
vk::SwapchainPresentModeInfoEXT present_mode_info;
|
2024-06-13 16:37:00 +00:00
|
|
|
auto present_mode = get_present_mode();
|
2024-06-12 21:51:12 +00:00
|
|
|
present_mode_info.setPresentModes(present_mode);
|
|
|
|
present_info.setPNext(&present_mode_info);
|
|
|
|
|
2024-06-13 16:16:36 +00:00
|
|
|
auto &present_fence = image_data[current_swapchain_image].fence.get();
|
|
|
|
device.resetFences(present_fence);
|
|
|
|
vk::SwapchainPresentFenceInfoEXT present_fence_info(present_fence);
|
2024-06-12 21:51:12 +00:00
|
|
|
present_mode_info.setPNext(&present_fence_info);
|
|
|
|
|
|
|
|
vk::Result result = queue.presentKHR(present_info);
|
2024-04-12 23:47:16 +00:00
|
|
|
if (result == vk::Result::eErrorOutOfDateKHR)
|
|
|
|
{
|
2023-10-11 00:32:31 +00:00
|
|
|
// NVIDIA binary drivers will set OutOfDate between acquire and
|
|
|
|
// present. Ignore this and fix it on the next swapchain acquire.
|
2024-06-11 21:06:30 +00:00
|
|
|
}
|
2022-06-15 00:09:51 +00:00
|
|
|
|
2023-07-03 22:19:54 +00:00
|
|
|
current_frame = (current_frame + 1) % num_swapchain_images;
|
2022-06-15 00:09:51 +00:00
|
|
|
|
2023-09-04 19:00:03 +00:00
|
|
|
if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR)
|
2022-06-15 00:09:51 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-23 21:24:01 +00:00
|
|
|
bool Swapchain::end_frame()
|
|
|
|
{
|
|
|
|
end_frame_without_swap();
|
|
|
|
return swap();
|
|
|
|
}
|
|
|
|
|
2022-06-15 00:09:51 +00:00
|
|
|
vk::Framebuffer Swapchain::get_framebuffer()
|
|
|
|
{
|
2024-06-13 16:16:36 +00:00
|
|
|
return image_data[current_swapchain_image].framebuffer.get();
|
2022-06-15 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vk::CommandBuffer &Swapchain::get_cmd()
|
|
|
|
{
|
|
|
|
return frames[current_frame].command_buffer.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Swapchain::begin_render_pass()
|
|
|
|
{
|
|
|
|
vk::ClearColorValue colorval;
|
|
|
|
colorval.setFloat32({ 0.0f, 0.0f, 0.0f, 1.0f });
|
|
|
|
vk::ClearValue value;
|
|
|
|
value.setColor(colorval);
|
|
|
|
|
|
|
|
auto render_pass_begin_info = vk::RenderPassBeginInfo{}
|
|
|
|
.setRenderPass(render_pass.get())
|
2024-06-13 16:16:36 +00:00
|
|
|
.setFramebuffer(image_data[current_swapchain_image].framebuffer.get())
|
2022-06-15 00:09:51 +00:00
|
|
|
.setRenderArea(vk::Rect2D({}, extents))
|
|
|
|
.setClearValues(value);
|
|
|
|
get_cmd().beginRenderPass(render_pass_begin_info, vk::SubpassContents::eInline);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Swapchain::end_render_pass()
|
|
|
|
{
|
2023-04-14 20:37:38 +00:00
|
|
|
if (end_render_pass_function)
|
|
|
|
{
|
|
|
|
end_render_pass_function();
|
|
|
|
end_render_pass_function = nullptr;
|
|
|
|
}
|
|
|
|
|
2022-06-15 00:09:51 +00:00
|
|
|
get_cmd().endRenderPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Swapchain::wait_on_frame(int frame_num)
|
|
|
|
{
|
2024-06-13 16:16:36 +00:00
|
|
|
auto result = device.waitForFences({ image_data[frame_num].fence.get() }, true, 33000000);
|
2022-06-15 00:09:51 +00:00
|
|
|
return (result == vk::Result::eSuccess);
|
|
|
|
}
|
|
|
|
|
2024-06-12 21:51:12 +00:00
|
|
|
void Swapchain::wait_on_frames()
|
|
|
|
{
|
2024-06-13 16:16:36 +00:00
|
|
|
for (auto i = 0; i < image_data.size(); i++)
|
2024-06-12 21:51:12 +00:00
|
|
|
wait_on_frame(i);
|
|
|
|
}
|
|
|
|
|
2022-06-15 00:09:51 +00:00
|
|
|
vk::Extent2D Swapchain::get_extents()
|
|
|
|
{
|
|
|
|
return extents;
|
|
|
|
}
|
|
|
|
|
|
|
|
vk::RenderPass &Swapchain::get_render_pass()
|
|
|
|
{
|
|
|
|
return render_pass.get();
|
|
|
|
}
|
|
|
|
|
2024-02-08 00:41:18 +00:00
|
|
|
} // namespace Vulkan
|