Skip to content

Commit

Permalink
Cleanup: Fix various divisions by zero reported by ASAN
Browse files Browse the repository at this point in the history
  • Loading branch information
brechtvl authored and Gitea committed Mar 6, 2025
1 parent f6eb81e commit 48398b2
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion intern/cycles/bvh/binning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ BVHObjectBinning::BVHObjectBinning(const BVHRange &job,

/* compute number of bins to use and precompute scaling factor for binning */
num_bins = min(size_t(MAX_BINS), size_t(4.0f + 0.05f * size()));
scale = reciprocal(cent_bounds_.size()) * make_float3((float)num_bins);
scale = safe_divide(cent_bounds_.size(), make_float3((float)num_bins));

/* initialize binning counter and bounds */
BoundBox bin_bounds[MAX_BINS][4]; /* bounds for every bin in every dimension */
Expand Down
2 changes: 1 addition & 1 deletion intern/cycles/kernel/closure/bsdf_microfacet.h
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ ccl_device int bsdf_microfacet_sample(KernelGlobals kg,
}

const float m_eta = bsdf->ior;
const float m_inv_eta = 1.0f / bsdf->ior;
const float m_inv_eta = safe_divide(1.0f, bsdf->ior);
const float alpha_x = bsdf->alpha_x;
const float alpha_y = bsdf->alpha_y;
bool m_singular = !bsdf_microfacet_eval_flag(bsdf);
Expand Down
15 changes: 9 additions & 6 deletions intern/cycles/kernel/geom/shader_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ ccl_device void shader_setup_from_displace(KernelGlobals kg,
0.5f,
!(kernel_data_fetch(object_flag, object) & SD_OBJECT_TRANSFORM_APPLIED),
false);

/* Assign some incoming direction to avoid division by zero. */
sd->wi = sd->N;
}

