Blender V5.0
light/triangle.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "kernel/globals.h"
8
10
12#include "kernel/geom/object.h"
14
15#include "util/math_fast.h"
16#include "util/math_intersect.h"
17
19
20/* returns true if the triangle is has motion blur or an instancing transform applied */
22 KernelGlobals kg, const int object, const int prim, const float time, float3 V[3])
23{
24 bool has_motion = false;
25 const int object_flag = kernel_data_fetch(object_flag, object);
26
27 if (object_flag & SD_OBJECT_HAS_VERTEX_MOTION && time >= 0.0f) {
28 motion_triangle_vertices(kg, object, prim, time, V);
29 has_motion = true;
30 }
31 else {
32 triangle_vertices(kg, prim, V);
33 }
34
35 if (!(object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
36#ifdef __OBJECT_MOTION__
37 const float object_time = (time >= 0.0f) ? time : 0.5f;
38 const Transform tfm = object_fetch_transform_motion_test(kg, object, object_time, nullptr);
39#else
41#endif
42 V[0] = transform_point(&tfm, V[0]);
43 V[1] = transform_point(&tfm, V[1]);
44 V[2] = transform_point(&tfm, V[2]);
45 has_motion = true;
46 }
47 return has_motion;
48}
49
51 const float3 I,
52 const float t)
53{
54 const float cos_pi = fabsf(dot(Ng, I));
55
56 if (cos_pi == 0.0f) {
57 return 0.0f;
58 }
59
60 return t * t / cos_pi;
61}
62
64 const ccl_private ShaderData *sd,
65 const float t)
66{
67 /* A naive heuristic to decide between costly solid angle sampling
68 * and simple area sampling, comparing the distance to the triangle plane
69 * to the length of the edges of the triangle. */
70
71 float3 V[3];
72 const bool has_motion = triangle_world_space_vertices(kg, sd->object, sd->prim, sd->time, V);
73
74 const float3 e0 = V[1] - V[0];
75 const float3 e1 = V[2] - V[0];
76 const float3 e2 = V[2] - V[1];
77 const float longest_edge_squared = max(len_squared(e0), max(len_squared(e1), len_squared(e2)));
78 const float3 N = cross(e0, e1);
79 const float distance_to_plane = fabsf(dot(N, sd->wi * t)) / dot(N, N);
80 const float area = 0.5f * len(N);
81
82 float pdf;
83
84 if (longest_edge_squared > distance_to_plane * distance_to_plane) {
85 /* sd contains the point on the light source
86 * calculate Px, the point that we're shading */
87 const float3 Px = sd->P + sd->wi * t;
88
89 const float3 A = safe_normalize(V[0] - Px);
90 const float3 B = safe_normalize(V[1] - Px);
91 const float3 C = safe_normalize(V[2] - Px);
92
93 const float solid_angle = 2.0f * fast_atan2f(fabsf(dot(A, cross(B, C))),
94 (1.0f + dot(B, C) + dot(A, C) + dot(A, B)));
95
96 /* distribution_pdf_triangles is calculated over triangle area, but we're not sampling over
97 * its area */
98 if (UNLIKELY(solid_angle == 0.0f)) {
99 return 0.0f;
100 }
101 pdf = 1.0f / solid_angle;
102 }
103 else {
104 if (UNLIKELY(area == 0.0f)) {
105 return 0.0f;
106 }
107
108 pdf = triangle_light_pdf_area_sampling(sd->Ng, sd->wi, t) / area;
109 }
110
111 /* Belongs in distribution.h but can reuse computations here. */
112 if (!kernel_data.integrator.use_light_tree) {
113 float distribution_area = area;
114
115 if (has_motion && area != 0.0f) {
116 /* For motion blur need area of triangle at fixed time as used in the CDF. */
117 triangle_world_space_vertices(kg, sd->object, sd->prim, -1.0f, V);
118 distribution_area = triangle_area(V[0], V[1], V[2]);
119 }
120
121 pdf *= distribution_area * kernel_data.integrator.distribution_pdf_triangles;
122 }
123
124 return pdf;
125}
126
127template<bool in_volume_segment>
129 const int prim,
130 const int object,
131 const float2 rand,
132 const float time,
134 const float3 P)
135{
136 /* A naive heuristic to decide between costly solid angle sampling
137 * and simple area sampling, comparing the distance to the triangle plane
138 * to the length of the edges of the triangle. */
139
140 float3 V[3];
141 const bool has_motion = triangle_world_space_vertices(kg, object, prim, time, V);
142
143 const float3 e0 = V[1] - V[0];
144 const float3 e1 = V[2] - V[0];
145 const float3 e2 = V[2] - V[1];
146 const float longest_edge_squared = max(len_squared(e0), max(len_squared(e1), len_squared(e2)));
147 float3 N0 = cross(e0, e1);
148 /* Flip normal if necessary. */
149 const int object_flag = kernel_data_fetch(object_flag, object);
150 if (object_flag & SD_OBJECT_NEGATIVE_SCALE) {
151 N0 = -N0;
152 }
153
154 /* Do not draw samples from the side without MIS. */
155 ls->shader = kernel_data_fetch(tri_shader, prim);
156 const float distance_to_plane = dot(N0, V[0] - P) / dot(N0, N0);
157 const int ls_shader_flag = kernel_data_fetch(shaders, ls->shader & SHADER_MASK).flags;
158 if (!in_volume_segment &&
159 !(ls_shader_flag & (distance_to_plane > 0 ? SD_MIS_BACK : SD_MIS_FRONT)))
160 {
161 return false;
162 }
163
164 float Nl = 0.0f;
165 ls->Ng = safe_normalize_len(N0, &Nl);
166 const float area = 0.5f * Nl;
167
168 ls->eval_fac = 1.0f;
169 ls->object = object;
170 ls->prim = prim;
171 ls->shader |= SHADER_USE_MIS;
172 ls->type = LIGHT_TRIANGLE;
173 ls->group = object_lightgroup(kg, object);
174
175 if (!in_volume_segment && (longest_edge_squared > distance_to_plane * distance_to_plane)) {
176 /* A modified version of James Arvo, "Stratified Sampling of Spherical Triangles"
177 * http://www.graphics.cornell.edu/pubs/1995/Arv95c.pdf */
178
179 /* Project the triangle to the unit sphere and calculate the three unit vector that spans the
180 * spherical triangle. */
181 const float3 A = safe_normalize(V[0] - P);
182 const float3 B = safe_normalize(V[1] - P);
183 const float3 C = safe_normalize(V[2] - P);
184
185 const float cos_a = dot(B, C);
186 const float cos_b = dot(A, C);
187 const float cos_c = dot(A, B);
188
189 const float mixed_product = fabsf(dot(A, cross(B, C)));
190
191 /* The area of the spherical triangle is equal to the subtended solid angle. */
192 const float solid_angle = 2.0f * fast_atan2f(mixed_product, (1.0f + cos_a + cos_b + cos_c));
193
194 /* Compute the angle at A. */
195 const float cos_alpha = dot(safe_normalize(cross(A, B)), safe_normalize(cross(A, C)));
196 const float sin_alpha = sin_from_cos(cos_alpha);
197 const float alpha = safe_acosf(cos_alpha);
198
199 /* Select a random sub-area of the spherical triangle and calculate the third vertex C_ of that
200 * new triangle. */
201 const float A_hat = rand.x * solid_angle;
202 float sin_phi;
203 float cos_phi;
204 fast_sincosf(A_hat - alpha, &sin_phi, &cos_phi);
205 const float u = cos_phi - cos_alpha;
206 const float v = sin_phi + sin_alpha * cos_c;
207 const float num = (v * cos_phi - u * sin_phi) * cos_alpha - v;
208 const float den = (v * sin_phi + u * cos_phi) * sin_alpha;
209 const float q = (den == 0.0f) ? 1.0f : num / den;
210
211 const float3 U = safe_normalize(C - cos_b * A);
212 const float3 C_ = safe_normalize(q * A + sin_from_cos(q) * U);
213
214 /* Finally, select a random point along the edge of the new triangle
215 * That point on the spherical triangle is the sampled ray direction */
216 const float z = 1.0f - rand.y * (1.0f - dot(C_, B));
217 ls->D = z * B + sin_from_cos(z) * safe_normalize(C_ - dot(C_, B) * B);
218
219 /* calculate intersection with the planar triangle */
220 if (!ray_triangle_intersect(P, ls->D, 0.0f, FLT_MAX, V[0], V[1], V[2], &ls->u, &ls->v, &ls->t))
221 {
222 ls->pdf = 0.0f;
223 return false;
224 }
225
226 ls->P = P + ls->D * ls->t;
227
228 /* distribution_pdf_triangles is calculated over triangle area, but we're sampling over solid
229 * angle */
230 if (UNLIKELY(solid_angle == 0.0f)) {
231 ls->pdf = 0.0f;
232 return false;
233 }
234 ls->pdf = 1.0f / solid_angle;
235 }
236 else {
237 if (UNLIKELY(area == 0.0f)) {
238 return 0.0f;
239 }
240
241 /* compute random point in triangle. From Eric Heitz's "A Low-Distortion Map Between Triangle
242 * and Square" */
243 float u = rand.x;
244 float v = rand.y;
245 if (v > u) {
246 u *= 0.5f;
247 v -= u;
248 }
249 else {
250 v *= 0.5f;
251 u -= v;
252 }
253
254 const float t = 1.0f - u - v;
255 ls->P = t * V[0] + u * V[1] + v * V[2];
256 /* compute incoming direction, distance and pdf */
257 ls->D = normalize_len(ls->P - P, &ls->t);
258 ls->pdf = triangle_light_pdf_area_sampling(ls->Ng, -ls->D, ls->t) / area;
259 ls->u = u;
260 ls->v = v;
261 }
262
263 /* Belongs in distribution.h but can reuse computations here. */
264 if (!kernel_data.integrator.use_light_tree) {
265 float distribution_area = area;
266
267 if (has_motion && area != 0.0f) {
268 /* For motion blur need area of triangle at fixed time as used in the CDF. */
269 triangle_world_space_vertices(kg, object, prim, -1.0f, V);
270 distribution_area = triangle_area(V[0], V[1], V[2]);
271 }
272
273 ls->pdf_selection = distribution_area * kernel_data.integrator.distribution_pdf_triangles;
274 }
275
276 return (ls->pdf > 0.0f);
277}
278
279/* Find the ray segment lit by the triangle light. */
281 const float3 P,
282 const float3 D,
284 const ccl_private LightSample *ls)
285{
286 const int shader_flag = kernel_data_fetch(shaders, ls->shader & SHADER_MASK).flags;
287 const int SD_MIS_BOTH = SD_MIS_BACK | SD_MIS_FRONT;
288 if ((shader_flag & SD_MIS_BOTH) == SD_MIS_BOTH) {
289 /* Both sides are sampled, the complete ray segment is visible. */
290 return true;
291 }
292
293 /* Only one side is sampled, intersect the ray and the triangle light plane to find the visible
294 * ray segment. Flip normal if Emission Sampling is set to back. */
295 const float3 N = ls->Ng;
296 return ray_plane_intersect((shader_flag & SD_MIS_BACK) ? -N : N, P, D, t_range);
297}
298
299template<bool in_volume_segment>
301 KernelGlobals kg,
302 const ccl_global KernelLightTreeEmitter *kemitter,
303 const float3 centroid,
304 const float3 P,
305 const float3 N,
306 const KernelBoundingCone bcone,
307 ccl_private float &cos_theta_u,
309 ccl_private float3 &point_to_centroid)
310{
311 /* TODO: a cheap substitute for minimal distance between point and primitive. Does it worth the
312 * overhead to compute the accurate minimal distance? */
313 float min_distance;
314 point_to_centroid = safe_normalize_len(centroid - P, &min_distance);
315 distance = make_float2(min_distance, min_distance);
316
317 cos_theta_u = FLT_MAX;
318
319 float3 vertices[3];
320 triangle_vertices(kg, kemitter->triangle.id, vertices);
321
322 bool shape_above_surface = false;
323 for (int i = 0; i < 3; i++) {
324 const float3 corner = vertices[i];
325 float distance_point_to_corner;
326 const float3 point_to_corner = safe_normalize_len(corner - P, &distance_point_to_corner);
327 cos_theta_u = fminf(cos_theta_u, dot(point_to_centroid, point_to_corner));
328 shape_above_surface |= dot(point_to_corner, N) > 0;
329 if (!in_volume_segment) {
330 distance.x = fmaxf(distance.x, distance_point_to_corner);
331 }
332 }
333
334 const bool front_facing = bcone.theta_o != 0.0f || dot(bcone.axis, point_to_centroid) < 0;
335
336 return front_facing && shape_above_surface;
337}
338
#define D
MINLINE float safe_acosf(float a)
ATTR_WARN_UNUSED_RESULT const size_t num
#define UNLIKELY(x)
#define C
Definition RandGen.cpp:29
#define U
ATTR_WARN_UNUSED_RESULT const BMVert * v
#define A
ccl_device float sin_phi(const float3 w)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
dot(value.rgb, luminance_coefficients)") DEFINE_VALUE("REDUCE(lhs
#define kernel_data
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define ccl_private
const ThreadKernelGlobalsCPU * KernelGlobals
#define ccl_device_inline
#define ccl_global
#define CCL_NAMESPACE_END
ccl_device_inline void triangle_vertices(KernelGlobals kg, const int prim, float3 P[3])
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
float distance(VecOp< float, D >, VecOp< float, D >) RET
@ OBJECT_TRANSFORM
ccl_device_inline int object_lightgroup(KernelGlobals kg, const int object)
ccl_device_inline Transform object_fetch_transform(KernelGlobals kg, const int object, enum ObjectTransform type)
ccl_device_inline Transform object_fetch_transform_motion_test(KernelGlobals kg, const int object, const float time, ccl_private Transform *itfm)
@ SD_MIS_BACK
@ SD_MIS_FRONT
@ SHADER_USE_MIS
@ SHADER_MASK
@ SD_OBJECT_NEGATIVE_SCALE
@ SD_OBJECT_TRANSFORM_APPLIED
@ SD_OBJECT_HAS_VERTEX_MOTION
@ LIGHT_TRIANGLE
ccl_device_inline bool triangle_light_valid_ray_segment(KernelGlobals kg, const float3 P, const float3 D, ccl_private Interval< float > *t_range, const ccl_private LightSample *ls)
ccl_device_forceinline bool triangle_light_sample(KernelGlobals kg, const int prim, const int object, const float2 rand, const float time, ccl_private LightSample *ls, const float3 P)
ccl_device_forceinline float triangle_light_pdf(KernelGlobals kg, const ccl_private ShaderData *sd, const float t)
ccl_device_forceinline bool triangle_light_tree_parameters(KernelGlobals kg, const ccl_global KernelLightTreeEmitter *kemitter, const float3 centroid, const float3 P, const float3 N, const KernelBoundingCone bcone, ccl_private float &cos_theta_u, ccl_private float2 &distance, ccl_private float3 &point_to_centroid)
CCL_NAMESPACE_BEGIN ccl_device_inline bool triangle_world_space_vertices(KernelGlobals kg, const int object, const int prim, const float time, float3 V[3])
ccl_device_inline float triangle_light_pdf_area_sampling(const float3 Ng, const float3 I, const float t)
ccl_device_inline float sin_from_cos(const float c)
Definition math_base.h:609
ccl_device float fast_atan2f(const float y, const float x)
Definition math_fast.h:309
ccl_device void fast_sincosf(float x, ccl_private float *sine, ccl_private float *cosine)
Definition math_fast.h:144
ccl_device_inline float len_squared(const float2 a)
ccl_device_inline float2 safe_normalize(const float2 a)
ccl_device_inline float2 normalize_len(const float2 a, ccl_private float *t)
ccl_device_inline float triangle_area(const ccl_private float3 &v1, const ccl_private float3 &v2, const ccl_private float3 &v3)
ccl_device_inline float3 safe_normalize_len(const float3 a, ccl_private float *t)
ccl_device_forceinline bool ray_triangle_intersect(const float3 ray_P, const float3 ray_D, const float ray_tmin, const float ray_tmax, const float3 tri_a, const float3 tri_b, const float3 tri_c, ccl_private float *isect_u, ccl_private float *isect_v, ccl_private float *isect_t)
ccl_device bool ray_plane_intersect(const float3 N, const float3 P, const float3 ray_D, ccl_private Interval< float > *t_range)
#define N
#define B
ccl_device_inline void motion_triangle_vertices(KernelGlobals kg, const int object, const uint3 tri_vindex, const int numsteps, const int numverts, const int step, const float t, float3 verts[3])
#define I
#define fabsf
#define fmaxf
#define make_float2
#define fminf
#define FLT_MAX
Definition stdcycles.h:14
packed_float3 axis
float x
float y
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251
ccl_device_inline float3 transform_point(const ccl_private Transform *t, const float3 a)
Definition transform.h:56
uint len
CCL_NAMESPACE_BEGIN struct Window V