ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_post_depth_coverage / sample-shading.c
blobf865a7a8efc69ea05c13642efb83b5744ac8f60a
1 /*
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
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
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 =
45 "#version 430\n"
46 "in vec4 pos_in;\n"
47 "void main()\n"
48 "{\n"
49 " gl_Position = pos_in;\n"
50 "}\n";
52 static const char *fs_text1 =
53 "#version 430\n"
54 "out vec4 color;\n"
55 "void main()\n"
56 "{\n"
57 " gl_FragDepth = 0.5f;\n"
58 " color = vec4(0.0, 1.0, 0.0, 1.0);\n"
59 "}\n";
61 static const char *fs_text2 =
62 "#version 430\n"
63 "#extension GL_ARB_post_depth_coverage: enable\n"
64 "out vec4 color;\n"
65 "layout(early_fragment_tests) in;\n"
66 "layout(post_depth_coverage) in;\n"
67 "layout(std430, binding = 0) buffer MaskOutput {\n"
68 " int data[];\n"
69 "} mask_output;\n"
70 "layout(location = 1) uniform int width;\n"
71 "layout(location = 2) uniform int samples;\n"
72 "void main()\n"
73 "{\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"
77 " } else {\n"
78 " mask_output.data[index] = 0;\n"
79 " }\n"
80 " color = vec4(1.0, 0.0, 0.0, 1.0);\n"
81 "}\n";
83 static GLuint
84 make_shader_program1(void)
86 GLuint prog;
88 prog = piglit_build_simple_program(vs_text, fs_text1);
89 glUseProgram(prog);
91 glBindAttribLocation(prog, 0, "pos_in");
93 glLinkProgram(prog);
95 if (!piglit_check_gl_error(GL_NO_ERROR)) {
96 piglit_report_result(PIGLIT_FAIL);
99 return prog;
102 static GLuint
103 make_shader_program2(void)
105 GLuint prog;
107 prog = piglit_build_simple_program(vs_text, fs_text2);
108 glUseProgram(prog);
110 glBindAttribLocation(prog, 0, "pos_in");
112 glLinkProgram(prog);
114 if (!piglit_check_gl_error(GL_NO_ERROR)) {
115 piglit_report_result(PIGLIT_FAIL);
118 return prog;
121 static GLuint
122 make_ssbo(void)
124 GLuint ssbo;
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);
132 return ssbo;
135 static GLuint
136 make_fbo(void)
138 GLuint fbo;
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);
149 return fbo;
152 static GLuint
153 make_texture_color(void)
155 GLuint tex;
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);
163 return tex;
166 static GLuint
167 make_texture_depth(void)
169 GLuint tex;
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);
177 return tex;
180 static GLuint
181 make_vao(void)
183 static const float pos_tc[12][2] = {
184 { -1.0, -1.0 },
185 { 0.0, -1.0 },
186 { 0.0, 1.0 },
187 { 0.0, 1.0 },
188 { -1.0, 1.0 },
189 { -1.0, -1.0 },
190 { -1.0, -1.0 },
191 { 1.0, -1.0 },
192 { 1.0, 1.0 },
193 { 1.0, 1.0 },
194 { -1.0, 1.0 },
195 { -1.0, -1.0 }
197 const int stride = sizeof(pos_tc[0]);
198 GLuint vbo, vao;
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);
216 return vbo;
219 void
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();
233 vao = make_vao();
234 ssbo = make_ssbo();
235 tex_color = make_texture_color();
236 tex_depth = make_texture_depth();
237 fbo = make_fbo();
241 enum piglit_result
242 piglit_display(void)
244 int samples[4] = { 2, 4, 8, 16 };
245 int max_samples;
246 bool pass = true;
247 int i, j, k;
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,
254 sizeof(GLint));
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);
270 glUseProgram(prog1);
271 glStencilFunc(GL_ALWAYS, 1, 0xFF);
272 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
273 glDrawArrays(GL_TRIANGLES, 0, 6);
275 glUseProgram(prog2);
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) {
289 pass = false;
290 break;
292 } else {
293 if (sample_mask[piglit_width * k + i] != 0) {
294 pass = false;
295 break;
300 if (!pass)
301 break;
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();
311 free(sample_mask);
312 if (!pass)
313 break;
316 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
318 return pass ? PIGLIT_PASS : PIGLIT_FAIL;