summaryrefslogtreecommitdiff
path: root/tests/bugs/fdo10370.c
blob: 08788d85d44c7ef30a1defbead6d1cda46c17200 (plain)
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
28
29
30
31
32
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
/*
 * Test case from fdo bug #10370
 * http://bugs.freedesktop.org/show_bug.cgi?id=10370
 */

#if defined(__APPLE__)
#include <GLUT/glut.h>
#else
#include "GL/glut.h"
#endif
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

static int Automatic = 0;

#define WIN_WIDTH 128
#define WIN_HEIGHT 128
#define BITMAP_WIDTH 1
#define BITMAP_HEIGHT 1
#define ALIGN 1
GLfloat read_buf[4 * BITMAP_WIDTH * BITMAP_HEIGHT];
static GLfloat r_map[] = { 0, 1 };
static GLfloat g_map[] = { 0, 0 };
static GLfloat b_map[] = { 1, 0 };
static GLfloat a_map[] = { 1, 1 };
static GLubyte data[] = { 0x8f, 0xff, 0x7f, 0x70 };

static GLuint tex_name;

void init()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glTranslatef(-1.0, -1.0, 0.0);
	glScalef(2.0/WIN_WIDTH, 2.0/WIN_HEIGHT, 1.0);

	glDisable(GL_DITHER);
	glClearColor(1, 1, 1, 1);
	glBlendFunc(GL_SRC_ALPHA, GL_ZERO);

	glPixelMapfv(GL_PIXEL_MAP_I_TO_R, 2, r_map);
	glPixelMapfv(GL_PIXEL_MAP_I_TO_G, 2, g_map);
	glPixelMapfv(GL_PIXEL_MAP_I_TO_B, 2, b_map);
	glPixelMapfv(GL_PIXEL_MAP_I_TO_A, 2, a_map);

	glPixelTransferi(GL_MAP_COLOR, GL_FALSE);

	glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
	glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
	glPixelStorei(GL_UNPACK_ALIGNMENT, ALIGN);

	glGenTextures(1, &tex_name);
	glBindTexture(GL_TEXTURE_2D, tex_name);

	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}



void display()
{
	int i, j, k, col, pixel;
	GLfloat expected[4];
	float dmax = 0.0;

	memset(read_buf, 0xff, sizeof(read_buf));	//reset

	for (k = 0; k < (sizeof(data)/sizeof(GLubyte)); k ++) {

		glClear(GL_COLOR_BUFFER_BIT);
		glNewList(1, GL_COMPILE_AND_EXECUTE);
			glEnable(GL_TEXTURE_2D);
			glBindTexture(GL_TEXTURE_2D, tex_name);

			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
				BITMAP_WIDTH, BITMAP_HEIGHT, 0,
				GL_COLOR_INDEX, GL_BITMAP, &data[k]);

			glBegin(GL_POLYGON);
			glTexCoord2f(0,0); glVertex2f(0, 0);
			glTexCoord2f(1,0); glVertex2f(BITMAP_WIDTH, 0);
			glTexCoord2f(1,1); glVertex2f(BITMAP_WIDTH, BITMAP_HEIGHT);
			glTexCoord2f(0,1); glVertex2f(0, BITMAP_HEIGHT);
			glEnd();
			glDisable(GL_TEXTURE_2D);
		glEndList();
		glFlush();

		glReadPixels(0, 0, BITMAP_WIDTH, BITMAP_HEIGHT,
			GL_RGBA, GL_FLOAT, read_buf);

		printf("data[0x%x], ", data[k]);
		if (data[k] & 0x80) {
			printf("foreground: expected RGBA (%1.1f, %1.1f %1.1f %1.1f)\n",
				r_map[1], g_map[1], b_map[1], a_map[1]);
			expected[0] = r_map[1];
			expected[1] = g_map[1];
			expected[2] = b_map[1];
			expected[3] = a_map[1];
		} else {
			printf("background: expected RGBA (%1.1f, %1.1f %1.1f %1.1f)\n",
				r_map[0], g_map[0], b_map[0], a_map[0]);
			expected[0] = r_map[0];
			expected[1] = g_map[0];
			expected[2] = b_map[0];
			expected[3] = a_map[0];
		}

		printf("First execution, Readback RGBA:\n");
		for (i = 0; i < BITMAP_HEIGHT; i ++) {
			for (j = 0; j < BITMAP_WIDTH; j ++) {
				pixel = j + i*BITMAP_WIDTH;
				printf("pixel[%d, %d]: %1.1f %1.1f %1.1f %1.1f\n", j, i,
					read_buf[pixel*4], read_buf[pixel*4+1],
					read_buf[pixel*4+2], read_buf[pixel*4+3]);

				for(col = 0; col < 4; ++col) {
					float delta = read_buf[pixel*4+col] - expected[col];
					if (delta > dmax) dmax = delta;
					else if (-delta > dmax) dmax = -delta;
				}
			}
		}

		/* 2nd time execution from call list */
		glCallList(1);
		glDeleteLists(1,1);

		memset(read_buf, 0xff, sizeof(read_buf));	//reset
		glReadPixels(0, 0, BITMAP_WIDTH, BITMAP_HEIGHT,
			GL_RGBA, GL_FLOAT, read_buf);

		printf("CallList execution, Readback RGBA:\n");
		for (i = 0; i < BITMAP_HEIGHT; i ++) {
			for (j = 0; j < BITMAP_WIDTH; j ++) {
				pixel = j + i*BITMAP_WIDTH;
				printf("pixel[%d, %d]: %1.1f %1.1f %1.1f %1.1f\n", j, i,
					read_buf[pixel*4], read_buf[pixel*4+1],
					read_buf[pixel*4+2], read_buf[pixel*4+3]);
				for(col = 0; col < 4; ++col) {
					float delta = read_buf[pixel*4+col] - expected[col];
					if (delta > dmax) dmax = delta;
					else if (-delta > dmax) dmax = -delta;
				}
			}
		}
		printf("------------------------------------\n");
	}	//end for(k)

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

	if (Automatic) {
		if (dmax > 0.02)
			printf("PIGLIT: {'result': 'fail' }\n");
		else
			printf("PIGLIT: {'result': 'pass' }\n");

		exit(0);
	}
}


int main(int argc, char**argv)
{
	glutInit(&argc, argv);
	if (argc == 2 && !strcmp(argv[1], "-auto"))
		Automatic = 1;
	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize (WIN_WIDTH, WIN_HEIGHT);
	glutInitWindowPosition (100, 100);
	glutCreateWindow ("fdo10370");
	init();
	glutDisplayFunc(display);
	glutMainLoop();

	return 0;
}