1 /*
2  * Copyright © 2010 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 /**
25  * \file rgtc-teximage-01.c
26  * Verify setting and getting image data for RED or RG formats
27  *
28  * Specify an RGBA image with a compressed RED internal format.  Read the image back as
29  * RGBA.  Verify the red components read back match the source image and the
30  * green, blue, and alpha components are 0, 0, and 1, respectively.
31  *
32  * \author Ian Romanick <ian.d.romanick@intel.com>
33  * Modified by Dave Airlie for RGTC
34  */
35 
36 #include "piglit-util-gl.h"
37 #include "rg-teximage-common.h"
38 
39 #define WIDTH  256
40 #define HEIGHT 256
41 static float rgba_image[4 * WIDTH * HEIGHT];
42 static float rgba_snorm_image[4 * WIDTH * HEIGHT];
43 
44 static const GLenum internal_formats[] = {
45 	GL_COMPRESSED_RED_RGTC1,
46 	GL_COMPRESSED_SIGNED_RED_RGTC1,
47 	GL_RED,
48 	GL_RED_SNORM,
49 	GL_RGBA,
50 	GL_RGBA_SNORM,
51 };
52 
53 GLuint tex[Elements(internal_formats)];
54 const unsigned num_tex = Elements(tex);
55 GLboolean pass = GL_TRUE;
56 
57 PIGLIT_GL_TEST_CONFIG_BEGIN
58 
59 	config.supports_gl_compat_version = 10;
60 
61 	config.window_width = WIDTH*Elements(tex);
62 	config.window_height = HEIGHT;
63 	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
64 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
65 
66 PIGLIT_GL_TEST_CONFIG_END
67 
68 void
piglit_init(int argc,char ** argv)69 piglit_init(int argc, char **argv)
70 {
71 	unsigned i;
72 	GLfloat *result;
73 
74 	piglit_require_extension("GL_ARB_texture_compression_rgtc");
75 
76 	result = (GLfloat *) malloc(4 * WIDTH * HEIGHT * sizeof(GLfloat));
77 	generate_rainbow_texture_data(WIDTH, HEIGHT, false, rgba_image);
78 	generate_rainbow_texture_data(WIDTH, HEIGHT, true, rgba_snorm_image);
79 
80 	glEnable(GL_TEXTURE_2D);
81 	glGenTextures(Elements(tex), tex);
82 
83 	for (i = 0; i < Elements(internal_formats); i++) {
84 		GLenum err;
85 		bool is_snorm = (internal_formats[i] == GL_COMPRESSED_SIGNED_RED_RGTC1 ||
86 				 internal_formats[i] == GL_RGBA_SNORM ||
87 				 internal_formats[i] == GL_RED_SNORM);
88 
89 		glBindTexture(GL_TEXTURE_2D, tex[i]);
90 		glTexImage2D(GL_TEXTURE_2D, 0, internal_formats[i],
91 			     WIDTH, HEIGHT, 0, GL_RGBA,
92 			     GL_FLOAT, is_snorm ? rgba_snorm_image : rgba_image);
93 		err = glGetError();
94 		if (err) {
95 			fprintf(stderr, "glTexImage2D(internalFormat = 0x%04x) "
96 				"generated GL error 0x%04x\n",
97 				internal_formats[i], err);
98 			pass = GL_FALSE;
99 		}
100 
101 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
102 				GL_NEAREST);
103 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
104 				GL_NEAREST);
105 
106 		if (internal_formats[i] != GL_RGBA && internal_formats[i] != GL_RGBA_SNORM) {
107 			glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT,
108 				      result);
109 			pass = compare_texture(is_snorm ? rgba_snorm_image : rgba_image, result,
110 					       internal_formats[i], GL_RGBA,
111 					       (WIDTH * HEIGHT), GL_FALSE, is_snorm)
112 				&& pass;
113 		}
114 	}
115 
116 	free(result);
117 
118 	if (piglit_automatic)
119 		piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
120 }
121