glx-multithread-texture: Avoid front-buffer rendering.
[piglit.git] / tests / shaders / fp-fragment-position.c
blob84dbf3cb9332fb0037d3150d32d43eec62ab2cd0
1 /*
2 * Copyright (c) The Piglit project 2007
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
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.
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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 /**
25 * Test fragment.position.
28 #include "piglit-util-gl.h"
30 PIGLIT_GL_TEST_CONFIG_BEGIN
32 config.supports_gl_compat_version = 10;
34 config.window_width = 200;
35 config.window_height = 200;
36 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_DEPTH;
38 PIGLIT_GL_TEST_CONFIG_END
40 extern float piglit_tolerance[4];
42 #define NUM_PROGRAMS 4
44 static GLuint FragProg[NUM_PROGRAMS];
46 static const char* const ProgramText[NUM_PROGRAMS] = {
47 /* Color = fragment pos * scale factor */
48 "!!ARBfp1.0\n"
49 "PARAM factor = { 0.01, 0.01, 1.0, 0.2 };\n"
50 "MUL result.color, fragment.position, factor;\n"
51 "END",
53 /* Color = dependent 2D texture read */
54 "!!ARBfp1.0\n"
55 "TEMP r0;\n"
56 "ALIAS scaled = r0;\n"
57 "MUL r0.xy, fragment.position, 0.01;\n"
58 "TEX result.color, scaled, texture[1], 2D;\n"
59 "END",
61 /* Color = RECT texture color at fragment pos */
62 "!!ARBfp1.0\n"
63 "TEX result.color, fragment.position, texture[0], RECT;\n"
64 "END",
66 /* Color = 2D texture color at fragment pos */
67 "!!ARBfp1.0\n"
68 "PARAM scale = { 0.01, 0.01, 1.0, 1.0 };\n"
69 "TEMP tc;\n"
70 "MUL tc, fragment.position, scale;\n"
71 "TEX result.color, tc, texture[1], 2D;\n"
72 "MOV result.color.w, 0.5;\n"
73 "END",
76 /**
77 * Draw four quadrilaterals, one for each fragment program:
78 * +--------+--------+
79 * | | |
80 * | Prog 1 | Prog 3 |
81 * | | |
82 * +--------+--------+
83 * | | |
84 * | Prog 0 | Prog 2 |
85 * | | |
86 * +--------+--------+
87 * Each quad is about 100x100 pixels in size.
89 static void DoFrame(void)
91 glClearColor(0.3, 0.3, 0.3, 0.3);
92 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
94 glEnable(GL_FRAGMENT_PROGRAM_ARB);
96 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, FragProg[0]);
98 glBegin(GL_QUADS);
99 glVertex3f(0, 0, 0);
100 glVertex3f(1, 0, 1);
101 glVertex3f(1, 1, 2);
102 glVertex3f(0, 1, 1);
103 glEnd();
105 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, FragProg[1]);
107 glBegin(GL_QUADS);
108 glVertex2f(0, 1);
109 glVertex2f(1, 1);
110 glVertex2f(1, 2);
111 glVertex2f(0, 2);
112 glEnd();
114 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, FragProg[2]);
116 glBegin(GL_QUADS);
117 glVertex2f(1, 0);
118 glVertex2f(2, 0);
119 glVertex2f(2, 1);
120 glVertex2f(1, 1);
121 glEnd();
123 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, FragProg[3]);
125 glBegin(GL_QUADS);
126 glVertex2f(1, 1);
127 glVertex2f(2, 1);
128 glVertex2f(2, 2);
129 glVertex2f(1, 2);
130 glEnd();
134 static const struct {
135 const char* name;
136 float x, y;
137 float expected[4];
138 } Probes[] = {
139 // Program 0
141 "basic #1",
142 0.2, 0.2,
143 { 0.2, 0.2, (0.4+2)/8, 0.2 }
146 "basic #2",
147 0.8, 0.2,
148 { 0.8, 0.2, (1.0+2)/8, 0.2 }
151 "basic #3",
152 0.8, 0.8,
153 { 0.8, 0.8, (1.6+2)/8, 0.2 }
156 "basic #4",
157 0.2, 0.8,
158 { 0.2, 0.8, (1.0+2)/8, 0.2 }
161 // Program 1
163 "tex2d scaled #1",
164 0.2, 1.2,
165 { 0.8, 0.2, 0.2, 0.2 }
168 "tex2d scaled #2",
169 0.8, 1.2,
170 { 0.2, 0.2, 0.8, 0.5 }
173 "tex2d scaled #3",
174 0.8, 1.8,
175 { 0.2, 0.8, 0.8, 0.8 }
178 "tex2d scaled #4",
179 0.2, 1.8,
180 { 0.8, 0.8, 0.2, 0.5 }
183 // Program 2
185 "texrect #1",
186 1.2, 0.2,
187 { 0.53, 0.47, 0.08, 0.27 }
190 "texrect #2",
191 1.8, 0.2,
192 { 0.29, 0.70, 0.08, 0.40 }
195 "texrect #1",
196 1.8, 0.8,
197 { 0.29, 0.70, 0.31, 0.51 }
200 "texrect #1",
201 1.2, 0.8,
202 { 0.53, 0.47, 0.31, 0.39 }
205 // Program 3
207 "tex2d unscaled #1",
208 1.2, 1.2,
209 { 0.8, 0.2, 0.2, 0.5 }
212 "tex2d unscaled #2",
213 1.8, 1.2,
214 { 0.2, 0.2, 0.8, 0.5 }
217 "tex2d unscaled #3",
218 1.8, 1.8,
219 { 0.2, 0.8, 0.8, 0.5 }
222 "tex2d unscaled #4",
223 1.2, 1.8,
224 { 0.8, 0.8, 0.2, 0.5 }
228 static bool
229 DoTest(void)
231 int i;
232 bool pass = true;
234 for (i = 0; i < ARRAY_SIZE(Probes); i++) {
235 printf("Testing: %s\n", Probes[i].name);
236 pass = piglit_probe_pixel_rgba(Probes[i].x * piglit_width / 2,
237 Probes[i].y * piglit_height / 2,
238 Probes[i].expected) && pass;
241 return pass;
245 enum piglit_result
246 piglit_display(void)
248 int pass;
250 DoFrame();
251 pass = DoTest();
253 if (!piglit_automatic)
254 piglit_present_results();
255 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
259 static void Reshape(int width, int height)
261 piglit_width = width;
262 piglit_height = height;
263 glViewport(0, 0, width, height);
264 glMatrixMode(GL_PROJECTION);
265 glLoadIdentity();
266 glOrtho(0.0, 2.0, 0.0, 2.0, -2.0, 6.0);
267 glScalef(1.0, 1.0, -1.0); // flip z-axis
268 glMatrixMode(GL_MODELVIEW);
269 glLoadIdentity();
272 void
273 piglit_init(int argc, char **argv)
275 int i, x, y;
276 GLubyte rectangle[200][200][4];
277 GLubyte tex[256*256][4];
278 GLuint texobj[2];
280 /* Need GL 1.4 for GL_GENERATE_MIPMAP tex param */
281 piglit_require_gl_version(14);
283 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
285 piglit_require_fragment_program();
287 piglit_tolerance[0] = 0.02;
288 piglit_tolerance[1] = 0.02;
289 piglit_tolerance[2] = 0.02;
290 piglit_tolerance[3] = 0.02;
292 for(i = 0; i < NUM_PROGRAMS; ++i)
293 FragProg[i] = piglit_compile_program(GL_FRAGMENT_PROGRAM_ARB, ProgramText[i]);
296 * Texture unit 0: 200x200 RECTANGLE texture
298 for(y = 0; y < 200; ++y) {
299 for(x = 0; x < 200; ++x) {
300 rectangle[y][x][0] = 255-x;
301 rectangle[y][x][1] = x;
302 rectangle[y][x][2] = y;
303 rectangle[y][x][3] = (x+y)/2;
307 glActiveTexture(GL_TEXTURE0);
308 glGenTextures(2, texobj);
309 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texobj[0]);
310 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 200, 200, 0,
311 GL_RGBA, GL_UNSIGNED_BYTE, rectangle);
314 * Texture unit 1: 256x256 2D texture
316 for(y = 0; y < 256; ++y) {
317 for(x = 0; x < 256; ++x) {
318 tex[256*y+x][0] = 255-x;
319 tex[256*y+x][1] = y;
320 tex[256*y+x][2] = x;
321 tex[256*y+x][3] = (x+y)/2;
325 glActiveTexture(GL_TEXTURE1);
326 glBindTexture(GL_TEXTURE_2D, texobj[1]);
327 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
328 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0,
329 GL_RGBA, GL_UNSIGNED_BYTE, tex);
331 // Overwrite higher mipmap levels
332 for(x = 0; x < 4; ++x) {
333 tex[x][0] = 255;
334 tex[x][1] = 128;
335 tex[x][2] = 128;
336 tex[x][3] = 255;
339 glTexImage2D(GL_TEXTURE_2D, 7, GL_RGBA, 2, 2, 0,
340 GL_RGBA, GL_UNSIGNED_BYTE, tex);
341 glTexImage2D(GL_TEXTURE_2D, 8, GL_RGBA, 1, 1, 0,
342 GL_RGBA, GL_UNSIGNED_BYTE, tex);
344 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
345 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
347 Reshape(piglit_width, piglit_height);