[Bf-blender-cvs] [d74bb7be191] blender-v3.1-release: Fix size_t -> int -> size_t round trip in Cycles

Sergey Sharybin noreply at git.blender.org
Wed Feb 9 14:45:46 CET 2022


Commit: d74bb7be1916744ae56347b49333eac22ebb7339
Author: Sergey Sharybin
Date:   Wed Feb 2 11:17:13 2022 +0100
Branches: blender-v3.1-release
https://developer.blender.org/rBd74bb7be1916744ae56347b49333eac22ebb7339

Fix size_t -> int -> size_t round trip in Cycles

There are two things achieved by this change:

- No possible downcast of size_t to int when calculating motion steps.
- Disambiguate call to min() which was for some reason considered
  ambiguous on 32bit platforms `min(int, unsigned int)`.

On an implementation side the `min()` is defined for a fixed width
integer type to disambiguate uint from size_t on 32bit platforms,
and yet be able to use it for 32bit operands on 64bit platforms without
upcast.

Fixes 32bit platforms (such as i386) in Debian package build system.

Differential Revision: https://developer.blender.org/D13992

===================================================================

M	intern/cycles/bvh/embree.cpp
M	intern/cycles/scene/hair.cpp
M	intern/cycles/scene/mesh.cpp
M	intern/cycles/scene/pointcloud.cpp
M	intern/cycles/util/math.h

===================================================================

diff --git a/intern/cycles/bvh/embree.cpp b/intern/cycles/bvh/embree.cpp
index 616b6273e6a..731fef52063 100644
--- a/intern/cycles/bvh/embree.cpp
+++ b/intern/cycles/bvh/embree.cpp
@@ -471,7 +471,7 @@ void BVHEmbree::add_instance(Object *ob, int i)
   assert(instance_bvh != NULL);
 
   const size_t num_object_motion_steps = ob->use_motion() ? ob->get_motion().size() : 1;
-  const size_t num_motion_steps = min(num_object_motion_steps, RTC_MAX_TIME_STEP_COUNT);
+  const size_t num_motion_steps = min(num_object_motion_steps, (size_t)RTC_MAX_TIME_STEP_COUNT);
   assert(num_object_motion_steps <= RTC_MAX_TIME_STEP_COUNT);
 
   RTCGeometry geom_id = rtcNewGeometry(rtc_device, RTC_GEOMETRY_TYPE_INSTANCE);
@@ -522,7 +522,7 @@ void BVHEmbree::add_triangles(const Object *ob, const Mesh *mesh, int i)
   }
 
   assert(num_motion_steps <= RTC_MAX_TIME_STEP_COUNT);
-  num_motion_steps = min(num_motion_steps, RTC_MAX_TIME_STEP_COUNT);
+  num_motion_steps = min(num_motion_steps, (size_t)RTC_MAX_TIME_STEP_COUNT);
 
   const size_t num_triangles = mesh->num_triangles();
 
@@ -775,7 +775,7 @@ void BVHEmbree::add_curves(const Object *ob, const Hair *hair, int i)
   }
 
   assert(num_motion_steps <= RTC_MAX_TIME_STEP_COUNT);
-  num_motion_steps = min(num_motion_steps, RTC_MAX_TIME_STEP_COUNT);
+  num_motion_steps = min(num_motion_steps, (size_t)RTC_MAX_TIME_STEP_COUNT);
 
   const size_t num_curves = hair->num_curves();
   size_t num_segments = 0;
