2026-03-02 00:25:14 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
#include <event/events.h>
|
|
|
|
|
|
#include <module/module.h>
|
|
|
|
|
|
#include <module/module_registry.h>
|
2026-03-03 02:16:29 +08:00
|
|
|
|
#include <renderer/command_queue.h>
|
|
|
|
|
|
#include <renderer/render_graph.h>
|
2026-03-02 00:25:14 +08:00
|
|
|
|
#include <renderer/render_types.h>
|
2026-03-03 02:16:29 +08:00
|
|
|
|
#include <renderer/rhi/rhi.h>
|
2026-03-02 04:50:28 +08:00
|
|
|
|
#include <renderer/viewport_adapter.h>
|
2026-03-02 00:25:14 +08:00
|
|
|
|
|
|
|
|
|
|
namespace extra2d {
|
|
|
|
|
|
|
2026-03-02 22:44:42 +08:00
|
|
|
|
// 前向声明
|
|
|
|
|
|
class AssetsModule;
|
|
|
|
|
|
|
2026-03-02 00:25:14 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 渲染器模块
|
|
|
|
|
|
*
|
2026-03-03 02:16:29 +08:00
|
|
|
|
* 基于新 RHI 架构的核心渲染系统模块,负责:
|
2026-03-02 00:25:14 +08:00
|
|
|
|
* - 通过事件接收渲染命令
|
2026-03-03 02:16:29 +08:00
|
|
|
|
* - 使用 RenderGraph 管理渲染流程
|
|
|
|
|
|
* - 使用 CommandQueue 进行命令排序和批处理
|
2026-03-02 00:25:14 +08:00
|
|
|
|
* - 执行实际渲染
|
|
|
|
|
|
*/
|
|
|
|
|
|
class RendererModule : public Module {
|
2026-03-03 05:53:40 +08:00
|
|
|
|
// 优先级为 4,在 AssetsModule (优先级 3) 之后初始化
|
|
|
|
|
|
E2D_REGISTER_MODULE(RendererModule, "Renderer", 4)
|
2026-03-02 00:25:14 +08:00
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 默认构造函数
|
|
|
|
|
|
*/
|
|
|
|
|
|
RendererModule();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 析构函数
|
|
|
|
|
|
*/
|
|
|
|
|
|
~RendererModule() override;
|
|
|
|
|
|
|
2026-03-03 02:16:29 +08:00
|
|
|
|
// 禁止拷贝和移动
|
2026-03-02 00:25:14 +08:00
|
|
|
|
RendererModule(const RendererModule &) = delete;
|
|
|
|
|
|
RendererModule &operator=(const RendererModule &) = delete;
|
2026-03-03 02:16:29 +08:00
|
|
|
|
RendererModule(RendererModule &&) = delete;
|
|
|
|
|
|
RendererModule &operator=(RendererModule &&) = delete;
|
2026-03-02 00:25:14 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 初始化模块
|
|
|
|
|
|
*
|
2026-03-03 02:16:29 +08:00
|
|
|
|
* 绑定事件监听器,初始化 RenderGraph 和 CommandQueue
|
2026-03-02 00:25:14 +08:00
|
|
|
|
*
|
|
|
|
|
|
* @return 初始化是否成功
|
|
|
|
|
|
*/
|
|
|
|
|
|
bool init() override;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 关闭模块
|
|
|
|
|
|
*
|
|
|
|
|
|
* 清理所有渲染资源
|
|
|
|
|
|
*/
|
|
|
|
|
|
void shutdown() override;
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
2026-03-03 02:16:29 +08:00
|
|
|
|
// 渲染接口
|
2026-03-02 00:25:14 +08:00
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 设置视口
|
|
|
|
|
|
* @param x 视口左上角 X 坐标
|
|
|
|
|
|
* @param y 视口左上角 Y 坐标
|
|
|
|
|
|
* @param width 视口宽度
|
|
|
|
|
|
* @param height 视口高度
|
|
|
|
|
|
*/
|
|
|
|
|
|
void setViewport(int32 x, int32 y, int32 width, int32 height);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 清除缓冲区
|
|
|
|
|
|
* @param color 清除颜色
|
2026-03-02 04:50:28 +08:00
|
|
|
|
* @param flags 清除标志(组合使用 CLEAR_COLOR_FLAG, CLEAR_DEPTH_FLAG,
|
|
|
|
|
|
* CLEAR_STENCIL_FLAG)
|
2026-03-02 00:25:14 +08:00
|
|
|
|
*/
|
|
|
|
|
|
void clear(const Color &color, uint32 flags = CLEAR_COLOR_FLAG);
|
|
|
|
|
|
|
2026-03-02 04:50:28 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取视口适配器
|
|
|
|
|
|
* @return 视口适配器引用
|
|
|
|
|
|
*/
|
|
|
|
|
|
ViewportAdapter &getViewportAdapter() { return viewportAdapter_; }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取视口适配器(const版本)
|
|
|
|
|
|
* @return 视口适配器const引用
|
|
|
|
|
|
*/
|
|
|
|
|
|
const ViewportAdapter &getViewportAdapter() const { return viewportAdapter_; }
|
|
|
|
|
|
|
2026-03-03 02:16:29 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取渲染图
|
|
|
|
|
|
* @return 渲染图指针
|
|
|
|
|
|
*/
|
|
|
|
|
|
RenderGraph *getRenderGraph() { return &renderGraph_; }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取命令队列
|
|
|
|
|
|
* @return 命令队列指针
|
|
|
|
|
|
*/
|
|
|
|
|
|
CommandQueue *getCommandQueue() { return renderGraph_.getCommandQueue(); }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取 RHI 上下文
|
|
|
|
|
|
* @return RHI 上下文指针
|
|
|
|
|
|
*/
|
|
|
|
|
|
RHIContext *getRHIContext() const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取默认材质句柄
|
|
|
|
|
|
* @return 默认材质句柄
|
|
|
|
|
|
*/
|
|
|
|
|
|
Handle<Material> getDefaultMaterialHandle() const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取默认四边形网格句柄
|
|
|
|
|
|
* @return 默认四边形网格句柄
|
|
|
|
|
|
*/
|
|
|
|
|
|
Handle<Mesh> getDefaultQuadHandle() const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取默认纹理句柄(1x1 白色纹理)
|
|
|
|
|
|
* @return 默认纹理句柄
|
|
|
|
|
|
*/
|
|
|
|
|
|
Handle<Texture> getDefaultTextureHandle() const;
|
|
|
|
|
|
|
2026-03-02 00:25:14 +08:00
|
|
|
|
private:
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
// 事件处理器
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 渲染开始事件处理
|
|
|
|
|
|
*
|
2026-03-03 02:16:29 +08:00
|
|
|
|
* 开始渲染帧,初始化 RenderGraph
|
2026-03-02 00:25:14 +08:00
|
|
|
|
*/
|
|
|
|
|
|
void onRenderBegin();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 渲染提交事件处理
|
|
|
|
|
|
* @param cmd 渲染命令
|
|
|
|
|
|
*/
|
|
|
|
|
|
void onRenderSubmit(const RenderCommand &cmd);
|
|
|
|
|
|
|
2026-03-02 04:50:28 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 渲染设置相机事件处理
|
|
|
|
|
|
* @param viewProj 视图投影矩阵
|
|
|
|
|
|
*/
|
|
|
|
|
|
void onRenderSetCamera(const Mat4 &viewProj);
|
|
|
|
|
|
|
2026-03-02 00:25:14 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 渲染结束事件处理
|
|
|
|
|
|
*
|
2026-03-03 02:16:29 +08:00
|
|
|
|
* 执行 RenderGraph 渲染
|
2026-03-02 00:25:14 +08:00
|
|
|
|
*/
|
|
|
|
|
|
void onRenderEnd();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 窗口大小改变事件处理
|
|
|
|
|
|
* @param width 新宽度
|
|
|
|
|
|
* @param height 新高度
|
|
|
|
|
|
*/
|
|
|
|
|
|
void onResize(int32 width, int32 height);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 窗口显示事件处理
|
|
|
|
|
|
*
|
2026-03-03 02:16:29 +08:00
|
|
|
|
* 延迟初始化到窗口显示时
|
2026-03-02 00:25:14 +08:00
|
|
|
|
*/
|
|
|
|
|
|
void onWindowShow();
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
// 渲染执行
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 执行单个渲染命令
|
|
|
|
|
|
* @param cmd 渲染命令
|
|
|
|
|
|
*/
|
|
|
|
|
|
void executeCommand(const RenderCommand &cmd);
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
2026-03-03 02:16:29 +08:00
|
|
|
|
// 渲染图和命令队列
|
2026-03-02 00:25:14 +08:00
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
2026-03-03 02:16:29 +08:00
|
|
|
|
RenderGraph renderGraph_; // 渲染图
|
|
|
|
|
|
CommandQueue *commandQueue_ = nullptr; // 命令队列(指向 renderGraph_ 内部)
|
2026-03-02 00:25:14 +08:00
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
// 事件监听器
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
events::OnRenderBegin::Listener onRenderBeginListener_;
|
|
|
|
|
|
events::OnRenderSubmit::Listener onRenderSubmitListener_;
|
2026-03-02 04:50:28 +08:00
|
|
|
|
events::OnRenderSetCamera::Listener onRenderSetCameraListener_;
|
2026-03-02 00:25:14 +08:00
|
|
|
|
events::OnRenderEnd::Listener onRenderEndListener_;
|
|
|
|
|
|
events::OnResize::Listener onResizeListener_;
|
|
|
|
|
|
events::OnShow::Listener onShowListener_;
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
// 状态标志
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
2026-03-03 02:16:29 +08:00
|
|
|
|
bool initialized_ = false; // 是否已初始化
|
2026-03-02 00:25:14 +08:00
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
// 渲染统计
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
struct Stats {
|
2026-03-02 04:50:28 +08:00
|
|
|
|
uint32 commandsSubmitted = 0; // 提交的命令数
|
|
|
|
|
|
uint32 drawCalls = 0; // 绘制调用次数
|
|
|
|
|
|
uint32 batches = 0; // 批次数
|
2026-03-02 00:25:14 +08:00
|
|
|
|
} stats_;
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
// 视口状态
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
int32 viewportX_ = 0, viewportY_ = 0;
|
2026-03-03 02:16:29 +08:00
|
|
|
|
int32 viewportWidth_ = 1280, viewportHeight_ = 720;
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
// 视口适配器
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
ViewportAdapter viewportAdapter_; // 视口适配器
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
// 相机矩阵
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
Mat4 viewProjectionMatrix_; // 当前视图投影矩阵
|
2026-03-02 00:25:14 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace extra2d
|