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