From a8eebf519fb4d3b98c8b8466fadfaa73d4c01f80 Mon Sep 17 00:00:00 2001 From: Xiang Silian Date: Tue, 16 Sep 2025 23:04:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20crash=20=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/texture_loading/texture_loading.cpp | 40 +++++++++++++++++++ samples/api/texture_loading/texture_loading.h | 12 ++++++ 2 files changed, 52 insertions(+) diff --git a/samples/api/texture_loading/texture_loading.cpp b/samples/api/texture_loading/texture_loading.cpp index 6af939a..8cc2692 100644 --- a/samples/api/texture_loading/texture_loading.cpp +++ b/samples/api/texture_loading/texture_loading.cpp @@ -26,6 +26,7 @@ TextureLoading::TextureLoading() zoom = -2.5f; rotation = { 0.0f, 15.0f, 0.0f }; title = "Texture loading"; + this_instance = this; } TextureLoading::~TextureLoading() @@ -50,6 +51,7 @@ TextureLoading::~TextureLoading() vertex_buffer.reset(); index_buffer.reset(); uniform_buffer_vs.reset(); + //stop(); } // Enable physical device features required for this example @@ -507,6 +509,7 @@ void TextureLoading::build_command_buffers() void TextureLoading::draw() { + std::unique_lock lock(mtx); ApiVulkanSample::prepare_frame(); // Command buffer to be submitted to the queue @@ -916,15 +919,51 @@ bool TextureLoading::prepare(const vkb::ApplicationOptions& options) setup_descriptor_set_bg(); build_command_buffers(); prepared = true; + //start(); return true; } +//void TextureLoading::updateTexture() +//{ +// std::unique_lock lock(mtx); +// std::cout << "Working in thread: " << std::this_thread::get_id() << std::endl; +// int width = 640; +// int height = 480; +// int rowStride; +// auto testImage = generateSimpleTestImage(width, height, &rowStride); +// size_t dataSize = testImage.size(); +// processWithVulkan(testImage.data(), width, height, rowStride, dataSize, cam_text); +//} +// +//void TextureLoading::run() { +// std::this_thread::sleep_for(std::chrono::milliseconds(5000)); +// while (running) +// { +// updateTexture(); +// std::this_thread::sleep_for(std::chrono::milliseconds(100)); +// } +//} +// +//void TextureLoading::start() { +// running = true; +// // 启动线程执行 run 方法 +// workerThread = std::thread(&TextureLoading::run, this); +//} +// +//void TextureLoading::stop() { +// running = false; +// if (workerThread.joinable()) { +// workerThread.join(); +// } +//} + void TextureLoading::render(float delta_time) { if (!prepared) { return; } + draw(); } @@ -959,6 +998,7 @@ void TextureLoadProcessWithVulkan(uint8_t* data, int width, int height, int rowS void TextureLoading::processWithVulkan(uint8_t* data, int width, int height, int rowStride, size_t dataSize, Texture& out_texture) { + std::unique_lock lock(mtx); VkDevice& device = get_device().get_handle(); const VkPhysicalDevice& physicalDevice = get_device().get_gpu().get_handle(); if (out_texture.image == VK_NULL_HANDLE) diff --git a/samples/api/texture_loading/texture_loading.h b/samples/api/texture_loading/texture_loading.h index 395e7e4..25b8409 100644 --- a/samples/api/texture_loading/texture_loading.h +++ b/samples/api/texture_loading/texture_loading.h @@ -24,6 +24,8 @@ #include #include "api_vulkan_sample.h" +#include +#include // Vertex layout for this example struct TextureLoadingVertexStructure @@ -117,6 +119,16 @@ public: public: void prepare_pipeline_bg(); +private: + std::thread workerThread; + bool running = false; + +public: + //void run(); + //void start(); + std::mutex mtx; + //void stop(); + //void updateTexture(); }; std::unique_ptr create_texture_loading();