blender/intern/cycles/kernel/kernel_differential.h
Brecht Van Lommel 5873301257 Sample as Lamp option for world shaders, to enable multiple importance sampling.
By default lighting from the world is computed solely with indirect light
sampling. However for more complex environment maps this can be too noisy, as
sampling the BSDF may not easily find the highlights in the environment map
image. By enabling this option, the world background will be sampled as a lamp,
with lighter parts automatically given more samples.

Map Resolution specifies the size of the importance map (res x res). Before
rendering starts, an importance map is generated by "baking" a grayscale image
from the world shader. This will then be used to determine which parts of the
background are light and so should receive more samples than darker parts.
Higher resolutions will result in more accurate sampling but take more setup
time and memory.

Patch by Mike Farnsworth, thanks!
2012-01-20 17:49:17 +00:00

91 lines
2.7 KiB
C

/*
* Copyright 2011, Blender Foundation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
CCL_NAMESPACE_BEGIN
/* See "Tracing Ray Differentials", Homan Igehy, 1999. */
__device void differential_transfer(differential3 *dP_, const differential3 dP, float3 D, const differential3 dD, float3 Ng, float t)
{
/* ray differential transfer through homogenous medium, to
* compute dPdx/dy at a shading point from the incoming ray */
float3 tmp = D/dot(D, Ng);
float3 tmpx = dP.dx + t*dD.dx;
float3 tmpy = dP.dy + t*dD.dy;
dP_->dx = tmpx - dot(tmpx, Ng)*tmp;
dP_->dy = tmpy - dot(tmpy, Ng)*tmp;
}
__device void differential_incoming(differential3 *dI, const differential3 dD)
{
/* compute dIdx/dy at a shading point, we just need to negate the
* differential of the ray direction */
dI->dx = -dD.dx;
dI->dy = -dD.dy;
}
__device void differential_dudv(differential *du, differential *dv, float3 dPdu, float3 dPdv, differential3 dP, float3 Ng)
{
/* now we have dPdx/dy from the ray differential transfer, and dPdu/dv
* from the primitive, we can compute dudx/dy and dvdx/dy. these are
* mainly used for differentials of arbitrary mesh attributes. */
/* find most stable axis to project to 2D */
float xn= fabsf(Ng.x);
float yn= fabsf(Ng.y);
float zn= fabsf(Ng.z);
if(zn < xn || zn < yn) {
if(yn < xn || yn < zn) {
dPdu.x = dPdu.y;
dPdv.x = dPdv.y;
dP.dx.x = dP.dx.y;
dP.dy.x = dP.dy.y;
}
dPdu.y = dPdu.z;
dPdv.y = dPdv.z;
dP.dx.y = dP.dx.z;
dP.dy.y = dP.dy.z;
}
/* using Cramer's rule, we solve for dudx and dvdx in a 2x2 linear system,
* and the same for dudy and dvdy. the denominator is the same for both
* solutions, so we compute it only once.
*
* dP.dx = dPdu * dudx + dPdv * dvdx;
* dP.dy = dPdu * dudy + dPdv * dvdy; */
float det = (dPdu.x*dPdv.y - dPdv.x*dPdu.y);
if(det != 0.0f)
det = 1.0f/det;
du->dx = (dP.dx.x*dPdv.y - dP.dx.y*dPdv.x)*det;
dv->dx = (dP.dx.y*dPdu.x - dP.dx.x*dPdu.y)*det;
du->dy = (dP.dy.x*dPdv.y - dP.dy.y*dPdv.x)*det;
dv->dy = (dP.dy.y*dPdu.x - dP.dy.x*dPdu.y)*det;
}
CCL_NAMESPACE_END