2011-04-27 11:58:34 +00:00
|
|
|
/*
|
|
|
|
* Parts adapted from Open Shading Language with this license:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Modifications Copyright 2011, Blender Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Sony Pictures Imageworks nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2012-06-09 17:22:52 +00:00
|
|
|
*/
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
#ifndef __KERNEL_MONTECARLO_CL__
|
|
|
|
#define __KERNEL_MONTECARLO_CL__
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2013-08-18 14:16:15 +00:00
|
|
|
/* distribute uniform xy on [0,1] over unit disk [-1,1] */
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device void to_unit_disk(float *x, float *y)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2013-06-14 14:29:00 +00:00
|
|
|
float phi = M_2PI_F * (*x);
|
2013-06-07 16:06:22 +00:00
|
|
|
float r = sqrtf(*y);
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
*x = r * cosf(phi);
|
|
|
|
*y = r * sinf(phi);
|
|
|
|
}
|
|
|
|
|
2013-08-18 14:16:15 +00:00
|
|
|
/* return an orthogonal tangent and bitangent given a normal and tangent that
|
|
|
|
* may not be exactly orthogonal */
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device void make_orthonormals_tangent(const float3 N, const float3 T, float3 *a, float3 *b)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-10-10 13:02:20 +00:00
|
|
|
*b = normalize(cross(N, T));
|
2011-04-27 11:58:34 +00:00
|
|
|
*a = cross(*b, N);
|
|
|
|
}
|
|
|
|
|
2013-08-18 14:16:15 +00:00
|
|
|
/* sample direction with cosine weighted distributed in hemisphere */
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline void sample_cos_hemisphere(const float3 N,
|
2011-04-27 11:58:34 +00:00
|
|
|
float randu, float randv, float3 *omega_in, float *pdf)
|
|
|
|
{
|
|
|
|
to_unit_disk(&randu, &randv);
|
|
|
|
float costheta = sqrtf(max(1.0f - randu * randu - randv * randv, 0.0f));
|
|
|
|
float3 T, B;
|
|
|
|
make_orthonormals(N, &T, &B);
|
|
|
|
*omega_in = randu * T + randv * B + costheta * N;
|
|
|
|
*pdf = costheta *M_1_PI_F;
|
|
|
|
}
|
|
|
|
|
2013-08-18 14:16:15 +00:00
|
|
|
/* sample direction uniformly distributed in hemisphere */
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline void sample_uniform_hemisphere(const float3 N,
|
2012-07-31 23:06:12 +00:00
|
|
|
float randu, float randv,
|
|
|
|
float3 *omega_in, float *pdf)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
float z = randu;
|
2012-05-05 19:44:35 +00:00
|
|
|
float r = sqrtf(max(0.0f, 1.0f - z*z));
|
2013-05-12 14:13:29 +00:00
|
|
|
float phi = M_2PI_F * randv;
|
2011-04-27 11:58:34 +00:00
|
|
|
float x = r * cosf(phi);
|
|
|
|
float y = r * sinf(phi);
|
|
|
|
|
|
|
|
float3 T, B;
|
|
|
|
make_orthonormals (N, &T, &B);
|
|
|
|
*omega_in = x * T + y * B + z * N;
|
|
|
|
*pdf = 0.5f * M_1_PI_F;
|
|
|
|
}
|
|
|
|
|
2013-08-18 14:16:15 +00:00
|
|
|
/* sample direction uniformly distributed in cone */
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline void sample_uniform_cone(const float3 N, float angle,
|
2012-12-19 21:17:16 +00:00
|
|
|
float randu, float randv,
|
|
|
|
float3 *omega_in, float *pdf)
|
|
|
|
{
|
|
|
|
float z = cosf(angle*randu);
|
|
|
|
float r = sqrtf(max(0.0f, 1.0f - z*z));
|
2013-05-12 14:13:29 +00:00
|
|
|
float phi = M_2PI_F * randv;
|
2012-12-19 21:17:16 +00:00
|
|
|
float x = r * cosf(phi);
|
|
|
|
float y = r * sinf(phi);
|
|
|
|
|
|
|
|
float3 T, B;
|
|
|
|
make_orthonormals (N, &T, &B);
|
|
|
|
*omega_in = x * T + y * B + z * N;
|
|
|
|
*pdf = 0.5f * M_1_PI_F / (1.0f - cosf(angle));
|
|
|
|
}
|
|
|
|
|
2013-08-18 14:16:15 +00:00
|
|
|
/* sample uniform point on the surface of a sphere */
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float3 sample_uniform_sphere(float u1, float u2)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-01-20 17:49:17 +00:00
|
|
|
float z = 1.0f - 2.0f*u1;
|
|
|
|
float r = sqrtf(fmaxf(0.0f, 1.0f - z*z));
|
2013-05-21 13:22:11 +00:00
|
|
|
float phi = M_2PI_F*u2;
|
2012-01-20 17:49:17 +00:00
|
|
|
float x = r*cosf(phi);
|
|
|
|
float y = r*sinf(phi);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-01-20 17:49:17 +00:00
|
|
|
return make_float3(x, y, z);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float balance_heuristic(float a, float b)
|
2013-08-18 14:15:57 +00:00
|
|
|
{
|
|
|
|
return (a)/(a + b);
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float balance_heuristic_3(float a, float b, float c)
|
2013-08-18 14:15:57 +00:00
|
|
|
{
|
|
|
|
return (a)/(a + b + c);
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float power_heuristic(float a, float b)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
return (a*a)/(a*a + b*b);
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float power_heuristic_3(float a, float b, float c)
|
2013-08-18 14:15:57 +00:00
|
|
|
{
|
|
|
|
return (a*a)/(a*a + b*b + c*c);
|
|
|
|
}
|
|
|
|
|
2014-03-29 12:03:49 +00:00
|
|
|
ccl_device float max_heuristic(float a, float b)
|
|
|
|
{
|
|
|
|
return (a > b)? 1.0f: 0.0f;
|
|
|
|
}
|
|
|
|
|
2013-08-18 14:16:15 +00:00
|
|
|
/* distribute uniform xy on [0,1] over unit disk [-1,1], with concentric mapping
|
|
|
|
* to better preserve stratification for some RNG sequences */
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float2 concentric_sample_disk(float u1, float u2)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2013-08-18 14:16:15 +00:00
|
|
|
float phi, r;
|
|
|
|
float a = 2.0f*u1 - 1.0f;
|
|
|
|
float b = 2.0f*u2 - 1.0f;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2013-08-18 14:16:15 +00:00
|
|
|
if(a == 0.0f && b == 0.0f) {
|
2011-04-27 11:58:34 +00:00
|
|
|
return make_float2(0.0f, 0.0f);
|
|
|
|
}
|
2013-08-18 14:16:15 +00:00
|
|
|
else if(a*a > b*b) {
|
|
|
|
r = a;
|
|
|
|
phi = M_PI_4_F * (b/a);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-08-18 14:16:15 +00:00
|
|
|
r = b;
|
|
|
|
phi = M_PI_2_F - M_PI_4_F * (a/b);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2013-08-18 14:16:15 +00:00
|
|
|
return make_float2(r*cosf(phi), r*sinf(phi));
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2013-08-18 14:16:15 +00:00
|
|
|
/* sample point in unit polygon with given number of corners and rotation */
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float2 regular_polygon_sample(float corners, float rotation, float u, float v)
|
2011-09-16 13:14:02 +00:00
|
|
|
{
|
|
|
|
/* sample corner number and reuse u */
|
|
|
|
float corner = floorf(u*corners);
|
|
|
|
u = u*corners - corner;
|
|
|
|
|
|
|
|
/* uniform sampled triangle weights */
|
|
|
|
u = sqrtf(u);
|
|
|
|
v = v*u;
|
|
|
|
u = 1.0f - u;
|
|
|
|
|
|
|
|
/* point in triangle */
|
|
|
|
float angle = M_PI_F/corners;
|
|
|
|
float2 p = make_float2((u + v)*cosf(angle), (u - v)*sinf(angle));
|
|
|
|
|
|
|
|
/* rotate */
|
|
|
|
rotation += corner*2.0f*angle;
|
|
|
|
|
|
|
|
float cr = cosf(rotation);
|
|
|
|
float sr = sinf(rotation);
|
|
|
|
|
|
|
|
return make_float2(cr*p.x - sr*p.y, sr*p.x + cr*p.y);
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* __KERNEL_MONTECARLO_CL__ */
|
|
|
|
|