Blender V5.3
path_trace_work_cpu.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 "device/cpu/kernel.h"
8#include "device/device.h"
9
10#ifdef WITH_CYCLES_DEBUG
11# include "kernel/film/write.h"
12#endif
13
15
18
19#include "scene/scene.h"
20#include "session/buffers.h"
21
22#include "util/tbb.h"
23#include "util/time.h"
24
26
27/* Create TBB arena for execution of path tracing and rendering tasks. */
28static inline tbb::task_arena local_tbb_arena_create(const Device *device)
29{
30 /* TODO: limit this to number of threads of CPU device, it may be smaller than
31 * the system number of threads when we reduce the number of CPU threads in
32 * CPU + GPU rendering to dedicate some cores to handling the GPU device. */
33 return tbb::task_arena(device->info.cpu_threads);
34}
35
36/* Get ThreadKernelGlobalsCPU for the current thread. */
38 vector<ThreadKernelGlobalsCPU> &kernel_thread_globals)
39{
40 const int thread_index = tbb::this_task_arena::current_thread_index();
41 DCHECK_GE(thread_index, 0);
42 DCHECK_LE(thread_index, kernel_thread_globals.size());
43
44 return &kernel_thread_globals[thread_index];
45}
46
48 Film *film,
49 DeviceScene *device_scene,
50 const bool *cancel_requested_flag)
51 : PathTraceWork(device, film, device_scene, cancel_requested_flag),
52 kernels_(Device::get_cpu_kernels())
53{
54 DCHECK_EQ(device->info.type, DEVICE_CPU);
55}
56
58{
59 /* Acquire thread globals, updating all data pointers. */
61}
62
68
70 const int start_sample,
71 const int samples_num,
72 const int sample_offset)
73{
74 const int64_t image_width = effective_buffer_params_.width;
75 const int64_t image_height = effective_buffer_params_.height;
76 const int64_t total_pixels_num = image_width * image_height;
77
78 if (device_->profiler.active()) {
79 for (ThreadKernelGlobalsCPU &kernel_globals : *kernel_thread_globals_) {
80 kernel_globals.start_profiling();
81 }
82 }
83
84 tbb::task_arena local_arena = local_tbb_arena_create(device_);
85 local_arena.execute([&]() {
86 parallel_for(int64_t(0), total_pixels_num, [&](int64_t work_index) {
87 if (is_cancel_requested()) {
88 return;
89 }
90
91 const int y = work_index / image_width;
92 const int x = work_index - y * image_width;
93
94 KernelWorkTile work_tile;
95 work_tile.x = effective_buffer_params_.full_x + x;
96 work_tile.y = effective_buffer_params_.full_y + y;
97 work_tile.w = 1;
98 work_tile.h = 1;
99 work_tile.start_sample = start_sample;
100 work_tile.sample_offset = sample_offset;
101 work_tile.num_samples = 1;
104
106
107 render_samples_full_pipeline(kernel_globals, work_tile, samples_num);
108 });
109 });
110 if (device_->profiler.active()) {
111 for (ThreadKernelGlobalsCPU &kernel_globals : *kernel_thread_globals_) {
112 kernel_globals.stop_profiling();
113 }
114 }
115
116 statistics.occupancy = 1.0f;
117}
118
120 const KernelWorkTile &work_tile,
121 const int samples_num)
122{
123 const bool has_bake = device_scene_->data.bake.use;
124
125 IntegratorStateCPU integrator_states[2];
126
127 IntegratorStateCPU *state = &integrator_states[0];
128 IntegratorStateCPU *shadow_catcher_state = nullptr;
129
130 if (device_scene_->data.integrator.has_shadow_catcher) {
131 shadow_catcher_state = &integrator_states[1];
132 path_state_init_queues(shadow_catcher_state);
133 }
134
135 KernelWorkTile sample_work_tile = work_tile;
136 float *render_buffer = buffers_->buffer.data();
137
138 fast_timer render_timer;
139
140 for (int sample = 0; sample < samples_num; ++sample) {
141 if (is_cancel_requested()) {
142 break;
143 }
144
145 if (has_bake) {
147 kernel_globals, state, &sample_work_tile, render_buffer))
148 {
149 break;
150 }
151 }
152 else {
154 kernel_globals, state, &sample_work_tile, render_buffer))
155 {
156 break;
157 }
158 }
159
160#if defined(WITH_PATH_GUIDING)
161 if (kernel_globals->data.integrator.train_guiding) {
162 assert(kernel_globals->opgl_path_segment_storage);
163 assert(kernel_globals->opgl_path_segment_storage->GetNumSegments() == 0);
164
166
167 /* Push the generated sample data to the global sample data storage. */
168 guiding_push_sample_data_to_global_storage(kernel_globals, state, render_buffer);
169
170 /* No training for shadow catcher paths. */
171 if (shadow_catcher_state) {
172 kernel_globals->data.integrator.train_guiding = false;
173 kernels_.integrator_megakernel(kernel_globals, shadow_catcher_state, render_buffer);
174 kernel_globals->data.integrator.train_guiding = true;
175 }
176 }
177 else
178#endif
179 {
181 if (shadow_catcher_state) {
182 kernels_.integrator_megakernel(kernel_globals, shadow_catcher_state, render_buffer);
183 }
184 }
185
186 if (kernel_globals->data.film.pass_render_time != PASS_UNUSED) {
187 uint64_t time;
188 if (render_timer.lap(time)) {
189 ccl_global float *buffer = render_buffer + (uint64_t)state->path.render_pixel_index *
190 kernel_globals->data.film.pass_stride;
191 *(buffer + kernel_globals->data.film.pass_render_time) += float(time);
192 }
193 }
194 ++sample_work_tile.start_sample;
195 }
196}
197
199 PassMode pass_mode,
200 const int num_samples)
201{
202 half4 *rgba_half = display->map_texture_buffer();
203 if (!rgba_half) {
204 /* TODO(sergey): Look into using copy_to_display() if mapping failed. Might be needed for
205 * some implementations of PathTraceDisplay which can not map memory? */
206 return;
207 }
208
209 const KernelFilm &kfilm = device_scene_->data.film;
210
211 const PassAccessor::PassAccessInfo pass_access_info = get_display_pass_access_info(pass_mode);
212 if (pass_access_info.type == PASS_NONE) {
213 return;
214 }
215
216 const BufferParams &effective_buffer_params = (pass_mode == PassMode::DENOISED) ?
219
220 const PassAccessorCPU pass_accessor(pass_access_info, kfilm.exposure, num_samples);
221
222 PassAccessor::Destination destination = get_display_destination_template(display, pass_mode);
223 destination.pixels_half_rgba = rgba_half;
224
225 tbb::task_arena local_arena = local_tbb_arena_create(device_);
226 local_arena.execute([&]() {
227 pass_accessor.get_render_tile_pixels(buffers_.get(), effective_buffer_params, destination);
228 });
229
230 display->unmap_texture_buffer();
231}
232
234
236{
237 return buffers_->copy_from_device();
238}
239
241{
242 buffers_->buffer.copy_to_device();
243 return true;
244}
245
247{
248 buffers_->zero();
249 return true;
250}
251
253 bool reset)
254{
255 const int full_x = effective_buffer_params_.full_x;
256 const int full_y = effective_buffer_params_.full_y;
257 const int width = effective_buffer_params_.width;
258 const int height = effective_buffer_params_.height;
259 const int offset = effective_buffer_params_.offset;
260 const int stride = effective_buffer_params_.stride;
261
262 float *render_buffer = buffers_->buffer.data();
263
264 uint num_active_pixels = 0;
265
266 tbb::task_arena local_arena = local_tbb_arena_create(device_);
267
268 /* Check convergency and do x-filter in a single `parallel_for`, to reduce threading overhead. */
269 local_arena.execute([&]() {
270 parallel_for(full_y, full_y + height, [&](int y) {
271 ThreadKernelGlobalsCPU *kernel_globals = kernel_thread_globals_->data();
272
273 bool row_converged = true;
274 uint num_row_pixels_active = 0;
275 for (int x = 0; x < width; ++x) {
277 kernel_globals, render_buffer, full_x + x, y, threshold, reset, offset, stride))
278 {
279 ++num_row_pixels_active;
280 row_converged = false;
281 }
282 }
283
284 atomic_fetch_and_add_uint32(&num_active_pixels, num_row_pixels_active);
285
286 if (!row_converged) {
288 kernel_globals, render_buffer, y, full_x, width, offset, stride);
289 }
290 });
291 });
292
293 if (num_active_pixels) {
294 local_arena.execute([&]() {
295 parallel_for(full_x, full_x + width, [&](int x) {
296 ThreadKernelGlobalsCPU *kernel_globals = kernel_thread_globals_->data();
298 kernel_globals, render_buffer, x, full_y, height, offset, stride);
299 });
300 });
301 }
302
303 return num_active_pixels;
304}
305
307{
308 const int width = effective_buffer_params_.width;
309 const int height = effective_buffer_params_.height;
310
311 float *render_buffer = buffers_->buffer.data();
312
313 tbb::task_arena local_arena = local_tbb_arena_create(device_);
314
315 /* Check convergency and do x-filter in a single `parallel_for`, to reduce threading overhead. */
316 local_arena.execute([&]() {
317 parallel_for(0, height, [&](int y) {
318 ThreadKernelGlobalsCPU *kernel_globals = kernel_thread_globals_->data();
319 int pixel_index = y * width;
320
321 for (int x = 0; x < width; ++x, ++pixel_index) {
322 kernels_.cryptomatte_postprocess(kernel_globals, render_buffer, pixel_index);
323 }
324 });
325 });
326}
327
329{
330 const int min_x = effective_buffer_params_.full_x;
331 const int min_y = effective_buffer_params_.full_y;
332 const int max_x = effective_buffer_params_.width + min_x;
333 const int max_y = effective_buffer_params_.height + min_y;
334 const int offset = effective_buffer_params_.offset;
335 const int stride = effective_buffer_params_.stride;
336
337 float *render_buffer = buffers_->buffer.data();
338
339 tbb::task_arena local_arena = local_tbb_arena_create(device_);
340
341 const blocked_range2d<int> range(min_x, max_x, min_y, max_y);
342
343 /* Filter in x direction. */
344 local_arena.execute([&]() {
345 parallel_for(range, [&](const blocked_range2d<int> r) {
346 ThreadKernelGlobalsCPU *kernel_globals = kernel_thread_globals_->data();
347 for (int y = r.cols().begin(); y < r.cols().end(); ++y) {
348 for (int x = r.rows().begin(); x < r.rows().end(); ++x) {
350 kernel_globals, render_buffer, y, x, min_x, max_x, offset, stride);
351 }
352 }
353 });
354 });
355
356 /* Filter in y direction. Unlike `filter_x`, the inner loop of `filter_y` is serially run inside
357 * the kernel, to avoid the need of intermediate buffers. */
358 local_arena.execute([&]() {
359 parallel_for(min_x, max_x, [&](int x) {
360 ThreadKernelGlobalsCPU *kernel_globals = kernel_thread_globals_->data();
362 kernel_globals, render_buffer, x, min_y, max_y, offset, stride);
363 });
364 });
365}
366
367#if defined(WITH_PATH_GUIDING)
368/* NOTE: It seems that this is called before every rendering iteration/progression and not once per
369 * rendering. May be we find a way to call it only once per rendering. */
370void PathTraceWorkCPU::guiding_init_kernel_globals(void *guiding_field,
371 void *sample_data_storage,
372 const bool train)
373{
374 /* Linking the global guiding structures (e.g., Field and SampleStorage) to the per-thread
375 * kernel globals. */
376 for (int thread_index = 0; thread_index < kernel_thread_globals_->size(); thread_index++) {
377 ThreadKernelGlobalsCPU &kg = (*kernel_thread_globals_)[thread_index];
378 openpgl::cpp::Field *field = (openpgl::cpp::Field *)guiding_field;
379
380 /* Allocate sampling distributions. */
381 kg.opgl_guiding_field = field;
382
383# if PATH_GUIDING_LEVEL >= 4
384 if (kg.opgl_surface_sampling_distribution) {
385 kg.opgl_surface_sampling_distribution.reset();
386 }
387 if (kg.opgl_volume_sampling_distribution) {
388 kg.opgl_volume_sampling_distribution.reset();
389 }
390
391 if (field) {
392 kg.opgl_surface_sampling_distribution =
393 make_unique<openpgl::cpp::SurfaceSamplingDistribution>(field);
394 kg.opgl_volume_sampling_distribution = make_unique<openpgl::cpp::VolumeSamplingDistribution>(
395 field);
396 }
397# endif
398
399 /* Reserve storage for training. */
400 kg.data.integrator.train_guiding = train;
401 kg.opgl_sample_data_storage = (openpgl::cpp::SampleStorage *)sample_data_storage;
402
403 if (train) {
404 kg.opgl_path_segment_storage_size = kg.data.integrator.transparent_max_bounce +
405 kg.data.integrator.max_bounce + 3;
406 kg.opgl_path_segment_storage->Reserve(kg.opgl_path_segment_storage_size);
407 kg.opgl_path_segment_storage->Clear();
408 }
409 }
410}
411
412void PathTraceWorkCPU::guiding_push_sample_data_to_global_storage(ThreadKernelGlobalsCPU *kg,
416{
417# ifdef WITH_CYCLES_DEBUG
419 /* Check if the generated path segments contain valid values. */
420 const bool validSegments = kg->opgl_path_segment_storage->ValidateSegments();
421 if (!validSegments) {
422 LOG_DEBUG << "Guiding: invalid path segments!";
423 }
424 }
425
426 /* Write debug render pass to validate it matches combined pass. */
427 pgl_vec3f pgl_final_color = kg->opgl_path_segment_storage->CalculatePixelEstimate(false);
429 float3 final_color = make_float3(pgl_final_color.x, pgl_final_color.y, pgl_final_color.z);
430 if (kernel_data.film.pass_guiding_color != PASS_UNUSED) {
431 film_write_pass_float3(buffer + kernel_data.film.pass_guiding_color, final_color);
432 }
433# else
434 (void)state;
435 (void)render_buffer;
436# endif
437
438 /* Convert the path segment representation of the random walk into radiance samples. */
439# if PATH_GUIDING_LEVEL >= 2
440 const bool use_direct_light = kernel_data.integrator.use_guiding_direct_light;
441 const bool use_mis_weights = kernel_data.integrator.use_guiding_mis_weights;
442 kg->opgl_path_segment_storage->PrepareSamples(use_mis_weights, use_direct_light, false);
443# endif
444
445# ifdef WITH_CYCLES_DEBUG
446 /* Check if the training/radiance samples generated by the path segment storage are valid. */
448 const bool validSamples = kg->opgl_path_segment_storage->ValidateSamples();
449 if (!validSamples) {
451 << "Guiding: path segment storage generated/contains invalid radiance/training samples!";
452 }
453 }
454# endif
455
456# if PATH_GUIDING_LEVEL >= 3
457 /* Push radiance samples from current random walk/path to the global sample storage. */
458 size_t num_samples = 0;
459 const openpgl::cpp::SampleData *samples = kg->opgl_path_segment_storage->GetSamples(num_samples);
460 kg->opgl_sample_data_storage->AddSamples(samples, num_samples);
461# endif
462
463 /* Clear storage for the current path, to be ready for the next path. */
464 kg->opgl_path_segment_storage->Clear();
465}
466#endif
467
ATOMIC_INLINE uint32_t atomic_fetch_and_add_uint32(uint32_t *p, uint32_t x)
long long int int64_t
unsigned long long int uint64_t
void reset()
NODE_DECLARE int width
Definition buffers.h:70
FilterYFunction volume_guiding_filter_y
AdaptiveSamplingConvergenceCheckFunction adaptive_sampling_convergence_check
CryptomattePostprocessFunction cryptomatte_postprocess
CPUKernelFunction< void(*)(const ThreadKernelGlobalsCPU *kg, ccl_global float *render_buffer, const int y, const int center_x, const int min_x, const int max_x, const int offset, int stride)> volume_guiding_filter_x
FilterYFunction adaptive_sampling_filter_y
FilterXFunction adaptive_sampling_filter_x
IntegratorShadeFunction integrator_megakernel
IntegratorInitFunction integrator_init_from_bake
IntegratorInitFunction integrator_init_from_camera
DeviceType type
KernelData data
Profiler & profiler
virtual vector< ThreadKernelGlobalsCPU > * acquire_cpu_kernel_thread_globals()
DeviceInfo info
virtual void release_cpu_kernel_thread_globals()
Definition film.h:29
bool get_render_tile_pixels(const RenderBuffers *render_buffers, const Destination &destination) const
bool copy_render_buffers_to_device() override
vector< ThreadKernelGlobalsCPU > * kernel_thread_globals_
void deinit_execution() override
void render_samples(RenderStatistics &statistics, const int start_sample, const int samples_num, const int sample_offset) override
void init_execution() override
bool copy_render_buffers_from_device() override
int adaptive_sampling_converge_filter_count_active(const float threshold, bool reset) override
void copy_to_display(PathTraceDisplay *display, PassMode pass_mode, const int num_samples) override
void destroy_gpu_resources(PathTraceDisplay *display) override
void cryptomatte_postproces() override
const CPUKernels & kernels_
void render_samples_full_pipeline(ThreadKernelGlobalsCPU *kernel_globals, const KernelWorkTile &work_tile, const int samples_num)
PathTraceWorkCPU(Device *device, Film *film, DeviceScene *device_scene, const bool *cancel_requested_flag)
bool zero_render_buffers() override
void denoise_volume_guiding_buffers() override
BufferParams effective_denoised_buffer_params_
PassAccessor::Destination get_display_destination_template(const PathTraceDisplay *display, const PassMode mode) const
unique_ptr< RenderBuffers > buffers_
PassAccessor::PassAccessInfo get_display_pass_access_info(PassMode pass_mode) const
BufferParams effective_buffer_params_
DeviceScene * device_scene_
PathTraceWork(Device *device, Film *film, DeviceScene *device_scene, const bool *cancel_requested_flag)
bool is_cancel_requested() const
bool active() const
bool lap(uint64_t &delta)
Definition time.h:70
#define kernel_data
#define PASS_UNUSED
#define ccl_restrict
#define ccl_global
#define CCL_NAMESPACE_END
@ DEVICE_CPU
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define assert(assertion)
VecBase< float, 3 > float3
const int state
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float * render_buffer
@ PASS_NONE
#define DCHECK_EQ(a, b)
Definition log.h:144
#define DCHECK_GE(a, b)
Definition log.h:142
#define LOG_DEBUG
Definition log.h:107
#define DCHECK_LE(a, b)
Definition log.h:147
#define LOG_IS_ON(level)
Definition log.h:113
@ LOG_LEVEL_DEBUG
Definition log.h:26
PassMode
Definition pass.h:20
@ DENOISED
Definition pass.h:22
CCL_NAMESPACE_BEGIN ccl_device_inline void path_state_init_queues(IntegratorState state)
Definition path_state.h:18
static ThreadKernelGlobalsCPU * kernel_thread_globals_get(vector< ThreadKernelGlobalsCPU > &kernel_thread_globals)
static CCL_NAMESPACE_BEGIN tbb::task_arena local_tbb_arena_create(const Device *device)
CCL_NAMESPACE_END KernelData data
Definition half.h:53
ccl_device_inline void film_write_pass_float3(ccl_global float *ccl_restrict buffer, const float3 value)
Definition write.h:68
CCL_NAMESPACE_BEGIN ccl_device_forceinline ccl_global float * film_pass_pixel_render_buffer(KernelGlobals kg, ConstIntegratorState state, ccl_global float *ccl_restrict render_buffer)
Definition write.h:24