framework/replay: disable AA accounting when comparing with no tolerance
[piglit.git] / tests / spec / ext_shader_samples_identical / simple-fs.c
blobe26a2bef6b13a93f2be99389f2b2c68deae092ae
1 /*
2 * Copyright (c) 2015 Intel Corporation
3 * Copyright 2014 VMware, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 /** \file simple-fs.c
26 * Simple fragment shader test for textureSamplesIdenticalEXT.
28 * Render a simple image. Scan the image. At each texel, render green if
29 * textureSamplesIdenticalEXT returns false. If textureSamplesIdenticalEXT
30 * returns true, examine each sample. If the samples are all the same color,
31 * render blue. Render red otherwise. The test passes if there are zero red
32 * pixels and non-zero green pixels.
34 * Much code borrowed from tests/spec/arb_texture_multisample/texelfetch.c.
36 * \note
37 * This is a pretty weak test. A stronger test would read back the original
38 * multisampled image and verify the sample-indenticalness using that.
40 #include "piglit-util-gl.h"
42 PIGLIT_GL_TEST_CONFIG_BEGIN
44 config.supports_gl_compat_version = 30;
45 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
47 PIGLIT_GL_TEST_CONFIG_END
49 #define MAX_SAMPLES 32
51 static const char *vs_src_draw =
52 "#version 130\n"
53 "\n"
54 "in vec4 piglit_vertex;\n"
55 "\n"
56 "void main()\n"
57 "{\n"
58 " gl_Position = piglit_vertex;\n"
59 "}\n"
62 static const char *fs_src_draw =
63 "#version 130\n"
64 "\n"
65 "out vec4 frag_color;\n"
66 "\n"
67 "void main()\n"
68 "{\n"
69 " frag_color = vec4(0.9, 0.8, 0, 1);\n"
70 "}\n"
73 static const char *vs_src_readback =
74 "#version 130\n"
75 "\n"
76 "in vec4 piglit_vertex;\n"
77 "in vec2 piglit_texcoord;\n"
78 "\n"
79 "out vec2 coord_fs;\n"
80 "\n"
81 "void main() {\n"
82 " gl_Position = piglit_vertex;\n"
83 " coord_fs = piglit_texcoord;\n"
84 "}\n"
87 static const char *fs_src_readback =
88 "#version 130\n"
89 "#extension GL_ARB_texture_multisample: require\n"
90 "#extension GL_EXT_shader_samples_identical: require\n"
91 "\n"
92 "uniform sampler2DMS tex;\n"
93 "uniform int num_samples;\n"
94 "\n"
95 "in vec2 coord_fs;\n"
96 "out vec4 frag_color;\n"
97 "\n"
98 "void main()\n"
99 "{\n"
100 "\n"
101 " if (textureSamplesIdenticalEXT(tex, ivec2(coord_fs))) {\n"
102 " frag_color = vec4(0.0, 0.0, 1.0, 1.0);\n"
103 "\n"
104 " /* Verify that all the samples have the same color. */\n"
105 " vec4 base = texelFetch(tex, ivec2(coord_fs), 0);\n"
106 " for (int i = 1; i < num_samples; i++) {\n"
107 " vec4 s = texelFetch(tex, ivec2(coord_fs), i);\n"
108 "\n"
109 " if (s != base)\n"
110 " frag_color = vec4(1.0, 0.0, 0.0, 1.0);\n"
111 " }\n"
112 " } else {\n"
113 " frag_color = vec4(0.0, 1.0, 0.0, 1.0);\n"
114 " }\n"
115 "}\n"
118 static GLuint tex;
119 static GLuint fbo;
120 static GLuint readback_prog, draw_prog;
122 enum piglit_result
123 piglit_display(void)
125 static const GLfloat quad_verts[4][4] = {
126 { 0.8, 0.1, 0, 1 },
127 { 0.1, 1.0, 0, 1 },
128 { -0.1, -1.0, 0, 1 },
129 { -0.8, -0.1, 0, 1 },
131 static const float green[] = { 0.0, 1.0, 0.0, 1.0 };
132 static const float blue[] = { 0.0, 0.0, 1.0, 1.0 };
133 unsigned i;
134 GLfloat image[32 * 32 * 4];
135 unsigned blue_pixels = 0;
136 unsigned green_pixels = 0;
137 bool pass = true;
139 glViewport(0, 0, 32, 32);
141 /* Draw triangle into MSAA texture */
142 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
143 glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
144 glClear(GL_COLOR_BUFFER_BIT);
146 glUseProgram(draw_prog);
147 piglit_draw_rect_from_arrays(quad_verts, NULL, false, 1);
149 /* Scan the previous render. Draw green if the samples are all
150 * (verifiably) the same, blue if there may be differences, and red if
151 * there were differences but textureSamplesIdentical said there were
152 * not.
154 glBindFramebuffer(GL_FRAMEBUFFER, 0);
155 glClearColor(0.25, 0.25, 0.25, 0);
156 glClear(GL_COLOR_BUFFER_BIT);
157 glUseProgram(readback_prog);
159 piglit_draw_rect_tex(-1, -1, 2, 2, 0, 0, 31, 31);
161 glReadPixels(0, 0, 32, 32, GL_RGBA, GL_FLOAT, image);
163 for (i = 0; i < 32 * 32; i++) {
164 if (memcmp(&image[i * 4], blue, sizeof(blue)) == 0) {
165 blue_pixels++;
166 } else if (memcmp(&image[i * 4], green, sizeof(green)) == 0) {
167 green_pixels++;
168 } else {
169 fprintf(stderr,
170 "Bad pixel color @ %u: { %f, %f, %f }\n",
172 image[(i * 4) + 0],
173 image[(i * 4) + 1],
174 image[(i * 4) + 2]);
175 pass = false;
179 printf("Blue pixels: %u\n", blue_pixels);
180 printf("Green pixels: %u\n", green_pixels);
181 piglit_present_results();
183 pass = (green_pixels > 0) && pass;
185 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
189 void
190 piglit_init(int argc, char **argv)
192 GLint num_samples_uniform;
193 int num_samples = 0;
194 GLint max_samples;
196 piglit_require_extension("GL_ARB_texture_multisample");
197 piglit_require_extension("GL_EXT_shader_samples_identical");
199 if (argc < 2) {
200 fprintf(stderr, "Usage: %s <sample_count>\n", argv[0]);
201 piglit_report_result(PIGLIT_SKIP);
204 num_samples = strtoul(argv[1], NULL, 0);
205 glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
206 if (num_samples > max_samples)
207 piglit_report_result(PIGLIT_SKIP);
209 assert(num_samples <= MAX_SAMPLES);
211 /* create MSAA tex and fbo */
212 glGenTextures(1, &tex);
213 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, tex);
214 glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, num_samples,
215 GL_RGBA8,
216 32, 32, GL_TRUE);
218 glGenFramebuffers(1, &fbo);
219 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
220 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
221 GL_TEXTURE_2D_MULTISAMPLE,
222 tex, 0);
223 glDrawBuffer(GL_COLOR_ATTACHMENT0);
225 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
226 piglit_report_result(PIGLIT_FAIL);
228 /* create sample readback shader */
229 readback_prog = piglit_build_simple_program(vs_src_readback,
230 fs_src_readback);
231 glUseProgram(readback_prog);
233 num_samples_uniform = glGetUniformLocation(readback_prog,
234 "num_samples");
235 glUniform1i(num_samples_uniform, num_samples);
237 /* create triangle drawing shader */
238 draw_prog = piglit_build_simple_program(vs_src_draw, fs_src_draw);
239 glUseProgram(draw_prog);
241 if (!piglit_check_gl_error(GL_NO_ERROR))
242 piglit_report_result(PIGLIT_FAIL);
244 glEnable(GL_MULTISAMPLE);