Blender  V3.3
path_trace_work.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #pragma once
5 
7 #include "scene/pass.h"
8 #include "session/buffers.h"
9 #include "util/types.h"
10 #include "util/unique_ptr.h"
11 
13 
14 class BufferParams;
15 class Device;
16 class DeviceScene;
17 class Film;
18 class PathTraceDisplay;
19 class RenderBuffers;
20 
22  public:
24  float occupancy = 1.0f;
25  };
26 
27  /* Create path trace work which fits best the device.
28  *
29  * The cancel request flag is used for a cheap check whether cancel is to be performed as soon as
30  * possible. This could be, for example, request to cancel rendering on camera navigation in
31  * viewport. */
32  static unique_ptr<PathTraceWork> create(Device *device,
33  Film *film,
34  DeviceScene *device_scene,
35  bool *cancel_requested_flag);
36 
37  virtual ~PathTraceWork();
38 
39  /* Access the render buffers.
40  *
41  * Is only supposed to be used by the PathTrace to update buffer allocation and slicing to
42  * correspond to the big tile size and relative device performance. */
44 
45  /* Set effective parameters of the big tile and the work itself. */
46  void set_effective_buffer_params(const BufferParams &effective_full_params,
47  const BufferParams &effective_big_tile_params,
48  const BufferParams &effective_buffer_params);
49 
50  /* Check whether the big tile is being worked on by multiple path trace works. */
51  bool has_multiple_works() const;
52 
53  /* Allocate working memory for execution. Must be called before init_execution(). */
54  virtual void alloc_work_memory(){};
55 
56  /* Initialize execution of kernels.
57  * Will ensure that all device queues are initialized for execution.
58  *
59  * This method is to be called after any change in the scene. It is not needed to call it prior
60  * to an every call of the `render_samples()`. */
61  virtual void init_execution() = 0;
62 
63  /* Render given number of samples as a synchronous blocking call.
64  * The samples are added to the render buffer associated with this work. */
65  virtual void render_samples(RenderStatistics &statistics,
66  int start_sample,
67  int samples_num,
68  int sample_offset) = 0;
69 
70  /* Copy render result from this work to the corresponding place of the GPU display.
71  *
72  * The `pass_mode` indicates whether to access denoised or noisy version of the display pass. The
73  * noisy pass mode will be passed here when it is known that the buffer does not have denoised
74  * passes yet (because denoiser did not run). If the denoised pass is requested and denoiser is
75  * not used then this function will fall-back to the noisy pass instead. */
76  virtual void copy_to_display(PathTraceDisplay *display, PassMode pass_mode, int num_samples) = 0;
77 
78  virtual void destroy_gpu_resources(PathTraceDisplay *display) = 0;
79 
80  /* Copy data from/to given render buffers.
81  * Will copy pixels from a corresponding place (from multi-device point of view) of the render
82  * buffers, and copy work's render buffers to the corresponding place of the destination. */
83 
84  /* Notes:
85  * - Copies work's render buffer from the device.
86  * - Copies CPU-side buffer of the given buffer
87  * - Does not copy the buffer to its device. */
88  void copy_to_render_buffers(RenderBuffers *render_buffers);
89 
90  /* Notes:
91  * - Does not copy given render buffers from the device.
92  * - Copies work's render buffer to its device. */
93  void copy_from_render_buffers(const RenderBuffers *render_buffers);
94 
95  /* Special version of the `copy_from_render_buffers()` which only copies denoised passes from the
96  * given render buffers, leaving rest of the passes.
97  *
98  * Same notes about device copying applies to this call as well. */
99  void copy_from_denoised_render_buffers(const RenderBuffers *render_buffers);
100 
101  /* Copy render buffers to/from device using an appropriate device queue when needed so that
102  * things are executed in order with the `render_samples()`. */
104  virtual bool copy_render_buffers_to_device() = 0;
105 
106  /* Zero render buffers to/from device using an appropriate device queue when needed so that
107  * things are executed in order with the `render_samples()`. */
108  virtual bool zero_render_buffers() = 0;
109 
110  /* Access pixels rendered by this work and copy them to the corresponding location in the
111  * destination.
112  *
113  * NOTE: Does not perform copy of buffers from the device. Use `copy_render_tile_from_device()`
114  * to update host-side data. */
115  bool get_render_tile_pixels(const PassAccessor &pass_accessor,
116  const PassAccessor::Destination &destination);
117 
118  /* Set pass data for baking. */
119  bool set_render_tile_pixels(PassAccessor &pass_accessor, const PassAccessor::Source &source);
120 
121  /* Perform convergence test on the render buffer, and filter the convergence mask.
122  * Returns number of active pixels (the ones which did not converge yet). */
124 
125  /* Run cryptomatte pass post-processing kernels. */
126  virtual void cryptomatte_postproces() = 0;
127 
128  /* Cheap-ish request to see whether rendering is requested and is to be stopped as soon as
129  * possible, without waiting for any samples to be finished. */
130  inline bool is_cancel_requested() const
131  {
132  /* NOTE: Rely on the fact that on x86 CPU reading scalar can happen without atomic even in
133  * threaded environment. */
134  return *cancel_requested_flag_;
135  }
136 
137  /* Access to the device which is used to path trace this work on. */
139  {
140  return device_;
141  }
142 
143  protected:
144  PathTraceWork(Device *device,
145  Film *film,
146  DeviceScene *device_scene,
147  bool *cancel_requested_flag);
148 
150 
151  /* Get destination which offset and stride are configured so that writing to it will write to a
152  * proper location of GPU display texture, taking current tile and device slice into account. */
154  const PathTraceDisplay *display) const;
155 
156  /* Device which will be used for path tracing.
157  * Note that it is an actual render device (and never is a multi-device). */
159 
160  /* Film is used to access display pass configuration for GPU display update.
161  * Note that only fields which are not a part of kernel data can be accessed via the Film. */
163 
164  /* Device side scene storage, that may be used for integrator logic. */
166 
167  /* Render buffers where sampling is being accumulated into, allocated for a fraction of the big
168  * tile which is being rendered by this work.
169  * It also defines possible subset of a big tile in the case of multi-device rendering. */
170  unique_ptr<RenderBuffers> buffers_;
171 
172  /* Effective parameters of the full, big tile, and current work render buffer.
173  * The latter might be different from `buffers_->params` when there is a resolution divider
174  * involved. */
178 
179  bool *cancel_requested_flag_ = nullptr;
180 };
181 
Definition: film.h:29
virtual bool zero_render_buffers()=0
virtual void copy_to_display(PathTraceDisplay *display, PassMode pass_mode, int num_samples)=0
virtual void cryptomatte_postproces()=0
virtual int adaptive_sampling_converge_filter_count_active(float threshold, bool reset)=0
void copy_from_denoised_render_buffers(const RenderBuffers *render_buffers)
RenderBuffers * get_render_buffers()
unique_ptr< RenderBuffers > buffers_
BufferParams effective_full_params_
virtual bool copy_render_buffers_from_device()=0
PassAccessor::PassAccessInfo get_display_pass_access_info(PassMode pass_mode) const
BufferParams effective_big_tile_params_
bool * cancel_requested_flag_
bool get_render_tile_pixels(const PassAccessor &pass_accessor, const PassAccessor::Destination &destination)
PassAccessor::Destination get_display_destination_template(const PathTraceDisplay *display) const
virtual bool copy_render_buffers_to_device()=0
bool has_multiple_works() const
void copy_to_render_buffers(RenderBuffers *render_buffers)
void set_effective_buffer_params(const BufferParams &effective_full_params, const BufferParams &effective_big_tile_params, const BufferParams &effective_buffer_params)
virtual void destroy_gpu_resources(PathTraceDisplay *display)=0
void copy_from_render_buffers(const RenderBuffers *render_buffers)
BufferParams effective_buffer_params_
DeviceScene * device_scene_
virtual void render_samples(RenderStatistics &statistics, int start_sample, int samples_num, int sample_offset)=0
virtual void init_execution()=0
static unique_ptr< PathTraceWork > create(Device *device, Film *film, DeviceScene *device_scene, bool *cancel_requested_flag)
bool is_cancel_requested() const
PathTraceWork(Device *device, Film *film, DeviceScene *device_scene, bool *cancel_requested_flag)
virtual ~PathTraceWork()
bool set_render_tile_pixels(PassAccessor &pass_accessor, const PassAccessor::Source &source)
virtual void alloc_work_memory()
Device * get_device() const
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
ccl_gpu_kernel_postfix ccl_global float int int int int float threshold
ccl_gpu_kernel_postfix ccl_global float int int int int float bool reset
clear internal cached data and reset random seed
ccl_gpu_kernel_postfix ccl_global float int int int int ccl_global const float int int int int int int int int int int int int num_samples
PassMode
Definition: pass.h:19