Blender V5.3
mnee.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
10
11#include "kernel/light/sample.h"
12
13/*
14 * Manifold Next Event Estimation
15 *
16 * This code adds manifold next event estimation through refractive surface(s) as a new sampling
17 * technique for direct lighting, i.e. finding the point on the refractive surface(s) along the
18 * path to a light sample, which satisfies fermat's principle for a given microfacet normal and
19 * the path's end points. This technique involves walking on the "specular manifold" using a pseudo
20 * newton solver. Such a manifold is defined by the specular constraint matrix from the manifold
21 * exploration framework [2]. For each refractive interface, this constraint is defined by
22 * enforcing that the generalized half-vector projection onto the interface local tangent plane is
23 * null. The newton solver guides the walk by linearizing the manifold locally before reprojecting
24 * the linear solution onto the refractive surface. See paper [1] for more details about
25 * the technique itself and [3] for the half-vector light transport formulation, from which it is
26 * derived.
27 *
28 * [1] Manifold Next Event Estimation
29 * Johannes Hanika, Marc Droske, and Luca Fascione. 2015.
30 * Comput. Graph. Forum 34, 4 (July 2015), 87–97.
31 * https://jo.dreggn.org/home/2015_mnee.pdf
32 *
33 * [2] Manifold exploration: a Markov Chain Monte Carlo technique for rendering scenes with
34 * difficult specular transport Wenzel Jakob and Steve Marschner. 2012. ACM Trans. Graph. 31, 4,
35 * Article 58 (July 2012), 13 pages.
36 * https://www.cs.cornell.edu/projects/manifolds-sg12/
37 *
38 * [3] The Natural-Constraint Representation of the Path Space for Efficient Light Transport
39 * Simulation Anton S. Kaplanyan, Johannes Hanika, and Carsten Dachsbacher. 2014. ACM Trans. Graph.
40 * 33, 4, Article 102 (July 2014), 13 pages.
41 * https://cg.ivd.kit.edu/english/HSLT.php
42 */
43
44// NOLINTBEGIN
45#define MNEE_MAX_ITERATIONS 64
46#define MNEE_MAX_INTERSECTION_COUNT 10
47#define MNEE_SOLVER_THRESHOLD 0.001f
48#define MNEE_MINIMUM_STEP_SIZE 0.0001f
49#define MNEE_MAX_CAUSTIC_CASTERS 6
50#define MNEE_MIN_DISTANCE 0.001f
51#define MNEE_MIN_PROGRESS_DISTANCE 0.0001f
52#define MNEE_MIN_DETERMINANT 0.0001f
53#define MNEE_PROJECTION_DISTANCE_MULTIPLIER 2.f
54// NOLINTEND
55
57
58/* Manifold struct containing the local differential geometry quantity */
60 /* Position and partials */
64
65 /* Normal and partials */
70
71 /* geometric info */
73 int object;
74 int prim;
75 int shader;
76
77 /* closure info */
78 float eta;
79 ccl_private ShaderClosure *bsdf;
81
82 /* constraint and its derivative matrices */
87};
88
89/* Multiplication of a 2x2 matrix encoded in a row-major order float4 by a vector */
91{
92 return make_float2(a.x * b.x + a.y * b.y, a.z * b.x + a.w * b.y);
93}
94
95/* Multiplication of 2x2 matrices encoded in a row-major order float4 */
97{
98 return make_float4(
99 a.x * b.x + a.y * b.z, a.x * b.y + a.y * b.w, a.z * b.x + a.w * b.z, a.z * b.y + a.w * b.w);
100}
101
102/* Determinant of a 2x2 matrix encoded in a row-major order float4 */
104{
105 return m.x * m.w - m.y * m.z;
106}
107
108/* Inverse of a 2x2 matrix encoded in a row-major order float4 */
110{
111 const float det = mat22_determinant(m);
112 if (fabsf(det) < MNEE_MIN_DETERMINANT) {
113 return 0.f;
114 }
115 m_inverse = make_float4(m.w, -m.y, -m.z, m.x) / det;
116 return det;
117}
118
119/* Manifold vertex setup from ray and intersection data */
122 ccl_private ShaderClosure *bsdf,
123 const float eta,
124 const float2 n_offset,
125 const ccl_private Ray *ray,
126 const ccl_private Intersection *isect,
127 ccl_private ShaderData *sd_vtx)
128{
129 sd_vtx->object = (isect->object == OBJECT_NONE) ? kernel_data_fetch(prim_object, isect->prim) :
130 isect->object;
131
132 sd_vtx->type = isect->type;
133 sd_vtx->runtime_flag = 0;
134 sd_vtx->shader_flag = 0;
135 sd_vtx->object_flag = kernel_data_fetch(object_flag, sd_vtx->object);
136
137 /* Matrices and time. */
138 shader_setup_object_transforms(kg, sd_vtx, ray->time);
139 sd_vtx->time = ray->time;
140
141 sd_vtx->prim = isect->prim;
142 sd_vtx->ray_length = isect->t;
143
144 sd_vtx->u = isect->u;
145 sd_vtx->v = isect->v;
146
147 sd_vtx->shader = kernel_data_fetch(tri_shader, sd_vtx->prim);
148
149 float3 verts[3];
150 float3 normals[3];
151 if (sd_vtx->type & PRIMITIVE_TRIANGLE) {
152 /* Load triangle vertices and normals. */
153 triangle_vertices_and_normals(kg, sd_vtx, verts, normals);
154 }
155 else { /* if (sd_vtx->type & PRIMITIVE_MOTION_TRIANGLE) */
156 /* Load triangle vertices and normals. */
157 motion_triangle_vertices_and_normals(kg, sd_vtx, verts, normals);
158 }
159
160 /* Compute refined position. */
161 sd_vtx->P = triangle_point_from_uv_and_verts(kg, sd_vtx, isect->u, isect->v, verts);
162
163 /* Instance transform. */
164 if (!(sd_vtx->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
165 object_position_transform(kg, sd_vtx, &verts[0]);
166 object_position_transform(kg, sd_vtx, &verts[1]);
167 object_position_transform(kg, sd_vtx, &verts[2]);
168 object_normal_transform(kg, sd_vtx, &normals[0]);
169 object_normal_transform(kg, sd_vtx, &normals[1]);
170 object_normal_transform(kg, sd_vtx, &normals[2]);
171 }
172
173 /* Tangent space (position derivatives) WRT barycentric (u, v). */
174 float3 dp_du = verts[1] - verts[0];
175 float3 dp_dv = verts[2] - verts[0];
176
177 /* Geometric normal. */
178 vtx->ng = normalize(cross(dp_du, dp_dv));
179 if (sd_vtx->object_flag & SD_OBJECT_NEGATIVE_SCALE) {
180 vtx->ng = -vtx->ng;
181 }
182
183 /* Shading normals: Interpolate normals between vertices. */
184 float n_len;
185 vtx->n = normalize_len(normals[0] * (1.0f - sd_vtx->u - sd_vtx->v) + normals[1] * sd_vtx->u +
186 normals[2] * sd_vtx->v,
187 &n_len);
188
189 /* Shading normal derivatives WRT barycentric (u, v)
190 * we calculate the derivative of n = |u*n0 + v*n1 + (1-u-v)*n2| using:
191 * d/du [f(u)/|f(u)|] = [d/du f(u)]/|f(u)| - f(u)/|f(u)|^3 <f(u), d/du f(u)>. */
192 const float inv_n_len = 1.f / n_len;
193 float3 dn_du = inv_n_len * (normals[1] - normals[0]);
194 float3 dn_dv = inv_n_len * (normals[2] - normals[0]);
195 dn_du -= vtx->n * dot(vtx->n, dn_du);
196 dn_dv -= vtx->n * dot(vtx->n, dn_dv);
197
198 /* Orthonormalize (dp_du,dp_dv) using a linear transformation, which
199 * we use on (dn_du,dn_dv) as well so the new (u,v) are consistent. */
200 const float inv_len_dp_du = 1.f / len(dp_du);
201 dp_du *= inv_len_dp_du;
202 dn_du *= inv_len_dp_du;
203
204 const float dpdu_dot_dpdv = dot(dp_du, dp_dv);
205 dp_dv -= dpdu_dot_dpdv * dp_du;
206 dn_dv -= dpdu_dot_dpdv * dn_du;
207
208 const float inv_len_dp_dv = 1.f / len(dp_dv);
209 dp_dv *= inv_len_dp_dv;
210 dn_dv *= inv_len_dp_dv;
211
212 /* Find consistent tangent frame for every point on the surface. */
213 make_orthonormals(vtx->ng, &vtx->dp_du, &vtx->dp_dv);
214 /* Apply the equivalent rotation to the normal derivatives. */
215 const float cos_theta = dot(dp_du, vtx->dp_du);
216 const float sin_theta = -dot(dp_dv, vtx->dp_du);
217 vtx->dn_du = cos_theta * dn_du - sin_theta * dn_dv;
218 vtx->dn_dv = sin_theta * dn_du + cos_theta * dn_dv;
219
220 /* Manifold vertex position. */
221 vtx->p = sd_vtx->P;
222
223 /* Initialize constraint and its derivatives. */
224 vtx->a = vtx->c = zero_float4();
225 vtx->b = make_float4(1.f, 0.f, 0.f, 1.f);
226 vtx->constraint = zero_float2();
227 vtx->n_offset = n_offset;
228
229 /* Closure information. */
230 vtx->bsdf = bsdf;
231 vtx->eta = eta;
232
233 /* Geometric information. */
234 vtx->uv = make_float2(isect->u, isect->v);
235 vtx->object = sd_vtx->object;
236 vtx->prim = sd_vtx->prim;
237 vtx->shader = sd_vtx->shader;
238}
239
240/* Compute constraint derivatives. */
241
242#if defined(__KERNEL_METAL__)
243/* Temporary workaround for front-end compilation bug (incorrect MNEE rendering when this is
244 * inlined). */
245__attribute__((noinline))
246#else
248#endif
249bool mnee_compute_constraint_derivatives(const int vertex_count,
250 ccl_private ManifoldVertex *vertices,
251 const ccl_private float3 &surface_sample_pos,
252 const bool light_fixed_direction,
253 const float3 light_sample)
254{
255 for (int vi = 0; vi < vertex_count; vi++) {
256 ccl_private ManifoldVertex &v = vertices[vi];
257
258 /* Direction toward surface sample. */
259 float3 wi = (vi == 0) ? surface_sample_pos - v.p : vertices[vi - 1].p - v.p;
260 float ili = len(wi);
261 if (ili < MNEE_MIN_DISTANCE) {
262 return false;
263 }
264 ili = 1.f / ili;
265 wi *= ili;
266
267 /* Direction toward light sample. */
268 float3 wo = (vi == vertex_count - 1) ?
269 (light_fixed_direction ? light_sample : light_sample - v.p) :
270 vertices[vi + 1].p - v.p;
271 float ilo = len(wo);
272 if (ilo < MNEE_MIN_DISTANCE) {
273 return false;
274 }
275 ilo = 1.f / ilo;
276 wo *= ilo;
277
278 /* Invert ior if coming from inside. */
279 float eta = v.eta;
280 if (dot(wi, v.ng) < .0f) {
281 eta = 1.f / eta;
282 }
283
284 /* Half vector. */
285 float3 H = -(wi + eta * wo);
286 const float ilh = 1.f / len(H);
287 H *= ilh;
288
289 ilo *= eta * ilh;
290 ili *= ilh;
291
292 /* Local shading frame. */
293 const float dp_du_dot_n = dot(v.dp_du, v.n);
294 float3 s = v.dp_du - dp_du_dot_n * v.n;
295 const float inv_len_s = 1.f / len(s);
296 s *= inv_len_s;
297 const float3 t = cross(v.n, s);
298
299 float3 dH_du;
300 float3 dH_dv;
301
302 /* Constraint derivatives WRT previous vertex. */
303 if (vi > 0) {
304 const ccl_private ManifoldVertex &v_prev = vertices[vi - 1];
305 dH_du = (v_prev.dp_du - wi * dot(wi, v_prev.dp_du)) * ili;
306 dH_dv = (v_prev.dp_dv - wi * dot(wi, v_prev.dp_dv)) * ili;
307 dH_du -= H * dot(dH_du, H);
308 dH_dv -= H * dot(dH_dv, H);
309 dH_du = -dH_du;
310 dH_dv = -dH_dv;
311
312 v.a = make_float4(dot(dH_du, s), dot(dH_dv, s), dot(dH_du, t), dot(dH_dv, t));
313 }
314
315 /* Constraint derivatives WRT current vertex. */
316 if (vi == vertex_count - 1 && light_fixed_direction) {
317 dH_du = ili * (-v.dp_du + wi * dot(wi, v.dp_du));
318 dH_dv = ili * (-v.dp_dv + wi * dot(wi, v.dp_dv));
319 }
320 else {
321 dH_du = -v.dp_du * (ili + ilo) + wi * (dot(wi, v.dp_du) * ili) +
322 wo * (dot(wo, v.dp_du) * ilo);
323 dH_dv = -v.dp_dv * (ili + ilo) + wi * (dot(wi, v.dp_dv) * ili) +
324 wo * (dot(wo, v.dp_dv) * ilo);
325 }
326 dH_du -= H * dot(dH_du, H);
327 dH_dv -= H * dot(dH_dv, H);
328 dH_du = -dH_du;
329 dH_dv = -dH_dv;
330
331 float3 ds_du = -inv_len_s * (dot(v.dp_du, v.dn_du) * v.n + dp_du_dot_n * v.dn_du);
332 float3 ds_dv = -inv_len_s * (dot(v.dp_du, v.dn_dv) * v.n + dp_du_dot_n * v.dn_dv);
333 ds_du -= s * dot(s, ds_du);
334 ds_dv -= s * dot(s, ds_dv);
335 const float3 dt_du = cross(v.dn_du, s) + cross(v.n, ds_du);
336 const float3 dt_dv = cross(v.dn_dv, s) + cross(v.n, ds_dv);
337
338 v.b = make_float4(dot(dH_du, s) + dot(H, ds_du),
339 dot(dH_dv, s) + dot(H, ds_dv),
340 dot(dH_du, t) + dot(H, dt_du),
341 dot(dH_dv, t) + dot(H, dt_dv));
342
343 /* Constraint derivatives WRT next vertex. */
344 if (vi < vertex_count - 1) {
345 const ccl_private ManifoldVertex &v_next = vertices[vi + 1];
346 dH_du = (v_next.dp_du - wo * dot(wo, v_next.dp_du)) * ilo;
347 dH_dv = (v_next.dp_dv - wo * dot(wo, v_next.dp_dv)) * ilo;
348 dH_du -= H * dot(dH_du, H);
349 dH_dv -= H * dot(dH_dv, H);
350 dH_du = -dH_du;
351 dH_dv = -dH_dv;
352
353 v.c = make_float4(dot(dH_du, s), dot(dH_dv, s), dot(dH_du, t), dot(dH_dv, t));
354 }
355
356 /* Constraint vector WRT. the local shading frame. */
357 v.constraint = make_float2(dot(s, H), dot(t, H)) - v.n_offset;
358 }
359 return true;
360}
361
362/* Invert (block) constraint derivative matrix and solve linear system so we can map dh back to dx:
363 * dh / dx = A
364 * dx = inverse(A) x dh
365 * to use for specular manifold walk
366 * (See for example http://faculty.washington.edu/finlayso/ebook/algebraic/advanced/LUtri.htm
367 * for block tridiagonal matrix based linear system solve) */
368ccl_device_inline bool mnee_solve_matrix_h_to_x(const int vertex_count,
369 ccl_private ManifoldVertex *vertices,
371{
374
375 /* Block tridiagonal LU factorization. */
376 float4 Lk = vertices[0].b;
377 if (mat22_inverse(Lk, Li[0]) == 0.f) {
378 return false;
379 }
380
381 C[0] = vertices[0].constraint;
382
383 for (int k = 1; k < vertex_count; k++) {
384 const float4 A = mat22_mult(vertices[k].a, Li[k - 1]);
385
386 Lk = vertices[k].b - mat22_mult(A, vertices[k - 1].c);
387 if (mat22_inverse(Lk, Li[k]) == 0.f) {
388 return false;
389 }
390
391 C[k] = vertices[k].constraint - mat22_mult(A, C[k - 1]);
392 }
393
394 dx[vertex_count - 1] = mat22_mult(Li[vertex_count - 1], C[vertex_count - 1]);
395 for (int k = vertex_count - 2; k > -1; k--) {
396 dx[k] = mat22_mult(Li[k], C[k] - mat22_mult(vertices[k].c, dx[k + 1]));
397 }
398
399 return true;
400}
401
402/* Newton solver to walk on specular manifold. */
404 const ccl_private ShaderData *sd,
405 ccl_private ShaderData *sd_vtx,
406 const ccl_private LightSample *ls,
407 const bool light_fixed_direction,
408 const int vertex_count,
409 ccl_private ManifoldVertex *vertices)
410{
413
414 Ray projection_ray;
415 projection_ray.self.light_object = OBJECT_NONE;
416 projection_ray.self.light_prim = PRIM_NONE;
417 projection_ray.dP = differential_make_compact(sd->dP);
418 projection_ray.dD = differential_zero_compact();
419 projection_ray.tmin = 0.0f;
420 projection_ray.time = sd->time;
421 Intersection projection_isect;
422
423 const float3 light_sample = light_fixed_direction ? ls->D : ls->P;
424
425 /* We start gently, potentially ramping up to beta = 1, since target configurations
426 * far from the seed path can send the proposed solution further than the linearized
427 * local differential geometric quantities are meant for (especially dn/du and dn/dv). */
428 float beta = .1f;
429 bool reduce_stepsize = false;
430 bool resolve_constraint = true;
431 for (int iteration = 0; iteration < MNEE_MAX_ITERATIONS; iteration++) {
432 if (resolve_constraint) {
433 /* Calculate constraint and its derivatives for vertices. */
435 vertex_count, vertices, sd->P, light_fixed_direction, light_sample))
436 {
437 return false;
438 }
439
440 /* Calculate constraint norm. */
441 float constraint_norm = 0.f;
442 for (int vi = 0; vi < vertex_count; vi++) {
443 constraint_norm = fmaxf(constraint_norm, len(vertices[vi].constraint));
444 }
445
446 /* Return if solve successful. */
447 if (constraint_norm < MNEE_SOLVER_THRESHOLD) {
448 return true;
449 }
450
451 /* Invert derivative matrix. */
452 if (!mnee_solve_matrix_h_to_x(vertex_count, vertices, dx)) {
453 return false;
454 }
455 }
456
457 /* Construct tentative new vertices and project back onto surface. */
458 for (int vi = 0; vi < vertex_count; vi++) {
459 const ccl_private ManifoldVertex &mv = vertices[vi];
460
461 /* Tentative new position on linearized manifold (tangent plane). */
462 float3 tentative_p = mv.p - beta * (dx[vi].x * mv.dp_du + dx[vi].y * mv.dp_dv);
463
464 /* For certain configs, the first solve ends up below the receiver. */
465 if (vi == 0) {
466 const float3 wo = tentative_p - sd->P;
467 if (dot(sd->Ng, wo) <= 0.f) {
468 /* Change direction for the 1st interface. */
469 tentative_p = mv.p + beta * (dx[vi].x * mv.dp_du + dx[vi].y * mv.dp_dv);
470 }
471 }
472
473 /* Project tentative point from tangent plane back to surface
474 * we ignore all other intersections since this tentative path could lead
475 * valid to a valid path even if occluded. */
476 if (vi == 0) {
477 projection_ray.self.object = sd->object;
478 projection_ray.self.prim = sd->prim;
479 projection_ray.P = sd->P;
480 }
481 else {
482 const ccl_private ManifoldVertex &pv = vertices[vi - 1];
483 projection_ray.self.object = pv.object;
484 projection_ray.self.prim = pv.prim;
485 projection_ray.P = pv.p;
486 }
487 projection_ray.D = normalize_len(tentative_p - projection_ray.P, &projection_ray.tmax);
489
490 bool projection_success = false;
491 for (int isect_count = 0; isect_count < MNEE_MAX_INTERSECTION_COUNT; isect_count++) {
492 const bool hit = scene_intersect(
493 kg, &projection_ray, PATH_RAY_VISIBILITY_TRANSMIT, &projection_isect);
494 if (!hit) {
495 break;
496 }
497
498 if (projection_isect.object == mv.object) {
499 projection_success = true;
500 break;
501 }
502
503 projection_ray.self.object = projection_isect.object;
504 projection_ray.self.prim = projection_isect.prim;
505 projection_ray.tmin = intersection_t_offset(projection_isect.t);
506 }
507 if (!projection_success) {
508 reduce_stepsize = true;
509 break;
510 }
511
512 /* Initialize tangent frame, which will be used as reference. */
513 ccl_private ManifoldVertex &tv = tentative[vi];
514 tv.p = mv.p;
515 tv.dp_du = mv.dp_du;
516 tv.dp_dv = mv.dp_dv;
517
518 /* Setup corrected manifold vertex. */
520 kg, &tv, mv.bsdf, mv.eta, mv.n_offset, &projection_ray, &projection_isect, sd_vtx);
521
522 /* Fail newton solve if we are not making progress, probably stuck trying to move off the
523 * edge of the mesh. */
524 const float distance = len(tv.p - mv.p);
526 return false;
527 }
528 }
529
530 /* Check that tentative path is still transmissive. */
531 if (!reduce_stepsize) {
532 for (int vi = 0; vi < vertex_count; vi++) {
533 const ccl_private ManifoldVertex &tv = tentative[vi];
534
535 /* Direction toward surface sample. */
536 const float3 wi = (vi == 0 ? sd->P : tentative[vi - 1].p) - tv.p;
537 /* Direction toward light sample. */
538 const float3 wo = (vi == vertex_count - 1) ? light_fixed_direction ? ls->D : ls->P - tv.p :
539 tentative[vi + 1].p - tv.p;
540
541 if (dot(tv.n, wi) * dot(tv.n, wo) >= 0.f) {
542 reduce_stepsize = true;
543 break;
544 }
545 }
546 }
547
548 if (reduce_stepsize) {
549 /* Adjust step if can't land on right surface. */
550 reduce_stepsize = false;
551 resolve_constraint = false;
552 beta *= .5f;
553
554 /* Fail newton solve if the stepsize is too small. */
556 return false;
557 }
558
559 continue;
560 }
561
562 /* Copy tentative vertices to main vertex list. */
563 for (int vi = 0; vi < vertex_count; vi++) {
564 vertices[vi] = tentative[vi];
565 }
566
567 /* Increase the step to get back to 1. */
568 resolve_constraint = true;
569 beta = min(1.f, 2.f * beta);
570 }
571
572 return false;
573}
574
575/* Sample bsdf in half-vector measure. */
577 const float alpha_x,
578 const float alpha_y,
579 const float sample_u,
580 const float sample_v)
581{
582 float alpha2;
583 float cos_phi;
584 float sin_phi;
585
586 if (alpha_x == alpha_y) {
587 const float phi = sample_v * M_2PI_F;
588 fast_sincosf(phi, &sin_phi, &cos_phi);
589 alpha2 = alpha_x * alpha_x;
590 }
591 else {
592 float phi = atanf(alpha_y / alpha_x * tanf(M_2PI_F * sample_v + M_PI_2_F));
593 if (sample_v > .5f) {
594 phi += M_PI_F;
595 }
596 fast_sincosf(phi, &sin_phi, &cos_phi);
597 const float alpha_x2 = alpha_x * alpha_x;
598 const float alpha_y2 = alpha_y * alpha_y;
599 alpha2 = 1.f / (cos_phi * cos_phi / alpha_x2 + sin_phi * sin_phi / alpha_y2);
600 }
601
602 /* Map sampled angles to micro-normal direction h. */
603 float tan2_theta = alpha2;
605 tan2_theta *= -logf(1.0f - sample_u);
606 }
607 else { /* type == CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID assumed */
608 tan2_theta *= sample_u / (1.0f - sample_u);
609 }
610 const float cos2_theta = 1.0f / (1.0f + tan2_theta);
611 const float sin_theta = safe_sqrtf(1.0f - cos2_theta);
612 return make_float2(cos_phi * sin_theta, sin_phi * sin_theta);
613}
614
615/* Evaluate product term inside eq.6 at solution interface vi
616 * divided by corresponding sampled pdf:
617 * fr(vi)_do / pdf_dh(vi) x |do/dh| x |n.wo / n.h|
618 * We assume here that the pdf (in half-vector measure) is the same as
619 * the one calculation when sampling the microfacet normals from the
620 * specular chain above: this allows us to simplify the bsdf weight */
622 ccl_private ShaderClosure *closure,
623 const float3 wi,
624 const float3 wo)
625{
627
628 const float cosNI = dot(bsdf->N, wi);
629 const float cosNO = dot(bsdf->N, wo);
630
631 const float3 Ht = normalize(-(bsdf->ior * wo + wi));
632 const float cosHI = dot(Ht, wi);
633
634 const float alpha2 = bsdf->alpha_x * bsdf->alpha_y;
635 const float cosThetaM = dot(bsdf->N, Ht);
636
637 /* Now calculate G1(i, m) and G1(o, m). */
638 float G;
640 G = bsdf_G<MicrofacetType::BECKMANN>(alpha2, cosNI, cosNO);
641 }
642 else { /* bsdf->type == CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID assumed */
643 G = bsdf_G<MicrofacetType::GGX>(alpha2, cosNI, cosNO);
644 }
645
646 const FresnelCoeff fresnel = microfacet_fresnel(kg, bsdf, cosHI, nullptr);
647
648 /*
649 * bsdf_do = (1 - F) * D_do * G * |h.wi| / (n.wi * n.wo)
650 * pdf_dh = D_dh * cosThetaM
651 * D_do = D_dh * |dh/do|
652 *
653 * contribution = bsdf_do * |do/dh| * |n.wo / n.h| / pdf_dh
654 * = (1 - F) * G * |h.wi / (n.wi * n.h^2)|
655 */
656 /* TODO: energy compensation for multi-GGX. */
657 return bsdf->weight * fresnel.transmittance * G * fabsf(cosHI / (cosNI * sqr(cosThetaM)));
658}
659
660/* Compute transfer matrix determinant |T1| = |dx1/dxn| (and |dh/dx| in the process) */
662 const ccl_private LightSample *ls,
663 const bool light_fixed_direction,
664 const int vertex_count,
665 ccl_private ManifoldVertex *vertices,
666 ccl_private float *dx1_dxlight,
667 ccl_private float *dh_dx)
668{
669 /* Simplified block tridiagonal LU factorization. */
670 float4 Li;
672
673 float4 Lk = vertices[0].b;
674 float Lk_det = mat22_inverse(Lk, Li);
675 if (Lk_det == 0.f) {
676 return false;
677 }
678
679 float det_dh_dx = Lk_det;
680
681 for (int k = 1; k < vertex_count; k++) {
682 U[k - 1] = mat22_mult(Li, vertices[k - 1].c);
683
684 Lk = vertices[k].b - mat22_mult(vertices[k].a, U[k - 1]);
685 Lk_det = mat22_inverse(Lk, Li);
686 if (Lk_det == 0.f) {
687 return false;
688 }
689
690 det_dh_dx *= Lk_det;
691 }
692
693 /* Fill out constraint derivatives WRT light vertex param. */
694
695 /* Local shading frame at last free vertex. */
696 const int mi = vertex_count - 1;
697 const ccl_private ManifoldVertex &m = vertices[mi];
698
699 const float3 s = normalize(m.dp_du - dot(m.dp_du, m.n) * m.n);
700 const float3 t = cross(m.n, s);
701
702 /* Local differential geometry. */
703 float3 dp_du;
704 float3 dp_dv;
705 make_orthonormals(ls->Ng, &dp_du, &dp_dv);
706
707 /* Direction toward surface sample. */
708 float3 wi = vertex_count == 1 ? sd->P - m.p : vertices[mi - 1].p - m.p;
709 const float ili = 1.f / len(wi);
710 wi *= ili;
711
712 /* Invert ior if coming from inside. */
713 float eta = m.eta;
714 if (dot(wi, m.ng) < .0f) {
715 eta = 1.f / eta;
716 }
717
718 float dxn_dwn;
719 float4 dc_dlight;
720
721 if (light_fixed_direction) {
722 /* Constant direction toward light sample. */
723 const float3 wo = ls->D;
724
725 /* Half vector. */
726 float3 H = -(wi + eta * wo);
727 const float ilh = 1.f / len(H);
728 H *= ilh;
729
730 const float ilo = -eta * ilh;
731
732 const float cos_theta = dot(wo, m.n);
733 const float sin_theta = sin_from_cos(cos_theta);
734 const float cos_phi = dot(wo, s);
735 const float sin_phi = sin_from_cos(cos_phi);
736
737 /* Wo = (cos_phi * sin_theta) * s + (sin_phi * sin_theta) * t + cos_theta * n. */
738 float3 dH_dtheta = ilo * (cos_theta * (cos_phi * s + sin_phi * t) - sin_theta * m.n);
739 float3 dH_dphi = ilo * sin_theta * (-sin_phi * s + cos_phi * t);
740 dH_dtheta -= H * dot(dH_dtheta, H);
741 dH_dphi -= H * dot(dH_dphi, H);
742
743 /* Constraint derivatives WRT light direction expressed
744 * in spherical coordinates (theta, phi). */
745 dc_dlight = make_float4(
746 dot(dH_dtheta, s), dot(dH_dphi, s), dot(dH_dtheta, t), dot(dH_dphi, t));
747
748 /* Jacobian to convert dtheta x dphi to dw measure. */
749 dxn_dwn = 1.f / fmaxf(MNEE_MIN_DISTANCE, fabsf(sin_theta));
750 }
751 else {
752 /* Direction toward light sample. */
753 float3 wo = ls->P - m.p;
754 float ilo = 1.f / len(wo);
755 wo *= ilo;
756
757 /* Half vector. */
758 float3 H = -(wi + eta * wo);
759 const float ilh = 1.f / len(H);
760 H *= ilh;
761
762 ilo *= eta * ilh;
763
764 float3 dH_du = (dp_du - wo * dot(wo, dp_du)) * ilo;
765 float3 dH_dv = (dp_dv - wo * dot(wo, dp_dv)) * ilo;
766 dH_du -= H * dot(dH_du, H);
767 dH_dv -= H * dot(dH_dv, H);
768 dH_du = -dH_du;
769 dH_dv = -dH_dv;
770
771 dc_dlight = make_float4(dot(dH_du, s), dot(dH_dv, s), dot(dH_du, t), dot(dH_dv, t));
772
773 /* Neutral value since dc_dlight is already in the desired vertex area measure. */
774 dxn_dwn = 1.f;
775 }
776
777 /* Compute transfer matrix. */
778 float4 Tp = -mat22_mult(Li, dc_dlight);
779 for (int k = vertex_count - 2; k > -1; k--) {
780 Tp = -mat22_mult(U[k], Tp);
781 }
782
783 *dx1_dxlight = fabsf(mat22_determinant(Tp)) * dxn_dwn;
784 *dh_dx = fabsf(det_dh_dx);
785 return true;
786}
787
788/* Calculate the path contribution. */
791 ccl_private ShaderData *sd,
792 ccl_private ShaderData *sd_mnee,
794 const bool light_fixed_direction,
795 const int vertex_count,
796 ccl_private ManifoldVertex *vertices,
797 ccl_private Spectrum *throughput,
798 ccl_private float3 *r_receiver_wo)
799{
800 float wo_len;
801 float3 wo = normalize_len(vertices[0].p - sd->P, &wo_len);
802
803 /* Initialize throughput. */
804 *r_receiver_wo = wo;
805 *throughput = one_spectrum();
806
807 /* Update light sample with new position / direction and keep pdf in vertex area measure. */
808 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
810 kg, ls, vertices[vertex_count - 1].p, vertices[vertex_count - 1].n, path_flag);
811
812 /* Save state path bounce info in case a light path node is used in the refractive interface or
813 * light shader graph. */
814 const int transmission_bounce = INTEGRATOR_STATE(state, path, transmission_bounce);
815 const int diffuse_bounce = INTEGRATOR_STATE(state, path, diffuse_bounce);
816 const int bounce = INTEGRATOR_STATE(state, path, bounce);
817
818 /* Set diffuse bounce info. */
819 INTEGRATOR_STATE_WRITE(state, path, diffuse_bounce) = diffuse_bounce + 1;
820
821 /* Set bounce info in case a light path node is used in the light shader graph. */
822 INTEGRATOR_STATE_WRITE(state, path, transmission_bounce) = transmission_bounce + vertex_count -
823 1;
824 INTEGRATOR_STATE_WRITE(state, path, bounce) = bounce + vertex_count;
825
826 if (sd_mnee->runtime_flag & SR_CACHE_MISS) {
827 /* Restore original state path bounce info. */
828 INTEGRATOR_STATE_WRITE(state, path, transmission_bounce) = transmission_bounce;
829 INTEGRATOR_STATE_WRITE(state, path, diffuse_bounce) = diffuse_bounce;
830 INTEGRATOR_STATE_WRITE(state, path, bounce) = bounce;
831
833 }
834 *throughput *= ls->eval_fac / ls->pdf;
835
836 /* Generalized geometry term. */
837 float dh_dx;
838 float dx1_dxlight;
840 sd, ls, light_fixed_direction, vertex_count, vertices, &dx1_dxlight, &dh_dx))
841 {
842 return SHADER_EVAL_EMPTY;
843 }
844
845 /* Receiver bsdf eval in shade_surface already contains |n.wo|. */
846 const float dw0_dx1 = fabsf(dot(wo, vertices[0].n)) / sqr(wo_len);
847
848 /* Clamp since it has a tendency to be unstable. */
849 const float G = fminf(dw0_dx1 * dx1_dxlight, 2.f);
850 *throughput *= G;
851
852 /* Specular reflectance. */
853
854 /* Probe ray / isect. */
855 Ray probe_ray;
856 probe_ray.self.light_object = ls->object;
857 probe_ray.self.light_prim = ls->prim;
858 probe_ray.tmin = 0.0f;
859 probe_ray.dP = differential_make_compact(sd->dP);
860 probe_ray.dD = differential_zero_compact();
861 probe_ray.time = sd->time;
862 Intersection probe_isect;
863
864 probe_ray.self.object = sd->object;
865 probe_ray.self.prim = sd->prim;
866 probe_ray.P = sd->P;
867
868 float3 wi;
869 float wi_len;
870 for (int vi = 0; vi < vertex_count; vi++) {
871 const ccl_private ManifoldVertex &v = vertices[vi];
872
873 /* Check visibility. */
874 probe_ray.D = normalize_len(v.p - probe_ray.P, &probe_ray.tmax);
875 if (scene_intersect(kg, &probe_ray, PATH_RAY_VISIBILITY_TRANSMIT, &probe_isect)) {
876 const int hit_object = (probe_isect.object == OBJECT_NONE) ?
877 kernel_data_fetch(prim_object, probe_isect.prim) :
878 probe_isect.object;
879 /* Test whether the ray hit the appropriate object at its intended location. */
880 if (hit_object != v.object || fabsf(probe_ray.tmax - probe_isect.t) > MNEE_MIN_DISTANCE) {
881 return SHADER_EVAL_EMPTY;
882 }
883 }
884 probe_ray.self.object = v.object;
885 probe_ray.self.prim = v.prim;
886 probe_ray.P = v.p;
887
888 /* Set view looking direction. */
889 wi = -wo;
890 wi_len = wo_len;
891
892 /* Setup shader data for vertex vi. */
894 sd_mnee,
895 v.p,
896 v.n,
897 wi,
898 v.shader,
899 v.object,
900 v.prim,
901 v.uv.x,
902 v.uv.y,
903 wi_len,
904 sd->time,
905 false,
906 false);
907
908 /* Set bounce info in case a light path node is used in the refractive interface
909 * shader graph. */
910 INTEGRATOR_STATE_WRITE(state, path, transmission_bounce) = transmission_bounce + vi;
911 INTEGRATOR_STATE_WRITE(state, path, bounce) = bounce + 1 + vi;
912
913 /* Evaluate shader nodes at solution vi. */
915 kg, state, sd_mnee, nullptr, PATH_RAY_VISIBILITY_DIFFUSE, PATH_RAY_FLAG_NONE, true);
916 if (sd_mnee->runtime_flag & SR_CACHE_MISS) {
917 /* Restore original state path bounce info. */
918 INTEGRATOR_STATE_WRITE(state, path, transmission_bounce) = transmission_bounce;
919 INTEGRATOR_STATE_WRITE(state, path, diffuse_bounce) = diffuse_bounce;
920 INTEGRATOR_STATE_WRITE(state, path, bounce) = bounce;
922 }
923
924 /* Set light looking direction. */
925 wo = (vi == vertex_count - 1) ? (light_fixed_direction ? ls->D : ls->P - v.p) :
926 vertices[vi + 1].p - v.p;
927 wo = normalize_len(wo, &wo_len);
928
929 /* Evaluate product term inside eq.6 at solution interface. vi
930 * divided by corresponding sampled pdf:
931 * fr(vi)_do / pdf_dh(vi) x |do/dh| x |n.wo / n.h| */
932 const Spectrum bsdf_contribution = mnee_eval_bsdf_contribution(kg, v.bsdf, wi, wo);
933 *throughput *= bsdf_contribution;
934 }
935
936 /* Restore original state path bounce info. */
937 INTEGRATOR_STATE_WRITE(state, path, transmission_bounce) = transmission_bounce;
938 INTEGRATOR_STATE_WRITE(state, path, diffuse_bounce) = diffuse_bounce;
939 INTEGRATOR_STATE_WRITE(state, path, bounce) = bounce;
940
941 return SHADER_EVAL_OK;
942}
943
944/* Manifold next event estimation path sampling. */
947 ccl_private ShaderData *sd,
948 ccl_private ShaderData *sd_mnee,
949 const ccl_private RNGState *rng_state,
951 ccl_private Spectrum *throughput,
952 ccl_private float3 *r_receiver_wo,
953 ccl_private int &r_vertex_count)
954{
955 /*
956 * 1. send seed ray from shading point to light sample position (or along sampled light
957 * direction), making sure it intersects a caustic caster at least once, ignoring all other
958 * intersections (the final path could be valid even though objects could occlude the light
959 * this seed point), building an array of manifold vertices.
960 */
961
962 /* Setup probe ray. */
963 Ray probe_ray;
964 probe_ray.self.object = sd->object;
965 probe_ray.self.prim = sd->prim;
966 probe_ray.self.light_object = ls->object;
967 probe_ray.self.light_prim = ls->prim;
968 probe_ray.P = sd->P;
969 probe_ray.tmin = 0.0f;
970 if (ls->t == FLT_MAX) {
971 /* Distant light. */
972 probe_ray.D = ls->D;
973 probe_ray.tmax = ls->t;
974 }
975 else {
976 /* Other lights, avoid self-intersection. */
977 probe_ray.D = ls->P - probe_ray.P;
978 probe_ray.D = normalize_len(probe_ray.D, &probe_ray.tmax);
979 }
980 probe_ray.dP = differential_make_compact(sd->dP);
981 probe_ray.dD = differential_zero_compact();
982 probe_ray.time = sd->time;
983 Intersection probe_isect;
984
986
987 int vertex_count = 0;
988 bool has_dispersion = false;
989 for (int isect_count = 0; isect_count < MNEE_MAX_INTERSECTION_COUNT; isect_count++) {
990 const bool hit = scene_intersect(kg, &probe_ray, PATH_RAY_VISIBILITY_TRANSMIT, &probe_isect);
991 if (!hit) {
992 break;
993 }
994
995 const uint object_flags = intersection_get_object_flags(kg, &probe_isect);
996 if (object_flags & SD_OBJECT_CAUSTICS_CASTER) {
997
998 /* Do we have enough slots. */
999 if (vertex_count >= MNEE_MAX_CAUSTIC_CASTERS) {
1000 return SHADER_EVAL_EMPTY;
1001 }
1002
1003 /* Reject caster if it is not a triangles mesh. */
1004 if (!(probe_isect.type & PRIMITIVE_TRIANGLE)) {
1005 return SHADER_EVAL_EMPTY;
1006 }
1007
1008 ccl_private ManifoldVertex &mv = vertices[vertex_count++];
1009
1010 /* Setup shader data on caustic caster and evaluate context. */
1011 shader_setup_from_ray(kg, sd_mnee, &probe_ray, &probe_isect);
1012
1013#ifdef __SPECTRAL__
1014 shader_setup_wavelength(kg, sd_mnee, state);
1015#endif
1016
1017 /* Reject caster if smooth normals are not available: Manifold exploration assumes local
1018 * differential geometry can be created at any point on the surface which is not possible if
1019 * normals are not smooth. */
1020 if (!(sd_mnee->shader & SHADER_SMOOTH_NORMAL)) {
1021 return SHADER_EVAL_EMPTY;
1022 }
1023
1024 /* Last bool argument is the MNEE flag (for TINY_MAX_CLOSURE cap in kernel_shader.h). */
1026 kg, state, sd_mnee, nullptr, PATH_RAY_VISIBILITY_DIFFUSE, PATH_RAY_FLAG_NONE, true);
1027 if (sd_mnee->runtime_flag & SR_CACHE_MISS) {
1029 }
1030
1031#if defined(__KERNEL_ONEAPI__)
1032 /* FIXME: Temporary workaround for a bug in the oneAPI + Embree backend that sometimes sets
1033 * the SR_BSDF_HAS_DISPERSION flag when calling `surface_shader_eval` inside the MNEE code
1034 * path. This happens even in scenes where no material uses dispersion. This workaround
1035 * disables dispersion support + MNEE for all oneAPI backends. Proper fix should be on the
1036 * oneAPI side. */
1037 sd_mnee->runtime_flag &= ~SR_BSDF_HAS_DISPERSION;
1038#endif
1039
1040 /* Query before #mnee_setup_manifold_vertex resets the runtime flag. */
1041 if (sd_mnee->runtime_flag & SR_BSDF_HAS_DISPERSION) {
1042 has_dispersion = true;
1043 }
1044
1045 /* Get and sample refraction bsdf */
1046 bool found_refractive_microfacet_bsdf = false;
1047 for (int ci = 0; ci < sd_mnee->num_closure; ci++) {
1048 ccl_private ShaderClosure *bsdf = &sd_mnee->closure[ci];
1049 if (CLOSURE_IS_REFRACTION(bsdf->type) || CLOSURE_IS_GLASS(bsdf->type)) {
1050 /* Note that Glass closures are treated as refractive further below. */
1051
1052 found_refractive_microfacet_bsdf = true;
1053 ccl_private MicrofacetBsdf *microfacet_bsdf = (ccl_private MicrofacetBsdf *)bsdf;
1054
1055 /* Figure out appropriate index of refraction ratio. */
1056 const float eta = (sd_mnee->runtime_flag & SR_BACKFACING) ? 1.0f / microfacet_bsdf->ior :
1057 microfacet_bsdf->ior;
1058
1059 float2 h = zero_float2();
1060 if (microfacet_bsdf->alpha_x > 0.f && microfacet_bsdf->alpha_y > 0.f) {
1061 /* Sample transmissive microfacet bsdf. */
1062 const float2 bsdf_uv = path_state_rng_2D(kg, rng_state, PRNG_SURFACE_BSDF);
1063 h = mnee_sample_bsdf_dh(bsdf->type,
1064 microfacet_bsdf->alpha_x,
1065 microfacet_bsdf->alpha_y,
1066 bsdf_uv.x,
1067 bsdf_uv.y);
1068 }
1069
1070 /* Setup differential geometry on vertex. */
1071 mnee_setup_manifold_vertex(kg, &mv, bsdf, eta, h, &probe_ray, &probe_isect, sd_mnee);
1072 break;
1073 }
1074 }
1075 if (!found_refractive_microfacet_bsdf) {
1076 return SHADER_EVAL_EMPTY;
1077 }
1078 }
1079
1080 probe_ray.self.object = probe_isect.object;
1081 probe_ray.self.prim = probe_isect.prim;
1082 probe_ray.tmin = intersection_t_offset(probe_isect.t);
1083 };
1084
1085 /* Mark the manifold walk invalid to keep mollification on by default. */
1087
1088 if (vertex_count == 0) {
1089 return SHADER_EVAL_EMPTY;
1090 }
1091
1092 /* Check whether the transmission depth limit is reached before continuing. */
1093 if ((INTEGRATOR_STATE(state, path, transmission_bounce) + vertex_count - 1) >=
1094 kernel_data.integrator.max_transmission_bounce)
1095 {
1096 return SHADER_EVAL_EMPTY;
1097 }
1098
1099 /* Check whether the diffuse depth limit is reached before continuing. */
1100 if ((INTEGRATOR_STATE(state, path, diffuse_bounce) + 1) >=
1101 kernel_data.integrator.max_diffuse_bounce)
1102 {
1103 return SHADER_EVAL_EMPTY;
1104 }
1105
1106 /* Check whether the overall depth limit is reached before continuing. */
1107 if ((INTEGRATOR_STATE(state, path, bounce) + vertex_count) >= kernel_data.integrator.max_bounce)
1108 {
1109 return SHADER_EVAL_EMPTY;
1110 }
1111
1112 /* Mark the manifold walk valid to turn off mollification regardless of how successful the walk
1113 * is: this is noticeable when another mnee is performed deeper in the path, for an internally
1114 * reflected ray for example. If mollification was active for the reflection, a clear
1115 * discontinuity is visible between direct and indirect contributions */
1117
1118 /* Distant light. */
1119 bool light_fixed_direction = (ls->t == FLT_MAX);
1120 if (ls->type == LIGHT_AREA) {
1121 const ccl_global KernelLight *klight = &kernel_data_fetch(lights, ls->prim);
1122 if (klight->area.tan_half_spread == 0.0f) {
1123 /* Area light with zero spread also has fixed direction. */
1124 light_fixed_direction = true;
1125 }
1126 }
1127
1128 /* 2. Walk on the specular manifold to find vertices on the casters that satisfy snell's law for
1129 * each interface. */
1130 if (mnee_newton_solver(kg, sd, sd_mnee, ls, light_fixed_direction, vertex_count, vertices)) {
1131 /* 3. If a solution exists, calculate contribution of the corresponding path */
1133 state,
1134 sd,
1135 sd_mnee,
1136 ls,
1137 light_fixed_direction,
1138 vertex_count,
1139 vertices,
1140 throughput,
1141 r_receiver_wo);
1142
1143 /* TODO: Cache misses are not handled correctly.
1144 * - PATH_MNEE_VALID flag is not handled properly
1145 * - AOVs and other passes have already been written at this point
1146 * MNEE should be moved into its own kernel to solve this problem. */
1147 if (result != SHADER_EVAL_OK) {
1148 return result;
1149 }
1150
1151#ifdef __SPECTRAL__
1152 if (has_dispersion && !(INTEGRATOR_STATE(state, path, flag) & PATH_RAY_SPECTRAL)) {
1153 *throughput *= dispersion_throughput_weight(kg, sd_mnee->rand_wavelength);
1154 }
1155#endif
1156
1157 r_vertex_count = vertex_count;
1158 return SHADER_EVAL_OK;
1159 }
1160
1161 return SHADER_EVAL_EMPTY;
1162}
1163
#define C
Definition RandGen.cpp:29
struct AtomicSpinLock __attribute__((aligned(32))) AtomicSpinLock
#define U
#define A
ccl_device_inline float bsdf_G(const float alpha2, const float cos_N)
ccl_device_forceinline FresnelCoeff microfacet_fresnel(KernelGlobals kg, const ccl_private MicrofacetBsdf *bsdf, const float cos_theta_i, ccl_private float *r_cos_theta_t)
ccl_device_inline float cos_theta(const float3 w)
ccl_device float sin_phi(const float3 w)
ccl_device_inline float sin_theta(const float3 w)
ccl_device_forceinline uint intersection_get_object_flags(KernelGlobals kg, const ccl_private Intersection *ccl_restrict isect)
ccl_device_forceinline float intersection_t_offset(const float t)
ccl_device_forceinline void light_sample_update(KernelGlobals kg, ccl_private LightSample *ls, const float3 P, const float3 N, const uint32_t path_flag)
#define CLOSURE_IS_GLASS(type)
#define kernel_data
#define M_PI_2_F
#define kernel_data_fetch(name, index)
#define one_spectrum
#define PRIM_NONE
#define OBJECT_NONE
#define ccl_private
#define ccl_device_inline
#define CLOSURE_IS_REFRACTION(type)
#define ccl_global
#define logf(x)
#define CCL_NAMESPACE_END
ccl_device_forceinline float differential_make_compact(const float dD)
ccl_device_forceinline float differential_zero_compact()
ccl_device_inline void triangle_vertices_and_normals(KernelGlobals kg, ccl_private const ShaderData *sd, float3 P[3], float3 N[3])
#define FLT_MAX
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
float distance(float, float) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
float sin_from_cos(float c)
ccl_device_intersect bool scene_intersect(KernelGlobals kg, const ccl_private Ray *ray, const uint visibility, ccl_private Intersection *isect)
const int state
ccl_device_inline void object_normal_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private T *N)
ccl_device_inline void object_position_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private T *P)
ccl_device_inline bool light_sample(KernelGlobals kg, const int lamp, const float2 rand, const float3 P, const float3 N, const int runtime_flags, const uint32_t path_flag, ccl_private LightSample *ls)
ClosureType
@ CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID
@ PATH_MNEE_VALID
@ SR_BACKFACING
@ SR_CACHE_MISS
@ SR_BSDF_HAS_DISPERSION
@ PRIMITIVE_TRIANGLE
ShaderEvalResult
@ SHADER_EVAL_EMPTY
@ SHADER_EVAL_CACHE_MISS
@ SHADER_EVAL_OK
@ PRNG_SURFACE_BSDF
@ PATH_RAY_FLAG_NONE
@ PATH_RAY_SPECTRAL
@ PATH_RAY_VISIBILITY_TRANSMIT
@ PATH_RAY_VISIBILITY_DIFFUSE
@ SD_OBJECT_NEGATIVE_SCALE
@ SD_OBJECT_TRANSFORM_APPLIED
@ SD_OBJECT_CAUSTICS_CASTER
@ SHADER_SMOOTH_NORMAL
@ LIGHT_AREA
ccl_device_inline float safe_sqrtf(const float f)
Definition math_base.h:571
ccl_device_inline float beta(const float x, const float y)
Definition math_base.h:708
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device void fast_sincosf(float x, ccl_private float *sine, ccl_private float *cosine)
Definition math_fast.h:144
CCL_NAMESPACE_BEGIN ccl_device_inline float2 zero_float2()
Definition math_float2.h:13
ccl_device_inline float2 normalize_len(const float2 a, ccl_private float *t)
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
CCL_NAMESPACE_BEGIN ccl_device_inline float4 zero_float4()
Definition math_float4.h:13
#define G(x, y, z)
#define H(x, y, z)
#define MNEE_MAX_INTERSECTION_COUNT
Definition mnee.h:46
ccl_device_inline float2 mnee_sample_bsdf_dh(ClosureType type, const float alpha_x, const float alpha_y, const float sample_u, const float sample_v)
Definition mnee.h:576
ccl_device_inline bool mnee_compute_constraint_derivatives(const int vertex_count, ccl_private ManifoldVertex *vertices, const ccl_private float3 &surface_sample_pos, const bool light_fixed_direction, const float3 light_sample)
Definition mnee.h:249
#define MNEE_MAX_ITERATIONS
Definition mnee.h:45
#define MNEE_MIN_PROGRESS_DISTANCE
Definition mnee.h:51
#define MNEE_MIN_DETERMINANT
Definition mnee.h:52
ccl_device_inline float mat22_inverse(const float4 m, ccl_private float4 &m_inverse)
Definition mnee.h:109
#define MNEE_MIN_DISTANCE
Definition mnee.h:50
ccl_device_inline ShaderEvalResult kernel_path_mnee_sample(KernelGlobals kg, IntegratorState state, ccl_private ShaderData *sd, ccl_private ShaderData *sd_mnee, const ccl_private RNGState *rng_state, ccl_private LightSample *ls, ccl_private Spectrum *throughput, ccl_private float3 *r_receiver_wo, ccl_private int &r_vertex_count)
Definition mnee.h:945
ccl_device_inline float mat22_determinant(const float4 m)
Definition mnee.h:103
ccl_device_inline void mnee_setup_manifold_vertex(KernelGlobals kg, ccl_private ManifoldVertex *vtx, ccl_private ShaderClosure *bsdf, const float eta, const float2 n_offset, const ccl_private Ray *ray, const ccl_private Intersection *isect, ccl_private ShaderData *sd_vtx)
Definition mnee.h:120
ccl_device_inline bool mnee_solve_matrix_h_to_x(const int vertex_count, ccl_private ManifoldVertex *vertices, ccl_private float2 *dx)
Definition mnee.h:368
#define MNEE_PROJECTION_DISTANCE_MULTIPLIER
Definition mnee.h:53
#define MNEE_MAX_CAUSTIC_CASTERS
Definition mnee.h:49
ccl_device_inline float2 mat22_mult(const float4 a, const float2 b)
Definition mnee.h:90
ccl_device_inline ShaderEvalResult mnee_path_contribution(KernelGlobals kg, IntegratorState state, ccl_private ShaderData *sd, ccl_private ShaderData *sd_mnee, ccl_private LightSample *ls, const bool light_fixed_direction, const int vertex_count, ccl_private ManifoldVertex *vertices, ccl_private Spectrum *throughput, ccl_private float3 *r_receiver_wo)
Definition mnee.h:789
ccl_device_inline bool mnee_newton_solver(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private ShaderData *sd_vtx, const ccl_private LightSample *ls, const bool light_fixed_direction, const int vertex_count, ccl_private ManifoldVertex *vertices)
Definition mnee.h:403
ccl_device_inline Spectrum mnee_eval_bsdf_contribution(KernelGlobals kg, ccl_private ShaderClosure *closure, const float3 wi, const float3 wo)
Definition mnee.h:621
#define MNEE_MINIMUM_STEP_SIZE
Definition mnee.h:48
#define MNEE_SOLVER_THRESHOLD
Definition mnee.h:47
ccl_device_inline bool mnee_compute_transfer_matrix(const ccl_private ShaderData *sd, const ccl_private LightSample *ls, const bool light_fixed_direction, const int vertex_count, ccl_private ManifoldVertex *vertices, ccl_private float *dx1_dxlight, ccl_private float *dh_dx)
Definition mnee.h:661
ccl_device_inline void motion_triangle_vertices_and_normals(KernelGlobals kg, const ccl_private ShaderData *sd, float3 verts[3], float3 normals[3])
ccl_device_inline float2 path_state_rng_2D(KernelGlobals kg, const ccl_private RNGState *rng_state, const int dimension)
Definition path_state.h:369
#define sqr
#define fabsf
#define M_2PI_F
#define atanf
#define fmaxf
#define make_float2
#define tanf
#define make_float4
#define fminf
#define M_PI_F
ccl_device_inline void shader_setup_from_sample(KernelGlobals kg, ccl_private ShaderData *ccl_restrict sd, const float3 P, const float3 Ng, const float3 I, const int shader, const int object, const int prim, const float u, const float v, const float t, const float time, const bool object_space, const bool is_lamp)
ccl_device_inline void shader_setup_from_ray(KernelGlobals kg, ccl_private ShaderData *ccl_restrict sd, const ccl_private Ray *ccl_restrict ray, const ccl_private Intersection *ccl_restrict isect)
Definition shader_data.h:48
CCL_NAMESPACE_BEGIN ccl_device void shader_setup_object_transforms(KernelGlobals kg, ccl_private ShaderData *ccl_restrict sd, const float time)
Definition shader_data.h:27
#define min(a, b)
Definition sort.cc:38
#define INTEGRATOR_STATE_WRITE(state, nested_struct, member)
Definition state.h:235
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition state.h:234
IntegratorStateCPU * IntegratorState
Definition state.h:227
Spectrum transmittance
float3 dp_dv
Definition mnee.h:63
float4 c
Definition mnee.h:86
int shader
Definition mnee.h:75
float2 n_offset
Definition mnee.h:80
float3 ng
Definition mnee.h:67
int object
Definition mnee.h:73
float3 dn_du
Definition mnee.h:68
float3 n
Definition mnee.h:66
float2 uv
Definition mnee.h:72
ccl_private ShaderClosure * bsdf
Definition mnee.h:79
float3 dn_dv
Definition mnee.h:69
float2 constraint
Definition mnee.h:83
float3 p
Definition mnee.h:61
float3 dp_du
Definition mnee.h:62
float eta
Definition mnee.h:78
float4 b
Definition mnee.h:85
float4 a
Definition mnee.h:84
float tmax
float tmin
float3 P
float time
RaySelfPrimitives self
float3 D
float x
float y
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
ccl_device void surface_shader_eval(KernelGlobals kg, ConstIntegratorGenericState state, ccl_private ShaderData *ccl_restrict sd, ccl_global float *ccl_restrict buffer, const PathRayVisibility path_visibility, const uint32_t path_flag, bool use_caustics_storage=false)
ccl_device_inline float3 triangle_point_from_uv_and_verts(KernelGlobals kg, ccl_private ShaderData *sd, const float u, const float v, const float3 verts[3])
float3 Spectrum