arb_program_interface_query: set vs_input2[1][0] as valid name
[piglit.git] / tests / spec / arb_sample_shading / execution / builtin-gl-num-samples.cpp
blob52965bf47755e633bbfd10ae5f4a467d6b3e7b18
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-num-samples.cpp
25 * This test verifies that using gl_NumSamples in fragment shader
26 * program works as per ARB_sample_shading specification.
28 **/
30 #include "piglit-fbo.h"
31 using namespace piglit_util_fbo;
33 const int pattern_width = 128; const int pattern_height = 128;
35 PIGLIT_GL_TEST_CONFIG_BEGIN
37 config.supports_gl_compat_version = 21;
38 config.supports_gl_core_version = 31;
40 config.window_width = pattern_width;
41 config.window_height = pattern_height;
42 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
43 config.khr_no_error_support = PIGLIT_NO_ERRORS;
45 PIGLIT_GL_TEST_CONFIG_END
47 static int num_samples;
48 static unsigned prog;
49 static Fbo multisampled_fbo, singlesampled_fbo;
51 static void
52 print_usage_and_exit(char *prog_name)
54 printf("Usage: %s <num_samples>\n", prog_name);
55 piglit_report_result(PIGLIT_FAIL);
58 void
59 compile_shader(void)
61 static const char *vert =
62 "#version 130\n"
63 "in vec4 piglit_vertex;\n"
64 "void main()\n"
65 "{\n"
66 " gl_Position = piglit_vertex;\n"
67 "}\n";
68 static const char *frag =
69 "#version 130\n"
70 "#extension GL_ARB_sample_shading : require\n"
71 "uniform int samples;\n"
72 "out vec4 out_color;\n"
73 "void main()\n"
74 "{\n"
75 " if (gl_NumSamples == samples)\n"
76 " out_color = vec4(0.0, 1.0, 0.0, 1.0);\n"
77 " else\n"
78 " out_color = vec4(1.0, 0.0, 0.0, 1.0);\n"
79 "}\n";
80 /* Compile program */
81 prog = piglit_build_simple_program(vert, frag);
82 if (!piglit_link_check_status(prog)) {
83 piglit_report_result(PIGLIT_FAIL);
87 void
88 piglit_init(int argc, char **argv)
90 if (argc != 2)
91 print_usage_and_exit(argv[0]);
93 /* 1st arg: num_samples */
94 char *endptr = NULL;
95 num_samples = strtol(argv[1], &endptr, 0);
96 if (endptr != argv[1] + strlen(argv[1]))
97 print_usage_and_exit(argv[0]);
99 piglit_require_extension("GL_ARB_vertex_array_object");
100 piglit_require_extension("GL_ARB_sample_shading");
101 piglit_require_GLSL_version(130);
103 /* Skip the test if num_samples > GL_MAX_SAMPLES */
104 GLint max_samples;
105 glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
106 if (num_samples > max_samples)
107 piglit_report_result(PIGLIT_SKIP);
109 singlesampled_fbo.setup(FboConfig(0,
110 pattern_width,
111 pattern_height));
113 FboConfig msConfig(num_samples, pattern_width, pattern_height);
114 multisampled_fbo.setup(msConfig);
116 compile_shader();
117 if (!piglit_check_gl_error(GL_NO_ERROR)) {
118 piglit_report_result(PIGLIT_FAIL);
122 bool test_builtin_num_samples(const Fbo& ms_fbo)
124 GLint samples;
125 GLfloat expected[4] = {0.0, 1.0, 0.0, 1.0};
126 bool pass = true;
128 glUseProgram(prog);
129 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, ms_fbo.handle);
130 glGetIntegerv(GL_SAMPLES, &samples);
131 glUniform1i(glGetUniformLocation(prog, "samples"), MAX2(1, samples));
133 glClear(GL_COLOR_BUFFER_BIT);
134 piglit_draw_rect(-1, -1, 2, 2);
136 glBindFramebuffer(GL_READ_FRAMEBUFFER, ms_fbo.handle);
137 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, singlesampled_fbo.handle);
138 glClear(GL_COLOR_BUFFER_BIT);
139 glBlitFramebuffer(0, 0,
140 pattern_width, pattern_height,
141 0, 0,
142 pattern_width, pattern_height,
143 GL_COLOR_BUFFER_BIT,
144 GL_NEAREST);
146 glBindFramebuffer(GL_READ_FRAMEBUFFER, singlesampled_fbo.handle);
147 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
149 pass = piglit_probe_rect_rgba(0, 0, pattern_width, pattern_height,
150 expected) && pass;
151 glClear(GL_COLOR_BUFFER_BIT);
152 glBlitFramebuffer(0, 0,
153 pattern_width, pattern_height,
154 0, 0,
155 pattern_width, pattern_height,
156 GL_COLOR_BUFFER_BIT,
157 GL_NEAREST);
159 piglit_present_results();
160 return pass;
163 enum piglit_result
164 piglit_display()
166 bool pass = true;
167 pass = test_builtin_num_samples(multisampled_fbo)
168 && pass;
169 return pass ? PIGLIT_PASS : PIGLIT_FAIL;