glx-multithread-texture: Avoid front-buffer rendering.
[piglit.git] / tests / shaders / glsl-kwin-blur-1.c
blobb02641f192011bc75730d8f3ee310c3cdb60793d
1 /*
2 * Copyright © 2010 Fredrik Höglund (fredrik@kde.org)
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.
23 * Authors:
24 * Fredrik Höglund (fredrik@kde.org)
27 /** @file glsl-kwin-blur-1.c
29 * Tests the blur effect used by the KWin window manager,
30 * with a 6 pixel blur radius (uses 7 varyings).
33 #include "piglit-util-gl.h"
35 PIGLIT_GL_TEST_CONFIG_BEGIN
37 config.supports_gl_compat_version = 10;
39 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
40 config.khr_no_error_support = PIGLIT_NO_ERRORS;
42 PIGLIT_GL_TEST_CONFIG_END
45 /* Size of viewport and test region. Note that there are pixel probes at
46 * specific locations.
48 #define WIDTH 100
49 #define HEIGHT 100
53 Note: In KWin, the code for these shaders is generated at runtime,
54 based on the blur radius. This is what the code looks like
55 with a 6 pixel blur radius. The code generator makes sure
56 that the code doesn't exceed GL_MAX_VARYING_FLOATS.
58 static const char vs_code[] =
59 "uniform vec2 pixelSize;\n"
61 "varying vec2 samplePos0;\n"
62 "varying vec2 samplePos1;\n"
63 "varying vec2 samplePos2;\n"
64 "varying vec2 samplePos3;\n"
65 "varying vec2 samplePos4;\n"
66 "varying vec2 samplePos5;\n"
67 "varying vec2 samplePos6;\n"
69 "void main(void)\n"
70 "{\n"
71 " vec2 center = vec4(gl_TextureMatrix[0] * gl_MultiTexCoord0).st;\n"
73 " samplePos0 = center + pixelSize * vec2(-5.5);\n"
74 " samplePos1 = center + pixelSize * vec2(-3.5);\n"
75 " samplePos2 = center + pixelSize * vec2(-1.5);\n"
76 " samplePos3 = center;\n"
77 " samplePos4 = center + pixelSize * vec2(1.5);\n"
78 " samplePos5 = center + pixelSize * vec2(3.5);\n"
79 " samplePos6 = center + pixelSize * vec2(5.5);\n"
81 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
82 "}\n";
85 This shader doesn't use the += operator because the old GLSL
86 compiler in Mesa didn't emit MAD's when it was used.
87 This isn't an issue with the new GLSL2 compiler.
89 static const char fs_code[] =
90 "uniform sampler2D texUnit;\n"
92 "varying vec2 samplePos0;\n"
93 "varying vec2 samplePos1;\n"
94 "varying vec2 samplePos2;\n"
95 "varying vec2 samplePos3;\n"
96 "varying vec2 samplePos4;\n"
97 "varying vec2 samplePos5;\n"
98 "varying vec2 samplePos6;\n"
100 "const vec4 kernel0 = vec4(0.0242836);\n"
101 "const vec4 kernel1 = vec4(0.11585);\n"
102 "const vec4 kernel2 = vec4(0.275987);\n"
103 "const vec4 kernel3 = vec4(0.167758);\n"
105 "void main(void)\n"
106 "{\n"
107 " vec4 sum = texture2D(texUnit, samplePos0) * kernel0;\n"
108 " sum = sum + texture2D(texUnit, samplePos1) * kernel1;\n"
109 " sum = sum + texture2D(texUnit, samplePos2) * kernel2;\n"
110 " sum = sum + texture2D(texUnit, samplePos3) * kernel3;\n"
111 " sum = sum + texture2D(texUnit, samplePos4) * kernel2;\n"
112 " sum = sum + texture2D(texUnit, samplePos5) * kernel1;\n"
113 " sum = sum + texture2D(texUnit, samplePos6) * kernel0;\n"
114 " gl_FragColor = sum;\n"
115 "}\n";
117 static const int expected_edge[] = {
118 0x00, 0x03, 0x06, 0x15, 0x24, 0x47, 0x6a, 0x95, 0xb8, 0xdb, 0xea, 0xf9,
119 0xfc, 0xff
122 static const int expected_corner[] = {
123 0x00, 0x02, 0x05, 0x14, 0x2c, 0x57, 0x85, 0xbc, 0xd7, 0xf3, 0xf9, 0xff
126 static GLuint setup_shaders()
128 GLuint vs, fs, prog;
130 vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vs_code);
131 fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fs_code);
132 prog = piglit_link_simple_program(vs, fs);
134 glDeleteShader(vs);
135 glDeleteShader(fs);
137 return prog;
140 static GLboolean test()
142 GLboolean pass = GL_TRUE;
144 /* Prepare the shaders */
145 GLint prog = setup_shaders();
146 GLint uPixelSize = glGetUniformLocation(prog, "pixelSize");
147 GLint uTexUnit = glGetUniformLocation(prog, "texUnit");
148 GLuint scratchTex;
149 int i;
151 /* Pixel sizes in texture coordinates for the horizontal and vertical passes */
152 const float horizontal[2] = { 1.0 / WIDTH, 0 };
153 const float vertical[2] = { 0, 1.0 / HEIGHT };
155 /* Texture and vertex coordinates */
156 const float tc[] = { 0,1, 1,1, 0,0, 0,0, 1,1, 1,0 };
157 const float vc[] = { -1,1, 1,1, -1,-1, -1,-1, 1,1, 1,-1 };
159 /* Draw the rectangle that we're going to blur */
160 piglit_draw_rect(-.5, -.5, 1, 1);
162 /* Create a scratch texture */
163 glGenTextures(1, &scratchTex);
164 glBindTexture(GL_TEXTURE_2D, scratchTex);
165 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
166 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
168 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
169 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, WIDTH, HEIGHT, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
171 glUseProgram(prog);
172 glUniform1i(uTexUnit, 0);
174 glEnableClientState(GL_VERTEX_ARRAY);
175 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
177 glTexCoordPointer(2, GL_FLOAT, 0, tc);
178 glVertexPointer(2, GL_FLOAT, 0, vc);
180 /* Horizontal pass */
181 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, WIDTH, HEIGHT);
182 glUniform2fv(uPixelSize, 1, horizontal);
183 glDrawArrays(GL_TRIANGLES, 0, 6);
185 /* Vertical pass */
186 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, WIDTH, HEIGHT);
187 glUniform2fv(uPixelSize, 1, vertical);
188 glDrawArrays(GL_TRIANGLES, 0, 6);
190 /* Clean up */
191 glUseProgram(0);
192 glBindTexture(GL_TEXTURE_2D, 0);
193 glDeleteTextures(1, &scratchTex);
194 glDeleteProgram(prog);
196 glDisableClientState(GL_VERTEX_ARRAY);
197 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
199 if (!piglit_check_gl_error(GL_NO_ERROR))
200 piglit_report_result(PIGLIT_FAIL);
202 /* Test the sides */
203 for (i = 0; i < 14; i++) {
204 float color[3];
205 color[0] = expected_edge[i] / 255.;
206 color[1] = color[0];
207 color[2] = color[0];
208 pass = piglit_probe_pixel_rgb(50, 18 + i, color) && pass;
209 pass = piglit_probe_pixel_rgb(50, HEIGHT - 19 - i, color) && pass;
210 pass = piglit_probe_pixel_rgb(18 + i, 50, color) && pass;
211 pass = piglit_probe_pixel_rgb(WIDTH - 19 - i, 50, color) && pass;
214 /* Test the corners */
215 for (i = 0; i < 12; i++) {
216 float color[3];
217 color[0] = expected_corner[i] / 255.;
218 color[1] = color[0];
219 color[2] = color[0];
220 pass = piglit_probe_pixel_rgb(20 + i, 20 + i, color) && pass;
221 pass = piglit_probe_pixel_rgb(20 + i, HEIGHT - 21 - i, color) && pass;
222 pass = piglit_probe_pixel_rgb(WIDTH - 21 - i, 20 + i, color) && pass;
223 pass = piglit_probe_pixel_rgb(WIDTH - 21 - i, HEIGHT - 21 - i, color) && pass;
226 return pass;
229 enum piglit_result piglit_display(void)
231 GLboolean pass;
233 glViewport(0, 0, WIDTH, HEIGHT);
234 glClearColor(0.0, 0.0, 0.0, 0.0);
235 glClear(GL_COLOR_BUFFER_BIT);
237 pass = test();
239 piglit_present_results();
241 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
244 void piglit_init(int argc, char **argv)
246 piglit_require_gl_version(20);