diff --git a/Common/GPU/Vulkan/VulkanContext.cpp b/Common/GPU/Vulkan/VulkanContext.cpp index 827737b195..f55b3a5a72 100644 --- a/Common/GPU/Vulkan/VulkanContext.cpp +++ b/Common/GPU/Vulkan/VulkanContext.cpp @@ -1072,37 +1072,29 @@ VkResult VulkanContext::InitSurface(WindowSystem winsys, void *data1, void *data return ReinitSurface(); } -VkResult VulkanContext::ReinitSurface() { - if (surface_ != VK_NULL_HANDLE) { - INFO_LOG(Log::G3D, "Destroying Vulkan surface (%d, %d)", swapChainExtent_.width, swapChainExtent_.height); - vkDestroySurfaceKHR(instance_, surface_, nullptr); - surface_ = VK_NULL_HANDLE; - } +VkResult VulkanContext::CreateSurface(WindowSystem winsys, void *data1, void *data2, VkSurfaceKHR *surface) { + VkResult retval = VK_SUCCESS; - INFO_LOG(Log::G3D, "Creating Vulkan surface for window (winsys=%s data1=%p data2=%p)", WindowSystemToString(winsys_), winsysData1_, winsysData2_); - - VkResult retval = VK_SUCCESS; - - switch (winsys_) { + switch (winsys) { #ifdef _WIN32 case WINDOWSYSTEM_WIN32: { VkWin32SurfaceCreateInfoKHR win32{ VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR }; win32.flags = 0; - win32.hinstance = (HINSTANCE)winsysData1_; - win32.hwnd = (HWND)winsysData2_; - retval = vkCreateWin32SurfaceKHR(instance_, &win32, nullptr, &surface_); + win32.hinstance = (HINSTANCE)data1; + win32.hwnd = (HWND)data2; + retval = vkCreateWin32SurfaceKHR(instance_, &win32, nullptr, surface); break; } #endif #if defined(__ANDROID__) case WINDOWSYSTEM_ANDROID: { - ANativeWindow *wnd = (ANativeWindow *)winsysData1_; + ANativeWindow *wnd = (ANativeWindow *)data1; VkAndroidSurfaceCreateInfoKHR android{ VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR }; android.flags = 0; android.window = wnd; - retval = vkCreateAndroidSurfaceKHR(instance_, &android, nullptr, &surface_); + retval = vkCreateAndroidSurfaceKHR(instance_, &android, nullptr, surface); break; } #endif @@ -1111,9 +1103,9 @@ VkResult VulkanContext::ReinitSurface() { { VkMetalSurfaceCreateInfoEXT metal{ VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT }; metal.flags = 0; - metal.pLayer = winsysData1_; - metal.pNext = winsysData2_; - retval = vkCreateMetalSurfaceEXT(instance_, &metal, nullptr, &surface_); + metal.pLayer = data1; + metal.pNext = data2; + retval = vkCreateMetalSurfaceEXT(instance_, &metal, nullptr, surface); break; } #endif @@ -1122,9 +1114,9 @@ VkResult VulkanContext::ReinitSurface() { { VkXlibSurfaceCreateInfoKHR xlib{ VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR }; xlib.flags = 0; - xlib.dpy = (Display *)winsysData1_; - xlib.window = (Window)winsysData2_; - retval = vkCreateXlibSurfaceKHR(instance_, &xlib, nullptr, &surface_); + xlib.dpy = (Display *)data1; + xlib.window = (Window)data2; + retval = vkCreateXlibSurfaceKHR(instance_, &xlib, nullptr, surface); break; } #endif @@ -1133,9 +1125,9 @@ VkResult VulkanContext::ReinitSurface() { { VkXCBSurfaceCreateInfoKHR xcb{ VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR }; xcb.flags = 0; - xcb.connection = (Connection *)winsysData1_; - xcb.window = (Window)(uintptr_t)winsysData2_; - retval = vkCreateXcbSurfaceKHR(instance_, &xcb, nullptr, &surface_); + xcb.connection = (Connection *)data1; + xcb.window = (Window)(uintptr_t)data2; + retval = vkCreateXcbSurfaceKHR(instance_, &xcb, nullptr, surface); break; } #endif @@ -1144,9 +1136,9 @@ VkResult VulkanContext::ReinitSurface() { { VkWaylandSurfaceCreateInfoKHR wayland{ VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR }; wayland.flags = 0; - wayland.display = (wl_display *)winsysData1_; - wayland.surface = (wl_surface *)winsysData2_; - retval = vkCreateWaylandSurfaceKHR(instance_, &wayland, nullptr, &surface_); + wayland.display = (wl_display *)data1; + wayland.surface = (wl_surface *)data2; + retval = vkCreateWaylandSurfaceKHR(instance_, &wayland, nullptr, surface); break; } #endif @@ -1336,15 +1328,28 @@ VkResult VulkanContext::ReinitSurface() { delete [] plane_props; #endif display.flags = 0; - retval = vkCreateDisplayPlaneSurfaceKHR(instance_, &display, nullptr, &surface_); + retval = vkCreateDisplayPlaneSurfaceKHR(instance_, &display, nullptr, surface); break; } #endif default: - _assert_msg_(false, "Vulkan support for chosen window system (%s) not implemented", WindowSystemToString(winsys_)); + _assert_msg_(false, "Vulkan support for chosen window system (%s) not implemented", WindowSystemToString(winsys)); return VK_ERROR_INITIALIZATION_FAILED; } + return retval; +} + +VkResult VulkanContext::ReinitSurface() { + if (surface_ != VK_NULL_HANDLE) { + INFO_LOG(Log::G3D, "Destroying Vulkan surface (%d, %d)", swapChainExtent_.width, swapChainExtent_.height); + vkDestroySurfaceKHR(instance_, surface_, nullptr); + surface_ = VK_NULL_HANDLE; + } + + INFO_LOG(Log::G3D, "Creating Vulkan surface for window (winsys=%s data1=%p data2=%p)", WindowSystemToString(winsys_), winsysData1_, winsysData2_); + + VkResult retval = CreateSurface(winsys_, winsysData1_, winsysData2_, &surface_); if (retval != VK_SUCCESS) { init_error_ = StringFromFormat("Failed to create a Vulkan surface for window system %s: %s", WindowSystemToString(winsys_), VulkanResultToString(retval)); diff --git a/Common/GPU/Vulkan/VulkanContext.h b/Common/GPU/Vulkan/VulkanContext.h index e00b878fdf..e9dd3cc647 100644 --- a/Common/GPU/Vulkan/VulkanContext.h +++ b/Common/GPU/Vulkan/VulkanContext.h @@ -236,6 +236,8 @@ public: // The extents will be automatically determined. VkResult InitSurface(WindowSystem winsys, void *data1, void *data2); VkResult ReinitSurface(); + // Creates a surface for a window, like InitSurface does for the main one. For additional swapchains. + VkResult CreateSurface(WindowSystem winsys, void *data1, void *data2, VkSurfaceKHR *surface); // If the present mode is not available, will fall back to the first available (which is almost always FIFO). bool InitSwapchain(VkPresentModeKHR desiredPresentMode); diff --git a/Common/GPU/Vulkan/VulkanFrameData.cpp b/Common/GPU/Vulkan/VulkanFrameData.cpp index f33d36e0bf..1bed118491 100644 --- a/Common/GPU/Vulkan/VulkanFrameData.cpp +++ b/Common/GPU/Vulkan/VulkanFrameData.cpp @@ -150,6 +150,51 @@ VkResult FrameData::QueuePresent(VulkanContext *vulkan, FrameDataShared &shared) return vkQueuePresentKHR(vulkan->GetGraphicsQueue(), &present); } +bool FrameData::AcquireExtraSwapchain(VulkanContext *vulkan, VKRSwapchain *swapchain) { + if (swapchain->acquired) { + return true; + } + if (swapchain->outOfDate || swapchain->swapchain == VK_NULL_HANDLE) { + return false; + } + // Doesn't wait, only the main swapchain paces the frame. + VkResult res = vkAcquireNextImageKHR(vulkan->GetDevice(), swapchain->swapchain, 0, swapchain->acquireSemaphores[index], (VkFence)VK_NULL_HANDLE, &swapchain->curImage); + switch (res) { + case VK_SUCCESS: + case VK_SUBOPTIMAL_KHR: + break; + case VK_ERROR_OUT_OF_DATE_KHR: + case VK_ERROR_SURFACE_LOST_KHR: + swapchain->outOfDate = true; + return false; + default: + // VK_NOT_READY or VK_TIMEOUT. Skip a frame. + return false; + } + swapchain->acquired = true; + acquiredSwapchains.push_back(swapchain); + return true; +} + +void FrameData::PresentExtraSwapchains(VulkanContext *vulkan) { + for (VKRSwapchain *swapchain : acquiredSwapchains) { + VkPresentInfoKHR present{ VK_STRUCTURE_TYPE_PRESENT_INFO_KHR }; + present.swapchainCount = 1; + present.pSwapchains = &swapchain->swapchain; + present.pImageIndices = &swapchain->curImage; + present.waitSemaphoreCount = 1; + present.pWaitSemaphores = &swapchain->images[swapchain->curImage].renderingCompleteSemaphore; + VkResult res = vkQueuePresentKHR(vulkan->GetGraphicsQueue(), &present); + if (res == VK_ERROR_OUT_OF_DATE_KHR || res == VK_ERROR_SURFACE_LOST_KHR) { + swapchain->outOfDate = true; + } else if (res != VK_SUCCESS && res != VK_SUBOPTIMAL_KHR) { + ERROR_LOG(Log::G3D, "vkQueuePresentKHR failed for an additional swapchain: %s", VulkanResultToString(res)); + } + swapchain->acquired = false; + } + acquiredSwapchains.clear(); +} + VkCommandBuffer FrameData::GetInitCmd(VulkanContext *vulkan) { if (!hasInitCommands) { VkCommandBufferBeginInfo begin = { @@ -226,19 +271,35 @@ void FrameData::Submit(VulkanContext *vulkan, FrameSubmitType type, FrameDataSha } VkSubmitInfo submit_info{ VK_STRUCTURE_TYPE_SUBMIT_INFO }; - VkPipelineStageFlags waitStage[1]{ VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT }; - if (type == FrameSubmitType::FinishFrame && !skipSwap) { - _dbg_assert_(hasAcquired); - submit_info.waitSemaphoreCount = 1; - submit_info.pWaitSemaphores = &acquireSemaphore; - submit_info.pWaitDstStageMask = waitStage; + // The main swapchain, then the additional ones. + VkSemaphore waitSemaphores[1 + MAX_EXTRA_SWAPCHAINS]; + VkPipelineStageFlags waitStages[1 + MAX_EXTRA_SWAPCHAINS]; + VkSemaphore signalSemaphores[1 + MAX_EXTRA_SWAPCHAINS]; + uint32_t numSemaphores = 0; + if (type == FrameSubmitType::FinishFrame) { + if (!skipSwap) { + _dbg_assert_(hasAcquired); + waitSemaphores[numSemaphores] = acquireSemaphore; + signalSemaphores[numSemaphores] = sharedData.swapchainImages_[curSwapchainImage].renderingCompleteSemaphore; + numSemaphores++; + } + _dbg_assert_(acquiredSwapchains.size() <= MAX_EXTRA_SWAPCHAINS); + for (const VKRSwapchain *swapchain : acquiredSwapchains) { + waitSemaphores[numSemaphores] = swapchain->acquireSemaphores[index]; + signalSemaphores[numSemaphores] = swapchain->images[swapchain->curImage].renderingCompleteSemaphore; + numSemaphores++; + } + for (uint32_t i = 0; i < numSemaphores; i++) { + waitStages[i] = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + } } + submit_info.waitSemaphoreCount = numSemaphores; + submit_info.pWaitSemaphores = waitSemaphores; + submit_info.pWaitDstStageMask = waitStages; submit_info.commandBufferCount = (uint32_t)numCmdBufs; submit_info.pCommandBuffers = cmdBufs; - if (type == FrameSubmitType::FinishFrame && !skipSwap) { - submit_info.signalSemaphoreCount = 1; - submit_info.pSignalSemaphores = &sharedData.swapchainImages_[curSwapchainImage].renderingCompleteSemaphore; - } + submit_info.signalSemaphoreCount = numSemaphores; + submit_info.pSignalSemaphores = signalSemaphores; vulkan->PrepareSubmit(submit_info); VkResult res; diff --git a/Common/GPU/Vulkan/VulkanFrameData.h b/Common/GPU/Vulkan/VulkanFrameData.h index ad281e4f79..6019e6dfd6 100644 --- a/Common/GPU/Vulkan/VulkanFrameData.h +++ b/Common/GPU/Vulkan/VulkanFrameData.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -73,6 +74,30 @@ struct SwapchainImageData { VkSemaphore renderingCompleteSemaphore = VK_NULL_HANDLE; }; +static constexpr int MAX_EXTRA_SWAPCHAINS = 4; + +// A swapchain other than the main one (see docs/multi-swapchain.md). The VulkanRenderManager owns it. +// It doesn't pace the frame: if it has no image ready, its pass is skipped that frame. +struct VKRSwapchain { + VkSurfaceKHR surface = VK_NULL_HANDLE; + VkSwapchainKHR swapchain = VK_NULL_HANDLE; + VkExtent2D extent{}; + int requestedWidth = 0; + int requestedHeight = 0; + std::vector images; + std::vector framebuffers; + VkImage depthImage = VK_NULL_HANDLE; + VmaAllocation depthAlloc = VK_NULL_HANDLE; + VkImageView depthView = VK_NULL_HANDLE; + VkSemaphore acquireSemaphores[VulkanContext::MAX_INFLIGHT_FRAMES]{}; + + // Render thread. The image acquired for the frame being run, if any. + uint32_t curImage = 0; + bool acquired = false; + // Set by the render thread, which can't recreate it. Checked at the start of a frame. + std::atomic outOfDate{false}; +}; + struct FrameDataShared { // For synchronous readbacks. VkFence readbackFence = VK_NULL_HANDLE; @@ -122,6 +147,8 @@ struct FrameData { // Swapchain. uint32_t curSwapchainImage = -1; + // Additional swapchains with an image acquired this frame, to wait for and present. + std::vector acquiredSwapchains; // Frames need unique IDs to wait for present on, let's keep them here. // Also used for indexing into the frame timing history buffer. @@ -140,6 +167,9 @@ struct FrameData { void AcquireNextImage(VulkanContext *vulkan); VkResult QueuePresent(VulkanContext *vulkan, FrameDataShared &shared); + // Additional swapchains. Acquiring doesn't wait, and returns false if there's no image ready. + bool AcquireExtraSwapchain(VulkanContext *vulkan, VKRSwapchain *swapchain); + void PresentExtraSwapchains(VulkanContext *vulkan); // Generally called from the main thread, unlike most of the rest. VkCommandBuffer GetInitCmd(VulkanContext *vulkan); diff --git a/Common/GPU/Vulkan/VulkanQueueRunner.cpp b/Common/GPU/Vulkan/VulkanQueueRunner.cpp index b898da5c08..c70cc79406 100644 --- a/Common/GPU/Vulkan/VulkanQueueRunner.cpp +++ b/Common/GPU/Vulkan/VulkanQueueRunner.cpp @@ -80,10 +80,8 @@ void VulkanQueueRunner::DestroyDeviceObjects() { renderPasses_.Clear(); } -bool VulkanQueueRunner::InitBackbufferFramebuffers(int width, int height, FrameDataShared &frameDataShared) { - VkResult res; - // We share the same depth buffer but have multiple color buffers, see the loop below. - VkImageView attachments[2] = { VK_NULL_HANDLE, depth_.view }; +VkFramebuffer VulkanQueueRunner::CreateBackbufferFramebuffer(VkImageView colorView, VkImageView depthView, int width, int height) { + VkImageView attachments[2] = { colorView, depthView }; VkFramebufferCreateInfo fb_info = { VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO }; fb_info.renderPass = GetCompatibleRenderPass()->Get(vulkan_, RenderPassType::BACKBUFFER, VK_SAMPLE_COUNT_1_BIT); @@ -93,13 +91,19 @@ bool VulkanQueueRunner::InitBackbufferFramebuffers(int width, int height, FrameD fb_info.height = height; fb_info.layers = 1; + VkFramebuffer framebuffer = VK_NULL_HANDLE; + VkResult res = vkCreateFramebuffer(vulkan_->GetDevice(), &fb_info, nullptr, &framebuffer); + _dbg_assert_(res == VK_SUCCESS); + return res == VK_SUCCESS ? framebuffer : VK_NULL_HANDLE; +} + +bool VulkanQueueRunner::InitBackbufferFramebuffers(int width, int height, FrameDataShared &frameDataShared) { + // We share the same depth buffer but have multiple color buffers. framebuffers_.resize(frameDataShared.swapchainImageCount_); for (uint32_t i = 0; i < frameDataShared.swapchainImageCount_; i++) { - attachments[0] = frameDataShared.swapchainImages_[i].view; - res = vkCreateFramebuffer(vulkan_->GetDevice(), &fb_info, nullptr, &framebuffers_[i]); - _dbg_assert_(res == VK_SUCCESS); - if (res != VK_SUCCESS) { + framebuffers_[i] = CreateBackbufferFramebuffer(frameDataShared.swapchainImages_[i].view, depth_.view, width, height); + if (framebuffers_[i] == VK_NULL_HANDLE) { framebuffers_.clear(); return false; } @@ -109,13 +113,18 @@ bool VulkanQueueRunner::InitBackbufferFramebuffers(int width, int height, FrameD } bool VulkanQueueRunner::InitDepthStencilBuffer(VkCommandBuffer cmd, VulkanBarrierBatch *barriers) { + depth_.format = vulkan_->GetDeviceInfo().preferredDepthStencilFormat; + return CreateDepthStencilImage(vulkan_->GetBackbufferWidth(), vulkan_->GetBackbufferHeight(), &depth_.image, &depth_.alloc, &depth_.view, barriers, "BackbufferDepth"); +} + +bool VulkanQueueRunner::CreateDepthStencilImage(int width, int height, VkImage *image, VmaAllocation *alloc, VkImageView *view, VulkanBarrierBatch *barriers, const char *tag) { const VkFormat depth_format = vulkan_->GetDeviceInfo().preferredDepthStencilFormat; int aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; VkImageCreateInfo image_info = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO }; image_info.imageType = VK_IMAGE_TYPE_2D; image_info.format = depth_format; - image_info.extent.width = vulkan_->GetBackbufferWidth(); - image_info.extent.height = vulkan_->GetBackbufferHeight(); + image_info.extent.width = width; + image_info.extent.height = height; image_info.extent.depth = 1; image_info.mipLevels = 1; image_info.arrayLayers = 1; @@ -126,21 +135,19 @@ bool VulkanQueueRunner::InitDepthStencilBuffer(VkCommandBuffer cmd, VulkanBarrie image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT; image_info.flags = 0; - depth_.format = depth_format; - VmaAllocationCreateInfo allocCreateInfo{}; VmaAllocationInfo allocInfo{}; allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; - VkResult res = vmaCreateImage(vulkan_->Allocator(), &image_info, &allocCreateInfo, &depth_.image, &depth_.alloc, &allocInfo); + VkResult res = vmaCreateImage(vulkan_->Allocator(), &image_info, &allocCreateInfo, image, alloc, &allocInfo); _dbg_assert_(res == VK_SUCCESS); if (res != VK_SUCCESS) return false; - vulkan_->SetDebugName(depth_.image, VK_OBJECT_TYPE_IMAGE, "BackbufferDepth"); + vulkan_->SetDebugName(*image, VK_OBJECT_TYPE_IMAGE, tag); - VkImageMemoryBarrier *barrier = barriers->Add(depth_.image, + VkImageMemoryBarrier *barrier = barriers->Add(*image, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, 0); barrier->subresourceRange.aspectMask = aspectMask; @@ -152,7 +159,7 @@ bool VulkanQueueRunner::InitDepthStencilBuffer(VkCommandBuffer cmd, VulkanBarrie _dbg_assert_(depth_format != VK_FORMAT_UNDEFINED); VkImageViewCreateInfo depth_view_info = { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO }; - depth_view_info.image = depth_.image; + depth_view_info.image = *image; depth_view_info.format = depth_format; depth_view_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY; depth_view_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; @@ -168,8 +175,8 @@ bool VulkanQueueRunner::InitDepthStencilBuffer(VkCommandBuffer cmd, VulkanBarrie VkDevice device = vulkan_->GetDevice(); - res = vkCreateImageView(device, &depth_view_info, NULL, &depth_.view); - vulkan_->SetDebugName(depth_.view, VK_OBJECT_TYPE_IMAGE_VIEW, "depth_stencil_backbuffer"); + res = vkCreateImageView(device, &depth_view_info, NULL, view); + vulkan_->SetDebugName(*view, VK_OBJECT_TYPE_IMAGE_VIEW, tag); _dbg_assert_(res == VK_SUCCESS); if (res != VK_SUCCESS) return false; @@ -312,6 +319,8 @@ void VulkanQueueRunner::RunSteps(std::vector &steps, int curFrame, Fr for (size_t i = 0; i < steps.size(); i++) { const VKRStep &step = *steps[i]; + // A pass to a swapchain closes its label and opens another in presentCmd, if there's an image to render to. + bool labelOpen = emitLabels; if (emitLabels) { VkDebugUtilsLabelEXT labelInfo{ VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT }; char temp[128]; @@ -331,14 +340,20 @@ void VulkanQueueRunner::RunSteps(std::vector &steps, int curFrame, Fr if (!step.render.framebuffer) { if (emitLabels) { vkCmdEndDebugUtilsLabelEXT(cmd); + labelOpen = false; + } + // The first pass to a swapchain moves us over to presentCmd. + if (!frameData.hasPresentCommands) { + frameData.Submit(vulkan_, FrameSubmitType::Pending, frameDataShared); } - frameData.Submit(vulkan_, FrameSubmitType::Pending, frameDataShared); // If the window is minimized and we don't have a swap chain, don't bother. - if (frameDataShared.swapchainImageCount_ > 0) { - // When stepping in the GE debugger, we can end up here multiple times in a "frame". - // So only acquire once. - if (!frameData.hasAcquired) { + if (step.render.swapchain || frameDataShared.swapchainImageCount_ > 0) { + if (step.render.swapchain) { + perform = frameData.AcquireExtraSwapchain(vulkan_, step.render.swapchain); + } else if (!frameData.hasAcquired) { + // When stepping in the GE debugger, we can end up here multiple times in a "frame". + // So only acquire once. frameData.AcquireNextImage(vulkan_); if (frameData.hasAcquired && frameData.curSwapchainImage != (uint32_t)-1) { SetBackbuffer(framebuffers_[frameData.curSwapchainImage], frameDataShared.swapchainImages_[frameData.curSwapchainImage].image); @@ -361,6 +376,7 @@ void VulkanQueueRunner::RunSteps(std::vector &steps, int curFrame, Fr VkDebugUtilsLabelEXT labelInfo{VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT}; labelInfo.pLabelName = "present"; vkCmdBeginDebugUtilsLabelEXT(cmd, &labelInfo); + labelOpen = true; } } else { perform = false; @@ -368,7 +384,7 @@ void VulkanQueueRunner::RunSteps(std::vector &steps, int curFrame, Fr } if (perform) { PerformRenderPass(step, cmd, curFrame, frameData.profile); - } else { + } else if (!step.render.swapchain) { frameData.skipSwap = true; } break; @@ -397,7 +413,7 @@ void VulkanQueueRunner::RunSteps(std::vector &steps, int curFrame, Fr profile->timestampDescriptions.push_back(StepToString(vulkan_, step)); } - if (emitLabels) { + if (labelOpen) { vkCmdEndDebugUtilsLabelEXT(cmd); } } @@ -869,8 +885,8 @@ std::string VulkanQueueRunner::StepToString(VulkanContext *vulkan, const VKRStep switch (step.stepType) { case VKRStepType::RENDER: { - int w = step.render.framebuffer ? step.render.framebuffer->width : vulkan->GetBackbufferWidth(); - int h = step.render.framebuffer ? step.render.framebuffer->height : vulkan->GetBackbufferHeight(); + int w = step.render.framebuffer ? step.render.framebuffer->width : (step.render.swapchain ? (int)step.render.swapchain->extent.width : vulkan->GetBackbufferWidth()); + int h = step.render.framebuffer ? step.render.framebuffer->height : (step.render.swapchain ? (int)step.render.swapchain->extent.height : vulkan->GetBackbufferHeight()); int actual_w = step.render.renderArea.extent.width; int actual_h = step.render.renderArea.extent.height; const char *renderCmd = GetRPTypeName(step.render.renderPassType); @@ -935,7 +951,8 @@ void VulkanQueueRunner::ApplyRenderPassMerge(std::vector &steps) { // we'll scan forward and slurp up any rendering that can be merged across. for (int i = 0; i < (int)steps.size(); i++) { decltype(steps[i]->render) &render_i = steps[i]->render; - if (steps[i]->stepType == VKRStepType::RENDER && counts[render_i.framebuffer] > 1) { + // Swapchain passes (no framebuffer) aren't merged, there can be several of them to different swapchains. + if (steps[i]->stepType == VKRStepType::RENDER && render_i.framebuffer && counts[render_i.framebuffer] > 1) { auto fb = render_i.framebuffer; TinySet touchedFramebuffers; // must be the same fast-size as the dependencies TinySet for annoying reasons. for (int j = i + 1; j < (int)steps.size(); j++) { @@ -1201,8 +1218,14 @@ void VulkanQueueRunner::PerformRenderPass(const VKRStep &step, VkCommandBuffer c int curWidth = step.render.framebuffer ? step.render.framebuffer->width : vulkan_->GetBackbufferWidth(); int curHeight = step.render.framebuffer ? step.render.framebuffer->height : vulkan_->GetBackbufferHeight(); + if (step.render.swapchain) { + curWidth = step.render.swapchain->extent.width; + curHeight = step.render.swapchain->extent.height; + } VKRFramebuffer *fb = step.render.framebuffer; + // Only the main swapchain is rotated to match the display. + const bool rotateToDisplay = !fb && !step.render.swapchain; VKRGraphicsPipeline *lastGraphicsPipeline = nullptr; VKRComputePipeline *lastComputePipeline = nullptr; @@ -1285,7 +1308,7 @@ void VulkanQueueRunner::PerformRenderPass(const VKRStep &step, VkCommandBuffer c } case VKRRenderCommand::VIEWPORT: - if (fb != nullptr) { + if (!rotateToDisplay) { vkCmdSetViewport(cmd, 0, 1, &c.viewport.vp); } else { const VkViewport &vp = c.viewport.vp; @@ -1304,7 +1327,7 @@ void VulkanQueueRunner::PerformRenderPass(const VKRStep &step, VkCommandBuffer c case VKRRenderCommand::SCISSOR: { - if (fb != nullptr) { + if (!rotateToDisplay) { vkCmdSetScissor(cmd, 0, 1, &c.scissor.scissor); } else { // Rendering to backbuffer. Might need to rotate. @@ -1517,11 +1540,16 @@ VKRRenderPass *VulkanQueueRunner::PerformBindFramebufferAsRenderTarget(const VKR VKRRenderPassStoreAction::STORE, VKRRenderPassStoreAction::DONT_CARE, VKRRenderPassStoreAction::DONT_CARE, }; renderPass = GetRenderPass(key); - framebuf = backbuffer_; - - // Raw, rotated backbuffer size. - w = vulkan_->GetBackbufferWidth(); - h = vulkan_->GetBackbufferHeight(); + if (VKRSwapchain *swapchain = step.render.swapchain) { + framebuf = swapchain->framebuffers[swapchain->curImage]; + w = swapchain->extent.width; + h = swapchain->extent.height; + } else { + framebuf = backbuffer_; + // Raw, rotated backbuffer size. + w = vulkan_->GetBackbufferWidth(); + h = vulkan_->GetBackbufferHeight(); + } Uint8x4ToFloat4(clearVal[0].color.float32, step.render.clearColor); numClearVals = hasDepth ? 2 : 1; // We might do depth-less backbuffer in the future, though doubtful of the value. @@ -1535,7 +1563,7 @@ VKRRenderPass *VulkanQueueRunner::PerformBindFramebufferAsRenderTarget(const VKR rp_begin.framebuffer = framebuf; VkRect2D rc = step.render.renderArea; - if (!step.render.framebuffer) { + if (!step.render.framebuffer && !step.render.swapchain) { // Rendering to backbuffer, must rotate, just like scissors. DisplayRect rotated_rc{ rc.offset.x, rc.offset.y, (int)rc.extent.width, (int)rc.extent.height }; RotateRectToDisplay(rotated_rc, vulkan_->GetBackbufferWidth(), vulkan_->GetBackbufferHeight()); diff --git a/Common/GPU/Vulkan/VulkanQueueRunner.h b/Common/GPU/Vulkan/VulkanQueueRunner.h index be808ca91c..21309001ff 100644 --- a/Common/GPU/Vulkan/VulkanQueueRunner.h +++ b/Common/GPU/Vulkan/VulkanQueueRunner.h @@ -150,6 +150,8 @@ struct VKRStep { union { struct { VKRFramebuffer *framebuffer; + // With no framebuffer: an additional swapchain, or nullptr for the main one. + VKRSwapchain *swapchain; VKRRenderPassLoadAction colorLoad; VKRRenderPassLoadAction depthLoad; VKRRenderPassLoadAction stencilLoad; @@ -271,6 +273,9 @@ public: bool InitBackbufferFramebuffers(int width, int height, FrameDataShared &frameDataShared); bool InitDepthStencilBuffer(VkCommandBuffer cmd, VulkanBarrierBatch *barriers); // Used for non-buffered rendering. + // For additional swapchains too. The depth image is transitioned by the barriers. + VkFramebuffer CreateBackbufferFramebuffer(VkImageView colorView, VkImageView depthView, int width, int height); + bool CreateDepthStencilImage(int width, int height, VkImage *image, VmaAllocation *alloc, VkImageView *view, VulkanBarrierBatch *barriers, const char *tag); private: VKRRenderPass *PerformBindFramebufferAsRenderTarget(const VKRStep &pass, VkCommandBuffer cmd); diff --git a/Common/GPU/Vulkan/VulkanRenderManager.cpp b/Common/GPU/Vulkan/VulkanRenderManager.cpp index a960239ab0..6486a121ad 100644 --- a/Common/GPU/Vulkan/VulkanRenderManager.cpp +++ b/Common/GPU/Vulkan/VulkanRenderManager.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -359,6 +360,40 @@ bool VulkanRenderManager::CreateBackbuffers() { return true; } +// A view of a swapchain image, in the main swapchain's format, and a semaphore to present it with. +static void InitSwapchainImageData(VulkanContext *vulkan, VkImage image, SwapchainImageData *data) { + static const VkSemaphoreCreateInfo semaphoreCreateInfo = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO }; + data->image = image; + VkResult res = vkCreateSemaphore(vulkan->GetDevice(), &semaphoreCreateInfo, nullptr, &data->renderingCompleteSemaphore); + _dbg_assert_(res == VK_SUCCESS); + + VkImageViewCreateInfo color_image_view = { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO }; + color_image_view.format = vulkan->GetSwapchainFormat(); + + _dbg_assert_(color_image_view.format != VK_FORMAT_UNDEFINED); + + color_image_view.components.r = VK_COMPONENT_SWIZZLE_IDENTITY; + color_image_view.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; + color_image_view.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; + color_image_view.components.a = VK_COMPONENT_SWIZZLE_IDENTITY; + color_image_view.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + color_image_view.subresourceRange.baseMipLevel = 0; + color_image_view.subresourceRange.levelCount = 1; + color_image_view.subresourceRange.baseArrayLayer = 0; + color_image_view.subresourceRange.layerCount = 1; // TODO: Investigate hw-assisted stereo. + color_image_view.viewType = VK_IMAGE_VIEW_TYPE_2D; + color_image_view.flags = 0; + color_image_view.image = image; + + // We leave the images as UNDEFINED, there's no need to pre-transition them as + // the backbuffer renderpass starts out with them being auto-transitioned from UNDEFINED anyway. + // Also, turns out it's illegal to transition un-acquired images, thanks Hans-Kristian. See #11417. + + res = vkCreateImageView(vulkan->GetDevice(), &color_image_view, nullptr, &data->view); + vulkan->SetDebugName(data->view, VK_OBJECT_TYPE_IMAGE_VIEW, "swapchain_view"); + _dbg_assert_(res == VK_SUCCESS); +} + bool VulkanRenderManager::CreateSwapchainViewsAndDepth(VkCommandBuffer cmdInit, VulkanBarrierBatch *barriers, FrameDataShared &frameDataShared) { std::vector swapchainImages; if (VulkanPresentation *presentation = vulkan_->GetPresentation()) { @@ -379,39 +414,10 @@ bool VulkanRenderManager::CreateSwapchainViewsAndDepth(VkCommandBuffer cmdInit, } } - static const VkSemaphoreCreateInfo semaphoreCreateInfo = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO }; for (uint32_t i = 0; i < frameDataShared.swapchainImageCount_; i++) { SwapchainImageData sc_buffer{}; - sc_buffer.image = swapchainImages[i]; - VkResult res = vkCreateSemaphore(vulkan_->GetDevice(), &semaphoreCreateInfo, nullptr, &sc_buffer.renderingCompleteSemaphore); - _dbg_assert_(res == VK_SUCCESS); - - VkImageViewCreateInfo color_image_view = { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO }; - color_image_view.format = vulkan_->GetSwapchainFormat(); - - _dbg_assert_(color_image_view.format != VK_FORMAT_UNDEFINED); - - color_image_view.components.r = VK_COMPONENT_SWIZZLE_IDENTITY; - color_image_view.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; - color_image_view.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; - color_image_view.components.a = VK_COMPONENT_SWIZZLE_IDENTITY; - color_image_view.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; - color_image_view.subresourceRange.baseMipLevel = 0; - color_image_view.subresourceRange.levelCount = 1; - color_image_view.subresourceRange.baseArrayLayer = 0; - color_image_view.subresourceRange.layerCount = 1; // TODO: Investigate hw-assisted stereo. - color_image_view.viewType = VK_IMAGE_VIEW_TYPE_2D; - color_image_view.flags = 0; - color_image_view.image = sc_buffer.image; - - // We leave the images as UNDEFINED, there's no need to pre-transition them as - // the backbuffer renderpass starts out with them being auto-transitioned from UNDEFINED anyway. - // Also, turns out it's illegal to transition un-acquired images, thanks Hans-Kristian. See #11417. - - res = vkCreateImageView(vulkan_->GetDevice(), &color_image_view, nullptr, &sc_buffer.view); - vulkan_->SetDebugName(sc_buffer.view, VK_OBJECT_TYPE_IMAGE_VIEW, "swapchain_view"); + InitSwapchainImageData(vulkan_, swapchainImages[i], &sc_buffer); frameDataShared.swapchainImages_.push_back(sc_buffer); - _dbg_assert_(res == VK_SUCCESS); } // Must be before InitBackbufferRenderPass. @@ -536,6 +542,211 @@ void VulkanRenderManager::DestroyBackbuffers() { queueRunner_.DestroyBackBuffers(); } +VKRSwapchain *VulkanRenderManager::CreateExtraSwapchain(WindowSystem winsys, void *data1, void *data2, int width, int height, std::string *error) { + if (vulkan_->GetPresentation() || !vulkan_->HasRealSwapchain()) { + *error = "No main swapchain to go with"; + return nullptr; + } + if (extraSwapchains_.size() >= MAX_EXTRA_SWAPCHAINS) { + *error = "Too many swapchains"; + return nullptr; + } + VKRSwapchain *swapchain = new VKRSwapchain(); + VkResult res = vulkan_->CreateSurface(winsys, data1, data2, &swapchain->surface); + if (res != VK_SUCCESS) { + *error = StringFromFormat("Failed to create a surface: %s", VulkanResultToString(res)); + delete swapchain; + return nullptr; + } + // Present from the queue we render on. + VkBool32 supported = VK_FALSE; + vkGetPhysicalDeviceSurfaceSupportKHR(vulkan_->GetCurrentPhysicalDevice(), vulkan_->GetGraphicsQueueFamilyIndex(), swapchain->surface, &supported); + if (!supported) { + *error = "The graphics queue can't present to the surface"; + vkDestroySurfaceKHR(vulkan_->GetInstance(), swapchain->surface, nullptr); + delete swapchain; + return nullptr; + } + static const VkSemaphoreCreateInfo semaphoreCreateInfo = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO }; + for (VkSemaphore &semaphore : swapchain->acquireSemaphores) { + vkCreateSemaphore(vulkan_->GetDevice(), &semaphoreCreateInfo, nullptr, &semaphore); + } + swapchain->requestedWidth = width; + swapchain->requestedHeight = height; + // Nothing refers to it yet, so the render thread can keep going. + if (!InitExtraSwapchain(swapchain, error)) { + DestroyExtraSwapchainResources(swapchain); + for (VkSemaphore semaphore : swapchain->acquireSemaphores) { + vkDestroySemaphore(vulkan_->GetDevice(), semaphore, nullptr); + } + vkDestroySurfaceKHR(vulkan_->GetInstance(), swapchain->surface, nullptr); + delete swapchain; + return nullptr; + } + extraSwapchains_.push_back(swapchain); + return swapchain; +} + +bool VulkanRenderManager::InitExtraSwapchain(VKRSwapchain *swapchain, std::string *error) { + VkPhysicalDevice physicalDevice = vulkan_->GetCurrentPhysicalDevice(); + VkSurfaceCapabilitiesKHR caps{}; + VkResult res = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, swapchain->surface, &caps); + if (res != VK_SUCCESS) { + *error = StringFromFormat("vkGetPhysicalDeviceSurfaceCapabilitiesKHR failed: %s", VulkanResultToString(res)); + return false; + } + + // The same format as the main swapchain, so the backbuffer render pass and its pipelines work for both. + const VkFormat format = vulkan_->GetSwapchainFormat(); + uint32_t formatCount = 0; + vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, swapchain->surface, &formatCount, nullptr); + std::vector formats(formatCount); + vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, swapchain->surface, &formatCount, formats.data()); + const VkSurfaceFormatKHR *surfaceFormat = nullptr; + for (const VkSurfaceFormatKHR &f : formats) { + if (f.format == format) { + surfaceFormat = &f; + break; + } + } + if (!surfaceFormat) { + *error = StringFromFormat("The surface doesn't support the main swapchain's format (%d)", (int)format); + return false; + } + + // 0xFFFFFFFF means the swapchain decides. MoltenVK can report 0 for a layer that's not been sized yet. + VkExtent2D extent = caps.currentExtent; + if (extent.width == 0xFFFFFFFF || extent.width == 0 || extent.height == 0) { + extent.width = std::clamp((uint32_t)swapchain->requestedWidth, caps.minImageExtent.width, caps.maxImageExtent.width); + extent.height = std::clamp((uint32_t)swapchain->requestedHeight, caps.minImageExtent.height, caps.maxImageExtent.height); + } + if (extent.width == 0 || extent.height == 0) { + *error = "The surface has no size"; + return false; + } + + uint32_t imageCount = caps.minImageCount + 1; + if (caps.maxImageCount > 0) { + imageCount = std::min(imageCount, caps.maxImageCount); + } + + VkSwapchainCreateInfoKHR info{ VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR }; + info.surface = swapchain->surface; + info.minImageCount = imageCount; + info.imageFormat = surfaceFormat->format; + info.imageColorSpace = surfaceFormat->colorSpace; + info.imageExtent = extent; + info.imageArrayLayers = 1; + info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; + info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; + info.preTransform = (caps.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) ? VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR : caps.currentTransform; + info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; + if (!(caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR)) { + // Take the lowest bit that's supported. + info.compositeAlpha = (VkCompositeAlphaFlagBitsKHR)(caps.supportedCompositeAlpha & (~caps.supportedCompositeAlpha + 1)); + } + info.presentMode = VK_PRESENT_MODE_FIFO_KHR; + info.clipped = VK_TRUE; + res = vkCreateSwapchainKHR(vulkan_->GetDevice(), &info, nullptr, &swapchain->swapchain); + if (res != VK_SUCCESS) { + *error = StringFromFormat("vkCreateSwapchainKHR failed: %s", VulkanResultToString(res)); + swapchain->swapchain = VK_NULL_HANDLE; + return false; + } + swapchain->extent = extent; + + uint32_t count = 0; + vkGetSwapchainImagesKHR(vulkan_->GetDevice(), swapchain->swapchain, &count, nullptr); + std::vector images(count); + vkGetSwapchainImagesKHR(vulkan_->GetDevice(), swapchain->swapchain, &count, images.data()); + + if (!queueRunner_.CreateDepthStencilImage(extent.width, extent.height, &swapchain->depthImage, &swapchain->depthAlloc, &swapchain->depthView, &postInitBarrier_, "ExtraSwapchainDepth")) { + *error = "Failed to create the depth buffer"; + return false; + } + for (VkImage image : images) { + SwapchainImageData data{}; + InitSwapchainImageData(vulkan_, image, &data); + swapchain->images.push_back(data); + VkFramebuffer framebuffer = queueRunner_.CreateBackbufferFramebuffer(data.view, swapchain->depthView, extent.width, extent.height); + if (framebuffer == VK_NULL_HANDLE) { + *error = "Failed to create a framebuffer"; + return false; + } + swapchain->framebuffers.push_back(framebuffer); + } + swapchain->outOfDate = false; + INFO_LOG(Log::G3D, "Created an additional swapchain: %dx%d, %d images", extent.width, extent.height, (int)count); + return true; +} + +// Only once the GPU and the render thread are done with it, or it was never used. +void VulkanRenderManager::DestroyExtraSwapchainResources(VKRSwapchain *swapchain) { + VkDevice device = vulkan_->GetDevice(); + for (VkFramebuffer framebuffer : swapchain->framebuffers) { + vulkan_->Delete().QueueDeleteFramebuffer(framebuffer); + } + swapchain->framebuffers.clear(); + for (SwapchainImageData &image : swapchain->images) { + vulkan_->Delete().QueueDeleteImageView(image.view); + vkDestroySemaphore(device, image.renderingCompleteSemaphore, nullptr); + } + swapchain->images.clear(); + if (swapchain->depthView) { + vulkan_->Delete().QueueDeleteImageView(swapchain->depthView); + swapchain->depthView = VK_NULL_HANDLE; + } + if (swapchain->depthImage) { + vulkan_->Delete().QueueDeleteImageAllocation(swapchain->depthImage, swapchain->depthAlloc); + swapchain->depthImage = VK_NULL_HANDLE; + swapchain->depthAlloc = VK_NULL_HANDLE; + } + if (swapchain->swapchain) { + vkDestroySwapchainKHR(device, swapchain->swapchain, nullptr); + swapchain->swapchain = VK_NULL_HANDLE; + } + swapchain->acquired = false; +} + +void VulkanRenderManager::RecreateExtraSwapchain(VKRSwapchain *swapchain) { + const bool threadsRunning = compileThread_.joinable(); + StopThreads(); + vulkan_->WaitUntilQueueIdle(); + DestroyExtraSwapchainResources(swapchain); + std::string error; + if (!InitExtraSwapchain(swapchain, &error)) { + // It stays without images, and its passes are skipped, until it's resized or destroyed. + ERROR_LOG(Log::G3D, "Failed to recreate an additional swapchain: %s", error.c_str()); + DestroyExtraSwapchainResources(swapchain); + swapchain->outOfDate = false; + } + if (threadsRunning) { + StartThreads(); + } +} + +void VulkanRenderManager::ResizeExtraSwapchain(VKRSwapchain *swapchain, int width, int height) { + swapchain->requestedWidth = width; + swapchain->requestedHeight = height; + RecreateExtraSwapchain(swapchain); +} + +void VulkanRenderManager::DestroyExtraSwapchain(VKRSwapchain *swapchain) { + const bool threadsRunning = compileThread_.joinable(); + StopThreads(); + vulkan_->WaitUntilQueueIdle(); + DestroyExtraSwapchainResources(swapchain); + for (VkSemaphore semaphore : swapchain->acquireSemaphores) { + vkDestroySemaphore(vulkan_->GetDevice(), semaphore, nullptr); + } + vkDestroySurfaceKHR(vulkan_->GetInstance(), swapchain->surface, nullptr); + extraSwapchains_.erase(std::remove(extraSwapchains_.begin(), extraSwapchains_.end(), swapchain), extraSwapchains_.end()); + delete swapchain; + if (threadsRunning) { + StartThreads(); + } +} + // Hm, I'm finding the occasional report of these asserts. void VulkanRenderManager::CheckNothingPending() { _assert_(pipelinesToCheck_.empty()); @@ -737,6 +948,12 @@ void VulkanRenderManager::PollPresentTiming() { void VulkanRenderManager::BeginFrame(bool enableProfiling, bool enableLogProfiler) { double frameBeginTime = time_now_d(); VLOG("BeginFrame"); + + for (VKRSwapchain *swapchain : extraSwapchains_) { + if (swapchain->outOfDate) { + RecreateExtraSwapchain(swapchain); + } + } VkDevice device = vulkan_->GetDevice(); int curFrame = vulkan_->GetCurFrame(); @@ -1056,7 +1273,7 @@ void VulkanRenderManager::EndCurRenderStep() { curPipelineFlags_ = (PipelineFlags)0; } -void VulkanRenderManager::BindFramebufferAsRenderTarget(VKRFramebuffer *fb, VKRRenderPassLoadAction colorLoad, VKRRenderPassLoadAction depthLoad, VKRRenderPassLoadAction stencilLoad, uint32_t clearColor, float clearDepth, uint8_t clearStencil, const char *tag) { +void VulkanRenderManager::BindFramebufferAsRenderTarget(VKRFramebuffer *fb, VKRRenderPassLoadAction colorLoad, VKRRenderPassLoadAction depthLoad, VKRRenderPassLoadAction stencilLoad, uint32_t clearColor, float clearDepth, uint8_t clearStencil, const char *tag, VKRSwapchain *swapchain) { _dbg_assert_(insideFrame_); if (!fb) { @@ -1067,7 +1284,7 @@ void VulkanRenderManager::BindFramebufferAsRenderTarget(VKRFramebuffer *fb, VKRR } // Eliminate dupes (bind of the framebuffer we already are rendering to), instantly convert to a clear if possible. - if (!steps_.empty() && steps_.back()->stepType == VKRStepType::RENDER && steps_.back()->render.framebuffer == fb) { + if (!steps_.empty() && steps_.back()->stepType == VKRStepType::RENDER && steps_.back()->render.framebuffer == fb && steps_.back()->render.swapchain == swapchain) { u32 clearMask = 0; if (colorLoad == VKRRenderPassLoadAction::CLEAR) { clearMask |= VK_IMAGE_ASPECT_COLOR_BIT; @@ -1111,7 +1328,7 @@ void VulkanRenderManager::BindFramebufferAsRenderTarget(VKRFramebuffer *fb, VKRR } #ifdef _DEBUG - SanityCheckPassesOnAdd(); + SanityCheckPassesOnAdd(fb, swapchain); #endif // More redundant bind elimination. @@ -1154,6 +1371,7 @@ void VulkanRenderManager::BindFramebufferAsRenderTarget(VKRFramebuffer *fb, VKRR VKRStep *step = new VKRStep{ VKRStepType::RENDER }; step->render.framebuffer = fb; + step->render.swapchain = fb ? nullptr : swapchain; step->render.colorLoad = colorLoad; step->render.depthLoad = depthLoad; step->render.stencilLoad = stencilLoad; @@ -1186,6 +1404,11 @@ void VulkanRenderManager::BindFramebufferAsRenderTarget(VKRFramebuffer *fb, VKRR curHeightRaw_ = fb->height; curWidth_ = fb->width; curHeight_ = fb->height; + } else if (swapchain) { + curWidthRaw_ = swapchain->extent.width; + curHeightRaw_ = swapchain->extent.height; + curWidth_ = curWidthRaw_; + curHeight_ = curHeightRaw_; } else { curWidthRaw_ = vulkan_->GetBackbufferWidth(); curHeightRaw_ = vulkan_->GetBackbufferHeight(); @@ -1412,7 +1635,7 @@ void VulkanRenderManager::Clear(uint32_t clearColor, float clearZ, int clearSten void VulkanRenderManager::CopyFramebuffer(VKRFramebuffer *src, VkRect2D srcRect, VKRFramebuffer *dst, VkOffset2D dstPos, VkImageAspectFlags aspectMask, const char *tag) { #ifdef _DEBUG - SanityCheckPassesOnAdd(); + SanityCheckPassesOnAdd(dst, nullptr); #endif // _dbg_assert_msg_(src != dst, "Can't copy within the same buffer"); if (src == dst) { @@ -1503,7 +1726,7 @@ void VulkanRenderManager::CopyFramebuffer(VKRFramebuffer *src, VkRect2D srcRect, void VulkanRenderManager::BlitFramebuffer(VKRFramebuffer *src, VkRect2D srcRect, VKRFramebuffer *dst, VkRect2D dstRect, VkImageAspectFlags aspectMask, VkFilter filter, const char *tag) { #ifdef _DEBUG - SanityCheckPassesOnAdd(); + SanityCheckPassesOnAdd(dst, nullptr); #endif _dbg_assert_msg_(srcRect.offset.x >= 0, "srcrect offset x (%d) < 0", srcRect.offset.x); @@ -1710,6 +1933,8 @@ void VulkanRenderManager::Run(VKRRenderThreadTask &task) { } frameData.skipSwap = false; } + // Even when the main swapchain skipped this frame. + frameData.PresentExtraSwapchains(vulkan_); if (VulkanPresentation *presentation = vulkan_->GetPresentation()) { presentation->EndPresent(); } @@ -2095,12 +2320,13 @@ void VKRPipelineLayout::FlushDescSets(VulkanContext *vulkan, int frame, QueuePro profile->descriptorsDeduped += dedupCount; } -void VulkanRenderManager::SanityCheckPassesOnAdd() { +void VulkanRenderManager::SanityCheckPassesOnAdd(VKRFramebuffer *fb, VKRSwapchain *swapchain) { #if _DEBUG - // Check that we don't have any previous passes that write to the backbuffer, that must ALWAYS be the last one. + // Passes to swapchains must ALWAYS come last, one per swapchain. for (int i = 0; i < (int)steps_.size(); i++) { - if (steps_[i]->stepType == VKRStepType::RENDER) { - _dbg_assert_msg_(steps_[i]->render.framebuffer != nullptr, "Adding second backbuffer pass? Not good!"); + if (steps_[i]->stepType == VKRStepType::RENDER && !steps_[i]->render.framebuffer) { + _dbg_assert_msg_(!fb, "Adding a framebuffer pass after a swapchain pass? Not good!"); + _dbg_assert_msg_(steps_[i]->render.swapchain != swapchain, "Adding second pass to a swapchain? Not good!"); } } #endif diff --git a/Common/GPU/Vulkan/VulkanRenderManager.h b/Common/GPU/Vulkan/VulkanRenderManager.h index dbb4672ce2..aa5b9a5ffd 100644 --- a/Common/GPU/Vulkan/VulkanRenderManager.h +++ b/Common/GPU/Vulkan/VulkanRenderManager.h @@ -263,7 +263,8 @@ public: // // It can be useful to use GetCurrentStepId() to figure out when you need to send all this state again, if you're // not keeping track of your calls to this function on your own. - void BindFramebufferAsRenderTarget(VKRFramebuffer *fb, VKRRenderPassLoadAction color, VKRRenderPassLoadAction depth, VKRRenderPassLoadAction stencil, uint32_t clearColor, float clearDepth, uint8_t clearStencil, const char *tag); + // With no framebuffer, renders to swapchain, or the main one if that's null too. + void BindFramebufferAsRenderTarget(VKRFramebuffer *fb, VKRRenderPassLoadAction color, VKRRenderPassLoadAction depth, VKRRenderPassLoadAction stencil, uint32_t clearColor, float clearDepth, uint8_t clearStencil, const char *tag, VKRSwapchain *swapchain = nullptr); // Returns an ImageView corresponding to a framebuffer. Is called BindFramebufferAsTexture to maintain a similar interface // as the other backends, even though there's no actual binding happening here. @@ -513,6 +514,12 @@ public: bool CreateBackbuffers(); void DestroyBackbuffers(); + // Swapchains other than the main one, for other windows or displays. Use between frames, on the emu thread. + // They use the main swapchain's format, and don't pace the frame. Resizing and destroying wait for the GPU. + VKRSwapchain *CreateExtraSwapchain(WindowSystem winsys, void *data1, void *data2, int width, int height, std::string *error); + void DestroyExtraSwapchain(VKRSwapchain *swapchain); + void ResizeExtraSwapchain(VKRSwapchain *swapchain, int width, int height); + bool HasBackbuffers() { return queueRunner_.HasBackbuffers(); } @@ -569,7 +576,10 @@ private: void ResetDescriptorLists(int frame); void FlushDescriptors(int frame); - void SanityCheckPassesOnAdd(); + void SanityCheckPassesOnAdd(VKRFramebuffer *fb, VKRSwapchain *swapchain); + bool InitExtraSwapchain(VKRSwapchain *swapchain, std::string *error); + void DestroyExtraSwapchainResources(VKRSwapchain *swapchain); + void RecreateExtraSwapchain(VKRSwapchain *swapchain); bool CreateSwapchainViewsAndDepth(VkCommandBuffer cmdInit, VulkanBarrierBatch *barriers, FrameDataShared &frameDataShared); bool RecreatePresentationIfNeeded(); @@ -606,6 +616,7 @@ private: BoundingRect curRenderArea_; std::vector steps_; + std::vector extraSwapchains_; // Execution time state VulkanContext *vulkan_; diff --git a/Common/GPU/Vulkan/thin3d_vulkan.cpp b/Common/GPU/Vulkan/thin3d_vulkan.cpp index 7af0d6814f..358d40d7b5 100644 --- a/Common/GPU/Vulkan/thin3d_vulkan.cpp +++ b/Common/GPU/Vulkan/thin3d_vulkan.cpp @@ -457,6 +457,10 @@ public: bool CopyFramebufferToMemory(Framebuffer *src, Aspect aspects, int x, int y, int w, int h, Draw::DataFormat format, void *pixels, int pixelStride, ReadbackMode mode, const char *tag) override; DataFormat PreferredFramebufferReadbackFormat(Framebuffer *src) override; + Swapchain *CreateSwapchain(const SwapchainDesc &desc, std::string *error) override; + void DestroySwapchain(Swapchain *swapchain) override; + void ResizeSwapchain(Swapchain *swapchain, int width, int height) override; + // These functions should be self explanatory. void BindFramebufferAsRenderTarget(Framebuffer *fbo, const RenderPassInfo &rp, const char *tag) override; void BindFramebufferAsTexture(Framebuffer *fbo, int binding, Aspect channelBit, int layer) override; @@ -1000,6 +1004,8 @@ VKContext::VKContext(VulkanContext *vulkan, bool useRenderThread) caps_.presentMaxInterval = 1; caps_.presentInstantModeChange = false; // TODO: Fix this with some work in VulkanContext caps_.presentModesSupported = (PresentMode)0; + // Additional swapchains go with a real main one. + caps_.multipleSwapchains = vulkan->GetPresentation() == nullptr; if (vulkan->GetPresentation()) { // No real present modes exist in this model (the host, e.g. libretro, owns real presentation @@ -1830,6 +1836,24 @@ uint32_t VKContext::GetDataFormatSupport(DataFormat fmt) const { // It also has a reference to the command buffer that it was last rendered to with. // If it needs to be transitioned, and the frame number matches, use it, otherwise // use this frame's init command buffer. +class VKSwapchain : public Swapchain { +public: + VKSwapchain(VKRSwapchain *swapchain, const char *tag) : swapchain(swapchain), tag_(tag ? tag : "") { + UpdateSize(); + } + // The render manager recreates it when it's out of date, possibly at another size. + void UpdateSize() { + width_ = swapchain->extent.width; + height_ = swapchain->extent.height; + } + const char *Tag() const override { return tag_.c_str(); } + + VKRSwapchain *swapchain; + +private: + std::string tag_; +}; + class VKFramebuffer : public Framebuffer { public: VKFramebuffer(VKRFramebuffer *fb, int multiSampleLevel) : buf_(fb) { @@ -1895,6 +1919,10 @@ bool VKContext::BlitFramebuffer(Framebuffer *srcfb, int srcX1, int srcY1, int sr bool VKContext::CopyFramebufferToMemory(Framebuffer *srcfb, Aspect aspects, int x, int y, int w, int h, Draw::DataFormat format, void *pixels, int pixelStride, ReadbackMode mode, const char *tag) { srcfb = PrimaryToNull(srcfb); + if (srcfb && srcfb->IsSwapchain()) { + // Not supported for additional swapchains. + return false; + } VKFramebuffer *src = (VKFramebuffer *)srcfb; if (!src && vulkan_->GetSwapchain() == VK_NULL_HANDLE) { // No backbuffer to read, as in headless. @@ -1925,16 +1953,26 @@ void VKContext::BindFramebufferAsRenderTarget(Framebuffer *fbo, const RenderPass fbo = PrimaryToNull(fbo); _dbg_assert_(fbo != nullptr || equals(tag, "BackBuffer")) - VKFramebuffer *fb = (VKFramebuffer *)fbo; VKRRenderPassLoadAction color = (VKRRenderPassLoadAction)rp.color; VKRRenderPassLoadAction depth = (VKRRenderPassLoadAction)rp.depth; VKRRenderPassLoadAction stencil = (VKRRenderPassLoadAction)rp.stencil; + if (fbo && fbo->IsSwapchain()) { + VKSwapchain *swapchain = (VKSwapchain *)fbo; + swapchain->UpdateSize(); + renderManager_.BindFramebufferAsRenderTarget(nullptr, color, depth, stencil, rp.clearColor, rp.clearDepth, rp.clearStencil, tag, swapchain->swapchain); + curFramebuffer_ = nullptr; + return; + } + + VKFramebuffer *fb = (VKFramebuffer *)fbo; + renderManager_.BindFramebufferAsRenderTarget(fb ? fb->GetFB() : nullptr, color, depth, stencil, rp.clearColor, rp.clearDepth, rp.clearStencil, tag); curFramebuffer_ = fb; } void VKContext::BindFramebufferAsTexture(Framebuffer *fbo, int binding, Aspect channelBit, int layer) { + _dbg_assert_(fbo && !fbo->IsSwapchain()); VKFramebuffer *fb = (VKFramebuffer *)fbo; _assert_(binding >= 0 && binding < MAX_BOUND_TEXTURES); @@ -1970,6 +2008,12 @@ void VKContext::BindFramebufferSamplesAsTexture(Framebuffer *fbo, int binding, i void VKContext::GetFramebufferDimensions(Framebuffer *fbo, int *w, int *h) { fbo = PrimaryToNull(fbo); + if (fbo && fbo->IsSwapchain()) { + const VKRSwapchain *swapchain = ((VKSwapchain *)fbo)->swapchain; + *w = swapchain->extent.width; + *h = swapchain->extent.height; + return; + } VKFramebuffer *fb = (VKFramebuffer *)fbo; if (fb) { *w = fb->GetFB()->width; @@ -1980,6 +2024,29 @@ void VKContext::GetFramebufferDimensions(Framebuffer *fbo, int *w, int *h) { } } +Swapchain *VKContext::CreateSwapchain(const SwapchainDesc &desc, std::string *error) { + VKRSwapchain *swapchain = renderManager_.CreateExtraSwapchain(desc.winsys, desc.data1, desc.data2, desc.width, desc.height, error); + return swapchain ? new VKSwapchain(swapchain, desc.tag) : nullptr; +} + +void VKContext::DestroySwapchain(Swapchain *swapchain) { + _dbg_assert_(swapchain && swapchain != primarySwapchain_); + VKSwapchain *vkSwapchain = (VKSwapchain *)swapchain; + renderManager_.DestroyExtraSwapchain(vkSwapchain->swapchain); + vkSwapchain->swapchain = nullptr; + vkSwapchain->Release(); +} + +void VKContext::ResizeSwapchain(Swapchain *swapchain, int width, int height) { + if (swapchain == primarySwapchain_) { + // Goes through GraphicsContext::Resize. + return; + } + VKSwapchain *vkSwapchain = (VKSwapchain *)swapchain; + renderManager_.ResizeExtraSwapchain(vkSwapchain->swapchain, width, height); + vkSwapchain->UpdateSize(); +} + void VKContext::HandleEvent(Event ev, int width, int height, void *param1, void *param2) { switch (ev) { case Event::LOST_BACKBUFFER: diff --git a/Core/CmdLine.cpp b/Core/CmdLine.cpp index 34651fb05e..45a1fba82a 100644 --- a/Core/CmdLine.cpp +++ b/Core/CmdLine.cpp @@ -189,7 +189,7 @@ static const CommandLineParam g_autoParams[] = { {POFF(fullscreen), CmdParamType::BoolInverse, "windowed", '\0', "Force windowed mode", CmdLineMode::Application}, {POFF(startScreen), CmdParamType::String, "start-screen", '\0', "Start on a specific screen (e.g. 'gamesettings', 'touchscreentest')", CmdLineMode::Application}, {POFF(escapeExit), CmdParamType::Bool, "escape-exit", '\0', "Escape key exits the application", CmdLineMode::Application}, - {POFF(gameWindow), CmdParamType::Bool, "game-window", '\0', "Show the game in a second window, for testing multiple outputs (SDL, Metal only)", CmdLineMode::Application}, + {POFF(gameWindow), CmdParamType::Bool, "game-window", '\0', "Show the game in a second window, for testing multiple outputs (Vulkan, or Metal on Mac)", CmdLineMode::Application}, {POFF(pauseMenuExit), CmdParamType::Bool, "pause-menu-exit", '\0', "Exit the application when the game ends, instead of returning to the menu", CmdLineMode::Application}, {POFF(appendConfig), CmdParamType::String, "appendconfig", '\0', "Merge config FILE into the current configuration"}, {POFF(root), CmdParamType::String, "root", 'r', "Mount directory as the root of host0:/."}, diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index b437fc78d1..a808a98ef3 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -1442,34 +1442,48 @@ void UpdateCursor() { SDL_SetCursor(g_builtinCursors[(int)curCursor]); } -// --game-window: a second window showing just the game, for testing multiple outputs. +// --game-window: a second window showing just the game. For now it's only a way to test multiple outputs on +// the desktop, standing in for a TV over AirPlay. Later, real multi-window support (like ImGui's multi-viewports) +// will take this over. static SDL_Window *g_gameWindow = nullptr; static int g_gameOutputId = 0; static void CreateGameWindow() { + Draw::SwapchainDesc desc{}; + desc.tag = "GameWindow"; + const GPUBackend backend = (GPUBackend)g_Config.iGPUBackend; + if (backend == GPUBackend::VULKAN) { + g_gameWindow = SDL_CreateWindow("PPSSPP game", 960, 544, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY); + WindowDesc windowDesc{}; + std::string error; + if (g_gameWindow && !DetermineVulkanWindowSystem(g_gameWindow, &windowDesc, &error)) { + fprintf(stderr, "Can't use the game window: %s\n", error.c_str()); + SDL_DestroyWindow(g_gameWindow); + g_gameWindow = nullptr; + return; + } + desc.winsys = windowDesc.winsys; + desc.data1 = windowDesc.data1; + desc.data2 = windowDesc.data2; #if PPSSPP_PLATFORM(MAC) - if (g_Config.iGPUBackend != (int)GPUBackend::METAL) { - fprintf(stderr, "--game-window needs --graphics=metal\n"); + } else if (backend == GPUBackend::METAL) { + g_gameWindow = SDL_CreateWindow("PPSSPP game", 960, 544, SDL_WINDOW_METAL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY); + if (g_gameWindow) { + desc.winsys = WINDOWSYSTEM_METAL_EXT; + desc.data1 = SDL_Metal_GetLayer(SDL_Metal_CreateView(g_gameWindow)); + } +#endif + } else { + // TODO: Windows (D3D11) and OpenGL. + fprintf(stderr, "--game-window needs Vulkan, or Metal on Mac\n"); return; } - g_gameWindow = SDL_CreateWindow("PPSSPP game", 960, 544, SDL_WINDOW_METAL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY); if (!g_gameWindow) { fprintf(stderr, "Error creating the game window: %s\n", SDL_GetError()); return; } - SDL_MetalView metalView = SDL_Metal_CreateView(g_gameWindow); - int width = 0, height = 0; - SDL_GetWindowSizeInPixels(g_gameWindow, &width, &height); - Draw::SwapchainDesc desc{}; - desc.winsys = WINDOWSYSTEM_METAL_EXT; - desc.data1 = SDL_Metal_GetLayer(metalView); - desc.width = width; - desc.height = height; - desc.tag = "GameWindow"; + SDL_GetWindowSizeInPixels(g_gameWindow, &desc.width, &desc.height); g_gameOutputId = Native_AddOutput(OutputRole::Game, desc); -#else - fprintf(stderr, "--game-window is only implemented on Mac (Metal)\n"); -#endif } // Returns true if the event was for the game window, and has been handled. Keys still go to the emulator. diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index bde2586881..48b2db3857 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -1683,8 +1683,7 @@ void EmuScreen::UpdateGameOutput() { return; } OutputParams params; - params.pixelWidth = output->swapchain->Width(); - params.pixelHeight = output->swapchain->Height(); + screenManager()->getDrawContext()->GetFramebufferDimensions(output->swapchain, ¶ms.pixelWidth, ¶ms.pixelHeight); params.orientation = params.pixelHeight > params.pixelWidth * 1.1f ? DeviceOrientation::Portrait : DeviceOrientation::Landscape; gameOutputOrientation_ = params.orientation; gpu->SetPresentationOutput(¶ms); diff --git a/Windows/MainWindow.cpp b/Windows/MainWindow.cpp index 966f84dfec..aa78f1f203 100644 --- a/Windows/MainWindow.cpp +++ b/Windows/MainWindow.cpp @@ -61,6 +61,8 @@ #include "Windows/Debugger/Debugger_MemoryDlg.h" #include "Common/GPU/GraphicsContext.h" +#include "Common/GPU/thin3d.h" +#include "Common/SysError.h" #include "Windows/main.h" #include "Windows/DinputDevice.h" @@ -1348,4 +1350,95 @@ namespace MainWindow { PostMessage(hwndMain, WM_USER_RUN_CALLBACK, reinterpret_cast(callback), reinterpret_cast(userdata)); } + // --game-window: a second window showing just the game. For now it's only a way to test multiple outputs on + // the desktop, standing in for a TV over AirPlay. Later, real multi-window support (like ImGui's multi-viewports) + // will take over these pieces: the window class, the input forwarding and the output lifetime. + static HWND hwndGame; + static int gameOutputId; + + static LRESULT CALLBACK GameWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { + switch (message) { + case WM_SIZE: + if (gameOutputId != 0 && wParam != SIZE_MINIMIZED) { + Native_ResizeOutput(gameOutputId, LOWORD(lParam), HIWORD(lParam)); + } + return 0; + + case WM_CLOSE: + // The window has to outlive the swapchain, so it stays until we quit. + Native_RemoveOutput(gameOutputId); + ShowWindow(hWnd, SW_HIDE); + return 0; + + case WM_ACTIVATE: + if (wParam == WA_ACTIVE || wParam == WA_CLICKACTIVE) { + WindowsRawInput::GainFocus(); + g_InputManager.GainFocus(); + } else { + WindowsRawInput::LoseFocus(); + g_InputManager.LoseFocus(); + } + return 0; + + case WM_INPUT: + { + // Keys still go to the emulator. The mouse would get main window coordinates, so it's left out. + RAWINPUTHEADER header; + UINT size = sizeof(header); + if (GetRawInputData((HRAWINPUT)lParam, RID_HEADER, &header, &size, sizeof(RAWINPUTHEADER)) != (UINT)-1 && header.dwType == RIM_TYPEKEYBOARD) { + return WindowsRawInput::Process(hWnd, wParam, lParam); + } + return DefWindowProc(hWnd, message, wParam, lParam); + } + + case WM_CHAR: + return WindowsRawInput::ProcessChar(hWnd, wParam, lParam); + + default: + return DefWindowProc(hWnd, message, wParam, lParam); + } + } + + void CreateGameWindow() { + if ((GPUBackend)g_Config.iGPUBackend != GPUBackend::VULKAN) { + ERROR_LOG(Log::System, "--game-window needs Vulkan"); + return; + } + + static constexpr const wchar_t *szGameWindowClass = L"PPSSPPGameWnd"; + WNDCLASSEX wcex{}; + wcex.cbSize = sizeof(WNDCLASSEX); + wcex.lpfnWndProc = (WNDPROC)GameWndProc; + wcex.hInstance = hInst; + wcex.hCursor = LoadCursor(NULL, IDC_ARROW); + wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); + wcex.lpszClassName = szGameWindowClass; + wcex.hIcon = LoadIcon(hInst, (LPCTSTR)IDI_PPSSPP); + wcex.hIconSm = (HICON)LoadImage(hInst, (LPCTSTR)IDI_PPSSPP, IMAGE_ICON, 16, 16, LR_SHARED); + RegisterClassEx(&wcex); + + RECT rc{ 0, 0, 960, 544 }; + AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE); + // No owner: an owned window is destroyed along with the main window, which can be before the swapchain is. + hwndGame = CreateWindowEx(0, szGameWindowClass, L"PPSSPP game", WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInst, NULL); + if (!hwndGame) { + ERROR_LOG(Log::System, "Error creating the game window: %s", GetLastErrorMsg().c_str()); + return; + } + + RECT client; + GetClientRect(hwndGame, &client); + Draw::SwapchainDesc desc{}; + desc.winsys = WINDOWSYSTEM_WIN32; + desc.data1 = hInst; + desc.data2 = hwndGame; + desc.width = client.right - client.left; + desc.height = client.bottom - client.top; + desc.tag = "GameWindow"; + gameOutputId = Native_AddOutput(OutputRole::Game, desc); + + ShowWindow(hwndGame, SW_SHOWNOACTIVATE); + } + } // namespace MainWindow diff --git a/Windows/MainWindow.h b/Windows/MainWindow.h index d48b0a3724..31035e13a2 100644 --- a/Windows/MainWindow.h +++ b/Windows/MainWindow.h @@ -80,6 +80,7 @@ namespace MainWindow void SetWindowSize(int zoom); void RunCallbackInWndProc(void (*callback)(void *window, void *userdata), void *userdata); void SetKeepScreenBright(bool keepBright); + void CreateGameWindow(); bool ConfirmAction(HWND hWnd, bool actionIsReset); } diff --git a/Windows/main.cpp b/Windows/main.cpp index 6ddb0441d3..91284155e5 100644 --- a/Windows/main.cpp +++ b/Windows/main.cpp @@ -1264,6 +1264,10 @@ int WINAPI WinMain(HINSTANCE _hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLin HWND hwndMain = MainWindow::GetHWND(); Achievements::InitializeRAIntegration(hwndMain); + if (cmdLineOptions.gameWindow.value_or(false)) { + MainWindow::CreateGameWindow(); + } + //initialize custom controls CtrlDisAsmView::init(); CtrlMemView::init(); diff --git a/docs/multi-swapchain.md b/docs/multi-swapchain.md index 5d1f41a187..ee23653578 100644 --- a/docs/multi-swapchain.md +++ b/docs/multi-swapchain.md @@ -1,6 +1,7 @@ # Multiple swapchains (design) -Status: design, nothing implemented yet. +Status: implemented on Metal and Vulkan, used by iOS TV mode and `--game-window` (Mac, Linux and +Windows). The first user is an iOS "TV mode": with AirPlay connected, the game runs full screen on the TV and the phone shows only the touch controls (and the menus). The same mechanism is meant to carry later uses: @@ -124,35 +125,29 @@ an unlucky vsync phase between two displays makes that stutter. Policy: ### Vulkan -1. **Split `VulkanContext`'s surface state into `VulkanSurfaceSwapchain`:** surface, swapchain, - format, extent, caps, pre-transform, present modes, `cbGetDrawSize`. Move `InitSurface`, - `ReinitSurface`, `InitSwapchain`, `DestroySwapchain` and `DestroySurface` onto it. `VulkanContext` - keeps a primary one, and its current accessors forward to it, so callers don't change. -2. **Make the real swapchain a `VulkanPresentation` too.** Today it's the `presentation_ == nullptr` - branch in each consumer. With a real-swapchain implementation, every output is a presentation: - KHR swapchain, libretro, offscreen, later OpenXR. The render manager then deals with one interface, - and the branches go away. -3. **Per-output state in the render manager.** `FrameDataShared::swapchainImages_` and the queue - runner's `backbuffer_`, `framebuffers_` and `depth_` move into a `VKROutput` (presentation, images, - views, semaphores, framebuffers, depth, out-of-date counter). The per-frame `acquireSemaphore`, - `curSwapchainImage`, `hasAcquired` and `skipSwap` become a small array indexed by output (cap of - 4). Output 0 is the primary. -4. **Steps.** A RENDER step with no framebuffer gets an output index. `SanityCheckPassesOnAdd` changes - from "one backbuffer pass" to "one pass per output, after all offscreen passes". `RunSteps` - already acquires lazily on the first backbuffer step, and that becomes per output. -5. **Submit and present.** The present command buffer waits on the acquire semaphore of every output - acquired this frame (stage COLOR_ATTACHMENT_OUTPUT) and signals each one's rendering-complete - semaphore. Real swapchains present in a single `vkQueuePresentKHR` with `swapchainCount = N`, and - read the per-swapchain results from `pResults`. Other presentations get their own `QueuePresent`. -6. **Queue support.** `CreateSwapchain` checks `vkGetPhysicalDeviceSurfaceSupportKHR` for the - existing graphics queue family and fails if it's missing. That never happens on Apple, Windows or - desktop Linux. On Android it's always the same queue. -7. **Rotation.** The pre-transform is per surface, and so is the rect rotation that reads - `g_display.rotation` (`VulkanRenderManager.cpp`, `RotateRectToDisplay` in the queue runner). The - rotation goes into `VKROutput` and gets applied per pass. +The main swapchain stays where it is, in `VulkanContext` and `FrameDataShared`. The others are +`VKRSwapchain`s (`VulkanFrameData.h`), owned by the render manager: -The single-output path should come out doing exactly what it does now: same waits, same single -present. Most of the regression risk is in step 5, which every platform runs. +- **Creating one:** `VulkanContext::CreateSurface` (the per-platform surface code, shared with the main + surface), a check that the graphics queue can present to it, then the swapchain in the main one's + format, with its own image views, framebuffers (compatible with the backbuffer render pass) and depth + buffer. Per in-flight frame it has an acquire semaphore, and per image a rendering-complete one. +- **Steps:** a RENDER step with no framebuffer has a `swapchain`, null for the main one. The queue + runner uses its framebuffer and size, and doesn't rotate it to the display. + `SanityCheckPassesOnAdd` allows one pass per swapchain, after all the offscreen ones. Swapchain + passes are never merged. +- **Pacing:** acquiring one doesn't wait (timeout 0). If there's no image, the pass is skipped. +- **Submit and present:** the frame's last submit waits on the acquire semaphore of every swapchain + acquired that frame, and signals each one's rendering-complete semaphore. They're presented one + `vkQueuePresentKHR` each, after the main one, even when the main one skipped the frame. +- **Out of date, resize, destroy:** the render thread only flags a swapchain as out of date. It's + recreated at the start of the next frame, on the emu thread. Recreating and destroying stop the + render threads and wait for the queue, like a resize of the main swapchain does. Creating doesn't need + to, since nothing refers to the new one yet. +- **Not done:** reading back an extra swapchain, and rotation (pre-transform) of one, which Android + will need. `VulkanPresentation` (libretro, offscreen) still only replaces the main swapchain. + +With only the main swapchain, the frame does what it did before: frametests on Vulkan match. ### Metal @@ -165,9 +160,8 @@ For a non-pacing layer, skip the pass when `nextDrawable` would block. - **D3D11:** easy, and later. Creating the `IDXGISwapChain` moves from `D3D11Context` into the draw context, one per HWND, and `Present()` presents each one. -- **OpenGL:** `multipleSwapchains = false` for now. Desktop GL could use one context with - `MakeCurrent(window_i)` before each window's pass and swap, which needs a per-surface default FBO - and swap function, and a surface ID in the PRESENT task. Nothing on the iOS path uses GL. +- **OpenGL:** not supported, and not planned: `multipleSwapchains` stays false. With OpenGL, the + platforms keep their single-display behaviour (iOS mirrors to the TV). ## Frontend: outputs and roles @@ -304,9 +298,9 @@ can be tested on its own. (with `--graphics=metal`). This tests most of the design in a fast loop, without AirPlay. Headless can add an offscreen second output, so frametests can check what it shows. 4. **iOS TV mode,** on Metal, tested with the Apple TV. -5. **Vulkan** (`VulkanSurfaceSwapchain`, presentation-for-everything, per-output render manager - state), for Android, the Mac default and Vulkan users on iOS. Single-output path unchanged: unit - tests, pspautotests, frametests, an Android device. Then D3D11 when something wants it. +5. **Vulkan:** done, tested with `--game-window` on Mac, Linux (SDL) and Windows (a second Win32 + window), and on iOS with MoltenVK. Then Android (Vulkan only, through `Presentation`), and D3D11 + when something wants it. Open questions: - Does removing the external window resume mirroring? diff --git a/ios/ExternalDisplaySceneDelegate.mm b/ios/ExternalDisplaySceneDelegate.mm index 957e1097da..e9f07313b7 100644 --- a/ios/ExternalDisplaySceneDelegate.mm +++ b/ios/ExternalDisplaySceneDelegate.mm @@ -111,9 +111,9 @@ static bool g_launched = false; if (!self.windowScene || self.window) { return; } - // Only Metal can present to a second display so far. Without a window, iOS mirrors the phone. - if (g_Config.iGPUBackend != (int)GPUBackend::METAL) { - INFO_LOG(Log::G3D, "External display: mirroring, as the backend isn't Metal"); + // Without a window, iOS mirrors the phone. + if (g_Config.iGPUBackend != (int)GPUBackend::METAL && g_Config.iGPUBackend != (int)GPUBackend::VULKAN) { + INFO_LOG(Log::G3D, "External display: mirroring, as the backend can't present to it"); return; } #if !TARGET_OS_TV diff --git a/ios/ViewController.mm b/ios/ViewController.mm index facf2e65b6..d725277511 100644 --- a/ios/ViewController.mm +++ b/ios/ViewController.mm @@ -648,10 +648,9 @@ static int GetPickerRequestId(id picker) { #if !PPSSPP_PLATFORM(TVOS) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 270000 // From iOS 27, an external display scene only goes to apps that register for one like this. Earlier - // versions go by the Info.plist role instead. Only Metal can show the game there so far, and without - // the scene, iOS mirrors the phone. + // versions go by the Info.plist role instead. Without the scene, iOS mirrors the phone. if (@available(iOS 27.0, *)) { - if (g_Config.iGPUBackend == (int)GPUBackend::METAL) { + if (g_Config.iGPUBackend == (int)GPUBackend::METAL || g_Config.iGPUBackend == (int)GPUBackend::VULKAN) { UISceneConfiguration *sceneConfiguration = [UISceneConfiguration new]; sceneConfiguration.delegateClass = [ExternalDisplaySceneDelegate class]; self.externalDisplayRegistration = [self registerSceneAccessory:[UISceneAccessory externalNonInteractiveSceneAccessoryWithConfiguration:sceneConfiguration]];