Free as in Freedom: Codeberg.org. Create your repos!
Browse Source

Removing the bottleneck for HaltonSampler, using lazy_static crate.

tags/v0.5.1
Jan Walter 2 months ago
parent
commit
863cbedf73
5 changed files with 17 additions and 15 deletions
  1. 1
    0
      Cargo.lock
  2. 1
    0
      Cargo.toml
  3. 2
    0
      src/lib.rs
  4. 11
    13
      src/samplers/halton.rs
  5. 2
    2
      src/samplers/sobol.rs

+ 1
- 0
Cargo.lock View File

@@ -512,6 +512,7 @@ dependencies = [
512 512
  "half 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
513 513
  "hexf 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
514 514
  "image 0.20.1 (registry+https://github.com/rust-lang/crates.io-index)",
515
+ "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
515 516
  "num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
516 517
  "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
517 518
  "openexr 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",

+ 1
- 0
Cargo.toml View File

@@ -16,6 +16,7 @@ getopts="0.2.15"
16 16
 half="1"
17 17
 hexf = "0.1.0"
18 18
 image="*"
19
+lazy_static = "1.2.0"
19 20
 num="*"
20 21
 num_cpus = "1.2"
21 22
 openexr = { version = "0.6", optional = true }

+ 2
- 0
src/lib.rs View File

@@ -27,6 +27,8 @@ extern crate byteorder;
27 27
 #[cfg(feature = "openexr")]
28 28
 extern crate half;
29 29
 extern crate image;
30
+#[macro_use]
31
+extern crate lazy_static;
30 32
 extern crate num;
31 33
 #[cfg(feature = "openexr")]
32 34
 extern crate openexr;

+ 11
- 13
src/samplers/halton.rs View File

@@ -12,6 +12,16 @@ use core::pbrt::Float;
12 12
 use core::rng::Rng;
13 13
 use core::sampler::{GlobalSampler, Sampler};
14 14
 
15
+/// Generate random digit permutations for Halton sampler
16
+lazy_static! {
17
+    #[derive(Debug)]
18
+    static ref RADICAL_INVERSE_PERMUTATIONS: Vec<u16> = {
19
+        let mut rng: Rng = Rng::new();
20
+        let radical_inverse_permutations: Vec<u16> = compute_radical_inverse_permutations(&mut rng);
21
+        radical_inverse_permutations
22
+    };
23
+}
24
+
15 25
 // see halton.h
16 26
 
17 27
 pub const K_MAX_RESOLUTION: i32 = 128_i32;
@@ -39,7 +49,6 @@ fn extended_gcd(a: u64, b: u64, x: &mut i64, y: &mut i64) {
39 49
 
40 50
 pub struct HaltonSampler {
41 51
     pub samples_per_pixel: i64,
42
-    pub radical_inverse_permutations: Vec<u16>,
43 52
     pub base_scales: Point2i,
44 53
     pub base_exponents: Point2i,
45 54
     pub sample_stride: u64,
@@ -69,11 +78,6 @@ impl HaltonSampler {
69 78
         sample_bounds: Bounds2i,
70 79
         sample_at_pixel_center: bool,
71 80
     ) -> Self {
72
-        // generate random digit permutations for Halton sampler
73
-        // if (radical_Inverse_Permutations.empty()) {
74
-        let mut rng: Rng = Rng::new();
75
-        let radical_inverse_permutations: Vec<u16> = compute_radical_inverse_permutations(&mut rng);
76
-        // }
77 81
         // find radical inverse base scales and exponents that cover sampling area
78 82
         let res: Vector2i = sample_bounds.p_max - sample_bounds.p_min;
79 83
         let mut base_scales: Point2i = Point2i::default();
@@ -103,7 +107,6 @@ impl HaltonSampler {
103 107
         ];
104 108
         HaltonSampler {
105 109
             samples_per_pixel: samples_per_pixel,
106
-            radical_inverse_permutations: radical_inverse_permutations.iter().cloned().collect(),
107 110
             base_scales: base_scales,
108 111
             base_exponents: base_exponents,
109 112
             sample_stride: sample_stride,
@@ -176,7 +179,7 @@ impl HaltonSampler {
176 179
                 PRIME_TABLE_SIZE
177 180
             );
178 181
         }
179
-        &self.radical_inverse_permutations[PRIME_SUMS[dim as usize] as usize..]
182
+        &RADICAL_INVERSE_PERMUTATIONS[PRIME_SUMS[dim as usize] as usize..]
180 183
     }
181 184
 }
182 185
 
@@ -310,11 +313,6 @@ impl Clone for HaltonSampler {
310 313
         let offset_for_current_pixel: u64 = *self.offset_for_current_pixel.read().unwrap();
311 314
         HaltonSampler {
312 315
             samples_per_pixel: self.samples_per_pixel,
313
-            radical_inverse_permutations: self
314
-                .radical_inverse_permutations
315
-                .iter()
316
-                .cloned()
317
-                .collect(),
318 316
             base_scales: self.base_scales,
319 317
             base_exponents: self.base_exponents,
320 318
             sample_stride: self.sample_stride,

+ 2
- 2
src/samplers/sobol.rs View File

@@ -211,8 +211,8 @@ impl Sampler for SobolSampler {
211 211
 impl GlobalSampler for SobolSampler {
212 212
     fn set_sample_number(&mut self, sample_num: i64) -> bool {
213 213
         // reset array offsets for next pixel sample
214
-        self.array_1d_offset = 0;
215
-        self.array_2d_offset = 0;
214
+        self.array_1d_offset = 0_usize;
215
+        self.array_2d_offset = 0_usize;
216 216
         self.current_pixel_sample_index = sample_num;
217 217
         self.current_pixel_sample_index < self.samples_per_pixel
218 218
     }

Loading…
Cancel
Save