Blender V2.61 - r43446

kernel_differential.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2011, Blender Foundation.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 CCL_NAMESPACE_BEGIN
00020 
00021 /* See "Tracing Ray Differentials", Homan Igehy, 1999. */
00022 
00023 __device void differential_transfer(differential3 *dP_, const differential3 dP, float3 D, const differential3 dD, float3 Ng, float t)
00024 {
00025     /* ray differential transfer through homogenous medium, to
00026      * compute dPdx/dy at a shading point from the incoming ray */
00027 
00028     float3 tmp = D/dot(D, Ng);
00029     float3 tmpx = dP.dx + t*dD.dx;
00030     float3 tmpy = dP.dy + t*dD.dy;
00031 
00032     dP_->dx = tmpx - dot(tmpx, Ng)*tmp;
00033     dP_->dy = tmpy - dot(tmpy, Ng)*tmp;
00034 }
00035 
00036 __device void differential_incoming(differential3 *dI, const differential3 dD)
00037 {
00038     /* compute dIdx/dy at a shading point, we just need to negate the
00039      * differential of the ray direction */
00040 
00041     dI->dx = -dD.dx;
00042     dI->dy = -dD.dy;
00043 }
00044 
00045 __device void differential_dudv(differential *du, differential *dv, float3 dPdu, float3 dPdv, differential3 dP, float3 Ng)
00046 {
00047     /* now we have dPdx/dy from the ray differential transfer, and dPdu/dv
00048      * from the primitive, we can compute dudx/dy and dvdx/dy. these are
00049      * mainly used for differentials of arbitrary mesh attributes. */
00050 
00051     /* find most stable axis to project to 2D */
00052     float xn= fabsf(Ng.x);
00053     float yn= fabsf(Ng.y);
00054     float zn= fabsf(Ng.z);
00055 
00056     if(zn < xn || zn < yn) {
00057         if(yn < xn || yn < zn) {
00058             dPdu.x = dPdu.y;
00059             dPdv.x = dPdv.y;
00060             dP.dx.x = dP.dx.y;
00061             dP.dy.x = dP.dy.y;
00062         }
00063 
00064         dPdu.y = dPdu.z;
00065         dPdv.y = dPdv.z;
00066         dP.dx.y = dP.dx.z;
00067         dP.dy.y = dP.dy.z;
00068     }
00069 
00070     /* using Cramer's rule, we solve for dudx and dvdx in a 2x2 linear system,
00071      * and the same for dudy and dvdy. the denominator is the same for both
00072      * solutions, so we compute it only once.
00073      *
00074      * dP.dx = dPdu * dudx + dPdv * dvdx;
00075      * dP.dy = dPdu * dudy + dPdv * dvdy; */
00076 
00077     float det = (dPdu.x*dPdv.y - dPdv.x*dPdu.y);
00078 
00079     if(det != 0.0f)
00080         det = 1.0f/det;
00081 
00082     du->dx = (dP.dx.x*dPdv.y - dP.dx.y*dPdv.x)*det;
00083     dv->dx = (dP.dx.y*dPdu.x - dP.dx.x*dPdu.y)*det;
00084 
00085     du->dy = (dP.dy.x*dPdv.y - dP.dy.y*dPdv.x)*det;
00086     dv->dy = (dP.dy.y*dPdu.x - dP.dy.x*dPdu.y)*det;
00087 }
00088 
00089 CCL_NAMESPACE_END
00090