Blender V4.3
path_trace.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
8#include "integrator/guiding.h"
12
13#include "session/buffers.h"
14
15#include "util/function.h"
16#include "util/guiding.h"
17#include "util/thread.h"
18#include "util/unique_ptr.h"
19#include "util/vector.h"
20
22
24class Device;
25class DeviceScene;
26class DisplayDriver;
27class Film;
28class RenderBuffers;
29class RenderScheduler;
30class RenderWork;
32class OutputDriver;
33class Progress;
34class TileManager;
35
36/* PathTrace class takes care of kernel graph and scheduling on a (multi)device. It takes care of
37 * all the common steps of path tracing which are not device-specific. The list of tasks includes
38 * but is not limited to:
39 * - Kernel graph.
40 * - Scheduling logic.
41 * - Queues management.
42 * - Adaptive stopping. */
43class PathTrace {
44 public:
45 /* Render scheduler is used to report timing information and access things like start/finish
46 * sample. */
47 PathTrace(Device *device,
48 Device *denoiser_device,
49 Film *film,
50 DeviceScene *device_scene,
51 RenderScheduler &render_scheduler,
52 TileManager &tile_manager);
53 ~PathTrace();
54
55 /* Create devices and load kernels which are created on-demand (for example, denoising devices).
56 * The progress is reported to the currently configure progress object (via `set_progress`). */
57 void load_kernels();
58
59 /* Allocate working memory. This runs before allocating scene memory so that we can estimate
60 * more accurately which scene device memory may need to allocated on the host. */
61 void alloc_work_memory();
62
63 /* Check whether now it is a good time to reset rendering.
64 * Used to avoid very often resets in the viewport, giving it a chance to draw intermediate
65 * render result. */
66 bool ready_to_reset();
67
68 void reset(const BufferParams &full_params,
69 const BufferParams &big_tile_params,
70 bool reset_rendering);
71
72 void device_free();
73
74 /* Set progress tracker.
75 * Used to communicate details about the progress to the outer world, check whether rendering is
76 * to be canceled.
77 *
78 * The path tracer writes to this object, and then at a convenient moment runs
79 * progress_update_cb() callback. */
80 void set_progress(Progress *progress);
81
82 /* NOTE: This is a blocking call. Meaning, it will not return until given number of samples are
83 * rendered (or until rendering is requested to be canceled). */
84 void render(const RenderWork &render_work);
85
86 /* TODO(sergey): Decide whether denoiser is really a part of path tracer. Currently it is
87 * convenient to have it here because then its easy to access render buffer. But the downside is
88 * that this adds too much of entities which can live separately with some clear API. */
89
90 /* Set denoiser parameters.
91 * Use this to configure the denoiser before rendering any samples. */
93
94 /* Set parameters used for adaptive sampling.
95 * Use this to configure the adaptive sampler before rendering any samples. */
96 void set_adaptive_sampling(const AdaptiveSampling &adaptive_sampling);
97
98 /* Set the parameters for guiding.
99 * Use to setup the guiding structures before each rendering iteration. */
100 void set_guiding_params(const GuidingParams &params, const bool reset);
101
102 /* Sets output driver for render buffer output. */
103 void set_output_driver(unique_ptr<OutputDriver> driver);
104
105 /* Set display driver for interactive render buffer display. */
106 void set_display_driver(unique_ptr<DisplayDriver> driver);
107
108 /* Clear the display buffer by filling it in with all zeroes. */
109 void clear_display();
110
111 /* Perform drawing of the current state of the DisplayDriver. */
112 void draw();
113
114 /* Flush outstanding display commands before ending the render loop. */
115 void flush_display();
116
117 /* Cancel rendering process as soon as possible, without waiting for full tile to be sampled.
118 * Used in cases like reset of render session.
119 *
120 * This is a blocking call, which returns as soon as there is no running `render_samples()` call.
121 */
122 void cancel();
123
124 /* Copy an entire render buffer to/from the path trace. */
125
126 /* Copy happens via CPU side buffer: data will be copied from every device of the path trace, and
127 * the data will be copied to the device of the given render buffers. */
129
130 /* Copy happens via CPU side buffer: data will be copied from the device of the given render
131 * buffers and will be copied to all devices of the path trace. */
133
134 /* Copy render buffers of the big tile from the device to host.
135 * Return true if all copies are successful. */
137
138 /* Read given full-frame file from disk, perform needed processing and write it to the software
139 * via the write callback. */
140 void process_full_buffer_from_disk(string_view filename);
141
142 /* Get number of samples in the current big tile render buffers. */
143 int get_num_render_tile_samples() const;
144
145 /* Get pass data of the entire big tile.
146 * This call puts pass render result from all devices into the final pixels storage.
147 *
148 * NOTE: Expects buffers to be copied to the host using `copy_render_tile_from_device()`.
149 *
150 * Returns false if any of the accessor's `get_render_tile_pixels()` returned false. */
151 bool get_render_tile_pixels(const PassAccessor &pass_accessor,
152 const PassAccessor::Destination &destination);
153
154 /* Set pass data for baking. */
155 bool set_render_tile_pixels(PassAccessor &pass_accessor, const PassAccessor::Source &source);
156
157 /* Check whether denoiser was run and denoised passes are available. */
159
160 /* Get size and offset (relative to the buffer's full x/y) of the currently rendering tile.
161 * In the case of tiled rendering this will return full-frame after all tiles has been rendered.
162 *
163 * NOTE: If the full-frame buffer processing is in progress, returns parameters of the full-frame
164 * instead. */
167 int2 get_render_size() const;
168
169 /* Get buffer parameters of the current tile.
170 *
171 * NOTE: If the full-frame buffer processing is in progress, returns parameters of the full-frame
172 * instead. */
174
175 /* Generate full multi-line report of the rendering process, including rendering parameters,
176 * times, and so on. */
177 string full_report() const;
178
179 /* Callback which is called to report current rendering progress.
180 *
181 * It is supposed to be cheaper than buffer update/write, hence can be called more often.
182 * Additionally, it might be called form the middle of wavefront (meaning, it is not guaranteed
183 * that the buffer is "uniformly" sampled at the moment of this callback). */
184 function<void(void)> progress_update_cb;
185
186 protected:
187 /* Actual implementation of the rendering pipeline.
188 * Calls steps in order, checking for the cancel to be requested in between.
189 *
190 * Is separate from `render()` to simplify dealing with the early outputs and keeping
191 * `render_cancel_` in the consistent state. */
192 void render_pipeline(RenderWork render_work);
193
194 /* Initialize kernel execution on all integrator queues. */
196
197 /* Make sure both allocated and effective buffer parameters of path tracer works are up to date
198 * with the current big tile parameters, performance-dependent slicing, and resolution divider.
199 */
200 void update_work_buffer_params_if_needed(const RenderWork &render_work);
202 void update_effective_work_buffer_params(const RenderWork &render_work);
203
204 /* Perform various steps of the render work.
205 *
206 * Note that some steps might modify the work, forcing some steps to happen within this iteration
207 * of rendering. */
208 void init_render_buffers(const RenderWork &render_work);
209 void path_trace(RenderWork &render_work);
210 void adaptive_sample(RenderWork &render_work);
211 void denoise(const RenderWork &render_work);
212 void cryptomatte_postprocess(const RenderWork &render_work);
213 void update_display(const RenderWork &render_work);
214 void rebalance(const RenderWork &render_work);
215 void write_tile_buffer(const RenderWork &render_work);
216 void finalize_full_buffer_on_disk(const RenderWork &render_work);
217
218 /* Updates/initializes the guiding structures after a rendering iteration.
219 * The structures are updated using the training data/samples generated during the previous
220 * rendering iteration */
222
223 /* Prepares the per-kernel thread related guiding structures (e.g., PathSegmentStorage,
224 * pointers to the global Field and SegmentStorage)*/
226
227 /* Get number of samples in the current state of the render buffers. */
229
230 /* Check whether user requested to cancel rendering, so that path tracing is to be finished as
231 * soon as possible. */
232 bool is_cancel_requested();
233
234 /* Write the big tile render buffer via the write callback. */
235 void tile_buffer_write();
236
237 /* Read the big tile render buffer via the read callback. */
238 void tile_buffer_read();
239
240 /* Write current tile into the file on disk. */
242
243 /* Run the progress_update_cb callback if it is needed. */
244 void progress_update_if_needed(const RenderWork &render_work);
245
246 void progress_set_status(const string &status, const string &substatus = "");
247
248 /* Destroy GPU resources (such as graphics interop) used by work. */
250
251 /* Pointer to a device which is configured to be used for path tracing. If multiple devices
252 * are configured this is a `MultiDevice`. */
253 Device *device_ = nullptr;
254
255 /* Pointer to a device which is configured to be used for denoising. Can be identical
256 * to the device */
258
259 /* CPU device for creating temporary render buffers on the CPU side. */
260 unique_ptr<Device> cpu_device_;
261
264
267
268 /* Display driver for interactive render buffer display. */
269 unique_ptr<PathTraceDisplay> display_;
270
271 /* Output driver to write render buffer to. */
272 unique_ptr<OutputDriver> output_driver_;
273
274 /* Per-compute device descriptors of work which is responsible for path tracing on its configured
275 * device. */
277
278 /* Per-path trace work information needed for multi-device balancing. */
280
281 /* Render buffer parameters of the full frame and current big tile. */
284
285 /* Denoiser which takes care of denoising the big tile. */
286 unique_ptr<Denoiser> denoiser_;
287
288 /* Denoiser device descriptor which holds the denoised big tile for multi-device workloads. */
289 unique_ptr<PathTraceWork> big_tile_denoise_work_;
290
291#ifdef WITH_PATH_GUIDING
292 /* Guiding related attributes */
293 GuidingParams guiding_params_;
294
295 /* The guiding field which holds the representation of the incident radiance field for the
296 * complete scene. */
297 unique_ptr<openpgl::cpp::Field> guiding_field_;
298
299 /* The storage container which holds the training data/samples generated during the last
300 * rendering iteration. */
301 unique_ptr<openpgl::cpp::SampleStorage> guiding_sample_data_storage_;
302
303 /* The number of already performed training iterations for the guiding field. */
304 int guiding_update_count = 0;
305#endif
306
307 /* State which is common for all the steps of the render work.
308 * Is brought up to date in the `render()` call and is accessed from all the steps involved into
309 * rendering the work. */
310 struct {
311 /* Denotes whether render buffers parameters of path trace works are to be reset for the new
312 * value of the big tile parameters. */
313 bool need_reset_params = false;
314
315 /* Divider of the resolution for faster previews.
316 *
317 * Allows to re-use same render buffer, but have less pixels rendered into in it. The way to
318 * think of render buffer in this case is as an over-allocated array: the resolution divider
319 * affects both resolution and stride as visible by the integrator kernels. */
321
322 /* Parameters of the big tile with the current resolution divider applied. */
324
325 /* Denoiser was run and there are denoised versions of the passes in the render buffers. */
327
328 /* Current tile has been written (to either disk or callback.
329 * Indicates that no more work will be done on this tile. */
330 bool tile_written = false;
332
333 /* Progress object which is used to communicate sample progress. */
335
336 /* Fields required for canceling render on demand, as quickly as possible. */
337 struct {
338 /* Indicates whether there is an on-going `render_samples()` call. */
339 bool is_rendering = false;
340
341 /* Indicates whether rendering is requested to be canceled by `cancel()`. */
342 bool is_requested = false;
343
344 /* Synchronization between thread which does `render_samples()` and thread which does
345 * `cancel()`. */
349
350 /* Indicates whether a render result was drawn after latest session reset.
351 * Used by `ready_to_reset()` to implement logic which feels the most interactive. */
353
354 /* State of the full frame processing and writing to the software. */
355 struct {
358};
359
void reset()
clear internal cached data and reset random seed
Definition film.h:30
void device_free()
Progress * progress_
Definition path_trace.h:334
thread_condition_variable condition
Definition path_trace.h:347
function< void(void)> progress_update_cb
Definition path_trace.h:184
void denoise(const RenderWork &render_work)
BufferParams effective_big_tile_params
Definition path_trace.h:323
int2 get_render_tile_size() const
void finalize_full_buffer_on_disk(const RenderWork &render_work)
struct PathTrace::@1422 render_state_
void process_full_buffer_from_disk(string_view filename)
void tile_buffer_write()
Device * device_
Definition path_trace.h:253
unique_ptr< Denoiser > denoiser_
Definition path_trace.h:286
void update_effective_work_buffer_params(const RenderWork &render_work)
BufferParams full_params_
Definition path_trace.h:282
void rebalance(const RenderWork &render_work)
bool is_cancel_requested()
void init_render_buffers(const RenderWork &render_work)
void copy_to_render_buffers(RenderBuffers *render_buffers)
void destroy_gpu_resources()
bool has_denoised_result() const
void set_progress(Progress *progress)
vector< unique_ptr< PathTraceWork > > path_trace_works_
Definition path_trace.h:276
void copy_from_render_buffers(RenderBuffers *render_buffers)
Device * denoise_device_
Definition path_trace.h:257
void set_denoiser_params(const DenoiseParams &params)
void progress_update_if_needed(const RenderWork &render_work)
bool need_reset_params
Definition path_trace.h:313
void update_display(const RenderWork &render_work)
void progress_set_status(const string &status, const string &substatus="")
void set_adaptive_sampling(const AdaptiveSampling &adaptive_sampling)
void set_output_driver(unique_ptr< OutputDriver > driver)
DeviceScene * device_scene_
Definition path_trace.h:263
bool ready_to_reset()
bool get_render_tile_pixels(const PassAccessor &pass_accessor, const PassAccessor::Destination &destination)
void cancel()
void guiding_update_structures()
void guiding_prepare_structures()
int2 get_render_tile_offset() const
unique_ptr< Device > cpu_device_
Definition path_trace.h:260
thread_mutex mutex
Definition path_trace.h:346
void set_guiding_params(const GuidingParams &params, const bool reset)
void tile_buffer_write_to_disk()
void set_display_driver(unique_ptr< DisplayDriver > driver)
void clear_display()
void path_trace(RenderWork &render_work)
void render_pipeline(RenderWork render_work)
unique_ptr< OutputDriver > output_driver_
Definition path_trace.h:272
bool tile_written
Definition path_trace.h:330
unique_ptr< PathTraceDisplay > display_
Definition path_trace.h:269
vector< WorkBalanceInfo > work_balance_infos_
Definition path_trace.h:279
RenderBuffers * render_buffers
Definition path_trace.h:356
bool copy_render_tile_from_device()
bool has_denoised_result
Definition path_trace.h:326
bool is_rendering
Definition path_trace.h:339
PathTrace(Device *device, Device *denoiser_device, Film *film, DeviceScene *device_scene, RenderScheduler &render_scheduler, TileManager &tile_manager)
bool did_draw_after_reset_
Definition path_trace.h:352
void render_init_kernel_execution()
int2 get_render_size() const
TileManager & tile_manager_
Definition path_trace.h:266
unique_ptr< PathTraceWork > big_tile_denoise_work_
Definition path_trace.h:289
void update_work_buffer_params_if_needed(const RenderWork &render_work)
void alloc_work_memory()
BufferParams big_tile_params_
Definition path_trace.h:283
void draw()
void write_tile_buffer(const RenderWork &render_work)
void tile_buffer_read()
bool set_render_tile_pixels(PassAccessor &pass_accessor, const PassAccessor::Source &source)
void render(const RenderWork &render_work)
Film * film_
Definition path_trace.h:262
string full_report() const
void adaptive_sample(RenderWork &render_work)
void load_kernels()
const BufferParams & get_render_tile_params() const
int resolution_divider
Definition path_trace.h:320
int get_num_render_tile_samples() const
struct PathTrace::@1424 full_frame_state_
bool is_requested
Definition path_trace.h:342
struct PathTrace::@1423 render_cancel_
void flush_display()
RenderScheduler & render_scheduler_
Definition path_trace.h:265
int get_num_samples_in_buffer()
void cryptomatte_postprocess(const RenderWork &render_work)
void update_allocated_work_buffer_params()
#define CCL_NAMESPACE_END
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition thread.h:29
std::condition_variable thread_condition_variable
Definition thread.h:31