Mesa (master): mesa: texparam: Add a clamping macro to handle out-of-range floats returned as integers.

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Apr 16 02:13:26 UTC 2021


Module: Mesa
Branch: master
Commit: 55fb9417a6a3fd5908a459b94de5f38b6e3a14ba
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=55fb9417a6a3fd5908a459b94de5f38b6e3a14ba

Author: Alexander Shi <alshi at microsoft.com>
Date:   Tue Apr 13 19:22:57 2021 -0700

mesa: texparam: Add a clamping macro to handle out-of-range floats returned as integers.

The parameters GL_TEXTURE_MIN_LOD, GL_TEXTURE_MAX_LOD,
GL_TEXTURE_MAX_ANISOTROPY_EXT, GL_TEXTURE_LOD_BIAS are stored as floats but
returned as integers. Setting their values outside of the integer range results
has undefined behaviour when the c-runtime method lroundf converts the value
back to an integer.

Fixes: 53c36dfc('replace IROUND with util functions')
Reviewed-by: Jesse Natalie <jenatali at microsoft.com>
Reviewed-by: Jason Ekstrand <jason at jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/10244>

---

 src/mesa/main/texparam.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/mesa/main/texparam.c b/src/mesa/main/texparam.c
index 89ad8abe3e3..f4f61f52883 100644
--- a/src/mesa/main/texparam.c
+++ b/src/mesa/main/texparam.c
@@ -47,6 +47,10 @@
 #include "program/prog_instruction.h"
 #include "util/u_math.h"
 
+/**
+ * Use macro to resolve undefined clamping behaviour when using lroundf
+ */
+#define LCLAMPF(a, lmin, lmax) ((a) > (lmin) ? ( (a) >= (lmax) ? (lmax) : (lroundf(a)) ) : (lmin))
 
 /**
  * Check if a coordinate wrap mode is supported for the texture target.
@@ -2481,7 +2485,7 @@ get_tex_parameteriv(struct gl_context *ctx,
           *   it cannot be represented by the returned data type, then the
           *   nearest value representable using that type is returned.
           */
-         *params = CLAMP(lroundf(obj->Sampler.Attrib.MinLod), INT_MIN, INT_MAX);
+         *params = LCLAMPF(obj->Sampler.Attrib.MinLod, INT_MIN, INT_MAX);
          break;
       case GL_TEXTURE_MAX_LOD:
          if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
@@ -2496,7 +2500,7 @@ get_tex_parameteriv(struct gl_context *ctx,
           *   it cannot be represented by the returned data type, then the
           *   nearest value representable using that type is returned.
           */
-         *params = CLAMP(lroundf(obj->Sampler.Attrib.MaxLod), INT_MIN, INT_MAX);
+         *params = LCLAMPF(obj->Sampler.Attrib.MaxLod, INT_MIN, INT_MAX);
          break;
       case GL_TEXTURE_BASE_LEVEL:
          if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
@@ -2520,7 +2524,7 @@ get_tex_parameteriv(struct gl_context *ctx,
           *   it cannot be represented by the returned data type, then the
           *   nearest value representable using that type is returned.
           */
-         *params = CLAMP(lroundf(obj->Sampler.Attrib.MaxAnisotropy), INT_MIN, INT_MAX);
+         *params = LCLAMPF(obj->Sampler.Attrib.MaxAnisotropy, INT_MIN, INT_MAX);
          break;
       case GL_GENERATE_MIPMAP_SGIS:
          if (ctx->API != API_OPENGL_COMPAT && ctx->API != API_OPENGLES)
@@ -2565,7 +2569,7 @@ get_tex_parameteriv(struct gl_context *ctx,
           *   it cannot be represented by the returned data type, then the
           *   nearest value representable using that type is returned.
           */
-         *params = CLAMP(lroundf(obj->Sampler.Attrib.LodBias), INT_MIN, INT_MAX);
+         *params = LCLAMPF(obj->Sampler.Attrib.LodBias, INT_MIN, INT_MAX);
          break;
       case GL_TEXTURE_CROP_RECT_OES:
          if (ctx->API != API_OPENGLES || !ctx->Extensions.OES_draw_texture)



More information about the mesa-commit mailing list