Blender  V3.3
bsdf_principled_diffuse.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 
6 /* DISNEY PRINCIPLED DIFFUSE BRDF
7  *
8  * Shading model by Brent Burley (Disney): "Physically Based Shading at Disney" (2012)
9  *
10  * "Extending the Disney BRDF to a BSDF with Integrated Subsurface Scattering" (2015)
11  * For the separation of retro-reflection, "2.3 Dielectric BRDF with integrated
12  * subsurface scattering"
13  */
14 
16 
17 #include "kernel/sample/mapping.h"
18 
20 
26 };
27 
28 typedef struct PrincipledDiffuseBsdf {
30 
31  float roughness;
34 
35 static_assert(sizeof(ShaderClosure) >= sizeof(PrincipledDiffuseBsdf),
36  "PrincipledDiffuseBsdf is too large!");
37 
39 {
41  bsdf->components = PRINCIPLED_DIFFUSE_FULL;
42  return SD_BSDF | SD_BSDF_HAS_EVAL;
43 }
44 
47  float3 N,
48  float3 V,
49  float3 L,
50  ccl_private float *pdf)
51 {
52  const float NdotL = dot(N, L);
53 
54  if (NdotL <= 0) {
55  return make_float3(0.0f, 0.0f, 0.0f);
56  }
57 
58  const float NdotV = dot(N, V);
59 
60  const float FV = schlick_fresnel(NdotV);
61  const float FL = schlick_fresnel(NdotL);
62 
63  float f = 0.0f;
64 
65  /* Lambertian component. */
66  if (bsdf->components & (PRINCIPLED_DIFFUSE_FULL | PRINCIPLED_DIFFUSE_LAMBERT)) {
67  f += (1.0f - 0.5f * FV) * (1.0f - 0.5f * FL);
68  }
69  else if (bsdf->components & PRINCIPLED_DIFFUSE_LAMBERT_EXIT) {
70  f += (1.0f - 0.5f * FL);
71  }
72 
73  /* Retro-reflection component. */
75  /* H = normalize(L + V); // Bisector of an angle between L and V
76  * LH2 = 2 * dot(L, H)^2 = 2cos(x)^2 = cos(2x) + 1 = dot(L, V) + 1,
77  * half-angle x between L and V is at most 90 deg. */
78  const float LH2 = dot(L, V) + 1;
79  const float RR = bsdf->roughness * LH2;
80  f += RR * (FL + FV + FL * FV * (RR - 1.0f));
81  }
82 
83  float value = M_1_PI_F * NdotL * f;
84 
85  return make_float3(value, value, value);
86 }
87 
88 /* Compute Fresnel at entry point, to be combined with #PRINCIPLED_DIFFUSE_LAMBERT_EXIT
89  * at the exit point to get the complete BSDF. */
91 {
92  const float FV = schlick_fresnel(NdotV);
93  return (1.0f - 0.5f * FV);
94 }
95 
96 /* Ad-hoc weight adjustment to avoid retro-reflection taking away half the
97  * samples from BSSRDF. */
100 {
101  return bsdf->roughness * schlick_fresnel(dot(bsdf->N, I));
102 }
103 
105  int components)
106 {
108  bsdf->components = components;
109  return SD_BSDF | SD_BSDF_HAS_EVAL;
110 }
111 
113  const float3 I,
114  const float3 omega_in,
115  ccl_private float *pdf)
116 {
118 
119  float3 N = bsdf->N;
120  float3 V = I; // outgoing
121  float3 L = omega_in; // incoming
122 
123  if (dot(N, omega_in) > 0.0f) {
124  *pdf = fmaxf(dot(N, omega_in), 0.0f) * M_1_PI_F;
125  return bsdf_principled_diffuse_compute_brdf(bsdf, N, V, L, pdf);
126  }
127  else {
128  *pdf = 0.0f;
129  return make_float3(0.0f, 0.0f, 0.0f);
130  }
131 }
132 
134  const float3 I,
135  const float3 omega_in,
136  ccl_private float *pdf)
137 {
138  *pdf = 0.0f;
139  return make_float3(0.0f, 0.0f, 0.0f);
140 }
141 
143  float3 Ng,
144  float3 I,
145  float3 dIdx,
146  float3 dIdy,
147  float randu,
148  float randv,
149  ccl_private float3 *eval,
150  ccl_private float3 *omega_in,
151  ccl_private float3 *domega_in_dx,
152  ccl_private float3 *domega_in_dy,
153  ccl_private float *pdf)
154 {
156 
157  float3 N = bsdf->N;
158 
159  sample_cos_hemisphere(N, randu, randv, omega_in, pdf);
160 
161  if (dot(Ng, *omega_in) > 0) {
162  *eval = bsdf_principled_diffuse_compute_brdf(bsdf, N, I, *omega_in, pdf);
163 
164 #ifdef __RAY_DIFFERENTIALS__
165  // TODO: find a better approximation for the diffuse bounce
166  *domega_in_dx = -((2 * dot(N, dIdx)) * N - dIdx);
167  *domega_in_dy = -((2 * dot(N, dIdy)) * N - dIdy);
168 #endif
169  }
170  else {
171  *pdf = 0.0f;
172  *eval = make_float3(0.0f, 0.0f, 0.0f);
173  }
174  return LABEL_REFLECT | LABEL_DIFFUSE;
175 }
176 
ccl_device float3 bsdf_principled_diffuse_eval_reflect(ccl_private const ShaderClosure *sc, const float3 I, const float3 omega_in, ccl_private float *pdf)
ccl_device int bsdf_principled_diffuse_sample(ccl_private const ShaderClosure *sc, float3 Ng, float3 I, float3 dIdx, float3 dIdy, float randu, float randv, ccl_private float3 *eval, ccl_private float3 *omega_in, ccl_private float3 *domega_in_dx, ccl_private float3 *domega_in_dy, ccl_private float *pdf)
ccl_device_inline float bsdf_principled_diffuse_retro_reflection_sample_weight(ccl_private PrincipledDiffuseBsdf *bsdf, const float3 I)
ccl_device float3 bsdf_principled_diffuse_eval_transmit(ccl_private const ShaderClosure *sc, const float3 I, const float3 omega_in, ccl_private float *pdf)
ccl_device int bsdf_principled_diffuse_setup(ccl_private PrincipledDiffuseBsdf *bsdf)
PrincipledDiffuseBsdfComponents
@ PRINCIPLED_DIFFUSE_RETRO_REFLECTION
@ PRINCIPLED_DIFFUSE_LAMBERT_EXIT
@ PRINCIPLED_DIFFUSE_LAMBERT
@ PRINCIPLED_DIFFUSE_FULL
ccl_device_inline float bsdf_principled_diffuse_compute_entry_fresnel(const float NdotV)
struct PrincipledDiffuseBsdf PrincipledDiffuseBsdf
ccl_device float3 bsdf_principled_diffuse_compute_brdf(ccl_private const PrincipledDiffuseBsdf *bsdf, float3 N, float3 V, float3 L, ccl_private float *pdf)
ccl_device float schlick_fresnel(float u)
Definition: bsdf_util.h:105
#define ccl_device
Definition: cuda/compat.h:32
#define ccl_private
Definition: cuda/compat.h:48
#define ccl_device_inline
Definition: cuda/compat.h:34
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
@ CLOSURE_BSDF_PRINCIPLED_DIFFUSE_ID
@ SD_BSDF_HAS_EVAL
Definition: kernel/types.h:744
@ SD_BSDF
Definition: kernel/types.h:742
@ LABEL_DIFFUSE
Definition: kernel/types.h:319
@ LABEL_REFLECT
Definition: kernel/types.h:318
ShaderClosure
Definition: kernel/types.h:726
#define N
#define L
#define fmaxf(x, y)
Definition: metal/compat.h:228
#define make_float3(x, y, z)
Definition: metal/compat.h:204
T dot(const vec_base< T, Size > &a, const vec_base< T, Size > &b)
#define I
ccl_device_inline void sample_cos_hemisphere(const float3 N, float randu, float randv, ccl_private float3 *omega_in, ccl_private float *pdf)
#define M_1_PI_F
Definition: util/math.h:43
CCL_NAMESPACE_BEGIN struct Window V