From f6e8543ff776043037aca84a733f9e9c38a25054 Mon Sep 17 00:00:00 2001 From: Alexander Shi Date: Tue, 13 Apr 2021 19:22:57 -0700 Subject: [PATCH] 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 Reviewed-by: Jason Ekstrand Part-of: (cherry picked from commit 55fb9417a6a3fd5908a459b94de5f38b6e3a14ba) --- .pick_status.json | 2 +- src/mesa/main/texparam.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.pick_status.json b/.pick_status.json index 3e298d12fb5..9e2e53b1604 100644 --- a/.pick_status.json +++ b/.pick_status.json @@ -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" }, diff --git a/src/mesa/main/texparam.c b/src/mesa/main/texparam.c index 16c0f282b7f..fc99f3f3279 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. @@ -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)