[Piglit] [PATCH] copyteximage: refactor the code for readability and debugging

Anuj Phogat anuj.phogat at gmail.com
Fri Aug 24 11:19:46 PDT 2012


On Fri, Aug 24, 2012 at 7:20 AM, Brian Paul <brianp at vmware.com> wrote:
> Test a single target/format combination in test_target_and_format().
> This makes it a bit easier to hack the code to debug a specific
> combination.  It's also easier to read.
>
> (note: the patch/diff might look really ugly and complicated but the
> code movement is pretty simple, actually)
> ---
>  tests/texturing/copyteximage.c |  373 +++++++++++++++++++++-------------------
>  1 files changed, 197 insertions(+), 176 deletions(-)
>
> diff --git a/tests/texturing/copyteximage.c b/tests/texturing/copyteximage.c
> index 4b7f877..4da8228 100644
> --- a/tests/texturing/copyteximage.c
> +++ b/tests/texturing/copyteximage.c
> @@ -398,221 +398,242 @@ static GLboolean probe_rect(int x, int y, int w, int h,
>         return piglit_probe_rect_rgba(x, y, w, h, expected_scaled);
>  }
>
> -enum piglit_result
> -piglit_display(void)
> +
> +/**
> + * Test a specific texture target and format combination.
> + */
> +static GLboolean
> +test_target_and_format(GLint x, GLint y, GLenum target, GLenum format,
> +                      const GLfloat *expected)
>  {
> -       GLuint tex;
>         GLboolean pass = GL_TRUE;
> -       GLenum format;
> -       const GLfloat *expected;
> -       int i, j, k;
> +       GLuint k;
>
> -       glClear(GL_COLOR_BUFFER_BIT);
> +       if (!piglit_automatic)
> +               printf("Texture target = %s, Internal format = %s",
> +                      piglit_get_gl_enum_name(target),
> +                      piglit_get_gl_enum_name(format));
> +
> +       if (!supported_format(format) ||
> +           !supported_target_format(target, format)) {
> +               if (!piglit_automatic)
> +                       printf(" - skipped\n");
> +               return GL_TRUE; /* not a failure */
> +       } else {
> +               if (!piglit_automatic)
> +                       printf("\n");
> +       }
>
> -       /* Do glCopyPixels and draw a textured rectangle for each format
> -        * and each texture target
> +       /* To avoid failures not related to this test case,
> +        * loosen up the tolerence for compressed texture
> +        * formats
>          */
> -       for (j = 0; j < ARRAY_SIZE(target); j++) {
> -               if (test_target != -1 && test_target != j)
> -                       continue;
> -               if (!supported_target(j))
> -                       continue;
> -
> -               printf("Testing %s\n", piglit_get_gl_enum_name(target[j].target));
> -
> -               if (target[j].target == GL_TEXTURE_1D_ARRAY) {
> -                       printf("NOTE: We use glCopyTexSubImage2D to set 4 texture layers at once.\n");
> +       if (is_compressed_format(format))
> +               piglit_set_tolerance_for_bits(7, 7, 7, 7);
> +       else
> +               piglit_set_tolerance_for_bits(8, 8, 8, 8);
> +
> +       switch(target) {
> +
> +       case GL_TEXTURE_1D:
> +               draw(format, 1.0);
> +               glCopyTexImage1D(GL_TEXTURE_1D, 0,
> +                                format,
> +                                0, 0, IMAGE_SIZE, 0);
> +               pass = piglit_check_gl_error(GL_NO_ERROR)
> +                       && pass;
> +
> +               glEnable(target);
> +               glTexCoordPointer(2, GL_FLOAT, 0, texCoords_2d);
> +               piglit_draw_rect(x, y, IMAGE_SIZE, IMAGE_SIZE);
> +               pass = piglit_probe_rect_rgba(x, y, IMAGE_SIZE,
> +                                             IMAGE_SIZE,
> +                                             expected)
> +                       && pass;
> +               break;
> +
> +       case GL_TEXTURE_2D:
> +               draw(format, 1.0);
> +               glCopyTexImage2D(GL_TEXTURE_2D, 0, format, 0, 0,
> +                                IMAGE_SIZE, IMAGE_SIZE, 0);
> +               pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
> +
> +               glEnable(target);
> +               glTexCoordPointer(2, GL_FLOAT, 0, texCoords_2d);
> +
> +               piglit_draw_rect(x, y, IMAGE_SIZE, IMAGE_SIZE);
> +               pass = piglit_probe_rect_rgba(x, y, IMAGE_SIZE,
> +                                             IMAGE_SIZE,
> +                                             expected)
> +                       && pass;
> +               break;
> +
> +       case GL_TEXTURE_3D:
> +               glTexImage3D(GL_TEXTURE_3D, 0, format, IMAGE_SIZE, IMAGE_SIZE, 4,
> +                            0, GL_RGBA, GL_FLOAT, NULL);
> +
> +               for (k = 0; k < 4; k++) {
> +                       draw(format, 1.0 - k*0.2);
> +                       glCopyTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, k,
> +                                           0, 0, IMAGE_SIZE, IMAGE_SIZE);
>                 }
>
> -               /* Texture setup */
> -               glGenTextures(1, &tex);
> -               glBindTexture(target[j].target, tex);
> -               glTexParameteri(target[j].target,
> -                               GL_TEXTURE_MIN_FILTER,
> -                               GL_NEAREST);
> -               glTexParameteri(target[j].target,
> -                               GL_TEXTURE_MAG_FILTER,
> -                               GL_NEAREST);
> -               glTexParameteri(target[j].target,
> -                               GL_GENERATE_MIPMAP,
> -                               GL_FALSE);
> -
> -               glEnableClientState(GL_TEXTURE_COORD_ARRAY);
> +               pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
>
> -               for (i = 0; i < ARRAY_SIZE(test_vectors); i++) {
> -                       GLint x = IMAGE_SIZE * (i + 1);
> -                       GLint y = 0;
> +               glEnable(target);
>
> -                       format = test_vectors[i].format;
> -                       expected = (const float*)test_vectors[i].expected;
> +               for (k = 0; k < 4; k++) {
> +                       glTexCoordPointer(3, GL_FLOAT, 0, texCoords_3d[k]);
> +                       piglit_draw_rect(x, y, IMAGE_SIZE, IMAGE_SIZE);
> +                       pass = probe_rect(x, y, IMAGE_SIZE, IMAGE_SIZE,
> +                                         expected, 1.0 - k*0.2) && pass;
> +               }
> +               break;
> +
> +       case GL_TEXTURE_CUBE_MAP:
> +               for (k = 0; k < 6; k++) {
> +                       draw(format, 1.0 - k*0.15);
> +                       glCopyTexImage2D(cube_face_targets[k],
> +                                        0, format, 0, 0,
> +                                        IMAGE_SIZE, IMAGE_SIZE, 0);
> +               }
>
> -                       if (!piglit_automatic)
> -                               printf("Texture target = %s, Internal"
> -                                      " format = %s",
> -                                      piglit_get_gl_enum_name(target[j].target),
> -                                      piglit_get_gl_enum_name(format));
> -
> -                       if (!supported_format(format) ||
> -                           !supported_target_format(target[j].target, format)) {
> -                               if (!piglit_automatic)
> -                                       printf(" - skipped\n");
> -                               continue;
> -                       } else {
> -                               if (!piglit_automatic)
> -                                       printf("\n");
> -                       }
> +               pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
>
> +               glEnable(target);
>
> -                       /* To avoid failures not related to this test case,
> -                        * loosen up the tolerence for compressed texture
> -                        * formats
> -                        */
> -                       if (is_compressed_format(format))
> -                               piglit_set_tolerance_for_bits(7, 7, 7, 7);
> -                       else
> -                               piglit_set_tolerance_for_bits(8, 8, 8, 8);
> -
> -                       switch(target[j].target) {
> -
> -                       case GL_TEXTURE_1D:
> -                               draw(format, 1.0);
> -                               glCopyTexImage1D(GL_TEXTURE_1D, 0,
> -                                                format,
> -                                                0, 0, IMAGE_SIZE, 0);
> -                               pass = piglit_check_gl_error(GL_NO_ERROR)
> -                                      && pass;
> -
> -                               glEnable(target[j].target);
> -                               glTexCoordPointer(2, GL_FLOAT, 0, texCoords_2d);
> -                               piglit_draw_rect(x, y, IMAGE_SIZE, IMAGE_SIZE);
> -                               pass = piglit_probe_rect_rgba(x, y, IMAGE_SIZE,
> -                                                             IMAGE_SIZE,
> -                                                             expected)
> -                                      && pass;
> -                               break;
> +               for (k = 0; k < 6; k++) {
> +                       glTexCoordPointer(3, GL_FLOAT, 0,
> +                                         cube_face_texcoords[k]);
> +                       piglit_draw_rect(x, y, IMAGE_SIZE, IMAGE_SIZE);
> +                       pass = probe_rect(x, y, IMAGE_SIZE, IMAGE_SIZE,
> +                                         expected, 1.0 - k*0.15) && pass;
> +               }
> +               break;
>
> -                       case GL_TEXTURE_2D:
> -                               draw(format, 1.0);
> -                               glCopyTexImage2D(GL_TEXTURE_2D, 0, format, 0, 0,
> -                                                IMAGE_SIZE, IMAGE_SIZE, 0);
> -                               pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
> +       case GL_TEXTURE_1D_ARRAY:
> +               glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, format, IMAGE_SIZE, 16,
> +                            0, get_format(format), GL_FLOAT, NULL);
>
> -                               glEnable(target[j].target);
> -                               glTexCoordPointer(2, GL_FLOAT, 0, texCoords_2d);
> +               for (k = 0; k < 4; k++) {
> +                       draw(format, 1.0 - 0.2*k);
> +                       glCopyTexSubImage2D(GL_TEXTURE_1D_ARRAY, 0, 0, 4*k,
> +                                           0, 0, IMAGE_SIZE, 4);
> +               }
>
> -                               piglit_draw_rect(x, y, IMAGE_SIZE, IMAGE_SIZE);
> -                               pass = piglit_probe_rect_rgba(x, y, IMAGE_SIZE,
> -                                                             IMAGE_SIZE,
> -                                                             expected)
> -                                      && pass;
> -                               break;
> +               pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
>
> -                       case GL_TEXTURE_3D:
> -                               glTexImage3D(GL_TEXTURE_3D, 0, format, IMAGE_SIZE, IMAGE_SIZE, 4,
> -                                            0, GL_RGBA, GL_FLOAT, NULL);
> +               glEnable(target);
>
> -                               for (k = 0; k < 4; k++) {
> -                                       draw(format, 1.0 - k*0.2);
> -                                       glCopyTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, k,
> -                                                           0, 0, IMAGE_SIZE, IMAGE_SIZE);
> -                               }
> +               for (k = 0; k < 16; k++) {
> +                       glTexCoordPointer(2, GL_FLOAT, 0, texCoords_1d_array[k]);
> +                       piglit_draw_rect(x, y, IMAGE_SIZE, IMAGE_SIZE);
> +                       pass = probe_rect(x, y, IMAGE_SIZE, IMAGE_SIZE,
> +                                         expected, 1.0 - 0.2*(k/4)) && pass;
> +               }
> +               break;
>
> -                               pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
> +       case GL_TEXTURE_2D_ARRAY:
> +               glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, format, IMAGE_SIZE, IMAGE_SIZE, 4,
> +                            0, get_format(format), GL_FLOAT, NULL);
>
> -                               glEnable(target[j].target);
> +               for (k = 0; k < 4; k++) {
> +                       draw(format, 1.0 - k*0.2);
> +                       glCopyTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, k,
> +                                           0, 0, IMAGE_SIZE, IMAGE_SIZE);
> +               }
>
> -                               for (k = 0; k < 4; k++) {
> -                                       glTexCoordPointer(3, GL_FLOAT, 0, texCoords_3d[k]);
> -                                       piglit_draw_rect(x, y, IMAGE_SIZE, IMAGE_SIZE);
> -                                       pass = probe_rect(x, y, IMAGE_SIZE, IMAGE_SIZE,
> -                                                         expected, 1.0 - k*0.2) && pass;
> -                               }
> -                               break;
> +               pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
>
> -                       case GL_TEXTURE_CUBE_MAP:
> -                               for (k = 0; k < 6; k++) {
> -                                       draw(format, 1.0 - k*0.15);
> -                                       glCopyTexImage2D(cube_face_targets[k],
> -                                                        0, format, 0, 0,
> -                                                        IMAGE_SIZE, IMAGE_SIZE, 0);
> -                               }
> +               glEnable(target);
>
> -                               pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
> +               for (k = 0; k < 4; k++) {
> +                       glTexCoordPointer(3, GL_FLOAT, 0, texCoords_2d_array[k]);
> +                       piglit_draw_rect(x, y, IMAGE_SIZE, IMAGE_SIZE);
> +                       pass = probe_rect(x, y, IMAGE_SIZE, IMAGE_SIZE,
> +                                         expected, 1.0 - k*0.2) && pass;
> +               }
> +               break;
> +
> +       case GL_TEXTURE_RECTANGLE:
> +               draw(format, 1.0);
> +               glCopyTexImage2D(GL_TEXTURE_RECTANGLE, 0, format, 0, 0,
> +                                IMAGE_SIZE, IMAGE_SIZE, 0);
> +               pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
> +
> +               glEnable(target);
> +               glTexCoordPointer(2, GL_FLOAT, 0, texCoords_rect);
> +
> +               piglit_draw_rect(x, y, IMAGE_SIZE, IMAGE_SIZE);
> +               pass = piglit_probe_rect_rgba(x, y, IMAGE_SIZE,
> +                                             IMAGE_SIZE,
> +                                             expected)
> +                       && pass;
> +               break;
> +       }
>
> -                               glEnable(target[j].target);
> +       glDisable(target);
>
> -                               for (k = 0; k < 6; k++) {
> -                                       glTexCoordPointer(3, GL_FLOAT, 0,
> -                                                         cube_face_texcoords[k]);
> -                                       piglit_draw_rect(x, y, IMAGE_SIZE, IMAGE_SIZE);
> -                                       pass = probe_rect(x, y, IMAGE_SIZE, IMAGE_SIZE,
> -                                                         expected, 1.0 - k*0.15) && pass;
> -                               }
> -                               break;
> +       return pass;
> +}
>
> -                       case GL_TEXTURE_1D_ARRAY:
> -                               glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, format, IMAGE_SIZE, 16,
> -                                            0, get_format(format), GL_FLOAT, NULL);
>
> -                               for (k = 0; k < 4; k++) {
> -                                       draw(format, 1.0 - 0.2*k);
> -                                       glCopyTexSubImage2D(GL_TEXTURE_1D_ARRAY, 0, 0, 4*k,
> -                                                           0, 0, IMAGE_SIZE, 4);
> -                               }
> +static GLuint
> +create_texture(GLenum target)
> +{
> +       GLuint tex;
>
> -                               pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
> +       glGenTextures(1, &tex);
> +       glBindTexture(target, tex);
> +       glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
> +       glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
> +       glTexParameteri(target, GL_GENERATE_MIPMAP, GL_FALSE);
>
> -                               glEnable(target[j].target);
> +       return tex;
> +}
>
> -                               for (k = 0; k < 16; k++) {
> -                                       glTexCoordPointer(2, GL_FLOAT, 0, texCoords_1d_array[k]);
> -                                       piglit_draw_rect(x, y, IMAGE_SIZE, IMAGE_SIZE);
> -                                       pass = probe_rect(x, y, IMAGE_SIZE, IMAGE_SIZE,
> -                                                         expected, 1.0 - 0.2*(k/4)) && pass;
> -                               }
> -                               break;
>
> -                       case GL_TEXTURE_2D_ARRAY:
> -                               glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, format, IMAGE_SIZE, IMAGE_SIZE, 4,
> -                                            0, get_format(format), GL_FLOAT, NULL);
> +enum piglit_result
> +piglit_display(void)
> +{
> +       GLuint tex;
> +       GLboolean pass = GL_TRUE;
> +       const GLfloat *expected;
> +       int i, j;
>
> -                               for (k = 0; k < 4; k++) {
> -                                       draw(format, 1.0 - k*0.2);
> -                                       glCopyTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, k,
> -                                                           0, 0, IMAGE_SIZE, IMAGE_SIZE);
> -                               }
> +       glClear(GL_COLOR_BUFFER_BIT);
>
> -                               pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
> +       glEnableClientState(GL_TEXTURE_COORD_ARRAY);
>
> -                               glEnable(target[j].target);
> +       /* Do glCopyPixels and draw a textured rectangle for each format
> +        * and each texture target
> +        */
> +       for (j = 0; j < ARRAY_SIZE(target); j++) {
> +               if (test_target != -1 && test_target != j)
> +                       continue;
> +               if (!supported_target(j))
> +                       continue;
>
> -                               for (k = 0; k < 4; k++) {
> -                                       glTexCoordPointer(3, GL_FLOAT, 0, texCoords_2d_array[k]);
> -                                       piglit_draw_rect(x, y, IMAGE_SIZE, IMAGE_SIZE);
> -                                       pass = probe_rect(x, y, IMAGE_SIZE, IMAGE_SIZE,
> -                                                         expected, 1.0 - k*0.2) && pass;
> -                               }
> -                               break;
> +               printf("Testing %s\n", piglit_get_gl_enum_name(target[j].target));
>
> -                       case GL_TEXTURE_RECTANGLE:
> -                               draw(format, 1.0);
> -                               glCopyTexImage2D(GL_TEXTURE_RECTANGLE, 0, format, 0, 0,
> -                                                IMAGE_SIZE, IMAGE_SIZE, 0);
> -                               pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
> +               if (target[j].target == GL_TEXTURE_1D_ARRAY) {
> +                       printf("NOTE: We use glCopyTexSubImage2D to set 4 texture layers at once.\n");
> +               }
>
> -                               glEnable(target[j].target);
> -                               glTexCoordPointer(2, GL_FLOAT, 0, texCoords_rect);
> +               tex = create_texture(target[j].target);
>
> -                               piglit_draw_rect(x, y, IMAGE_SIZE, IMAGE_SIZE);
> -                               pass = piglit_probe_rect_rgba(x, y, IMAGE_SIZE,
> -                                                             IMAGE_SIZE,
> -                                                             expected)
> -                                      && pass;
> -                               break;
> +               for (i = 0; i < ARRAY_SIZE(test_vectors); i++) {
> +                       GLint x = IMAGE_SIZE * (i + 1);
> +                       GLint y = 0;
> +                       expected = (const float*)test_vectors[i].expected;
> +
> +                       if (!test_target_and_format(x, y, target[j].target,
> +                                                   test_vectors[i].format,
> +                                                   expected)) {
> +                               pass = GL_FALSE;
>                         }
> -                       glDisable(target[j].target);
>                 }
> -               glDisableClientState(GL_TEXTURE_COORD_ARRAY);
> +
>                 glDeleteTextures(1, &tex);
>         }
>         if (!piglit_automatic)
> --
> 1.7.3.4
>
> _______________________________________________
> Piglit mailing list
> Piglit at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/piglit

Reviewed-by: Anuj Phogat <anuj.phogat at gmail.com>


More information about the Piglit mailing list