[Bf-blender-cvs] [8611c37f975] master: Cycles: Generate OSL closures using macros and a template file

Patrick Mours noreply at git.blender.org
Fri Sep 9 15:49:41 CEST 2022


Commit: 8611c37f975737efe0d159822edfc21733268f51
Author: Patrick Mours
Date:   Thu Sep 8 19:31:44 2022 +0200
Branches: master
https://developer.blender.org/rB8611c37f975737efe0d159822edfc21733268f51

Cycles: Generate OSL closures using macros and a template file

This has the advantage of being able to use information about the
existing OSL closures in various places without code duplication. In
addition, the setup code for all closures was moved to standalone
functions to avoid usage of virtual function calls in preparation for GPU
support.

This patch was split from D15902.

Differential Revision: https://developer.blender.org/D15917

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

M	.clang-format
M	intern/cycles/kernel/closure/alloc.h
M	intern/cycles/kernel/osl/CMakeLists.txt
D	intern/cycles/kernel/osl/background.cpp
D	intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp
D	intern/cycles/kernel/osl/bsdf_phong_ramp.cpp
D	intern/cycles/kernel/osl/bssrdf.cpp
M	intern/cycles/kernel/osl/closures.cpp
D	intern/cycles/kernel/osl/closures.h
A	intern/cycles/kernel/osl/closures_setup.h
A	intern/cycles/kernel/osl/closures_template.h
D	intern/cycles/kernel/osl/emissive.cpp
M	intern/cycles/kernel/osl/services.cpp
M	intern/cycles/kernel/osl/services.h
M	intern/cycles/kernel/osl/shader.cpp
M	intern/cycles/kernel/osl/shader.h
A	intern/cycles/kernel/osl/types.h
M	intern/cycles/scene/osl.cpp

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

diff --git a/.clang-format b/.clang-format
index 7e88e6d1cb1..72add4594a4 100644
--- a/.clang-format
+++ b/.clang-format
@@ -273,5 +273,5 @@ StatementMacros:
   - PyObject_VAR_HEAD
   - ccl_gpu_kernel_postfix
 