diff --git a/intern/cycles/scene/hair.cpp b/intern/cycles/scene/hair.cpp
index 2951a609ae9..0fd9c70cc26 100644
--- a/intern/cycles/scene/hair.cpp
+++ b/intern/cycles/scene/hair.cpp
@@ -119,7 +119,7 @@ void Hair::Curve::motion_keys(const float3 *curve_keys,
 {
   /* Figure out which steps we need to fetch and their interpolation factor. */
   const size_t max_step = num_steps - 1;
-  const size_t step = min((int)(time * max_step), max_step - 1);
+  const size_t step = std::min((size_t)(time * max_step), max_step - 1);
   const float t = time * max_step - step;
   /* Fetch vertex coordinates. */
   float4 curr_keys[2];
@@ -147,7 +147,7 @@ void Hair::Curve::cardinal_motion_keys(const float3 *curve_keys,
 {
   /* Figure out which steps we need to fetch and their interpolation factor. */
   const size_t max_step = num_steps - 1;
-  const size_t step = min((int)(time * max_step), max_step - 1);
+  const size_t step = min((size_t)(time * max_step), max_step - 1);
   const float t = time * max_step - step;
   /* Fetch vertex coordinates. */
   float4 curr_keys[4];
@@ -192,7 +192,7 @@ void Hair::Curve::keys_for_step(const float3 *curve_keys,
                                 float4 r_keys[2]) const
 {
   k0 = max(k0, 0);
-  k1 = min(k1, num_keys - 1);
+  k1 = min(k1, (size_t)(num_keys - 1));
   const size_t center_step = ((num_steps - 1) / 2);
   if (step == center_step) {
     /* Center step: regular key location. */
@@ -238,7 +238,7 @@ void Hair::Curve::cardinal_keys_for_step(const float3 *curve_keys,
                                          float4 r_keys[4]) const
 {
   k0 = max(k0, 0);
-  k3 = min(k3, num_keys - 1);
+  k3 = min(k3, (size_t)(num_keys - 1));
   const size_t center_step = ((num_steps - 1) / 2);
   if (step == center_step) {
     /* Center step: regular key location. */
diff --git a/intern/cycles/scene/mesh.cpp b/intern/cycles/scene/mesh.cpp
index c381d7a54ff..f53dca88ee0 100644
--- a/intern/cycles/scene/mesh.cpp
+++ b/intern/cycles/scene/mesh.cpp
@@ -53,7 +53,7 @@ void Mesh::Triangle::motion_verts(const float3 *verts,
 {
   /* Figure out which steps we need to fetch and their interpolation factor. */
   const size_t max_step = num_steps - 1;
-  const size_t step = min((int)(time * max_step), max_step - 1);
+  const size_t step = min((size_t)(time * max_step), max_step - 1);
   const float t = time * max_step - step;
   /* Fetch vertex coordinates. */
   float3 curr_verts[3];
diff --git a/intern/cycles/scene/pointcloud.cpp b/intern/cycles/scene/pointcloud.cpp
index 4f88fe9db3d..6356a5030f3 100644
--- a/intern/cycles/scene/pointcloud.cpp
+++ b/intern/cycles/scene/pointcloud.cpp
@@ -55,7 +55,7 @@ float4 PointCloud::Point::motion_key(const float3 *points,
   /* Figure out which steps we need to fetch and their
    * interpolation factor. */
   const size_t max_step = num_steps - 1;
-  const size_t step = min((int)(time * max_step), max_step - 1);
+  const size_t step = min((size_t)(time * max_step), max_step - 1);
   const float t = time * max_step - step;
   /* Fetch vertex coordinates. */
   const float4 curr_key = point_for_step(
diff --git a/intern/cycles/util/math.h b/intern/cycles/util/math.h
index 605a19aaef0..a580a21c28b 100644
--- a/intern/cycles/util/math.h
+++ b/intern/cycles/util/math.h
@@ -124,7 +124,12 @@ ccl_device_inline int min(int a, int b)
   return (a < b) ? a : b;
 }
 
-ccl_device_inline uint min(uint a, uint b)
+ccl_device_inline uint32_t min(uint32_t a, uint32_t b)
+{
+  return (a < b) ? a : b;
+}
+
+ccl_device_inline uint64_t min(uint64_t a, uint64_t b)
 {
   return (a < b) ? a : b;
 }



More information about the Bf-blender-cvs mailing list