Add a method to round GLfloat parameters to the nearest integral value, for use in state-setting API methods.

TRAC #23545

Signed-off-by: Shannon Woods
Signed-off-by: Nicolas Capens
Authored-by: Jamie Madill
diff --git a/src/common/utilities.h b/src/common/utilities.h
index 88f8d68..41e1936 100644
--- a/src/common/utilities.h
+++ b/src/common/utilities.h
@@ -44,6 +44,12 @@
 
 bool IsTriangleMode(GLenum drawMode);
 
+// [OpenGL ES 3.0.2] Section 2.3.1 page 14
+// Data Conversion For State-Setting Commands
+// Floating-point values are rounded to the nearest integer, instead of truncated, as done by static_cast.
+template <typename outT> outT iround(GLfloat value) { return static_cast<outT>(value > 0.0f ? floor(value + 0.5f) : ceil(value - 0.5f)); }
+template <typename outT> outT uiround(GLfloat value) { return static_cast<outT>(value + 0.5f); }
+
 }
 
 std::string getTempPath();
diff --git a/src/libGLESv2/libGLESv2.cpp b/src/libGLESv2/libGLESv2.cpp
index 16e5c93..349cb88 100644
--- a/src/libGLESv2/libGLESv2.cpp
+++ b/src/libGLESv2/libGLESv2.cpp
@@ -4982,7 +4982,7 @@
                             params[i] = (GLint)(((GLfloat)(0xFFFFFFFF) * floatParams[i] - 1.0f) / 2.0f);
                         }
                         else
-                            params[i] = (GLint)(floatParams[i] > 0.0f ? floor(floatParams[i] + 0.5) : ceil(floatParams[i] - 0.5));
+                            params[i] = gl::iround<GLint>(floatParams[i]);
                     }
 
                     delete [] floatParams;
@@ -5938,7 +5938,7 @@
                 for (int i = 0; i < 4; ++i)
                 {
                     float currentValue = currentValueData.FloatValues[i];
-                    params[i] = (GLint)(currentValue > 0.0f ? floor(currentValue + 0.5f) : ceil(currentValue - 0.5f));
+                    params[i] = gl::iround<GLint>(currentValue);
                 }
             }
             else
@@ -7002,37 +7002,37 @@
             switch (pname)
             {
               case GL_TEXTURE_WRAP_S:
-                if (!texture->setWrapS((GLenum)param))
+                if (!texture->setWrapS(gl::uiround<GLenum>(param)))
                 {
                     return gl::error(GL_INVALID_ENUM);
                 }
                 break;
               case GL_TEXTURE_WRAP_T:
-                if (!texture->setWrapT((GLenum)param))
+                if (!texture->setWrapT(gl::uiround<GLenum>(param)))
                 {
                     return gl::error(GL_INVALID_ENUM);
                 }
                 break;
               case GL_TEXTURE_WRAP_R:
-                if (context->getClientVersion() < 3 || !texture->setWrapR((GLenum)param))
+                if (context->getClientVersion() < 3 || !texture->setWrapR(gl::uiround<GLenum>(param)))
                 {
                     return gl::error(GL_INVALID_ENUM);
                 }
                 break;
               case GL_TEXTURE_MIN_FILTER:
-                if (!texture->setMinFilter((GLenum)param))
+                if (!texture->setMinFilter(gl::uiround<GLenum>(param)))
                 {
                     return gl::error(GL_INVALID_ENUM);
                 }
                 break;
               case GL_TEXTURE_MAG_FILTER:
-                if (!texture->setMagFilter((GLenum)param))
+                if (!texture->setMagFilter(gl::uiround<GLenum>(param)))
                 {
                     return gl::error(GL_INVALID_ENUM);
                 }
                 break;
               case GL_TEXTURE_USAGE_ANGLE:
-                if (!texture->setUsage((GLenum)param))
+                if (!texture->setUsage(gl::uiround<GLenum>(param)))
                 {
                     return gl::error(GL_INVALID_ENUM);
                 }