2 * Adapted from Open Shading Language with this license:
4 * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
7 * Modifications Copyright 2011, Blender Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
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.
33 #ifndef __BSDF_ASHIKHMIN_VELVET_H__
34 #define __BSDF_ASHIKHMIN_VELVET_H__
38 typedef ccl_addr_space struct VelvetBsdf {
45 ccl_device int bsdf_ashikhmin_velvet_setup(VelvetBsdf *bsdf)
47 float sigma = fmaxf(bsdf->sigma, 0.01f);
48 bsdf->invsigma2 = 1.0f/(sigma * sigma);
50 bsdf->type = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
52 return SD_BSDF|SD_BSDF_HAS_EVAL;
55 ccl_device bool bsdf_ashikhmin_velvet_merge(const ShaderClosure *a, const ShaderClosure *b)
57 const VelvetBsdf *bsdf_a = (const VelvetBsdf*)a;
58 const VelvetBsdf *bsdf_b = (const VelvetBsdf*)b;
60 return (isequal_float3(bsdf_a->N, bsdf_b->N)) &&
61 (bsdf_a->sigma == bsdf_b->sigma);
64 ccl_device float3 bsdf_ashikhmin_velvet_eval_reflect(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
66 const VelvetBsdf *bsdf = (const VelvetBsdf*)sc;
67 float m_invsigma2 = bsdf->invsigma2;
70 float cosNO = dot(N, I);
71 float cosNI = dot(N, omega_in);
72 if(cosNO > 0 && cosNI > 0) {
73 float3 H = normalize(omega_in + I);
75 float cosNH = dot(N, H);
76 float cosHO = fabsf(dot(I, H));
78 if(!(fabsf(cosNH) < 1.0f-1e-5f && cosHO > 1e-5f))
79 return make_float3(0.0f, 0.0f, 0.0f);
81 float cosNHdivHO = cosNH / cosHO;
82 cosNHdivHO = fmaxf(cosNHdivHO, 1e-5f);
84 float fac1 = 2 * fabsf(cosNHdivHO * cosNO);
85 float fac2 = 2 * fabsf(cosNHdivHO * cosNI);
87 float sinNH2 = 1 - cosNH * cosNH;
88 float sinNH4 = sinNH2 * sinNH2;
89 float cotangent2 = (cosNH * cosNH) / sinNH2;
91 float D = expf(-cotangent2 * m_invsigma2) * m_invsigma2 * M_1_PI_F / sinNH4;
92 float G = min(1.0f, min(fac1, fac2)); // TODO: derive G from D analytically
94 float out = 0.25f * (D * G) / cosNO;
96 *pdf = 0.5f * M_1_PI_F;
97 return make_float3(out, out, out);
100 return make_float3(0.0f, 0.0f, 0.0f);
103 ccl_device float3 bsdf_ashikhmin_velvet_eval_transmit(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
105 return make_float3(0.0f, 0.0f, 0.0f);
108 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)
110 const VelvetBsdf *bsdf = (const VelvetBsdf*)sc;
111 float m_invsigma2 = bsdf->invsigma2;
114 // we are viewing the surface from above - send a ray out with uniform
115 // distribution over the hemisphere
116 sample_uniform_hemisphere(N, randu, randv, omega_in, pdf);
118 if(dot(Ng, *omega_in) > 0) {
119 float3 H = normalize(*omega_in + I);
121 float cosNI = dot(N, *omega_in);
122 float cosNO = dot(N, I);
123 float cosNH = dot(N, H);
124 float cosHO = fabsf(dot(I, H));
126 if(fabsf(cosNO) > 1e-5f && fabsf(cosNH) < 1.0f-1e-5f && cosHO > 1e-5f) {
127 float cosNHdivHO = cosNH / cosHO;
128 cosNHdivHO = fmaxf(cosNHdivHO, 1e-5f);
130 float fac1 = 2 * fabsf(cosNHdivHO * cosNO);
131 float fac2 = 2 * fabsf(cosNHdivHO * cosNI);
133 float sinNH2 = 1 - cosNH * cosNH;
134 float sinNH4 = sinNH2 * sinNH2;
135 float cotangent2 = (cosNH * cosNH) / sinNH2;
137 float D = expf(-cotangent2 * m_invsigma2) * m_invsigma2 * M_1_PI_F / sinNH4;
138 float G = min(1.0f, min(fac1, fac2)); // TODO: derive G from D analytically
140 float power = 0.25f * (D * G) / cosNO;
142 *eval = make_float3(power, power, power);
144 #ifdef __RAY_DIFFERENTIALS__
145 // TODO: find a better approximation for the retroreflective bounce
146 *domega_in_dx = (2 * dot(N, dIdx)) * N - dIdx;
147 *domega_in_dy = (2 * dot(N, dIdy)) * N - dIdy;
156 return LABEL_REFLECT|LABEL_DIFFUSE;
161 #endif /* __BSDF_ASHIKHMIN_VELVET_H__ */