Add more structure constructor tests.
[piglit/hramrach.git] / tests / shaders / trinity-fp1.c
blob639a38509b6dcf58c6b5596a3eeebb8697fb3f66
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 a fragment program that
26 * \sa http://www.mail-archive.com/dri-devel%40lists.sourceforge.net/msg30180.html
29 #include "piglit-util.h"
31 int piglit_width = 200, piglit_height = 100;
32 int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH;
34 static GLuint TexDiffuse = 1;
35 static GLuint TexNormal = 2;
36 static GLuint TexSpecular = 3;
37 static GLuint TexLookup = 4;
39 static GLuint FragProg;
41 static void DoFrame(void)
43 static float Local[3][4] = {
44 { 1.0, 0.8, 1.0, 1.0 },
45 { 0.5, 0.5, 0.5, 1.0 },
46 { 1.0, 0.0, 0.0, 1.0 }
48 static float Local2[3][4] = {
49 { 0.8, 1.0, 1.0, 1.0 },
50 { 0.5, 0.5, 0.5, 1.0 },
51 { 1.0, 0.0, 1.0, 1.0 }
53 int i;
55 glClearColor(0.8, 0.8, 0.8, 0.8);
56 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
58 glActiveTexture(GL_TEXTURE0);
59 glBindTexture(GL_TEXTURE_2D, TexDiffuse);
60 glEnable(GL_TEXTURE_2D);
62 glActiveTexture(GL_TEXTURE1);
63 glBindTexture(GL_TEXTURE_2D, TexNormal);
64 glEnable(GL_TEXTURE_2D);
66 glActiveTexture(GL_TEXTURE2);
67 glBindTexture(GL_TEXTURE_2D, TexSpecular);
68 glEnable(GL_TEXTURE_2D);
70 glActiveTexture(GL_TEXTURE3);
71 glBindTexture(GL_TEXTURE_2D, TexLookup);
72 glEnable(GL_TEXTURE_2D);
74 glActiveTexture(GL_TEXTURE4);
75 glBindTexture(GL_TEXTURE_2D, TexLookup);
76 glEnable(GL_TEXTURE_2D);
78 glActiveTexture(GL_TEXTURE5);
79 glBindTexture(GL_TEXTURE_2D, TexLookup);
80 glEnable(GL_TEXTURE_2D);
82 glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);
83 glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0);
84 glMultiTexCoord2f(GL_TEXTURE2, 0.0, 0.0);
85 glMultiTexCoord3f(GL_TEXTURE3, 0.0, 0.05, 0.25);
86 glMultiTexCoord3f(GL_TEXTURE4, 4, -3, 0);
87 glMultiTexCoord3f(GL_TEXTURE5, 0, 3, 4);
89 glEnable(GL_FRAGMENT_PROGRAM_ARB);
90 for(i = 0; i < 3; ++i)
91 glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, Local[i]);
93 glBegin(GL_QUADS);
94 glVertex2f(0.75, 0.75);
95 glVertex2f(0.25, 0.75);
96 glVertex2f(0.25, 0.25);
97 glVertex2f(0.75, 0.25);
98 glEnd();
100 glEnable(GL_FRAGMENT_PROGRAM_ARB);
101 for(i = 0; i < 3; ++i)
102 glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, Local2[i]);
104 glBegin(GL_QUADS);
105 glVertex2f(1.75, 0.75);
106 glVertex2f(1.25, 0.75);
107 glVertex2f(1.25, 0.25);
108 glVertex2f(1.75, 0.25);
109 glEnd();
111 glutSwapBuffers();
114 static int DoTest( void )
116 static const float expected[2][3] = {
117 { 0.30, 0.23, 0.40 },
118 { 0.24, 0.29, 0.40 }
120 int i;
121 GLfloat dmax = 0;
123 glReadBuffer( GL_FRONT );
125 for(i = 0; i < 2; ++i) {
126 GLfloat probe[4];
127 GLfloat delta[3];
128 int j;
130 glReadPixels(piglit_width*(2*i+1)/4, piglit_height/2, 1, 1, GL_RGBA, GL_FLOAT, probe);
131 printf("Probe: %f,%f,%f\n", probe[0], probe[1], probe[2]);
133 for(j = 0; j < 3; ++j) {
134 delta[j] = probe[j] - expected[i][j];
135 if (delta[j] > dmax) dmax = delta[j];
136 else if (-delta[j] > dmax) dmax = -delta[j];
139 printf(" Delta: %f,%f,%f\n", delta[0], delta[1], delta[2]);
142 printf("Max delta: %f\n", dmax);
144 if (dmax >= 0.02)
145 return 0;
146 else
147 return 1;
151 enum piglit_result
152 piglit_display(void)
154 int succ;
156 DoFrame();
157 succ = DoTest();
159 return succ ? PIGLIT_SUCCESS : PIGLIT_FAILURE;
163 static void Reshape(int width, int height)
165 piglit_width = width;
166 piglit_height = height;
167 glViewport(0, 0, width, height);
168 glMatrixMode(GL_PROJECTION);
169 glLoadIdentity();
170 glOrtho(0.0, 2.0, 0.0, 1.0, -1.0, 1.0);
171 glMatrixMode(GL_MODELVIEW);
172 glLoadIdentity();
175 void
176 piglit_init(int argc, char **argv)
178 GLubyte data[256][256][4];
179 int x,y;
181 static const char *fragProgramText =
182 "!!ARBfp1.0\n"
183 "# $Id$\n"
184 "# Copyright (C) 2006 Oliver McFadden <z3ro.geek@gmail.com>\n"
185 "#\n"
186 "# This program is free software; you can redistribute it and/or modify\n"
187 "# it under the terms of the GNU General Public License as published by\n"
188 "# the Free Software Foundation; either version 2 of the License, or\n"
189 "# (at your option) any later version.\n"
190 "#\n"
191 "# This program is distributed in the hope that it will be useful,\n"
192 "# but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
193 "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
194 "# GNU General Public License for more details.\n"
195 "#\n"
196 "# You should have received a copy of the GNU General Public License\n"
197 "# along with this program; if not, write to the Free Software\n"
198 "# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n"
200 "TEMP H, L, N, V, attenuationxy, attenuationz, color, diffuse, dot, specular, tmp;\n"
202 "DP3 L.x, fragment.texcoord[4], fragment.texcoord[4];\n"
203 "RSQ L.x, L.x;\n"
204 "MUL L.xyz, L.x, fragment.texcoord[4];\n"
206 "DP3 V.x, fragment.texcoord[5], fragment.texcoord[5];\n"
207 "RSQ V.x, V.x;\n"
208 "MUL V.xyz, V.x, fragment.texcoord[5];\n"
210 "ADD tmp, L, V;\n"
211 "DP3 H.x, tmp, tmp;\n"
212 "RSQ H.x, H.x;\n"
213 "MUL H.xyz, H.x, tmp;\n"
215 "TEX tmp.xyz, fragment.texcoord[1], texture[1], 2D;\n"
216 "MAD tmp.xyz, tmp, 2.0, -1.0;\n"
217 "DP3 N.x, tmp, tmp;\n"
218 "RSQ N.x, N.x;\n"
219 "MUL N.xyz, N.x, tmp;\n"
221 "DP3_SAT dot.x, N, L;\n"
222 "MUL dot.xyz, program.local[0], dot.x;\n"
224 "TEX diffuse.xyz, fragment.texcoord[0], texture[0], 2D;\n"
226 "DP3_SAT tmp.x, N, H;\n"
227 "POW tmp.x, tmp.x, program.local[2].x;\n"
228 "TEX specular.xyz, fragment.texcoord[2], texture[2], 2D;\n"
229 "MUL specular.xyz, specular, program.local[0];\n"
230 "MUL specular.xyz, specular, tmp.x;\n"
232 "TEX attenuationxy.xyz, fragment.texcoord[3], texture[3], 2D;\n"
234 "MOV tmp.x, fragment.texcoord[3].z;\n"
235 "MOV tmp.y, 0;\n"
236 "TEX attenuationz.xyz, tmp, texture[4], 2D;\n"
238 "MOV color, diffuse;\n"
239 "MUL color.xyz, color, dot;\n"
240 "ADD color.xyz, color, specular;\n"
241 "MUL color.xyz, color, attenuationxy;\n"
242 "MUL color.xyz, color, attenuationz;\n"
243 "MUL color.xyz, color, program.local[1].x;\n"
244 "MOV result.color, color;\n"
246 "END";
248 if (!GLEW_VERSION_1_3) {
249 printf("Requires OpenGL 1.3\n");
250 piglit_report_result(PIGLIT_SKIP);
253 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
255 piglit_require_fragment_program();
256 FragProg = piglit_compile_program(GL_FRAGMENT_PROGRAM_ARB, fragProgramText);
259 * Initialize textures
261 // Diffuse
262 for(y = 0; y < 256; ++y) {
263 for(x = 0; x < 256; ++x) {
264 data[y][x][0] = 255; // 1.0
265 data[y][x][1] = 192; // 0.75
266 data[y][x][2] = 255; // 1.0
267 data[y][x][3] = 0;
271 glBindTexture(GL_TEXTURE_2D, TexDiffuse);
272 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
273 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
274 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
276 // Normal
277 for(y = 0; y < 256; ++y) {
278 for(x = 0; x < 256; ++x) {
279 data[y][x][0] = 255; // 1.0
280 data[y][x][1] = 0; // 0.0
281 data[y][x][2] = 0; // 0.0
282 data[y][x][3] = 0;
286 glBindTexture(GL_TEXTURE_2D, TexNormal);
287 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
288 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
289 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
291 // Specular
292 for(y = 0; y < 256; ++y) {
293 for(x = 0; x < 256; ++x) {
294 data[y][x][0] = 255; // 1.0
295 data[y][x][1] = 255; // 1.0
296 data[y][x][2] = 192; // 0.75
297 data[y][x][3] = 0;
301 glBindTexture(GL_TEXTURE_2D, TexSpecular);
302 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
303 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
304 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
306 // Lookup texture
307 for(y = 0; y < 256; ++y) {
308 for(x = 0; x < 256; ++x) {
309 data[y][x][0] = 255-x;
310 data[y][x][1] = 255-y;
311 data[y][x][2] = 255;
312 data[y][x][3] = 0;
316 glBindTexture(GL_TEXTURE_2D, TexLookup);
317 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
318 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
319 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
321 Reshape(piglit_width, piglit_height);