summaryrefslogtreecommitdiff
path: root/tests/texturing/tex-srgb.c
blob: 1f5350837f61521433ce1c42156fc1a6bf50279d (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
/*
 * Copyright © 2010 Red Hat
 *
 * 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
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 *
 * Authors:
 *    Dave Airlie
 *
 */

/** @file tex-srgb
 *
 * Check srgb texturing and EXT_texture_sRGB_decode
 */

#include "piglit-util.h"

int piglit_width = 128, piglit_height = 128;
int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;

#define SIZE 128

// Convert an 8-bit sRGB value from non-linear space to a
// linear RGB value in [0, 1].
// Implemented with a 256-entry lookup table.
static float
nonlinear_to_linear(GLubyte cs8)
{
	static GLfloat table[256];
	static GLboolean tableReady = GL_FALSE;
	if (!tableReady) {
		// compute lookup table now
		GLuint i;
		for (i = 0; i < 256; i++) {
			const GLfloat cs = i / 255.0;
			if (cs <= 0.04045) {
				table[i] = cs / 12.92;
			}
			else {
				table[i] = pow((cs + 0.055) / 1.055, 2.4);
			}
		}
		tableReady = GL_TRUE;
	}
	return table[cs8];
}

static void fill_level(int level, const GLfloat *color)
{
        GLfloat *data;
        int size = SIZE / (1 << level);
        int i;

        /* Update a square inside the texture to red */
        data = malloc(size * size * 4 * sizeof(GLfloat));
        for (i = 0; i < 4 * size * size; i += 4) {
                data[i + 0] = color[0];
                data[i + 1] = color[1];
                data[i + 2] = color[2];
                data[i + 3] = color[3];
        }
        glTexImage2D(GL_TEXTURE_2D, level, GL_SRGB8_ALPHA8, size, size, 0,
                     GL_RGBA, GL_FLOAT, data);
        free(data);
}

static GLboolean
srgb_tex_test(int srgb_format)
{
	GLboolean pass = GL_TRUE;
	float green[] = {0, 0.3, 0.0, 0};
	float expected_green[4];
	float expected_srgb_green[4];
	GLuint tex;
	GLboolean have_decode;

	have_decode = glutExtensionSupported("GL_EXT_texture_sRGB_decode");

	glGenTextures(1, &tex);

	glBindTexture(GL_TEXTURE_2D, tex);

	fill_level(0, green);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
			GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
			GL_NEAREST);

	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glEnable(GL_TEXTURE_2D);
	piglit_draw_rect_tex(0, 0, 20, 20, 0, 0, 1, 1);

	if (have_decode) {
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SRGB_DECODE_EXT,
				GL_SKIP_DECODE_EXT);

		piglit_draw_rect_tex(20, 0, 20, 20, 0, 0, 1, 1);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SRGB_DECODE_EXT,
				GL_DECODE_EXT);

		piglit_draw_rect_tex(40, 0, 20, 20, 0, 0, 1, 1);
	}

	memcpy(expected_green, green, sizeof(float) * 4);
	memcpy(expected_srgb_green, green, sizeof(float) * 4);
	expected_srgb_green[1] = nonlinear_to_linear(255.0*green[1]);

	if (!piglit_probe_rect_rgb(0, 0, 20, 20, expected_srgb_green))
		pass = GL_FALSE;

	if (have_decode) {

		if (!piglit_probe_rect_rgb(20, 0, 20, 20, expected_green))
			pass = GL_FALSE;

		if (!piglit_probe_rect_rgb(40, 0, 20, 20, expected_srgb_green))
			pass = GL_FALSE;
	}


	glDeleteTextures(1, &tex);
	glutSwapBuffers();

	return pass;
}

enum piglit_result
piglit_display(void)
{
	GLboolean pass = GL_TRUE;

	pass = srgb_tex_test(0);
	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}


static void reshape(int width, int height)
{
	piglit_width = width;
	piglit_height = height;

	piglit_ortho_projection(width, height, GL_FALSE);
}

void
piglit_init(int argc, char **argv)
{
	piglit_require_extension("GL_EXT_texture_sRGB");
	reshape(piglit_width, piglit_height);
}