Skip to content

Commit 258607a

Browse files
committed
TBR: Optimize global buffer write-back logic
Signed-off-by: ZhouFANG <indevn@outlook.com>
1 parent 0549211 commit 258607a

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

src/renderer.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ void SimpleRenderer::RasterizeTile(
536536
size_t tile_width = screen_x_end - screen_x_start;
537537
size_t tile_height = screen_y_end - screen_y_start;
538538
std::fill_n(tile_depth_buffer, tile_width * tile_height, 1.0f);
539-
std::fill_n(tile_color_buffer, tile_width * tile_height, 0);
539+
std::fill_n(tile_color_buffer, tile_width * tile_height, 0); // 默认背景色为0/黑色
540540

541541
for (const auto &tri : triangles) { // 用来应对scratch传入nullptr的情况
542542
// 始终走 SoA + 限制矩形的光栅化路径;如未提供 scratch,则使用函数内局部容器
@@ -579,15 +579,21 @@ void SimpleRenderer::RasterizeTile(
579579
}
580580

581581
// 写回全局缓冲
582+
// TBR 下不同 tile 覆盖的屏幕区域互不重叠,且在 tile 内部已通过 Early‑Z
583+
// 得出每个像素的最终值。因此可以直接将 tile 行数据拷贝到全局缓冲
582584
for (size_t y = 0; y < tile_height; y++) {
583-
for (size_t x = 0; x < tile_width; x++) {
584-
size_t tile_index = x + y * tile_width;
585-
size_t global_index = (screen_x_start + x) + (screen_y_start + y) * width_;
586-
if (tile_depth_buffer[tile_index] < global_depth_buffer[global_index]) {
587-
global_depth_buffer[global_index] = tile_depth_buffer[tile_index];
588-
global_color_buffer[global_index] = tile_color_buffer[tile_index];
589-
}
590-
}
585+
const size_t tile_row_off = y * tile_width;
586+
const size_t global_row_off = (screen_y_start + y) * width_ + screen_x_start;
587+
588+
// 拷贝本行 color 到全局 color
589+
std::memcpy(global_color_buffer.get() + global_row_off,
590+
tile_color_buffer + tile_row_off,
591+
tile_width * sizeof(uint32_t));
592+
593+
// 拷贝本行 depth 到全局 depth
594+
std::memcpy(global_depth_buffer.get() + global_row_off,
595+
tile_depth_buffer + tile_row_off,
596+
tile_width * sizeof(float));
591597
}
592598
}
593599

0 commit comments

Comments
 (0)