1 /*
2  * Copyright © 2009 Intel Corporation
3  * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Marek Olšák <maraeo@gmail.com>
26  *
27  * Based on fbo-clearmipmap by:
28  *    Eric Anholt <eric@anholt.net>
29  *
30  */
31 
32 #include "piglit-util-gl.h"
33 
34 PIGLIT_GL_TEST_CONFIG_BEGIN
35 
36 	config.supports_gl_compat_version = 10;
37 
38 	config.window_width = 512;
39 	config.window_height = 256;
40 	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
41 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
42 
43 PIGLIT_GL_TEST_CONFIG_END
44 
45 #define TEX_WIDTH 256
46 #define TEX_HEIGHT 256
47 
48 static const float colors[][3] = {
49 	{1.0, 0.0, 0.0},
50 	{0.0, 1.0, 0.0},
51 	{0.0, 0.0, 1.0},
52 	{1.0, 1.0, 0.0},
53 	{0.0, 1.0, 1.0},
54 	{1.0, 0.0, 1.0},
55 	{0.5, 0.0, 0.5},
56 	{1.0, 1.0, 1.0},
57 };
58 
59 static const char *sh_tex =
60 	"uniform sampler2D tex;"
61 	"void main()"
62 	"{"
63 	"   gl_FragColor = texture2D(tex, gl_TexCoord[0].xy);"
64 	"}";
65 
66 static const char *sh_texgrad =
67 	"#extension GL_ARB_shader_texture_lod : enable\n"
68 	"uniform sampler2D tex;"
69 	"void main()"
70 	"{"
71 	"   gl_FragColor = texture2DGradARB(tex, gl_TexCoord[0].xy,"
72 	"                                   dFdx(gl_TexCoord[0].xy),"
73 	"                                   dFdy(gl_TexCoord[0].xy));"
74 	"}";
75 
76 static GLint prog_tex, prog_texgrad;
77 
piglit_init(int argc,char ** argv)78 void piglit_init(int argc, char **argv)
79 {
80 	GLuint tex, fb;
81 	GLenum status;
82 	int i, dim;
83 
84 	piglit_require_GLSL();
85 	piglit_require_extension("GL_EXT_framebuffer_object");
86 	piglit_require_extension("GL_ARB_shader_texture_lod");
87 
88 	prog_tex = piglit_build_simple_program(NULL, sh_tex);
89 	prog_texgrad = piglit_build_simple_program(NULL, sh_texgrad);
90 
91 	glGenTextures(1, &tex);
92 	glBindTexture(GL_TEXTURE_2D, tex);
93 
94 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
95 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
96 
97 	for (i = 0, dim = TEX_WIDTH; dim >0; i++, dim /= 2) {
98 		glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA,
99 			     dim, dim,
100 			     0,
101 			     GL_RGBA, GL_UNSIGNED_BYTE, NULL);
102 	}
103 	if (!piglit_check_gl_error(GL_NO_ERROR))
104 		piglit_report_result(PIGLIT_FAIL);
105 
106 	glBindTexture(GL_TEXTURE_2D, 0);
107 	glDisable(GL_TEXTURE_2D);
108 
109 	glGenFramebuffersEXT(1, &fb);
110 	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
111 
112 	for (i = 0, dim = TEX_WIDTH; dim >0; i++, dim /= 2) {
113 		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
114 					  GL_COLOR_ATTACHMENT0_EXT,
115 					  GL_TEXTURE_2D,
116 					  tex,
117 					  i);
118 
119 		status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
120 		if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
121 			fprintf(stderr, "FBO incomplete\n");
122 			piglit_report_result(PIGLIT_SKIP);
123 		}
124 
125 		glClearColor(colors[i][0],
126 			     colors[i][1],
127 			     colors[i][2],
128 			     0.0);
129 		glClear(GL_COLOR_BUFFER_BIT);
130 
131 		if (!piglit_check_gl_error(GL_NO_ERROR))
132 		        piglit_report_result(PIGLIT_FAIL);
133 	}
134 	glDeleteFramebuffersEXT(1, &fb);
135 	glBindTexture(GL_TEXTURE_2D, tex);
136 
137 	glMatrixMode(GL_PROJECTION);
138 	glLoadIdentity();
139 	glFrustum(-0.1, 0.1, -0.1, 0.1, 0.1, 1000.0);
140 
141 	glMatrixMode(GL_MODELVIEW);
142 	glLoadIdentity();
143 	glTranslatef(-0.5, -0.5, -1.2);
144 	glRotatef(68, 0, 1, 0);
145 	glScalef(2000, 1, 1);
146 
147 	glEnable(GL_TEXTURE_2D);
148 	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
149 
150 	piglit_set_tolerance_for_bits(7, 7, 7, 7);
151 
152 	printf("Left: texture2D, Right: texture2DGradARB\n");
153 }
154 
draw_quad()155 static void draw_quad()
156 {
157 	glBegin(GL_QUADS);
158 	glTexCoord2f(0, 0);
159 	glVertex2f(0, 0);
160 	glTexCoord2f(1, 0);
161 	glVertex2f(1, 0);
162 	glTexCoord2f(1, 1);
163 	glVertex2f(1, 1);
164 	glTexCoord2f(0, 1);
165 	glVertex2f(0, 1);
166 	glEnd();
167 }
168 
piglit_display(void)169 enum piglit_result piglit_display(void)
170 {
171 	GLboolean pass = GL_TRUE;
172 
173 	glViewport(0, 0, piglit_width, piglit_height);
174 	glClearColor(0.5, 0.5, 0.5, 0.5);
175 	glClear(GL_COLOR_BUFFER_BIT);
176 
177 	glViewport(0, 0, piglit_width/2, piglit_height);
178 	glUseProgram(prog_tex);
179 	draw_quad();
180 
181 	glViewport(piglit_width/2, 0, piglit_width/2, piglit_height);
182 	glUseProgram(prog_texgrad);
183 	draw_quad();
184 
185 	if (!piglit_probe_rect_halves_equal_rgba(0, 0, piglit_width, piglit_height))
186 		pass = GL_FALSE;
187 
188 	piglit_present_results();
189 
190 	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
191 }
192