2 * Copyright (c) 2015 Intel Corporation
3 * Copyright 2014 VMware, Inc.
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
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
22 * DEALINGS IN THE SOFTWARE.
26 * Simple fragment shader test for textureSamplesIdenticalEXT.
28 * Render a simple image. Scan the image. At each texel, render green if
29 * textureSamplesIdenticalEXT returns false. If textureSamplesIdenticalEXT
30 * returns true, examine each sample. If the samples are all the same color,
31 * render blue. Render red otherwise. The test passes if there are zero red
32 * pixels and non-zero green pixels.
34 * Much code borrowed from tests/spec/arb_texture_multisample/texelfetch.c.
37 * This is a pretty weak test. A stronger test would read back the original
38 * multisampled image and verify the sample-indenticalness using that.
40 #include "piglit-util-gl.h"
42 PIGLIT_GL_TEST_CONFIG_BEGIN
44 config
.supports_gl_compat_version
= 30;
45 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
;
47 PIGLIT_GL_TEST_CONFIG_END
49 #define MAX_SAMPLES 32
51 static const char *vs_src_draw
=
54 "in vec4 piglit_vertex;\n"
58 " gl_Position = piglit_vertex;\n"
62 static const char *fs_src_draw
=
65 "out vec4 frag_color;\n"
69 " frag_color = vec4(0.9, 0.8, 0, 1);\n"
73 static const char *vs_src_readback
=
76 "in vec4 piglit_vertex;\n"
77 "in vec2 piglit_texcoord;\n"
79 "out vec2 coord_fs;\n"
82 " gl_Position = piglit_vertex;\n"
83 " coord_fs = piglit_texcoord;\n"
87 static const char *fs_src_readback
=
89 "#extension GL_ARB_texture_multisample: require\n"
90 "#extension GL_EXT_shader_samples_identical: require\n"
92 "uniform sampler2DMS tex;\n"
93 "uniform int num_samples;\n"
96 "out vec4 frag_color;\n"
101 " if (textureSamplesIdenticalEXT(tex, ivec2(coord_fs))) {\n"
102 " frag_color = vec4(0.0, 0.0, 1.0, 1.0);\n"
104 " /* Verify that all the samples have the same color. */\n"
105 " vec4 base = texelFetch(tex, ivec2(coord_fs), 0);\n"
106 " for (int i = 1; i < num_samples; i++) {\n"
107 " vec4 s = texelFetch(tex, ivec2(coord_fs), i);\n"
110 " frag_color = vec4(1.0, 0.0, 0.0, 1.0);\n"
113 " frag_color = vec4(0.0, 1.0, 0.0, 1.0);\n"
120 static GLuint readback_prog
, draw_prog
;
125 static const GLfloat quad_verts
[4][4] = {
128 { -0.1, -1.0, 0, 1 },
129 { -0.8, -0.1, 0, 1 },
131 static const float green
[] = { 0.0, 1.0, 0.0, 1.0 };
132 static const float blue
[] = { 0.0, 0.0, 1.0, 1.0 };
134 GLfloat image
[32 * 32 * 4];
135 unsigned blue_pixels
= 0;
136 unsigned green_pixels
= 0;
139 glViewport(0, 0, 32, 32);
141 /* Draw triangle into MSAA texture */
142 glBindFramebuffer(GL_FRAMEBUFFER
, fbo
);
143 glClearColor(0.5f
, 0.5f
, 0.5f
, 0.0f
);
144 glClear(GL_COLOR_BUFFER_BIT
);
146 glUseProgram(draw_prog
);
147 piglit_draw_rect_from_arrays(quad_verts
, NULL
, false, 1);
149 /* Scan the previous render. Draw green if the samples are all
150 * (verifiably) the same, blue if there may be differences, and red if
151 * there were differences but textureSamplesIdentical said there were
154 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
155 glClearColor(0.25, 0.25, 0.25, 0);
156 glClear(GL_COLOR_BUFFER_BIT
);
157 glUseProgram(readback_prog
);
159 piglit_draw_rect_tex(-1, -1, 2, 2, 0, 0, 31, 31);
161 glReadPixels(0, 0, 32, 32, GL_RGBA
, GL_FLOAT
, image
);
163 for (i
= 0; i
< 32 * 32; i
++) {
164 if (memcmp(&image
[i
* 4], blue
, sizeof(blue
)) == 0) {
166 } else if (memcmp(&image
[i
* 4], green
, sizeof(green
)) == 0) {
170 "Bad pixel color @ %u: { %f, %f, %f }\n",
179 printf("Blue pixels: %u\n", blue_pixels
);
180 printf("Green pixels: %u\n", green_pixels
);
181 piglit_present_results();
183 pass
= (green_pixels
> 0) && pass
;
185 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
190 piglit_init(int argc
, char **argv
)
192 GLint num_samples_uniform
;
196 piglit_require_extension("GL_ARB_texture_multisample");
197 piglit_require_extension("GL_EXT_shader_samples_identical");
200 fprintf(stderr
, "Usage: %s <sample_count>\n", argv
[0]);
201 piglit_report_result(PIGLIT_SKIP
);
204 num_samples
= strtoul(argv
[1], NULL
, 0);
205 glGetIntegerv(GL_MAX_SAMPLES
, &max_samples
);
206 if (num_samples
> max_samples
)
207 piglit_report_result(PIGLIT_SKIP
);
209 assert(num_samples
<= MAX_SAMPLES
);
211 /* create MSAA tex and fbo */
212 glGenTextures(1, &tex
);
213 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, tex
);
214 glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE
, num_samples
,
218 glGenFramebuffers(1, &fbo
);
219 glBindFramebuffer(GL_FRAMEBUFFER
, fbo
);
220 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
221 GL_TEXTURE_2D_MULTISAMPLE
,
223 glDrawBuffer(GL_COLOR_ATTACHMENT0
);
225 if (glCheckFramebufferStatus(GL_FRAMEBUFFER
) != GL_FRAMEBUFFER_COMPLETE
)
226 piglit_report_result(PIGLIT_FAIL
);
228 /* create sample readback shader */
229 readback_prog
= piglit_build_simple_program(vs_src_readback
,
231 glUseProgram(readback_prog
);
233 num_samples_uniform
= glGetUniformLocation(readback_prog
,
235 glUniform1i(num_samples_uniform
, num_samples
);
237 /* create triangle drawing shader */
238 draw_prog
= piglit_build_simple_program(vs_src_draw
, fs_src_draw
);
239 glUseProgram(draw_prog
);
241 if (!piglit_check_gl_error(GL_NO_ERROR
))
242 piglit_report_result(PIGLIT_FAIL
);
244 glEnable(GL_MULTISAMPLE
);