Blender V5.3
render_scheduler.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6
7#include "scene/integrator.h"
8
9#include "session/session.h"
10#include "session/tile.h"
11
12#include "util/log.h"
13#include "util/time.h"
14
16
17/* --------------------------------------------------------------------
18 * Render scheduler.
19 */
20
22 : headless_(params.headless),
23 background_(params.background),
24 pixel_size_(params.pixel_size),
25 tile_manager_(tile_manager),
26 default_start_resolution_divider_(params.use_resolution_divider ? pixel_size_ * 8 : 0)
27{
29}
30
31void RenderScheduler::set_need_schedule_cryptomatte(bool need_schedule_cryptomatte)
32{
33 need_schedule_cryptomatte_ = need_schedule_cryptomatte;
34}
35
36void RenderScheduler::set_need_schedule_rebalance(bool need_schedule_rebalance)
37{
38 need_schedule_rebalance_works_ = need_schedule_rebalance;
39}
40
42{
43 return background_;
44}
45
47{
48 denoiser_params_ = params;
49
51 state_.resolution_divider = pixel_size_;
52 }
53}
54
59
61{
64 }
65 else {
66 limit_samples_per_update_ = limit_samples;
67 }
68}
69
71{
72 adaptive_sampling_ = adaptive_sampling;
73}
74
79
80void RenderScheduler::set_sample_params(const int num_samples,
81 const bool use_sample_subset,
82 const int sample_subset_offset,
83 const int sample_subset_length)
84{
87
88 if (use_sample_subset) {
89 sample_offset_ = sample_subset_offset;
91 min(sample_subset_offset + sample_subset_length, num_samples_) - sample_subset_offset, 0);
92 }
93}
94
96{
97 return num_samples_;
98}
99
101{
102 return sample_offset_;
103}
104
105void RenderScheduler::set_time_limit(const double time_limit)
106{
107 time_limit_ = time_limit;
108}
109
111{
112 return time_limit_;
113}
114
121
123{
124 return state_.num_rendered_samples;
125}
126
128{
129 return state_.total_rendered_samples;
130}
131
132void RenderScheduler::reset(const BufferParams &buffer_params)
133{
134 buffer_params_ = buffer_params;
135
137
138 /* In background mode never do lower resolution render preview, as it is not really supported
139 * by the software. */
141 state_.resolution_divider = 1;
142 }
143 else {
144 state_.user_is_navigating = true;
145 state_.resolution_divider = start_resolution_divider_;
146 }
147
148 state_.num_rendered_samples = 0;
149 state_.total_rendered_samples = 0;
150 state_.last_display_update_time = 0.0;
151 state_.last_display_update_sample = -1;
152
153 state_.last_rebalance_time = 0.0;
154 state_.num_rebalance_requested = 0;
155 state_.num_rebalance_changes = 0;
156 state_.last_rebalance_changed = false;
157 state_.need_rebalance_at_next_work = false;
158
159 /* TODO(sergey): Choose better initial value. */
160 /* NOTE: The adaptive sampling settings might not be available here yet. */
161 state_.adaptive_sampling_threshold = 0.4f;
162
163 state_.last_work_tile_was_denoised = false;
164 state_.tile_result_was_written = false;
165 state_.postprocess_work_scheduled = false;
166 state_.full_frame_work_scheduled = false;
167 state_.full_frame_was_written = false;
168
169 state_.path_trace_finished = false;
170
171 state_.start_render_time = 0.0;
172 state_.end_render_time = 0.0;
173 state_.time_limit_reached = false;
174
175 state_.occupancy_num_samples = 0;
176 state_.occupancy = 1.0f;
177
178 first_render_time_.path_trace_per_sample = 0.0;
179 first_render_time_.denoise_time = 0.0;
180 first_render_time_.display_update_time = 0.0;
181
188}
189
194
196{
197 /* Move to the next resolution divider. Assume adaptive filtering is not needed during
198 * navigation. */
199 if (state_.resolution_divider != pixel_size_) {
200 return false;
201 }
202
203 if (render_work_reschedule_on_idle(render_work)) {
204 return true;
205 }
206
207 state_.path_trace_finished = true;
208
209 bool denoiser_delayed;
210 bool denoiser_ready_to_display;
211 render_work.tile.denoise = work_need_denoise(denoiser_delayed, denoiser_ready_to_display);
212
213 render_work.display.update = work_need_update_display(denoiser_delayed);
214 render_work.display.use_denoised_result = denoiser_ready_to_display;
215
216 return false;
217}
218
220{
222 return false;
223 }
224
225 /* Move to the next resolution divider. Assume adaptive filtering is not needed during
226 * navigation. */
227 if (state_.resolution_divider != pixel_size_) {
228 return false;
229 }
230
232 if (state_.adaptive_sampling_threshold > adaptive_sampling_.threshold) {
233 state_.adaptive_sampling_threshold = max(state_.adaptive_sampling_threshold / 2,
235
236 render_work.adaptive_sampling.threshold = state_.adaptive_sampling_threshold;
237 render_work.adaptive_sampling.reset = true;
238
239 return true;
240 }
241 }
242
243 return false;
244}
245
247{
248 LOG_DEBUG << "Schedule work for cancel.";
249
250 /* Un-schedule samples: they will not be rendered and should not be counted. */
251 state_.num_rendered_samples -= render_work.path_trace.num_samples;
252 state_.total_rendered_samples -= render_work.path_trace.num_samples;
253
254 const bool has_rendered_samples = get_num_rendered_samples() != 0;
255
256 /* Reset all fields of the previous work, canceling things like adaptive sampling filtering and
257 * denoising.
258 * However, need to preserve write requests, since those will not be possible to recover and
259 * writes are only to happen once. */
260 const bool tile_write = render_work.tile.write;
261 const bool full_write = render_work.full.write;
262
263 render_work = RenderWork();
264
265 render_work.tile.write = tile_write;
266 render_work.full.write = full_write;
267
268 /* Do not write tile if it has zero samples in it, treat it similarly to all other tiles which
269 * got canceled. */
270 if (!state_.tile_result_was_written && has_rendered_samples) {
271 render_work.tile.write = true;
272 }
273
274 if (!state_.full_frame_was_written) {
275 render_work.full.write = true;
276 }
277
278 /* Update current tile, but only if any sample was rendered.
279 * Allows to have latest state of tile visible while full buffer is being processed.
280 *
281 * Note that if there are no samples in the current tile its render buffer might have pixels
282 * remained from previous state.
283 *
284 * If the full result was written, then there is no way any updates were made to the render
285 * buffers. And the buffers might have been freed from the device, so display update is not
286 * possible. */
287 if (has_rendered_samples && !state_.full_frame_was_written) {
288 render_work.display.update = true;
289 }
290}
291
293{
294 if (state_.resolution_divider != pixel_size_) {
295 return false;
296 }
297
298 if (state_.path_trace_finished || state_.time_limit_reached) {
299 return true;
300 }
301
303}
304
306{
308
309 const double time_now = time_dt();
310
311 if (done()) {
312 RenderWork render_work;
313 render_work.resolution_divider = state_.resolution_divider;
314 render_work.denoised_resolution_divider = state_.resolution_divider;
315 if (denoiser_params_.use) {
317 }
318
319 if (!set_postprocess_render_work(&render_work)) {
320 set_full_frame_render_work(&render_work);
321 }
322
323 if (!render_work) {
324 state_.end_render_time = time_now;
325 }
326
327 update_state_for_render_work(render_work);
328
329 return render_work;
330 }
331
332 RenderWork render_work;
333
334 if (state_.resolution_divider != pixel_size_) {
335 if (state_.user_is_navigating) {
336 /* Don't progress the resolution divider as the user is currently navigating in the scene. */
337 state_.user_is_navigating = false;
338 }
339 else if (default_start_resolution_divider_ != 0) {
340 /* If the resolution divider is greater than or equal to default_start_resolution_divider_,
341 * drop the resolution divider down to 4. This is so users with slow hardware and thus high
342 * resolution dividers (E.G. 16), get an update to let them know something is happening
343 * rather than having to wait for the full 1:1 render to show up. */
344 state_.resolution_divider = state_.resolution_divider > default_start_resolution_divider_ ?
345 (4 * pixel_size_) :
346 1;
347 }
348
349 state_.resolution_divider = max(state_.resolution_divider, pixel_size_);
350 state_.num_rendered_samples = 0;
351 state_.total_rendered_samples = 0;
352 state_.last_display_update_sample = -1;
353 }
354
355 render_work.resolution_divider = state_.resolution_divider;
356 render_work.denoised_resolution_divider = state_.resolution_divider;
357 if (denoiser_params_.use) {
359 }
361 state_.num_rendered_samples = 0;
362 }
363
364 render_work.path_trace.start_sample = get_start_sample_to_path_trace();
365 render_work.path_trace.num_samples = get_num_samples_to_path_trace();
366 render_work.path_trace.sample_offset = get_sample_offset();
367
368 render_work.init_render_buffers = (render_work.path_trace.start_sample == get_sample_offset());
369
370 /* NOTE: Rebalance scheduler requires current number of samples to not be advanced forward. */
371 render_work.rebalance = work_need_rebalance();
372
373 /* NOTE: Advance number of samples now, so that filter and denoising check can see that all the
374 * samples are rendered. */
375 state_.num_rendered_samples += render_work.path_trace.num_samples;
376 state_.total_rendered_samples += render_work.path_trace.num_samples;
377
378 render_work.adaptive_sampling.filter = work_need_adaptive_filter();
379 render_work.adaptive_sampling.threshold = work_adaptive_threshold();
380 render_work.adaptive_sampling.reset = false;
381
382 /* Denoise volume guiding at power of two samples, but not needed when done. */
383 render_work.volume_guiding_denoise = is_power_of_two(state_.num_rendered_samples) && !done();
384
385 bool denoiser_delayed;
386 bool denoiser_ready_to_display;
387 render_work.tile.denoise = work_need_denoise(denoiser_delayed, denoiser_ready_to_display);
388
389 render_work.tile.write = done();
390
391 render_work.display.update = work_need_update_display(denoiser_delayed);
392 render_work.display.use_denoised_result = denoiser_ready_to_display;
393
394 if (done()) {
395 set_postprocess_render_work(&render_work);
396 }
397
398 update_state_for_render_work(render_work);
399
400 return render_work;
401}
402
404{
405 const double time_now = time_dt();
406
407 if (render_work.rebalance) {
408 state_.last_rebalance_time = time_now;
409 ++state_.num_rebalance_requested;
410 }
411
412 /* A fallback display update time, for the case there is an error of display update, or when
413 * there is no display at all. */
414 if (render_work.display.update) {
415 state_.last_display_update_time = time_now;
416 state_.last_display_update_sample = state_.num_rendered_samples;
417 }
418
419 state_.last_work_tile_was_denoised = render_work.tile.denoise;
420 state_.tile_result_was_written |= render_work.tile.write;
421 state_.full_frame_was_written |= render_work.full.write;
422}
423
425{
426 if (state_.postprocess_work_scheduled) {
427 return false;
428 }
429 state_.postprocess_work_scheduled = true;
430
431 bool any_scheduled = false;
432
434 render_work->cryptomatte.postprocess = true;
435 any_scheduled = true;
436 }
437
438 if (denoiser_params_.use && !state_.last_work_tile_was_denoised) {
439 render_work->tile.denoise = !tile_manager_.has_multiple_tiles();
440 any_scheduled = true;
441 }
442
443 if (!state_.tile_result_was_written) {
444 render_work->tile.write = true;
445 any_scheduled = true;
446 }
447
448 if (any_scheduled) {
449 render_work->display.update = true;
450 }
451
452 return any_scheduled;
453}
454
456{
457 if (state_.full_frame_work_scheduled) {
458 return;
459 }
460
462 /* There is only single tile, so all work has been performed already. */
463 return;
464 }
465
466 if (!tile_manager_.done()) {
467 /* There are still tiles to be rendered. */
468 return;
469 }
470
471 if (state_.full_frame_was_written) {
472 return;
473 }
474
475 state_.full_frame_work_scheduled = true;
476
477 render_work->full.write = true;
478}
479
480/* Knowing time which it took to complete a task at the current resolution divider approximate how
481 * long it would have taken to complete it at a final resolution. */
482static double approximate_final_time(const RenderWork &render_work, const double time)
483{
484 if (render_work.resolution_divider == 1) {
485 return time;
486 }
487
488 const double resolution_divider_sq = render_work.resolution_divider *
489 render_work.resolution_divider;
490 return time * resolution_divider_sq;
491}
492
494{
495 /* Start counting render time when rendering samples at their final resolution.
496 *
497 * NOTE: The work might have the path trace part be all zero: this happens when a post-processing
498 * work is scheduled after the path tracing. Check for whether path tracing is actually happening
499 * as it is expected to happen in the first work. */
500 if (render_work.resolution_divider == pixel_size_ && render_work.path_trace.num_samples != 0 &&
501 state_.start_render_time == 0.0)
502 {
503 state_.start_render_time = time_dt();
504 }
505}
506
508 const double time,
509 bool is_cancelled)
510{
512
513 if (is_cancelled) {
514 return;
515 }
516
517 const double final_time_approx = approximate_final_time(render_work, time);
518
520 first_render_time_.path_trace_per_sample = final_time_approx /
521 render_work.path_trace.num_samples;
522 }
523
524 if (work_report_reset_average(render_work)) {
526 }
527
528 path_trace_time_.add_average(final_time_approx, render_work.path_trace.num_samples);
529
530 LOG_DEBUG << "Average path tracing time: " << path_trace_time_.get_average() << " seconds.";
531}
532
534 const float occupancy)
535{
536 state_.occupancy_num_samples = render_work.path_trace.num_samples;
537 state_.occupancy = occupancy;
538 LOG_DEBUG << "Measured path tracing occupancy: " << occupancy;
539}
540
542 const double time,
543 bool is_cancelled)
544{
546
547 if (is_cancelled) {
548 return;
549 }
550
551 const double final_time_approx = approximate_final_time(render_work, time);
552
553 if (work_report_reset_average(render_work)) {
555 }
556
557 adaptive_filter_time_.add_average(final_time_approx, render_work.path_trace.num_samples);
558
559 LOG_DEBUG << "Average adaptive sampling filter time: " << adaptive_filter_time_.get_average()
560 << " seconds.";
561}
562
563void RenderScheduler::report_denoise_time(const RenderWork &render_work, const double time)
564{
566
567 const double final_time_approx = approximate_final_time(render_work, time);
568
570 first_render_time_.denoise_time = final_time_approx;
571 }
572
573 if (work_report_reset_average(render_work)) {
575 }
576
577 denoise_time_.add_average(final_time_approx);
578
579 LOG_DEBUG << "Average denoising time: " << denoise_time_.get_average() << " seconds.";
580}
581
583 const double time)
584{
586
587 const double final_time_approx = approximate_final_time(render_work, time);
588
589 if (work_report_reset_average(render_work)) {
591 }
592
593 volume_guiding_denoise_time_.add_average(final_time_approx, render_work.path_trace.num_samples);
594
595 LOG_DEBUG << "Average volume guiding denoising time: "
596 << volume_guiding_denoise_time_.get_average() << " seconds.";
597}
598
599void RenderScheduler::report_display_update_time(const RenderWork &render_work, const double time)
600{
602
603 const double final_time_approx = approximate_final_time(render_work, time);
604
606 first_render_time_.display_update_time = final_time_approx;
607 }
608
609 if (work_report_reset_average(render_work)) {
611 }
612
613 display_update_time_.add_average(final_time_approx);
614
615 LOG_DEBUG << "Average display update time: " << display_update_time_.get_average()
616 << " seconds.";
617
618 /* Move the display update moment further in time, so that logic which checks when last update
619 * did happen have more reliable point in time (without path tracing and denoising parts of the
620 * render work). */
621 state_.last_display_update_time = time_dt();
622}
623
625 const double time,
626 bool balance_changed)
627{
629
630 if (work_report_reset_average(render_work)) {
632 }
633
635
636 if (balance_changed) {
637 ++state_.num_rebalance_changes;
638 }
639
640 state_.last_rebalance_changed = balance_changed;
641
642 LOG_DEBUG << "Average rebalance time: " << rebalance_time_.get_average() << " seconds.";
643}
644
646{
647 const double render_wall_time = state_.end_render_time - state_.start_render_time;
648 const int num_rendered_samples = get_total_rendered_samples();
649
650 string result = "\nRender Scheduler Summary\n\n";
651
652 {
653 string mode;
654 if (headless_) {
655 mode = "Headless";
656 }
657 else if (background_) {
658 mode = "Background";
659 }
660 else {
661 mode = "Interactive";
662 }
663 result += "Mode: " + mode + "\n";
664 }
665
666 result += "Resolution: " + to_string(buffer_params_.width) + "x" +
668
669 result += "\nAdaptive sampling:\n";
670 result += " Use: " + string_from_bool(adaptive_sampling_.use) + "\n";
672 result += " Step: " + to_string(adaptive_sampling_.adaptive_step) + "\n";
673 result += " Min Samples: " + to_string(adaptive_sampling_.min_samples) + "\n";
674 result += " Threshold: " + to_string(adaptive_sampling_.threshold) + "\n";
675 }
676
677 result += "\nDenoiser:\n";
678 result += " Use: " + string_from_bool(denoiser_params_.use) + "\n";
679 if (denoiser_params_.use) {
680 result += " Type: " + string(denoiserTypeToHumanReadable(denoiser_params_.type)) + "\n";
681 result += " Start Sample: " + to_string(denoiser_params_.start_sample) + "\n";
682
683 string passes = "Color";
685 passes += ", Albedo";
686 }
688 passes += ", Specular Albedo";
689 }
691 passes += ", Normal";
692 }
694 passes += ", Roughness";
695 }
697 passes += ", Depth";
698 }
700 passes += ", Backward Motion";
701 }
703 passes += ", Specular Motion";
704 }
705
706 result += " Passes: " + passes + "\n";
707 }
708
709 if (state_.num_rebalance_requested) {
710 result += "\nRebalancer:\n";
711 result += " Number of requested rebalances: " + to_string(state_.num_rebalance_requested) +
712 "\n";
713 result += " Number of performed rebalances: " + to_string(state_.num_rebalance_changes) +
714 "\n";
715 }
716
717 result += "\nTime (in seconds):\n";
718 result += string_printf(" %20s %20s %20s\n", "", "Wall", "Average");
719 result += string_printf(" %20s %20f %20f\n",
720 "Path Tracing",
723
725 result += string_printf(" %20s %20f %20f\n",
726 "Adaptive Filter",
729 }
730
731 if (denoiser_params_.use) {
732 result += string_printf(
733 " %20s %20f %20f\n", "Denoiser", denoise_time_.get_wall(), denoise_time_.get_average());
734 }
735
736 result += string_printf(" %20s %20f %20f\n",
737 "Display Update",
740
741 if (state_.num_rebalance_requested) {
742 result += string_printf(" %20s %20f %20f\n",
743 "Rebalance",
746 }
747
748 const double total_time = path_trace_time_.get_wall() + adaptive_filter_time_.get_wall() +
750 result += "\n Total: " + to_string(total_time) + "\n";
751
752 result += string_printf(
753 "\nRendered %d samples in %f seconds\n", num_rendered_samples, render_wall_time);
754
755 /* When adaptive sampling is used the average time becomes meaningless, because different samples
756 * will likely render different number of pixels. */
757 if (!adaptive_sampling_.use) {
758 result += string_printf("Average time per sample: %f seconds\n",
759 render_wall_time / num_rendered_samples);
760 }
761
762 return result;
763}
764
769
771 int num_rendered_samples) const
772{
774 num_rendered_samples);
775
776 if (time_limit_ != 0.0 && state_.start_render_time != 0.0) {
777 const double remaining_render_time = max(0.0,
778 time_limit_ - (time_dt() - state_.start_render_time));
779
780 update_interval = min(update_interval, remaining_render_time);
781 }
782
783 return update_interval;
784}
785
786/* TODO(sergey): This is just a quick implementation, exact values might need to be tweaked based
787 * on a more careful experiments with viewport rendering. */
789 int num_rendered_samples) const
790{
791 /* TODO(sergey): Need a decision on whether this should be using number of samples rendered
792 * within the current render session, or use absolute number of samples with the start sample
793 * taken into account. It will depend on whether the start sample offset clears the render
794 * buffer. */
795
796 if (state_.need_rebalance_at_next_work) {
797 return 0.1;
798 }
799 if (state_.last_rebalance_changed) {
800 return 0.2;
801 }
802
803 if (headless_) {
804 /* In headless mode do rare updates, so that the device occupancy is high, but there are still
805 * progress messages printed to the logs. */
806 return 30.0;
807 }
808
809 if (background_) {
810 if (num_rendered_samples < 32) {
811 return 1.0;
812 }
813 return 2.0;
814 }
815
816 /* Render time and number of samples rendered are used to figure out the display update interval.
817 * Render time is used to allow for fast display updates in the first few seconds of rendering
818 * on fast devices. Number of samples rendered is used to allow for potentially quicker display
819 * updates on slow devices during the first few samples. */
820 const double render_time = path_trace_time_.get_wall();
821 if (render_time < 1) {
822 return 0.1;
823 }
824 if (render_time < 2) {
825 return 0.25;
826 }
827 if (render_time < 4) {
828 return 0.5;
829 }
830 if (render_time < 8 || num_rendered_samples < 32) {
831 return 1.0;
832 }
833 return 2.0;
834}
835
837{
838 const double time_per_sample_average = path_trace_time_.get_average();
839 /* Fall back to 1 sample if we have not recorded a time yet. */
840 if (time_per_sample_average == 0.0) {
841 return 1;
842 }
843
844 const double num_samples_in_second = pixel_size_ * pixel_size_ / time_per_sample_average;
845
846 const double update_interval_in_seconds = guess_display_update_interval_in_seconds();
847
848 return max(int(num_samples_in_second * update_interval_in_seconds), 1);
849}
850
852{
853 return sample_offset_ + state_.num_rendered_samples;
854}
855
856/* Round number of samples to the closest power of two.
857 * Rounding might happen to higher or lower value depending on which one is closer. Such behavior
858 * allows to have number of samples to be power of two without diverging from the planned number of
859 * samples too much. */
860static inline uint round_num_samples_to_power_of_2(const uint num_samples)
861{
862 if (num_samples == 1) {
863 return 1;
864 }
865
866 if (is_power_of_two(num_samples)) {
867 return num_samples;
868 }
869
870 const uint num_samples_up = next_power_of_two(num_samples);
871 const uint num_samples_down = num_samples_up - (num_samples_up >> 1);
872
873 const uint delta_up = num_samples_up - num_samples;
874 const uint delta_down = num_samples - num_samples_down;
875
876 if (delta_up <= delta_down) {
877 return num_samples_up;
878 }
879
880 return num_samples_down;
881}
882
884{
885 if (state_.resolution_divider != pixel_size_) {
886 return get_num_samples_during_navigation(state_.resolution_divider);
887 }
888
889 /* Always start full resolution render with a single sample. Gives more instant feedback to
890 * artists, and allows to gather information for a subsequent path tracing works. Do it in the
891 * headless mode as well, to give some estimate of how long samples are taking. */
892 if (state_.num_rendered_samples == 0 && state_.last_display_update_sample == -1) {
893 return 1;
894 }
895
896 const int num_samples_per_update = calculate_num_samples_per_update();
897 const int path_trace_start_sample = get_start_sample_to_path_trace();
898
899 /* Round number of samples to a power of two, so that division of path states into tiles goes in
900 * a more integer manner.
901 * This might make it so updates happens more rarely due to rounding up. In the test scenes this
902 * is not huge deal because it is not seen that more than 8 samples can be rendered between
903 * updates. If that becomes a problem we can add some extra rules like never allow to round up
904 * more than N samples. */
905 const int num_samples_pot = round_num_samples_to_power_of_2(num_samples_per_update);
906
907 const int max_num_samples_to_render = get_num_samples() - get_total_rendered_samples();
908
909 int num_samples_to_render = min(num_samples_pot, max_num_samples_to_render);
910
911 /* When enough statistics is available and doing an offline rendering prefer to keep device
912 * occupied. */
913 if (state_.occupancy_num_samples && (background_ || headless_)) {
914 /* Keep occupancy at about 0.5 (this is more of an empirical figure which seems to match scenes
915 * with good performance without forcing occupancy to be higher). */
916 int num_samples_to_occupy = state_.occupancy_num_samples;
917 float ratio_to_increase_occupancy = 1.0f;
918 if (state_.occupancy > 0 && state_.occupancy < 0.5f) {
919 ratio_to_increase_occupancy = 0.7f / state_.occupancy;
920 num_samples_to_occupy = lround(state_.occupancy_num_samples * ratio_to_increase_occupancy);
921 }
922
923 /* Time limit for path tracing, which constraints the scheduler from "over-scheduling" work
924 * in scenes which have very long path trace times and low occupancy. This allows faster
925 * feedback of render results, and faster canceling when artists notice something is wrong.
926 *
927 * Additionally, when the time limit is enabled, do not render more samples than it is needed
928 * to reach the time limit. */
929 double path_tracing_time_limit = 0;
930 if (headless_) {
931 /* In the headless (command-line) render "over-scheduling" is not as bad, as it ensures the
932 * best possible render time. */
933 }
934 else if (background_) {
935 /* For the first few seconds prefer quicker updates, giving it a better chance for artists
936 * to cancel render early on when they notice something is wrong. After that increase the
937 * update times a lot, giving the best possible performance on a complicated scenes like
938 * the Spring splash screen (where occupancy is just very bad). */
939 if (state_.start_render_time == 0.0 || time_dt() - state_.start_render_time < 10) {
940 path_tracing_time_limit = 2.0;
941 }
942 else {
943 path_tracing_time_limit = 15.0;
944 }
945 }
946 else {
947 /* Viewport render: prefer faster updates over overall render time reduction. */
948 /* TODO: Look into enabling this entire code-path for the viewport as well, allowing
949 * compensation even in viewport (currently parent scope checks for non-viewport render). */
950 path_tracing_time_limit = guess_display_update_interval_in_seconds();
951 }
952 if (time_limit_ != 0.0 && state_.start_render_time != 0.0) {
953 const double remaining_render_time = max(
954 0.0, time_limit_ - (time_dt() - state_.start_render_time));
955 if (path_tracing_time_limit == 0) {
956 path_tracing_time_limit = remaining_render_time;
957 }
958 else {
959 path_tracing_time_limit = min(path_tracing_time_limit, remaining_render_time);
960 }
961 }
962 if (path_tracing_time_limit != 0) {
963 /* Use the per-sample time from the previously rendered batch of samples, so that the
964 * correction is applied much quicker. Also use the predicted increase in performance from
965 * increased occupancy. */
966 const double predicted_render_time = num_samples_to_occupy *
968 ratio_to_increase_occupancy;
969 if (predicted_render_time > path_tracing_time_limit) {
970 num_samples_to_occupy = lround(num_samples_to_occupy *
971 (path_tracing_time_limit / predicted_render_time));
972 }
973 }
974
975 num_samples_to_render = max(num_samples_to_render,
976 min(num_samples_to_occupy, max_num_samples_to_render));
977 }
978
980 num_samples_to_render = min(limit_samples_per_update_, num_samples_to_render);
981 }
982
983 /* If adaptive sampling is not use, render as many samples per update as possible, keeping
984 * the device fully occupied, without much overhead of display updates. */
985 if (!adaptive_sampling_.use) {
986 return num_samples_to_render;
987 }
988
989 /* TODO(sergey): Add extra "clamping" here so that none of the filtering points is missing. This
990 * is to ensure that the final render is pixel-matched regardless of how many samples per second
991 * compute device can do. */
992
993 return adaptive_sampling_.align_samples(path_trace_start_sample - sample_offset_,
994 num_samples_to_render);
995}
996
997int RenderScheduler::get_num_samples_during_navigation(const int resolution_divider) const
998{
999 /* Special trick for fast navigation: schedule multiple samples during fast navigation
1000 * (which will prefer to use lower resolution to keep up with refresh rate). This gives more
1001 * usable visual feedback for artists. */
1002
1004 /* When denoising is used during navigation prefer using a higher resolution with less samples
1005 * (scheduling less samples here will make it so the resolution_divider calculation will use a
1006 * lower value for the divider). This is because both OpenImageDenoise and OptiX denoiser
1007 * give visually better results on a higher resolution image with less samples. */
1008 return 1;
1009 }
1010
1011 /* Schedule samples equal to the resolution divider up to a maximum of 4, limited by the maximum
1012 * number of samples overall.
1013 * The idea is to have enough information on the screen by increasing the sample count as the
1014 * resolution is decreased. */
1015 const int max_navigation_samples = min(num_samples_, 4);
1016 /* NOTE: Changing this formula will change the formula in
1017 * `RenderScheduler::calculate_resolution_divider_for_time()`. */
1018 return min(max(1, resolution_divider / pixel_size_), max_navigation_samples);
1019}
1020
1025
1027{
1030 }
1031
1032 return max(state_.adaptive_sampling_threshold, adaptive_sampling_.threshold);
1033}
1034
1035bool RenderScheduler::work_need_denoise(bool &delayed, bool &ready_to_display)
1036{
1037 delayed = false;
1038 ready_to_display = true;
1039
1040 if (!denoiser_params_.use) {
1041 /* Denoising is disabled, no need to scheduler work for it. */
1042 return false;
1043 }
1044
1045 /* When multiple tiles are used the full frame will be denoised.
1046 * Avoid per-tile denoising to save up render time. */
1048 return false;
1049 }
1050
1051 if (done()) {
1052 /* Always denoise at the last sample. */
1053 return true;
1054 }
1055
1056 if (background_) {
1057 /* Background render, only denoise when rendering the last sample. */
1058 /* TODO(sergey): Follow similar logic to viewport, giving an overview of how final denoised
1059 * image looks like even for the background rendering. */
1060 return false;
1061 }
1062
1063 /* Viewport render. */
1064
1066 return true;
1067 }
1068
1069 /* Navigation might render multiple samples at a lower resolution. Those are not to be counted as
1070 * final samples. */
1071 const int num_samples_finished = state_.resolution_divider == pixel_size_ ?
1072 state_.num_rendered_samples :
1073 1;
1074
1075 /* Immediately denoise when we reach the start sample or last sample. */
1076 if (num_samples_finished == denoiser_params_.start_sample ||
1077 num_samples_finished == num_samples_)
1078 {
1079 return true;
1080 }
1081
1082 /* Do not denoise until the sample at which denoising should start is reached. */
1083 if (num_samples_finished < denoiser_params_.start_sample) {
1084 ready_to_display = false;
1085 return false;
1086 }
1087
1088 /* Avoid excessive denoising in viewport after reaching a certain sample count and render time.
1089 */
1090 /* TODO(sergey): Consider making time interval and sample configurable. */
1091 delayed = (path_trace_time_.get_wall() > 4 && num_samples_finished >= 20 &&
1092 (time_dt() - state_.last_display_update_time) < 1.0);
1093
1094 return !delayed;
1095}
1096
1097bool RenderScheduler::work_need_update_display(const bool denoiser_delayed)
1098{
1099 if (headless_) {
1100 /* Force disable display update in headless mode. There will be nothing to display the
1101 * in-progress result. */
1102 return false;
1103 }
1104
1105 if (denoiser_delayed) {
1106 /* If denoiser has been delayed the display can not be updated as it will not contain
1107 * up-to-date state of the render result. */
1108 return false;
1109 }
1110
1111 if (!adaptive_sampling_.use) {
1112 /* When adaptive sampling is not used the work is scheduled in a way that they keep render
1113 * device busy for long enough, so that the display update can happen right after the
1114 * rendering. */
1115 return true;
1116 }
1117
1118 if (done() || state_.last_display_update_sample == -1) {
1119 /* Make sure an initial and final results of adaptive sampling is communicated ot the display.
1120 */
1121 return true;
1122 }
1123
1124 /* For the development purposes of adaptive sampling it might be very useful to see all updates
1125 * of active pixels after convergence check. However, it would cause a slowdown for regular usage
1126 * users. Possibly, make it a debug panel option to allow rapid update to ease development
1127 * without need to re-compiled. */
1128 // if (work_need_adaptive_filter()) {
1129 // return true;
1130 // }
1131
1132 /* When adaptive sampling is used, its possible that only handful of samples of a very simple
1133 * scene will be scheduled to a powerful device (in order to not "miss" any of filtering points).
1134 * We take care of skipping updates here based on when previous display update did happen. */
1135 const double update_interval = guess_display_update_interval_in_seconds_for_num_samples(
1136 state_.last_display_update_sample);
1137 return (time_dt() - state_.last_display_update_time) > update_interval;
1138}
1139
1141{
1142 /* This is the minimum time, as the rebalancing can not happen more often than the path trace
1143 * work. */
1144 static const double kRebalanceIntervalInSeconds = 1;
1145
1147 return false;
1148 }
1149
1150 if (state_.resolution_divider != pixel_size_) {
1151 /* Don't rebalance at a non-final resolution divider. Some reasons for this:
1152 * - It will introduce unnecessary during navigation.
1153 * - Per-render device timing information is not very reliable yet. */
1154 return false;
1155 }
1156
1157 if (state_.num_rendered_samples == 0) {
1158 state_.need_rebalance_at_next_work = true;
1159 return false;
1160 }
1161
1162 if (state_.need_rebalance_at_next_work) {
1163 state_.need_rebalance_at_next_work = false;
1164 return true;
1165 }
1166
1167 if (state_.last_rebalance_changed) {
1168 return true;
1169 }
1170
1171 return (time_dt() - state_.last_rebalance_time) > kRebalanceIntervalInSeconds;
1172}
1173
1175{
1178 return;
1179 }
1180
1182 return;
1183 }
1184
1185 /* Calculate the maximum resolution divider possible while keeping the long axis of the viewport
1186 * above our preferred minimum axis size (128). */
1187 const int long_viewport_axis = max(buffer_params_.width, buffer_params_.height);
1188 const int max_res_divider_for_desired_size = long_viewport_axis / 128;
1189
1190 if (start_resolution_divider_ == 0) {
1191 /* Resolution divider has never been calculated before: start with a high resolution divider so
1192 * that we have a somewhat good initial behavior, giving a chance to collect real numbers. */
1194 max_res_divider_for_desired_size);
1195 LOG_DEBUG << "Initial resolution divider is " << start_resolution_divider_;
1196 return;
1197 }
1198
1199 if (first_render_time_.path_trace_per_sample == 0.0) {
1200 /* Not enough information to calculate better resolution, keep the existing one. */
1201 return;
1202 }
1203
1204 const double desired_update_interval_in_seconds =
1206
1207 const double actual_time_per_update = first_render_time_.path_trace_per_sample +
1208 first_render_time_.denoise_time +
1209 first_render_time_.display_update_time;
1210
1211 /* Allow some percent of tolerance, so that if the render time is close enough to the higher
1212 * resolution we prefer to use it instead of going way lower resolution and time way below the
1213 * desired one. */
1214 const int resolution_divider_for_update = calculate_resolution_divider_for_time(
1215 desired_update_interval_in_seconds * 1.4, actual_time_per_update);
1216
1217 /* TODO(sergey): Need to add hysteresis to avoid resolution divider bouncing around when actual
1218 * render time is somewhere on a boundary between two resolutions. */
1219
1220 /* Don't let resolution drop below the desired one. It's better to be slow than provide an
1221 * unreadable viewport render. */
1222 start_resolution_divider_ = min(resolution_divider_for_update, max_res_divider_for_desired_size);
1223
1224 LOG_DEBUG << "Calculated resolution divider is " << start_resolution_divider_;
1225}
1226
1228{
1230 /* Use lower value than the non-denoised case to allow having more pixels to reconstruct the
1231 * image from. With the faster updates and extra compute required the resolution becomes too
1232 * low to give usable feedback. */
1233 /* NOTE: Based on performance of OpenImageDenoise on CPU. For OptiX denoiser or other denoiser
1234 * on GPU the value might need to become lower for faster navigation. */
1235 return 1.0 / 12.0;
1236 }
1237
1238 /* For the best match with the Blender's viewport the refresh ratio should be 60fps. This will
1239 * avoid "jelly" effects. However, on a non-trivial scenes this can only be achieved with high
1240 * values of the resolution divider which does not give very pleasant updates during navigation.
1241 * Choose less frequent updates to allow more noise-free and higher resolution updates. */
1242
1243 /* TODO(sergey): Can look into heuristic which will allow to have 60fps if the resolution divider
1244 * is not too high. Alternatively, synchronize Blender's overlays updates to Cycles updates. */
1245
1246 return 1.0 / 30.0;
1247}
1248
1250{
1251 if (!denoiser_params_.use) {
1252 return false;
1253 }
1254
1256 return false;
1257 }
1258
1259 return true;
1260}
1261
1266
1268{
1269 return render_work.resolution_divider == pixel_size_ &&
1270 render_work.path_trace.start_sample == sample_offset_;
1271}
1272
1274{
1275 /* When rendering at a non-final resolution divider time average is not very useful because it
1276 * will either bias average down (due to lower render times on the smaller images) or will give
1277 * incorrect result when trying to estimate time which would have spent on the final resolution.
1278 *
1279 * So we only accumulate average for the latest resolution divider which was rendered. */
1280 return render_work.resolution_divider != pixel_size_;
1281}
1282
1284{
1285 if (time_limit_ == 0.0) {
1286 /* No limit is enforced. */
1287 return;
1288 }
1289
1290 if (state_.start_render_time == 0.0) {
1291 /* Rendering did not start yet. */
1292 return;
1293 }
1294
1295 const double current_time = time_dt();
1296
1297 if (current_time - state_.start_render_time < time_limit_) {
1298 /* Time limit is not reached yet. */
1299 return;
1300 }
1301
1302 state_.time_limit_reached = true;
1303 state_.end_render_time = current_time;
1304}
1305
1306/* --------------------------------------------------------------------
1307 * Utility functions.
1308 */
1309
1311 const double actual_time)
1312{
1313 const double ratio_between_times = actual_time / desired_time;
1314
1315 /* We can pass `ratio_between_times` to `get_num_samples_during_navigation()` to get our
1316 * navigation samples because the equation for calculating the resolution divider is as follows:
1317 * `actual_time / desired_time = sqr(resolution_divider) / sample_count`.
1318 * While `resolution_divider` is less than or equal to 4, `resolution_divider = sample_count`
1319 * (This relationship is determined in `get_num_samples_during_navigation()`). With some
1320 * substitution we end up with `actual_time / desired_time = resolution_divider` while the
1321 * resolution divider is less than or equal to 4. Once the resolution divider increases above 4,
1322 * the relationship of `actual_time / desired_time = resolution_divider` is no longer true,
1323 * however the sample count retrieved from `get_num_samples_during_navigation()` is still
1324 * accurate if we continue using this assumption. It should be noted that the interaction between
1325 * `pixel_size`, sample count, and resolution divider are automatically accounted for and that's
1326 * why `pixel_size` isn't included in any of the equations. */
1327 const int navigation_samples = get_num_samples_during_navigation(
1328 ceil_to_int(ratio_between_times));
1329
1330 return ceil_to_int(sqrt(navigation_samples * ratio_between_times));
1331}
1332
1333int calculate_resolution_divider_for_resolution(int width, int height, const int resolution)
1334{
1335 if (resolution == INT_MAX) {
1336 return 1;
1337 }
1338
1339 int resolution_divider = 1;
1340 while (width * height > resolution * resolution) {
1341 width = max(1, width / 2);
1342 height = max(1, height / 2);
1343
1344 resolution_divider <<= 1;
1345 }
1346
1347 return resolution_divider;
1348}
1349
1351 const int height,
1352 const int resolution_divider)
1353{
1354 const int pixel_area = width * height;
1355 const int resolution = lround(sqrt(pixel_area));
1356
1357 return resolution / resolution_divider;
1358}
1359
btScalar max
int align_samples(const int start_sample, const int num_samples) const
bool need_filter(const int sample) const
NODE_DECLARE int width
Definition buffers.h:70
float upscale_factor
Definition denoise.h:90
DenoiserType type
Definition denoise.h:73
bool use_gpu
Definition denoise.h:86
DenoiserPassMask passes
Definition denoise.h:79
int start_sample
Definition denoise.h:76
NODE_DECLARE bool use
Definition denoise.h:70
static const int MAX_SAMPLES
Definition integrator.h:99
void add_average(const double time, const int num_measurements=1)
void add_wall(const double time)
AdaptiveSampling adaptive_sampling_
DenoiseParams denoiser_params_
void set_time_limit(const double time_limit)
void update_state_for_render_work(const RenderWork &render_work)
void report_path_trace_occupancy(const RenderWork &render_work, const float occupancy)
double guess_display_update_interval_in_seconds_for_num_samples(const int num_rendered_samples) const
float work_adaptive_threshold() const
int calculate_num_samples_per_update() const
int get_rendered_sample() const
void report_adaptive_filter_time(const RenderWork &render_work, const double time, bool is_cancelled)
bool work_need_update_display(const bool denoiser_delayed)
BufferParams buffer_params_
void set_need_schedule_rebalance(bool need_schedule_rebalance)
double guess_display_update_interval_in_seconds() const
bool work_need_denoise(bool &delayed, bool &ready_to_display)
bool work_need_adaptive_filter() const
int get_num_rendered_samples() const
string full_report() const
bool need_schedule_rebalance_works_
bool is_denoise_active_during_update() const
void report_denoise_time(const RenderWork &render_work, const double time)
bool set_postprocess_render_work(RenderWork *render_work)
double get_time_limit() const
double guess_display_update_interval_in_seconds_for_num_samples_no_limit(int num_rendered_samples) const
bool work_is_usable_for_first_render_estimation(const RenderWork &render_work)
int calculate_resolution_divider_for_time(const double desired_time, const double actual_time)
TimeWithAverage volume_guiding_denoise_time_
bool is_adaptive_sampling_used() const
void set_denoiser_params(const DenoiseParams &params)
bool is_denoiser_interactive() const
int get_total_rendered_samples() const
struct RenderScheduler::@152371007237337307247005247137000032232276335003 first_render_time_
bool render_work_reschedule_on_idle(RenderWork &render_work)
int get_num_samples() const
TimeWithAverage adaptive_filter_time_
void report_display_update_time(const RenderWork &render_work, const double time)
TimeWithAverage rebalance_time_
void set_sample_params(const int num_samples, const bool use_sample_subset, const int sample_subset_offset, const int sample_subset_length)
int get_sample_offset() const
int default_start_resolution_divider_
bool is_denoiser_gpu_used() const
TimeWithAverage display_update_time_
void reset(const BufferParams &buffer_params)
void set_full_frame_render_work(RenderWork *render_work)
double guess_viewport_navigation_update_interval_in_seconds() const
void update_start_resolution_divider()
void report_path_trace_time(const RenderWork &render_work, const double time, bool is_cancelled)
void set_adaptive_sampling(const AdaptiveSampling &adaptive_sampling)
int get_num_samples_during_navigation(const int resolution_divider) const
bool render_work_reschedule_on_converge(RenderWork &render_work)
TileManager & tile_manager_
bool work_report_reset_average(const RenderWork &render_work)
TimeWithAverage path_trace_time_
int get_start_sample_to_path_trace() const
int get_num_samples_to_path_trace() const
void render_work_reschedule_on_cancel(RenderWork &render_work)
void report_work_begin(const RenderWork &render_work)
TimeWithAverage denoise_time_
void set_need_schedule_cryptomatte(bool need_schedule_cryptomatte)
void report_rebalance_time(const RenderWork &render_work, const double time, bool balance_changed)
void set_limit_samples_per_update(const int limit_samples)
RenderWork get_render_work()
RenderScheduler(TileManager &tile_manager, const SessionParams &params)
struct RenderScheduler::@161253332237067242016063160144242156123242025211 state_
bool is_background() const
void report_volume_guiding_denoise_time(const RenderWork &render_work, const double time)
float resolution_divider
bool volume_guiding_denoise
bool init_render_buffers
struct RenderWork::@062144024112073257370032340252251130221344035014 tile
struct RenderWork::@045110325327334210271225251376000202010073303033 adaptive_sampling
struct RenderWork::@103345022157251202112140335355011252273171271005 path_trace
struct RenderWork::@310313166116170111017300246342343006316141322247 display
struct RenderWork::@026114172320136364120000247117121135046160342264 cryptomatte
struct RenderWork::@075102241252202025353112266176333317356231366323 full
float denoised_resolution_divider
bool has_multiple_tiles() const
CCL_NAMESPACE_BEGIN const char * denoiserTypeToHumanReadable(DenoiserType type)
Definition denoise.cpp:9
@ DENOISER_PASS_DEPTH
Definition denoise.h:32
@ DENOISER_PASS_ROUGHNESS
Definition denoise.h:31
@ DENOISER_PASS_NORMAL
Definition denoise.h:30
@ DENOISER_PASS_ALBEDO
Definition denoise.h:28
@ DENOISER_PASS_SPECULAR_ALBEDO
Definition denoise.h:29
@ DENOISER_PASS_BACKWARD_MOTION
Definition denoise.h:33
@ DENOISER_PASS_SPECULAR_MOTION
Definition denoise.h:34
@ DENOISER_DLSS
Definition denoise.h:14
#define CCL_NAMESPACE_END
#define INT_MAX
#define sqrt
#define LOG_DEBUG
Definition log.h:107
#define DCHECK_GT(a, b)
Definition log.h:145
ccl_device_inline int ceil_to_int(const float f)
Definition math_base.h:471
ccl_device_inline uint next_power_of_two(const uint x)
Definition math_base.h:832
int calculate_resolution_divider_for_resolution(int width, int height, const int resolution)
int calculate_resolution_for_divider(const int width, const int height, const int resolution_divider)
static double approximate_final_time(const RenderWork &render_work, const double time)
static uint round_num_samples_to_power_of_2(const uint num_samples)
#define min(a, b)
Definition sort.cc:38
string string_from_bool(bool var)
Definition string.cpp:185
string to_string(const char *str)
Definition string.cpp:202
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
CCL_NAMESPACE_BEGIN double time_dt()
Definition time.cpp:31
ccl_device_inline bool is_power_of_two(const size_t x)
Definition types_base.h:78