fix bug and add log
This commit is contained in:
+115
-4
@@ -148,6 +148,105 @@ void Application::cleanupSecondInit()
|
||||
_sceondInited = false;
|
||||
}
|
||||
|
||||
void Application::cleanupForWindowLost()
|
||||
{
|
||||
if (!_applicationInited || !_sceondInited) {
|
||||
FACE_DBG_LOG("cleanupForWindowLost: skip (_applicationInited=%d _sceondInited=%d)",
|
||||
(int)_applicationInited, (int)_sceondInited);
|
||||
return;
|
||||
}
|
||||
FACE_DBG_LOG("cleanupForWindowLost: begin (imageCount=%zu MAX_FRAMES_IN_FLIGHT=%d)",
|
||||
swapChainImages.size(), MAX_FRAMES_IN_FLIGHT);
|
||||
|
||||
// Block until GPU is no longer using any of the resources we are about
|
||||
// to destroy. Anything weaker than this risks hitting VK_ERROR_DEVICE_LOST
|
||||
// on drivers that don't tolerate destroying in-flight resources.
|
||||
vkDeviceWaitIdle(device);
|
||||
|
||||
for (auto framebuffer : swapChainFramebuffers) {
|
||||
if (framebuffer != VK_NULL_HANDLE) {
|
||||
vkDestroyFramebuffer(device, framebuffer, nullptr);
|
||||
}
|
||||
}
|
||||
swapChainFramebuffers.clear();
|
||||
|
||||
for (auto imageView : swapChainImageViews) {
|
||||
if (imageView != VK_NULL_HANDLE) {
|
||||
vkDestroyImageView(device, imageView, nullptr);
|
||||
}
|
||||
}
|
||||
swapChainImageViews.clear();
|
||||
|
||||
// Free the command buffers allocated from commandPool. The pool itself is
|
||||
// kept alive so textures/staging uploads that run concurrently off the
|
||||
// render thread (via commandPool_ex) aren't disrupted.
|
||||
if (!commandBuffers.empty()) {
|
||||
vkFreeCommandBuffers(device, commandPool,
|
||||
(uint32_t)commandBuffers.size(), commandBuffers.data());
|
||||
commandBuffers.clear();
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < imageAvailableSemaphores.size(); ++i) {
|
||||
if (imageAvailableSemaphores[i] != VK_NULL_HANDLE) {
|
||||
vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
|
||||
}
|
||||
}
|
||||
imageAvailableSemaphores.clear();
|
||||
for (size_t i = 0; i < renderFinishedSemaphores.size(); ++i) {
|
||||
if (renderFinishedSemaphores[i] != VK_NULL_HANDLE) {
|
||||
vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
|
||||
}
|
||||
}
|
||||
renderFinishedSemaphores.clear();
|
||||
for (size_t i = 0; i < inFlightFences.size(); ++i) {
|
||||
if (inFlightFences[i] != VK_NULL_HANDLE) {
|
||||
vkDestroyFence(device, inFlightFences[i], nullptr);
|
||||
}
|
||||
}
|
||||
inFlightFences.clear();
|
||||
imagesInFlight.clear();
|
||||
|
||||
if (swapChain != VK_NULL_HANDLE) {
|
||||
vkDestroySwapchainKHR(device, swapChain, nullptr);
|
||||
swapChain = VK_NULL_HANDLE;
|
||||
}
|
||||
swapChainImages.clear();
|
||||
|
||||
if (surface != VK_NULL_HANDLE) {
|
||||
vkDestroySurfaceKHR(instance, surface, nullptr);
|
||||
surface = VK_NULL_HANDLE;
|
||||
}
|
||||
|
||||
currentFrame = 0;
|
||||
_sceondInited = false;
|
||||
FACE_DBG_LOG("cleanupForWindowLost: done");
|
||||
}
|
||||
|
||||
void Application::reinitForNewWindow()
|
||||
{
|
||||
if (!_applicationInited) {
|
||||
FACE_DBG_LOG("reinitForNewWindow: skip, _applicationInited=0 (must go through initVulkan first)");
|
||||
return;
|
||||
}
|
||||
if (_sceondInited) {
|
||||
FACE_DBG_LOG("reinitForNewWindow: skip, already _sceondInited=1");
|
||||
return;
|
||||
}
|
||||
FACE_DBG_LOG("reinitForNewWindow: begin");
|
||||
|
||||
createSurface();
|
||||
createSwapChain();
|
||||
createImageViews();
|
||||
createFramebuffers();
|
||||
createCommandBuffer();
|
||||
createSyncObjects();
|
||||
|
||||
_sceondInited = true;
|
||||
FACE_DBG_LOG("reinitForNewWindow: done (imageCount=%zu extent=%ux%u MAX_FRAMES_IN_FLIGHT=%d)",
|
||||
swapChainImages.size(), swapChainExtent.width, swapChainExtent.height,
|
||||
MAX_FRAMES_IN_FLIGHT);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Application::createImageViews() {
|
||||
@@ -590,18 +689,24 @@ void Application::drawFrame(long long frameTime)
|
||||
// 1. 等待前一帧完成
|
||||
VkResult waitRes = vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
|
||||
if (waitRes != VK_SUCCESS) {
|
||||
FACE_DBG_LOG("drawFrame[%llu] vkWaitForFences -> %d (%s) currentFrame=%u",
|
||||
#ifndef _WIN32
|
||||
DebugLog::log_throttled("drawFrame.waitFences",
|
||||
"drawFrame[%llu] vkWaitForFences -> %d (%s) currentFrame=%u",
|
||||
(unsigned long long)s_drawFrameCount,
|
||||
(int)waitRes, VkResultStr(waitRes), currentFrame);
|
||||
#endif
|
||||
}
|
||||
|
||||
// 2. 获取交换链图像
|
||||
uint32_t imageIndex;
|
||||
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
|
||||
if (result != VK_SUCCESS) {
|
||||
FACE_DBG_LOG("drawFrame[%llu] vkAcquireNextImageKHR -> %d (%s) currentFrame=%u",
|
||||
#ifndef _WIN32
|
||||
DebugLog::log_throttled("drawFrame.acquire",
|
||||
"drawFrame[%llu] vkAcquireNextImageKHR -> %d (%s) currentFrame=%u",
|
||||
(unsigned long long)s_drawFrameCount,
|
||||
(int)result, VkResultStr(result), currentFrame);
|
||||
#endif
|
||||
throw std::runtime_error("failed to acquire swap chain image!");
|
||||
}
|
||||
|
||||
@@ -634,10 +739,13 @@ void Application::drawFrame(long long frameTime)
|
||||
|
||||
VkResult submitRes = vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]);
|
||||
if (submitRes != VK_SUCCESS) {
|
||||
FACE_DBG_LOG("drawFrame[%llu] vkQueueSubmit -> %d (%s) currentFrame=%u imageIndex=%u",
|
||||
#ifndef _WIN32
|
||||
DebugLog::log_throttled("drawFrame.submit",
|
||||
"drawFrame[%llu] vkQueueSubmit -> %d (%s) currentFrame=%u imageIndex=%u",
|
||||
(unsigned long long)s_drawFrameCount,
|
||||
(int)submitRes, VkResultStr(submitRes),
|
||||
currentFrame, imageIndex);
|
||||
#endif
|
||||
throw std::runtime_error("failed to submit draw command buffer!");
|
||||
}
|
||||
// 7. 呈现图像
|
||||
@@ -652,10 +760,13 @@ void Application::drawFrame(long long frameTime)
|
||||
result = vkQueuePresentKHR(presentQueue, &presentInfo);
|
||||
|
||||
if (result != VK_SUCCESS) {
|
||||
FACE_DBG_LOG("drawFrame[%llu] vkQueuePresentKHR -> %d (%s) currentFrame=%u imageIndex=%u",
|
||||
#ifndef _WIN32
|
||||
DebugLog::log_throttled("drawFrame.present",
|
||||
"drawFrame[%llu] vkQueuePresentKHR -> %d (%s) currentFrame=%u imageIndex=%u",
|
||||
(unsigned long long)s_drawFrameCount,
|
||||
(int)result, VkResultStr(result),
|
||||
currentFrame, imageIndex);
|
||||
#endif
|
||||
throw std::runtime_error("failed to present swap chain image!");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user