Blender  V2.93
bsdf_util.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_UTIL_H__
34 #define __BSDF_UTIL_H__
35 
37 
39  const float3 N,
40  const float3 I,
41  float3 *R,
42  float3 *T,
44  const float3 dIdx,
45  const float3 dIdy,
46  float3 *dRdx,
47  float3 *dRdy,
48  float3 *dTdx,
49  float3 *dTdy,
50 #endif
51  bool *is_inside)
52 {
53  float cos = dot(N, I), neta;
54  float3 Nn;
55 
56  // check which side of the surface we are on
57  if (cos > 0) {
58  // we are on the outside of the surface, going in
59  neta = 1 / eta;
60  Nn = N;
61  *is_inside = false;
62  }
63  else {
64  // we are inside the surface
65  cos = -cos;
66  neta = eta;
67  Nn = -N;
68  *is_inside = true;
69  }
70 
71  // compute reflection
72  *R = (2 * cos) * Nn - I;
73 #ifdef __RAY_DIFFERENTIALS__
74  *dRdx = (2 * dot(Nn, dIdx)) * Nn - dIdx;
75  *dRdy = (2 * dot(Nn, dIdy)) * Nn - dIdy;
76 #endif
77 
78  float arg = 1 - (neta * neta * (1 - (cos * cos)));
79  if (arg < 0) {
80  *T = make_float3(0.0f, 0.0f, 0.0f);
81 #ifdef __RAY_DIFFERENTIALS__
82  *dTdx = make_float3(0.0f, 0.0f, 0.0f);
83  *dTdy = make_float3(0.0f, 0.0f, 0.0f);
84 #endif
85  return 1; // total internal reflection
86  }
87  else {
88  float dnp = max(sqrtf(arg), 1e-7f);
89  float nK = (neta * cos) - dnp;
90  *T = -(neta * I) + (nK * Nn);
91 #ifdef __RAY_DIFFERENTIALS__
92  *dTdx = -(neta * dIdx) + ((neta - neta * neta * cos / dnp) * dot(dIdx, Nn)) * Nn;
93  *dTdy = -(neta * dIdy) + ((neta - neta * neta * cos / dnp) * dot(dIdy, Nn)) * Nn;
94 #endif
95  // compute Fresnel terms
96  float cosTheta1 = cos; // N.R
97  float cosTheta2 = -dot(Nn, *T);
98  float pPara = (cosTheta1 - eta * cosTheta2) / (cosTheta1 + eta * cosTheta2);
99  float pPerp = (eta * cosTheta1 - cosTheta2) / (eta * cosTheta1 + cosTheta2);
100  return 0.5f * (pPara * pPara + pPerp * pPerp);
101  }
102 }
103 
104 ccl_device float fresnel_dielectric_cos(float cosi, float eta)
105 {
106  // compute fresnel reflectance without explicitly computing
107  // the refracted direction
108  float c = fabsf(cosi);
109  float g = eta * eta - 1 + c * c;
110  if (g > 0) {
111  g = sqrtf(g);
112  float A = (g - c) / (g + c);
113  float B = (c * (g + c) - 1) / (c * (g - c) + 1);
114  return 0.5f * A * A * (1 + B * B);
115  }
116  return 1.0f; // TIR(no refracted component)
117 }
118 
119 ccl_device float3 fresnel_conductor(float cosi, const float3 eta, const float3 k)
120 {
121  float3 cosi2 = make_float3(cosi * cosi, cosi * cosi, cosi * cosi);
122  float3 one = make_float3(1.0f, 1.0f, 1.0f);
123  float3 tmp_f = eta * eta + k * k;
124  float3 tmp = tmp_f * cosi2;
125  float3 Rparl2 = (tmp - (2.0f * eta * cosi) + one) / (tmp + (2.0f * eta * cosi) + one);
126  float3 Rperp2 = (tmp_f - (2.0f * eta * cosi) + cosi2) / (tmp_f + (2.0f * eta * cosi) + cosi2);
127  return (Rparl2 + Rperp2) * 0.5f;
128 }
129 
131 {
132  float m = clamp(1.0f - u, 0.0f, 1.0f);
133  float m2 = m * m;
134  return m2 * m2 * m; // pow(m, 5)
135 }
136 
137 /* Calculate the fresnel color which is a blend between white and the F0 color (cspec0) */
139 interpolate_fresnel_color(float3 L, float3 H, float ior, float F0, float3 cspec0)
140 {
141  /* Calculate the fresnel interpolation factor
142  * The value from fresnel_dielectric_cos(...) has to be normalized because
143  * the cspec0 keeps the F0 color
144  */
145  float F0_norm = 1.0f / (1.0f - F0);
146  float FH = (fresnel_dielectric_cos(dot(L, H), ior) - F0) * F0_norm;
147 
148  /* Blend between white and a specular color with respect to the fresnel */
149  return cspec0 * (1.0f - FH) + make_float3(1.0f, 1.0f, 1.0f) * FH;
150 }
151 
153 
154 #endif /* __BSDF_UTIL_H__ */
#define A
ccl_device float fresnel_dielectric_cos(float cosi, float eta)
Definition: bsdf_util.h:104
CCL_NAMESPACE_BEGIN ccl_device float fresnel_dielectric(float eta, const float3 N, const float3 I, float3 *R, float3 *T, bool *is_inside)
Definition: bsdf_util.h:38
ccl_device_forceinline float3 interpolate_fresnel_color(float3 L, float3 H, float ior, float F0, float3 cspec0)
Definition: bsdf_util.h:139
ccl_device float schlick_fresnel(float u)
Definition: bsdf_util.h:130
ccl_device float3 fresnel_conductor(float cosi, const float3 eta, const float3 k)
Definition: bsdf_util.h:119
static bool is_inside(int x, int y, int cols, int rows)
Definition: filesel.c:663
#define FH(b, c, d)
#define ccl_device_forceinline
#define ccl_device
#define CCL_NAMESPACE_END
#define fabsf(x)
#define sqrtf(x)
#define make_float3(x, y, z)
#define __RAY_DIFFERENTIALS__
Definition: kernel_types.h:91
#define T
#define B
#define R
#define L
static unsigned c
Definition: RandGen.cpp:97
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:319
#define I
params N
float max
ccl_device_inline int clamp(int a, int mn, int mx)
Definition: util_math.h:283
ccl_device_inline float dot(const float2 &a, const float2 &b)
#define H(x, y, z)