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
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
217
218
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
/*
* 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 framebuffer-srgb
*
* Test the EXT_framebuffer_sRGB API changes
* this test enables even when the extension isn't available, and confirms
* the API acts correctly in that case.
*/
#include "piglit-util-gl-common.h"
PIGLIT_GL_TEST_CONFIG_BEGIN
config.window_width = 128;
config.window_height = 128;
config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
PIGLIT_GL_TEST_CONFIG_END
/* size of texture/renderbuffer (power of two) */
#define FBO_SIZE 128
/* do some basic API tests */
static GLboolean
framebuffer_srgb_api_no_ext(void)
{
GLboolean pass = GL_TRUE;
GLboolean boolmode = GL_FALSE;
GLenum ret;
glGetBooleanv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &boolmode);
ret = glGetError();
if (ret != GL_INVALID_ENUM || boolmode) {
printf("no extension but no enum error or enabled\n");
pass = GL_FALSE;
}
(void)glIsEnabled(GL_FRAMEBUFFER_SRGB_EXT);
ret = glGetError();
if (ret != GL_INVALID_ENUM) {
printf("no invalid enum on glIsEnabled(GL_FRAMEBUFFER_SRGB_EXT)\n");
pass = GL_FALSE;
}
glEnable(GL_FRAMEBUFFER_SRGB_EXT);
ret = glGetError();
if (ret != GL_INVALID_ENUM) {
printf("no invalid enum on glEnable(GL_FRAMEBUFFER_SRGB_EXT)\n");
pass = GL_FALSE;
}
glDisable(GL_FRAMEBUFFER_SRGB_EXT);
ret = glGetError();
if (ret != GL_INVALID_ENUM) {
printf("no invalid enum on glDisable(GL_FRAMEBUFFER_SRGB_EXT)\n");
pass = GL_FALSE;
}
return pass;
}
/* do some basic API tests */
static GLboolean
framebuffer_srgb_api_ext(void)
{
GLboolean pass = GL_TRUE;
GLboolean boolmode;
GLenum ret;
GLboolean is_enabled;
/* check if visual supports API */
glGetBooleanv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &boolmode);
ret = glGetError();
if (ret != 0) {
printf("unexpected error glGetBooleanv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT) %d\n", ret);
pass = GL_FALSE;
}
is_enabled = glIsEnabled(GL_FRAMEBUFFER_SRGB_EXT);
ret = glGetError();
if (ret != 0) {
printf("unexpected error getting IsEnabled %d\n", ret);
pass = GL_FALSE;
}
/* should be possible to enable/disable independent of capable bit */
glEnable(GL_FRAMEBUFFER_SRGB_EXT);
is_enabled = glIsEnabled(GL_FRAMEBUFFER_SRGB_EXT);
if (!is_enabled) {
printf("SRGB not enabled after calling glEnable\n");
pass = GL_FALSE;
}
glDisable(GL_FRAMEBUFFER_SRGB_EXT);
is_enabled = glIsEnabled(GL_FRAMEBUFFER_SRGB_EXT);
if (is_enabled) {
printf("SRGB not disabled after calling glDisable\n");
pass = GL_FALSE;
}
return pass;
}
static GLfloat
linear_to_nonlinear(GLfloat cl)
{
/* can't have values outside [0, 1] */
GLfloat cs;
if (cl < 0.0031308f) {
cs = 12.92f * cl;
}
else {
cs = (GLfloat)(1.055 * pow(cl, 0.41666) - 0.055);
}
return cs;
}
static GLboolean
framebuffer_srgb_non_fbo(void)
{
GLboolean pass = GL_TRUE;
GLboolean boolmode;
float green[] = {0, 0.3, 0, 0};
float expected_green[4];
float expected_blend[4];
glGetBooleanv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &boolmode);
/* drawing test */
/* draw two green squares without sRGB */
glDisable(GL_BLEND);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor4fv(green);
piglit_draw_rect(0, 0, 20, 20);
/* draw a green square with sRGB enabled and no blending */
glEnable(GL_FRAMEBUFFER_SRGB_EXT);
piglit_draw_rect(30, 30, 20, 20);
/* enable blending and draw a green square with sRGB enabled on top of the previous green square */
piglit_draw_rect(20, 0, 20, 20);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
piglit_draw_rect(20, 0, 20, 20);
glDisable(GL_FRAMEBUFFER_SRGB_EXT);
if (!piglit_probe_rect_rgb(0, 0, 20, 20, green))
pass = GL_FALSE;
/* check pixel path */
glEnable(GL_FRAMEBUFFER_SRGB_EXT);
if (!piglit_probe_rect_rgb(0, 0, 20, 20, green))
pass = GL_FALSE;
glDisable(GL_FRAMEBUFFER_SRGB_EXT);
memcpy(expected_green, green, sizeof(float) * 4);
if (boolmode)
expected_green[1] = linear_to_nonlinear(green[1]);
memcpy(expected_blend, green, sizeof(float) * 4);
if (boolmode)
expected_blend[1] = linear_to_nonlinear(green[1] * 2.0);
else
expected_blend[1] = green[1] * 2.0;
if (!piglit_probe_rect_rgb(20, 0, 20, 20, expected_blend))
pass = GL_FALSE;
if (!piglit_probe_rect_rgb(30, 30, 20, 20, expected_green))
pass = GL_FALSE;
piglit_present_results();
return pass;
}
static GLuint
make_fbo(int w, int h)
{
GLuint tex;
GLuint fb;
GLenum status;
glGenFramebuffersEXT(1, &fb);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8_EXT,
w, h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D,
tex,
0);
assert(glGetError() == 0);
status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
fprintf(stderr, "fbo incomplete (status = 0x%04x)\n", status);
piglit_report_result(PIGLIT_SKIP);
}
return fb;
}
static void
draw_fbo(int x, int y)
{
glViewport(0, 0, piglit_width, piglit_height);
piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glEnable(GL_TEXTURE_2D);
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
piglit_draw_rect_tex(x, y, FBO_SIZE, FBO_SIZE,
0, 0, 1, 1);
glDisable(GL_TEXTURE_2D);
}
static GLboolean
framebuffer_srgb_fbo(void)
{
GLboolean pass = GL_TRUE;
int fbo_width = FBO_SIZE;
int fbo_height = FBO_SIZE;
GLuint fbo;
GLboolean boolmode;
float green[] = {0, 0.3, 0.0, 0};
float expected_green[4];
float expected_blend[4];
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
fbo = make_fbo(fbo_width, fbo_height);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, fbo);
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, fbo);
glGetBooleanv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &boolmode);
assert(glGetError() == 0);
glViewport(0, 0, fbo_width, fbo_height);
piglit_ortho_projection(fbo_width, fbo_height, GL_FALSE);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor4fv(green);
piglit_draw_rect(0, 0, 20, 20);
/* draw a green square with sRGB enabled and no blending */
glEnable(GL_FRAMEBUFFER_SRGB_EXT);
piglit_draw_rect(30, 30, 20, 20);
/* enable blending and draw a green square with sRGB enabled on top of the previous green square */
piglit_draw_rect(20, 0, 20, 20);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
piglit_draw_rect(20, 0, 20, 20);
glDisable(GL_BLEND);
glDisable(GL_FRAMEBUFFER_SRGB_EXT);
if (!piglit_probe_rect_rgb(0, 0, 20, 20, green))
pass = GL_FALSE;
memcpy(expected_green, green, sizeof(float) * 4);
if (boolmode)
expected_green[1] = linear_to_nonlinear(green[1]);
if (!piglit_probe_rect_rgb(30, 30, 20, 20, expected_green))
pass = GL_FALSE;
/* check it doesn't affect the pixel path */
glEnable(GL_FRAMEBUFFER_SRGB_EXT);
if (!piglit_probe_rect_rgb(30, 30, 20, 20, expected_green))
pass = GL_FALSE;
glDisable(GL_FRAMEBUFFER_SRGB_EXT);
memcpy(expected_blend, green, sizeof(float) * 4);
if (boolmode)
expected_blend[1] = linear_to_nonlinear(green[1] * 2.0);
else
expected_blend[1] = green[1] * 2.0;
if (!piglit_probe_rect_rgb(20, 0, 20, 20, expected_blend))
pass = GL_FALSE;
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
draw_fbo(0, 0);
piglit_present_results();
glDeleteFramebuffersEXT(1, &fbo);
return pass;
}
enum piglit_result
piglit_display(void)
{
GLboolean pass = GL_TRUE;
GLboolean have_srgb_ext = GL_FALSE;
if (piglit_is_extension_supported("GL_EXT_framebuffer_sRGB"))
have_srgb_ext = GL_TRUE;
if (!have_srgb_ext) {
pass = framebuffer_srgb_api_no_ext();
goto out;
}
pass = framebuffer_srgb_api_ext();
if (pass == GL_TRUE)
pass = framebuffer_srgb_non_fbo();
if (pass == GL_TRUE)
pass = framebuffer_srgb_fbo();
out:
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)
{
reshape(piglit_width, piglit_height);
}
|