/* ShaderData setup for point on curve. */
Expand Down Expand Up @@ -321,7 +324,7 @@ ccl_device void shader_setup_from_curve(KernelGlobals kg,
P_curve[3] = kernel_data_fetch(curve_keys, kb);

/* Interpolate position and tangent. */
sd->P = make_float3(catmull_rom_basis_derivative(P_curve, sd->u));
sd->P = make_float3(catmull_rom_basis_eval(P_curve, sd->u));
# ifdef __DPDU__
sd->dPdu = make_float3(catmull_rom_basis_derivative(P_curve, sd->u));
# endif
Expand All @@ -334,12 +337,12 @@ ccl_device void shader_setup_from_curve(KernelGlobals kg,
# endif
}

/* No view direction, normals or bitangent. */
sd->wi = zero_float3();
sd->N = zero_float3();
sd->Ng = zero_float3();
/* Pick arbitrary view direction, normals and bitangent to avoid NaNs elsewhere. */
sd->wi = normalize(cross(make_float3(0, 1, 0), sd->dPdu));
sd->N = sd->wi;
sd->Ng = sd->wi;
# ifdef __DPDU__
sd->dPdv = zero_float3();
sd->dPdv = cross(sd->dPdu, sd->Ng);
# endif

/* No ray differentials currently. */
Expand Down
2 changes: 1 addition & 1 deletion intern/cycles/kernel/integrator/intersect_volume_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ccl_device void integrator_volume_stack_update_for_subsurface(KernelGlobals kg,

Ray volume_ray ccl_optional_struct_init;
volume_ray.P = from_P;
volume_ray.D = normalize_len(to_P - from_P, &volume_ray.tmax);
volume_ray.D = safe_normalize_len(to_P - from_P, &volume_ray.tmax);
volume_ray.tmin = 0.0f;
volume_ray.self.object = INTEGRATOR_STATE(state, isect, object);
volume_ray.self.prim = INTEGRATOR_STATE(state, isect, prim);
Expand Down
8 changes: 4 additions & 4 deletions intern/cycles/kernel/light/area.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ ccl_device_inline float area_light_rect_sample(const float3 P,
* the extra +pi that would remain in the expression for au can be removed by flipping the sign
* of cos(au) and sin(au), which also cancels if we flip the sign of b1 in the fu term. */
const float au = rand.x * S + g2 + g3;
const float fu = (cosf(au) * b0 + b1) / sinf(au);
const float fu = safe_divide(cosf(au) * b0 + b1, sinf(au));
float cu = copysignf(1.0f / sqrtf(fu * fu + b0sq), fu);
cu = clamp(cu, -1.0f, 1.0f);
/* Compute xu. */
Expand Down Expand Up @@ -96,7 +96,7 @@ ccl_device_inline float area_light_rect_sample(const float3 P,
* needed for the case where the light is viewed from grazing angles, see e.g. #98930.
*/
const float t = len(dir);
return -t * t * t / (z0 * len_u * len_v);
return safe_divide(-t * t * t, (z0 * len_u * len_v));
}
return 1.0f / S;
}
Expand Down Expand Up @@ -306,7 +306,7 @@ ccl_device_inline bool area_light_eval(const ccl_global KernelLight *klight,

if (sample_coord) {
*light_P = light_P_new;
ls->D = normalize_len(*light_P - ray_P, &ls->t);
ls->D = safe_normalize_len(*light_P - ray_P, &ls->t);
}

/* Convert radiant flux to radiance. */
Expand Down Expand Up @@ -384,7 +384,7 @@ ccl_device_forceinline void area_light_mnee_sample_update(const ccl_global Kerne
area_light_eval<false>(klight, P, &ls->P, ls, zero_float2(), true);
}
else {
ls->D = normalize_len(ls->P - P, &ls->t);
ls->D = safe_normalize_len(ls->P - P, &ls->t);
area_light_eval<false>(klight, P, &ls->P, ls, zero_float2(), false);
/* Convert pdf to be in area measure. */
ls->pdf /= light_pdf_area_to_solid_angle(ls->Ng, -ls->D, ls->t);
Expand Down
4 changes: 2 additions & 2 deletions intern/cycles/kernel/light/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ ccl_device_inline bool point_light_sample(KernelGlobals kg,
ls->P += disk_light_sample(lightN, rand) * klight->spot.radius;
}

ls->D = normalize_len(ls->P - P, &ls->t);
ls->D = safe_normalize_len(ls->P - P, &ls->t);
ls->Ng = -ls->D;

/* PDF. */
Expand Down Expand Up @@ -102,7 +102,7 @@ ccl_device_forceinline void point_light_mnee_sample_update(KernelGlobals kg,
const float3 N,
const uint32_t path_flag)
{
ls->D = normalize_len(ls->P - P, &ls->t);
ls->D = safe_normalize_len(ls->P - P, &ls->t);

const float radius = klight->spot.radius;

Expand Down
2 changes: 1 addition & 1 deletion intern/cycles/kernel/light/sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ ccl_device_inline void shadow_ray_setup(const ccl_private ShaderData *ccl_restri
else {
/* other lights, avoid self-intersection */
ray->D = ls->P - P;
ray->D = normalize_len(ray->D, &ray->tmax);
ray->D = safe_normalize_len(ray->D, &ray->tmax);
}
}
else {
Expand Down
4 changes: 2 additions & 2 deletions intern/cycles/kernel/light/spot.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ ccl_device_inline bool spot_light_sample(KernelGlobals kg,
ls->P += disk_light_sample(lightN, rand) * klight->spot.radius;
}

ls->D = normalize_len(ls->P - P, &ls->t);
ls->D = safe_normalize_len(ls->P - P, &ls->t);
ls->Ng = -ls->D;

/* Attenuation. */
Expand Down Expand Up @@ -177,7 +177,7 @@ ccl_device_forceinline void spot_light_mnee_sample_update(KernelGlobals kg,
const float3 N,
const uint32_t path_flag)
{
ls->D = normalize_len(ls->P - P, &ls->t);
ls->D = safe_normalize_len(ls->P - P, &ls->t);

ls->eval_fac = klight->spot.eval_fac;

Expand Down
4 changes: 2 additions & 2 deletions intern/cycles/scene/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,11 @@ void Camera::update(Scene *scene)

if (camera_type == CAMERA_PERSPECTIVE) {
float3 v = transform_perspective(&full_rastertocamera,
make_float3(full_width, full_height, 1.0f));
make_float3(full_width, full_height, 0.0f));
frustum_right_normal = normalize(make_float3(v.z, 0.0f, -v.x));
frustum_top_normal = normalize(make_float3(0.0f, v.z, -v.y));

v = transform_perspective(&full_rastertocamera, make_float3(0.0f, 0.0f, 1.0f));
v = transform_perspective(&full_rastertocamera, make_float3(0.0f, 0.0f, 0.0f));
frustum_left_normal = normalize(make_float3(-v.z, 0.0f, v.x));
frustum_bottom_normal = normalize(make_float3(0.0f, -v.z, v.y));
}
Expand Down

0 comments on commit 48398b2

Please sign in to comment.