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
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);
105 return m.
x * m.
w - m.
y * m.
z;
132 sd_vtx->type = isect->type;
133 sd_vtx->runtime_flag = 0;
134 sd_vtx->shader_flag = 0;
139 sd_vtx->time = ray->time;
141 sd_vtx->prim = isect->prim;
142 sd_vtx->ray_length = isect->t;
144 sd_vtx->u = isect->u;
145 sd_vtx->v = isect->v;
174 float3 dp_du = verts[1] - verts[0];
175 float3 dp_dv = verts[2] - verts[0];
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,
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);
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;
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;
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;
227 vtx->n_offset = n_offset;
235 vtx->object = sd_vtx->object;
236 vtx->prim = sd_vtx->prim;
237 vtx->shader = sd_vtx->shader;
242#if defined(__KERNEL_METAL__)
252 const bool light_fixed_direction,
255 for (
int vi = 0; vi < vertex_count; vi++) {
259 float3 wi = (vi == 0) ? surface_sample_pos - v.p : vertices[vi - 1].p - v.p;
268 float3 wo = (vi == vertex_count - 1) ?
270 vertices[vi + 1].p - v.p;
280 if (
dot(wi, v.ng) < .0f) {
286 const float ilh = 1.f /
len(
H);
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);
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);
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));
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);
326 dH_du -=
H *
dot(dH_du,
H);
327 dH_dv -=
H *
dot(dH_dv,
H);
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);
341 dot(dH_dv, t) +
dot(
H, dt_dv));
344 if (vi < vertex_count - 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);
376 float4 Lk = vertices[0].b;
381 C[0] = vertices[0].constraint;
383 for (
int k = 1; k < vertex_count; k++) {
386 Lk = vertices[k].b -
mat22_mult(
A, vertices[k - 1].c);
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--) {
407 const bool light_fixed_direction,
408 const int vertex_count,
419 projection_ray.
tmin = 0.0f;
420 projection_ray.
time = sd->time;
429 bool reduce_stepsize =
false;
430 bool resolve_constraint =
true;
432 if (resolve_constraint) {
435 vertex_count, vertices, sd->P, light_fixed_direction,
light_sample))
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));
458 for (
int vi = 0; vi < vertex_count; vi++) {
466 const float3 wo = tentative_p - sd->P;
467 if (
dot(sd->Ng, wo) <= 0.f) {
469 tentative_p =
mv.p +
beta * (dx[vi].
x *
mv.dp_du + dx[vi].
y *
mv.dp_dv);
478 projection_ray.
self.
prim = sd->prim;
479 projection_ray.
P = sd->P;
485 projection_ray.
P = pv.p;
490 bool projection_success =
false;
498 if (projection_isect.
object ==
mv.object) {
499 projection_success =
true;
507 if (!projection_success) {
508 reduce_stepsize =
true;
520 kg, &tv,
mv.bsdf,
mv.eta,
mv.n_offset, &projection_ray, &projection_isect, sd_vtx);
531 if (!reduce_stepsize) {
532 for (
int vi = 0; vi < vertex_count; vi++) {
536 const float3 wi = (vi == 0 ? sd->P : tentative[vi - 1].
p) - tv.p;
538 const float3 wo = (vi == vertex_count - 1) ? light_fixed_direction ? ls->D : ls->P - tv.p :
539 tentative[vi + 1].
p - tv.p;
541 if (
dot(tv.n, wi) *
dot(tv.n, wo) >= 0.f) {
542 reduce_stepsize =
true;
548 if (reduce_stepsize) {
550 reduce_stepsize =
false;
551 resolve_constraint =
false;
563 for (
int vi = 0; vi < vertex_count; vi++) {
564 vertices[vi] = tentative[vi];
568 resolve_constraint =
true;
579 const float sample_u,
580 const float sample_v)
586 if (alpha_x == alpha_y) {
587 const float phi = sample_v *
M_2PI_F;
589 alpha2 = alpha_x * alpha_x;
593 if (sample_v > .5f) {
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);
603 float tan2_theta = alpha2;
605 tan2_theta *= -
logf(1.0f - sample_u);
608 tan2_theta *= sample_u / (1.0f - sample_u);
610 const float cos2_theta = 1.0f / (1.0f + tan2_theta);
628 const float cosNI =
dot(bsdf->N, wi);
629 const float cosNO =
dot(bsdf->N, wo);
632 const float cosHI =
dot(Ht, wi);
634 const float alpha2 = bsdf->alpha_x * bsdf->alpha_y;
635 const float cosThetaM =
dot(bsdf->N, Ht);
663 const bool light_fixed_direction,
664 const int vertex_count,
673 float4 Lk = vertices[0].b;
679 float det_dh_dx = Lk_det;
681 for (
int k = 1; k < vertex_count; k++) {
684 Lk = vertices[k].b -
mat22_mult(vertices[k].a,
U[k - 1]);
696 const int mi = vertex_count - 1;
708 float3 wi = vertex_count == 1 ? sd->P - m.p : vertices[mi - 1].p - m.p;
709 const float ili = 1.f /
len(wi);
714 if (
dot(wi, m.ng) < .0f) {
721 if (light_fixed_direction) {
727 const float ilh = 1.f /
len(
H);
730 const float ilo = -eta * ilh;
734 const float cos_phi =
dot(wo, s);
740 dH_dtheta -=
H *
dot(dH_dtheta,
H);
741 dH_dphi -=
H *
dot(dH_dphi,
H);
746 dot(dH_dtheta, s),
dot(dH_dphi, s),
dot(dH_dtheta, t),
dot(dH_dphi, t));
754 float ilo = 1.f /
len(wo);
759 const float ilh = 1.f /
len(
H);
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);
779 for (
int k = vertex_count - 2; k > -1; k--) {
784 *dh_dx =
fabsf(det_dh_dx);
794 const bool light_fixed_direction,
795 const int vertex_count,
810 kg, ls, vertices[vertex_count - 1].p, vertices[vertex_count - 1].n, path_flag);
834 *throughput *= ls->eval_fac / ls->pdf;
840 sd, ls, light_fixed_direction, vertex_count, vertices, &dx1_dxlight, &dh_dx))
846 const float dw0_dx1 =
fabsf(
dot(wo, vertices[0].n)) /
sqr(wo_len);
849 const float G =
fminf(dw0_dx1 * dx1_dxlight, 2.f);
858 probe_ray.
tmin = 0.0f;
861 probe_ray.
time = sd->time;
870 for (
int vi = 0; vi < vertex_count; vi++) {
925 wo = (vi == vertex_count - 1) ? (light_fixed_direction ? ls->D : ls->P - v.p) :
926 vertices[vi + 1].p - v.p;
933 *throughput *= bsdf_contribution;
969 probe_ray.
tmin = 0.0f;
973 probe_ray.
tmax = ls->t;
977 probe_ray.
D = ls->P - probe_ray.
P;
982 probe_ray.
time = sd->time;
987 int vertex_count = 0;
988 bool has_dispersion =
false;
1014 shader_setup_wavelength(kg, sd_mnee,
state);
1031#if defined(__KERNEL_ONEAPI__)
1042 has_dispersion =
true;
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];
1052 found_refractive_microfacet_bsdf =
true;
1056 const float eta = (sd_mnee->runtime_flag &
SR_BACKFACING) ? 1.0f / microfacet_bsdf->
ior :
1057 microfacet_bsdf->ior;
1060 if (microfacet_bsdf->alpha_x > 0.f && microfacet_bsdf->alpha_y > 0.f) {
1064 microfacet_bsdf->alpha_x,
1065 microfacet_bsdf->alpha_y,
1075 if (!found_refractive_microfacet_bsdf) {
1088 if (vertex_count == 0) {
1119 bool light_fixed_direction = (ls->t ==
FLT_MAX);
1122 if (klight->area.tan_half_spread == 0.0f) {
1124 light_fixed_direction =
true;
1130 if (
mnee_newton_solver(kg, sd, sd_mnee, ls, light_fixed_direction, vertex_count, vertices)) {
1137 light_fixed_direction,
1153 *throughput *= dispersion_throughput_weight(kg, sd_mnee->rand_wavelength);
1157 r_vertex_count = vertex_count;
struct AtomicSpinLock __attribute__((aligned(32))) AtomicSpinLock
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_fetch(name, index)
#define ccl_device_inline
#define CLOSURE_IS_REFRACTION(type)
#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])
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)
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)
@ CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID
@ PATH_RAY_VISIBILITY_TRANSMIT
@ PATH_RAY_VISIBILITY_DIFFUSE
@ SD_OBJECT_NEGATIVE_SCALE
@ SD_OBJECT_TRANSFORM_APPLIED
@ SD_OBJECT_CAUSTICS_CASTER
ccl_device_inline float safe_sqrtf(const float f)
ccl_device_inline float beta(const float x, const float y)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
ccl_device void fast_sincosf(float x, ccl_private float *sine, ccl_private float *cosine)
CCL_NAMESPACE_BEGIN ccl_device_inline float2 zero_float2()
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()
#define MNEE_MAX_INTERSECTION_COUNT
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)
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)
#define MNEE_MAX_ITERATIONS
#define MNEE_MIN_PROGRESS_DISTANCE
#define MNEE_MIN_DETERMINANT
ccl_device_inline float mat22_inverse(const float4 m, ccl_private float4 &m_inverse)
#define MNEE_MIN_DISTANCE
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)
ccl_device_inline float mat22_determinant(const float4 m)
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)
ccl_device_inline bool mnee_solve_matrix_h_to_x(const int vertex_count, ccl_private ManifoldVertex *vertices, ccl_private float2 *dx)
#define MNEE_PROJECTION_DISTANCE_MULTIPLIER
#define MNEE_MAX_CAUSTIC_CASTERS
ccl_device_inline float2 mat22_mult(const float4 a, const float2 b)
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)
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)
ccl_device_inline Spectrum mnee_eval_bsdf_contribution(KernelGlobals kg, ccl_private ShaderClosure *closure, const float3 wi, const float3 wo)
#define MNEE_MINIMUM_STEP_SIZE
#define MNEE_SOLVER_THRESHOLD
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)
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)
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)
CCL_NAMESPACE_BEGIN ccl_device void shader_setup_object_transforms(KernelGlobals kg, ccl_private ShaderData *ccl_restrict sd, const float time)
#define INTEGRATOR_STATE_WRITE(state, nested_struct, member)
#define INTEGRATOR_STATE(state, nested_struct, member)
IntegratorStateCPU * IntegratorState
ccl_private ShaderClosure * bsdf
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])