ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_clear_texture / multisample.c
blobe366f0a16d154b90e638ac3a1a80b2b0cd267da1
1 /*
2 * Copyright (c) 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 /** @file multisample.c
26 * A test of using glClearTexSubImage to clear sub-regions of a
27 * multisample texture. An 8x8 multisample texture is created with 4
28 * samples. The whole texture is cleared using glClearTexImage and
29 * then two sub-regions are cleared using glClearTexSubImage. One
30 * region is using NULL for the data and the other is using a known
31 * value. The texture is then drawn at 16x16 so that every sample of
32 * every texel can be drawn using special shader. The values are then
33 * compared to check that all of the samples are cleared.
36 #define TEX_WIDTH 8
37 #define TEX_HEIGHT 8
38 #define TEX_SAMPLES 4
40 #define VALUE_CLEAR_X 2
41 #define VALUE_CLEAR_Y 4
42 #define VALUE_CLEAR_WIDTH 3
43 #define VALUE_CLEAR_HEIGHT 2
45 #define ZERO_CLEAR_X 5
46 #define ZERO_CLEAR_Y 1
47 #define ZERO_CLEAR_WIDTH 2
48 #define ZERO_CLEAR_HEIGHT 3
50 #include "piglit-util-gl.h"
52 PIGLIT_GL_TEST_CONFIG_BEGIN
54 config.supports_gl_compat_version = 21;
56 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
58 config.khr_no_error_support = PIGLIT_NO_ERRORS;
60 PIGLIT_GL_TEST_CONFIG_END
62 static const float red[4] = {1.0, 0.0, 0.0, 1.0};
63 static const float green[4] = {0.0, 1.0, 0.0, 1.0};
64 static const float black[4] = {0.0, 0.0, 0.0, 1.0};
66 static GLuint
67 create_texture(void)
69 GLuint tex;
71 glGenTextures(1, &tex);
72 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, tex);
73 glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE,
74 TEX_SAMPLES,
75 GL_RGBA,
76 TEX_WIDTH, TEX_HEIGHT,
77 GL_FALSE /* fixedsamplelocations */);
79 if (!piglit_check_gl_error(GL_NO_ERROR))
80 piglit_report_result(PIGLIT_FAIL);
82 return tex;
85 static void
86 clear_texture(GLuint tex)
88 /* Clear the entire texture to red */
89 glClearTexImage(tex,
90 0, /* level */
91 GL_RGBA,
92 GL_FLOAT,
93 red);
95 /* Clear two other sub-regions */
97 glClearTexSubImage(tex,
98 0, /* level */
99 ZERO_CLEAR_X, ZERO_CLEAR_Y, 0,
100 ZERO_CLEAR_WIDTH, ZERO_CLEAR_HEIGHT, 1,
101 GL_RGBA,
102 GL_UNSIGNED_BYTE,
103 NULL);
105 glClearTexSubImage(tex,
106 0, /* level */
107 VALUE_CLEAR_X, VALUE_CLEAR_Y, 0,
108 VALUE_CLEAR_WIDTH, VALUE_CLEAR_HEIGHT, 1,
109 GL_RGBA,
110 GL_FLOAT,
111 green);
113 if (!piglit_check_gl_error(GL_NO_ERROR))
114 piglit_report_result(PIGLIT_FAIL);
117 static void
118 init_program(void)
120 GLuint prog;
121 GLuint uniform;
123 static const char vs_source[] =
124 "#version 130\n"
125 "in vec2 piglit_vertex;\n"
126 "uniform vec2 fb_size;\n"
127 "out vec2 sample_coord;\n"
128 "\n"
129 "void main()\n"
130 "{\n"
131 " gl_Position = vec4(piglit_vertex * 2.0 /\n"
132 " fb_size - 1.0,\n"
133 " 0.0, 1.0);\n"
134 " sample_coord = piglit_vertex;\n"
135 "}\n";
136 static const char fs_source[] =
137 "#version 130\n"
138 "#extension GL_ARB_texture_multisample : enable\n"
139 "uniform sampler2DMS tex;\n"
140 "in vec2 sample_coord;\n"
141 "\n"
142 "void main()\n"
143 "{\n"
144 " ivec2 isample_coord = ivec2(sample_coord);\n"
145 " ivec2 tex_coord = isample_coord / 2;\n"
146 " int sample = ((isample_coord.x & 1) * 2 +\n"
147 " (isample_coord.y & 1));\n"
148 " gl_FragColor = texelFetch(tex, tex_coord, sample);\n"
149 "}\n";
151 prog = piglit_build_simple_program(vs_source, fs_source);
153 glUseProgram(prog);
155 uniform = glGetUniformLocation(prog, "tex");
156 glUniform1i(uniform, 0);
158 uniform = glGetUniformLocation(prog, "fb_size");
159 glUniform2f(uniform, piglit_width, piglit_height);
162 void
163 piglit_init(int argc, char **argv)
165 GLint maxColorTextureSamples;
167 /* glClearTexture is either in the GL_ARB_clear_texture
168 * extension or in core in GL 4.4
170 if (piglit_get_gl_version() < 44 &&
171 !piglit_is_extension_supported("GL_ARB_clear_texture")) {
172 printf("OpenGL 4.4 or GL_ARB_clear_texture is required.\n");
173 piglit_report_result(PIGLIT_SKIP);
176 piglit_require_extension("GL_ARB_texture_multisample");
177 piglit_require_GLSL_version(130);
179 /* We need to support multisample textures with at least 4
180 * samples */
181 glGetIntegerv(GL_MAX_COLOR_TEXTURE_SAMPLES, &maxColorTextureSamples);
182 if (maxColorTextureSamples < TEX_SAMPLES) {
183 printf("At least %i texture samples are required\n",
184 TEX_SAMPLES);
185 piglit_report_result(PIGLIT_SKIP);
188 init_program();
191 static void
192 draw_tex(GLuint tex)
194 static const struct {
195 float x, y;
196 } attribs[] = {
197 { 0.0f, 0.0f },
198 { TEX_WIDTH * 2.0f, 0.0f },
199 { 0.0f, TEX_HEIGHT * 2.0f },
200 { TEX_WIDTH * 2, TEX_HEIGHT * 2.0f },
203 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, tex);
205 glEnableVertexAttribArray(PIGLIT_ATTRIB_POS);
206 glVertexAttribPointer(PIGLIT_ATTRIB_POS,
207 2, /* size */
208 GL_FLOAT,
209 GL_FALSE, /* normalized */
210 sizeof attribs[0],
211 &attribs[0].x);
213 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
215 glDisableVertexAttribArray(PIGLIT_ATTRIB_POS);
218 static bool
219 probe_tex(int x, int y, int width, int height)
221 /* The texture coordinates are double because it was drawn a
222 * quadruple the size */
223 return piglit_probe_rect_rgb(x * 2, y * 2, width * 2, height * 2, red);
226 enum piglit_result
227 piglit_display(void)
229 bool pass = true;
230 GLuint tex;
232 tex = create_texture();
234 clear_texture(tex);
236 draw_tex(tex);
238 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
240 glDeleteTextures(1, &tex);
242 pass &= piglit_probe_rect_rgb(VALUE_CLEAR_X * 2,
243 VALUE_CLEAR_Y * 2,
244 VALUE_CLEAR_WIDTH * 2,
245 VALUE_CLEAR_HEIGHT * 2,
246 green);
248 pass &= piglit_probe_rect_rgb(ZERO_CLEAR_X * 2,
249 ZERO_CLEAR_Y * 2,
250 ZERO_CLEAR_WIDTH * 2,
251 ZERO_CLEAR_HEIGHT * 2,
252 black);
254 /* Everything else should be red */
256 pass &= probe_tex(0, 0,
257 TEX_WIDTH, ZERO_CLEAR_Y);
258 pass &= probe_tex(0, ZERO_CLEAR_Y,
259 ZERO_CLEAR_X, ZERO_CLEAR_HEIGHT);
260 pass &= probe_tex(ZERO_CLEAR_X + ZERO_CLEAR_WIDTH, ZERO_CLEAR_Y,
261 TEX_WIDTH - ZERO_CLEAR_X - ZERO_CLEAR_WIDTH,
262 ZERO_CLEAR_HEIGHT);
263 pass &= probe_tex(0, VALUE_CLEAR_Y,
264 VALUE_CLEAR_X, VALUE_CLEAR_HEIGHT);
265 pass &= probe_tex(VALUE_CLEAR_X + VALUE_CLEAR_WIDTH, VALUE_CLEAR_Y,
266 TEX_WIDTH - VALUE_CLEAR_X - VALUE_CLEAR_WIDTH,
267 VALUE_CLEAR_HEIGHT);
268 pass &= probe_tex(0, VALUE_CLEAR_Y + VALUE_CLEAR_HEIGHT,
269 TEX_WIDTH,
270 TEX_HEIGHT - VALUE_CLEAR_Y - VALUE_CLEAR_HEIGHT);
272 piglit_present_results();
274 return pass ? PIGLIT_PASS : PIGLIT_FAIL;