Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
979a251
fix typo
indevn May 27, 2025
b9f2ae8
Implement perspective division and viewport transformation with persp…
indevn Aug 4, 2025
7093d82
implement tile-based rasterizer and refractor the pipeline to support…
indevn Jul 17, 2025
8d58a84
Add Performance Profiling for Deffered Pipeline. Remove detailed debu…
indevn Aug 29, 2025
d0ddf62
Expand the Vertex data structure, implement frustum culling and backf…
indevn Aug 29, 2025
a4021cd
Fix rendering consistency between TRADITIONAL and TILE_BASED modes
indevn Aug 30, 2025
70e1581
add debug mode
indevn Aug 30, 2025
4538667
Revert "add debug mode", which is not necessary for rendering tests.
indevn Sep 2, 2025
b57d907
Add Early-Z to TBR. Remove obsolete functions previously used for TBR…
indevn Sep 4, 2025
1d2d9a9
TBR: Pre-allocate and reuse fragment caches; add RasterizeTo; two-pas…
indevn Sep 5, 2025
8a74379
vertex optimization: avoid data movement and multi-stage memory reall…
indevn Sep 6, 2025
bb5acc1
TBR: Use SoA vertex layout to improve cache locality
indevn Sep 6, 2025
0549211
TBR: Use global framebuffer to avoid merge overhead
indevn Sep 7, 2025
258607a
TBR: Optimize global buffer write-back logic
indevn Sep 11, 2025
ffe0d75
Optimize perspective correction, add helper func, simplify code
indevn Sep 12, 2025
957c9b0
Refactor: Extract pipeline into standalone class; rename TraditionalP…
indevn Sep 12, 2025
d6e3b40
TBR: Replace barycentric coordinate computation with half-space testi…
indevn Sep 14, 2025
30038ef
DR: Optimize fragment collection(pre-reserve per bucket, move-insert,…
indevn Sep 15, 2025
61e75a8
Refactor: Modify the triangle binning logic in TBR to use the TileGri…
indevn Sep 15, 2025
86d06ad
Change timing-related debug messages from SPDLOG_INFO to SPDLOG_DEBUG…
indevn Sep 15, 2025
0ea7f22
TBR: Perform mask-based computation for TBR rasterization to achieve …
indevn Sep 16, 2025
b659f57
VS: Optimize the vertex matrix caching in shaders by adding cache pre…
indevn Sep 16, 2025
e81bcff
FS: Cache vectors and matrices to avoid redundant computations.
indevn Sep 16, 2025
b84cfd2
Enhanced shader class with LUT caching for specular reflection to opt…
indevn Sep 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ cmake --build build-macos --target all
#### 3. 运行示例应用程序

```bash
./build/bin/system_test ../obj
./build/bin/system_test ./obj
```

---
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ cmake --build build-macos --target all
#### 3. Run the Example Application

```bash
./build/bin/system_test ../obj
./build/bin/system_test ./obj
```

---
Expand Down
13 changes: 13 additions & 0 deletions src/include/rasterizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "config.h"
#include "shader.hpp"
#include "vertex_soa.hpp"

namespace simple_renderer {

Expand All @@ -20,6 +21,18 @@ class Rasterizer {
std::vector<Fragment> Rasterize(const Vertex& v0, const Vertex& v1,
const Vertex& v2);

// 非分配版本:将片段直接写入调用方提供的容器
Comment thread
indevn marked this conversation as resolved.
Outdated
// 可选的裁剪区域为半开区间 [x0, x1) × [y0, y1)
// 用于 TBR:将光栅化限制在 tile 边界内,便于复用外部 scratch 容器
void RasterizeTo(const Vertex& v0, const Vertex& v1, const Vertex& v2,
Comment thread
ZzzhHe marked this conversation as resolved.
int x0, int y0, int x1, int y1,
std::vector<Fragment>& out);

// SoA 版本:按顶点索引从 SoA 读取三角形三顶点
void RasterizeTo(const VertexSoA& soa, size_t i0, size_t i1, size_t i2,
int x0, int y0, int x1, int y1,
std::vector<Fragment>& out);

private:
size_t width_, height_;

Expand Down
145 changes: 141 additions & 4 deletions src/include/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@

namespace simple_renderer {

// 渲染模式枚举
enum class RenderingMode {
TRADITIONAL, // 传统光栅化模式 - 立即深度测试
TILE_BASED, // Tile-based光栅化模式 - 移动GPU架构
DEFERRED // 延迟渲染模式 - 经典GPU管线教学模拟
};


// SoA 版 tile 列表中的三角形引用(仅存索引与材质指针)
struct TriangleRef {
size_t i0, i1, i2;
const Material* material = nullptr;
size_t face_index = 0;
};

class SimpleRenderer {
public:
/**
Expand All @@ -53,22 +68,144 @@ class SimpleRenderer {
virtual ~SimpleRenderer() = default;
/// @}

bool Render(const Model &model, const Shader &shader, uint32_t *buffer);
/**
* 绘制单个模型
* @param model 要绘制的模型
* @param shader 用于渲染的着色器
* @param buffer 输出缓冲区
* @return 绘制是否成功
*/
bool DrawModel(const Model &model, const Shader &shader, uint32_t *buffer);

/**
* 设置渲染模式
* @param mode 渲染模式(传统或基于Tile)
*/
void SetRenderingMode(RenderingMode mode);

/**
* 获取当前渲染模式
* @return 当前渲染模式
*/
RenderingMode GetRenderingMode() const;

private:
const size_t height_;
const size_t width_;
LogSystem log_system_;
RenderingMode current_mode_; // 当前渲染模式
bool early_z_enabled_; // Early-Z优化开关
Comment thread
indevn marked this conversation as resolved.
Outdated

std::shared_ptr<Shader> shader_;
std::shared_ptr<Rasterizer> rasterizer_;

/**
* 绘制模型
* 执行绘制管线
* @param model 模型
* @param buffer 输出缓冲区
*/
void ExecuteDrawPipeline(const Model &model, uint32_t *buffer);


/**
* 传统光栅化渲染
* @param model 模型
* @param processedVertices 已处理的顶点
* @param buffer 输出缓冲区
* @return 渲染统计信息
*/
struct RenderStats {
double buffer_alloc_ms;
double rasterization_ms;
double merge_ms;
double total_ms;
};

RenderStats ExecuteTraditionalPipeline(const Model &model,
const std::vector<Vertex> &processedVertices,
uint32_t *buffer);

/**
* Tile-based光栅化渲染
* @param model 模型
* @param processedVertices 已处理的顶点
* @param buffer 输出缓冲区
* @return 渲染统计信息
*/
struct TileRenderStats {
double setup_ms;
double binning_ms;
double buffer_alloc_ms;
double rasterization_ms;
double merge_ms;
double total_ms;
};

/**
* 延迟渲染统计信息
*/
struct DeferredRenderStats {
double buffer_alloc_ms;
double rasterization_ms;
double fragment_collection_ms;
double fragment_merge_ms;
double deferred_shading_ms;
double total_ms;
};
TileRenderStats ExecuteTileBasedPipeline(const Model &model,
const VertexSoA &soa,
uint32_t *buffer);

/**
* 延迟渲染管线
* @param model 模型
* @param processedVertices 已处理的顶点
* @param buffer 输出缓冲区
* @return 渲染统计信息
*/
DeferredRenderStats ExecuteDeferredPipeline(const Model &model,
const std::vector<Vertex> &processedVertices,
uint32_t *buffer);


private:


Comment thread
indevn marked this conversation as resolved.
Outdated
// SoA 版本的 Triangle-Tile binning(两遍计数 + reserve)
void TriangleTileBinning(
const Model &model,
const VertexSoA &soa,
std::vector<std::vector<TriangleRef>> &tile_triangles,
size_t tiles_x, size_t tiles_y, size_t tile_size);


// SoA 版本的 tile 光栅化
void RasterizeTile(
size_t tile_id,
const std::vector<TriangleRef> &triangles,
size_t tiles_x, size_t tiles_y, size_t tile_size,
float* tile_depth_buffer, uint32_t* tile_color_buffer,
std::unique_ptr<float[]> &global_depth_buffer,
std::unique_ptr<uint32_t[]> &global_color_buffer,
const VertexSoA &soa,
bool use_early_z = false,
std::vector<Fragment>* scratch_fragments = nullptr);


/**
* 透视除法 - 将裁剪空间坐标转换为归一化设备坐标(NDC)
* @param vertex 裁剪空间坐标的顶点
* @return 转换后的顶点(NDC坐标)
*/
Vertex PerspectiveDivision(const Vertex &vertex);

/**
* 视口变换 - 将NDC坐标转换为屏幕坐标
* @param vertex NDC坐标的顶点
* @return 转换后的顶点(屏幕坐标)
*/
void DrawModel(const Model &model, uint32_t *buffer);
void DrawModelSlower(const Model &model, uint32_t *buffer);
Vertex ViewportTransformation(const Vertex &vertex);

};
} // namespace simple_renderer

Expand Down
18 changes: 17 additions & 1 deletion src/include/vertex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ class Vertex {
// Constructor with parameters 带参数的构造函数
explicit Vertex(const Vector4f& pos, const Vector3f& norm,
const Vector2f& tex, const Color& color_)
: position_(pos), normal_(norm), texCoords_(tex), color_(color_) {}
: position_(pos), normal_(norm), texCoords_(tex), color_(color_),
clip_position_(pos), has_clip_position_(false) {}

// 扩展构造函数:包含裁剪空间坐标
explicit Vertex(const Vector4f& pos, const Vector3f& norm,
Comment thread
indevn marked this conversation as resolved.
const Vector2f& tex, const Color& color_,
const Vector4f& clip_pos)
: position_(pos), normal_(norm), texCoords_(tex), color_(color_),
clip_position_(clip_pos), has_clip_position_(true) {}

// Transform the vertex with a matrix 使用矩阵变换顶点
void transform(const Matrix4f& matrix) { position_ = matrix * position_; }
Expand All @@ -45,12 +53,20 @@ class Vertex {
[[nodiscard]] inline Vector3f GetNormal() const { return normal_; }
[[nodiscard]] inline Vector2f GetTexCoords() const { return texCoords_; }
[[nodiscard]] inline Color GetColor() const { return color_; }

// 扩展坐标访问
[[nodiscard]] inline Vector4f GetClipPosition() const { return clip_position_; }
[[nodiscard]] inline bool HasClipPosition() const { return has_clip_position_; }

private:
Vector4f position_; // 3D position, 3D顶点坐标
Vector3f normal_; // Normal vector, 顶点法向量
Vector2f texCoords_; // Texture coordinates, 顶点纹理坐标
Color color_;

// 扩展坐标用于裁剪优化
Vector4f clip_position_; // 裁剪空间坐标 (用于视锥体裁剪)
bool has_clip_position_; // 是否包含裁剪坐标
};

inline Vertex operator*(const Matrix4f& matrix, const Vertex& vertex) {
Expand Down
33 changes: 33 additions & 0 deletions src/include/vertex_soa.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Minimal SoA layout for TBR pipeline (Phase 1)
#ifndef SIMPLERENDER_SRC_INCLUDE_VERTEX_SOA_HPP_
#define SIMPLERENDER_SRC_INCLUDE_VERTEX_SOA_HPP_

#include <vector>

#include "math.hpp"
#include "color.h"

namespace simple_renderer {

struct VertexSoA {
Comment thread
indevn marked this conversation as resolved.
Outdated
// 屏幕空间坐标(视口变换后)
std::vector<Vector4f> pos_screen; // screen space position (x,y,z,w)
// 裁剪空间坐标(用于视锥体剔除):clip = MVP * pos
std::vector<Vector4f> pos_clip;
std::vector<Vector3f> normal;
std::vector<Vector2f> uv;
std::vector<Color> color;

inline size_t size() const { return pos_screen.size(); }
inline void resize(size_t n) {
pos_screen.resize(n);
pos_clip.resize(n);
normal.resize(n);
uv.resize(n);
color.resize(n);
}
};

} // namespace simple_renderer

#endif // SIMPLERENDER_SRC_INCLUDE_VERTEX_SOA_HPP_
Loading
Loading