refactor(渲染): 移除OnRenderPresent事件及相关处理

将缓冲区交换逻辑移至RHI模块处理,移除不再需要的OnRenderPresent事件及相关监听代码。同时统一日志消息中的中英文混用问题,将部分英文日志消息改为中文。
This commit is contained in:
ChestnutYueyue 2026-03-03 22:02:21 +08:00
parent 579aa2dd0d
commit 79e939ce5e
7 changed files with 29 additions and 60 deletions

View File

@ -259,11 +259,4 @@ DECLARE_EVENT_1(OnRenderSetCamera, Engine, extra2d::Mat4)
*/ */
DECLARE_EVENT_0(OnRenderEnd, Engine) DECLARE_EVENT_0(OnRenderEnd, Engine)
/**
* @brief
*
* swapBuffers
*/
DECLARE_EVENT_0(OnRenderPresent, Engine)
} // namespace extra2d::events } // namespace extra2d::events

View File

@ -130,13 +130,6 @@ private:
*/ */
void onModuleConfig(const AppConfig &config); void onModuleConfig(const AppConfig &config);
/**
* @brief
*
* swapBuffers
*/
void onRenderPresent();
SDL_Window *window_ = nullptr; SDL_Window *window_ = nullptr;
bool shouldClose_ = false; bool shouldClose_ = false;
@ -145,9 +138,6 @@ private:
// 模块配置事件监听器 // 模块配置事件监听器
std::unique_ptr<events::OnModuleConfig<AppConfig>::Listener> configListener_; std::unique_ptr<events::OnModuleConfig<AppConfig>::Listener> configListener_;
// 渲染呈现事件监听器
std::unique_ptr<events::OnRenderPresent::Listener> renderPresentListener_;
}; };
} // namespace extra2d } // namespace extra2d

View File

@ -79,9 +79,6 @@ void Context::tick(float dt) {
// 3. 渲染结束并执行绘制 // 3. 渲染结束并执行绘制
events::OnRenderEnd::emit(); events::OnRenderEnd::emit();
// 4. 呈现渲染结果(交换缓冲区)
events::OnRenderPresent::emit();
} }
ModuleRegistry &Context::modules() { return ModuleRegistry::instance(); } ModuleRegistry &Context::modules() { return ModuleRegistry::instance(); }

View File

