Skip to content

Commit 6872f7b

Browse files
committed
style: update
Signed-off-by: Niu Zhihong <zhihong@nzhnb.com>
1 parent 5d99073 commit 6872f7b

4 files changed

Lines changed: 34 additions & 35 deletions

File tree

simple_renderer/src/material.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,14 @@ use crate::error::{RendererError, Result};
55
use crate::math::Vec3;
66

77
/// An image texture with pixel data stored in a flat `Vec<u8>`.
8-
#[derive(Debug, Clone)]
8+
#[derive(Debug, Clone, Default)]
99
pub struct Texture {
1010
pub data: Vec<u8>,
1111
pub width: u32,
1212
pub height: u32,
1313
pub channels: u32,
1414
}
1515

16-
impl Default for Texture {
17-
fn default() -> Self {
18-
Self {
19-
data: Vec::new(),
20-
width: 0,
21-
height: 0,
22-
channels: 0,
23-
}
24-
}
25-
}
26-
2716
impl Texture {
2817
/// Load a texture from an image file (PNG, JPEG, BMP, TGA).
2918
pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Texture> {

simple_renderer/src/renderers/tile_based.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ use crate::renderers::tile_common::{
2525
use crate::renderers::Renderer;
2626
use 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;

simple_renderer/src/renderers/tile_based_deferred.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ impl Renderer for TileBasedDeferredRenderer {
7979
let t = Instant::now();
8080
// 3. Setup tile grid
8181
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;
82+
let tiles_x = width.div_ceil(tile_size);
83+
let tiles_y = height.div_ceil(tile_size);
8484

8585
let grid = TileGridContext {
8686
soa,

simple_renderer/src/shader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,9 @@ impl Shader {
443443
values.fill(1.0);
444444
return SpecularLut { values };
445445
}
446-
for i in 0..SPECULAR_LUT_RESOLUTION {
446+
for (i, value) in values.iter_mut().enumerate().take(SPECULAR_LUT_RESOLUTION) {
447447
let cos_theta = i as f32 / (SPECULAR_LUT_RESOLUTION - 1) as f32;
448-
values[i] = if cos_theta <= 0.0 {
448+
*value = if cos_theta <= 0.0 {
449449
0.0
450450
} else {
451451
cos_theta.powf(shininess)

0 commit comments

Comments
 (0)