fix the spelling in whole piglit
[piglit.git] / tests / spec / arb_sample_shading / execution / interpolate-at-sample-position.cpp
blob9216964da06a2e89c8976a7a0a198237c07da5f0
1 /*
2 * Copyright (c) 2014 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 /**
25 * @file interpolate-at-sample-position.cpp
27 * Tests that 'in' and 'centroid in" variables used at the same time
28 * in a fragment shader are interpolated at sample positions when using
29 * per sample shading.
31 #include "piglit-util-gl.h"
32 #include "piglit-fbo.h"
34 using namespace piglit_util_fbo;
35 const int pattern_width = 128; const int pattern_height = 128;
37 PIGLIT_GL_TEST_CONFIG_BEGIN
38 config.supports_gl_compat_version = 21;
39 config.supports_gl_core_version = 31;
40 config.window_width = 2 * pattern_width;
41 config.window_height = 2 * 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 Fbo multisampled_fbo;
48 static int sample_pos_loc, sample_id_loc, num_samples;
49 static int draw_prog_left, draw_prog_right, test_prog;
51 enum piglit_result
52 piglit_display(void)
54 float pos[2];
55 bool result = true, pass = true;
56 GLenum buffers[2] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
58 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, multisampled_fbo.handle);
59 glDrawBuffers(2, buffers);
60 glClear(GL_COLOR_BUFFER_BIT);
62 /* Draw test image in to left half of multisample fbo*/
63 glUseProgram(draw_prog_left);
64 glViewport(0, 0, pattern_width, pattern_height);
65 glEnable(GL_SAMPLE_SHADING);
66 glMinSampleShading(1.0);
67 piglit_draw_rect(-1, -1, 2, 2);
68 glDisable(GL_SAMPLE_SHADING);
70 for(int i = 0; i < num_samples; i++) {
71 /* Draw reference image in to right half of multisample fbo */
72 glUseProgram(draw_prog_right);
73 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, multisampled_fbo.handle);
74 glDrawBuffers(2, buffers);
75 glGetMultisamplefv(GL_SAMPLE_POSITION, i, pos);
76 glUniform2fv(sample_pos_loc, 1, pos);
77 glEnable(GL_SCISSOR_TEST);
78 glScissor(pattern_width, 0, pattern_width, pattern_height);
79 glViewport(pattern_width, 0, pattern_width, pattern_height);
80 glClear(GL_COLOR_BUFFER_BIT);
81 piglit_draw_rect(-1, -1, 2, 2);
82 glDisable(GL_SCISSOR_TEST);
84 /* Draw sample color from multisample texture in to winsys fbo.
85 * The first color attachment should be in the upper half of
86 * the screen and the second one should be in the lower half.
88 glUseProgram(test_prog);
89 glUniform1i(sample_id_loc, i);
90 glActiveTexture(GL_TEXTURE0);
91 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, multisampled_fbo.color_tex[0]);
92 glActiveTexture(GL_TEXTURE1);
93 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, multisampled_fbo.color_tex[1]);
95 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
96 glDrawBuffer(GL_BACK);
97 glClear(GL_COLOR_BUFFER_BIT);
98 glViewport(0, 0, 2 * pattern_width, 2 * pattern_height);
99 piglit_draw_rect(-1, -1, 2, 2);
101 result = piglit_probe_rect_halves_equal_rgba(0, 0,
102 piglit_width,
103 piglit_height);
104 pass = pass && result;
105 printf("sample_id = %d, result = %s\n", i,
106 result ? "pass" : "fail");
108 piglit_present_results();
109 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
112 static void
113 print_usage_and_exit(char *prog_name)
115 printf("Usage: %s <num_samples>\n", prog_name);
116 piglit_report_result(PIGLIT_FAIL);
119 void
120 piglit_init(int argc, char**argv)
122 if (argc != 2)
123 print_usage_and_exit(argv[0]);
125 /* 1st arg: num_samples */
126 char *endptr = NULL;
127 num_samples = strtol(argv[1], &endptr, 0);
128 if (endptr != argv[1] + strlen(argv[1]))
129 print_usage_and_exit(argv[0]);
131 piglit_require_extension("GL_ARB_texture_multisample");
132 piglit_require_extension("GL_ARB_sample_shading");
133 piglit_require_GLSL_version(130);
135 /* Skip the test if num_samples > GL_MAX_SAMPLES */
136 GLint max_samples;
137 glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
138 if (num_samples == 0 || num_samples > max_samples)
139 piglit_report_result(PIGLIT_SKIP);
141 FboConfig msConfig(num_samples, piglit_width, piglit_height);
142 msConfig.num_rb_attachments = 0;
143 msConfig.num_tex_attachments = 2;
144 msConfig.tex_attachment[1] = GL_COLOR_ATTACHMENT1;
145 multisampled_fbo.setup(msConfig);
147 /* Reduced tolerance for stricter color matching */
148 piglit_set_tolerance_for_bits(16, 16, 16, 16);
149 draw_prog_left = piglit_build_simple_program(
150 "#version 130\n"
151 "#extension GL_ARB_sample_shading: require\n"
152 "in vec4 piglit_vertex;\n"
153 "out vec2 test_center;\n"
154 "centroid out vec2 test_centroid;\n"
155 "void main() {\n"
156 " gl_Position = piglit_vertex;\n"
157 " test_center = piglit_vertex.xy;\n"
158 " test_centroid = piglit_vertex.xy;\n"
159 "}\n",
161 "#version 130\n"
162 "#extension GL_ARB_sample_shading: require\n"
163 "in vec2 test_center;\n"
164 "centroid in vec2 test_centroid;\n"
165 "out vec4 fragdata[2];\n"
166 "void main() {\n"
167 " fragdata[0] = vec4(abs(test_center), 0, 1);\n"
168 " fragdata[1] = vec4(abs(test_centroid), 0, 1);\n"
169 "}\n");
171 draw_prog_right = piglit_build_simple_program(
172 "#version 130\n"
173 "uniform vec2 sample_pos;\n"
174 "in vec4 piglit_vertex;\n"
175 "out vec2 ref;\n"
176 "void main() {\n"
177 " gl_Position = piglit_vertex;\n"
178 " ref = piglit_vertex.xy;\n"
179 /* Add an offset to account for interplolation at
180 * sample position. pattern_width == pattern_height
181 * == 128, so the scaling factor between normalized
182 * device coordinates and pixels is 128/2 == 64.
184 " ref += (sample_pos - 0.5) / 64;\n"
185 "}\n",
187 "#version 130\n"
188 "in vec2 ref;\n"
189 "out vec4 fragdata[2];\n"
190 "void main() {\n"
191 " fragdata[0] = vec4(abs(ref), 0, 1);\n"
192 " fragdata[1] = vec4(abs(ref), 0, 1);\n"
193 "}\n");
194 sample_pos_loc = glGetUniformLocation(draw_prog_right, "sample_pos");
196 test_prog = piglit_build_simple_program(
197 "#version 130\n"
198 "in vec4 piglit_vertex;\n"
199 "void main() {\n"
200 " gl_Position = piglit_vertex;\n"
201 "}\n",
203 "#version 130\n"
204 "#extension GL_ARB_texture_multisample: require\n"
205 "uniform int sample_id;\n"
206 "uniform sampler2DMS tex_center;\n"
207 "uniform sampler2DMS tex_centroid;\n"
208 "out vec4 fragcolor;\n"
209 "void main() {\n"
210 " ivec2 coord = ivec2(gl_FragCoord.xy);\n"
211 " fragcolor = coord.y < 128 ? \n"
212 " texelFetch(tex_center, coord, sample_id) :\n"
213 " texelFetch(tex_centroid, coord - ivec2(0, 128), sample_id);\n"
214 "}\n");
216 glUseProgram(test_prog);
217 glUniform1i(glGetUniformLocation(test_prog, "tex_center"), 0);
218 glUniform1i(glGetUniformLocation(test_prog, "tex_centroid"), 1);
219 sample_id_loc = glGetUniformLocation(test_prog, "sample_id");