ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_stencil_texturing / draw.c
blobfa6ea42f01255886a566f042abc9f5c24d396189
1 /*
2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
24 /**
25 * \file draw.c
27 * A basic drawing test for GL_ARB_stencil_texturing which ensures that
28 * sampling occurs from the right position in the texture.
30 * It creates two packed depth/stencil textures. The first has a horizontal
31 * gradient (0.0 -> 1.0 for depth, 0 -> 255 for stencil), and the second a
32 * vertical gradient.
34 * The expected output is four squares, separated by a blue border.
35 * The left half of the window is generated by stencil texturing, and drawn
36 * in red. The right half is generated by depth texturing, and drawn in green.
38 * Stencil | Depth
39 * (red) | (green)
40 * |
41 * 0 --> 1 | 0 --> 1
42 * --------------------
43 * 1 | 1
44 * ^ | ^
45 * | | |
46 * 0 | 0
49 #define __STDC_FORMAT_MACROS
50 #include <inttypes.h>
51 #include "piglit-util-gl.h"
53 PIGLIT_GL_TEST_CONFIG_BEGIN
55 config.supports_gl_compat_version = 30;
56 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
57 config.window_width = 256 * 2 + 3;
58 config.window_height = 256 * 2 + 3;
60 PIGLIT_GL_TEST_CONFIG_END
62 float stencil_horiz_expected[256 * 256 * 3];
63 float stencil_vert_expected[256 * 256 * 3];
64 float depth_horiz_expected[256 * 256 * 3];
65 float depth_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 /** A program that samples from depth using sampler2D. */
74 GLint depth_prog;
76 /******************************************************************************/
78 /**
79 * Bind a texture and change DEPTH_STENCIL_TEXTURE_MODE (convenience function).
81 static void
82 select_tex(GLint handle, GLenum depth_stencil_texture_mode)
84 glBindTexture(GL_TEXTURE_2D, handle);
85 glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_STENCIL_TEXTURE_MODE,
86 depth_stencil_texture_mode);
89 enum piglit_result
90 piglit_display(void)
92 bool pass = true;
94 glClearColor(0.0, 0.0, 1.0, 1.0);
95 glClear(GL_COLOR_BUFFER_BIT);
97 glUseProgram(stencil_prog);
99 /* Upper left corner: Stencil (black to red, left to right). */
100 glViewport(0, 259, 256, 256);
101 select_tex(horiz_tex, GL_STENCIL_INDEX);
102 piglit_draw_rect(-1, -1, 2, 2);
103 if (!piglit_probe_image_rgb(0, 259, 256, 256, stencil_horiz_expected)) {
104 printf(" FAIL: stencil (horizontal).\n");
105 pass = false;
109 /* Lower left corner: Stencil (black to red, upwards). */
110 glViewport(0, 0, 256, 256);
111 select_tex(vert_tex, GL_STENCIL_INDEX);
112 piglit_draw_rect(-1, -1, 2, 2);
113 if (!piglit_probe_image_rgb(0, 0, 256, 256, stencil_vert_expected)) {
114 printf(" FAIL: stencil (vertical).\n");
115 pass = false;
118 glUseProgram(depth_prog);
120 /* Upper right corner: Depth (black to green, left to right). */
121 glViewport(259, 259, 256, 256);
122 select_tex(horiz_tex, GL_DEPTH_COMPONENT);
123 piglit_draw_rect(-1, -1, 2, 2);
124 if (!piglit_probe_image_rgb(259, 259, 256, 256, depth_horiz_expected)) {
125 printf(" FAIL: depth (horizontal).\n");
126 pass = false;
129 /* Lower right corner: Depth (black to green, upwards). */
130 glViewport(259, 0, 256, 256);
131 select_tex(vert_tex, GL_DEPTH_COMPONENT);
132 piglit_draw_rect(-1, -1, 2, 2);
133 if (!piglit_probe_image_rgb(259, 0, 256, 256, depth_vert_expected)) {
134 printf(" FAIL: depth (vertical).\n");
135 pass = false;
138 piglit_present_results();
140 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
143 /******************************************************************************/
146 * Pack a floating point depth value and 8-bit stencil value into
147 * GL_UNSIGNED_INT24_S8 data.
149 uint32_t
150 pack_z24_s8(float z, uint8_t s)
152 uint32_t z24 = z * (double) 0xffffff;
153 return (z24 << 8) | s;
157 * Generate two textures containing gradients:
158 * - Depth ranges from 0.0 to 1.0.
159 * - Stencil ranges from 0 to 255.
161 * horiz_tex is left to right (increasing in the +x direction);
162 * vert_tex is bottom to top (increasing in the +y direction).
164 * Also set up expected values.
166 static void
167 setup_textures(void)
169 int i;
170 uint32_t horiz_data[256 * 256];
171 uint32_t vert_data[256 * 256];
173 for (i = 0; i < ARRAY_SIZE(horiz_data); i++) {
174 unsigned x = i % 256;
175 unsigned y = i / 256;
176 horiz_data[i] = pack_z24_s8(x / 255.0f, x);
177 vert_data[i] = pack_z24_s8(y / 255.0f, y);
180 glGenTextures(1, &horiz_tex);
181 glBindTexture(GL_TEXTURE_2D, horiz_tex);
182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
184 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8,
185 256, 256, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8,
186 horiz_data);
187 if (!piglit_check_gl_error(GL_NO_ERROR))
188 piglit_report_result(PIGLIT_FAIL);
190 glGenTextures(1, &vert_tex);
191 glBindTexture(GL_TEXTURE_2D, vert_tex);
192 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
193 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
194 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8,
195 256, 256, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8,
196 vert_data);
197 if (!piglit_check_gl_error(GL_NO_ERROR))
198 piglit_report_result(PIGLIT_FAIL);
200 /* Set up expected values. */
201 memset(stencil_horiz_expected, 0, sizeof(stencil_horiz_expected));
202 memset(stencil_vert_expected, 0, sizeof(stencil_vert_expected));
203 memset(depth_horiz_expected, 0, sizeof(depth_horiz_expected));
204 memset(depth_vert_expected, 0, sizeof(depth_vert_expected));
206 for (i = 0; i < 256 * 256; i++) {
207 unsigned x = i % 256;
208 unsigned y = i / 256;
209 stencil_horiz_expected[3*i] = x / 255.0f;
210 stencil_vert_expected[3*i] = y / 255.0f;
211 depth_horiz_expected[3*i+1] = x / 255.0f;
212 depth_vert_expected[3*i+1] = y / 255.0f;
219 * Compile the shaders used by this program.
221 * Depth data should be read using a floating point sampler2D; stencil data
222 * should be read using an unsigned integer usampler2D. So, we need two
223 * separate shaders.
225 static void
226 setup_shaders(void)
228 int loc;
230 const char *vs =
231 "#version 130\n"
232 "out vec2 texcoords;\n"
233 "void main()\n"
234 "{\n"
235 " gl_Position = gl_Vertex;\n"
236 " texcoords = (gl_Vertex.xy + 1.0) / 2.0;\n"
237 "}\n";
239 const char *fs_stencil =
240 "#version 130\n"
241 "in vec2 texcoords;\n"
242 "uniform usampler2D tex;\n"
243 "void main()\n"
244 "{\n"
245 " uint stencil = texture(tex, texcoords).x;\n"
246 " gl_FragColor = vec4(float(stencil) / 255.0, 0, 0, 1);\n"
247 "}\n";
249 const char *fs_depth =
250 "#version 130\n"
251 "in vec2 texcoords;\n"
252 "uniform sampler2D tex;\n"
253 "void main()\n"
254 "{\n"
255 " float depth = texture(tex, texcoords).x;\n"
256 " gl_FragColor = vec4(0, depth, 0, 1);\n"
257 "}\n";
259 stencil_prog = piglit_build_simple_program(vs, fs_stencil);
260 loc = glGetUniformLocation(stencil_prog, "tex");
261 glUseProgram(stencil_prog);
262 glUniform1i(loc, 0);
264 depth_prog = piglit_build_simple_program(vs, fs_depth);
265 loc = glGetUniformLocation(depth_prog, "tex");
266 glUseProgram(depth_prog);
267 glUniform1i(loc, 0);
270 void
271 piglit_init(int argc, char **argv)
273 piglit_require_extension("GL_ARB_stencil_texturing");
275 setup_textures();
276 setup_shaders();