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@microsoft.com>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/10244>
(cherry picked from commit 55fb9417a6)
This commit is contained in:
Alexander Shi 2021-04-13 19:22:57 -07:00 committed by Eric Engestrom
parent d7e9722382
commit f6e8543ff7
2 changed files with 9 additions and 5 deletions

View file

@ -769,7 +769,7 @@
"description": "mesa: texparam: Add a clamping macro to handle out-of-range floats returned as integers.",
"nominated": true,
"nomination_type": 1,
"resolution": 0,
"resolution": 1,
"master_sha": null,
"because_sha": "53c36dfcfe3eb3749a53267f054870280afb0d71"
},

View file

@ -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.
@ -2479,7 +2483,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))
@ -2494,7 +2498,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))
@ -2518,7 +2522,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)
@ -2563,7 +2567,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)