ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_texture_stencil8 / draw.c
blob244ffcf8dfe9f8abc01b2a7dd401563bdb463d03
1 /*
2 * Copyright © 2014 Intel Corporation
3 * Copyright © 2015 Red Hat
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
25 /**
26 * \file draw.c
28 * A basic drawing test for GL_ARB_texture_stencil8 which ensures that
29 * sampling occurs from the right position in the texture.
31 * It creates two stencil textures. The first has a horizontal
32 * gradient (0 -> 255 for stencil), and the second a
33 * vertical gradient.
35 * The expected output is two squares, separated by a blue border.
36 * The left half of the window is generated by stencil texturing, and drawn
37 * in red.
39 * Stencil
40 * (red)
42 * 0 --> 1
43 * --------
44 * 1
45 * ^
46 * |
47 * 0
50 #define __STDC_FORMAT_MACROS
51 #include <inttypes.h>
52 #include "piglit-util-gl.h"
54 PIGLIT_GL_TEST_CONFIG_BEGIN
56 config.supports_gl_core_version = 32;
57 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
58 config.khr_no_error_support = PIGLIT_NO_ERRORS;
59 config.window_width = 256 + 3;
60 config.window_height = 256 * 2 + 3;
62 PIGLIT_GL_TEST_CONFIG_END
64 float stencil_horiz_expected[256 * 256 * 3];
65 float stencil_vert_expected[256 * 256 * 3];
67 GLuint horiz_tex;
68 GLuint vert_tex;
70 /** A program that samples from stencil using usampler2D. */
71 GLint stencil_prog;
73 GLuint vao;
75 /******************************************************************************/
77 enum piglit_result
78 piglit_display(void)
80 bool pass = true;
82 glClearColor(0.0, 0.0, 1.0, 1.0);
83 glClear(GL_COLOR_BUFFER_BIT);
85 glUseProgram(stencil_prog);
87 /* Upper left corner: Stencil (black to red, left to right). */
88 glViewport(0, 259, 256, 256);
89 glBindTexture(GL_TEXTURE_2D, horiz_tex);
90 piglit_draw_rect(-1, -1, 2, 2);
91 if (!piglit_probe_image_rgb(0, 259, 256, 256, stencil_horiz_expected)) {
92 printf(" FAIL: stencil (horizontal).\n");
93 pass = false;
97 /* Lower left corner: Stencil (black to red, upwards). */
98 glViewport(0, 0, 256, 256);
99 glBindTexture(GL_TEXTURE_2D, vert_tex);
100 piglit_draw_rect(-1, -1, 2, 2);
101 if (!piglit_probe_image_rgb(0, 0, 256, 256, stencil_vert_expected)) {
102 printf(" FAIL: stencil (vertical).\n");
103 pass = false;
106 piglit_present_results();
108 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
111 /******************************************************************************/
114 * - Stencil ranges from 0 to 255.
116 * horiz_tex is left to right (increasing in the +x direction);
117 * vert_tex is bottom to top (increasing in the +y direction).
119 * Also set up expected values.
121 static void
122 setup_textures(void)
124 int i;
125 uint8_t horiz_data[256 * 256];
126 uint8_t vert_data[256 * 256];
128 for (i = 0; i < ARRAY_SIZE(horiz_data); i++) {
129 unsigned x = i % 256;
130 unsigned y = i / 256;
131 horiz_data[i] = x;
132 vert_data[i] = y;
135 glGenTextures(1, &horiz_tex);
136 glBindTexture(GL_TEXTURE_2D, horiz_tex);
137 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
138 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
139 glTexImage2D(GL_TEXTURE_2D, 0, GL_STENCIL_INDEX8,
140 256, 256, 0, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
141 horiz_data);
142 if (!piglit_check_gl_error(GL_NO_ERROR))
143 piglit_report_result(PIGLIT_FAIL);
145 glGenTextures(1, &vert_tex);
146 glBindTexture(GL_TEXTURE_2D, vert_tex);
147 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
148 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
149 glTexImage2D(GL_TEXTURE_2D, 0, GL_STENCIL_INDEX8,
150 256, 256, 0, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
151 vert_data);
152 if (!piglit_check_gl_error(GL_NO_ERROR))
153 piglit_report_result(PIGLIT_FAIL);
155 /* Set up expected values. */
156 memset(stencil_horiz_expected, 0, sizeof(stencil_horiz_expected));
157 memset(stencil_vert_expected, 0, sizeof(stencil_vert_expected));
159 for (i = 0; i < 256 * 256; i++) {
160 unsigned x = i % 256;
161 unsigned y = i / 256;
162 stencil_horiz_expected[3*i] = x / 255.0f;
163 stencil_vert_expected[3*i] = y / 255.0f;
170 * Compile the shaders used by this program.
173 static void
174 setup_shaders(void)
176 int loc;
178 const char *vs =
179 "#version 130\n"
180 "in vec4 piglit_vertex;\n"
181 "out vec2 texcoords;\n"
182 "void main()\n"
183 "{\n"
184 " gl_Position = piglit_vertex;\n"
185 " texcoords = (piglit_vertex.xy + 1.0) / 2.0;\n"
186 "}\n";
188 const char *fs_stencil =
189 "#version 130\n"
190 "in vec2 texcoords;\n"
191 "uniform usampler2D tex;\n"
192 "void main()\n"
193 "{\n"
194 " uint stencil = texture(tex, texcoords).x;\n"
195 " gl_FragColor = vec4(float(stencil) / 255.0, 0, 0, 1);\n"
196 "}\n";
198 stencil_prog = piglit_build_simple_program(vs, fs_stencil);
199 loc = glGetUniformLocation(stencil_prog, "tex");
200 glUseProgram(stencil_prog);
201 glUniform1i(loc, 0);
205 void
206 piglit_init(int argc, char **argv)
208 piglit_require_extension("GL_ARB_texture_stencil8");
210 glGenVertexArrays(1, &vao);
211 glBindVertexArray(vao);
212 setup_textures();
213 setup_shaders();