[Bf-blender-cvs] [9c3f1e01e79] cycles_path_guiding: Simplify/optimize code for initializing integrator state

Brecht Van Lommel noreply at git.blender.org
Wed Sep 21 21:47:17 CEST 2022


Commit: 9c3f1e01e79580774eba53d30174fc9acf6d89d3
Author: Brecht Van Lommel
Date:   Wed Sep 21 20:21:15 2022 +0200
Branches: cycles_path_guiding
https://developer.blender.org/rB9c3f1e01e79580774eba53d30174fc9acf6d89d3

Simplify/optimize code for initializing integrator state

* Don't duplicate some openpgl pointers from kernel globals in state, only
  adds overhead.
* Reserve path segment storage only once in kernel globals.
* Reduce PATH_GUIDING_LEVEL checks, in a way that should not affect performance.

===================================================================

M	intern/cycles/CMakeLists.txt
M	intern/cycles/device/cpu/device_impl.cpp
M	intern/cycles/device/cpu/device_impl.h
M	intern/cycles/device/cpu/kernel_thread_globals.cpp
M	intern/cycles/device/cpu/kernel_thread_globals.h
M	intern/cycles/integrator/path_trace.h
M	intern/cycles/integrator/path_trace_work.h
M	intern/cycles/integrator/path_trace_work_cpu.cpp
M	intern/cycles/integrator/path_trace_work_cpu.h
M	intern/cycles/kernel/device/cpu/globals.h
M	intern/cycles/kernel/integrator/guiding.h
M	intern/cycles/kernel/integrator/path_state.h
M	intern/cycles/kernel/integrator/shade_shadow.h
M	intern/cycles/kernel/integrator/state.h
M	intern/cycles/kernel/integrator/state_template.h
M	intern/cycles/kernel/integrator/subsurface_random_walk.h
M	intern/cycles/kernel/integrator/surface_shader.h
M	intern/cycles/kernel/integrator/volume_shader.h
M	intern/cycles/util/guiding.h

===================================================================

diff --git a/intern/cycles/CMakeLists.txt b/intern/cycles/CMakeLists.txt
index e580f134984..d10cae13fbf 100644
--- a/intern/cycles/CMakeLists.txt
+++ b/intern/cycles/CMakeLists.txt
@@ -350,14 +350,13 @@ endif()
 if(WITH_CYCLES_PATH_GUIDING)
   add_definitions(-DWITH_PATH_GUIDING)
 
-  # The level of the guiding integration. 
-  # Different levels can be selected to measure the overhead of 
-  # different stages.
+  # The level of the guiding integration.
+  # Different levels can be selected to measure the overhead of different stages.
   # 1 = recording the path segments
   # 2 = 1 + generating (not storing) sample data from the segments
   # 3 = 2 + storing the generates sample data
   # 4 = 3 + training the guiding fields
