fix crash bug

This commit is contained in:
xsl
2026-02-05 21:06:02 +08:00
parent 52c9da08ed
commit 5d8e108306
3 changed files with 34 additions and 46 deletions
+26 -40
View File
@@ -1,5 +1,6 @@
#include "Application.h"
#include "Application.h"
#include "lodepng.h"
#include <string>
#ifdef _WIN32
#include <codecvt>
@@ -75,14 +76,13 @@ void Application::initVulkan()
}
if (!_sceondInited) {
createSurface();
createSwapChain(); // 创建交换链
createImageViews(); // 创建交换链图像视图
createRenderPass(); // 创建渲染流程
createPipelineLayout(); // 在这里调用
createGraphicsPipeline(); // 创建图形管线
createFramebuffers(); // 创建帧缓冲区
createSwapChain();
createImageViews();
createRenderPass();
createPipelineLayout();
createGraphicsPipeline();
createFramebuffers();
_sceondInited = true;
}
}
@@ -476,7 +476,6 @@ void Application::createCommandBuffer()
void Application::createSyncObjects()
{
imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
@@ -489,16 +488,11 @@ void Application::createSyncObjects()
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
// 为每个交换链图像创建信号量
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS ||
vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS) {
throw std::runtime_error("failed to create synchronization objects!");
}
}
// 为每个帧在飞行创建栅栏
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
if (vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
throw std::runtime_error("failed to create synchronization objects!");
}
@@ -554,10 +548,9 @@ void Application::drawFrame(long long frameTime)
// 1. 等待前一帧完成
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
// 2. 获取交换链图像 - 使用当前帧的信号量
// 2. 获取交换链图像
uint32_t imageIndex;
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX,imageAvailableSemaphores[currentFrame],VK_NULL_HANDLE, &imageIndex);
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
if (result != VK_SUCCESS) {
throw std::runtime_error("failed to acquire swap chain image!");
}
@@ -566,7 +559,6 @@ void Application::drawFrame(long long frameTime)
if (imagesInFlight[imageIndex] != VK_NULL_HANDLE) {
vkWaitForFences(device, 1, &imagesInFlight[imageIndex], VK_TRUE, UINT64_MAX);
}
// 标记该图像正在被当前帧使用
imagesInFlight[imageIndex] = inFlightFences[currentFrame];
// 4. 重置栅栏
@@ -579,39 +571,34 @@ void Application::drawFrame(long long frameTime)
// 6. 提交命令缓冲区
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
// 等待图像获取完成
VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffers[currentFrame];
// 使用对应图像的渲染完成信号量
VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[imageIndex] };
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;
if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
throw std::runtime_error("failed to submit draw command buffer!");
// graphicsQueue 与主线程 drawFrame、FaceApp::endSingleTimeCommandsJNI/worker 线程)共用,必须串行
{
std::lock_guard<std::mutex> lock(mtx);
if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
throw std::runtime_error("failed to submit draw command buffer!");
}
// 7. 呈现图像
VkPresentInfoKHR presentInfo{};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
VkSwapchainKHR swapChains[] = { swapChain };
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains;
presentInfo.pImageIndices = &imageIndex;
result = vkQueuePresentKHR(presentQueue, &presentInfo);
}
// 7. 呈现图像
VkPresentInfoKHR presentInfo{};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores; // 等待对应图像的渲染完成信号量
VkSwapchainKHR swapChains[] = { swapChain };
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains;
presentInfo.pImageIndices = &imageIndex;
result = vkQueuePresentKHR(presentQueue, &presentInfo);
if (result != VK_SUCCESS) {
throw std::runtime_error("failed to present swap chain image!");
}
@@ -635,7 +622,6 @@ void Application::cleanup() {
vkDeviceWaitIdle(device);
// 正确销毁所有同步对象
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
+1 -2
View File
@@ -1,4 +1,4 @@
#pragma once
@@ -80,7 +80,6 @@ public:
int MAX_FRAMES_IN_FLIGHT = 2;
bool _applicationInited = false;
void cleanupSecondInit();
protected:
void loadTexture(std::string path, Texture& tex, bool srgb, VkCommandPool pool);
+7 -4
View File
@@ -1,4 +1,4 @@
#include "FaceApp.h"
#include "FaceApp.h"
#include "hardcode_data.h"
#include "lodepng.h"
#include <sstream>
@@ -984,9 +984,12 @@ void FaceApp::endSingleTimeCommands(VkCommandBuffer commandBuffer) {
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer;
vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
vkQueueWaitIdle(graphicsQueue);
// 与 Application::drawFrame 共用 graphicsQueue,必须串行,避免驱动内 SIGSEGV
{
std::lock_guard<std::mutex> lock(mtx);
vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
vkQueueWaitIdle(graphicsQueue);
}
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
}