Blender  V2.93
bsdf_ashikhmin_velvet.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_ASHIKHMIN_VELVET_H__
34 #define __BSDF_ASHIKHMIN_VELVET_H__
35 
37 
38 typedef ccl_addr_space struct VelvetBsdf {
40 
41  float sigma;
42  float invsigma2;
44 
45 static_assert(sizeof(ShaderClosure) >= sizeof(VelvetBsdf), "VelvetBsdf is too large!");
46 
48 {
49  float sigma = fmaxf(bsdf->sigma, 0.01f);
50  bsdf->invsigma2 = 1.0f / (sigma * sigma);
51 
53 
54  return SD_BSDF | SD_BSDF_HAS_EVAL;
55 }
56 
58 {
59  const VelvetBsdf *bsdf_a = (const VelvetBsdf *)a;
60  const VelvetBsdf *bsdf_b = (const VelvetBsdf *)b;
61 
62  return (isequal_float3(bsdf_a->N, bsdf_b->N)) && (bsdf_a->sigma == bsdf_b->sigma);
63 }
64 
66  const float3 I,
67  const float3 omega_in,
68  float *pdf)
69 {
70  const VelvetBsdf *bsdf = (const VelvetBsdf *)sc;
71  float m_invsigma2 = bsdf->invsigma2;
72  float3 N = bsdf->N;
73 
74  float cosNO = dot(N, I);
75  float cosNI = dot(N, omega_in);
76  if (cosNO > 0 && cosNI > 0) {
77  float3 H = normalize(omega_in + I);
78 
79  float cosNH = dot(N, H);
80  float cosHO = fabsf(dot(I, H));
81 
82  if (!(fabsf(cosNH) < 1.0f - 1e-5f && cosHO > 1e-5f))
83  return make_float3(0.0f, 0.0f, 0.0f);
84 
85  float cosNHdivHO = cosNH / cosHO;
86  cosNHdivHO = fmaxf(cosNHdivHO, 1e-5f);
87 
88  float fac1 = 2 * fabsf(cosNHdivHO * cosNO);
89  float fac2 = 2 * fabsf(cosNHdivHO * cosNI);
90 
91  float sinNH2 = 1 - cosNH * cosNH;
92  float sinNH4 = sinNH2 * sinNH2;
93  float cotangent2 = (cosNH * cosNH) / sinNH2;
94 
95  float D = expf(-cotangent2 * m_invsigma2) * m_invsigma2 * M_1_PI_F / sinNH4;
96  float G = min(1.0f, min(fac1, fac2)); // TODO: derive G from D analytically
97 
98  float out = 0.25f * (D * G) / cosNO;
99 
100  *pdf = 0.5f * M_1_PI_F;
101  return make_float3(out, out, out);
102  }
103 
104  return make_float3(0.0f, 0.0f, 0.0f);
105 }
106 
108  const float3 I,
109  const float3 omega_in,
110  float *pdf)
111 {
112  return make_float3(0.0f, 0.0f, 0.0f);
113 }
114 
116  float3 Ng,
117  float3 I,
118  float3 dIdx,
119  float3 dIdy,
120  float randu,
121  float randv,
122  float3 *eval,
123  float3 *omega_in,
124  float3 *domega_in_dx,
125  float3 *domega_in_dy,
126  float *pdf)
127 {
128  const VelvetBsdf *bsdf = (const VelvetBsdf *)sc;
129  float m_invsigma2 = bsdf->invsigma2;
130  float3 N = bsdf->N;
131 
132  // we are viewing the surface from above - send a ray out with uniform
133  // distribution over the hemisphere
134  sample_uniform_hemisphere(N, randu, randv, omega_in, pdf);
135 
136  if (dot(Ng, *omega_in) > 0) {
137  float3 H = normalize(*omega_in + I);
138 
139  float cosNI = dot(N, *omega_in);
140  float cosNO = dot(N, I);
141  float cosNH = dot(N, H);
142  float cosHO = fabsf(dot(I, H));
143 
144  if (fabsf(cosNO) > 1e-5f && fabsf(cosNH) < 1.0f - 1e-5f && cosHO > 1e-5f) {
145  float cosNHdivHO = cosNH / cosHO;
146  cosNHdivHO = fmaxf(cosNHdivHO, 1e-5f);
147 
148  float fac1 = 2 * fabsf(cosNHdivHO * cosNO);
149  float fac2 = 2 * fabsf(cosNHdivHO * cosNI);
150 
151  float sinNH2 = 1 - cosNH * cosNH;
152  float sinNH4 = sinNH2 * sinNH2;
153  float cotangent2 = (cosNH * cosNH) / sinNH2;
154 
155  float D = expf(-cotangent2 * m_invsigma2) * m_invsigma2 * M_1_PI_F / sinNH4;
156  float G = min(1.0f, min(fac1, fac2)); // TODO: derive G from D analytically
157 
158  float power = 0.25f * (D * G) / cosNO;
159 
160  *eval = make_float3(power, power, power);
161 
162 #ifdef __RAY_DIFFERENTIALS__
163  // TODO: find a better approximation for the retroreflective bounce
164  *domega_in_dx = (2 * dot(N, dIdx)) * N - dIdx;
165  *domega_in_dy = (2 * dot(N, dIdy)) * N - dIdy;
166 #endif
167  }
168  else
169  *pdf = 0.0f;
170  }
171  else
172  *pdf = 0.0f;
173 
174  return LABEL_REFLECT | LABEL_DIFFUSE;
175 }
176 
178 
179 #endif /* __BSDF_ASHIKHMIN_VELVET_H__ */
ccl_device float3 bsdf_ashikhmin_velvet_eval_reflect(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
ccl_device int bsdf_ashikhmin_velvet_setup(VelvetBsdf *bsdf)
CCL_NAMESPACE_BEGIN typedef ccl_addr_space struct VelvetBsdf VelvetBsdf
ccl_device bool bsdf_ashikhmin_velvet_merge(const ShaderClosure *a, const ShaderClosure *b)
ccl_device float3 bsdf_ashikhmin_velvet_eval_transmit(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
ccl_device int bsdf_ashikhmin_velvet_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)
#define ccl_addr_space
#define ccl_device
#define expf(x)
#define CCL_NAMESPACE_END
#define fmaxf(x, y)
#define fabsf(x)
#define make_float3(x, y, z)
ccl_device_inline void sample_uniform_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_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
#define min(a, b)
Definition: sort.c:51
@ CLOSURE_BSDF_ASHIKHMIN_VELVET_ID
Definition: svm_types.h:551
#define M_1_PI_F
Definition: util_math.h:52
ccl_device_inline float2 normalize(const float2 &a)
ccl_device_inline float dot(const float2 &a, const float2 &b)
ccl_device_inline bool isequal_float3(const float3 a, const float3 b)
#define G(x, y, z)
#define H(x, y, z)
BLI_INLINE float D(const float *data, const int res[3], int x, int y, int z)
Definition: voxel.c:29