arb_program_interface_query: set vs_input2[1][0] as valid name
[piglit.git] / tests / spec / arb_sample_shading / execution / builtin-gl-sample-id.cpp
blob6690818db03589ced452173b7f5bc630f4129263
1 /*
2 * Copyright © 2013 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 DEALINGS
21 * IN THE SOFTWARE.
24 /** \file builtin-gl-sample-id.cpp
25 * This test verifies that using gl_SampleID in fragment shader program
26 * works as per ARB_sample_shading specification.
28 * Note that we do not have to enable GL_SAMPLE_SHADING_ARB.
29 * The GL_ARB_sample_shading spec (and the GLSL 4.0 spec) say:
31 * 9) Is per-sample shading ever triggered by properties of the fragment
32 * shader?
34 * RESOLVED: Yes. The variables "gl_SampleID" and "gl_SamplePosition"
35 * can be used to read properties of the current sample, which wouldn't
36 * make much sense if the fragment shader were run at a lower frequency
37 * than per-sample.
39 **/
41 #include "piglit-fbo.h"
42 using namespace piglit_util_fbo;
44 const int pattern_width = 128; const int pattern_height = 128;
46 PIGLIT_GL_TEST_CONFIG_BEGIN
48 config.supports_gl_compat_version = 31;
49 config.supports_gl_core_version = 31;
51 config.window_width = pattern_width;
52 config.window_height = pattern_height;
53 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
54 config.khr_no_error_support = PIGLIT_NO_ERRORS;
56 PIGLIT_GL_TEST_CONFIG_END
58 static int num_samples;
59 static unsigned prog_0, prog_1;
60 static Fbo multisampled_fbo, multisampled_tex;
62 static void
63 print_usage_and_exit(char *prog_name)
65 printf("Usage: %s <num_samples>\n", prog_name);
66 piglit_report_result(PIGLIT_FAIL);
69 void
70 compile_shader(void)
72 static const char *vert =
73 "#version 140\n"
74 "in vec4 piglit_vertex;\n"
75 "void main()\n"
76 "{\n"
77 " gl_Position = piglit_vertex;\n"
78 "}\n";
79 static const char *frag_0 =
80 "#version 130\n"
81 "#extension GL_ARB_sample_shading : enable\n"
82 "uniform int samples;\n"
83 "out ivec4 out_color;\n"
84 "void main()\n"
85 "{\n"
86 " out_color = ivec4(0, gl_SampleID, 0, 1);\n"
87 "}\n";
89 static const char *frag_template =
90 "#version 140\n"
91 "%s\n"
92 "uniform %s ms_tex;\n"
93 "uniform int samples;\n"
94 "out vec4 out_color;\n"
95 "void main()\n"
96 "{\n"
97 " int i = 0;\n"
98 " bool pass = true;\n"
99 /* Use do-while to include 'samples = 0' case. */
100 " do {\n"
101 " ivec4 sample_color =\n"
102 " texelFetch(ms_tex, ivec2(gl_FragCoord.xy)%s);\n"
103 " if (sample_color.g != i)\n"
104 " pass = false;\n"
105 " i++;\n"
106 " } while (i < samples);\n"
107 "\n"
108 " if (pass)\n"
109 " out_color = vec4(0.0, 1.0, 0.0, 1.0);\n"
110 " else\n"
111 " out_color = vec4(1.0, 0.0, 0.0, 1.0);\n"
112 "}\n";
114 /* Compile program */
115 prog_0 = piglit_build_simple_program(vert, frag_0);
116 if (!piglit_link_check_status(prog_0)) {
117 piglit_report_result(PIGLIT_FAIL);
121 char *frag_1;
122 if (num_samples)
123 (void)!asprintf(&frag_1, frag_template,
124 "#extension GL_ARB_texture_multisample : require",
125 "isampler2DMS", ", i");
126 else
127 (void)!asprintf(&frag_1, frag_template, "", "isampler2DRect", "");
129 prog_1 = piglit_build_simple_program(vert, frag_1);
130 free(frag_1);
131 if (!piglit_link_check_status(prog_1)) {
132 piglit_report_result(PIGLIT_FAIL);
136 void
137 piglit_init(int argc, char **argv)
139 if (argc != 2)
140 print_usage_and_exit(argv[0]);
142 /* 1st arg: num_samples */
143 char *endptr = NULL;
144 num_samples = strtol(argv[1], &endptr, 0);
145 if (endptr != argv[1] + strlen(argv[1]))
146 print_usage_and_exit(argv[0]);
148 piglit_require_extension("GL_ARB_texture_multisample");
149 piglit_require_extension("GL_ARB_sample_shading");
151 /* Skip the test if num_samples > GL_MAX_SAMPLES */
152 GLint max_samples;
153 glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
154 if (num_samples > max_samples)
155 piglit_report_result(PIGLIT_SKIP);
157 FboConfig msConfig(num_samples, pattern_width, pattern_height);
158 msConfig.color_format = GL_RGBA_INTEGER;
159 msConfig.color_internalformat = GL_RGBA8UI;
160 multisampled_fbo.setup(msConfig);
162 msConfig.num_tex_attachments = 1;
163 msConfig.num_rb_attachments = 0; /* default value is 1 */
164 multisampled_tex.setup(msConfig);
166 compile_shader();
167 if (!piglit_check_gl_error(GL_NO_ERROR)) {
168 piglit_report_result(PIGLIT_FAIL);
172 bool test_builtin_sample_id(const Fbo& ms_fbo)
174 int samples;
175 bool result = true;
176 float expected[4] = {0.0, 1.0, 0.0, 1.0};
178 glUseProgram(prog_0);
179 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, ms_fbo.handle);
180 glGetIntegerv(GL_SAMPLES, &samples);
181 glClear(GL_COLOR_BUFFER_BIT);
182 glUniform1i(glGetUniformLocation(prog_0, "samples"), samples);
183 piglit_draw_rect(-1, -1, 2, 2);
185 if(ms_fbo.config.num_tex_attachments == 0) {
186 /* Blit the framebuffer with multisample renderbuffer attachment
187 * into the framebuffer with multisample texture attachment.
189 glBindFramebuffer(GL_READ_FRAMEBUFFER, ms_fbo.handle);
190 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, multisampled_tex.handle);
191 glClear(GL_COLOR_BUFFER_BIT);
192 glBlitFramebuffer(0, 0,
193 ms_fbo.config.width,
194 ms_fbo.config.height,
195 0, 0,
196 ms_fbo.config.width,
197 ms_fbo.config.height,
198 GL_COLOR_BUFFER_BIT, GL_NEAREST);
201 glBindFramebuffer(GL_READ_FRAMEBUFFER, multisampled_tex.handle);
202 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
203 glClear(GL_COLOR_BUFFER_BIT);
205 glUseProgram(prog_1);
206 glUniform1i(glGetUniformLocation(prog_1, "ms_tex"), 0);
207 glUniform1i(glGetUniformLocation(prog_1, "samples"), samples);
208 piglit_draw_rect(-1, -1, 2, 2);
210 glBindFramebuffer(GL_READ_FRAMEBUFFER, piglit_winsys_fbo);
211 result = piglit_probe_rect_rgba(0, 0, pattern_width,
212 pattern_width, expected)
213 && result;
214 piglit_present_results();
215 printf("FBO attachment = %s, result = %s\n",
216 ms_fbo.config.num_tex_attachments > 0 ?
217 "TEXTURE" :
218 "RENDERBUFFER",
219 result ? "pass" : "fail");
220 return result;
223 enum piglit_result
224 piglit_display()
226 bool pass = true;
227 pass = test_builtin_sample_id(multisampled_tex) && pass;
228 pass = test_builtin_sample_id(multisampled_fbo) && pass;
229 return pass ? PIGLIT_PASS : PIGLIT_FAIL;