summaryrefslogtreecommitdiff
path: root/tests/general/early-z.c
blob: b4f67205dcd0f232ba89502a445e3518b718c4f1 (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
/*
 * Copyright (c) 2011 Christoph Bumiller
 *
 * 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 VMWARE 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.
 */

/**
 * @file
 * Test for bugs with early depth testing and early depth update.
 */

#include "piglit-util.h"


int piglit_width = 128;
int piglit_height = 128;
int piglit_window_mode = GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE | GLUT_DEPTH;

static const char *fpFragDepthText =
   "void main() \n"
   "{ \n"
   "   gl_FragColor = vec4(gl_Color.rgb, 1.0); \n"
   "   gl_FragDepth = 0.9; \n"
   "} \n";

static const char *fpDiscardText =
   "void main() \n"
   "{ \n"
   "   if (gl_Color.r > 0.25) \n"
   "      discard; \n"
   "   gl_FragColor = vec4(gl_Color.rgb, 1.0); \n"
   "} \n";

static const char *fpAlphaText =
   "void main() \n"
   "{ \n"
   "   gl_FragColor = vec4(gl_Color.rgb, 0.0); \n"
   "} \n";


static GLuint shader[3];
static GLuint program[3];

static void quad(const GLfloat z, uint32_t colour)
{
   glColor3ub(colour & 0xff, (colour >> 8) & 0xff, (colour >> 16) & 0xff);
   glBegin(GL_QUADS);
   glVertex3f(-1.0f, -1.0f, z);
   glVertex3f(-1.0f,  1.0f, z);
   glVertex3f( 1.0f,  1.0f, z);
   glVertex3f( 1.0f, -1.0f, z);
   glEnd();
}

static GLboolean
test_early_depth(void)
{
   glClearColor(0.0, 0.0, 0.0, 0.0);
   glClearDepth(1.0);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   glEnable(GL_DEPTH_TEST);
   glDepthMask(GL_TRUE);
   glDepthFunc(GL_LESS);

   /* 1. (blue) depth should be adjusted to 0.9 by the FP */
   glUseProgram(program[0]);
   quad(0.3, 0xff0000);

   /* 2. (red) should be discarded, no depth value written */
   glUseProgram(program[1]);
   quad(0.2, 0x0000ff);

   /* 3. (white) should be discarded by the alpha test, no depth written */
   glUseProgram(program[2]);
   glEnable(GL_ALPHA_TEST);
   glAlphaFunc(GL_GREATER, 0.5f);
   quad(0.1, 0xffffff);
   glDisable(GL_ALPHA_TEST);

   /* 4. (green) should be drawn because depth is still 0.9 */
   quad(0.7, 0x00ff00);

   {
      const GLfloat color[4] = { 0.0f, 1.0f, 0.0f, 0.0f };
      GLint pos[4];

      /* use glRasterPos to determine where to read a sample pixel */
      glRasterPos2f(0.0f, 0.0f);
      glGetIntegerv(GL_CURRENT_RASTER_POSITION, pos);

      if (!piglit_probe_pixel_rgba(pos[0], pos[1], color)) {
         glutSwapBuffers();
         return GL_FALSE;
      }
   }

   glutSwapBuffers();

   return GL_TRUE;
}


enum piglit_result
piglit_display(void)
{
   if (!test_early_depth())
      return PIGLIT_FAILURE;

   return PIGLIT_SUCCESS;
}


void
piglit_init(int argc, char **argv)
{
   int i;

   shader[0] = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fpFragDepthText);
   assert(shader[0]);

   shader[1] = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fpDiscardText);
   assert(shader[1]);

   shader[2] = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fpAlphaText);
   assert(shader[2]);

   for (i = 0; i < 3; ++i)
      program[i] = piglit_link_simple_program(shader[i], 0);
}