Skip to content

Commit 48398b2

Browse files
brechtvlGitea
authored andcommitted
Cleanup: Fix various divisions by zero reported by ASAN
Pull Request: https://projects.blender.org/blender/blender/pulls/135326
1 parent f6eb81e commit 48398b2

File tree

9 files changed

+23
-20
lines changed

9 files changed

+23
-20
lines changed

intern/cycles/bvh/binning.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ BVHObjectBinning::BVHObjectBinning(const BVHRange &job,
7777

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

8282
/* initialize binning counter and bounds */
8383
BoundBox bin_bounds[MAX_BINS][4]; /* bounds for every bin in every dimension */

intern/cycles/kernel/closure/bsdf_microfacet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ ccl_device int bsdf_microfacet_sample(KernelGlobals kg,
677677
}
678678

679679
const float m_eta = bsdf->ior;
680-
const float m_inv_eta = 1.0f / bsdf->ior;
680+
const float m_inv_eta = safe_divide(1.0f, bsdf->ior);
681681
const float alpha_x = bsdf->alpha_x;
682682
const float alpha_y = bsdf->alpha_y;
683683
bool m_singular = !bsdf_microfacet_eval_flag(bsdf);

intern/cycles/kernel/geom/shader_data.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ ccl_device void shader_setup_from_displace(KernelGlobals kg,
274274
0.5f,
275275
!(kernel_data_fetch(object_flag, object) & SD_OBJECT_TRANSFORM_APPLIED),
276276
false);
277+
278+
/* Assign some incoming direction to avoid division by zero. */
279+
sd->wi = sd->N;
277280
}
278281

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

323326
/* Interpolate position and tangent. */
324-
sd->P = make_float3(catmull_rom_basis_derivative(P_curve, sd->u));
327+
sd->P = make_float3(catmull_rom_basis_eval(P_curve, sd->u));
325328
# ifdef __DPDU__
326329
sd->dPdu = make_float3(catmull_rom_basis_derivative(P_curve, sd->u));
327330
# endif
@@ -334,12 +337,12 @@ ccl_device void shader_setup_from_curve(KernelGlobals kg,
334337
# endif
335338
}
336339

337-
/* No view direction, normals or bitangent. */
338-
sd->wi = zero_float3();
339-
sd->N = zero_float3();
340-
sd->Ng = zero_float3();
340+
/* Pick arbitrary view direction, normals and bitangent to avoid NaNs elsewhere. */
341+
sd->wi = normalize(cross(make_float3(0, 1, 0), sd->dPdu));
342+
sd->N = sd->wi;
343+
sd->Ng = sd->wi;
341344
# ifdef __DPDU__
342-
sd->dPdv = zero_float3();
345+
sd->dPdv = cross(sd->dPdu, sd->Ng);
343346
# endif
344347

345348
/* No ray differentials currently. */

