aboutsummaryrefslogtreecommitdiff
path: root/tests/shaders/glsl-arb-fragment-coord-conventions.c
blob: 9fb630989a61540a3f3fa2ded5d5f4831c9379ae (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
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
/*
 * Copyright © 2009 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.
 *
 * Author:
 *    Brian Paul
 *
 */

/**
 * @file glsl-arb-fragment-coord-conventions.c
 *
 * Test ARB_fragment_coord_conventions extension.
 */

#include "piglit-util-gl-common.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.window_width = 100;
	config.window_height = 100;
	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;

PIGLIT_GL_TEST_CONFIG_END

static const float black[4] = {0.0, 0.0, 0.0, 0.0};
static const float red[4] = {1.0, 0.0, 0.0, 0.0};
static const float green[4] = {0.0, 1.0, 0.0, 0.0};
static const float yellow[4] = {1.0, 1.0, 0.0, 0.0};
static const float gray25[4] = {0.25, 0.25, 0.0, 0.0};
static const float gray75[4] = {0.75, 0.75, 0.0, 0.0};

static int test = 0;

/*
 * For each of the various pixel center/origin layout qualifier modes
 * draw a full-window quad where the fragment color is a function of
 * the fragment coordinate.
 */
enum piglit_result
piglit_display(void)
{
   GLuint prog;
   GLuint vs, fs;
   GLboolean pass = GL_TRUE;

   piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

   vs = piglit_compile_shader(GL_VERTEX_SHADER, "shaders/glsl-mvp.vert");

   /* No layout: test regular gl_FragCoord */
   if (piglit_automatic || test == 0)
   {
      const char *fragtext =
         "void main(void) \n"
         "{ \n"
         "   gl_FragColor = gl_FragCoord * 0.01; \n"
         "} \n";

      printf("Regular gl_FragCoord\n");
      fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fragtext);
      prog = piglit_link_simple_program(vs, fs);
      glUseProgram(prog);

      glClear(GL_COLOR_BUFFER_BIT);

      piglit_draw_rect(0, 0, piglit_width, piglit_height);

      /* lower-left corner */
      pass = piglit_probe_pixel_rgb(0, 0, black) && pass;

      /* upper-right corner */
      pass = piglit_probe_pixel_rgb(99, 99, yellow) && pass;
   }

   /* No layout, test pixel center is half integer */
   if (piglit_automatic || test == 1)
   {
      const char *fragtext =
         "#extension GL_ARB_fragment_coord_conventions: enable \n"
         "void main(void) \n"
         "{ \n"
         "   gl_FragColor = fract(gl_FragCoord) + 0.25; \n"
         "   gl_FragColor.z = 0.0; \n"
         "} \n";

      printf("Pixel center half integer\n");
      fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fragtext);
      prog = piglit_link_simple_program(vs, fs);
      glUseProgram(prog);

      glClear(GL_COLOR_BUFFER_BIT);

      piglit_draw_rect(0, 0, piglit_width, piglit_height);

      /* lower-left corner */
      pass = piglit_probe_pixel_rgb(0, 0, gray75) && pass;

      /* upper-right corner */
      pass = piglit_probe_pixel_rgb(99, 99, gray75) && pass;
   }

   /* Pixel center integer */
   if (piglit_automatic || test == 2)
   {
      const char *fragtext =
         "#extension GL_ARB_fragment_coord_conventions: enable \n"
         "layout(pixel_center_integer) varying vec4 gl_FragCoord; \n"
         "void main(void) \n"
         "{ \n"
         "   gl_FragColor = fract(gl_FragCoord) + 0.25; \n"
         "   gl_FragColor.z = 0.0; \n"
         "} \n";

      printf("Pixel center integer\n");
      fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fragtext);
      prog = piglit_link_simple_program(vs, fs);
      glUseProgram(prog);

      glClear(GL_COLOR_BUFFER_BIT);

      piglit_draw_rect(0, 0, piglit_width, piglit_height);

      /* lower-left corner */
      pass = piglit_probe_pixel_rgb(0, 0, gray25) && pass;

      /* upper-right corner */
      pass = piglit_probe_pixel_rgb(99, 99, gray25) && pass;
   }

   /* Pixel origin upper left */
   if (piglit_automatic || test == 3)
   {
      const char *fragtext =
         "#extension GL_ARB_fragment_coord_conventions: enable \n"
         "layout(origin_upper_left) varying vec4 gl_FragCoord; \n"
         "void main(void) \n"
         "{ \n"
         "   gl_FragColor = gl_FragCoord * 0.01; \n"
         "   gl_FragColor.z = 0.0; \n"
         "} \n";

      printf("Pixel origin upper left\n");
      fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fragtext);
      prog = piglit_link_simple_program(vs, fs);
      glUseProgram(prog);

      glClear(GL_COLOR_BUFFER_BIT);

      piglit_draw_rect(0, 0, piglit_width, piglit_height);

      /* lower-left corner */
      pass = piglit_probe_pixel_rgb(0, 0, green) && pass;

      /* upper-right corner */
      pass = piglit_probe_pixel_rgb(99, 99, red) && pass;
   }

   /* Pixel origin upper left and pixel center integer */
   if (piglit_automatic || test == 4)
   {
      static const float color1[4] = {0.125, 0.3725, 0.0, 0.0};
      static const float color2[4] = {0.3725, 0.125, 0.0, 0.0};
      const char *fragtext =
         "#extension GL_ARB_fragment_coord_conventions: enable \n"
         "layout(origin_upper_left, pixel_center_integer) varying vec4 gl_FragCoord; \n"
         "void main(void) \n"
         "{ \n"
         "   gl_FragColor = gl_FragCoord * 0.0025 + 0.125; \n"
         "   gl_FragColor.z = 0.0; \n"
         "} \n";

      printf("Pixel origin upper left and pixel center integer\n");
      fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fragtext);
      prog = piglit_link_simple_program(vs, fs);
      glUseProgram(prog);

      glClear(GL_COLOR_BUFFER_BIT);

      piglit_draw_rect(0, 0, piglit_width, piglit_height);

      /* lower-left corner */
      pass = piglit_probe_pixel_rgb(0, 0, color1) && pass;

      /* upper-right corner */
      pass = piglit_probe_pixel_rgb(99, 99, color2) && pass;
   }

   piglit_present_results();

   return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}

static void key_func(unsigned char key, int x, int y)
{
   switch (key) {
   case 't':
      test = (test + 1) % 5;
      break;
   }

   piglit_escape_exit_key(key, x, y);
}

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

	piglit_require_extension("GL_ARB_fragment_coord_conventions");

	if (!piglit_automatic) {
		printf("Press t to switch between subtests.\n");
		piglit_set_keyboard_func(key_func);
	}
}