Blender  V2.93
bsdf_diffuse.h
Go to the documentation of this file.
1 /*
2  * Adapted from Open Shading Language with this license:
3  *
4  * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
5  * All Rights Reserved.
6  *
7  * Modifications Copyright 2011, Blender Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of Sony Pictures Imageworks nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef __BSDF_DIFFUSE_H__
34 #define __BSDF_DIFFUSE_H__
35 
37 
38 typedef ccl_addr_space struct DiffuseBsdf {
41 
42 static_assert(sizeof(ShaderClosure) >= sizeof(DiffuseBsdf), "DiffuseBsdf is too large!");
43 
44 /* DIFFUSE */
45 
47 {
48  bsdf->type = CLOSURE_BSDF_DIFFUSE_ID;
49  return SD_BSDF | SD_BSDF_HAS_EVAL;
50 }
51 
53 {
54  const DiffuseBsdf *bsdf_a = (const DiffuseBsdf *)a;
55  const DiffuseBsdf *bsdf_b = (const DiffuseBsdf *)b;
56 
57  return (isequal_float3(bsdf_a->N, bsdf_b->N));
58 }
59 
61  const float3 I,
62  const float3 omega_in,
63  float *pdf)
64 {
65  const DiffuseBsdf *bsdf = (const DiffuseBsdf *)sc;
66  float3 N = bsdf->N;
67 
68  float cos_pi = fmaxf(dot(N, omega_in), 0.0f) * M_1_PI_F;
69  *pdf = cos_pi;
70  return make_float3(cos_pi, cos_pi, cos_pi);
71 }
72 
74  const float3 I,
75  const float3 omega_in,
76  float *pdf)
77 {
78  return make_float3(0.0f, 0.0f, 0.0f);
79 }
80 
82  float3 Ng,
83  float3 I,
84  float3 dIdx,
85  float3 dIdy,
86  float randu,
87  float randv,
88  float3 *eval,
89  float3 *omega_in,
90  float3 *domega_in_dx,
91  float3 *domega_in_dy,
92  float *pdf)
93 {
94  const DiffuseBsdf *bsdf = (const DiffuseBsdf *)sc;
95  float3 N = bsdf->N;
96 
97  // distribution over the hemisphere
98  sample_cos_hemisphere(N, randu, randv, omega_in, pdf);
99 
100  if (dot(Ng, *omega_in) > 0.0f) {
101  *eval = make_float3(*pdf, *pdf, *pdf);
102 #ifdef __RAY_DIFFERENTIALS__
103  // TODO: find a better approximation for the diffuse bounce
104  *domega_in_dx = (2 * dot(N, dIdx)) * N - dIdx;
105  *domega_in_dy = (2 * dot(N, dIdy)) * N - dIdy;
106 #endif
107  }
108  else
109  *pdf = 0.0f;
110 
111  return LABEL_REFLECT | LABEL_DIFFUSE;
112 }
113 
114 /* TRANSLUCENT */
115 
117 {
118  bsdf->type = CLOSURE_BSDF_TRANSLUCENT_ID;
119  return SD_BSDF | SD_BSDF_HAS_EVAL;
120 }
121 
123  const float3 I,
124  const float3 omega_in,
125  float *pdf)
126 {
127  return make_float3(0.0f, 0.0f, 0.0f);
128 }
129 
131  const float3 I,
132  const float3 omega_in,
133  float *pdf)
134 {
135  const DiffuseBsdf *bsdf = (const DiffuseBsdf *)sc;
136  float3 N = bsdf->N;
137 
138  float cos_pi = fmaxf(-dot(N, omega_in), 0.0f) * M_1_PI_F;
139  *pdf = cos_pi;
140  return make_float3(cos_pi, cos_pi, cos_pi);
141 }
142 
144  float3 Ng,
145  float3 I,
146  float3 dIdx,
147  float3 dIdy,
148  float randu,
149  float randv,
150  float3 *eval,
151  float3 *omega_in,
152  float3 *domega_in_dx,
153  float3 *domega_in_dy,
154  float *pdf)
155 {
156  const DiffuseBsdf *bsdf = (const DiffuseBsdf *)sc;
157  float3 N = bsdf->N;
158 
159  // we are viewing the surface from the right side - send a ray out with cosine
160  // distribution over the hemisphere
161  sample_cos_hemisphere(-N, randu, randv, omega_in, pdf);
162  if (dot(Ng, *omega_in) < 0) {
163  *eval = make_float3(*pdf, *pdf, *pdf);
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;
172  }
173  return LABEL_TRANSMIT | LABEL_DIFFUSE;
174 }
175 
177 
178 #endif /* __BSDF_DIFFUSE_H__ */
CCL_NAMESPACE_BEGIN typedef ccl_addr_space struct DiffuseBsdf DiffuseBsdf
ccl_device float3 bsdf_diffuse_eval_reflect(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
Definition: bsdf_diffuse.h:60
ccl_device int bsdf_diffuse_setup(DiffuseBsdf *bsdf)
Definition: bsdf_diffuse.h:46
ccl_device int bsdf_diffuse_sample(const ShaderClosure *sc, float3 Ng, float3 I, float3 dIdx, float3 dIdy, float randu, float randv, float3 *eval, float3 *omega_in, float3 *domega_in_dx, float3 *domega_in_dy, float *pdf)
Definition: bsdf_diffuse.h:81
ccl_device bool bsdf_diffuse_merge(const ShaderClosure *a, const ShaderClosure *b)
Definition: bsdf_diffuse.h:52
ccl_device float3 bsdf_translucent_eval_transmit(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
Definition: bsdf_diffuse.h:130
ccl_device float3 bsdf_translucent_eval_reflect(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
Definition: bsdf_diffuse.h:122
ccl_device float3 bsdf_diffuse_eval_transmit(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
Definition: bsdf_diffuse.h:73
ccl_device int bsdf_translucent_setup(DiffuseBsdf *bsdf)
Definition: bsdf_diffuse.h:116
ccl_device int bsdf_translucent_sample(const ShaderClosure *sc, float3 Ng, float3 I, float3 dIdx, float3 dIdy, float randu, float randv, float3 *eval, float3 *omega_in, float3 *domega_in_dx, float3 *domega_in_dy, float *pdf)
Definition: bsdf_diffuse.h:143
#define ccl_addr_space
#define ccl_device
#define CCL_NAMESPACE_END
#define fmaxf(x, y)
#define make_float3(x, y, z)
ccl_device_inline void sample_cos_hemisphere(const float3 N, float randu, float randv, float3 *omega_in, float *pdf)
@ SD_BSDF_HAS_EVAL
Definition: kernel_types.h:849
@ SD_BSDF
Definition: kernel_types.h:847
@ LABEL_TRANSMIT
Definition: kernel_types.h:328
@ LABEL_DIFFUSE
Definition: kernel_types.h:330
@ LABEL_REFLECT
Definition: kernel_types.h:329
ShaderClosure
Definition: kernel_types.h:831
static unsigned a[3]
Definition: RandGen.cpp:92
#define I
params N
@ CLOSURE_BSDF_DIFFUSE_ID
Definition: svm_types.h:534
@ CLOSURE_BSDF_TRANSLUCENT_ID
Definition: svm_types.h:540
#define M_1_PI_F
Definition: util_math.h:52
ccl_device_inline float dot(const float2 &a, const float2 &b)
ccl_device_inline bool isequal_float3(const float3 a, const float3 b)