ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_post_depth_coverage / basic.c
blob07f6ab1afac49762a5834f4a370c81662bcacc17
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 rudimentary test to check whether the correct values are being written
26 * to gl_SampleMaskIn when ARB_post_depth_coverage is enabled.
29 #include "piglit-util-gl.h"
31 PIGLIT_GL_TEST_CONFIG_BEGIN
32 config.supports_gl_compat_version = 43;
33 config.supports_gl_core_version = 43;
34 config.window_width = 160;
35 config.window_height = 160;
36 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DEPTH | PIGLIT_GL_VISUAL_DOUBLE;
37 PIGLIT_GL_TEST_CONFIG_END
39 static GLuint prog1, prog2, vao, ssbo;
40 static GLint *sample_mask;
42 static const char *vs_text =
43 "#version 430\n"
44 "in vec4 pos_in;\n"
45 "void main()\n"
46 "{\n"
47 " gl_Position = pos_in;\n"
48 "}\n";
50 static const char *fs_text1 =
51 "#version 430\n"
52 "out vec4 color;\n"
53 "void main()\n"
54 "{\n"
55 " gl_FragDepth = 0.5f;\n"
56 " color = vec4(0.0, 1.0, 0.0, 1.0);\n"
57 "}\n";
59 static const char *fs_text2 =
60 "#version 430\n"
61 "#extension GL_ARB_post_depth_coverage: enable\n"
62 "out vec4 color;\n"
63 "layout(early_fragment_tests) in;\n"
64 "layout(post_depth_coverage) in;\n"
65 "layout(location = 2) uniform int width;"
66 "layout(std430, binding = 3) buffer MaskOutput {"
67 " int data[];"
68 "} mask_output;"
69 "void main()\n"
70 "{\n"
71 " int index = int(gl_FragCoord.y) * width + int(gl_FragCoord.x);\n"
72 " mask_output.data[index] = int(gl_SampleMaskIn[0]);\n"
73 " color = vec4(1.0, 0.0, 0.0, 1.0);\n"
74 "}\n";
76 static GLuint
77 make_shader_program1(void)
79 GLuint prog;
81 prog = piglit_build_simple_program(vs_text, fs_text1);
82 glUseProgram(prog);
84 glBindAttribLocation(prog, 0, "pos_in");
86 glLinkProgram(prog);
88 if (!piglit_check_gl_error(GL_NO_ERROR)) {
89 piglit_report_result(PIGLIT_FAIL);
92 return prog;
95 static GLuint
96 make_shader_program2(void)
98 GLuint prog;
100 prog = piglit_build_simple_program(vs_text, fs_text2);
101 glUseProgram(prog);
103 glBindAttribLocation(prog, 0, "pos_in");
105 glLinkProgram(prog);
107 if (!piglit_check_gl_error(GL_NO_ERROR)) {
108 piglit_report_result(PIGLIT_FAIL);
111 return prog;
115 static GLuint
116 make_ssbo(void)
118 GLuint ssbo;
119 glGenBuffers(1, &ssbo);
120 glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
122 if (!piglit_check_gl_error(GL_NO_ERROR)) {
123 piglit_report_result(PIGLIT_FAIL);
126 return ssbo;
129 static GLuint
130 make_vao(void)
132 static const float pos_tc[12][2] = {
133 { -1.0, -1.0 },
134 { 0.0, -1.0 },
135 { 0.0, 1.0 },
136 { 0.0, 1.0 },
137 { -1.0, 1.0 },
138 { -1.0, -1.0 },
139 { -1.0, -1.0 },
140 { 1.0, -1.0 },
141 { 1.0, 1.0 },
142 { 1.0, 1.0 },
143 { -1.0, 1.0 },
144 { -1.0, -1.0 }
146 const int stride = sizeof(pos_tc[0]);
147 GLuint vbo, vao;
149 glGenVertexArrays(1, &vao);
150 glBindVertexArray(vao);
152 glGenBuffers(1, &vbo);
153 glBindBuffer(GL_ARRAY_BUFFER, vbo);
154 glBufferData(GL_ARRAY_BUFFER, sizeof(pos_tc), pos_tc, GL_STATIC_DRAW);
155 piglit_check_gl_error(GL_NO_ERROR);
157 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, stride, (void *) 0);
159 glEnableVertexAttribArray(0);
161 if (!piglit_check_gl_error(GL_NO_ERROR)) {
162 piglit_report_result(PIGLIT_FAIL);
165 return vbo;
169 void
170 piglit_init(int argc, char **argv)
172 piglit_require_extension("GL_ARB_post_depth_coverage");
173 prog1 = make_shader_program1();
174 prog2 = make_shader_program2();
175 vao = make_vao();
176 ssbo = make_ssbo();
180 enum piglit_result
181 piglit_display(void)
183 float green[4] = {0.0, 1.0, 0.0, 1.0};
184 float red[4] = {1.0, 0.0, 0.0, 1.0};
185 bool pass = true;
186 int i, j;
188 glEnable(GL_DEPTH_TEST);
189 glViewport(0, 0, piglit_width, piglit_height);
190 sample_mask = (GLint*) malloc (sizeof(GLint) * (piglit_width * piglit_height));
191 for (i = 0; i < piglit_width * piglit_height; i++) {
192 sample_mask[i] = 0;
194 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLint) * (piglit_width *
195 piglit_height), &sample_mask[0], GL_DYNAMIC_COPY);
196 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, ssbo);
198 glClearColor(0.0, 0.0, 0.0, 1.0);
199 glEnable(GL_DEPTH_TEST);
200 glEnable(GL_STENCIL_TEST);
201 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
202 glUseProgram(prog1);
203 glStencilFunc(GL_ALWAYS, 1, 0xFF);
204 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
205 glDrawArrays(GL_TRIANGLES, 0, 6);
206 glUseProgram(prog2);
207 glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
208 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
209 glUniform1i(2, piglit_width);
210 glDrawArrays(GL_TRIANGLES, 6, 6);
212 glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(GLint) *
213 piglit_width * piglit_height, &sample_mask[0]);
215 for (i = 0; i < piglit_width; i++) {
216 for (j = 0; j < piglit_height; j++) {
217 if (i >= piglit_width / 2) {
218 if (sample_mask[piglit_width * j + i] != 1) {
219 pass = false;
220 break;
222 } else {
223 if (sample_mask[piglit_width * j + i] != 0) {
224 pass = false;
225 break;
231 pass = piglit_probe_rect_rgba(0, 0, piglit_width / 2, piglit_height,
232 green) && pass;
233 pass = piglit_probe_rect_rgba(piglit_width / 2, 0, piglit_width / 2,
234 piglit_height, red) && pass;
235 piglit_present_results();
237 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
239 free(sample_mask);
240 return pass ? PIGLIT_PASS : PIGLIT_FAIL;