Skip to content

Commit 90dc328

Browse files
committed
Remove unused functions and scatch containers
Signed-off-by: ZhouFANG <indevn@outlook.com>
1 parent bd73dc3 commit 90dc328

4 files changed

Lines changed: 2 additions & 197 deletions

File tree

src/include/rasterizer.hpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,6 @@ class Rasterizer {
3333
std::vector<Fragment> Rasterize(const Vertex& v0, const Vertex& v1,
3434
const Vertex& v2);
3535

36-
/**
37-
* @brief 非分配版本:将片段直接写入调用方提供的容器
38-
*
39-
* 可选的裁剪区域为半开区间 [x0, x1) × [y0, y1)
40-
* 用于 TBR:将光栅化限制在 tile 边界内,便于复用外部 scratch 容器
41-
*
42-
* @param v0 三角形第一个顶点
43-
* @param v1 三角形第二个顶点
44-
* @param v2 三角形第三个顶点
45-
* @param x0 裁剪区域左边界(包含)
46-
* @param y0 裁剪区域上边界(包含)
47-
* @param x1 裁剪区域右边界(不包含)
48-
* @param y1 裁剪区域下边界(不包含)
49-
* @param out 输出片段容器
50-
*/
51-
void RasterizeTo(const Vertex& v0, const Vertex& v1, const Vertex& v2,
52-
int x0, int y0, int x1, int y1,
53-
std::vector<Fragment>& out);
54-
55-
/**
56-
* @brief SoA 版本:按顶点索引从 SoA 读取三角形三顶点
57-
* @param soa 结构体数组格式的顶点数据
58-
* @param i0 三角形第一个顶点索引
59-
* @param i1 三角形第二个顶点索引
60-
* @param i2 三角形第三个顶点索引
61-
* @param x0 裁剪区域左边界(包含)
62-
* @param y0 裁剪区域上边界(包含)
63-
* @param x1 裁剪区域右边界(不包含)
64-
* @param y1 裁剪区域下边界(不包含)
65-
* @param out 输出片段容器
66-
*/
67-
void RasterizeTo(const VertexSoA& soa, size_t i0, size_t i1, size_t i2,
68-
int x0, int y0, int x1, int y1,
69-
std::vector<Fragment>& out);
70-
7136
private:
7237
size_t width_, height_;
7338

src/include/renderers/tile_based_renderer.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ class TileBasedRenderer final : public RendererBase {
103103
* @param soa 经过变换后的 SoA 顶点数据
104104
* @param shader 着色器
105105
* @param use_early_z 是否启用 Early‑Z
106-
* @param scratch_fragments 可复用片段临时容器
107106
*/
108107
void RasterizeTile(size_t tile_id,
109108
const std::vector<TileTriangleRef> &triangles,
@@ -113,7 +112,6 @@ class TileBasedRenderer final : public RendererBase {
113112
std::unique_ptr<uint32_t[]> &global_color_buffer,
114113
const Shader& shader,
115114
bool use_early_z,
116-
std::vector<Fragment>* scratch_fragments,
117115
TileMaskStats* out_stats);
118116

119117
private:

src/rasterizer.cpp

Lines changed: 0 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -81,159 +81,6 @@ std::vector<Fragment> Rasterizer::Rasterize(const Vertex& v0, const Vertex& v1,
8181
return fragments;
8282
}
8383

84-
void Rasterizer::RasterizeTo(const Vertex& v0, const Vertex& v1, const Vertex& v2,
85-
int x0, int y0, int x1, int y1,
86-
std::vector<Fragment>& out) {
87-
// 获取三角形的最小 box(屏幕空间)
88-
const Vector4f p0 = v0.GetPosition();
89-
const Vector4f p1 = v1.GetPosition();
90-
const Vector4f p2 = v2.GetPosition();
91-
92-
Vector2f a(p0.x, p0.y);
93-
Vector2f b(p1.x, p1.y);
94-
Vector2f c(p2.x, p2.y);
95-
96-
Vector2f bboxMin = Vector2f{std::min({a.x, b.x, c.x}), std::min({a.y, b.y, c.y})};
97-
Vector2f bboxMax = Vector2f{std::max({a.x, b.x, c.x}), std::max({a.y, b.y, c.y})};
98-
99-
// Clamp 到屏幕尺寸
100-
float minx = std::max(0.0f, bboxMin.x);
101-
float miny = std::max(0.0f, bboxMin.y);
102-
float maxx = std::min(float(width_ - 1), bboxMax.x);
103-
float maxy = std::min(float(height_ - 1), bboxMax.y);
104-
105-
// 与外部提供的裁剪区域相交(半开区间) -> 闭区间扫描
106-
int sx = std::max(x0, static_cast<int>(std::floor(minx)));
107-
int sy = std::max(y0, static_cast<int>(std::floor(miny)));
108-
int ex = std::min(x1 - 1, static_cast<int>(std::floor(maxx)));
109-
int ey = std::min(y1 - 1, static_cast<int>(std::floor(maxy)));
110-
if (sx > ex || sy > ey) return;
111-
112-
for (int x = sx; x <= ex; ++x) {
113-
for (int y = sy; y <= ey; ++y) {
114-
auto [is_inside, bary] = GetBarycentricCoord(
115-
Vector3f(p0.x, p0.y, p0.z), Vector3f(p1.x, p1.y, p1.z), Vector3f(p2.x, p2.y, p2.z),
116-
Vector3f(static_cast<float>(x), static_cast<float>(y), 0));
117-
if (!is_inside) continue;
118-
119-
// 透视矫正插值
120-
auto perspective_result = PerformPerspectiveCorrection(
121-
p0.w, p1.w, p2.w,
122-
p0.z, p1.z, p2.z,
123-
bary);
124-
125-
const Vector3f& corrected_bary = perspective_result.corrected_barycentric;
126-
float z = perspective_result.interpolated_z;
127-
128-
Fragment frag; // material 指针由调用方填写
129-
frag.screen_coord = {x, y};
130-
frag.normal = Interpolate(v0.GetNormal(), v1.GetNormal(), v2.GetNormal(), corrected_bary);
131-
frag.uv = Interpolate(v0.GetTexCoords(), v1.GetTexCoords(), v2.GetTexCoords(), corrected_bary);
132-
frag.color = InterpolateColor(v0.GetColor(), v1.GetColor(), v2.GetColor(), corrected_bary);
133-
frag.depth = z;
134-
135-
out.push_back(frag);
136-
}
137-
}
138-
}
139-
140-
void Rasterizer::RasterizeTo(const VertexSoA& soa, size_t i0, size_t i1, size_t i2,
141-
int x0, int y0, int x1, int y1,
142-
std::vector<Fragment>& out) {
143-
// 读取三顶点的屏幕空间位置
144-
const Vector4f& p0 = soa.pos_screen[i0];
145-
const Vector4f& p1 = soa.pos_screen[i1];
146-
const Vector4f& p2 = soa.pos_screen[i2];
147-
148-
// 为BarycentricCoord预构造Vec3f,避免循环内重复构造
149-
const Vector3f sp0(p0.x, p0.y, p0.z);
150-
const Vector3f sp1(p1.x, p1.y, p1.z);
151-
const Vector3f sp2(p2.x, p2.y, p2.z);
152-
153-
// 计算屏幕空间AABB包围盒
154-
const float minx_f = std::max(0.0f, std::min({p0.x, p1.x, p2.x}));
155-
const float miny_f = std::max(0.0f, std::min({p0.y, p1.y, p2.y}));
156-
const float maxx_f = std::min(float(width_ - 1), std::max({p0.x, p1.x, p2.x}));
157-
const float maxy_f = std::min(float(height_ - 1), std::max({p0.y, p1.y, p2.y}));
158-
159-
// 与外部提供的裁剪区域相交(半开区间) -> 闭区间扫描
160-
int sx = std::max(x0, static_cast<int>(std::floor(minx_f)));
161-
int sy = std::max(y0, static_cast<int>(std::floor(miny_f)));
162-
int ex = std::min(x1 - 1, static_cast<int>(std::floor(maxx_f)));
163-
int ey = std::min(y1 - 1, static_cast<int>(std::floor(maxy_f)));
164-
if (sx > ex || sy > ey) return;
165-
166-
// 预计算边函数系数:E(x,y) = A*x + B*y + C
167-
// 使用相对坐标的边函数定义,避免大常数项导致的数值不稳定
168-
// 如使用绝对形式Ax+By+C会由于常数C的量级过大,造成浮点抵消,有效位丢失不稳定
169-
auto cross2 = [](float ax, float ay, float bx, float by) {
170-
return ax * by - ay * bx;
171-
};
172-
// 边向量
173-
const float e01x = p1.x - p0.x, e01y = p1.y - p0.y; // (p0->p1)
174-
const float e12x = p2.x - p1.x, e12y = p2.y - p1.y; // (p1->p2)
175-
const float e20x = p0.x - p2.x, e20y = p0.y - p2.y; // (p2->p0)
176-
177-
// 有向面积(两倍),用相对面积定义:area2 = cross(p1 - p0, p2 - p0)
178-
float area2 = cross2(e01x, e01y, p2.x - p0.x, p2.y - p0.y);
179-
if (std::abs(area2) < 1e-6f) return; // 退化三角形
180-
const float inv_area2 = 1.0f / area2;
181-
const bool positive = (area2 > 0.0f);
182-
183-
// 行优先遍历:有利于 cache 与向量化
184-
#pragma omp simd
185-
for (int y = sy; y <= ey; ++y) {
186-
const float yf = static_cast<float>(y);
187-
188-
// 注意:此处存在对 out.push_back 的写入,属于有副作用操作,不适合使用
189-
// omp simd 进行强制向量化,否则可能导致不符合预期的行为(如周期性伪影)。
190-
// 先保持标量内层,后续如切换为“直写像素回调”再考虑安全的 SIMD 化。
191-
for (int x = sx; x <= ex; ++x) {
192-
const float xf = static_cast<float>(x);
193-
194-
// 相对坐标边函数:
195-
// E01(p) = cross(p1 - p0, p - p0)
196-
// E12(p) = cross(p2 - p1, p - p1)
197-
// E20(p) = cross(p0 - p2, p - p2)
198-
const float E01 = cross2(e01x, e01y, xf - p0.x, yf - p0.y);
199-
const float E12 = cross2(e12x, e12y, xf - p1.x, yf - p1.y);
200-
const float E20 = cross2(e20x, e20y, xf - p2.x, yf - p2.y);
201-
202-
// 半空间测试(根据朝向选择符号)
203-
const bool inside = positive ? (E01 >= 0.0f && E12 >= 0.0f && E20 >= 0.0f)
204-
: (E01 <= 0.0f && E12 <= 0.0f && E20 <= 0.0f);
205-
if (!inside) continue;
206-
207-
// 重心权重映射:
208-
// b0 对应 v0,取与对边 (v1,v2) 的子面积 → E12
209-
// b1 对应 v1 → E20
210-
// b2 对应 v2 → E01
211-
const float b0 = E12 * inv_area2;
212-
const float b1 = E20 * inv_area2;
213-
const float b2 = E01 * inv_area2;
214-
const Vector3f bary(b0, b1, b2);
215-
216-
// 透视矫正插值
217-
auto perspective_result = PerformPerspectiveCorrection(
218-
p0.w, p1.w, p2.w,
219-
p0.z, p1.z, p2.z,
220-
bary);
221-
222-
const Vector3f& corrected_bary = perspective_result.corrected_barycentric;
223-
const float z = perspective_result.interpolated_z;
224-
225-
Fragment frag; // Note: material 指针由调用方填写
226-
frag.screen_coord = {x, y};
227-
frag.normal = Interpolate(soa.normal[i0], soa.normal[i1], soa.normal[i2], corrected_bary);
228-
frag.uv = Interpolate(soa.uv[i0], soa.uv[i1], soa.uv[i2], corrected_bary);
229-
frag.color = InterpolateColor(soa.color[i0], soa.color[i1], soa.color[i2], corrected_bary);
230-
frag.depth = z;
231-
232-
out.push_back(frag);
233-
}
234-
}
235-
}
236-
23784
std::pair<bool, Vector3f> Rasterizer::GetBarycentricCoord(const Vector3f& p0,
23885
const Vector3f& p1,
23986
const Vector3f& p2,

src/renderers/tile_based_renderer.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,14 @@ bool TileBasedRenderer::Render(const Model &model, const Shader &shader_in,
100100
std::unique_ptr<uint32_t[]> tile_color_buffer =
101101
std::make_unique<uint32_t[]>(grid_ctx.tile_size * grid_ctx.tile_size);
102102

103-
// 为每个 tile 分配可复用片段临时容器,容量按单 tile 上限预估
104-
std::vector<Fragment> scratch_fragments;
105-
scratch_fragments.reserve(grid_ctx.tile_size * grid_ctx.tile_size);
106-
107103
#pragma omp for schedule(static)
108104
for (size_t tile_id = 0; tile_id < total_tiles; ++tile_id) {
109105
// 按照 tile 进行光栅化(SoA)
110106
// 直接写入单份全局 framebuffer;不同 tile 不重叠,无需加锁
111107
RasterizeTile(tile_id, tile_triangles[tile_id], grid_ctx,
112108
tile_depth_buffer.get(), tile_color_buffer.get(),
113109
depthBuffer, colorBuffer, *shader, early_z_,
114-
&scratch_fragments, &tile_stats[tile_id]);
110+
&tile_stats[tile_id]);
115111
}
116112
}
117113
auto raster_end = std::chrono::high_resolution_clock::now();
@@ -129,7 +125,7 @@ bool TileBasedRenderer::Render(const Model &model, const Shader &shader_in,
129125
sum_shaded += s.shaded;
130126
}
131127
auto rate = [](uint64_t num, uint64_t den) -> double {
132-
if (den == 0) return 0.0; return double(num) / double(den) * 100.0;
128+
return (den == 0)?0.0:double(num) / double(den) * 100.0;
133129
};
134130
SPDLOG_DEBUG(
135131
"TBR Mask Stats: tested={}, covered={} ({:.1f}%), zpass={} ({:.1f}%), shaded={} ({:.1f}%)",
@@ -220,7 +216,6 @@ void TileBasedRenderer::RasterizeTile(
220216
uint32_t *tile_color_buffer, std::unique_ptr<float[]> &global_depth_buffer,
221217
std::unique_ptr<uint32_t[]> &global_color_buffer,
222218
const Shader &shader, bool use_early_z,
223-
std::vector<Fragment> *scratch_fragments,
224219
TileMaskStats* out_stats) {
225220
// 计算 tile 屏幕范围
226221
size_t tile_x = tile_id % grid.tiles_x;

0 commit comments

Comments
 (0)