glsl-arb-fragment-coord-conventions.c 6.96 KB
Newer Older
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
/*
 * 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.h"

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

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};

46
static int test = 0;
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

/*
 * 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 */
65
   if (piglit_automatic || test == 0)
66 67 68 69 70 71 72
   {
      const char *fragtext =
         "void main(void) \n"
         "{ \n"
         "   gl_FragColor = gl_FragCoord * 0.01; \n"
         "} \n";

73
      printf("Regular gl_FragCoord\n");
74 75 76 77 78 79 80 81 82
      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 */
83
      pass = piglit_probe_pixel_rgb(0, 0, black) && pass;
84 85

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

   /* No layout, test pixel center is half integer */
90
   if (piglit_automatic || test == 1)
91 92 93 94 95 96 97 98 99
   {
      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";

100
      printf("Pixel center half integer\n");
101 102 103 104 105 106 107 108 109
      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 */
110
      pass = piglit_probe_pixel_rgb(0, 0, gray75) && pass;
111 112

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

   /* Pixel center integer */
117
   if (piglit_automatic || test == 2)
118 119 120 121 122 123 124 125 126 127
   {
      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";

128
      printf("Pixel center integer\n");
129 130 131 132 133 134 135 136 137
      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 */
138
      pass = piglit_probe_pixel_rgb(0, 0, gray25) && pass;
139 140

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

   /* Pixel origin upper left */
145
   if (piglit_automatic || test == 3)
146 147 148 149 150 151 152 153 154 155
   {
      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";

156
      printf("Pixel origin upper left\n");
157 158 159 160 161 162 163 164 165
      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 */
166
      pass = piglit_probe_pixel_rgb(0, 0, green) && pass;
167 168

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

   /* Pixel origin upper left and pixel center integer */
173
   if (piglit_automatic || test == 4)
174 175 176 177 178 179 180 181 182 183 184 185
   {
      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";

186
      printf("Pixel origin upper left and pixel center integer\n");
187 188 189 190 191 192 193 194 195
      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 */
196
      pass = piglit_probe_pixel_rgb(0, 0, color1) && pass;
197 198

      /* upper-right corner */
199
      pass = piglit_probe_pixel_rgb(99, 99, color2) && pass;
200 201 202 203
   }

   glutSwapBuffers();

204
   return pass ? PIGLIT_PASS : PIGLIT_FAIL;
205 206
}

207 208 209 210 211 212 213 214 215 216
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);
}
217 218 219 220

void
piglit_init(int argc, char **argv)
{
Paul Berry's avatar
Paul Berry committed
221
	if (piglit_get_gl_version() < 20) {
222 223 224 225 226
      printf("Requires OpenGL 2.0\n");
      piglit_report_result(PIGLIT_SKIP);
   }

   piglit_require_extension("GL_ARB_fragment_coord_conventions");
227 228 229 230 231 232

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

   glutKeyboardFunc(key_func);
233
}