Browse Source

Fix casting (after integer overflow) in hash(...) function.

tags/v0.5.1
Jan Walter 6 months ago
parent
commit
fe5ae69281
3 changed files with 26 additions and 4 deletions
  1. 2
    0
      Makefile
  2. 19
    0
      examples/integrators_sspm_hash.rs
  3. 5
    4
      src/integrators/sppm.rs

+ 2
- 0
Makefile View File

@@ -52,10 +52,12 @@ examples: release
./target/release/examples/geometry_spherical_direction_vec3
./target/release/examples/geometry_vector2_null
./target/release/examples/geometry_vector3_null
./target/release/examples/integrators_sspm_hash.rs
./target/release/examples/lights_diffuse_area_light_new
./target/release/examples/lights_distant_light_new
./target/release/examples/lights_infinite_area_light_new
./target/release/examples/lights_point_light_new
./target/release/examples/parse_ass_file.rs
./target/release/examples/pbrt_spheres_differentials_texfilt
./target/release/examples/pbrt_teapot_area_light
./target/release/examples/pest_test

+ 19
- 0
examples/integrators_sspm_hash.rs View File

@@ -0,0 +1,19 @@
extern crate pbrt;

use pbrt::core::geometry::Point3i;

fn hash(p: &Point3i, hash_size: i32) -> usize {
(((p.x * 73856093) ^ (p.y * 19349663) ^ (p.z * 83492791)) as u32 % hash_size as u32) as usize
}

fn main() {
let photon_grid_index: Point3i = Point3i {
x: 225,
y: 0,
z: 267,
};
let hash_size: usize = 700000;
let h: usize = hash(&photon_grid_index, hash_size as i32);
assert!(h < hash_size, "hash({:?}, {:?})", photon_grid_index, hash_size);
println!("hash({:?}, {:?}) = {}", photon_grid_index, hash_size, h);
}

+ 5
- 4
src/integrators/sppm.rs View File

@@ -108,8 +108,8 @@ fn to_grid(p: &Point3f, bounds: &Bounds3f, grid_res: &[i32; 3], pi: &mut Point3i
in_bounds
}

fn hash(p: &Point3i, hash_size: usize) -> usize {
(((p.x * 73856093) ^ (p.y * 19349663) ^ (p.z * 83492791)) % hash_size as i32) as usize
fn hash(p: &Point3i, hash_size: i32) -> usize {
(((p.x * 73856093) ^ (p.y * 19349663) ^ (p.z * 83492791)) as u32 % hash_size as u32) as usize
}

/// **Main function** to **render** a scene multi-threaded (using all
@@ -382,7 +382,7 @@ pub fn render_sppm(
for y in p_min.y..p_max.y {
for x in p_min.x..p_max.x {
// add visible point to grid cell $(x, y, z)$
let h: usize = hash(&Point3i { x: x, y: y, z: z }, hash_size);
let h: usize = hash(&Point3i { x: x, y: y, z: z }, hash_size as i32);
let mut node_arc: Arc<SPPMPixelListNode> =
Arc::new(SPPMPixelListNode::default());
let pixel_clone: Arc<SPPMPixel> = pixel.clone();
@@ -471,8 +471,9 @@ pub fn render_sppm(
&grid_res,
&mut photon_grid_index,
) {
let h: usize = hash(&photon_grid_index, hash_size);
let h: usize = hash(&photon_grid_index, hash_size as i32);
// add photon contribution to visible points in _grid[h]_
assert!(h < hash_size, "hash({:?}, {:?})", photon_grid_index, hash_size);
if let Some(root) = grid[h].get() {
let mut node: &SPPMPixelListNode = root;
loop {

Loading…
Cancel
Save