fp-kil.c 6.08 KB
Newer Older
Nicolai Hähnle's avatar
Nicolai Hähnle committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright (c) The Piglit project 2007
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * Test KIL instruction.
 */

28
#include "piglit-util.h"
Nicolai Hähnle's avatar
Nicolai Hähnle committed
29

30 31 32
int piglit_width = 200, piglit_height = 200;
int piglit_window_mode = GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH;

Nicolai Hähnle's avatar
Nicolai Hähnle committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#define NUM_PROGRAMS 2

static GLuint FragProg[NUM_PROGRAMS];

static const char* const ProgramText[NUM_PROGRAMS] = {
		"!!ARBfp1.0\n"
		"TEMP r0;\n"
		"MOV result.color, fragment.color;\n"
		"KIL fragment.texcoord[0];\n"
		"END",

		"!!ARBfp1.0\n"
		"TEMP r0;\n"
		"TEX r0, fragment.texcoord[0], texture[0], 2D;\n"
		"KIL -r0;\n"
		"MOV result.color, fragment.color;\n"
		"END"
};

static void DoFrame(void)
{
	glClearColor(0.0, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glEnable(GL_FRAGMENT_PROGRAM_ARB);

59
	glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, FragProg[0]);
Nicolai Hähnle's avatar
Nicolai Hähnle committed
60 61 62 63 64 65 66 67 68 69 70 71 72

	glColor3f(0.0, 1.0, 0.0);
	glBegin(GL_QUADS);
		glTexCoord2f(-1, -1);
		glVertex2f(0, 0);
		glTexCoord2f(1, -1);
		glVertex2f(1, 0);
		glTexCoord2f(1, 1);
		glVertex2f(1, 1);
		glTexCoord2f(-1, 1);
		glVertex2f(0, 1);
	glEnd();

73
	glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, FragProg[1]);
Nicolai Hähnle's avatar
Nicolai Hähnle committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

	glBegin(GL_QUADS);
		glTexCoord2f(0, 0);
		glVertex2f(0, 1);
		glTexCoord2f(1, 0);
		glVertex2f(1, 1);
		glTexCoord2f(1, 1);
		glVertex2f(1, 2);
		glTexCoord2f(0, 1);
		glVertex2f(0, 2);
	glEnd();
}

static const struct {
	const char* name;
	float x, y;
	float expected[4];
} Probes[] = {
	// Program 0
	{
		"basic #1",
		0.2, 0.2,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"basic #2",
		0.8, 0.2,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"basic #3",
		0.8, 0.8,
		{ 0.0, 1.0, 0.0, 1.0 }
	},
	{
		"basic #4",
		0.2, 0.8,
		{ 0.0, 0.0, 0.0, 1.0 }
	},

	// Program 0
	{
		"texture #1",
		0.125, 1.125,
		{ 0.0, 1.0, 0.0, 1.0 }
	},
	{
		"texture #2",
		0.375, 1.125,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"texture #3",
		0.625, 1.125,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
		{
		"texture #4",
		0.875, 1.125,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"texture #5",
		0.125, 1.375,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"texture #6",
		0.375, 1.375,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"texture #7",
		0.625, 1.375,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"texture #8",
		0.875, 1.375,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"texture #9",
		0.125, 1.625,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"texture #10",
		0.375, 1.625,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"texture #11",
		0.625, 1.625,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"texture #12",
		0.875, 1.625,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"texture #13",
		0.125, 1.875,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"texture #14",
		0.375, 1.875,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"texture #15",
		0.625, 1.875,
		{ 0.0, 0.0, 0.0, 1.0 }
	},
	{
		"texture #16",
		0.875, 1.875,
		{ 0.0, 0.0, 0.0, 1.0 }
	},

	// Sentinel!
	{
		0,
			0, 0,
		{ 0, 0, 0, 0 }
	}
};

static int DoTest( void )
{
	int idx;
	GLfloat dmax;

	dmax = 0;

	idx = 0;
	while(Probes[idx].name) {
		GLfloat probe[4];
		GLfloat delta[4];
		int i;

217 218
		glReadPixels((int)(Probes[idx].x*piglit_width/2),
		             (int)(Probes[idx].y*piglit_height/2),
Nicolai Hähnle's avatar
Nicolai Hähnle committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
		             1, 1,
		             GL_RGBA, GL_FLOAT, probe);

		printf("%20s (%3.1f,%3.1f): %f,%f,%f,%f",
		       Probes[idx].name,
		       Probes[idx].x, Probes[idx].y,
		       probe[0], probe[1], probe[2], probe[3]);

		for(i = 0; i < 4; ++i) {
			delta[i] = probe[i] - Probes[idx].expected[i];

			if (delta[i] > dmax) dmax = delta[i];
			else if (-delta[i] > dmax) dmax = -delta[i];
		}

		printf("   Delta: %f,%f,%f,%f\n", delta[0], delta[1], delta[2], delta[3]);

		idx++;
	}

	printf("Max delta: %f\n", dmax);

	if (dmax >= 0.02)
		return 0;
	else
		return 1;
}

247 248
enum piglit_result
piglit_display(void)
Nicolai Hähnle's avatar
Nicolai Hähnle committed
249
{
250
	int pass;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
251 252

	DoFrame();
253
	pass = DoTest();
254
	glutSwapBuffers();
Nicolai Hähnle's avatar
Nicolai Hähnle committed
255

256
	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
257 258 259 260 261
}


static void Reshape(int width, int height)
{
262 263
	piglit_width = width;
	piglit_height = height;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
264 265 266 267 268 269 270 271 272
	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0, 2.0, 0.0, 2.0, -2.0, 6.0);
	glScalef(1.0, 1.0, -1.0); // flip z-axis
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

273 274
void
piglit_init(int argc, char **argv)
Nicolai Hähnle's avatar
Nicolai Hähnle committed
275 276 277 278
{
	int i, x, y;
	GLubyte tex[4][4][4];

279 280
	glutReshapeFunc(Reshape);

Paul Berry's avatar
Paul Berry committed
281
	if (piglit_get_gl_version() < 13) {
282 283 284 285
		printf("Requires OpenGL 1.3\n");
		piglit_report_result(PIGLIT_SKIP);
	}

Nicolai Hähnle's avatar
Nicolai Hähnle committed
286 287
	printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));

288
	piglit_require_fragment_program();
Nicolai Hähnle's avatar
Nicolai Hähnle committed
289 290 291 292

	/*
	 * Fragment programs
	 */
293 294
	for(i = 0; i < NUM_PROGRAMS; ++i)
		FragProg[i] = piglit_compile_program(GL_FRAGMENT_PROGRAM_ARB, ProgramText[i]);
Nicolai Hähnle's avatar
Nicolai Hähnle committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

	/*
	 * Textures
	 */
	for(y = 0; y < 4; ++y) {
		for(x = 0; x < 4; ++x) {
			tex[y][x][0] = (x & 1) ? 255 : 0;
			tex[y][x][1] = (x & 2) ? 255 : 0;
			tex[y][x][2] = (y & 1) ? 255 : 0;
			tex[y][x][3] = (y & 2) ? 255 : 0;
		}
	}

	glActiveTexture(GL_TEXTURE0);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, 1);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0,
	             GL_RGBA, GL_UNSIGNED_BYTE, tex);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

317
	Reshape(piglit_width, piglit_height);
Nicolai Hähnle's avatar
Nicolai Hähnle committed
318
}