2019-03-29 03:58:43 +01:00
|
|
|
// Copyright 2019 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <limits>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/frontend/framebuffer_layout.h"
|
|
|
|
#include "video_core/renderer_vulkan/vk_device.h"
|
|
|
|
#include "video_core/renderer_vulkan/vk_resource_manager.h"
|
|
|
|
#include "video_core/renderer_vulkan/vk_swapchain.h"
|
2020-03-27 05:33:21 +01:00
|
|
|
#include "video_core/renderer_vulkan/wrapper.h"
|
2019-03-29 03:58:43 +01:00
|
|
|
|
|
|
|
namespace Vulkan {
|
|
|
|
|
|
|
|
namespace {
|
2019-12-07 02:41:42 +01:00
|
|
|
|
2020-03-27 05:33:21 +01:00
|
|
|
VkSurfaceFormatKHR ChooseSwapSurfaceFormat(vk::Span<VkSurfaceFormatKHR> formats, bool srgb) {
|
|
|
|
if (formats.size() == 1 && formats[0].format == VK_FORMAT_UNDEFINED) {
|
|
|
|
VkSurfaceFormatKHR format;
|
|
|
|
format.format = VK_FORMAT_B8G8R8A8_UNORM;
|
|
|
|
format.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
|
2019-12-07 02:41:42 +01:00
|
|
|
return format;
|
2019-03-29 03:58:43 +01:00
|
|
|
}
|
2019-12-07 02:41:42 +01:00
|
|
|
const auto& found = std::find_if(formats.begin(), formats.end(), [srgb](const auto& format) {
|
2020-03-27 05:33:21 +01:00
|
|
|
const auto request_format = srgb ? VK_FORMAT_B8G8R8A8_SRGB : VK_FORMAT_B8G8R8A8_UNORM;
|
2019-12-07 02:41:42 +01:00
|
|
|
return format.format == request_format &&
|
2020-03-27 05:33:21 +01:00
|
|
|
format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
|
2019-03-29 03:58:43 +01:00
|
|
|
});
|
|
|
|
return found != formats.end() ? *found : formats[0];
|
|
|
|
}
|
|
|
|
|
2020-03-27 05:33:21 +01:00
|
|
|
VkPresentModeKHR ChooseSwapPresentMode(vk::Span<VkPresentModeKHR> modes) {
|
2019-03-29 03:58:43 +01:00
|
|
|
// Mailbox doesn't lock the application like fifo (vsync), prefer it
|
2020-03-27 05:33:21 +01:00
|
|
|
const auto found = std::find(modes.begin(), modes.end(), VK_PRESENT_MODE_MAILBOX_KHR);
|
|
|
|
return found != modes.end() ? *found : VK_PRESENT_MODE_FIFO_KHR;
|
2019-03-29 03:58:43 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 05:33:21 +01:00
|
|
|
VkExtent2D ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, u32 width, u32 height) {
|
2019-03-29 03:58:43 +01:00
|
|
|
constexpr auto undefined_size{std::numeric_limits<u32>::max()};
|
|
|
|
if (capabilities.currentExtent.width != undefined_size) {
|
|
|
|
return capabilities.currentExtent;
|
|
|
|
}
|
2020-03-27 05:33:21 +01:00
|
|
|
VkExtent2D extent;
|
2019-03-29 03:58:43 +01:00
|
|
|
extent.width = std::max(capabilities.minImageExtent.width,
|
2020-03-27 05:33:21 +01:00
|
|
|
std::min(capabilities.maxImageExtent.width, width));
|
2019-03-29 03:58:43 +01:00
|
|
|
extent.height = std::max(capabilities.minImageExtent.height,
|
2020-03-27 05:33:21 +01:00
|
|
|
std::min(capabilities.maxImageExtent.height, height));
|
2019-03-29 03:58:43 +01:00
|
|
|
return extent;
|
|
|
|
}
|
2019-12-07 02:41:42 +01:00
|
|
|
|
|
|
|
} // Anonymous namespace
|
2019-03-29 03:58:43 +01:00
|
|
|
|
2020-03-27 05:33:21 +01:00
|
|
|
VKSwapchain::VKSwapchain(VkSurfaceKHR surface, const VKDevice& device)
|
2019-03-29 03:58:43 +01:00
|
|
|
: surface{surface}, device{device} {}
|
|
|
|
|
|
|
|
VKSwapchain::~VKSwapchain() = default;
|
|
|
|
|
2019-12-07 02:41:42 +01:00
|
|
|
void VKSwapchain::Create(u32 width, u32 height, bool srgb) {
|
2019-03-29 03:58:43 +01:00
|
|
|
const auto physical_device = device.GetPhysical();
|
2020-03-27 05:33:21 +01:00
|
|
|
const auto capabilities{physical_device.GetSurfaceCapabilitiesKHR(surface)};
|
2019-03-29 03:58:43 +01:00
|
|
|
if (capabilities.maxImageExtent.width == 0 || capabilities.maxImageExtent.height == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-27 05:33:21 +01:00
|
|
|
device.GetLogical().WaitIdle();
|
2019-03-29 03:58:43 +01:00
|
|
|
Destroy();
|
|
|
|
|
2019-12-07 02:41:42 +01:00
|
|
|
CreateSwapchain(capabilities, width, height, srgb);
|
2019-03-29 03:58:43 +01:00
|
|
|
CreateSemaphores();
|
|
|
|
CreateImageViews();
|
|
|
|
|
|
|
|
fences.resize(image_count, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VKSwapchain::AcquireNextImage() {
|
2020-03-27 05:33:21 +01:00
|
|
|
device.GetLogical().AcquireNextImageKHR(*swapchain, std::numeric_limits<u64>::max(),
|
|
|
|
*present_semaphores[frame_index], {}, &image_index);
|
2019-03-29 03:58:43 +01:00
|
|
|
|
|
|
|
if (auto& fence = fences[image_index]; fence) {
|
|
|
|
fence->Wait();
|
|
|
|
fence->Release();
|
|
|
|
fence = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 05:33:21 +01:00
|
|
|
bool VKSwapchain::Present(VkSemaphore render_semaphore, VKFence& fence) {
|
|
|
|
const VkSemaphore present_semaphore{*present_semaphores[frame_index]};
|
|
|
|
const std::array<VkSemaphore, 2> semaphores{present_semaphore, render_semaphore};
|
2019-03-29 03:58:43 +01:00
|
|
|
const auto present_queue{device.GetPresentQueue()};
|
|
|
|
bool recreated = false;
|
|
|
|
|
2020-07-17 01:27:02 +02:00
|
|
|
const VkPresentInfoKHR present_info{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
|
|
|
|
.pNext = nullptr,
|
|
|
|
.waitSemaphoreCount = render_semaphore ? 2U : 1U,
|
|
|
|
.pWaitSemaphores = semaphores.data(),
|
|
|
|
.swapchainCount = 1,
|
|
|
|
.pSwapchains = swapchain.address(),
|
|
|
|
.pImageIndices = &image_index,
|
|
|
|
.pResults = nullptr,
|
|
|
|
};
|
2020-03-27 05:33:21 +01:00
|
|
|
|
|
|
|
switch (const VkResult result = present_queue.Present(present_info)) {
|
|
|
|
case VK_SUCCESS:
|
|
|
|
break;
|
|
|
|
case VK_SUBOPTIMAL_KHR:
|
|
|
|
LOG_DEBUG(Render_Vulkan, "Suboptimal swapchain");
|
2019-03-29 03:58:43 +01:00
|
|
|
break;
|
2020-03-27 05:33:21 +01:00
|
|
|
case VK_ERROR_OUT_OF_DATE_KHR:
|
2019-03-29 03:58:43 +01:00
|
|
|
if (current_width > 0 && current_height > 0) {
|
2019-12-07 02:41:42 +01:00
|
|
|
Create(current_width, current_height, current_srgb);
|
2019-03-29 03:58:43 +01:00
|
|
|
recreated = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2020-03-27 05:33:21 +01:00
|
|
|
LOG_CRITICAL(Render_Vulkan, "Failed to present with error {}", vk::ToString(result));
|
|
|
|
break;
|
2019-03-29 03:58:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(fences[image_index] == nullptr);
|
|
|
|
fences[image_index] = &fence;
|
2020-01-20 22:29:54 +01:00
|
|
|
frame_index = (frame_index + 1) % static_cast<u32>(image_count);
|
2019-03-29 03:58:43 +01:00
|
|
|
return recreated;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VKSwapchain::HasFramebufferChanged(const Layout::FramebufferLayout& framebuffer) const {
|
|
|
|
// TODO(Rodrigo): Handle framebuffer pixel format changes
|
|
|
|
return framebuffer.width != current_width || framebuffer.height != current_height;
|
|
|
|
}
|
|
|
|
|
2020-03-27 05:33:21 +01:00
|
|
|
void VKSwapchain::CreateSwapchain(const VkSurfaceCapabilitiesKHR& capabilities, u32 width,
|
2019-12-07 02:41:42 +01:00
|
|
|
u32 height, bool srgb) {
|
2019-03-29 03:58:43 +01:00
|
|
|
const auto physical_device{device.GetPhysical()};
|
2020-03-27 05:33:21 +01:00
|
|
|
const auto formats{physical_device.GetSurfaceFormatsKHR(surface)};
|
|
|
|
const auto present_modes{physical_device.GetSurfacePresentModesKHR(surface)};
|
2019-03-29 03:58:43 +01:00
|
|
|
|
2020-03-27 05:33:21 +01:00
|
|
|
const VkSurfaceFormatKHR surface_format{ChooseSwapSurfaceFormat(formats, srgb)};
|
|
|
|
const VkPresentModeKHR present_mode{ChooseSwapPresentMode(present_modes)};
|
2019-03-29 03:58:43 +01:00
|
|
|
|
|
|
|
u32 requested_image_count{capabilities.minImageCount + 1};
|
|
|
|
if (capabilities.maxImageCount > 0 && requested_image_count > capabilities.maxImageCount) {
|
|
|
|
requested_image_count = capabilities.maxImageCount;
|
|
|
|
}
|
|
|
|
|
2020-07-17 01:27:02 +02:00
|
|
|
VkSwapchainCreateInfoKHR swapchain_ci{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
|
|
|
|
.pNext = nullptr,
|
|
|
|
.flags = 0,
|
|
|
|
.surface = surface,
|
|
|
|
.minImageCount = requested_image_count,
|
|
|
|
.imageFormat = surface_format.format,
|
|
|
|
.imageColorSpace = surface_format.colorSpace,
|
2020-07-25 09:46:20 +02:00
|
|
|
.imageExtent = {},
|
2020-07-17 01:27:02 +02:00
|
|
|
.imageArrayLayers = 1,
|
|
|
|
.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
|
|
|
.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
|
|
|
.queueFamilyIndexCount = 0,
|
|
|
|
.pQueueFamilyIndices = nullptr,
|
|
|
|
.preTransform = capabilities.currentTransform,
|
|
|
|
.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
|
|
|
|
.presentMode = present_mode,
|
|
|
|
.clipped = VK_FALSE,
|
|
|
|
.oldSwapchain = nullptr,
|
|
|
|
};
|
2019-03-29 03:58:43 +01:00
|
|
|
|
|
|
|
const u32 graphics_family{device.GetGraphicsFamily()};
|
|
|
|
const u32 present_family{device.GetPresentFamily()};
|
|
|
|
const std::array<u32, 2> queue_indices{graphics_family, present_family};
|
|
|
|
if (graphics_family != present_family) {
|
2020-03-27 05:33:21 +01:00
|
|
|
swapchain_ci.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
2019-03-29 03:58:43 +01:00
|
|
|
swapchain_ci.queueFamilyIndexCount = static_cast<u32>(queue_indices.size());
|
|
|
|
swapchain_ci.pQueueFamilyIndices = queue_indices.data();
|
|
|
|
}
|
|
|
|
|
2020-02-26 20:51:53 +01:00
|
|
|
// Request the size again to reduce the possibility of a TOCTOU race condition.
|
2020-03-27 05:33:21 +01:00
|
|
|
const auto updated_capabilities = physical_device.GetSurfaceCapabilitiesKHR(surface);
|
2020-02-26 20:51:53 +01:00
|
|
|
swapchain_ci.imageExtent = ChooseSwapExtent(updated_capabilities, width, height);
|
|
|
|
// Don't add code within this and the swapchain creation.
|
2020-03-27 05:33:21 +01:00
|
|
|
swapchain = device.GetLogical().CreateSwapchainKHR(swapchain_ci);
|
2019-03-29 03:58:43 +01:00
|
|
|
|
2020-02-26 20:51:53 +01:00
|
|
|
extent = swapchain_ci.imageExtent;
|
|
|
|
current_width = extent.width;
|
|
|
|
current_height = extent.height;
|
|
|
|
current_srgb = srgb;
|
|
|
|
|
2020-03-27 05:33:21 +01:00
|
|
|
images = swapchain.GetImages();
|
2019-03-29 03:58:43 +01:00
|
|
|
image_count = static_cast<u32>(images.size());
|
|
|
|
image_format = surface_format.format;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VKSwapchain::CreateSemaphores() {
|
|
|
|
present_semaphores.resize(image_count);
|
2020-03-27 05:33:21 +01:00
|
|
|
std::generate(present_semaphores.begin(), present_semaphores.end(),
|
|
|
|
[this] { return device.GetLogical().CreateSemaphore(); });
|
2019-03-29 03:58:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VKSwapchain::CreateImageViews() {
|
2020-07-17 01:27:02 +02:00
|
|
|
VkImageViewCreateInfo ci{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
|
|
|
|
.pNext = nullptr,
|
|
|
|
.flags = 0,
|
2020-07-25 09:46:20 +02:00
|
|
|
.image = {},
|
2020-07-17 01:27:02 +02:00
|
|
|
.viewType = VK_IMAGE_VIEW_TYPE_2D,
|
|
|
|
.format = image_format,
|
|
|
|
.components =
|
|
|
|
{
|
|
|
|
.r = VK_COMPONENT_SWIZZLE_IDENTITY,
|
|
|
|
.g = VK_COMPONENT_SWIZZLE_IDENTITY,
|
|
|
|
.b = VK_COMPONENT_SWIZZLE_IDENTITY,
|
|
|
|
.a = VK_COMPONENT_SWIZZLE_IDENTITY,
|
|
|
|
},
|
|
|
|
.subresourceRange =
|
|
|
|
{
|
|
|
|
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
.baseMipLevel = 0,
|
|
|
|
.levelCount = 1,
|
|
|
|
.baseArrayLayer = 0,
|
|
|
|
.layerCount = 1,
|
|
|
|
},
|
|
|
|
};
|
2019-03-29 03:58:43 +01:00
|
|
|
|
|
|
|
image_views.resize(image_count);
|
|
|
|
for (std::size_t i = 0; i < image_count; i++) {
|
2020-03-27 05:33:21 +01:00
|
|
|
ci.image = images[i];
|
|
|
|
image_views[i] = device.GetLogical().CreateImageView(ci);
|
2019-03-29 03:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VKSwapchain::Destroy() {
|
|
|
|
frame_index = 0;
|
|
|
|
present_semaphores.clear();
|
|
|
|
framebuffers.clear();
|
|
|
|
image_views.clear();
|
|
|
|
swapchain.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Vulkan
|