mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-04 18:42:40 +08:00
460 lines
22 KiB
C++
460 lines
22 KiB
C++
/****************************************************************************
|
|
Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd.
|
|
|
|
http://www.cocos.com
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated engine source code (the "Software"), a limited,
|
|
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
|
to use Cocos Creator solely to develop games on your target platforms. You shall
|
|
not use Cocos Creator software for developing other software or tools that's
|
|
used for developing games. You are not granted to publish, distribute,
|
|
sublicense, and/or sell copies of Cocos Creator.
|
|
|
|
The software or tools in this License Agreement are licensed, not sold.
|
|
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
****************************************************************************/
|
|
|
|
#include "VKSwapchain.h"
|
|
#include "VKCommands.h"
|
|
#include "VKDevice.h"
|
|
#include "VKGPUObjects.h"
|
|
#include "VKQueue.h"
|
|
#include "VKRenderPass.h"
|
|
#include "VKTexture.h"
|
|
#include "VKUtils.h"
|
|
|
|
#include "application/ApplicationManager.h"
|
|
#include "platform/interfaces/modules/ISystemWindow.h"
|
|
|
|
#if CC_SWAPPY_ENABLED
|
|
#include "swappy/swappyVk.h"
|
|
#include "swappy/swappy_common.h"
|
|
#endif
|
|
|
|
namespace cc {
|
|
namespace gfx {
|
|
|
|
CCVKSwapchain::CCVKSwapchain() {
|
|
_typedID = generateObjectID<decltype(this)>();
|
|
_preRotationEnabled = ENABLE_PRE_ROTATION;
|
|
}
|
|
|
|
CCVKSwapchain::~CCVKSwapchain() {
|
|
destroy();
|
|
}
|
|
|
|
void CCVKSwapchain::doInit(const SwapchainInfo &info) {
|
|
auto *gpuDevice = CCVKDevice::getInstance()->gpuDevice();
|
|
const auto *gpuContext = CCVKDevice::getInstance()->gpuContext();
|
|
_gpuSwapchain = ccnew CCVKGPUSwapchain;
|
|
gpuDevice->swapchains.insert(_gpuSwapchain);
|
|
|
|
createVkSurface();
|
|
|
|
///////////////////// Parameter Selection /////////////////////
|
|
|
|
uint32_t queueFamilyPropertiesCount = utils::toUint(gpuContext->queueFamilyProperties.size());
|
|
_gpuSwapchain->queueFamilyPresentables.resize(queueFamilyPropertiesCount);
|
|
for (uint32_t propertyIndex = 0U; propertyIndex < queueFamilyPropertiesCount; propertyIndex++) {
|
|
vkGetPhysicalDeviceSurfaceSupportKHR(gpuContext->physicalDevice, propertyIndex,
|
|
_gpuSwapchain->vkSurface, &_gpuSwapchain->queueFamilyPresentables[propertyIndex]);
|
|
}
|
|
|
|
// find other possible queues if not presentable
|
|
auto *queue = static_cast<CCVKQueue *>(CCVKDevice::getInstance()->getQueue());
|
|
if (!_gpuSwapchain->queueFamilyPresentables[queue->gpuQueue()->queueFamilyIndex]) {
|
|
auto &indices = queue->gpuQueue()->possibleQueueFamilyIndices;
|
|
indices.erase(std::remove_if(indices.begin(), indices.end(), [this](uint32_t i) {
|
|
return !_gpuSwapchain->queueFamilyPresentables[i];
|
|
}),
|
|
indices.end());
|
|
CC_ASSERT(!_gpuSwapchain->queueFamilyPresentables.empty());
|
|
cmdFuncCCVKGetDeviceQueue(CCVKDevice::getInstance(), queue->gpuQueue());
|
|
}
|
|
|
|
Format colorFmt = Format::BGRA8;
|
|
Format depthStencilFmt = Format::DEPTH_STENCIL;
|
|
|
|
VkSurfaceCapabilitiesKHR surfaceCapabilities{};
|
|
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(gpuContext->physicalDevice, _gpuSwapchain->vkSurface, &surfaceCapabilities);
|
|
|
|
uint32_t surfaceFormatCount = 0U;
|
|
VK_CHECK(vkGetPhysicalDeviceSurfaceFormatsKHR(gpuContext->physicalDevice, _gpuSwapchain->vkSurface, &surfaceFormatCount, nullptr));
|
|
ccstd::vector<VkSurfaceFormatKHR> surfaceFormats(surfaceFormatCount);
|
|
VK_CHECK(vkGetPhysicalDeviceSurfaceFormatsKHR(gpuContext->physicalDevice, _gpuSwapchain->vkSurface, &surfaceFormatCount, surfaceFormats.data()));
|
|
|
|
uint32_t presentModeCount = 0U;
|
|
VK_CHECK(vkGetPhysicalDeviceSurfacePresentModesKHR(gpuContext->physicalDevice, _gpuSwapchain->vkSurface, &presentModeCount, nullptr));
|
|
ccstd::vector<VkPresentModeKHR> presentModes(presentModeCount);
|
|
VK_CHECK(vkGetPhysicalDeviceSurfacePresentModesKHR(gpuContext->physicalDevice, _gpuSwapchain->vkSurface, &presentModeCount, presentModes.data()));
|
|
|
|
VkFormat colorFormat = VK_FORMAT_B8G8R8A8_UNORM;
|
|
VkColorSpaceKHR colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
|
|
// If the surface format list only includes one entry with VK_FORMAT_UNDEFINED,
|
|
// there is no preferred format, so we assume VK_FORMAT_B8G8R8A8_UNORM
|
|
if ((surfaceFormatCount == 1) && (surfaceFormats[0].format == VK_FORMAT_UNDEFINED)) {
|
|
colorFormat = VK_FORMAT_B8G8R8A8_UNORM;
|
|
colorSpace = surfaceFormats[0].colorSpace;
|
|
} else {
|
|
// iterate over the list of available surface format and
|
|
// check for the presence of VK_FORMAT_B8G8R8A8_UNORM
|
|
bool imageFormatFound = false;
|
|
for (VkSurfaceFormatKHR &surfaceFormat : surfaceFormats) {
|
|
if (surfaceFormat.format == VK_FORMAT_B8G8R8A8_UNORM) {
|
|
colorFormat = surfaceFormat.format;
|
|
colorSpace = surfaceFormat.colorSpace;
|
|
imageFormatFound = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// in case VK_FORMAT_B8G8R8A8_UNORM is not available
|
|
// select the first available color format
|
|
if (!imageFormatFound) {
|
|
colorFormat = surfaceFormats[0].format;
|
|
colorSpace = surfaceFormats[0].colorSpace;
|
|
switch (colorFormat) {
|
|
case VK_FORMAT_R8G8B8A8_UNORM: colorFmt = Format::RGBA8; break;
|
|
case VK_FORMAT_R8G8B8A8_SRGB: colorFmt = Format::SRGB8_A8; break;
|
|
case VK_FORMAT_R5G6B5_UNORM_PACK16: colorFmt = Format::R5G6B5; break;
|
|
default: CC_ASSERT(false); break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Select a present mode for the swapchain
|
|
|
|
ccstd::vector<VkPresentModeKHR> presentModePriorityList;
|
|
|
|
switch (_vsyncMode) {
|
|
case VsyncMode::OFF: presentModePriorityList.insert(presentModePriorityList.end(), {VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_FIFO_KHR}); break;
|
|
case VsyncMode::ON: presentModePriorityList.insert(presentModePriorityList.end(), {VK_PRESENT_MODE_FIFO_KHR}); break;
|
|
case VsyncMode::RELAXED: presentModePriorityList.insert(presentModePriorityList.end(), {VK_PRESENT_MODE_FIFO_RELAXED_KHR, VK_PRESENT_MODE_FIFO_KHR}); break;
|
|
case VsyncMode::MAILBOX: presentModePriorityList.insert(presentModePriorityList.end(), {VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR}); break;
|
|
case VsyncMode::HALF: presentModePriorityList.insert(presentModePriorityList.end(), {VK_PRESENT_MODE_FIFO_KHR}); break; // no easy fallback
|
|
}
|
|
|
|
VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
|
|
|
|
// UNASSIGNED-BestPractices-vkCreateSwapchainKHR-swapchain-presentmode-not-fifo
|
|
#if !defined(VK_USE_PLATFORM_ANDROID_KHR)
|
|
for (VkPresentModeKHR presentMode : presentModePriorityList) {
|
|
if (std::find(presentModes.begin(), presentModes.end(), presentMode) != presentModes.end()) {
|
|
swapchainPresentMode = presentMode;
|
|
break;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
// Determine the number of images
|
|
uint32_t desiredNumberOfSwapchainImages = std::max(gpuDevice->backBufferCount, surfaceCapabilities.minImageCount);
|
|
|
|
VkExtent2D imageExtent = {1U, 1U};
|
|
|
|
// Find a supported composite alpha format (not all devices support alpha opaque)
|
|
VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
|
// Simply select the first composite alpha format available
|
|
ccstd::vector<VkCompositeAlphaFlagBitsKHR> compositeAlphaFlags = {
|
|
VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
|
|
VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
|
|
VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
|
|
VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
|
|
};
|
|
for (VkCompositeAlphaFlagBitsKHR compositeAlphaFlag : compositeAlphaFlags) {
|
|
if (surfaceCapabilities.supportedCompositeAlpha & compositeAlphaFlag) {
|
|
compositeAlpha = compositeAlphaFlag;
|
|
break;
|
|
};
|
|
}
|
|
VkImageUsageFlags imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
|
// Enable transfer source on swap chain images if supported
|
|
if (surfaceCapabilities.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
|
|
imageUsage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
|
|
}
|
|
|
|
// Enable transfer destination on swap chain images if supported
|
|
if (surfaceCapabilities.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
|
|
imageUsage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
|
}
|
|
|
|
_gpuSwapchain->createInfo.surface = _gpuSwapchain->vkSurface;
|
|
_gpuSwapchain->createInfo.minImageCount = desiredNumberOfSwapchainImages;
|
|
_gpuSwapchain->createInfo.imageFormat = colorFormat;
|
|
_gpuSwapchain->createInfo.imageColorSpace = colorSpace;
|
|
_gpuSwapchain->createInfo.imageExtent = imageExtent;
|
|
_gpuSwapchain->createInfo.imageUsage = imageUsage;
|
|
_gpuSwapchain->createInfo.imageArrayLayers = 1;
|
|
_gpuSwapchain->createInfo.preTransform = VkSurfaceTransformFlagBitsKHR::VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
|
_gpuSwapchain->createInfo.compositeAlpha = compositeAlpha;
|
|
_gpuSwapchain->createInfo.presentMode = swapchainPresentMode;
|
|
_gpuSwapchain->createInfo.clipped = VK_TRUE; // Setting clipped to VK_TRUE allows the implementation to discard rendering outside of the surface area
|
|
|
|
///////////////////// Texture Creation /////////////////////
|
|
|
|
_colorTexture = ccnew CCVKTexture;
|
|
_depthStencilTexture = ccnew CCVKTexture;
|
|
|
|
SwapchainTextureInfo textureInfo;
|
|
textureInfo.swapchain = this;
|
|
textureInfo.format = colorFmt;
|
|
textureInfo.width = info.width;
|
|
textureInfo.height = info.height;
|
|
initTexture(textureInfo, _colorTexture);
|
|
|
|
textureInfo.format = depthStencilFmt;
|
|
initTexture(textureInfo, _depthStencilTexture);
|
|
|
|
#if CC_PLATFORM == CC_PLATFORM_ANDROID
|
|
auto *window = CC_CURRENT_ENGINE()->getInterface<cc::ISystemWindow>();
|
|
auto viewSize = window->getViewSize();
|
|
checkSwapchainStatus(viewSize.x, viewSize.y);
|
|
|
|
// Android Game Frame Pacing:swappy
|
|
#if CC_SWAPPY_ENABLED
|
|
int32_t fps = cc::BasePlatform::getPlatform()->getFps();
|
|
|
|
uint64_t frameRefreshIntervalNS;
|
|
SwappyVk_initAndGetRefreshCycleDuration(static_cast<JNIEnv *>(platform->getEnv()),
|
|
static_cast<jobject>(platform->getActivity()),
|
|
gpuContext->physicalDevice,
|
|
gpuDevice->vkDevice,
|
|
_gpuSwapchain->vkSwapchain,
|
|
&frameRefreshIntervalNS);
|
|
SwappyVk_setSwapIntervalNS(gpuDevice->vkDevice, _gpuSwapchain->vkSwapchain, fps ? 1000000000L / fps : frameRefreshIntervalNS);
|
|
SwappyVk_setWindow(gpuDevice->vkDevice, _gpuSwapchain->vkSwapchain, static_cast<ANativeWindow *>(info.windowHandle));
|
|
#endif
|
|
#else
|
|
checkSwapchainStatus();
|
|
#endif
|
|
}
|
|
|
|
void CCVKSwapchain::doDestroy() {
|
|
if (!_gpuSwapchain) return;
|
|
|
|
CCVKDevice::getInstance()->waitAllFences();
|
|
|
|
CC_SAFE_DESTROY(_depthStencilTexture)
|
|
CC_SAFE_DESTROY(_colorTexture)
|
|
|
|
auto *gpuDevice = CCVKDevice::getInstance()->gpuDevice();
|
|
const auto *gpuContext = CCVKDevice::getInstance()->gpuContext();
|
|
|
|
destroySwapchain(gpuDevice);
|
|
|
|
if (_gpuSwapchain->vkSurface != VK_NULL_HANDLE) {
|
|
vkDestroySurfaceKHR(gpuContext->vkInstance, _gpuSwapchain->vkSurface, nullptr);
|
|
_gpuSwapchain->vkSurface = VK_NULL_HANDLE;
|
|
}
|
|
|
|
gpuDevice->swapchains.erase(_gpuSwapchain);
|
|
CC_SAFE_DELETE(_gpuSwapchain)
|
|
}
|
|
|
|
void CCVKSwapchain::doResize(uint32_t width, uint32_t height, SurfaceTransform /*transform*/) {
|
|
checkSwapchainStatus(width, height); // the orientation info from system event is not reliable
|
|
}
|
|
|
|
bool CCVKSwapchain::checkSwapchainStatus(uint32_t width, uint32_t height) {
|
|
if (_gpuSwapchain->vkSurface == VK_NULL_HANDLE) { // vkSurface will be set to VK_NULL_HANDLE after call doDestroySurface
|
|
return false;
|
|
}
|
|
auto *gpuDevice = CCVKDevice::getInstance()->gpuDevice();
|
|
const auto *gpuContext = CCVKDevice::getInstance()->gpuContext();
|
|
|
|
VkSurfaceCapabilitiesKHR surfaceCapabilities;
|
|
VK_CHECK(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(gpuContext->physicalDevice, _gpuSwapchain->vkSurface, &surfaceCapabilities));
|
|
|
|
// surfaceCapabilities.currentExtent seems to remain the same
|
|
// during any size/orientation change events on android devices
|
|
// so we prefer the system input (oriented size) here
|
|
uint32_t newWidth = width ? width : surfaceCapabilities.currentExtent.width;
|
|
uint32_t newHeight = height ? height : surfaceCapabilities.currentExtent.height;
|
|
|
|
if (_gpuSwapchain->createInfo.imageExtent.width == newWidth &&
|
|
_gpuSwapchain->createInfo.imageExtent.height == newHeight && _gpuSwapchain->lastPresentResult == VK_SUCCESS) {
|
|
return true;
|
|
}
|
|
|
|
if (newWidth == static_cast<uint32_t>(-1)) {
|
|
_gpuSwapchain->createInfo.imageExtent.width = _colorTexture->getWidth();
|
|
_gpuSwapchain->createInfo.imageExtent.height = _colorTexture->getHeight();
|
|
} else {
|
|
_gpuSwapchain->createInfo.imageExtent.width = newWidth;
|
|
_gpuSwapchain->createInfo.imageExtent.height = newHeight;
|
|
}
|
|
|
|
if (newWidth == 0 || newHeight == 0) {
|
|
_gpuSwapchain->lastPresentResult = VK_NOT_READY;
|
|
return false;
|
|
}
|
|
|
|
_gpuSwapchain->createInfo.surface = _gpuSwapchain->vkSurface;
|
|
_gpuSwapchain->createInfo.oldSwapchain = _gpuSwapchain->vkSwapchain;
|
|
|
|
CC_LOG_INFO("Resizing surface: %dx%d", newWidth, newHeight);
|
|
|
|
CCVKDevice::getInstance()->waitAllFences();
|
|
|
|
VkSwapchainKHR vkSwapchain = VK_NULL_HANDLE;
|
|
VK_CHECK(vkCreateSwapchainKHR(gpuDevice->vkDevice, &_gpuSwapchain->createInfo, nullptr, &vkSwapchain));
|
|
|
|
destroySwapchain(gpuDevice);
|
|
|
|
gpuDevice->curBackBufferIndex = 0;
|
|
_gpuSwapchain->curImageIndex = 0;
|
|
_gpuSwapchain->vkSwapchain = vkSwapchain;
|
|
|
|
uint32_t imageCount;
|
|
VK_CHECK(vkGetSwapchainImagesKHR(gpuDevice->vkDevice, _gpuSwapchain->vkSwapchain, &imageCount, nullptr));
|
|
CCVKDevice::getInstance()->updateBackBufferCount(imageCount);
|
|
_gpuSwapchain->swapchainImages.resize(imageCount);
|
|
VK_CHECK(vkGetSwapchainImagesKHR(gpuDevice->vkDevice, _gpuSwapchain->vkSwapchain, &imageCount, _gpuSwapchain->swapchainImages.data()));
|
|
|
|
// should skip size check, since the old swapchain has already been destroyed
|
|
static_cast<CCVKTexture *>(_colorTexture.get())->_info.width = 1;
|
|
static_cast<CCVKTexture *>(_depthStencilTexture.get())->_info.width = 1;
|
|
_colorTexture->resize(newWidth, newHeight);
|
|
_depthStencilTexture->resize(newWidth, newHeight);
|
|
|
|
bool hasStencil = GFX_FORMAT_INFOS[toNumber(_depthStencilTexture->getFormat())].hasStencil;
|
|
ccstd::vector<VkImageMemoryBarrier> barriers(imageCount * 2, VkImageMemoryBarrier{});
|
|
VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
|
VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
|
|
ThsvsImageBarrier tempBarrier{};
|
|
tempBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
|
tempBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
|
tempBarrier.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS;
|
|
tempBarrier.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS;
|
|
VkPipelineStageFlags tempSrcStageMask = 0;
|
|
VkPipelineStageFlags tempDstStageMask = 0;
|
|
auto *colorGPUTexture = static_cast<CCVKTexture *>(_colorTexture.get())->gpuTexture();
|
|
auto *depthStencilGPUTexture = static_cast<CCVKTexture *>(_depthStencilTexture.get())->gpuTexture();
|
|
for (uint32_t i = 0U; i < imageCount; i++) {
|
|
tempBarrier.nextAccessCount = 1;
|
|
tempBarrier.pNextAccesses = getAccessType(AccessFlagBit::PRESENT);
|
|
tempBarrier.image = _gpuSwapchain->swapchainImages[i];
|
|
tempBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
thsvsGetVulkanImageMemoryBarrier(tempBarrier, &tempSrcStageMask, &tempDstStageMask, &barriers[i]);
|
|
srcStageMask |= tempSrcStageMask;
|
|
dstStageMask |= tempDstStageMask;
|
|
|
|
tempBarrier.nextAccessCount = 1;
|
|
tempBarrier.pNextAccesses = getAccessType(AccessFlagBit::DEPTH_STENCIL_ATTACHMENT_WRITE);
|
|
tempBarrier.image = depthStencilGPUTexture->swapchainVkImages[i];
|
|
tempBarrier.subresourceRange.aspectMask = hasStencil ? VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT : VK_IMAGE_ASPECT_DEPTH_BIT;
|
|
thsvsGetVulkanImageMemoryBarrier(tempBarrier, &tempSrcStageMask, &tempDstStageMask, &barriers[imageCount + i]);
|
|
srcStageMask |= tempSrcStageMask;
|
|
dstStageMask |= tempDstStageMask;
|
|
}
|
|
CCVKDevice::getInstance()->gpuTransportHub()->checkIn(
|
|
[&](const CCVKGPUCommandBuffer *gpuCommandBuffer) {
|
|
vkCmdPipelineBarrier(gpuCommandBuffer->vkCommandBuffer, srcStageMask, dstStageMask, 0, 0, nullptr, 0, nullptr,
|
|
utils::toUint(barriers.size()), barriers.data());
|
|
},
|
|
true); // submit immediately
|
|
|
|
colorGPUTexture->currentAccessTypes.assign(1, THSVS_ACCESS_PRESENT);
|
|
depthStencilGPUTexture->currentAccessTypes.assign(1, THSVS_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ);
|
|
|
|
_gpuSwapchain->lastPresentResult = VK_SUCCESS;
|
|
|
|
return true;
|
|
}
|
|
|
|
void CCVKSwapchain::destroySwapchain(const CCVKGPUDevice *gpuDevice) {
|
|
if (_gpuSwapchain->vkSwapchain != VK_NULL_HANDLE) {
|
|
for (auto &it : _gpuSwapchain->vkSwapchainFramebufferListMap) {
|
|
FramebufferList &list = it.second;
|
|
for (VkFramebuffer framebuffer : list) {
|
|
vkDestroyFramebuffer(gpuDevice->vkDevice, framebuffer, nullptr);
|
|
}
|
|
list.clear();
|
|
}
|
|
|
|
_gpuSwapchain->swapchainImages.clear();
|
|
|
|
#if CC_SWAPPY_ENABLED
|
|
SwappyVk_destroySwapchain(gpuDevice->vkDevice, _gpuSwapchain->vkSwapchain);
|
|
#endif
|
|
|
|
vkDestroySwapchainKHR(gpuDevice->vkDevice, _gpuSwapchain->vkSwapchain, nullptr);
|
|
_gpuSwapchain->vkSwapchain = VK_NULL_HANDLE;
|
|
}
|
|
}
|
|
|
|
void CCVKSwapchain::doDestroySurface() {
|
|
if (!_gpuSwapchain || _gpuSwapchain->vkSurface == VK_NULL_HANDLE) return;
|
|
const auto *gpuDevice = CCVKDevice::getInstance()->gpuDevice();
|
|
const auto *gpuContext = CCVKDevice::getInstance()->gpuContext();
|
|
|
|
CCVKDevice::getInstance()->waitAllFences();
|
|
destroySwapchain(gpuDevice);
|
|
_gpuSwapchain->lastPresentResult = VK_NOT_READY;
|
|
|
|
vkDestroySurfaceKHR(gpuContext->vkInstance, _gpuSwapchain->vkSurface, nullptr);
|
|
_gpuSwapchain->vkSurface = VK_NULL_HANDLE;
|
|
}
|
|
|
|
void CCVKSwapchain::doCreateSurface(void *windowHandle) { // NOLINT
|
|
if (!_gpuSwapchain || _gpuSwapchain->vkSurface != VK_NULL_HANDLE) return;
|
|
createVkSurface();
|
|
#if CC_PLATFORM == CC_PLATFORM_ANDROID
|
|
auto *window = CC_CURRENT_ENGINE()->getInterface<cc::ISystemWindow>();
|
|
auto viewSize = window->getViewSize();
|
|
checkSwapchainStatus(viewSize.x, viewSize.y);
|
|
#else
|
|
checkSwapchainStatus();
|
|
#endif
|
|
#if CC_SWAPPY_ENABLED
|
|
auto *gpuDevice = CCVKDevice::getInstance()->gpuDevice();
|
|
SwappyVk_setWindow(gpuDevice->vkDevice, _gpuSwapchain->vkSwapchain, static_cast<ANativeWindow *>(windowHandle));
|
|
#endif
|
|
}
|
|
|
|
void CCVKSwapchain::createVkSurface() {
|
|
const auto *gpuContext = CCVKDevice::getInstance()->gpuContext();
|
|
|
|
#if defined(VK_USE_PLATFORM_ANDROID_KHR)
|
|
VkAndroidSurfaceCreateInfoKHR surfaceCreateInfo{VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR};
|
|
surfaceCreateInfo.window = reinterpret_cast<ANativeWindow *>(_windowHandle);
|
|
VK_CHECK(vkCreateAndroidSurfaceKHR(gpuContext->vkInstance, &surfaceCreateInfo, nullptr, &_gpuSwapchain->vkSurface));
|
|
#elif defined(VK_USE_PLATFORM_WIN32_KHR)
|
|
VkWin32SurfaceCreateInfoKHR surfaceCreateInfo{VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR};
|
|
surfaceCreateInfo.hinstance = static_cast<HINSTANCE>(GetModuleHandle(nullptr));
|
|
surfaceCreateInfo.hwnd = reinterpret_cast<HWND>(_windowHandle);
|
|
VK_CHECK(vkCreateWin32SurfaceKHR(gpuContext->vkInstance, &surfaceCreateInfo, nullptr, &_gpuSwapchain->vkSurface));
|
|
#elif defined(VK_USE_PLATFORM_VI_NN)
|
|
VkViSurfaceCreateInfoNN surfaceCreateInfo{VK_STRUCTURE_TYPE_VI_SURFACE_CREATE_INFO_NN};
|
|
surfaceCreateInfo.window = _windowHandle;
|
|
VK_CHECK(vkCreateViSurfaceNN(gpuContext->vkInstance, &surfaceCreateInfo, nullptr, &_gpuSwapchain->vkSurface));
|
|
#elif defined(VK_USE_PLATFORM_METAL_EXT)
|
|
VkMetalSurfaceCreateInfoEXT surfaceCreateInfo{VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT};
|
|
surfaceCreateInfo.pLayer = reinterpret_cast<CAMetalLayer *>(_windowHandle);
|
|
VK_CHECK(vkCreateMetalSurfaceEXT(gpuContext->vkInstance, &surfaceCreateInfo, nullptr, &_gpuSwapchain->vkSurface));
|
|
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
|
|
VkWaylandSurfaceCreateInfoKHR surfaceCreateInfo{VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR};
|
|
surfaceCreateInfo.display = nullptr; // TODO
|
|
surfaceCreateInfo.surface = reinterpret_cast<wl_surface *>(_windowHandle);
|
|
VK_CHECK(vkCreateWaylandSurfaceKHR(gpuContext->vkInstance, &surfaceCreateInfo, nullptr, &_gpuSwapchain->vkSurface));
|
|
#elif defined(VK_USE_PLATFORM_XCB_KHR)
|
|
VkXcbSurfaceCreateInfoKHR surfaceCreateInfo{VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR};
|
|
surfaceCreateInfo.connection = nullptr; // TODO
|
|
surfaceCreateInfo.window = reinterpret_cast<uint64_t>(_windowHandle);
|
|
VK_CHECK(vkCreateXcbSurfaceKHR(gpuContext->vkInstance, &surfaceCreateInfo, nullptr, &_gpuSwapchain->vkSurface));
|
|
#else
|
|
#pragma error Platform not supported
|
|
#endif
|
|
}
|
|
|
|
} // namespace gfx
|
|
} // namespace cc
|