You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The refactored rendering code collects all rays upfront in a vector before processing, which could lead to high memory usage for large scenes or high sample counts. Consider if a chunked approach might be more memory-efficient.
letmut d:Vec<(usize,usize,Ray)> = Vec::with_capacity(pixels_count);for j in(0..self.settings.height).rev(){for i in0..self.settings.width{for sample in0..self.settings.samples_per_pixel{let(dx, dy) = if(sample asusize) < cmj_samples.len(){
cmj_samples[sample asusize]}else{(utils::random(), utils::random())};let u = (i asf32 + dx) / (self.settings.width - 1)asf32;let v = (j asf32 + dy) / (self.settings.height - 1)asf32;let ray = self.camera.get_ray(u, v);
d.push((i, j, ray));}}}
The progress bar is declared but not initialized with a value. The code references 'bar' on line 40 but the initialization is missing, which could cause a compilation error.
let bar = ProgressBar::new(pixels_count asu64);
bar.set_style(
The memory usage is excessive because all rays are stored in memory before processing. For high-resolution renders with many samples, this could lead to out-of-memory errors. Consider processing rays in batches or chunks to maintain parallelism while reducing memory overhead.
-let mut d: Vec<(usize, usize, Ray)> = Vec::with_capacity(pixels_count);+let chunk_size = 1024; // Process rays in manageable chunks+let mut buffer = Buffer::new(self.settings.width, self.settings.height);+
for j in (0..self.settings.height).rev() {
- for i in 0..self.settings.width {- for sample in 0..self.settings.samples_per_pixel {- let (dx, dy) = if (sample as usize) < cmj_samples.len() {- cmj_samples[sample as usize]- } else {- (utils::random(), utils::random())- };-- let u = (i as f32 + dx) / (self.settings.width - 1) as f32;- let v = (j as f32 + dy) / (self.settings.height - 1) as f32;-- let ray = self.camera.get_ray(u, v);- d.push((i, j, ray));+ for i_chunk in (0..self.settings.width).step_by(chunk_size) {+ let mut rays = Vec::with_capacity(chunk_size * self.settings.samples_per_pixel as usize);++ let i_end = (i_chunk + chunk_size).min(self.settings.width);+ for i in i_chunk..i_end {+ for sample in 0..self.settings.samples_per_pixel {+ let (dx, dy) = if (sample as usize) < cmj_samples.len() {+ cmj_samples[sample as usize]+ } else {+ (utils::random(), utils::random())+ };++ let u = (i as f32 + dx) / (self.settings.width - 1) as f32;+ let v = (j as f32 + dy) / (self.settings.height - 1) as f32;++ let ray = self.camera.get_ray(u, v);+ rays.push((i, j, ray));+ }+ }++ // Process this chunk in parallel+ let colors: Vec<(usize, usize, Vec3A)> = rays+ .into_par_iter()+ .map(|(i, j, r)| {+ let col = ray_color(+ &r,+ &self.world,+ &self.lights,+ self.settings.max_depth as i32,+ );+ bar.inc(1);+ (i, j, col)+ })+ .collect();++ // Accumulate colors for this chunk+ for (i, j, col) in colors {+ buffer.set_mut_pixel(i, j, col / self.settings.samples_per_pixel as f32);
}
}
}
+bar.finish();+return buffer;+
Apply this suggestion
Suggestion importance[1-10]: 8
__
Why: This suggestion addresses a significant memory optimization issue. Processing all rays at once could lead to excessive memory usage for high-resolution renders. The chunked approach maintains parallelism while significantly reducing peak memory consumption, which could prevent out-of-memory errors in production.
Medium
More
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Type
Enhancement
Description
Refactored rendering logic for improved parallelism and efficiency
Added
set_mut_pixelmethod to buffer for color accumulationSimplified and updated sample scene RON file structure
framesettingMinor code formatting and clarity improvements
Changes walkthrough 📝
buffer.rs
Add pixel color accumulation method to buffercrates/crust-render/src/buffer.rs
set_mut_pixelmethod for in-place pixel color accumulationtracer.rs
Refactor rendering for parallelism and color accumulationcrates/crust-render/src/tracer.rs
set_mut_pixelfor accumulating color per pixelteapot.ron
Simplify and update sample scene RON structuresamples/teapot.ron
smooth: trueto teapot objectframesetting to settingsinstance.rs
Code formatting for normal transformationcrates/crust-render/src/instance.rs
main.rs
Minor formatting in render mode selectioncrates/crust-render/src/main.rs