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
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.
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
31 * s t r q [input texture coordinates]
34 * r g b a [color of texture images]
44 #include "piglit-util.h"
47 ================================================================
64 struct testinstance
* instances
;
67 static void expect(FILE* filp
, const char* str
)
70 fscanf(filp
, "%40s", buf
);
71 if (strcmp(buf
, str
)) {
72 fprintf(stderr
, "Expected '%s', got '%s'\n", str
, buf
);
77 static GLfloat
* readfloatarray(FILE* filp
, int count
)
79 GLfloat
* dest
= (GLfloat
*)malloc(sizeof(GLfloat
)*count
);
82 for(i
= 0; i
< count
; ++i
)
83 fscanf(filp
, "%f", &dest
[i
]);
88 static void readTestcase(struct testcase
* tc
, const char* filename
)
90 FILE* filp
= fopen(filename
, "rt");
95 fprintf(stderr
, "Failed to read test data: %s\n", filename
);
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
;
106 inst
->texcoords
= readfloatarray(filp
, tc
->nrTexCoords
*4);
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
)) {
121 if (!*tc
->programtext
&& buf
[0] != '!')
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))
136 ================================================================
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
)
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
);
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
);
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
);
206 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
207 glMatrixMode(GL_MODELVIEW
);
213 piglit_init(int argc
, char **argv
)
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
) {
229 fprintf(stderr
, "Need to give a testcase file\n");
230 printf("PIGLIT: {'result': 'fail' }\n");
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
);