Blender  V2.93
stdcycles.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al. All Rights Reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of Sony Pictures Imageworks nor the names of its
13 // contributors may be used to endorse or promote products derived from
14 // this software without specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #ifndef CCL_STDCYCLESOSL_H
29 #define CCL_STDCYCLESOSL_H
30 
31 #include "stdosl.h"
32 
33 // Declaration of built-in functions and closures, stdosl.h does not make
34 // these available so we have to redefine them.
35 #define BUILTIN [[int builtin = 1]]
36 #define BUILTIN_DERIV [[ int builtin = 1, int deriv = 1 ]]
37 
38 closure color diffuse_ramp(normal N, color colors[8]) BUILTIN;
39 closure color phong_ramp(normal N, float exponent, color colors[8]) BUILTIN;
40 closure color diffuse_toon(normal N, float size, float smooth) BUILTIN;
41 closure color glossy_toon(normal N, float size, float smooth) BUILTIN;
42 closure color microfacet_ggx(normal N, float ag) BUILTIN;
43 closure color microfacet_ggx_aniso(normal N, vector T, float ax, float ay) BUILTIN;
44 closure color microfacet_ggx_refraction(normal N, float ag, float eta) BUILTIN;
45 closure color microfacet_multi_ggx(normal N, float ag, color C) BUILTIN;
46 closure color microfacet_multi_ggx_aniso(normal N, vector T, float ax, float ay, color C) BUILTIN;
47 closure color microfacet_multi_ggx_glass(normal N, float ag, float eta, color C) BUILTIN;
48 closure color microfacet_ggx_fresnel(normal N, float ag, float eta, color C, color Cspec0) BUILTIN;
50  normal N, vector T, float ax, float ay, float eta, color C, color Cspec0) BUILTIN;
51 closure color
52 microfacet_multi_ggx_fresnel(normal N, float ag, float eta, color C, color Cspec0) BUILTIN;
54  normal N, vector T, float ax, float ay, float eta, color C, color Cspec0) BUILTIN;
55 closure color
56 microfacet_multi_ggx_glass_fresnel(normal N, float ag, float eta, color C, color Cspec0) BUILTIN;
57 closure color microfacet_beckmann(normal N, float ab) BUILTIN;
58 closure color microfacet_beckmann_aniso(normal N, vector T, float ax, float ay) BUILTIN;
59 closure color microfacet_beckmann_refraction(normal N, float ab, float eta) BUILTIN;
60 closure color ashikhmin_shirley(normal N, vector T, float ax, float ay) BUILTIN;
61 closure color ashikhmin_velvet(normal N, float sigma) BUILTIN;
62 closure color ambient_occlusion() BUILTIN;
65 closure color principled_clearcoat(normal N, float clearcoat, float clearcoat_roughness) BUILTIN;
66 
67 // BSSRDF
68 closure color bssrdf(string method, normal N, vector radius, color albedo) BUILTIN;
69 
70 // Hair
71 closure color
72 hair_reflection(normal N, float roughnessu, float roughnessv, vector T, float offset) BUILTIN;
73 closure color
74 hair_transmission(normal N, float roughnessu, float roughnessv, vector T, float offset) BUILTIN;
75 closure color principled_hair(normal N,
76  color sigma,
77  float roughnessu,
78  float roughnessv,
79  float coat,
80  float alpha,
81  float eta) BUILTIN;
82 
83 // Volume
84 closure color henyey_greenstein(float g) BUILTIN;
85 closure color absorption() BUILTIN;
86 
88 {
89  /* The implementation here mirrors the one in kernel_montecarlo.h,
90  * check there for an explanation of the algorithm. */
91 
92  float sqr(float x)
93  {
94  return x * x;
95  }
96 
97  vector R = 2 * dot(N, I) * N - I;
98 
99  float threshold = min(0.9 * dot(Ng, I), 0.01);
100  if (dot(Ng, R) >= threshold) {
101  return N;
102  }
103 
104  float NdotNg = dot(N, Ng);
105  vector X = normalize(N - NdotNg * Ng);
106 
107  float Ix = dot(I, X), Iz = dot(I, Ng);
108  float Ix2 = sqr(Ix), Iz2 = sqr(Iz);
109  float a = Ix2 + Iz2;
110 
111  float b = sqrt(Ix2 * (a - sqr(threshold)));
112  float c = Iz * threshold + a;
113 
114  float fac = 0.5 / a;
115  float N1_z2 = fac * (b + c), N2_z2 = fac * (-b + c);
116  int valid1 = (N1_z2 > 1e-5) && (N1_z2 <= (1.0 + 1e-5));
117  int valid2 = (N2_z2 > 1e-5) && (N2_z2 <= (1.0 + 1e-5));
118 
119  float N_new_x, N_new_z;
120  if (valid1 && valid2) {
121  float N1_x = sqrt(1.0 - N1_z2), N1_z = sqrt(N1_z2);
122  float N2_x = sqrt(1.0 - N2_z2), N2_z = sqrt(N2_z2);
123 
124  float R1 = 2 * (N1_x * Ix + N1_z * Iz) * N1_z - Iz;
125  float R2 = 2 * (N2_x * Ix + N2_z * Iz) * N2_z - Iz;
126 
127  valid1 = (R1 >= 1e-5);
128  valid2 = (R2 >= 1e-5);
129  if (valid1 && valid2) {
130  N_new_x = (R1 < R2) ? N1_x : N2_x;
131  N_new_z = (R1 < R2) ? N1_z : N2_z;
132  }
133  else {
134  N_new_x = (R1 > R2) ? N1_x : N2_x;
135  N_new_z = (R1 > R2) ? N1_z : N2_z;
136  }
137  }
138  else if (valid1 || valid2) {
139  float Nz2 = valid1 ? N1_z2 : N2_z2;
140  N_new_x = sqrt(1.0 - Nz2);
141  N_new_z = sqrt(Nz2);
142  }
143  else {
144  return Ng;
145  }
146 
147  return N_new_x * X + N_new_z * Ng;
148 }
149 
150 #endif /* CCL_STDOSL_H */
sqrt(x)+1/max(0
#define X
Definition: GeomUtils.cpp:213
#define C
Definition: RandGen.cpp:39
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
static CCL_NAMESPACE_BEGIN const double alpha
IconTextureDrawCall normal
#define T
#define R
static unsigned c
Definition: RandGen.cpp:97
static unsigned a[3]
Definition: RandGen.cpp:92
static const pxr::TfToken roughness("roughness", pxr::TfToken::Immortal)
#define I
params N
#define min(a, b)
Definition: sort.c:51
closure color microfacet_ggx(normal N, float ag) BUILTIN
closure color principled_diffuse(normal N, float roughness) BUILTIN
closure color microfacet_multi_ggx_glass_fresnel(normal N, float ag, float eta, color C, color Cspec0) BUILTIN
closure color absorption() BUILTIN
closure color microfacet_ggx_aniso_fresnel(normal N, vector T, float ax, float ay, float eta, color C, color Cspec0) BUILTIN
closure color phong_ramp(normal N, float exponent, color colors[8]) BUILTIN
closure color microfacet_multi_ggx_glass(normal N, float ag, float eta, color C) BUILTIN
closure color microfacet_multi_ggx_aniso_fresnel(normal N, vector T, float ax, float ay, float eta, color C, color Cspec0) BUILTIN
closure color microfacet_multi_ggx_fresnel(normal N, float ag, float eta, color C, color Cspec0) BUILTIN
closure color diffuse_toon(normal N, float size, float smooth) BUILTIN
closure color henyey_greenstein(float g) BUILTIN
#define BUILTIN
Definition: stdcycles.h:35
closure color ambient_occlusion() BUILTIN
closure color microfacet_multi_ggx(normal N, float ag, color C) BUILTIN
closure color microfacet_multi_ggx_aniso(normal N, vector T, float ax, float ay, color C) BUILTIN
closure color hair_transmission(normal N, float roughnessu, float roughnessv, vector T, float offset) BUILTIN
closure color ashikhmin_velvet(normal N, float sigma) BUILTIN
closure color diffuse_ramp(normal N, color colors[8]) BUILTIN
closure color bssrdf(string method, normal N, vector radius, color albedo) BUILTIN
closure color microfacet_ggx_refraction(normal N, float ag, float eta) BUILTIN
closure color principled_clearcoat(normal N, float clearcoat, float clearcoat_roughness) BUILTIN
closure color hair_reflection(normal N, float roughnessu, float roughnessv, vector T, float offset) BUILTIN
normal ensure_valid_reflection(normal Ng, vector I, normal N)
Definition: stdcycles.h:87
closure color microfacet_beckmann(normal N, float ab) BUILTIN
closure color microfacet_ggx_aniso(normal N, vector T, float ax, float ay) BUILTIN
closure color principled_hair(normal N, color sigma, float roughnessu, float roughnessv, float coat, float alpha, float eta) BUILTIN
closure color microfacet_ggx_fresnel(normal N, float ag, float eta, color C, color Cspec0) BUILTIN
closure color ashikhmin_shirley(normal N, vector T, float ax, float ay) BUILTIN
closure color microfacet_beckmann_aniso(normal N, vector T, float ax, float ay) BUILTIN
closure color glossy_toon(normal N, float size, float smooth) BUILTIN
closure color microfacet_beckmann_refraction(normal N, float ab, float eta) BUILTIN
closure color principled_sheen(normal N) BUILTIN
ccl_device_inline float sqr(float a)
Definition: util_math.h:651
ccl_device_inline float2 normalize(const float2 &a)
ccl_device_inline float dot(const float2 &a, const float2 &b)