@@ -25,6 +25,16 @@ use crate::renderers::tile_common::{
2525use crate :: renderers:: Renderer ;
2626use crate :: shader:: Shader ;
2727
28+ /// Result of rasterizing a single tile.
29+ struct TileResult {
30+ depth : Vec < f32 > ,
31+ color : Vec < u32 > ,
32+ screen_x : usize ,
33+ screen_y : usize ,
34+ width : usize ,
35+ height : usize ,
36+ }
37+
2838/// SoA tile-based renderer with edge function rasterization and optional Early-Z.
2939///
3040/// Mirrors C++ `TileBasedRenderer`: vertex transform → tile binning →
@@ -79,8 +89,8 @@ impl Renderer for TileBasedRenderer {
7989 let t = Instant :: now ( ) ;
8090 // 3. Setup tile grid
8191 let tile_size = self . tile_size ;
82- let tiles_x = ( width + tile_size - 1 ) / tile_size ;
83- let tiles_y = ( height + tile_size - 1 ) / tile_size ;
92+ let tiles_x = width. div_ceil ( tile_size) ;
93+ let tiles_y = height. div_ceil ( tile_size) ;
8494
8595 let grid = TileGridContext {
8696 soa,
@@ -107,7 +117,7 @@ impl Renderer for TileBasedRenderer {
107117 let total_tiles = tiles_x * tiles_y;
108118 let early_z = self . early_z ;
109119
110- let tile_results: Vec < ( Vec < f32 > , Vec < u32 > , usize , usize , usize , usize ) > = ( 0 ..total_tiles)
120+ let tile_results: Vec < TileResult > = ( 0 ..total_tiles)
111121 . into_par_iter ( )
112122 . map ( |tile_id| {
113123 let tile_x = tile_id % tiles_x;
@@ -139,29 +149,29 @@ impl Renderer for TileBasedRenderer {
139149 height,
140150 ) ;
141151
142- (
143- tile_depth,
144- tile_color,
145- screen_x_start,
146- screen_y_start,
147- tile_width,
148- tile_height,
149- )
152+ TileResult {
153+ depth : tile_depth,
154+ color : tile_color,
155+ screen_x : screen_x_start,
156+ screen_y : screen_y_start,
157+ width : tile_width,
158+ height : tile_height,
159+ }
150160 } )
151161 . collect ( ) ;
152162
153163 let raster_ms = t. elapsed ( ) . as_secs_f64 ( ) * 1000.0 ;
154164
155165 // 7. Copy tile results to global framebuffer
156166 let t = Instant :: now ( ) ;
157- for ( tile_depth , tile_color , sx , sy , tw , th ) in & tile_results {
158- for y in 0 ..* th {
159- let tile_row_off = y * tw ;
160- let global_row_off = ( sy + y) * width + sx ;
161- global_color[ global_row_off..global_row_off + tw ]
162- . copy_from_slice ( & tile_color [ tile_row_off..tile_row_off + tw ] ) ;
163- global_depth[ global_row_off..global_row_off + tw ]
164- . copy_from_slice ( & tile_depth [ tile_row_off..tile_row_off + tw ] ) ;
167+ for tile in & tile_results {
168+ for y in 0 ..tile . height {
169+ let tile_row_off = y * tile . width ;
170+ let global_row_off = ( tile . screen_y + y) * width + tile . screen_x ;
171+ global_color[ global_row_off..global_row_off + tile . width ]
172+ . copy_from_slice ( & tile . color [ tile_row_off..tile_row_off + tile . width ] ) ;
173+ global_depth[ global_row_off..global_row_off + tile . width ]
174+ . copy_from_slice ( & tile . depth [ tile_row_off..tile_row_off + tile . width ] ) ;
165175 }
166176 }
167177 let copy_ms = t. elapsed ( ) . as_secs_f64 ( ) * 1000.0 ;
0 commit comments