1 /*
2  * Copyright © 2009 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
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /**
25  * This test draws a point sprite with a checkerboard texture and tests
26  * whether the correct colors were drawn using piglit_probe_pixel_rgb.
27  *
28  * \author Ben Holmes
29  */
30 
31 #include "piglit-util-gl.h"
32 
33 #define BOX_SIZE 64
34 #define TEST_COLS 6
35 #define TEST_ROWS 2
36 
37 PIGLIT_GL_TEST_CONFIG_BEGIN
38 
39 	config.supports_gl_compat_version = 10;
40 
41 	config.window_width = 1+((BOX_SIZE+1)*TEST_COLS);
42 	config.window_height = 1+((BOX_SIZE+1)*TEST_ROWS);
43 	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB;
44 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
45 
46 PIGLIT_GL_TEST_CONFIG_END
47 
48 static float maxSize = 0.0f;
49 static GLuint tex;
50 static const GLfloat black[4] = {0.0, 0.0, 0.0, 1.0};
51 static const GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
52 
53 void
piglit_init(int argc,char ** argv)54 piglit_init(int argc, char **argv)
55 {
56 	GLfloat realMaxSize;
57 
58 	(void) argc;
59 	(void) argv;
60 
61 	piglit_require_extension("GL_ARB_point_sprite");
62 
63 	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
64 
65 	glEnable(GL_TEXTURE_2D);
66 	glEnable(GL_POINT_SPRITE_ARB);
67 
68 	glGetFloatv(GL_POINT_SIZE_MAX, &realMaxSize);
69 	maxSize = (realMaxSize > BOX_SIZE) ? BOX_SIZE : realMaxSize;
70 
71 	glClearColor(0.2, 0.2, 0.2, 1.0);
72 	glColor3f(1.0, 1.0, 1.0);
73 
74 	tex = piglit_checkerboard_texture(0, 0, 2, 2, 1, 1, black, white);
75 	glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
76 
77 	if (!piglit_automatic)
78 		printf("Maximum point size is %f, using %f\n",
79 		       realMaxSize, maxSize);
80 }
81 
82 enum piglit_result
piglit_display(void)83 piglit_display(void)
84 {
85 	static const GLenum origins[2] = { GL_UPPER_LEFT, GL_LOWER_LEFT	};
86 	const unsigned num_rows = (piglit_get_gl_version() >= 20) ? 2 : 1;
87 	GLboolean pass = GL_TRUE;
88 	unsigned i;
89 	unsigned j;
90 
91 	glClear(GL_COLOR_BUFFER_BIT);
92 	glBindTexture(GL_TEXTURE_2D, tex);
93 
94 	for (i = 0; i < num_rows; i++) {
95 		const float y = 1 + (BOX_SIZE / 2) + (i * (BOX_SIZE + 1));
96 		const float *const upper_left = (origins[i] == GL_UPPER_LEFT)
97 			? black : white;
98 		const float *const lower_left = (origins[i] == GL_UPPER_LEFT)
99 			? white : black;
100 
101 		/* OpenGL version must be at least 2.0 to support modifying
102 		 * GL_POINT_SPRITE_COORD_ORIGIN.
103 		 */
104 		if (piglit_get_gl_version() >= 20)
105 			glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN,
106 					  origins[i]);
107 
108 		for (j = 0; j < TEST_COLS; j++) {
109 			const float x = 1 + (BOX_SIZE / 2)
110 				+ (j * (BOX_SIZE + 1));
111 			const float size = maxSize / (float) (1 << j);
112 
113 			/* If the point size is too small, there won't be
114 			 * enough pixels drawn for the tests (below).
115 			 */
116 			if (size < 2.0)
117 				continue;
118 
119 			glPointSize(size - 0.2);
120 			glBegin(GL_POINTS);
121 			glTexCoord2f(1.5, 1.5);
122 			glVertex2f(x, y);
123 			glEnd();
124 
125 			if (!piglit_probe_pixel_rgb(x - (size / 4),
126 						    y + (size / 4),
127 						    upper_left)
128 			    || !piglit_probe_pixel_rgb(x - (size / 4),
129 						       y - (size / 4),
130 						       lower_left)
131 			    || !piglit_probe_pixel_rgb(x + (size / 4),
132 						       y + (size / 4),
133 						       lower_left)
134 			    || !piglit_probe_pixel_rgb(x + (size / 4),
135 						       y - (size / 4),
136 						       upper_left)) {
137 				if (!piglit_automatic)
138 					printf("  size = %.3f, "
139 					       "origin = %s left\n",
140 					       size,
141 					       (origins[i] == GL_UPPER_LEFT)
142 					       ? "upper" : "lower");
143 				pass = GL_FALSE;
144 			}
145 		}
146 	}
147 
148 	piglit_present_results();
149 
150 	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
151 }
152