Blender  V2.93
bsdf_toon.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_TOON_H__
34 #define __BSDF_TOON_H__
35 
37 
38 typedef ccl_addr_space struct ToonBsdf {
40 
41  float size;
42  float smooth;
44 
45 static_assert(sizeof(ShaderClosure) >= sizeof(ToonBsdf), "ToonBsdf is too large!");
46 
47 /* DIFFUSE TOON */
48 
50 {
51  bsdf->type = CLOSURE_BSDF_DIFFUSE_TOON_ID;
52  bsdf->size = saturate(bsdf->size);
53  bsdf->smooth = saturate(bsdf->smooth);
54 
55  return SD_BSDF | SD_BSDF_HAS_EVAL;
56 }
57 
59 {
60  const ToonBsdf *bsdf_a = (const ToonBsdf *)a;
61  const ToonBsdf *bsdf_b = (const ToonBsdf *)b;
62 
63  return (isequal_float3(bsdf_a->N, bsdf_b->N)) && (bsdf_a->size == bsdf_b->size) &&
64  (bsdf_a->smooth == bsdf_b->smooth);
65 }
66 
67 ccl_device float3 bsdf_toon_get_intensity(float max_angle, float smooth, float angle)
68 {
69  float is;
70 
71  if (angle < max_angle)
72  is = 1.0f;
73  else if (angle < (max_angle + smooth) && smooth != 0.0f)
74  is = (1.0f - (angle - max_angle) / smooth);
75  else
76  is = 0.0f;
77 
78  return make_float3(is, is, is);
79 }
80 
81 ccl_device float bsdf_toon_get_sample_angle(float max_angle, float smooth)
82 {
83  return fminf(max_angle + smooth, M_PI_2_F);
84 }
85 
87  const float3 I,
88  const float3 omega_in,
89  float *pdf)
90 {
91  const ToonBsdf *bsdf = (const ToonBsdf *)sc;
92  float max_angle = bsdf->size * M_PI_2_F;
93  float smooth = bsdf->smooth * M_PI_2_F;
94  float angle = safe_acosf(fmaxf(dot(bsdf->N, omega_in), 0.0f));
95 
96  float3 eval = bsdf_toon_get_intensity(max_angle, smooth, angle);
97 
98  if (eval.x > 0.0f) {
99  float sample_angle = bsdf_toon_get_sample_angle(max_angle, smooth);
100 
101  *pdf = 0.5f * M_1_PI_F / (1.0f - cosf(sample_angle));
102  return *pdf * eval;
103  }
104 
105  return make_float3(0.0f, 0.0f, 0.0f);
106 }
107 
109  const float3 I,
110  const float3 omega_in,
111  float *pdf)
112 {
113  return make_float3(0.0f, 0.0f, 0.0f);
114 }
115 
117  float3 Ng,
118  float3 I,
119  float3 dIdx,
120  float3 dIdy,
121  float randu,
122  float randv,
123  float3 *eval,
124  float3 *omega_in,
125  float3 *domega_in_dx,
126  float3 *domega_in_dy,
127  float *pdf)
128 {
129  const ToonBsdf *bsdf = (const ToonBsdf *)sc;
130  float max_angle = bsdf->size * M_PI_2_F;
131  float smooth = bsdf->smooth * M_PI_2_F;
132  float sample_angle = bsdf_toon_get_sample_angle(max_angle, smooth);
133  float angle = sample_angle * randu;
134 
135  if (sample_angle > 0.0f) {
136  sample_uniform_cone(bsdf->N, sample_angle, randu, randv, omega_in, pdf);
137 
138  if (dot(Ng, *omega_in) > 0.0f) {
139  *eval = *pdf * bsdf_toon_get_intensity(max_angle, smooth, angle);
140 
141 #ifdef __RAY_DIFFERENTIALS__
142  // TODO: find a better approximation for the bounce
143  *domega_in_dx = (2.0f * dot(bsdf->N, dIdx)) * bsdf->N - dIdx;
144  *domega_in_dy = (2.0f * dot(bsdf->N, dIdy)) * bsdf->N - dIdy;
145 #endif
146  }
147  else
148  *pdf = 0.0f;
149  }
150 
151  return LABEL_REFLECT | LABEL_DIFFUSE;
152 }
153 
154 /* GLOSSY TOON */
155 
157 {
158  bsdf->type = CLOSURE_BSDF_GLOSSY_TOON_ID;
159  bsdf->size = saturate(bsdf->size);
160  bsdf->smooth = saturate(bsdf->smooth);
161 
162  return SD_BSDF | SD_BSDF_HAS_EVAL;
163 }
164 
166  const float3 I,
167  const float3 omega_in,
168  float *pdf)
169 {
170  const ToonBsdf *bsdf = (const ToonBsdf *)sc;
171  float max_angle = bsdf->size * M_PI_2_F;
172  float smooth = bsdf->smooth * M_PI_2_F;
173  float cosNI = dot(bsdf->N, omega_in);
174  float cosNO = dot(bsdf->N, I);
175 
176  if (cosNI > 0 && cosNO > 0) {
177  /* reflect the view vector */
178  float3 R = (2 * cosNO) * bsdf->N - I;
179  float cosRI = dot(R, omega_in);
180 
181  float angle = safe_acosf(fmaxf(cosRI, 0.0f));
182 
183  float3 eval = bsdf_toon_get_intensity(max_angle, smooth, angle);
184  float sample_angle = bsdf_toon_get_sample_angle(max_angle, smooth);
185 
186  *pdf = 0.5f * M_1_PI_F / (1.0f - cosf(sample_angle));
187  return *pdf * eval;
188  }
189 
190  return make_float3(0.0f, 0.0f, 0.0f);
191 }
192 
194  const float3 I,
195  const float3 omega_in,
196  float *pdf)
197 {
198  return make_float3(0.0f, 0.0f, 0.0f);
199 }
200 
202  float3 Ng,
203  float3 I,
204  float3 dIdx,
205  float3 dIdy,
206  float randu,
207  float randv,
208  float3 *eval,
209  float3 *omega_in,
210  float3 *domega_in_dx,
211  float3 *domega_in_dy,
212  float *pdf)
213 {
214  const ToonBsdf *bsdf = (const ToonBsdf *)sc;
215  float max_angle = bsdf->size * M_PI_2_F;
216  float smooth = bsdf->smooth * M_PI_2_F;
217  float cosNO = dot(bsdf->N, I);
218 
219  if (cosNO > 0) {
220  /* reflect the view vector */
221  float3 R = (2 * cosNO) * bsdf->N - I;
222 
223  float sample_angle = bsdf_toon_get_sample_angle(max_angle, smooth);
224  float angle = sample_angle * randu;
225 
226  sample_uniform_cone(R, sample_angle, randu, randv, omega_in, pdf);
227 
228  if (dot(Ng, *omega_in) > 0.0f) {
229  float cosNI = dot(bsdf->N, *omega_in);
230 
231  /* make sure the direction we chose is still in the right hemisphere */
232  if (cosNI > 0) {
233  *eval = *pdf * bsdf_toon_get_intensity(max_angle, smooth, angle);
234 
235 #ifdef __RAY_DIFFERENTIALS__
236  *domega_in_dx = (2 * dot(bsdf->N, dIdx)) * bsdf->N - dIdx;
237  *domega_in_dy = (2 * dot(bsdf->N, dIdy)) * bsdf->N - dIdy;
238 #endif
239  }
240  else
241  *pdf = 0.0f;
242  }
243  else
244  *pdf = 0.0f;
245  }
246 
247  return LABEL_GLOSSY | LABEL_REFLECT;
248 }
249 
251 
252 #endif /* __BSDF_TOON_H__ */
MINLINE float safe_acosf(float a)
ccl_device float3 bsdf_diffuse_toon_eval_transmit(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
Definition: bsdf_toon.h:108
ccl_device int bsdf_glossy_toon_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_toon.h:201
ccl_device float3 bsdf_diffuse_toon_eval_reflect(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
Definition: bsdf_toon.h:86
ccl_device float bsdf_toon_get_sample_angle(float max_angle, float smooth)
Definition: bsdf_toon.h:81
ccl_device float3 bsdf_glossy_toon_eval_transmit(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
Definition: bsdf_toon.h:193
CCL_NAMESPACE_BEGIN typedef ccl_addr_space struct ToonBsdf ToonBsdf
ccl_device float3 bsdf_glossy_toon_eval_reflect(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
Definition: bsdf_toon.h:165
ccl_device int bsdf_diffuse_toon_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_toon.h:116
ccl_device bool bsdf_toon_merge(const ShaderClosure *a, const ShaderClosure *b)
Definition: bsdf_toon.h:58
ccl_device int bsdf_diffuse_toon_setup(ToonBsdf *bsdf)
Definition: bsdf_toon.h:49
ccl_device int bsdf_glossy_toon_setup(ToonBsdf *bsdf)
Definition: bsdf_toon.h:156
ccl_device float3 bsdf_toon_get_intensity(float max_angle, float smooth, float angle)
Definition: bsdf_toon.h:67
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
#define ccl_addr_space
#define cosf(x)
#define ccl_device
#define CCL_NAMESPACE_END
#define fmaxf(x, y)
#define fminf(x, y)
#define make_float3(x, y, z)
ccl_device_inline void sample_uniform_cone(const float3 N, float angle, 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_DIFFUSE
Definition: kernel_types.h:330
@ LABEL_GLOSSY
Definition: kernel_types.h:331
@ LABEL_REFLECT
Definition: kernel_types.h:329
ShaderClosure
Definition: kernel_types.h:831
#define R
static unsigned a[3]
Definition: RandGen.cpp:92
#define I
float smooth
Definition: bsdf_toon.h:42
float size
Definition: bsdf_toon.h:41
SHADER_CLOSURE_BASE
Definition: bsdf_toon.h:39
float x
Definition: sky_float3.h:35
@ CLOSURE_BSDF_DIFFUSE_TOON_ID
Definition: svm_types.h:539
@ CLOSURE_BSDF_GLOSSY_TOON_ID
Definition: svm_types.h:553
ccl_device_inline float saturate(float a)
Definition: util_math.h:315
#define M_PI_2_F
Definition: util_math.h:46
#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)