Blender  V3.3
bsdf_diffuse.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Adapted from Open Shading Language
4  * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
5  * All Rights Reserved.
6  *
7  * Modifications Copyright 2011-2022 Blender Foundation. */
8 
9 #pragma once
10 
11 #include "kernel/sample/mapping.h"
12 
14 
15 typedef struct DiffuseBsdf {
18 
19 static_assert(sizeof(ShaderClosure) >= sizeof(DiffuseBsdf), "DiffuseBsdf is too large!");
20 
21 /* DIFFUSE */
22 
24 {
25  bsdf->type = CLOSURE_BSDF_DIFFUSE_ID;
26  return SD_BSDF | SD_BSDF_HAS_EVAL;
27 }
28 
30  const float3 I,
31  const float3 omega_in,
32  ccl_private float *pdf)
33 {
34  ccl_private const DiffuseBsdf *bsdf = (ccl_private const DiffuseBsdf *)sc;
35  float3 N = bsdf->N;
36 
37  float cos_pi = fmaxf(dot(N, omega_in), 0.0f) * M_1_PI_F;
38  *pdf = cos_pi;
39  return make_float3(cos_pi, cos_pi, cos_pi);
40 }
41 
43  const float3 I,
44  const float3 omega_in,
45  ccl_private float *pdf)
46 {
47  *pdf = 0.0f;
48  return make_float3(0.0f, 0.0f, 0.0f);
49 }
50 
52  float3 Ng,
53  float3 I,
54  float3 dIdx,
55  float3 dIdy,
56  float randu,
57  float randv,
58  ccl_private float3 *eval,
59  ccl_private float3 *omega_in,
60  ccl_private float3 *domega_in_dx,
61  ccl_private float3 *domega_in_dy,
62  ccl_private float *pdf)
63 {
64  ccl_private const DiffuseBsdf *bsdf = (ccl_private const DiffuseBsdf *)sc;
65  float3 N = bsdf->N;
66 
67  // distribution over the hemisphere
68  sample_cos_hemisphere(N, randu, randv, omega_in, pdf);
69 
70  if (dot(Ng, *omega_in) > 0.0f) {
71  *eval = make_float3(*pdf, *pdf, *pdf);
72 #ifdef __RAY_DIFFERENTIALS__
73  // TODO: find a better approximation for the diffuse bounce
74  *domega_in_dx = (2 * dot(N, dIdx)) * N - dIdx;
75  *domega_in_dy = (2 * dot(N, dIdy)) * N - dIdy;
76 #endif
77  }
78  else {
79  *pdf = 0.0f;
80  *eval = make_float3(0.0f, 0.0f, 0.0f);
81  }
83 }
84 
85 /* TRANSLUCENT */
86 
88 {
89  bsdf->type = CLOSURE_BSDF_TRANSLUCENT_ID;
90  return SD_BSDF | SD_BSDF_HAS_EVAL;
91 }
92 
94  const float3 I,
95  const float3 omega_in,
96  ccl_private float *pdf)
97 {
98  *pdf = 0.0f;
99  return make_float3(0.0f, 0.0f, 0.0f);
100 }
101 
103  const float3 I,
104  const float3 omega_in,
105  ccl_private float *pdf)
106 {
107  ccl_private const DiffuseBsdf *bsdf = (ccl_private const DiffuseBsdf *)sc;
108  float3 N = bsdf->N;
109 
110  float cos_pi = fmaxf(-dot(N, omega_in), 0.0f) * M_1_PI_F;
111  *pdf = cos_pi;
112  return make_float3(cos_pi, cos_pi, cos_pi);
113 }
114 
116  float3 Ng,
117  float3 I,
118  float3 dIdx,
119  float3 dIdy,
120  float randu,
121  float randv,
122  ccl_private float3 *eval,
123  ccl_private float3 *omega_in,
124  ccl_private float3 *domega_in_dx,
125  ccl_private float3 *domega_in_dy,
126  ccl_private float *pdf)
127 {
128  ccl_private const DiffuseBsdf *bsdf = (ccl_private const DiffuseBsdf *)sc;
129  float3 N = bsdf->N;
130 
131  // we are viewing the surface from the right side - send a ray out with cosine
132  // distribution over the hemisphere
133  sample_cos_hemisphere(-N, randu, randv, omega_in, pdf);
134  if (dot(Ng, *omega_in) < 0) {
135  *eval = make_float3(*pdf, *pdf, *pdf);
136 #ifdef __RAY_DIFFERENTIALS__
137  // TODO: find a better approximation for the diffuse bounce
138  *domega_in_dx = -((2 * dot(N, dIdx)) * N - dIdx);
139  *domega_in_dy = -((2 * dot(N, dIdy)) * N - dIdy);
140 #endif
141  }
142  else {
143  *pdf = 0;
144  *eval = make_float3(0.0f, 0.0f, 0.0f);
145  }
146  return LABEL_TRANSMIT | LABEL_DIFFUSE;
147 }
148 
ccl_device float3 bsdf_translucent_eval_transmit(ccl_private const ShaderClosure *sc, const float3 I, const float3 omega_in, ccl_private float *pdf)
Definition: bsdf_diffuse.h:102
ccl_device float3 bsdf_diffuse_eval_reflect(ccl_private const ShaderClosure *sc, const float3 I, const float3 omega_in, ccl_private float *pdf)
Definition: bsdf_diffuse.h:29
ccl_device float3 bsdf_translucent_eval_reflect(ccl_private const ShaderClosure *sc, const float3 I, const float3 omega_in, ccl_private float *pdf)
Definition: bsdf_diffuse.h:93
ccl_device int bsdf_translucent_setup(ccl_private DiffuseBsdf *bsdf)
Definition: bsdf_diffuse.h:87
ccl_device int bsdf_diffuse_setup(ccl_private DiffuseBsdf *bsdf)
Definition: bsdf_diffuse.h:23
ccl_device float3 bsdf_diffuse_eval_transmit(ccl_private const ShaderClosure *sc, const float3 I, const float3 omega_in, ccl_private float *pdf)
Definition: bsdf_diffuse.h:42
ccl_device int bsdf_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)
Definition: bsdf_diffuse.h:51
ccl_device int bsdf_translucent_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)
Definition: bsdf_diffuse.h:115
CCL_NAMESPACE_BEGIN struct DiffuseBsdf DiffuseBsdf
#define ccl_device
Definition: cuda/compat.h:32
#define ccl_private
Definition: cuda/compat.h:48
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
@ CLOSURE_BSDF_DIFFUSE_ID
@ CLOSURE_BSDF_TRANSLUCENT_ID
@ SD_BSDF_HAS_EVAL
Definition: kernel/types.h:744
@ SD_BSDF
Definition: kernel/types.h:742
@ LABEL_TRANSMIT
Definition: kernel/types.h:317
@ LABEL_DIFFUSE
Definition: kernel/types.h:319
@ LABEL_REFLECT
Definition: kernel/types.h:318
ShaderClosure
Definition: kernel/types.h:726
#define N
#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