Cycles: Remove few function arguments needed only for the split kernel
[blender.git] / intern / cycles / kernel / kernel_differential.h
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 CCL_NAMESPACE_BEGIN
18
19 /* See "Tracing Ray Differentials", Homan Igehy, 1999. */
20
21 ccl_device void differential_transfer(ccl_addr_space differential3 *dP_, const differential3 dP, float3 D, const differential3 dD, float3 Ng, float t)
22 {
23         /* ray differential transfer through homogeneous medium, to
24          * compute dPdx/dy at a shading point from the incoming ray */
25
26         float3 tmp = D/dot(D, Ng);
27         float3 tmpx = dP.dx + t*dD.dx;
28         float3 tmpy = dP.dy + t*dD.dy;
29
30         dP_->dx = tmpx - dot(tmpx, Ng)*tmp;
31         dP_->dy = tmpy - dot(tmpy, Ng)*tmp;
32 }
33
34 ccl_device void differential_incoming(ccl_addr_space differential3 *dI, const differential3 dD)
35 {
36         /* compute dIdx/dy at a shading point, we just need to negate the
37          * differential of the ray direction */
38
39         dI->dx = -dD.dx;
40         dI->dy = -dD.dy;
41 }
42
43 ccl_device void differential_dudv(ccl_addr_space differential *du, ccl_addr_space differential *dv, float3 dPdu, float3 dPdv, differential3 dP, float3 Ng)
44 {
45         /* now we have dPdx/dy from the ray differential transfer, and dPdu/dv
46          * from the primitive, we can compute dudx/dy and dvdx/dy. these are
47          * mainly used for differentials of arbitrary mesh attributes. */
48
49         /* find most stable axis to project to 2D */
50         float xn = fabsf(Ng.x);
51         float yn = fabsf(Ng.y);
52         float zn = fabsf(Ng.z);
53
54         if(zn < xn || zn < yn) {
55                 if(yn < xn || yn < zn) {
56                         dPdu.x = dPdu.y;
57                         dPdv.x = dPdv.y;
58                         dP.dx.x = dP.dx.y;
59                         dP.dy.x = dP.dy.y;
60                 }
61
62                 dPdu.y = dPdu.z;
63                 dPdv.y = dPdv.z;
64                 dP.dx.y = dP.dx.z;
65                 dP.dy.y = dP.dy.z;
66         }
67
68         /* using Cramer's rule, we solve for dudx and dvdx in a 2x2 linear system,
69          * and the same for dudy and dvdy. the denominator is the same for both
70          * solutions, so we compute it only once.
71          *
72          * dP.dx = dPdu * dudx + dPdv * dvdx;
73          * dP.dy = dPdu * dudy + dPdv * dvdy; */
74
75         float det = (dPdu.x*dPdv.y - dPdv.x*dPdu.y);
76
77         if(det != 0.0f)
78                 det = 1.0f/det;
79
80         du->dx = (dP.dx.x*dPdv.y - dP.dx.y*dPdv.x)*det;
81         dv->dx = (dP.dx.y*dPdu.x - dP.dx.x*dPdu.y)*det;
82
83         du->dy = (dP.dy.x*dPdv.y - dP.dy.y*dPdv.x)*det;
84         dv->dy = (dP.dy.y*dPdu.x - dP.dy.x*dPdu.y)*det;
85 }
86
87 ccl_device differential differential_zero()
88 {
89         differential d;
90         d.dx = 0.0f;
91         d.dy = 0.0f;
92
93         return d;
94 }
95
96 ccl_device differential3 differential3_zero()
97 {
98         differential3 d;
99         d.dx = make_float3(0.0f, 0.0f, 0.0f);
100         d.dy = make_float3(0.0f, 0.0f, 0.0f);
101
102         return d;
103 }
104
105 CCL_NAMESPACE_END
106