intern/cycles/kernel/integrator/intersect_volume_stack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ ccl_device void integrator_volume_stack_update_for_subsurface(KernelGlobals kg,
2828

2929
Ray volume_ray ccl_optional_struct_init;
3030
volume_ray.P = from_P;
31-
volume_ray.D = normalize_len(to_P - from_P, &volume_ray.tmax);
31+
volume_ray.D = safe_normalize_len(to_P - from_P, &volume_ray.tmax);
3232
volume_ray.tmin = 0.0f;
3333
volume_ray.self.object = INTEGRATOR_STATE(state, isect, object);
3434
volume_ray.self.prim = INTEGRATOR_STATE(state, isect, prim);

intern/cycles/kernel/light/area.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ ccl_device_inline float area_light_rect_sample(const float3 P,
6767
* the extra +pi that would remain in the expression for au can be removed by flipping the sign
6868
* of cos(au) and sin(au), which also cancels if we flip the sign of b1 in the fu term. */
6969
const float au = rand.x * S + g2 + g3;
70-
const float fu = (cosf(au) * b0 + b1) / sinf(au);
70+
const float fu = safe_divide(cosf(au) * b0 + b1, sinf(au));
7171
float cu = copysignf(1.0f / sqrtf(fu * fu + b0sq), fu);
7272
cu = clamp(cu, -1.0f, 1.0f);
7373
/* Compute xu. */
@@ -96,7 +96,7 @@ ccl_device_inline float area_light_rect_sample(const float3 P,
9696
* needed for the case where the light is viewed from grazing angles, see e.g. #98930.
9797
*/
9898
const float t = len(dir);
99-
return -t * t * t / (z0 * len_u * len_v);
99+
return safe_divide(-t * t * t, (z0 * len_u * len_v));
100100
}
101101
return 1.0f / S;
102102
}
@@ -306,7 +306,7 @@ ccl_device_inline bool area_light_eval(const ccl_global KernelLight *klight,
306306

307307
if (sample_coord) {
308308
*light_P = light_P_new;
309-
ls->D = normalize_len(*light_P - ray_P, &ls->t);
309+
ls->D = safe_normalize_len(*light_P - ray_P, &ls->t);
310310
}
311311

312312
/* Convert radiant flux to radiance. */
@@ -384,7 +384,7 @@ ccl_device_forceinline void area_light_mnee_sample_update(const ccl_global Kerne
384384
area_light_eval<false>(klight, P, &ls->P, ls, zero_float2(), true);
385385
}
386386
else {
387-
ls->D = normalize_len(ls->P - P, &ls->t);
387+
ls->D = safe_normalize_len(ls->P - P, &ls->t);
388388
area_light_eval<false>(klight, P, &ls->P, ls, zero_float2(), false);
389389
/* Convert pdf to be in area measure. */
390390
ls->pdf /= light_pdf_area_to_solid_angle(ls->Ng, -ls->D, ls->t);

intern/cycles/kernel/light/point.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ ccl_device_inline bool point_light_sample(KernelGlobals kg,
6666
ls->P += disk_light_sample(lightN, rand) * klight->spot.radius;
6767
}
6868

69-
ls->D = normalize_len(ls->P - P, &ls->t);
69+
ls->D = safe_normalize_len(ls->P - P, &ls->t);
7070
ls->Ng = -ls->D;
7171

7272
/* PDF. */
@@ -102,7 +102,7 @@ ccl_device_forceinline void point_light_mnee_sample_update(KernelGlobals kg,
102102
const float3 N,
103103
const uint32_t path_flag)
104104
{
105-
ls->D = normalize_len(ls->P - P, &ls->t);
105+
ls->D = safe_normalize_len(ls->P - P, &ls->t);
106106

107107
const float radius = klight->spot.radius;
108108

intern/cycles/kernel/light/sample.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ ccl_device_inline void shadow_ray_setup(const ccl_private ShaderData *ccl_restri
233233
else {
234234
/* other lights, avoid self-intersection */
235235
ray->D = ls->P - P;
236-
ray->D = normalize_len(ray->D, &ray->tmax);
236+
ray->D = safe_normalize_len(ray->D, &ray->tmax);
237237
}
238238
}
239239
else {

intern/cycles/kernel/light/spot.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ ccl_device_inline bool spot_light_sample(KernelGlobals kg,
133133
ls->P += disk_light_sample(lightN, rand) * klight->spot.radius;
134134
}
135135

136-
ls->D = normalize_len(ls->P - P, &ls->t);
136+
ls->D = safe_normalize_len(ls->P - P, &ls->t);
137137
ls->Ng = -ls->D;
138138

139139
/* Attenuation. */
@@ -177,7 +177,7 @@ ccl_device_forceinline void spot_light_mnee_sample_update(KernelGlobals kg,
177177
const float3 N,
178178
const uint32_t path_flag)
179179
{
180-
ls->D = normalize_len(ls->P - P, &ls->t);
180+
ls->D = safe_normalize_len(ls->P - P, &ls->t);
181181

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

intern/cycles/scene/camera.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,11 @@ void Camera::update(Scene *scene)
312312

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

319-
v = transform_perspective(&full_rastertocamera, make_float3(0.0f, 0.0f, 1.0f));
319+
v = transform_perspective(&full_rastertocamera, make_float3(0.0f, 0.0f, 0.0f));
320320
frustum_left_normal = normalize(make_float3(-v.z, 0.0f, v.x));
321321
frustum_bottom_normal = normalize(make_float3(0.0f, -v.z, v.y));
322322
}

0 commit comments

Comments
 (0)