2011-04-27 11:58:34 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2011-05-20 12:26:01 +00:00
|
|
|
typedef struct LightSample {
|
2011-04-27 11:58:34 +00:00
|
|
|
float3 P;
|
2011-09-27 20:37:24 +00:00
|
|
|
float3 D;
|
2011-04-27 11:58:34 +00:00
|
|
|
float3 Ng;
|
2011-09-27 20:37:24 +00:00
|
|
|
float t;
|
2012-06-04 17:17:10 +00:00
|
|
|
float eval_fac;
|
2011-04-27 11:58:34 +00:00
|
|
|
int object;
|
|
|
|
int prim;
|
|
|
|
int shader;
|
2012-01-20 17:49:17 +00:00
|
|
|
LightType type;
|
2011-05-20 12:26:01 +00:00
|
|
|
} LightSample;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-09-27 20:37:24 +00:00
|
|
|
/* Regular Light */
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-09-27 20:37:24 +00:00
|
|
|
__device float3 disk_light_sample(float3 v, float randu, float randv)
|
|
|
|
{
|
|
|
|
float3 ru, rv;
|
|
|
|
|
|
|
|
make_orthonormals(v, &ru, &rv);
|
|
|
|
to_unit_disk(&randu, &randv);
|
|
|
|
|
|
|
|
return ru*randu + rv*randv;
|
|
|
|
}
|
|
|
|
|
|
|
|
__device float3 distant_light_sample(float3 D, float size, float randu, float randv)
|
|
|
|
{
|
|
|
|
return normalize(D + disk_light_sample(D, randu, randv)*size);
|
|
|
|
}
|
|
|
|
|
|
|
|
__device float3 sphere_light_sample(float3 P, float3 center, float size, float randu, float randv)
|
|
|
|
{
|
|
|
|
return disk_light_sample(normalize(P - center), randu, randv)*size;
|
|
|
|
}
|
|
|
|
|
|
|
|
__device float3 area_light_sample(float3 axisu, float3 axisv, float randu, float randv)
|
|
|
|
{
|
|
|
|
randu = randu - 0.5f;
|
|
|
|
randv = randv - 0.5f;
|
|
|
|
|
|
|
|
return axisu*randu + axisv*randv;
|
|
|
|
}
|
|
|
|
|
2012-05-13 12:32:44 +00:00
|
|
|
#ifdef __BACKGROUND_MIS__
|
2012-01-20 17:49:17 +00:00
|
|
|
__device float3 background_light_sample(KernelGlobals *kg, float randu, float randv, float *pdf)
|
|
|
|
{
|
|
|
|
/* for the following, the CDF values are actually a pair of floats, with the
|
2012-06-09 17:22:52 +00:00
|
|
|
* function value as X and the actual CDF as Y. The last entry's function
|
|
|
|
* value is the CDF total. */
|
2012-01-20 17:49:17 +00:00
|
|
|
int res = kernel_data.integrator.pdf_background_res;
|
|
|
|
int cdf_count = res + 1;
|
|
|
|
|
|
|
|
/* this is basically std::lower_bound as used by pbrt */
|
|
|
|
int first = 0;
|
|
|
|
int count = res;
|
|
|
|
|
|
|
|
while(count > 0) {
|
|
|
|
int step = count >> 1;
|
|
|
|
int middle = first + step;
|
|
|
|
|
|
|
|
if(kernel_tex_fetch(__light_background_marginal_cdf, middle).y < randv) {
|
|
|
|
first = middle + 1;
|
|
|
|
count -= step + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
count = step;
|
|
|
|
}
|
|
|
|
|
|
|
|
int index_v = max(0, first - 1);
|
|
|
|
kernel_assert(index_v >= 0 && index_v < res);
|
|
|
|
|
|
|
|
float2 cdf_v = kernel_tex_fetch(__light_background_marginal_cdf, index_v);
|
|
|
|
float2 cdf_next_v = kernel_tex_fetch(__light_background_marginal_cdf, index_v + 1);
|
|
|
|
float2 cdf_last_v = kernel_tex_fetch(__light_background_marginal_cdf, res);
|
|
|
|
|
|
|
|
/* importance-sampled V direction */
|
|
|
|
float dv = (randv - cdf_v.y) / (cdf_next_v.y - cdf_v.y);
|
|
|
|
float v = (index_v + dv) / res;
|
|
|
|
|
|
|
|
/* this is basically std::lower_bound as used by pbrt */
|
|
|
|
first = 0;
|
|
|
|
count = res;
|
|
|
|
while(count > 0) {
|
|
|
|
int step = count >> 1;
|
|
|
|
int middle = first + step;
|
|
|
|
|
|
|
|
if(kernel_tex_fetch(__light_background_conditional_cdf, index_v * cdf_count + middle).y < randu) {
|
|
|
|
first = middle + 1;
|
|
|
|
count -= step + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
count = step;
|
|
|
|
}
|
|
|
|
|
|
|
|
int index_u = max(0, first - 1);
|
|
|
|
kernel_assert(index_u >= 0 && index_u < res);
|
|
|
|
|
|
|
|
float2 cdf_u = kernel_tex_fetch(__light_background_conditional_cdf, index_v * cdf_count + index_u);
|
|
|
|
float2 cdf_next_u = kernel_tex_fetch(__light_background_conditional_cdf, index_v * cdf_count + index_u + 1);
|
|
|
|
float2 cdf_last_u = kernel_tex_fetch(__light_background_conditional_cdf, index_v * cdf_count + res);
|
|
|
|
|
|
|
|
/* importance-sampled U direction */
|
|
|
|
float du = (randu - cdf_u.y) / (cdf_next_u.y - cdf_u.y);
|
|
|
|
float u = (index_u + du) / res;
|
|
|
|
|
|
|
|
/* compute pdf */
|
|
|
|
float denom = cdf_last_u.x * cdf_last_v.x;
|
2012-02-28 16:44:54 +00:00
|
|
|
float sin_theta = sinf(M_PI_F * v);
|
2012-01-20 17:49:17 +00:00
|
|
|
|
|
|
|
if(sin_theta == 0.0f || denom == 0.0f)
|
|
|
|
*pdf = 0.0f;
|
|
|
|
else
|
|
|
|
*pdf = (cdf_u.x * cdf_v.x)/(2.0f * M_PI_F * M_PI_F * sin_theta * denom);
|
|
|
|
|
|
|
|
*pdf *= kernel_data.integrator.pdf_lights;
|
|
|
|
|
|
|
|
/* compute direction */
|
2012-02-28 16:44:54 +00:00
|
|
|
return -equirectangular_to_direction(u, v);
|
2012-01-20 17:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__device float background_light_pdf(KernelGlobals *kg, float3 direction)
|
|
|
|
{
|
|
|
|
float2 uv = direction_to_equirectangular(direction);
|
|
|
|
int res = kernel_data.integrator.pdf_background_res;
|
|
|
|
|
|
|
|
float sin_theta = sinf(uv.y * M_PI_F);
|
|
|
|
|
|
|
|
if(sin_theta == 0.0f)
|
|
|
|
return 0.0f;
|
|
|
|
|
|
|
|
int index_u = clamp((int)(uv.x * res), 0, res - 1);
|
|
|
|
int index_v = clamp((int)(uv.y * res), 0, res - 1);
|
|
|
|
|
|
|
|
/* pdfs in V direction */
|
|
|
|
float2 cdf_last_u = kernel_tex_fetch(__light_background_conditional_cdf, index_v * (res + 1) + res);
|
|
|
|
float2 cdf_last_v = kernel_tex_fetch(__light_background_marginal_cdf, res);
|
|
|
|
|
|
|
|
float denom = cdf_last_u.x * cdf_last_v.x;
|
|
|
|
|
|
|
|
if(denom == 0.0f)
|
|
|
|
return 0.0f;
|
|
|
|
|
|
|
|
/* pdfs in U direction */
|
|
|
|
float2 cdf_u = kernel_tex_fetch(__light_background_conditional_cdf, index_v * (res + 1) + index_u);
|
|
|
|
float2 cdf_v = kernel_tex_fetch(__light_background_marginal_cdf, index_v);
|
|
|
|
|
|
|
|
float pdf = (cdf_u.x * cdf_v.x)/(2.0f * M_PI_F * M_PI_F * sin_theta * denom);
|
|
|
|
|
|
|
|
return pdf * kernel_data.integrator.pdf_lights;
|
|
|
|
}
|
2012-05-13 12:32:44 +00:00
|
|
|
#endif
|
2012-01-20 17:49:17 +00:00
|
|
|
|
2011-09-27 20:37:24 +00:00
|
|
|
__device void regular_light_sample(KernelGlobals *kg, int point,
|
2012-01-20 17:49:17 +00:00
|
|
|
float randu, float randv, float3 P, LightSample *ls, float *pdf)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-09-27 20:37:24 +00:00
|
|
|
float4 data0 = kernel_tex_fetch(__light_data, point*LIGHT_SIZE + 0);
|
|
|
|
float4 data1 = kernel_tex_fetch(__light_data, point*LIGHT_SIZE + 1);
|
|
|
|
|
|
|
|
LightType type = (LightType)__float_as_int(data0.x);
|
2012-01-20 17:49:17 +00:00
|
|
|
ls->type = type;
|
2011-09-27 20:37:24 +00:00
|
|
|
|
|
|
|
if(type == LIGHT_DISTANT) {
|
|
|
|
/* distant light */
|
|
|
|
float3 D = make_float3(data0.y, data0.z, data0.w);
|
|
|
|
float size = data1.y;
|
|
|
|
|
|
|
|
if(size > 0.0f)
|
|
|
|
D = distant_light_sample(D, size, randu, randv);
|
|
|
|
|
|
|
|
ls->P = D;
|
2011-10-15 23:49:01 +00:00
|
|
|
ls->Ng = D;
|
|
|
|
ls->D = -D;
|
2011-09-27 20:37:24 +00:00
|
|
|
ls->t = FLT_MAX;
|
2012-06-04 17:17:10 +00:00
|
|
|
ls->eval_fac = 1.0f;
|
2011-09-27 20:37:24 +00:00
|
|
|
}
|
2012-01-26 19:45:59 +00:00
|
|
|
#ifdef __BACKGROUND_MIS__
|
2012-01-20 17:49:17 +00:00
|
|
|
else if(type == LIGHT_BACKGROUND) {
|
|
|
|
/* infinite area light (e.g. light dome or env light) */
|
|
|
|
float3 D = background_light_sample(kg, randu, randv, pdf);
|
|
|
|
|
|
|
|
ls->P = D;
|
|
|
|
ls->Ng = D;
|
|
|
|
ls->D = -D;
|
|
|
|
ls->t = FLT_MAX;
|
2012-06-04 17:17:10 +00:00
|
|
|
ls->eval_fac = 1.0f;
|
2012-01-20 17:49:17 +00:00
|
|
|
}
|
2012-01-26 19:45:59 +00:00
|
|
|
#endif
|
2011-09-27 20:37:24 +00:00
|
|
|
else {
|
|
|
|
ls->P = make_float3(data0.y, data0.z, data0.w);
|
|
|
|
|
|
|
|
if(type == LIGHT_POINT) {
|
|
|
|
float size = data1.y;
|
|
|
|
|
|
|
|
/* sphere light */
|
|
|
|
if(size > 0.0f)
|
|
|
|
ls->P += sphere_light_sample(P, ls->P, size, randu, randv);
|
|
|
|
|
|
|
|
ls->Ng = normalize(P - ls->P);
|
2012-06-04 17:17:10 +00:00
|
|
|
ls->eval_fac = 0.25f*M_1_PI_F;
|
|
|
|
}
|
|
|
|
else if(type == LIGHT_SPOT) {
|
|
|
|
float4 data2 = kernel_tex_fetch(__light_data, point*LIGHT_SIZE + 2);
|
|
|
|
float size = data1.y;
|
|
|
|
|
|
|
|
/* spot light */
|
|
|
|
if(size > 0.0f)
|
|
|
|
ls->P += sphere_light_sample(P, ls->P, size, randu, randv);
|
|
|
|
|
|
|
|
float3 dir = make_float3(data1.z, data1.w, data2.x);
|
|
|
|
float3 I = normalize(P - ls->P);
|
|
|
|
|
|
|
|
float spot_angle = data2.y;
|
|
|
|
float spot_smooth = data2.z;
|
|
|
|
|
2012-06-04 19:38:10 +00:00
|
|
|
float eval_fac = dot(dir, I);
|
2012-06-04 17:17:10 +00:00
|
|
|
|
|
|
|
if(eval_fac <= spot_angle) {
|
|
|
|
eval_fac = 0.0f;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
float t = eval_fac - spot_angle;
|
|
|
|
|
|
|
|
if(t < spot_smooth && spot_smooth != 0.0f)
|
|
|
|
eval_fac *= smoothstepf(t/spot_smooth);
|
|
|
|
}
|
|
|
|
|
|
|
|
ls->Ng = I;
|
|
|
|
ls->eval_fac = eval_fac*0.25f*M_1_PI_F;
|
2011-09-27 20:37:24 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* area light */
|
|
|
|
float4 data2 = kernel_tex_fetch(__light_data, point*LIGHT_SIZE + 2);
|
|
|
|
float4 data3 = kernel_tex_fetch(__light_data, point*LIGHT_SIZE + 3);
|
|
|
|
|
|
|
|
float3 axisu = make_float3(data1.y, data1.z, data2.w);
|
|
|
|
float3 axisv = make_float3(data2.y, data2.z, data2.w);
|
|
|
|
float3 D = make_float3(data3.y, data3.z, data3.w);
|
|
|
|
|
|
|
|
ls->P += area_light_sample(axisu, axisv, randu, randv);
|
|
|
|
ls->Ng = D;
|
2012-06-04 17:17:10 +00:00
|
|
|
ls->eval_fac = 0.25f;
|
2011-09-27 20:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ls->t = 0.0f;
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-09-27 20:37:24 +00:00
|
|
|
ls->shader = __float_as_int(data1.x);
|
2011-04-27 11:58:34 +00:00
|
|
|
ls->object = ~0;
|
|
|
|
ls->prim = ~0;
|
|
|
|
}
|
|
|
|
|
2011-09-27 20:37:24 +00:00
|
|
|
__device float regular_light_pdf(KernelGlobals *kg,
|
|
|
|
const float3 Ng, const float3 I, float t)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-09-27 20:37:24 +00:00
|
|
|
float pdf = kernel_data.integrator.pdf_lights;
|
|
|
|
|
|
|
|
if(t == FLT_MAX)
|
|
|
|
return pdf;
|
|
|
|
|
2011-10-15 23:49:01 +00:00
|
|
|
float cos_pi = dot(Ng, I);
|
2011-09-27 20:37:24 +00:00
|
|
|
|
2011-10-15 23:49:01 +00:00
|
|
|
if(cos_pi <= 0.0f)
|
2011-09-27 20:37:24 +00:00
|
|
|
return 0.0f;
|
|
|
|
|
|
|
|
return t*t*pdf/cos_pi;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Triangle Light */
|
|
|
|
|
|
|
|
__device void triangle_light_sample(KernelGlobals *kg, int prim, int object,
|
2012-04-30 12:49:26 +00:00
|
|
|
float randu, float randv, float time, LightSample *ls)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
/* triangle, so get position, normal, shader */
|
|
|
|
ls->P = triangle_sample_MT(kg, prim, randu, randv);
|
|
|
|
ls->Ng = triangle_normal_MT(kg, prim, &ls->shader);
|
|
|
|
ls->object = object;
|
|
|
|
ls->prim = prim;
|
2011-09-27 20:37:24 +00:00
|
|
|
ls->t = 0.0f;
|
2012-01-20 17:49:17 +00:00
|
|
|
ls->type = LIGHT_AREA;
|
2012-06-04 17:17:10 +00:00
|
|
|
ls->eval_fac = 1.0f;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
#ifdef __INSTANCING__
|
|
|
|
/* instance transform */
|
|
|
|
if(ls->object >= 0) {
|
2012-04-30 12:49:26 +00:00
|
|
|
Transform tfm = object_fetch_transform(kg, ls->object, time, OBJECT_TRANSFORM);
|
|
|
|
Transform itfm = object_fetch_transform(kg, ls->object, time, OBJECT_INVERSE_TRANSFORM);
|
|
|
|
|
|
|
|
ls->P = transform_point(&tfm, ls->P);
|
2012-05-08 23:39:31 +00:00
|
|
|
ls->Ng = normalize(transform_direction_transposed(&itfm, ls->Ng));
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
__device float triangle_light_pdf(KernelGlobals *kg,
|
|
|
|
const float3 Ng, const float3 I, float t)
|
|
|
|
{
|
|
|
|
float cos_pi = fabsf(dot(Ng, I));
|
|
|
|
|
|
|
|
if(cos_pi == 0.0f)
|
|
|
|
return 0.0f;
|
|
|
|
|
|
|
|
return (t*t*kernel_data.integrator.pdf_triangles)/cos_pi;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Light Distribution */
|
|
|
|
|
|
|
|
__device int light_distribution_sample(KernelGlobals *kg, float randt)
|
|
|
|
{
|
|
|
|
/* this is basically std::upper_bound as used by pbrt, to find a point light or
|
2012-06-09 17:22:52 +00:00
|
|
|
* triangle to emit from, proportional to area. a good improvement would be to
|
|
|
|
* also sample proportional to power, though it's not so well defined with
|
|
|
|
* OSL shaders. */
|
2011-04-27 11:58:34 +00:00
|
|
|
int first = 0;
|
|
|
|
int len = kernel_data.integrator.num_distribution + 1;
|
|
|
|
|
|
|
|
while(len > 0) {
|
|
|
|
int half_len = len >> 1;
|
|
|
|
int middle = first + half_len;
|
|
|
|
|
|
|
|
if(randt < kernel_tex_fetch(__light_distribution, middle).x) {
|
|
|
|
len = half_len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
first = middle + 1;
|
|
|
|
len = len - half_len - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
first = max(0, first-1);
|
|
|
|
kernel_assert(first >= 0 && first < kernel_data.integrator.num_distribution);
|
|
|
|
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generic Light */
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
__device void light_sample(KernelGlobals *kg, float randt, float randu, float randv, float time, float3 P, LightSample *ls, float *pdf)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
/* sample index */
|
|
|
|
int index = light_distribution_sample(kg, randt);
|
|
|
|
|
|
|
|
/* fetch light data */
|
|
|
|
float4 l = kernel_tex_fetch(__light_distribution, index);
|
|
|
|
int prim = __float_as_int(l.y);
|
|
|
|
|
|
|
|
if(prim >= 0) {
|
|
|
|
int object = __float_as_int(l.w);
|
2012-04-30 12:49:26 +00:00
|
|
|
triangle_light_sample(kg, prim, object, randu, randv, time, ls);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int point = -prim-1;
|
2012-01-20 17:49:17 +00:00
|
|
|
regular_light_sample(kg, point, randu, randv, P, ls, pdf);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2011-09-27 20:37:24 +00:00
|
|
|
|
|
|
|
/* compute incoming direction and distance */
|
|
|
|
if(ls->t != FLT_MAX)
|
|
|
|
ls->D = normalize_len(ls->P - P, &ls->t);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
__device float light_sample_pdf(KernelGlobals *kg, LightSample *ls, float3 I, float t)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
float pdf;
|
|
|
|
|
|
|
|
if(ls->prim != ~0)
|
|
|
|
pdf = triangle_light_pdf(kg, ls->Ng, I, t);
|
|
|
|
else
|
2011-09-27 20:37:24 +00:00
|
|
|
pdf = regular_light_pdf(kg, ls->Ng, I, t);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
return pdf;
|
|
|
|
}
|
|
|
|
|
2012-06-13 11:44:48 +00:00
|
|
|
__device int light_select_num_samples(KernelGlobals *kg, int index)
|
|
|
|
{
|
|
|
|
float4 data3 = kernel_tex_fetch(__light_data, index*LIGHT_SIZE + 3);
|
|
|
|
return __float_as_int(data3.x);
|
|
|
|
}
|
|
|
|
|
2012-01-20 17:49:17 +00:00
|
|
|
__device void light_select(KernelGlobals *kg, int index, float randu, float randv, float3 P, LightSample *ls, float *pdf)
|
2011-09-12 13:13:56 +00:00
|
|
|
{
|
2012-01-20 17:49:17 +00:00
|
|
|
regular_light_sample(kg, index, randu, randv, P, ls, pdf);
|
2012-06-06 23:27:38 +00:00
|
|
|
|
|
|
|
/* compute incoming direction and distance */
|
|
|
|
if(ls->t != FLT_MAX)
|
|
|
|
ls->D = normalize_len(ls->P - P, &ls->t);
|
2011-09-12 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__device float light_select_pdf(KernelGlobals *kg, LightSample *ls, float3 I, float t)
|
|
|
|
{
|
2011-09-27 20:37:24 +00:00
|
|
|
return regular_light_pdf(kg, ls->Ng, I, t);
|
2011-09-12 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|