2 * Copyright (c) 2015 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
21 * DEALINGS IN THE SOFTWARE.
25 * A test to check whether the right values are written to gl_SampleMaskIn
26 * when ARB_post_depth_coverage, sample shading and multisampling are enabled.
27 * Tests at 2, 4, 8, 16 sample rates.
30 #include "piglit-util-gl.h"
32 PIGLIT_GL_TEST_CONFIG_BEGIN
33 config
.supports_gl_compat_version
= 43;
34 config
.supports_gl_core_version
= 43;
35 config
.window_width
= 160;
36 config
.window_height
= 160;
37 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DEPTH
|
38 PIGLIT_GL_VISUAL_DOUBLE
;
39 PIGLIT_GL_TEST_CONFIG_END
41 static GLuint prog1
, prog2
, vao
, ssbo
, tex_color
, tex_depth
, fbo
;
42 static GLint
*sample_mask
;
44 static const char *vs_text
=
49 " gl_Position = pos_in;\n"
52 static const char *fs_text1
=
57 " gl_FragDepth = 0.5f;\n"
58 " color = vec4(0.0, 1.0, 0.0, 1.0);\n"
61 static const char *fs_text2
=
63 "#extension GL_ARB_post_depth_coverage: enable\n"
65 "layout(early_fragment_tests) in;\n"
66 "layout(post_depth_coverage) in;\n"
67 "layout(std430, binding = 0) buffer MaskOutput {\n"
70 "layout(location = 1) uniform int width;\n"
71 "layout(location = 2) uniform int samples;\n"
74 " int index = int(gl_FragCoord.y) * width + int(gl_FragCoord.x);\n"
75 " if (gl_SampleMaskIn[0] == (1 << gl_SampleID)) {\n"
76 " mask_output.data[index] = 1;\n"
78 " mask_output.data[index] = 0;\n"
80 " color = vec4(1.0, 0.0, 0.0, 1.0);\n"
84 make_shader_program1(void)
88 prog
= piglit_build_simple_program(vs_text
, fs_text1
);
91 glBindAttribLocation(prog
, 0, "pos_in");
95 if (!piglit_check_gl_error(GL_NO_ERROR
)) {
96 piglit_report_result(PIGLIT_FAIL
);
103 make_shader_program2(void)
107 prog
= piglit_build_simple_program(vs_text
, fs_text2
);
110 glBindAttribLocation(prog
, 0, "pos_in");
114 if (!piglit_check_gl_error(GL_NO_ERROR
)) {
115 piglit_report_result(PIGLIT_FAIL
);
125 glGenBuffers(1, &ssbo
);
126 glBindBuffer(GL_SHADER_STORAGE_BUFFER
, ssbo
);
128 if (!piglit_check_gl_error(GL_NO_ERROR
)) {
129 piglit_report_result(PIGLIT_FAIL
);
139 glGenFramebuffers(1, &fbo
);
140 glBindFramebuffer(GL_FRAMEBUFFER
, fbo
);
141 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, tex_color
);
142 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
143 GL_TEXTURE_2D_MULTISAMPLE
, tex_color
, 0);
144 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, tex_depth
);
145 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_DEPTH_STENCIL_ATTACHMENT
,
146 GL_TEXTURE_2D_MULTISAMPLE
, tex_depth
, 0);
147 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, 0);
153 make_texture_color(void)
157 glGenTextures(1, &tex
);
158 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, tex
);
159 glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE
, 2,
160 GL_RGBA32F
, piglit_width
, piglit_height
, false);
161 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, 0);
167 make_texture_depth(void)
171 glGenTextures(1, &tex
);
172 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, tex
);
173 glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE
, 2,
174 GL_DEPTH24_STENCIL8
, piglit_width
, piglit_height
, false);
175 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, 0);
183 static const float pos_tc
[12][2] = {
197 const int stride
= sizeof(pos_tc
[0]);
200 glGenVertexArrays(1, &vao
);
201 glBindVertexArray(vao
);
203 glGenBuffers(1, &vbo
);
204 glBindBuffer(GL_ARRAY_BUFFER
, vbo
);
205 glBufferData(GL_ARRAY_BUFFER
, sizeof(pos_tc
), pos_tc
, GL_STATIC_DRAW
);
206 piglit_check_gl_error(GL_NO_ERROR
);
208 glVertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
, stride
, (void *) 0);
210 glEnableVertexAttribArray(0);
212 if (!piglit_check_gl_error(GL_NO_ERROR
)) {
213 piglit_report_result(PIGLIT_FAIL
);
220 piglit_init(int argc
, char **argv
)
222 piglit_require_extension("GL_ARB_post_depth_coverage");
224 glEnable(GL_DEPTH_TEST
);
225 glEnable(GL_STENCIL_TEST
);
226 glEnable(GL_MULTISAMPLE
);
227 glEnable(GL_SAMPLE_SHADING
);
228 glMinSampleShading(1);
229 glClearColor(0.0, 0.0, 0.0, 1.0);
231 prog1
= make_shader_program1();
232 prog2
= make_shader_program2();
235 tex_color
= make_texture_color();
236 tex_depth
= make_texture_depth();
244 int samples
[4] = { 2, 4, 8, 16 };
249 glViewport(0, 0, piglit_width
, piglit_height
);
250 glGetIntegerv(GL_MAX_SAMPLES
, &max_samples
);
252 for (j
= 0; j
< 4 && samples
[j
] <= max_samples
; j
++) {
253 sample_mask
= (GLint
*) calloc (piglit_width
* piglit_height
,
255 glBufferData(GL_SHADER_STORAGE_BUFFER
, sizeof(GLint
) * piglit_width
*
256 piglit_height
, &sample_mask
[0], GL_DYNAMIC_DRAW
);
257 glBindBufferBase(GL_SHADER_STORAGE_BUFFER
, 0, ssbo
);
259 glBindFramebuffer(GL_DRAW_FRAMEBUFFER
, fbo
);
260 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, tex_color
);
261 glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE
, samples
[j
],
262 GL_RGBA8
, piglit_width
, piglit_height
, false);
263 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, tex_depth
);
264 glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE
, samples
[j
],
265 GL_DEPTH24_STENCIL8
, piglit_width
, piglit_height
, false);
267 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
|
268 GL_STENCIL_BUFFER_BIT
);
271 glStencilFunc(GL_ALWAYS
, 1, 0xFF);
272 glStencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
273 glDrawArrays(GL_TRIANGLES
, 0, 6);
276 glStencilFunc(GL_NOTEQUAL
, 1, 0xFF);
277 glStencilOp(GL_KEEP
, GL_KEEP
, GL_KEEP
);
278 glUniform1i(1, piglit_width
);
279 glUniform1i(2, samples
[j
]);
280 glDrawArrays(GL_TRIANGLES
, 6, 6);
282 glGetBufferSubData(GL_SHADER_STORAGE_BUFFER
, 0, sizeof(GLint
) *
283 piglit_width
* piglit_height
, sample_mask
);
285 for (i
= 0; i
< piglit_width
; i
++) {
286 for (k
= 0; k
< piglit_height
; k
++) {
287 if (i
>= piglit_width
/ 2) {
288 if (sample_mask
[piglit_width
* k
+ i
] != 1) {
293 if (sample_mask
[piglit_width
* k
+ i
] != 0) {
304 glBindFramebuffer(GL_DRAW_FRAMEBUFFER
, 0);
305 glBindFramebuffer(GL_READ_FRAMEBUFFER
, fbo
);
306 glDrawBuffer(GL_BACK
);
307 glBlitFramebuffer(0, 0, piglit_width
, piglit_height
, 0, 0, piglit_width
,
308 piglit_height
, GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
310 piglit_present_results();
316 pass
= piglit_check_gl_error(GL_NO_ERROR
) && pass
;
318 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;