ClangFormat: apply to source, most of intern
[blender.git] / intern / cycles / kernel / closure / bsdf_diffuse.h
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_DIFFUSE_H__
34 #define __BSDF_DIFFUSE_H__
35
36 CCL_NAMESPACE_BEGIN
37
38 typedef ccl_addr_space struct DiffuseBsdf {
39   SHADER_CLOSURE_BASE;
40 } DiffuseBsdf;
41
42 /* DIFFUSE */
43
44 ccl_device int bsdf_diffuse_setup(DiffuseBsdf *bsdf)
45 {
46   bsdf->type = CLOSURE_BSDF_DIFFUSE_ID;
47   return SD_BSDF | SD_BSDF_HAS_EVAL;
48 }
49
50 ccl_device bool bsdf_diffuse_merge(const ShaderClosure *a, const ShaderClosure *b)
51 {
52   const DiffuseBsdf *bsdf_a = (const DiffuseBsdf *)a;
53   const DiffuseBsdf *bsdf_b = (const DiffuseBsdf *)b;
54
55   return (isequal_float3(bsdf_a->N, bsdf_b->N));
56 }
57
58 ccl_device float3 bsdf_diffuse_eval_reflect(const ShaderClosure *sc,
59                                             const float3 I,
60                                             const float3 omega_in,
61                                             float *pdf)
62 {
63   const DiffuseBsdf *bsdf = (const DiffuseBsdf *)sc;
64   float3 N = bsdf->N;
65
66   float cos_pi = fmaxf(dot(N, omega_in), 0.0f) * M_1_PI_F;
67   *pdf = cos_pi;
68   return make_float3(cos_pi, cos_pi, cos_pi);
69 }
70
71 ccl_device float3 bsdf_diffuse_eval_transmit(const ShaderClosure *sc,
72                                              const float3 I,
73                                              const float3 omega_in,
74                                              float *pdf)
75 {
76   return make_float3(0.0f, 0.0f, 0.0f);
77 }
78
79 ccl_device int bsdf_diffuse_sample(const ShaderClosure *sc,
80                                    float3 Ng,
81                                    float3 I,
82                                    float3 dIdx,
83                                    float3 dIdy,
84                                    float randu,
85                                    float randv,
86                                    float3 *eval,
87                                    float3 *omega_in,
88                                    float3 *domega_in_dx,
89                                    float3 *domega_in_dy,
90                                    float *pdf)
91 {
92   const DiffuseBsdf *bsdf = (const DiffuseBsdf *)sc;
93   float3 N = bsdf->N;
94
95   // distribution over the hemisphere
96   sample_cos_hemisphere(N, randu, randv, omega_in, pdf);
97
98   if (dot(Ng, *omega_in) > 0.0f) {
99     *eval = make_float3(*pdf, *pdf, *pdf);
100 #ifdef __RAY_DIFFERENTIALS__
101     // TODO: find a better approximation for the diffuse bounce
102     *domega_in_dx = (2 * dot(N, dIdx)) * N - dIdx;
103     *domega_in_dy = (2 * dot(N, dIdy)) * N - dIdy;
104 #endif
105   }
106   else
107     *pdf = 0.0f;
108
109   return LABEL_REFLECT | LABEL_DIFFUSE;
110 }
111
112 /* TRANSLUCENT */
113
114 ccl_device int bsdf_translucent_setup(DiffuseBsdf *bsdf)
115 {
116   bsdf->type = CLOSURE_BSDF_TRANSLUCENT_ID;
117   return SD_BSDF | SD_BSDF_HAS_EVAL;
118 }
119
120 ccl_device float3 bsdf_translucent_eval_reflect(const ShaderClosure *sc,
121                                                 const float3 I,
122                                                 const float3 omega_in,
123                                                 float *pdf)
124 {
125   return make_float3(0.0f, 0.0f, 0.0f);
126 }
127
128 ccl_device float3 bsdf_translucent_eval_transmit(const ShaderClosure *sc,
129                                                  const float3 I,
130                                                  const float3 omega_in,
131                                                  float *pdf)
132 {
133   const DiffuseBsdf *bsdf = (const DiffuseBsdf *)sc;
134   float3 N = bsdf->N;
135
136   float cos_pi = fmaxf(-dot(N, omega_in), 0.0f) * M_1_PI_F;
137   *pdf = cos_pi;
138   return make_float3(cos_pi, cos_pi, cos_pi);
139 }
140
141 ccl_device int bsdf_translucent_sample(const ShaderClosure *sc,
142                                        float3 Ng,
143                                        float3 I,
144                                        float3 dIdx,
145                                        float3 dIdy,
146                                        float randu,
147                                        float randv,
148                                        float3 *eval,
149                                        float3 *omega_in,
150                                        float3 *domega_in_dx,
151                                        float3 *domega_in_dy,
152                                        float *pdf)
153 {
154   const DiffuseBsdf *bsdf = (const DiffuseBsdf *)sc;
155   float3 N = bsdf->N;
156
157   // we are viewing the surface from the right side - send a ray out with cosine
158   // distribution over the hemisphere
159   sample_cos_hemisphere(-N, randu, randv, omega_in, pdf);
160   if (dot(Ng, *omega_in) < 0) {
161     *eval = make_float3(*pdf, *pdf, *pdf);
162 #ifdef __RAY_DIFFERENTIALS__
163     // TODO: find a better approximation for the diffuse 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;
170   }
171   return LABEL_TRANSMIT | LABEL_DIFFUSE;
172 }
173
174 CCL_NAMESPACE_END
175
176 #endif /* __BSDF_DIFFUSE_H__ */