@ -22,18 +22,12 @@ bool WindowModule::init() {
configListener_->bind( configListener_->bind(
[this](const AppConfig &config) { this->onModuleConfig(config); }); [this](const AppConfig &config) { this->onModuleConfig(config); });
// 监听渲染呈现事件
renderPresentListener_ =
std::make_unique<events::OnRenderPresent::Listener>();
renderPresentListener_->bind([this]() { this->onRenderPresent(); });
return true; return true;
} }
void WindowModule::shutdown() { void WindowModule::shutdown() {
// 清理事件监听器 // 清理事件监听器
configListener_.reset(); configListener_.reset();
renderPresentListener_.reset();
if (window_) { if (window_) {
SDL_DestroyWindow(window_); SDL_DestroyWindow(window_);
@ -206,9 +200,4 @@ bool WindowModule::isVisible() const {
return (flags & SDL_WINDOW_SHOWN) != 0; return (flags & SDL_WINDOW_SHOWN) != 0;
} }
void WindowModule::onRenderPresent() {
// 交换缓冲区由 RHI 模块处理
// 这里可以触发呈现事件
}
} // namespace extra2d } // namespace extra2d

View File

@ -198,33 +198,33 @@ bool CommandQueue::initialize() {
// 获取 RHI 模块 // 获取 RHI 模块
auto *rhiModule = RHIModule::get(); auto *rhiModule = RHIModule::get();
if (!rhiModule) { if (!rhiModule) {
E2D_ERROR("RHIModule not available"); E2D_ERROR("RHI模块 不可用");
return false; return false;
} }
auto *device = rhiModule->getDevice(); auto *device = rhiModule->getDevice();
if (!device) { if (!device) {
E2D_ERROR("RHIDevice not available"); E2D_ERROR("RHI 设备不可用");
return false; return false;
} }
context_ = rhiModule->getContext(); context_ = rhiModule->getContext();
if (!context_) { if (!context_) {
E2D_ERROR("RHIContext not available"); E2D_ERROR("RHI 上下文不可用");
return false; return false;
} }
// 创建命令列表 // 创建命令列表
commandList_ = device->createCommandList(); commandList_ = device->createCommandList();
if (!commandList_) { if (!commandList_) {
E2D_ERROR("Failed to create command list"); E2D_ERROR("创建命令列表失败");
return false; return false;
} }
// 初始化 UBO 管理器 // 初始化 UBO 管理器
uboManager_ = std::make_unique<UniformBufferManager>(); uboManager_ = std::make_unique<UniformBufferManager>();
if (!uboManager_->initialize()) { if (!uboManager_->initialize()) {
E2D_ERROR("Failed to initialize UniformBufferManager"); E2D_ERROR("统一缓冲区管理器初始化失败");
return false; return false;
} }

View File

@ -49,7 +49,7 @@ bool TextureAtlas::initialize(int width, int height) {
shutdown(); shutdown();
if (width <= 0 || height <= 0) { if (width <= 0 || height <= 0) {
E2D_ERROR("TextureAtlas::initialize: 无效的尺寸 {}x{}", width, height); E2D_ERROR("纹理图集::初始化: 无效的尺寸 {}x{}", width, height);
return false; return false;
} }
@ -79,17 +79,17 @@ void TextureAtlas::shutdown() {
bool TextureAtlas::addTexture(const std::string &name, Ptr<Texture> texture) { bool TextureAtlas::addTexture(const std::string &name, Ptr<Texture> texture) {
if (finalized_) { if (finalized_) {
E2D_WARN("TextureAtlas::addTexture: 无法向已完成的图集添加纹理"); E2D_WARN("纹理图集::添加纹理: 无法向已完成的图集添加纹理");
return false; return false;
} }
if (!texture) { if (!texture) {
E2D_WARN("TextureAtlas::addTexture: 纹理为空"); E2D_WARN("纹理图集::添加纹理: 纹理为空");
return false; return false;
} }
if (regions_.find(name) != regions_.end()) { if (regions_.find(name) != regions_.end()) {
E2D_WARN("TextureAtlas::addTexture: 纹理 '{}' 已存在", name); E2D_WARN("纹理图集::添加纹理: 纹理 '{}' 已存在", name);
return false; return false;
} }
@ -98,14 +98,13 @@ bool TextureAtlas::addTexture(const std::string &name, Ptr<Texture> texture) {
int texHeight = static_cast<int>(texture->getHeight()); int texHeight = static_cast<int>(texture->getHeight());
if (texWidth <= 0 || texHeight <= 0) { if (texWidth <= 0 || texHeight <= 0) {
E2D_WARN("TextureAtlas::addTexture: 无效的纹理尺寸 {}x{}", texWidth, E2D_WARN("纹理图集::添加纹理: 无效的纹理尺寸 {}x{}", texWidth, texHeight);
texHeight);
return false; return false;
} }
// 检查纹理是否适合图集 // 检查纹理是否适合图集
if (texWidth > width_ || texHeight > height_) { if (texWidth > width_ || texHeight > height_) {
E2D_WARN("TextureAtlas::addTexture: 纹理 {}x{} 太大,无法放入图集 {}x{}", E2D_WARN("纹理图集::添加纹理: 纹理 {}x{} 太大,无法放入图集 {}x{}",
texWidth, texHeight, width_, height_); texWidth, texHeight, width_, height_);
return false; return false;
} }
@ -134,23 +133,22 @@ bool TextureAtlas::addTexture(const std::string &name, Ptr<Texture> texture) {
bool TextureAtlas::addTextureData(const std::string &name, const uint8_t *data, bool TextureAtlas::addTextureData(const std::string &name, const uint8_t *data,
int width, int height, TextureFormat format) { int width, int height, TextureFormat format) {
if (finalized_) { if (finalized_) {
E2D_WARN("TextureAtlas::addTextureData: 无法向已完成的图集添加纹理"); E2D_WARN("纹理图集::添加纹理数据: 无法向已完成的图集添加纹理");
return false; return false;
} }
if (!data || width <= 0 || height <= 0) { if (!data || width <= 0 || height <= 0) {
E2D_WARN("TextureAtlas::addTextureData: 无效参数"); E2D_WARN("纹理图集::添加纹理数据: 无效参数");
return false; return false;
} }
if (regions_.find(name) != regions_.end()) { if (regions_.find(name) != regions_.end()) {
E2D_WARN("TextureAtlas::addTextureData: 纹理 '{}' 已存在", name); E2D_WARN("纹理图集::添加纹理数据: 纹理 '{}' 已存在", name);
return false; return false;
} }
if (width > width_ || height > height_) { if (width > width_ || height > height_) {
E2D_WARN( E2D_WARN("纹理图集::添加纹理数据: 纹理 {}x{} 太大,无法放入图集 {}x{}",
"TextureAtlas::addTextureData: 纹理 {}x{} 太大,无法放入图集 {}x{}",
width, height, width_, height_); width, height, width_, height_);
return false; return false;
} }
@ -179,7 +177,7 @@ bool TextureAtlas::finalize() {
} }
if (pendingTextures_.empty()) { if (pendingTextures_.empty()) {
E2D_WARN("TextureAtlas::finalize: 没有要打包的纹理"); E2D_WARN("纹理图集::清理: 没有要打包的纹理");
return false; return false;
} }
@ -203,7 +201,7 @@ bool TextureAtlas::finalize() {
static_cast<int>(rects.size())); static_cast<int>(rects.size()));
if (!result) { if (!result) {
E2D_ERROR("TextureAtlas::finalize: 打包所有纹理失败"); E2D_ERROR("纹理图集::清理: 打包所有纹理失败");
return false; return false;
} }
@ -213,7 +211,7 @@ bool TextureAtlas::finalize() {
// 处理打包结果 // 处理打包结果
for (const auto &rect : rects) { for (const auto &rect : rects) {
if (!rect.was_packed) { if (!rect.was_packed) {
E2D_WARN("TextureAtlas::finalize: 纹理 {} 未被打包", rect.id); E2D_WARN("纹理图集::清理: 纹理 {} 未被打包", rect.id);
continue; continue;
} }
@ -254,7 +252,7 @@ bool TextureAtlas::finalize() {
atlasTexture_ = makePtr<Texture>(); atlasTexture_ = makePtr<Texture>();
if (!atlasTexture_->loadFromMemory(atlasData.data(), width_, height_, if (!atlasTexture_->loadFromMemory(atlasData.data(), width_, height_,
TextureFormat::RGBA8)) { TextureFormat::RGBA8)) {
E2D_ERROR("TextureAtlas::finalize: 创建图集纹理失败"); E2D_ERROR("纹理图集::清理: 创建图集纹理失败");
return false; return false;
} }
@ -308,7 +306,7 @@ float TextureAtlas::getUsageRatio() const {
Ptr<TextureAtlas> AtlasBuilder::build() { Ptr<TextureAtlas> AtlasBuilder::build() {
if (textures_.empty()) { if (textures_.empty()) {
E2D_WARN("AtlasBuilder::build: No textures to build"); E2D_WARN("图集生成器::构建: 没有需要构建的纹理");
return nullptr; return nullptr;
} }
@ -330,7 +328,7 @@ Ptr<TextureAtlas> AtlasBuilder::build() {
Ptr<TextureAtlas> AtlasBuilder::buildAuto() { Ptr<TextureAtlas> AtlasBuilder::buildAuto() {
if (textures_.empty()) { if (textures_.empty()) {
E2D_WARN("AtlasBuilder::buildAuto: No textures to build"); E2D_WARN("图集生成器::自动构建: 没有需要构建的纹理");
return nullptr; return nullptr;
} }
@ -355,7 +353,7 @@ Ptr<TextureAtlas> AtlasBuilder::buildAuto() {
atlasSize * atlasSize < totalArea * 1.5f) { atlasSize * atlasSize < totalArea * 1.5f) {
atlasSize *= 2; atlasSize *= 2;
if (atlasSize > 8192) { if (atlasSize > 8192) {
E2D_ERROR("AtlasBuilder::buildAuto: Textures too large for atlas"); E2D_ERROR("图集生成器::自动构建: 纹理尺寸过大,无法放入图集");
return nullptr; return nullptr;
} }
} }

View File

@ -41,7 +41,6 @@ bool UniformBuffer::create(uint32_t size, uint32_t binding) {
// 获取缓冲区句柄 // 获取缓冲区句柄
handle_ = BufferHandle(buffer.release()); handle_ = BufferHandle(buffer.release());
E2D_DEBUG("UBO 已创建: 大小={}, 绑定槽={}", size, binding);
return true; return true;
} }
@ -123,9 +122,12 @@ bool UniformBufferManager::initialize() {
globalUBOs_[i] = std::make_unique<UniformBuffer>(); globalUBOs_[i] = std::make_unique<UniformBuffer>();
if (!globalUBOs_[i]->create(UniformBufferManager::GLOBAL_UBO_SIZE, if (!globalUBOs_[i]->create(UniformBufferManager::GLOBAL_UBO_SIZE,
UniformBufferManager::GLOBAL_UBO_BINDING)) { UniformBufferManager::GLOBAL_UBO_BINDING)) {
E2D_ERROR("创建全局 UBO {} 失败", i); E2D_ERROR("创建全局 UBO [{}] 失败", i);
return false; return false;
} }
E2D_DEBUG("全局 UBO [{}] 已创建: 大小={}, 绑定槽={}", i,
UniformBufferManager::GLOBAL_UBO_SIZE,
UniformBufferManager::GLOBAL_UBO_BINDING);
} }
// 预分配材质 UBO 池 // 预分配材质 UBO 池
@ -134,7 +136,7 @@ bool UniformBufferManager::initialize() {
// 预分配材质 UBO CPU 缓冲区 // 预分配材质 UBO CPU 缓冲区
materialUBOBuffer_.resize(MATERIAL_UBO_BUFFER_SIZE); materialUBOBuffer_.resize(MATERIAL_UBO_BUFFER_SIZE);
E2D_INFO("UniformBufferManager 已初始化,使用双缓冲"); E2D_INFO("统一缓冲区管理器已初始化,使用双缓冲");
return true; return true;
} }
@ -148,7 +150,7 @@ void UniformBufferManager::shutdown() {
materialUBOBufferOffset_ = 0; materialUBOBufferOffset_ = 0;
currentMaterialUBO_ = nullptr; currentMaterialUBO_ = nullptr;
E2D_INFO("UniformBufferManager 已关闭"); E2D_INFO("统一缓冲区管理器 已关闭");
} }
UniformBuffer *UniformBufferManager::getGlobalUBO(uint32_t frameIndex) { UniformBuffer *UniformBufferManager::getGlobalUBO(uint32_t frameIndex) {