Welcome to mirror list, hosted at ThFree Co, Russian Federation.

kernel_light.h « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 537f7ea00f0b29c127c491655828920f48683343 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * 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

struct LightSample {
	float3 P;
	float3 Ng;
	int object;
	int prim;
	int shader;
	float weight;
};

/* Point Light */

__device void point_light_sample(KernelGlobals *kg, int point,
	float randu, float randv, float3 P, LightSample *ls)
{
	float4 f = kernel_tex_fetch(__light_point, point);

	ls->P = make_float3(f.x, f.y, f.z);
	ls->Ng = normalize(ls->P - P);
	ls->shader = __float_as_int(f.w);
	ls->object = ~0;
	ls->prim = ~0;
}

__device float point_light_pdf(KernelGlobals *kg, float t)
{
	return t*t*kernel_data.integrator.pdf_lights;
}

/* Triangle Light */

__device void triangle_light_sample(KernelGlobals *kg, int prim, int object,
	float randu, float randv, LightSample *ls)
{
	/* 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;

#ifdef __INSTANCING__
	/* instance transform */
	if(ls->object >= 0) {
		object_position_transform(kg, ls->object, &ls->P);
		object_normal_transform(kg, ls->object, &ls->Ng);
	}
#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
	   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. */
	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 */

__device void light_sample(KernelGlobals *kg, float randt, float randu, float randv, float3 P, LightSample *ls)
{
	/* 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);
	ls->weight = l.z;

	if(prim >= 0) {
		int object = __float_as_int(l.w);
		triangle_light_sample(kg, prim, object, randu, randv, ls);
	}
	else {
		int point = -prim-1;
		point_light_sample(kg, point, randu, randv, P, ls);
	}
}

__device float light_pdf(KernelGlobals *kg, LightSample *ls, float3 I, float t)
{
	float pdf;

	if(ls->prim != ~0)
		pdf = triangle_light_pdf(kg, ls->Ng, I, t);
	else
		pdf = point_light_pdf(kg, t);
	
	return pdf;
}

CCL_NAMESPACE_END