-  # 5 = 4 + querying the trained guiding for sampling (full path guiding)  
+  # 5 = 4 + querying the trained guiding for sampling (full path guiding)
   add_definitions(-DPATH_GUIDING_LEVEL=5)
 
   include_directories(
diff --git a/intern/cycles/device/cpu/device_impl.cpp b/intern/cycles/device/cpu/device_impl.cpp
index ae1592d42e1..3d0f3195f74 100644
--- a/intern/cycles/device/cpu/device_impl.cpp
+++ b/intern/cycles/device/cpu/device_impl.cpp
@@ -17,10 +17,6 @@
 #  include <embree3/rtcore.h>
 #endif
 
-#ifdef WITH_PATH_GUIDING
-#  include <openpgl/cpp/OpenPGL.h>
-#endif
-
 #include "device/cpu/kernel.h"
 #include "device/cpu/kernel_thread_globals.h"
 
diff --git a/intern/cycles/device/cpu/device_impl.h b/intern/cycles/device/cpu/device_impl.h
index 38edd2f3930..cbc78d3247a 100644
--- a/intern/cycles/device/cpu/device_impl.h
+++ b/intern/cycles/device/cpu/device_impl.h
@@ -26,6 +26,7 @@
 #include "kernel/osl/globals.h"
 // clang-format on
 
+#include "util/guiding.h"
 #include "util/unique_ptr.h"
 
 CCL_NAMESPACE_BEGIN
diff --git a/intern/cycles/device/cpu/kernel_thread_globals.cpp b/intern/cycles/device/cpu/kernel_thread_globals.cpp
index 28fa3d7c728..90880f5e5f7 100644
--- a/intern/cycles/device/cpu/kernel_thread_globals.cpp
+++ b/intern/cycles/device/cpu/kernel_thread_globals.cpp
@@ -14,7 +14,7 @@ CPUKernelThreadGlobals::CPUKernelThreadGlobals(const KernelGlobalsCPU &kernel_gl
                                                Profiler &cpu_profiler)
     : KernelGlobalsCPU(kernel_globals), cpu_profiler_(cpu_profiler)
 {
-  reset_runtime_memory();
+  clear_runtime_pointers();
 
 #ifdef WITH_OSL
   OSLGlobals::thread_init(this, static_cast<OSLGlobals *>(osl_globals_memory));
@@ -22,23 +22,15 @@ CPUKernelThreadGlobals::CPUKernelThreadGlobals(const KernelGlobalsCPU &kernel_gl
   (void)osl_globals_memory;
 #endif
 
-#if defined(WITH_PATH_GUIDING)
-#  if PATH_GUIDING_LEVEL >= 1
+#ifdef WITH_PATH_GUIDING
   opgl_path_segment_storage = new openpgl::cpp::PathSegmentStorage();
-#  endif
-#  if PATH_GUIDING_LEVEL >= 3
-  opgl_sample_data_storage = nullptr;
-#  endif
-#  if PATH_GUIDING_LEVEL >= 4
-  opgl_guiding_field = nullptr;
-#  endif
 #endif
 }
 
 CPUKernelThreadGlobals::CPUKernelThreadGlobals(CPUKernelThreadGlobals &&other) noexcept
     : KernelGlobalsCPU(std::move(other)), cpu_profiler_(other.cpu_profiler_)
 {
-  other.reset_runtime_memory();
+  other.clear_runtime_pointers();
 }
 
 CPUKernelThreadGlobals::~CPUKernelThreadGlobals()
@@ -47,8 +39,10 @@ CPUKernelThreadGlobals::~CPUKernelThreadGlobals()
   OSLGlobals::thread_free(this);
 #endif
 
-#if defined(WITH_PATH_GUIDING) && PATH_GUIDING_LEVEL >= 1
+#ifdef WITH_PATH_GUIDING
   delete opgl_path_segment_storage;
+  delete opgl_surface_sampling_distribution;
+  delete opgl_volume_sampling_distribution;
 #endif
 }
 
@@ -60,19 +54,24 @@ CPUKernelThreadGlobals &CPUKernelThreadGlobals::operator=(CPUKernelThreadGlobals
 
   *static_cast<KernelGlobalsCPU *>(this) = *static_cast<KernelGlobalsCPU *>(&other);
 
-  other.reset_runtime_memory();
+  other.clear_runtime_pointers();
 
   return *this;
 }
 
-void CPUKernelThreadGlobals::reset_runtime_memory()
+void CPUKernelThreadGlobals::clear_runtime_pointers()
 {
 #ifdef WITH_OSL
   osl = nullptr;
 #endif
 
-#if defined(WITH_PATH_GUIDING) && PATH_GUIDING_LEVEL >= 1
+#ifdef WITH_PATH_GUIDING
+  opgl_sample_data_storage = nullptr;
+  opgl_guiding_field = nullptr;
+
   opgl_path_segment_storage = nullptr;
+  opgl_surface_sampling_distribution = nullptr;
+  opgl_volume_sampling_distribution = nullptr;
 #endif
 }
 
diff --git a/intern/cycles/device/cpu/kernel_thread_globals.h b/intern/cycles/device/cpu/kernel_thread_globals.h
index 96d2bd9e165..2462eb4cb2c 100644
--- a/intern/cycles/device/cpu/kernel_thread_globals.h
+++ b/intern/cycles/device/cpu/kernel_thread_globals.h
@@ -36,7 +36,7 @@ class CPUKernelThreadGlobals : public KernelGlobalsCPU {
   void stop_profiling();
 
  protected:
-  void reset_runtime_memory();
+  void clear_runtime_pointers();
 
   Profiler &cpu_profiler_;
 };
diff --git a/intern/cycles/integrator/path_trace.h b/intern/cycles/integrator/path_trace.h
index 0523fcf6398..d3a238696fd 100644
--- a/intern/cycles/integrator/path_trace.h
+++ b/intern/cycles/integrator/path_trace.h
@@ -12,15 +12,11 @@
 #include "session/buffers.h"
 
 #include "util/function.h"
+#include "util/guiding.h"
 #include "util/thread.h"
 #include "util/unique_ptr.h"
 #include "util/vector.h"
 
-#ifdef WITH_PATH_GUIDING
-#  include <openpgl/cpp/OpenPGL.h>
-#  include <openpgl/version.h>
-#endif
-
 CCL_NAMESPACE_BEGIN
 
 class AdaptiveSampling;
diff --git a/intern/cycles/integrator/path_trace_work.h b/intern/cycles/integrator/path_trace_work.h
index 2ffebc14a25..e31a6ef8819 100644
--- a/intern/cycles/integrator/path_trace_work.h
+++ b/intern/cycles/integrator/path_trace_work.h
@@ -141,10 +141,7 @@ class PathTraceWork {
   }
 
 #ifdef WITH_PATH_GUIDING
-  /* Intializes the per-thread guiding kernel data. The function sets the pointers to the
-   * global guiding field and the sample data storage as well es initializes the per-thread
-   * guided sampling distrubtions (e.g., SurfaceSamplingDistribution and
-   * VolumeSamplingDistribution). */
+  /* Initializes the per-thread guiding kernel data. */
   virtual void guiding_init_kernel_globals(void *, void *, const bool)
   {
   }
diff --git a/intern/cycles/integrator/path_trace_work_cpu.cpp b/intern/cycles/integrator/path_trace_work_cpu.cpp
index 51e13d1ebf4..d5ac830db58 100644
--- a/intern/cycles/integrator/path_trace_work_cpu.cpp
+++ b/intern/cycles/integrator/path_trace_work_cpu.cpp
@@ -57,47 +57,6 @@ void PathTraceWorkCPU::init_execution()
   device_->get_cpu_kernel_thread_globals(kernel_thread_globals_);
 }
 
