Add more structure constructor tests.
[piglit/hramrach.git] / tests / shaders / fp-generic.c
blob3c806da8ecea24a82dc5507e08316440182fe104
1 /*
2 * Copyright (c) The Piglit project 2008
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 * @file
26 * Generic ARB_fragment_program test, to test ALU correctness.
27 * Takes an input file of the following form:
29 * nr-tests nr-texcoords nr-teximages
30 * tc
31 * s t r q [input texture coordinates]
32 * ...
33 * tex
34 * r g b a [color of texture images]
35 * ...
36 * expected
37 * r g b a
38 * tc
39 * ...
40 * !!ARBfp1.0
41 * ...
44 #include "piglit-util.h"
47 ================================================================
49 Testcase helpers
53 struct testinstance {
54 GLfloat* texcoords;
55 GLfloat* teximages;
56 GLfloat expected[4];
59 struct testcase {
60 char* programtext;
61 int nrTexCoords;
62 int nrTexImages;
63 int nrInstances;
64 struct testinstance* instances;
67 static void expect(FILE* filp, const char* str)
69 char buf[41];
70 fscanf(filp, "%40s", buf);
71 if (strcmp(buf, str)) {
72 fprintf(stderr, "Expected '%s', got '%s'\n", str, buf);
73 exit(-1);
77 static GLfloat* readfloatarray(FILE* filp, int count)
79 GLfloat* dest = (GLfloat*)malloc(sizeof(GLfloat)*count);
80 int i;
82 for(i = 0; i < count; ++i)
83 fscanf(filp, "%f", &dest[i]);
85 return dest;
88 static void readTestcase(struct testcase* tc, const char* filename)
90 FILE* filp = fopen(filename, "rt");
91 char buf[256];
92 int i;
94 if (!filp) {
95 fprintf(stderr, "Failed to read test data: %s\n", filename);
96 exit(-1);
99 fscanf(filp, "%i %i %i", &tc->nrInstances, &tc->nrTexCoords, &tc->nrTexImages);
100 tc->instances = (struct testinstance*)malloc(tc->nrInstances * sizeof(struct testinstance));
102 for(i = 0; i < tc->nrInstances; ++i) {
103 struct testinstance* inst = tc->instances + i;
105 expect(filp, "tc");
106 inst->texcoords = readfloatarray(filp, tc->nrTexCoords*4);
108 expect(filp, "tex");
109 inst->teximages = readfloatarray(filp, tc->nrTexImages*4);
111 expect(filp, "expected");
112 fscanf(filp, "%f %f %f %f",
113 &inst->expected[0], &inst->expected[1],
114 &inst->expected[2], &inst->expected[3]);
117 /* Yeah, this is not especially efficient... */
118 tc->programtext = strdup("");
119 while(fgets(buf, sizeof(buf), filp)) {
120 int newlen;
121 if (!*tc->programtext && buf[0] != '!')
122 continue;
123 newlen = tc->programtext ? strlen(tc->programtext) : 0;
124 newlen += strlen(buf);
125 tc->programtext = (char*)realloc(tc->programtext, newlen+1);
126 strcat(tc->programtext, buf);
127 if (!strncmp(buf, "END", 3))
128 break;
131 fclose(filp);
136 ================================================================
138 GL program
142 int piglit_width = 100, piglit_height = 100;
143 int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA;
145 static const char* Filename = 0;
146 static struct testcase Testcase;
147 static GLuint FragProg;
151 static void TestInstance(struct testinstance* instance)
153 int i;
155 glClearColor(0.0, 0.0, 1.0, 0.0);
156 glClear(GL_COLOR_BUFFER_BIT);
158 glEnable(GL_FRAGMENT_PROGRAM_ARB);
159 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, FragProg);
161 for(i = 0; i < Testcase.nrTexCoords; ++i)
162 glMultiTexCoord4fv(GL_TEXTURE0+i, instance->texcoords + 4*i);
164 for(i = 0; i < Testcase.nrTexImages; ++i) {
165 glActiveTexture(GL_TEXTURE0+i);
166 glBindTexture(GL_TEXTURE_2D, i+1);
167 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_FLOAT, instance->teximages + 4*i);
168 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
169 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
172 glBegin(GL_QUADS);
173 glVertex2f(0, 0);
174 glVertex2f(1, 0);
175 glVertex2f(1, 1);
176 glVertex2f(0, 1);
177 glEnd();
179 glutSwapBuffers();
181 glReadBuffer(GL_FRONT);
182 if (!piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, instance->expected)) {
183 fprintf(stderr, "Test %s, instance #%i failed\n", Filename, instance-Testcase.instances);
184 piglit_report_result(PIGLIT_FAILURE);
188 enum piglit_result
189 piglit_display(void)
191 int i;
192 for(i = 0; i < Testcase.nrInstances; ++i)
193 TestInstance(&Testcase.instances[i]);
195 return PIGLIT_SUCCESS;
199 static void Reshape(int width, int height)
201 piglit_width = width;
202 piglit_height = height;
203 glViewport(0, 0, width, height);
204 glMatrixMode(GL_PROJECTION);
205 glLoadIdentity();
206 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
207 glMatrixMode(GL_MODELVIEW);
208 glLoadIdentity();
212 void
213 piglit_init(int argc, char **argv)
215 int i;
217 piglit_automatic = GL_TRUE;
219 if (!GLEW_VERSION_1_3) {
220 printf("Requires OpenGL 1.3\n");
221 piglit_report_result(PIGLIT_SKIP);
224 for(i = 1; i < argc; ++i) {
225 if (!Filename)
226 Filename = argv[i];
228 if (!Filename) {
229 fprintf(stderr, "Need to give a testcase file\n");
230 printf("PIGLIT: {'result': 'fail' }\n");
231 exit(-1);
233 readTestcase(&Testcase, Filename);
235 glutReshapeFunc(Reshape);
237 piglit_require_fragment_program();
238 FragProg = piglit_compile_program(GL_FRAGMENT_PROGRAM_ARB, Testcase.programtext);
240 Reshape(piglit_width, piglit_height);