From 79e939ce5ee2fd9e48bd28bbbc1030b267fb36d7 Mon Sep 17 00:00:00 2001 From: ChestnutYueyue <952134128@qq.com> Date: Tue, 3 Mar 2026 22:02:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B8=B2=E6=9F=93):=20=E7=A7=BB?= =?UTF-8?q?=E9=99=A4OnRenderPresent=E4=BA=8B=E4=BB=B6=E5=8F=8A=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将缓冲区交换逻辑移至RHI模块处理,移除不再需要的OnRenderPresent事件及相关监听代码。同时统一日志消息中的中英文混用问题,将部分英文日志消息改为中文。 --- include/event/events.h | 7 ------ include/platform/window_module.h | 10 --------- src/context/context.cpp | 3 --- src/platform/window_module.cpp | 11 --------- src/renderer/command_queue.cpp | 10 ++++----- src/renderer/texture_atlas.cpp | 38 +++++++++++++++----------------- src/renderer/uniform_buffer.cpp | 10 +++++---- 7 files changed, 29 insertions(+), 60 deletions(-) diff --git a/include/event/events.h b/include/event/events.h index 83d864e..ea32396 100644 --- a/include/event/events.h +++ b/include/event/events.h @@ -259,11 +259,4 @@ DECLARE_EVENT_1(OnRenderSetCamera, Engine, extra2d::Mat4) */ DECLARE_EVENT_0(OnRenderEnd, Engine) -/** - * @brief 渲染呈现事件 - * - * 在渲染完成并准备好呈现时触发,窗口模块应调用 swapBuffers - */ -DECLARE_EVENT_0(OnRenderPresent, Engine) - } // namespace extra2d::events diff --git a/include/platform/window_module.h b/include/platform/window_module.h index 7f9d781..507afa2 100644 --- a/include/platform/window_module.h +++ b/include/platform/window_module.h @@ -130,13 +130,6 @@ private: */ void onModuleConfig(const AppConfig &config); - /** - * @brief 监听渲染呈现事件 - * - * 当渲染完成时调用 swapBuffers 呈现渲染结果 - */ - void onRenderPresent(); - SDL_Window *window_ = nullptr; bool shouldClose_ = false; @@ -145,9 +138,6 @@ private: // 模块配置事件监听器 std::unique_ptr::Listener> configListener_; - - // 渲染呈现事件监听器 - std::unique_ptr renderPresentListener_; }; } // namespace extra2d diff --git a/src/context/context.cpp b/src/context/context.cpp index 84fcb80..a343994 100644 --- a/src/context/context.cpp +++ b/src/context/context.cpp @@ -79,9 +79,6 @@ void Context::tick(float dt) { // 3. 渲染结束并执行绘制 events::OnRenderEnd::emit(); - - // 4. 呈现渲染结果(交换缓冲区) - events::OnRenderPresent::emit(); } ModuleRegistry &Context::modules() { return ModuleRegistry::instance(); } diff --git a/src/platform/window_module.cpp b/src/platform/window_module.cpp index 3842555..b906de6 100644 --- a/src/platform/window_module.cpp +++ b/src/platform/window_module.cpp @@ -22,18 +22,12 @@ bool WindowModule::init() { configListener_->bind( [this](const AppConfig &config) { this->onModuleConfig(config); }); - // 监听渲染呈现事件 - renderPresentListener_ = - std::make_unique(); - renderPresentListener_->bind([this]() { this->onRenderPresent(); }); - return true; } void WindowModule::shutdown() { // 清理事件监听器 configListener_.reset(); - renderPresentListener_.reset(); if (window_) { SDL_DestroyWindow(window_); @@ -206,9 +200,4 @@ bool WindowModule::isVisible() const { return (flags & SDL_WINDOW_SHOWN) != 0; } -void WindowModule::onRenderPresent() { - // 交换缓冲区由 RHI 模块处理 - // 这里可以触发呈现事件 -} - } // namespace extra2d diff --git a/src/renderer/command_queue.cpp b/src/renderer/command_queue.cpp index 9e3c6ea..aa572bc 100644 --- a/src/renderer/command_queue.cpp +++ b/src/renderer/command_queue.cpp @@ -198,33 +198,33 @@ bool CommandQueue::initialize() { // 获取 RHI 模块 auto *rhiModule = RHIModule::get(); if (!rhiModule) { - E2D_ERROR("RHIModule not available"); + E2D_ERROR("RHI模块 不可用"); return false; } auto *device = rhiModule->getDevice(); if (!device) { - E2D_ERROR("RHIDevice not available"); + E2D_ERROR("RHI 设备不可用"); return false; } context_ = rhiModule->getContext(); if (!context_) { - E2D_ERROR("RHIContext not available"); + E2D_ERROR("RHI 上下文不可用"); return false; } // 创建命令列表 commandList_ = device->createCommandList(); if (!commandList_) { - E2D_ERROR("Failed to create command list"); + E2D_ERROR("创建命令列表失败"); return false; } // 初始化 UBO 管理器 uboManager_ = std::make_unique(); if (!uboManager_->initialize()) { - E2D_ERROR("Failed to initialize UniformBufferManager"); + E2D_ERROR("统一缓冲区管理器初始化失败"); return false; } diff --git a/src/renderer/texture_atlas.cpp b/src/renderer/texture_atlas.cpp index 9d3fe37..da4aef1 100644 --- a/src/renderer/texture_atlas.cpp +++ b/src/renderer/texture_atlas.cpp @@ -49,7 +49,7 @@ bool TextureAtlas::initialize(int width, int height) { shutdown(); if (width <= 0 || height <= 0) { - E2D_ERROR("TextureAtlas::initialize: 无效的尺寸 {}x{}", width, height); + E2D_ERROR("纹理图集::初始化: 无效的尺寸 {}x{}", width, height); return false; } @@ -79,17 +79,17 @@ void TextureAtlas::shutdown() { bool TextureAtlas::addTexture(const std::string &name, Ptr texture) { if (finalized_) { - E2D_WARN("TextureAtlas::addTexture: 无法向已完成的图集添加纹理"); + E2D_WARN("纹理图集::添加纹理: 无法向已完成的图集添加纹理"); return false; } if (!texture) { - E2D_WARN("TextureAtlas::addTexture: 纹理为空"); + E2D_WARN("纹理图集::添加纹理: 纹理为空"); return false; } if (regions_.find(name) != regions_.end()) { - E2D_WARN("TextureAtlas::addTexture: 纹理 '{}' 已存在", name); + E2D_WARN("纹理图集::添加纹理: 纹理 '{}' 已存在", name); return false; } @@ -98,14 +98,13 @@ bool TextureAtlas::addTexture(const std::string &name, Ptr texture) { int texHeight = static_cast(texture->getHeight()); if (texWidth <= 0 || texHeight <= 0) { - E2D_WARN("TextureAtlas::addTexture: 无效的纹理尺寸 {}x{}", texWidth, - texHeight); + E2D_WARN("纹理图集::添加纹理: 无效的纹理尺寸 {}x{}", texWidth, texHeight); return false; } // 检查纹理是否适合图集 if (texWidth > width_ || texHeight > height_) { - E2D_WARN("TextureAtlas::addTexture: 纹理 {}x{} 太大,无法放入图集 {}x{}", + E2D_WARN("纹理图集::添加纹理: 纹理 {}x{} 太大,无法放入图集 {}x{}", texWidth, texHeight, width_, height_); return false; } @@ -134,24 +133,23 @@ bool TextureAtlas::addTexture(const std::string &name, Ptr texture) { bool TextureAtlas::addTextureData(const std::string &name, const uint8_t *data, int width, int height, TextureFormat format) { if (finalized_) { - E2D_WARN("TextureAtlas::addTextureData: 无法向已完成的图集添加纹理"); + E2D_WARN("纹理图集::添加纹理数据: 无法向已完成的图集添加纹理"); return false; } if (!data || width <= 0 || height <= 0) { - E2D_WARN("TextureAtlas::addTextureData: 无效参数"); + E2D_WARN("纹理图集::添加纹理数据: 无效参数"); return false; } if (regions_.find(name) != regions_.end()) { - E2D_WARN("TextureAtlas::addTextureData: 纹理 '{}' 已存在", name); + E2D_WARN("纹理图集::添加纹理数据: 纹理 '{}' 已存在", name); return false; } if (width > width_ || height > height_) { - E2D_WARN( - "TextureAtlas::addTextureData: 纹理 {}x{} 太大,无法放入图集 {}x{}", - width, height, width_, height_); + E2D_WARN("纹理图集::添加纹理数据: 纹理 {}x{} 太大,无法放入图集 {}x{}", + width, height, width_, height_); return false; } @@ -179,7 +177,7 @@ bool TextureAtlas::finalize() { } if (pendingTextures_.empty()) { - E2D_WARN("TextureAtlas::finalize: 没有要打包的纹理"); + E2D_WARN("纹理图集::清理: 没有要打包的纹理"); return false; } @@ -203,7 +201,7 @@ bool TextureAtlas::finalize() { static_cast(rects.size())); if (!result) { - E2D_ERROR("TextureAtlas::finalize: 打包所有纹理失败"); + E2D_ERROR("纹理图集::清理: 打包所有纹理失败"); return false; } @@ -213,7 +211,7 @@ bool TextureAtlas::finalize() { // 处理打包结果 for (const auto &rect : rects) { if (!rect.was_packed) { - E2D_WARN("TextureAtlas::finalize: 纹理 {} 未被打包", rect.id); + E2D_WARN("纹理图集::清理: 纹理 {} 未被打包", rect.id); continue; } @@ -254,7 +252,7 @@ bool TextureAtlas::finalize() { atlasTexture_ = makePtr(); if (!atlasTexture_->loadFromMemory(atlasData.data(), width_, height_, TextureFormat::RGBA8)) { - E2D_ERROR("TextureAtlas::finalize: 创建图集纹理失败"); + E2D_ERROR("纹理图集::清理: 创建图集纹理失败"); return false; } @@ -308,7 +306,7 @@ float TextureAtlas::getUsageRatio() const { Ptr AtlasBuilder::build() { if (textures_.empty()) { - E2D_WARN("AtlasBuilder::build: No textures to build"); + E2D_WARN("图集生成器::构建: 没有需要构建的纹理"); return nullptr; } @@ -330,7 +328,7 @@ Ptr AtlasBuilder::build() { Ptr AtlasBuilder::buildAuto() { if (textures_.empty()) { - E2D_WARN("AtlasBuilder::buildAuto: No textures to build"); + E2D_WARN("图集生成器::自动构建: 没有需要构建的纹理"); return nullptr; } @@ -355,7 +353,7 @@ Ptr AtlasBuilder::buildAuto() { atlasSize * atlasSize < totalArea * 1.5f) { atlasSize *= 2; if (atlasSize > 8192) { - E2D_ERROR("AtlasBuilder::buildAuto: Textures too large for atlas"); + E2D_ERROR("图集生成器::自动构建: 纹理尺寸过大,无法放入图集"); return nullptr; } } diff --git a/src/renderer/uniform_buffer.cpp b/src/renderer/uniform_buffer.cpp index 17eaa70..f140f53 100644 --- a/src/renderer/uniform_buffer.cpp +++ b/src/renderer/uniform_buffer.cpp @@ -41,7 +41,6 @@ bool UniformBuffer::create(uint32_t size, uint32_t binding) { // 获取缓冲区句柄 handle_ = BufferHandle(buffer.release()); - E2D_DEBUG("UBO 已创建: 大小={}, 绑定槽={}", size, binding); return true; } @@ -123,9 +122,12 @@ bool UniformBufferManager::initialize() { globalUBOs_[i] = std::make_unique(); if (!globalUBOs_[i]->create(UniformBufferManager::GLOBAL_UBO_SIZE, UniformBufferManager::GLOBAL_UBO_BINDING)) { - E2D_ERROR("创建全局 UBO {} 失败", i); + E2D_ERROR("创建全局 UBO [{}] 失败", i); return false; } + E2D_DEBUG("全局 UBO [{}] 已创建: 大小={}, 绑定槽={}", i, + UniformBufferManager::GLOBAL_UBO_SIZE, + UniformBufferManager::GLOBAL_UBO_BINDING); } // 预分配材质 UBO 池 @@ -134,7 +136,7 @@ bool UniformBufferManager::initialize() { // 预分配材质 UBO CPU 缓冲区 materialUBOBuffer_.resize(MATERIAL_UBO_BUFFER_SIZE); - E2D_INFO("UniformBufferManager 已初始化,使用双缓冲"); + E2D_INFO("统一缓冲区管理器已初始化,使用双缓冲"); return true; } @@ -148,7 +150,7 @@ void UniformBufferManager::shutdown() { materialUBOBufferOffset_ = 0; currentMaterialUBO_ = nullptr; - E2D_INFO("UniformBufferManager 已关闭"); + E2D_INFO("统一缓冲区管理器 已关闭"); } UniformBuffer *UniformBufferManager::getGlobalUBO(uint32_t frameIndex) {