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
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
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.
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};
71 glGenTextures(1, &tex
);
72 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, tex
);
73 glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE
,
76 TEX_WIDTH
, TEX_HEIGHT
,
77 GL_FALSE
/* fixedsamplelocations */);
79 if (!piglit_check_gl_error(GL_NO_ERROR
))
80 piglit_report_result(PIGLIT_FAIL
);
86 clear_texture(GLuint tex
)
88 /* Clear the entire texture to red */
95 /* Clear two other sub-regions */
97 glClearTexSubImage(tex
,
99 ZERO_CLEAR_X
, ZERO_CLEAR_Y
, 0,
100 ZERO_CLEAR_WIDTH
, ZERO_CLEAR_HEIGHT
, 1,
105 glClearTexSubImage(tex
,
107 VALUE_CLEAR_X
, VALUE_CLEAR_Y
, 0,
108 VALUE_CLEAR_WIDTH
, VALUE_CLEAR_HEIGHT
, 1,
113 if (!piglit_check_gl_error(GL_NO_ERROR
))
114 piglit_report_result(PIGLIT_FAIL
);
123 static const char vs_source
[] =
125 "in vec2 piglit_vertex;\n"
126 "uniform vec2 fb_size;\n"
127 "out vec2 sample_coord;\n"
131 " gl_Position = vec4(piglit_vertex * 2.0 /\n"
134 " sample_coord = piglit_vertex;\n"
136 static const char fs_source
[] =
138 "#extension GL_ARB_texture_multisample : enable\n"
139 "uniform sampler2DMS tex;\n"
140 "in vec2 sample_coord;\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"
151 prog
= piglit_build_simple_program(vs_source
, fs_source
);
155 uniform
= glGetUniformLocation(prog
, "tex");
156 glUniform1i(uniform
, 0);
158 uniform
= glGetUniformLocation(prog
, "fb_size");
159 glUniform2f(uniform
, piglit_width
, piglit_height
);
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
181 glGetIntegerv(GL_MAX_COLOR_TEXTURE_SAMPLES
, &maxColorTextureSamples
);
182 if (maxColorTextureSamples
< TEX_SAMPLES
) {
183 printf("At least %i texture samples are required\n",
185 piglit_report_result(PIGLIT_SKIP
);
194 static const struct {
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
,
209 GL_FALSE
, /* normalized */
213 glDrawArrays(GL_TRIANGLE_STRIP
, 0, 4);
215 glDisableVertexAttribArray(PIGLIT_ATTRIB_POS
);
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
);
232 tex
= create_texture();
238 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, 0);
240 glDeleteTextures(1, &tex
);
242 pass
&= piglit_probe_rect_rgb(VALUE_CLEAR_X
* 2,
244 VALUE_CLEAR_WIDTH
* 2,
245 VALUE_CLEAR_HEIGHT
* 2,
248 pass
&= piglit_probe_rect_rgb(ZERO_CLEAR_X
* 2,
250 ZERO_CLEAR_WIDTH
* 2,
251 ZERO_CLEAR_HEIGHT
* 2,
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
,
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
,
268 pass
&= probe_tex(0, VALUE_CLEAR_Y
+ VALUE_CLEAR_HEIGHT
,
270 TEX_HEIGHT
- VALUE_CLEAR_Y
- VALUE_CLEAR_HEIGHT
);
272 piglit_present_results();
274 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;