-MacroBlockBegin: "^BSDF_CLOSURE_CLASS_BEGIN$"
-MacroBlockEnd: "^BSDF_CLOSURE_CLASS_END$"
+MacroBlockBegin: "^OSL_CLOSURE_STRUCT_BEGIN$"
+MacroBlockEnd: "^OSL_CLOSURE_STRUCT_END$"
diff --git a/intern/cycles/kernel/closure/alloc.h b/intern/cycles/kernel/closure/alloc.h
index 9847898ee89..1cf06614f3b 100644
--- a/intern/cycles/kernel/closure/alloc.h
+++ b/intern/cycles/kernel/closure/alloc.h
@@ -59,39 +59,10 @@ ccl_device_inline ccl_private ShaderClosure *bsdf_alloc(ccl_private ShaderData *
    * we will not allocate new closure. */
   if (sample_weight >= CLOSURE_WEIGHT_CUTOFF) {
     ccl_private ShaderClosure *sc = closure_alloc(sd, size, CLOSURE_NONE_ID, weight);
-    if (sc == NULL) {
-      return NULL;
-    }
-
-    sc->sample_weight = sample_weight;
-
-    return sc;
-  }
-
-  return NULL;
-}
-
-#ifdef __OSL__
-ccl_device_inline ShaderClosure *bsdf_alloc_osl(ShaderData *sd,
-                                                int size,
-                                                Spectrum weight,
-                                                void *data)
-{
-  kernel_assert(isfinite_safe(weight));
-
-  const float sample_weight = fabsf(average(weight));
-
-  /* Use comparison this way to help dealing with non-finite weight: if the average is not finite
-   * we will not allocate new closure. */
-  if (sample_weight >= CLOSURE_WEIGHT_CUTOFF) {
-    ShaderClosure *sc = closure_alloc(sd, size, CLOSURE_NONE_ID, weight);
     if (!sc) {
       return NULL;
     }
 
-    memcpy((void *)sc, data, size);
-
-    sc->weight = weight;
     sc->sample_weight = sample_weight;
 
     return sc;
@@ -99,6 +70,5 @@ ccl_device_inline ShaderClosure *bsdf_alloc_osl(ShaderData *sd,
 
   return NULL;
 }
-#endif
 
 CCL_NAMESPACE_END
diff --git a/intern/cycles/kernel/osl/CMakeLists.txt b/intern/cycles/kernel/osl/CMakeLists.txt
index 7570490be7c..b27bcb066fd 100644
--- a/intern/cycles/kernel/osl/CMakeLists.txt
+++ b/intern/cycles/kernel/osl/CMakeLists.txt
@@ -10,18 +10,14 @@ set(INC_SYS
 )
 
 set(SRC
-  background.cpp
-  bsdf_diffuse_ramp.cpp
-  bsdf_phong_ramp.cpp
-  emissive.cpp
-  bssrdf.cpp
   closures.cpp
   services.cpp
   shader.cpp
 )
 
 set(HEADER_SRC
-  closures.h
+  closures_setup.h
+  closures_template.h
   globals.h
   services.h
   shader.h
diff --git a/intern/cycles/kernel/osl/background.cpp b/intern/cycles/kernel/osl/background.cpp
deleted file mode 100644
index 4b5a2686117..00000000000
--- a/intern/cycles/kernel/osl/background.cpp
+++ /dev/null
@@ -1,77 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- *
- * Adapted from Open Shading Language
- * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
- * All Rights Reserved.
- *
- * Modifications Copyright 2011-2022 Blender Foundation. */
-
-#include <OpenImageIO/fmath.h>
-
-#include <OSL/genclosure.h>
-
-#include "kernel/osl/closures.h"
-
-// clang-format off
-#include "kernel/device/cpu/compat.h"
-#include "kernel/device/cpu/globals.h"
-
-#include "kernel/closure/alloc.h"
-#include "kernel/closure/emissive.h"
-
-#include "kernel/util/color.h"
-// clang-format on
-
-CCL_NAMESPACE_BEGIN
-
-using namespace OSL;
-
-/// Generic background closure
-///
-/// We only have a background closure for the shaders
-/// to return a color in background shaders. No methods,
-/// only the weight is taking into account
-///
-class GenericBackgroundClosure : public CClosurePrimitive {
- public:
-  void setup(ShaderData *sd, uint32_t /* path_flag */, float3 weight)
-  {
-    background_setup(sd, rgb_to_spectrum(weight));
-  }
-};
-
-/// Holdout closure
-///
-/// This will be used by the shader to mark the
-/// amount of holdout for the current shading
-/// point. No parameters, only the weight will be
-/// used
-///
-class HoldoutClosure : CClosurePrimitive {
- public:
-  void setup(ShaderData *sd, uint32_t /* path_flag */, float3 weight)
-  {
-    closure_alloc(sd, sizeof(ShaderClosure), CLOSURE_HOLDOUT_ID, rgb_to_spectrum(weight));
-    sd->flag |= SD_HOLDOUT;
-  }
-};
-
-ClosureParam *closure_background_params()
-{
-  static ClosureParam params[] = {
-      CLOSURE_STRING_KEYPARAM(GenericBackgroundClosure, label, "label"),
-      CLOSURE_FINISH_PARAM(GenericBackgroundClosure)};
-  return params;
-}
-
-CCLOSURE_PREPARE(closure_background_prepare, GenericBackgroundClosure)
-
-ClosureParam *closure_holdout_params()
-{
-  static ClosureParam params[] = {CLOSURE_FINISH_PARAM(HoldoutClosure)};
-  return params;
-}
-
-CCLOSURE_PREPARE(closure_holdout_prepare, HoldoutClosure)
-
-CCL_NAMESPACE_END
diff --git a/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp b/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp
deleted file mode 100644
index 667207ec6bf..00000000000
--- a/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp
+++ /dev/null
@@ -1,68 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- *
- * Adapted from Open Shading Language
- * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
- * All Rights Reserved.
- *
- * Modifications Copyright 2011-2022 Blender Foundation. */
-
-#include <OpenImageIO/fmath.h>
-
-#include <OSL/genclosure.h>
-
-#include "kernel/device/cpu/compat.h"
-#include "kernel/osl/closures.h"
-
-// clang-format off
-#include "kernel/device/cpu/compat.h"
-#include "kernel/device/cpu/globals.h"
-
-#include "kernel/types.h"
-#include "kernel/closure/alloc.h"
-#include "kernel/closure/bsdf_diffuse_ramp.h"
-#include "kernel/closure/bsdf_util.h"
-
-#include "kernel/util/color.h"
-// clang-format on
-
-CCL_NAMESPACE_BEGIN
-
-using namespace OSL;
-
-class DiffuseRampClosure : public CBSDFClosure {
- public:
-  DiffuseRampBsdf params;
-  Color3 colors[8];
-
-  void setup(ShaderData *sd, uint32_t /* path_flag */, float3 weight)
-  {
-    params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N);
-
-    DiffuseRampBsdf *bsdf = (DiffuseRampBsdf *)bsdf_alloc_osl(
-        sd, sizeof(DiffuseRampBsdf), rgb_to_spectrum(weight), &params);
-
-    if (bsdf) {
-      bsdf->colors = (float3 *)closure_alloc_extra(sd, sizeof(float3) * 8);
-
-      if (bsdf->colors) {
-        for (int i = 0; i < 8; i++)
-          bsdf->colors[i] = TO_FLOAT3(colors[i]);
-
-        sd->flag |= bsdf_diffuse_ramp_setup(bsdf);
-      }
-    }
-  }
-};
-
-ClosureParam *closure_bsdf_diffuse_ramp_params()
-{
-  static ClosureParam params[] = {CLOSURE_FLOAT3_PARAM(DiffuseRampClosure, params.N),
-                                  CLOSURE_COLOR_ARRAY_PARAM(DiffuseRampClosure, colors, 8),
-                                  CLOSURE_STRING_KEYPARAM(DiffuseRampClosure, label, "label"),
-                                  CLOSURE_FINISH_PARAM(DiffuseRampClosure)};
-  return params;
-}
-
-CCLOSURE_PREPARE(closure_bsdf_diffuse_ramp_prepare, DiffuseRampClosure)
-
-CCL_NAMESPACE_END
diff --git a/intern/cycles/kernel/osl/bsdf_phong_ramp.cpp b/intern/cycles/kernel/osl/bsdf_phong_ramp.cpp
deleted file mode 100644
index 6f54a96e542..00000000000
--- a/intern/cycles/kernel/osl/bsdf_phong_ramp.cpp
+++ /dev/null
@@ -1,69 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- *
- * Adapted from Open Shading Language
- * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
- * All Rights Reserved.
- *
- * Modifications Copyright 2011-2022 Blender Foundation. */
-
-#include <OpenImageIO/fmath.h>
-
-#include <OSL/genclosure.h>
-
-#include "kernel/device/cpu/compat.h"
-#include "kernel/osl/closures.h"
-
-// clang-format off
-#include "kernel/device/cpu/compat.h"
-#include "kernel/device/cpu/globals.h"
-
-#include "kernel/types.h"
-#include "kernel/closure/alloc.h"
-#include "kernel/closure/bsdf_phong_ramp.h"
-#include "kernel/closure/bsdf_util.h"
-
-#include "kernel/util/color.h"
-// clang-format on
-
-CCL_NAMESPACE_BEGIN
-
-using namespace OSL;
-
-class PhongRampClosure : public CBSDFClosure {
- public:
-  PhongRampBsdf params;
-  Color3 colors[8];
-
-  void setup(ShaderData *sd, uint32_t /* path_flag */, float3 weight)
-  {
-    params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N);
-
-    PhongRampBsdf *bsdf = (PhongRampBsdf *)bsdf_alloc_osl(
-        sd, sizeof(PhongRampBsdf), rgb_to_spectrum(weight), &params);
-
-    if (bsdf) {
-      bsdf->colors = (float3 *)closure_alloc_extra(sd, sizeof(float3) * 8);
-
-      if (bsdf->colors) {
-        for (int i = 0; i < 8; i++)
-          bsdf->colors[i] = TO_FLOAT3(colors[i]);
-
-        sd->flag |= bsdf_phong_ramp_setup(bsdf);
-      }
-    }
-  }
-};
-
-ClosureParam *closure_bsdf_phong_ramp_params()
-{
-  static ClosureParam params[] = {CLOSURE_FLOAT3_PARAM(PhongRampClosure, params.N),
-                                  CLOSURE_FLOAT_PARAM(PhongRampClosure, params.exponent),
-                                  CLOSURE_COLOR_ARRAY_PARAM(PhongRampClosure, colors, 8),
-                                  CLOSURE_STRING_KEYPARAM(PhongRampClosure, label, "label"),
-                                  CLOSURE_FINISH_PARAM(PhongRampClosure)};
-  return params;
-}
-
-CCLOSURE_PREPARE(closure_bsdf_phong_ramp_prepare, PhongRampClosure)
-
-CCL_NAMESPACE_END
diff --git a/intern/cycles/kernel/osl/bssrdf.cpp b/intern/cycles/kernel/osl/bssrdf.cpp
deleted file mode 100644
index 3054946ba5a..00000000000
--- a/intern/cycles/kernel/osl/bssrdf.cpp
+++ /dev/null
@@ -1,105 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- *
- * Adapted from Open Shading Language
- * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
- * All Rights Reserved.
- *
- * Modifications Copyright 2011-2022 Blender Foundation. */
-
-#include <OSL/genclosure.h>
-
-#include "kernel/device/cpu/compat.h"
-#include "kernel/osl/closures.h"
-
-// clang-format off
-#include "kernel/device/cpu/compat.h"
-#include "kernel/device/cpu/globals.h"
-
-#include "kernel/types.h"
-
-#include "kernel/closure/alloc.h"
-#include "kernel/closure/bsdf_util.h"
-#include "kernel/closure/bsdf_diffuse.h"
-#include "kernel/closure/bsdf_principled_diffuse.h"
-#include "kernel/closure/bssrdf.h"
-
-#include "kernel/util/color.h"
-// clang-format on
-
-CCL_NAMESPACE_BEGIN
-
-using namespace OSL;
-
-static ustring u_burley("burley");
-static ustring u_random_wal

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list