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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
|
/*
* Copyright (c) 2011 VMware, Inc.
*
* 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.
*/
/**
* Test rendering to a depth texture, sampling from it, and comparing the
* texture depth values against the fragment Z values when drawing the
* same object a second time.
*
* The left side of the window should be mostly black. Red pixels indicate
* errors.
* The center and right parts of the window should show gray-scale spheres
* on a white background (they're just Z buffer images as gray-scale).
*
* Brian Paul
* 17 Feb 2011
*/
#include <assert.h>
#include "piglit-util-gl-common.h"
/** Set DEBUG to 1 to enable extra output when trying to debug failures */
#define DEBUG 0
#define SIZE 256
PIGLIT_GL_TEST_CONFIG_BEGIN
config.supports_gl_compat_version = 10;
config.window_width = 3*SIZE;
config.window_height = SIZE;
config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_DEPTH;
PIGLIT_GL_TEST_CONFIG_END
static GLfloat ErrorScale = 0.0;
static GLuint ColorTex, DepthTex, FBO;
static GLuint ShaderProg;
static GLint Zbits;
static GLenum TexTarget = GL_TEXTURE_2D;
static void
create_fbo(void)
{
GLenum depthIntFormat = GL_DEPTH_COMPONENT24;
GLenum status;
/* depth texture */
glGenTextures(1, &DepthTex);
glBindTexture(TexTarget, DepthTex);
glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(TexTarget, 0, depthIntFormat,
SIZE, SIZE, 0,
GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
assert(glGetError() == 0);
glGetTexLevelParameteriv(TexTarget, 0, GL_TEXTURE_DEPTH_SIZE, &Zbits);
/* color texture */
glGenTextures(1, &ColorTex);
glBindTexture(TexTarget, ColorTex);
glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(TexTarget, 0, GL_RGBA,
SIZE, SIZE, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
assert(glGetError() == 0);
/* Create FBO */
glGenFramebuffersEXT(1, &FBO);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
TexTarget,
ColorTex,
0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
GL_DEPTH_ATTACHMENT_EXT,
TexTarget,
DepthTex,
0);
assert(glGetError() == 0);
status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
piglit_report_result(PIGLIT_SKIP);
}
}
static void
create_frag_shader(void)
{
/* This shader samples the currently bound depth texture, then compares
* that value to the current fragment Z value to produce a shade of red
* indicating error/difference.
*
* E.g: gl_FragColor = scale * abs(texture.Z - fragment.Z);
*
* Note that we have to be pretty careful with converting gl_FragCoord
* into a 2D texture coordinate. There's a -0.5 bias and scale factor.
*/
static const char *text_2d =
"uniform sampler2D zTex; \n"
"uniform float sizeScale; \n"
"uniform float errorScale; \n"
"void main() \n"
"{ \n"
" vec2 coord = (gl_FragCoord.xy - vec2(0.5)) / sizeScale; \n"
" vec4 z = texture2D(zTex, coord); \n"
" float diff = errorScale * abs(z.r - gl_FragCoord.z); \n"
" //gl_FragColor = vec4(gl_FragCoord.z, 0, 0, 0); \n"
" //gl_FragColor = z; \n"
" gl_FragColor = vec4(diff, 0, 0, 0); \n"
" gl_FragDepth = gl_FragCoord.z; \n"
"} \n";
static const char *text_rect =
"#extension GL_ARB_texture_rectangle: require \n"
"uniform sampler2DRect zTex; \n"
"uniform float sizeScale; \n"
"uniform float errorScale; \n"
"void main() \n"
"{ \n"
" vec2 coord = gl_FragCoord.xy; \n"
" vec4 z = texture2DRect(zTex, coord); \n"
" float diff = errorScale * abs(z.r - gl_FragCoord.z); \n"
" //gl_FragColor = vec4(gl_FragCoord.z, 0, 0, 0); \n"
" //gl_FragColor = z; \n"
" gl_FragColor = vec4(diff, 0, 0, 0); \n"
" gl_FragDepth = gl_FragCoord.z; \n"
"} \n";
GLuint fs;
GLint zTex, errorScale, sizeScale;
if (TexTarget == GL_TEXTURE_2D)
fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, text_2d);
else
fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, text_rect);
assert(fs);
ShaderProg = piglit_link_simple_program(0, fs);
assert(ShaderProg);
piglit_UseProgram(ShaderProg);
zTex = piglit_GetUniformLocation(ShaderProg, "zTex");
piglit_Uniform1i(zTex, 0); /* unit 0 */
errorScale = piglit_GetUniformLocation(ShaderProg, "errorScale");
piglit_Uniform1f(errorScale, ErrorScale);
sizeScale = piglit_GetUniformLocation(ShaderProg, "sizeScale");
piglit_Uniform1f(sizeScale, (float) (SIZE - 1));
piglit_UseProgram(0);
}
#if DEBUG
static void
find_float_min_max_center(const GLfloat *buf, GLuint n,
GLfloat *min, GLfloat *max, GLfloat *center)
{
GLint cx = SIZE/4, cy = SIZE/4;
GLuint i;
*min = 1.0e20;
*max = -1.0e20;
for (i = 0; i < n; i++) {
if (buf[i] != 1.0) {
if (buf[i] < *min)
*min = buf[i];
if (buf[i] > *max)
*max = buf[i];
}
}
*center = buf[cy * SIZE + cx];
}
static void
find_uint_min_max_center(const GLuint *buf, GLuint n,
GLuint *min, GLuint *max, GLuint *center)
{
GLint cx = SIZE/4, cy = SIZE/4;
GLuint i;
*min = ~0U;
*max = 0;
for (i = 0; i < n; i++) {
if (buf[i] != ~0U) {
if (buf[i] < *min)
*min = buf[i];
if (buf[i] > *max)
*max = buf[i];
}
}
*center = buf[cy * SIZE + cx];
}
#endif /* DEBUG */
static void
draw_sphere(void)
{
glutSolidSphere(0.95, 40, 20);
}
static void
render_to_fbo(void)
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO);
glViewport(0, 0, SIZE, SIZE);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1, 1.0);
glColor4f(1.0, 0.0, 0.0, 0.0);
draw_sphere();
glDisable(GL_DEPTH_TEST);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
static GLfloat *
read_float_z_image(GLint x, GLint y)
{
GLfloat *z = (GLfloat *) malloc(SIZE * SIZE * sizeof(GLfloat));
glReadPixels(x, y, SIZE, SIZE, GL_DEPTH_COMPONENT, GL_FLOAT, z);
return z;
}
#if DEBUG
static GLuint *
read_uint_z_image(GLint x, GLint y)
{
GLuint *z = (GLuint *) malloc(SIZE * SIZE * sizeof(GLuint));
glReadPixels(x, y, SIZE, SIZE, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, z);
return z;
}
#endif /* DEBUG */
/** Show contents of depth buffer in middle of window */
static void
show_depth_fbo(void)
{
GLfloat *zf;
glViewport(1 * SIZE, 0, SIZE, SIZE); /* not really needed */
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO);
zf = read_float_z_image(0, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glWindowPos2i(SIZE, 0);
glDrawPixels(SIZE, SIZE, GL_LUMINANCE, GL_FLOAT, zf);
assert(glGetError() == 0);
#if DEBUG
{
GLfloat min, max, center;
find_float_min_max_center(zf, SIZE * SIZE, &min, &max, ¢er);
printf("depth fbo min %f max %f center %f\n", min, max, center);
}
{
GLuint min, max, center;
GLuint *zi;
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO);
zi = read_uint_z_image(0, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
find_uint_min_max_center(zi, SIZE * SIZE, &min, &max, ¢er);
printf("depth fbo min 0x%x max 0x%x center 0x%x\n", min, max, center);
free(zi);
}
#endif /* DEBUG */
free(zf);
}
/** Draw quad textured with depth image on right side of window */
static void
draw_quad_with_depth_texture(void)
{
GLfloat s1, t1;
if (TexTarget == GL_TEXTURE_2D) {
s1 = t1 = 1.0;
}
else {
s1 = SIZE;
t1 = SIZE;
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glViewport(2 * SIZE, 0, SIZE, SIZE);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1, 1.0);
glBindTexture(TexTarget, DepthTex);
glEnable(TexTarget);
glBegin(GL_POLYGON);
glTexCoord2f(0, 0);
glVertex2f(-1, -1);
glTexCoord2f(s1, 0);
glVertex2f( 1, -1);
glTexCoord2f(s1, t1);
glVertex2f( 1, 1);
glTexCoord2f(0, t1);
glVertex2f(-1, 1);
glEnd();
glDisable(TexTarget);
}
/**
* Draw quad with fragment shader that compares fragment.z against the
* depth texture value (draw on left side of window).
* We draw on the left side of the window to easily convert gl_FragCoord
* into a texture coordinate.
*/
static void
draw_sphere_with_fragment_shader_compare(void)
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glViewport(0 * SIZE, 0, SIZE, SIZE);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1, 1.0);
glBindTexture(TexTarget, DepthTex);
piglit_UseProgram(ShaderProg);
glEnable(GL_DEPTH_TEST);
if (1) {
draw_sphere();
}
else {
/* To test using gl_TexCoord[0].xy instead of gl_FragCoord.xy in the shader
*/
static const GLfloat sPlane[4] = {0.5, 0, 0, 0.5};
static const GLfloat tPlane[4] = {0, 0.5, 0, 0.5};
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_S, GL_EYE_PLANE, sPlane);
glTexGenfv(GL_T, GL_EYE_PLANE, tPlane);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
draw_sphere();
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
}
glDisable(GL_DEPTH_TEST);
piglit_UseProgram(0);
#if DEBUG
{
GLfloat *z = read_float_z_image(0, 0);
GLfloat min, max, center;
find_float_min_max_center(z, SIZE * SIZE, &min, &max, ¢er);
printf("rendered min %f max %f center %f\n", min, max, center);
free(z);
}
{
GLuint *z = read_uint_z_image(0, 0);
GLuint min, max, center;
find_uint_min_max_center(z, SIZE * SIZE, &min, &max, ¢er);
printf("rendered min 0x%x max 0x%x center 0x%x\n", min, max, center);
free(z);
}
#endif /* DEBUG */
}
static enum piglit_result
count_and_report_bad_pixels(void)
{
GLubyte *z;
GLuint error = 0;
int i;
z = (GLubyte *) malloc(SIZE * SIZE * 4 * sizeof(GLubyte));
glReadPixels(0, 0, SIZE, SIZE, GL_RGBA, GL_UNSIGNED_BYTE, z);
for (i = 0; i < SIZE * SIZE * 4; i += 4) {
error += z[i];
}
free(z);
if (!piglit_automatic)
printf("total error = %u\n", error);
/* XXX this error test is a total hack for now */
if (error > ErrorScale)
return PIGLIT_FAIL;
else
return PIGLIT_PASS;
}
enum piglit_result
piglit_display(void)
{
enum piglit_result result;
render_to_fbo();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
show_depth_fbo();
draw_quad_with_depth_texture();
draw_sphere_with_fragment_shader_compare();
result = count_and_report_bad_pixels();
piglit_present_results();
return result;
}
void
piglit_init(int argc, char **argv)
{
int i = 1;
if (i < argc && strcmp(argv[i], "rect") == 0) {
TexTarget = GL_TEXTURE_RECTANGLE;
i++;
}
if (i < argc) {
ErrorScale = atof(argv[i]);
}
piglit_require_extension("GL_EXT_framebuffer_object");
piglit_require_fragment_shader();
if (TexTarget == GL_TEXTURE_RECTANGLE) {
piglit_require_extension("GL_ARB_texture_rectangle");
}
create_fbo();
if (ErrorScale == 0.0) {
/* A 1-bit error/difference in Z values results in a delta of 64 in
* pixel intensity (where pixels are in [0,255]).
*/
ErrorScale = ((double) (1ull << Zbits)) * 64.0 / 255.0;
}
create_frag_shader();
if (!piglit_automatic) {
printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
printf("Left: Shader showing difference pixels (black=good, red=error)\n");
printf("Middle: Depth buffer of FBO\n");
printf("Right: Quad textured with depth values\n");
printf("Z bits = %d\n", Zbits);
printf("ErrorScale = %f\n", ErrorScale);
printf("Texture target: %s\n",
TexTarget == GL_TEXTURE_RECTANGLE ? "RECTANGLE" : "2D" );
}
}
|