[Piglit] [PATCH v3 2/5] Split piglit_get_gl_version() into two functions.

Paul Berry stereotype441 at gmail.com
Thu Oct 13 13:45:34 PDT 2011


Previously, piglit_get_gl_version() had three return values: the GL
version (major and minor version numbers separately) and a boolean
indicating whether or not the API is GL ES.  This meant that we had to
jump through hoops in order to use it, e.g.:

    /* Are we using GL with a version of at least 3.1? */
    bool es;
    int major;
    int minor;
    piglit_get_gl_version(&es, &major, &minor);
    if (!es && (major > 3 || (major == 3 && minor >= 1)))
        ...

This patch changes piglit_get_gl_version() so that it returns a single
integer whose value is 10 times the GL version (e.g. 31 for GL version
3.1), and splits off the functionality for checking whether the API is
GL ES to a new function, piglit_is_gles().

Now the same logic looks like this:

    /* Are we using GL with a version of at least 3.1? */
    if (!piglit_is_gles() && piglit_get_gl_version() >= 31)
        ...
---
 tests/glslparsertest/glslparsertest.c |   10 +++---
 tests/util/piglit-util.c              |   53 +++++++++++++++-----------------
 tests/util/piglit-util.h              |   14 +++++---
 3 files changed, 39 insertions(+), 38 deletions(-)

diff --git a/tests/glslparsertest/glslparsertest.c b/tests/glslparsertest/glslparsertest.c
index cfe8d10..b7a2dc9 100644
--- a/tests/glslparsertest/glslparsertest.c
+++ b/tests/glslparsertest/glslparsertest.c
@@ -41,7 +41,7 @@
 
 static char *filename;
 static int expected_pass;
-static int gl_major_version = 0;
+static int gl_version_times_10 = 0;
 static int check_link = 0;
 static float requested_version = 1.10;
 
@@ -51,7 +51,7 @@ get_shader_compile_status(GLuint shader)
 	GLint status;
 
 #if defined USE_OPENGL
-	if (gl_major_version >= 2) {
+	if (gl_version_times_10 >= 20) {
 		glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
 	} else {
 		glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
@@ -71,7 +71,7 @@ get_shader_info_log_length(GLuint shader)
 	GLsizei length;
 
 #if defined USE_OPENGL
-	if (gl_major_version >= 2) {
+	if (gl_version_times_10 >= 20) {
 		glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
 	} else {
 		glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
@@ -308,13 +308,13 @@ int main(int argc, char**argv)
 	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
 	glutInitWindowSize(WIN_WIDTH, WIN_HEIGHT);
 	glutCreateWindow("glslparsertest");
-	piglit_get_gl_version(NULL, &gl_major_version, NULL);
+	gl_version_times_10 = piglit_get_gl_version();
 
 #ifdef USE_OPENGL
 	glewInit();
 #endif
 
-	if (gl_major_version < 2
+	if (gl_version_times_10 < 20
 	    && !piglit_is_extension_supported("GL_ARB_shader_objects")) {
 		printf("Requires OpenGL 2.0\n");
 		piglit_report_result(PIGLIT_SKIP);
diff --git a/tests/util/piglit-util.c b/tests/util/piglit-util.c
index 358b0dc..026c64e 100644
--- a/tests/util/piglit-util.c
+++ b/tests/util/piglit-util.c
@@ -56,38 +56,35 @@ void piglit_glutInit(int argc, char **argv)
 #endif
 }
 
-void piglit_get_gl_version(bool *es, int* major, int* minor)
+bool piglit_is_gles()
 {
-	/* Version of OpenGL API. */
-	bool es_local;
-	int major_local;
-	int minor_local;
-
-	const char *version_string;
-	int c; /* scanf count */
-
-	version_string = (const char*) glGetString(GL_VERSION);
-	es_local = strncmp("OpenGL ES ", version_string, 10) == 0;
-	if (es_local) {
-		c = sscanf(version_string,
-		           "OpenGL ES %i.%i",
-		           &major_local,
-		           &minor_local);
-	} else {
-		c = sscanf(version_string,
-		           "%i.%i",
-		           &major_local,
-		           &minor_local);
+	const char *version_string = (const char *) glGetString(GL_VERSION);
+	return strncmp("OpenGL ES ", version_string, 10) == 0;
+}
+
+int piglit_get_gl_version()
+{
+	const char *version_string = (const char *) glGetString(GL_VERSION);
+	const char *version_number_string;
+	int scanf_count;
+	int major;
+	int minor;
+
+	/* skip to version number */
+	if (strncmp("OpenGL ES ", version_string, 10) == 0)
+		version_number_string = version_string + 10;
+	else
+		version_number_string = version_string;
+
+	/* Interpret version number */
+	scanf_count = sscanf(version_string, "%i.%i", &major, &minor);
+	if (scanf_count != 2) {
+		printf("Unable to interpret GL_VERSION string: %s\n",
+		       version_string);
+		piglit_report_result(PIGLIT_FAIL);
+		exit(1);
 	}
-	assert(c == 2);
-
-	/* Write outputs. */
-	if (es != NULL)
-		*es = es_local;
-	if (major != NULL)
-		*major = major_local;
-	if (minor != NULL)
-		*minor = minor_local;
+	return 10*major+minor;
 }
 
 bool piglit_is_extension_supported(const char *name)
diff --git a/tests/util/piglit-util.h b/tests/util/piglit-util.h
index a12a234..2a423c3 100644
--- a/tests/util/piglit-util.h
+++ b/tests/util/piglit-util.h
@@ -100,13 +100,17 @@ extern const unsigned int fdo_bitmap_height;
 void piglit_glutInit(int argc, char **argv);
 
 /**
- * \brief Get version of OpenGL API.
- *
- * Null parameters are ignored.
+ * Determine if the API is OpenGL ES.
+ */
+bool piglit_is_gles();
+
+/**
+ * \brief Get version of OpenGL or OpenGL ES API.
  *
- * \param es Is the API OpenGL or OpenGL ES?
+ * Returned version is multiplied by 10 to make it an integer.  So for
+ * example, if the GL version is 2.1, the return value is 21.
  */
-void piglit_get_gl_version(bool *es, int* major, int* minor);
+int piglit_get_gl_version();
 
 /**
  * \precondition name is not null
-- 
1.7.6.4



More information about the Piglit mailing list