-#ifdef WITH_PATH_GUIDING
-/* Note: It seems that this is called before every rendering iteration/progression and not once per
- * rendering. May be we find a way to call it only once per rendering. */
-void PathTraceWorkCPU::guiding_init_kernel_globals(void *guiding_field,
-                                                   void *sample_data_storage,
-                                                   const bool train)
-{
-  /* Linking the global guiding structures (e.g., Field and SampleStorage) to the per-thread
-   * kernel globals. */
-#  if PATH_GUIDING_LEVEL >= 4
-  openpgl::cpp::Field *field = (openpgl::cpp::Field *)guiding_field;
-#  endif
-  for (int thread_index = 0; thread_index < kernel_thread_globals_.size(); thread_index++) {
-#  if PATH_GUIDING_LEVEL >= 3
-    CPUKernelThreadGlobals &kg = kernel_thread_globals_[thread_index];
-
-    kg.opgl_sample_data_storage = (openpgl::cpp::SampleStorage *)sample_data_storage;
-#  endif
-
-#  if PATH_GUIDING_LEVEL >= 4
-    if (field) {
-      kg.opgl_guiding_field = field;
-      if (kg.opgl_surface_sampling_distribution)
-        delete kg.opgl_surface_sampling_distribution;
-      kg.opgl_surface_sampling_distribution = new openpgl::cpp::SurfaceSamplingDistribution(field);
-      if (kg.opgl_volume_sampling_distribution)
-        delete kg.opgl_volume_sampling_distribution;
-      kg.opgl_volume_sampling_distribution = new openpgl::cpp::VolumeSamplingDistribution(field);
-    }
-    else {
-      kg.opgl_guiding_field = nullptr;
-      kg.opgl_surface_sampling_distribution = nullptr;
-      kg.opgl_volume_sampling_distribution = nullptr;
-    }
-#  endif
-
-    kg.data.integrator.train_guiding = train;
-  }
-}
-#endif
-
 void PathTraceWorkCPU::render_samples(RenderStatistics &statistics,
                                       int start_sample,
                                       int samples_num,
@@ -148,7 +107,7 @@ void PathTraceWorkCPU::render_samples(RenderStatistics &statistics,
   statistics.occupancy = 1.0f;
 }
 
-void PathTraceWorkCPU::render_samples_full_pipeline(KernelGlobalsCPU *kg,
+void PathTraceWorkCPU::render_samples_full_pipeline(KernelGlobalsCPU *kernel_globals,
                                                     const KernelWorkTile &work_tile,
                                                     const int samples_num)
 {
@@ -173,35 +132,29 @@ void PathTraceWorkCPU::render_samples_full_pipeline(KernelGlobalsCPU *kg,
     }
 
     if (has_bake) {
-      if (!kernels_.integrator_init_from_bake(kg, state, &sample_work_tile, render_buffer)) {
+      if (!kernels_.integrator_init_from_bake(
+              kernel_globals, state, &sample_work_tile, render_buffer)) {
         break;
       }
     }
     else {
-      if (!kernels_.integrator_init_from_camera(kg, state, &sample_work_tile, render_buffer)) {
+      if (!kernels_.integrator_init_from_camera(
+              kernel_globals, state, &sample_work_tile, render_buffer)) {
         break;
       }
     }
 
-#ifdef WITH_PATH_GUIDING
-    const bool use_guiding = kernel_data.integrator.use_guiding;
-    if (use_guiding) {
-      /* Clear path segment storage. */
-      guiding_prepare_integrator_state(kg, state);
-    }
-#endif
+    kernels_.integrator_megakernel(kernel_globals, state, render_buffer);
 
-    kernels_.integrator_megakernel(kg, state, render_buffer);
-
-#if defined(WITH_PATH_GUIDING) && PATH_GUIDING_LEVEL >= 1
-    const bool train_guiding = kernel_data.integrator.use_guiding;
-    if (use_guiding && train_guiding) {
+#ifdef WITH_PATH_GUIDING
+    if (kernel_globals->data.integrator.train_guiding) {
       /* Push the generated sample data to the global sample data storage. */
-      guiding_push_sample_data_to_global_storage(kg, state, render_buffer);
+      gu

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list