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
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
24 * Fredrik Höglund (fredrik@kde.org)
27 /** @file glsl-kwin-blur-2.c
29 * Tests the blur effect used by the KWin window manager,
30 * with a 12 pixel blur radius (uses 13 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
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 the default radius (12 pixels). The code generator makes
56 sure 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"
68 "varying vec2 samplePos7;\n"
69 "varying vec2 samplePos8;\n"
70 "varying vec2 samplePos9;\n"
71 "varying vec2 samplePos10;\n"
72 "varying vec2 samplePos11;\n"
73 "varying vec2 samplePos12;\n"
77 " vec2 center = vec4(gl_TextureMatrix[0] * gl_MultiTexCoord0).st;\n"
79 " samplePos0 = center + pixelSize * vec2(-11.5);\n"
80 " samplePos1 = center + pixelSize * vec2(-9.5);\n"
81 " samplePos2 = center + pixelSize * vec2(-7.5);\n"
82 " samplePos3 = center + pixelSize * vec2(-5.5);\n"
83 " samplePos4 = center + pixelSize * vec2(-3.5);\n"
84 " samplePos5 = center + pixelSize * vec2(-1.5);\n"
85 " samplePos6 = center;\n"
86 " samplePos7 = center + pixelSize * vec2(1.5);\n"
87 " samplePos8 = center + pixelSize * vec2(3.5);\n"
88 " samplePos9 = center + pixelSize * vec2(5.5);\n"
89 " samplePos10 = center + pixelSize * vec2(7.5);\n"
90 " samplePos11 = center + pixelSize * vec2(9.5);\n"
91 " samplePos12 = center + pixelSize * vec2(11.5);\n"
93 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
97 This shader doesn't use the += operator because the old GLSL
98 compiler in Mesa didn't emit MAD's when it was used.
99 This isn't an issue with the new GLSL2 compiler.
101 static const char fs_code
[] =
102 "uniform sampler2D texUnit;\n"
104 "varying vec2 samplePos0;\n"
105 "varying vec2 samplePos1;\n"
106 "varying vec2 samplePos2;\n"
107 "varying vec2 samplePos3;\n"
108 "varying vec2 samplePos4;\n"
109 "varying vec2 samplePos5;\n"
110 "varying vec2 samplePos6;\n"
111 "varying vec2 samplePos7;\n"
112 "varying vec2 samplePos8;\n"
113 "varying vec2 samplePos9;\n"
114 "varying vec2 samplePos10;\n"
115 "varying vec2 samplePos11;\n"
116 "varying vec2 samplePos12;\n"
118 "const vec4 kernel0 = vec4(0.00951198);\n"
119 "const vec4 kernel1 = vec4(0.0236653);\n"
120 "const vec4 kernel2 = vec4(0.0494943);\n"
121 "const vec4 kernel3 = vec4(0.0870162);\n"
122 "const vec4 kernel4 = vec4(0.128602);\n"
123 "const vec4 kernel5 = vec4(0.15977);\n"
124 "const vec4 kernel6 = vec4(0.0838822);\n"
128 " vec4 sum = texture2D(texUnit, samplePos0) * kernel0;\n"
129 " sum = sum + texture2D(texUnit, samplePos1) * kernel1;\n"
130 " sum = sum + texture2D(texUnit, samplePos2) * kernel2;\n"
131 " sum = sum + texture2D(texUnit, samplePos3) * kernel3;\n"
132 " sum = sum + texture2D(texUnit, samplePos4) * kernel4;\n"
133 " sum = sum + texture2D(texUnit, samplePos5) * kernel5;\n"
134 " sum = sum + texture2D(texUnit, samplePos6) * kernel6;\n"
135 " sum = sum + texture2D(texUnit, samplePos7) * kernel5;\n"
136 " sum = sum + texture2D(texUnit, samplePos8) * kernel4;\n"
137 " sum = sum + texture2D(texUnit, samplePos9) * kernel3;\n"
138 " sum = sum + texture2D(texUnit, samplePos10) * kernel2;\n"
139 " sum = sum + texture2D(texUnit, samplePos11) * kernel1;\n"
140 " sum = sum + texture2D(texUnit, samplePos12) * kernel0;\n"
141 " gl_FragColor = sum;\n"
144 static const int expected_edge
[] = {
145 0x00, 0x01, 0x02, 0x05, 0x08, 0x0f, 0x15, 0x20, 0x2b, 0x3c, 0x4c, 0x60,
146 0x75, 0x8a, 0x9f, 0xb3, 0xc3, 0xd4, 0xdf, 0xea, 0xf0, 0xf7, 0xfa, 0xfd,
150 static const int expected_corner
[] = {
151 0x00, 0x01, 0x02, 0x04, 0x07, 0x0e, 0x17, 0x24, 0x36, 0x4b, 0x63, 0x7e,
152 0x95, 0xb0, 0xc3, 0xd7, 0xe2, 0xef, 0xf5, 0xfb, 0xfd, 0xff
155 static GLuint
setup_shaders()
159 vs
= piglit_compile_shader_text(GL_VERTEX_SHADER
, vs_code
);
160 fs
= piglit_compile_shader_text(GL_FRAGMENT_SHADER
, fs_code
);
161 prog
= piglit_link_simple_program(vs
, fs
);
169 static GLboolean
test()
171 GLboolean pass
= GL_TRUE
;
173 /* Prepare the shaders */
174 GLint prog
= setup_shaders();
175 GLint uPixelSize
= glGetUniformLocation(prog
, "pixelSize");
176 GLint uTexUnit
= glGetUniformLocation(prog
, "texUnit");
180 /* Pixel sizes in texture coordinates for the horizontal and vertical passes */
181 const float horizontal
[2] = { 1.0 / WIDTH
, 0 };
182 const float vertical
[2] = { 0, 1.0 / HEIGHT
};
184 /* Texture and vertex coordinates */
185 const float tc
[] = { 0,1, 1,1, 0,0, 0,0, 1,1, 1,0 };
186 const float vc
[] = { -1,1, 1,1, -1,-1, -1,-1, 1,1, 1,-1 };
188 /* Draw the rectangle that we're going to blur */
189 piglit_draw_rect(-.5, -.5, 1, 1);
191 /* Create a scratch texture */
192 glGenTextures(1, &scratchTex
);
193 glBindTexture(GL_TEXTURE_2D
, scratchTex
);
194 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
195 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
196 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
197 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
198 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA8
, WIDTH
, HEIGHT
, 0, GL_BGRA
, GL_UNSIGNED_BYTE
, 0);
201 glUniform1i(uTexUnit
, 0);
203 glEnableClientState(GL_VERTEX_ARRAY
);
204 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
206 glTexCoordPointer(2, GL_FLOAT
, 0, tc
);
207 glVertexPointer(2, GL_FLOAT
, 0, vc
);
209 /* Horizontal pass */
210 glCopyTexSubImage2D(GL_TEXTURE_2D
, 0, 0, 0, 0, 0, WIDTH
, HEIGHT
);
211 glUniform2fv(uPixelSize
, 1, horizontal
);
212 glDrawArrays(GL_TRIANGLES
, 0, 6);
215 glCopyTexSubImage2D(GL_TEXTURE_2D
, 0, 0, 0, 0, 0, WIDTH
, HEIGHT
);
216 glUniform2fv(uPixelSize
, 1, vertical
);
217 glDrawArrays(GL_TRIANGLES
, 0, 6);
221 glBindTexture(GL_TEXTURE_2D
, 0);
222 glDeleteTextures(1, &scratchTex
);
223 glDeleteProgram(prog
);
225 glDisableClientState(GL_VERTEX_ARRAY
);
226 glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
228 if (!piglit_check_gl_error(GL_NO_ERROR
))
229 piglit_report_result(PIGLIT_FAIL
);
232 for (i
= 0; i
< 26; i
++) {
234 color
[0] = expected_edge
[i
] / 255.;
237 pass
= piglit_probe_pixel_rgb(50, 12 + i
, color
) && pass
;
238 pass
= piglit_probe_pixel_rgb(50, HEIGHT
- 13 - i
, color
) && pass
;
239 pass
= piglit_probe_pixel_rgb(12 + i
, 50, color
) && pass
;
240 pass
= piglit_probe_pixel_rgb(WIDTH
- 13 - i
, 50, color
) && pass
;
243 /* Test the corners */
244 for (i
= 0; i
< 22; i
++) {
246 color
[0] = expected_corner
[i
] / 255.;
249 pass
= piglit_probe_pixel_rgb(16 + i
, 16 + i
, color
) && pass
;
250 pass
= piglit_probe_pixel_rgb(16 + i
, HEIGHT
- 17 - i
, color
) && pass
;
251 pass
= piglit_probe_pixel_rgb(WIDTH
- 17 - i
, 16 + i
, color
) && pass
;
252 pass
= piglit_probe_pixel_rgb(WIDTH
- 17 - i
, HEIGHT
- 17 - i
, color
) && pass
;
258 enum piglit_result
piglit_display(void)
262 glViewport(0, 0, WIDTH
, HEIGHT
);
263 glClearColor(0.0, 0.0, 0.0, 0.0);
264 glClear(GL_COLOR_BUFFER_BIT
);
268 piglit_present_results();
270 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
273 void piglit_init(int argc
, char **argv
)
277 piglit_require_gl_version(20);
279 glGetIntegerv(GL_MAX_VARYING_FLOATS
, &value
);
280 if (value
< (13 * 4)) {
281 printf("Requires at least 13 varyings\n");
282 piglit_report_result(PIGLIT_SKIP
);