summaryrefslogtreecommitdiff
path: root/tests/shaders/glsl-kwin-blur-1.c
blob: 32a6335b8ec9887422700eb767ab47e65fb838f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright © 2010 Fredrik Höglund (fredrik@kde.org)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Fredrik Höglund (fredrik@kde.org)
 */

/** @file glsl-kwin-blur-1.c
 *
 * Tests the blur effect used by the KWin window manager,
 * with a 6 pixel blur radius (uses 7 varyings).
 */

#include "piglit-util-gl-common.h"

PIGLIT_GL_TEST_MAIN(
    100 /*window_width*/,
    100 /*window_height*/,
    GLUT_RGB | GLUT_DOUBLE)

/*
    Note: In KWin, the code for these shaders is generated at runtime,
          based on the blur radius. This is what the code looks like
          with a 6 pixel blur radius. The code generator makes sure
          that the code doesn't exceed GL_MAX_VARYING_FLOATS.
*/
static const char vs_code[] =
    "uniform vec2 pixelSize;\n"

    "varying vec2 samplePos0;\n"
    "varying vec2 samplePos1;\n"
    "varying vec2 samplePos2;\n"
    "varying vec2 samplePos3;\n"
    "varying vec2 samplePos4;\n"
    "varying vec2 samplePos5;\n"
    "varying vec2 samplePos6;\n"

    "void main(void)\n"
    "{\n"
    "    vec2 center = vec4(gl_TextureMatrix[0] * gl_MultiTexCoord0).st;\n"

    "    samplePos0 = center + pixelSize * vec2(-5.5);\n"
    "    samplePos1 = center + pixelSize * vec2(-3.5);\n"
    "    samplePos2 = center + pixelSize * vec2(-1.5);\n"
    "    samplePos3 = center;\n"
    "    samplePos4 = center + pixelSize * vec2(1.5);\n"
    "    samplePos5 = center + pixelSize * vec2(3.5);\n"
    "    samplePos6 = center + pixelSize * vec2(5.5);\n"

    "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
    "}\n";

/*
    This shader doesn't use the += operator because the old GLSL
    compiler in Mesa didn't emit MAD's when it was used.
    This isn't an issue with the new GLSL2 compiler.
*/
static const char fs_code[] =
    "uniform sampler2D texUnit;\n"

    "varying vec2 samplePos0;\n"
    "varying vec2 samplePos1;\n"
    "varying vec2 samplePos2;\n"
    "varying vec2 samplePos3;\n"
    "varying vec2 samplePos4;\n"
    "varying vec2 samplePos5;\n"
    "varying vec2 samplePos6;\n"

    "const vec4 kernel0 = vec4(0.0242836);\n"
    "const vec4 kernel1 = vec4(0.11585);\n"
    "const vec4 kernel2 = vec4(0.275987);\n"
    "const vec4 kernel3 = vec4(0.167758);\n"

    "void main(void)\n"
    "{\n"
    "    vec4 sum = texture2D(texUnit, samplePos0) * kernel0;\n"
    "    sum = sum + texture2D(texUnit, samplePos1) * kernel1;\n"
    "    sum = sum + texture2D(texUnit, samplePos2) * kernel2;\n"
    "    sum = sum + texture2D(texUnit, samplePos3) * kernel3;\n"
    "    sum = sum + texture2D(texUnit, samplePos4) * kernel2;\n"
    "    sum = sum + texture2D(texUnit, samplePos5) * kernel1;\n"
    "    sum = sum + texture2D(texUnit, samplePos6) * kernel0;\n"
    "    gl_FragColor = sum;\n"
    "}\n";

static const int expected_edge[] = {
    0x00, 0x03, 0x06, 0x15, 0x24, 0x47, 0x6a, 0x95, 0xb8, 0xdb, 0xea, 0xf9,
    0xfc, 0xff
};

static const int expected_corner[] = {
    0x00, 0x02, 0x05, 0x14, 0x2c, 0x57, 0x85, 0xbc, 0xd7, 0xf3, 0xf9, 0xff
};

static GLuint setup_shaders()
{
    GLuint vs, fs, prog;

    vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vs_code);
    fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fs_code);
    prog = piglit_link_simple_program(vs, fs);

    glDeleteShader(vs);
    glDeleteShader(fs);

    return prog;
}

static GLboolean test()
{
    GLboolean pass = GL_TRUE;

    /* Prepare the shaders */
    GLint prog       = setup_shaders();
    GLint uPixelSize = glGetUniformLocation(prog, "pixelSize");
    GLint uTexUnit   = glGetUniformLocation(prog, "texUnit");
    GLuint scratchTex;
    int i;

    /* Pixel sizes in texture coordinates for the horizontal and vertical passes */
    const float horizontal[2] = { 1.0 / piglit_width, 0 };
    const float vertical[2]   = { 0, 1.0 / piglit_height };

    /* Texture and vertex coordinates */
    const float tc[] = { 0,1, 1,1, 0,0, 0,0, 1,1, 1,0 };
    const float vc[] = { -1,1, 1,1, -1,-1, -1,-1, 1,1, 1,-1 };

    /* Draw the rectangle that we're going to blur */
    piglit_draw_rect(-.5, -.5, 1, 1);

    /* Create a scratch texture */
    glGenTextures(1, &scratchTex);
    glBindTexture(GL_TEXTURE_2D, scratchTex);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, piglit_width, piglit_height, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);

    glUseProgram(prog);
    glUniform1i(uTexUnit, 0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glTexCoordPointer(2, GL_FLOAT, 0, tc);
    glVertexPointer(2, GL_FLOAT, 0, vc);

    /* Horizontal pass */
    glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, piglit_width, piglit_height);
    glUniform2fv(uPixelSize, 1, horizontal);
    glDrawArrays(GL_TRIANGLES, 0, 6);

    /* Vertical pass */
    glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, piglit_width, piglit_height);
    glUniform2fv(uPixelSize, 1, vertical);
    glDrawArrays(GL_TRIANGLES, 0, 6);

    /* Clean up */
    glUseProgram(0);
    glBindTexture(GL_TEXTURE_2D, 0);
    glDeleteTextures(1, &scratchTex);
    glDeleteProgram(prog);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    assert(glGetError() == 0);

    /* Test the sides */
    for (i = 0; i < 14; i++) {
        float color[3];
        color[0] = expected_edge[i] / 255.;
        color[1] = color[0];
        color[2] = color[0];
        pass = piglit_probe_pixel_rgb(50, 18 + i, color) && pass;
        pass = piglit_probe_pixel_rgb(50, piglit_height - 19 - i, color) && pass;
        pass = piglit_probe_pixel_rgb(18 + i, 50, color) && pass;
        pass = piglit_probe_pixel_rgb(piglit_width - 19 - i, 50, color) && pass;
    }

    /* Test the corners */
    for (i = 0; i < 12; i++) {
        float color[3];
        color[0] = expected_corner[i] / 255.;
        color[1] = color[0];
        color[2] = color[0];
        pass = piglit_probe_pixel_rgb(20 + i, 20 + i, color) && pass;
        pass = piglit_probe_pixel_rgb(20 + i, piglit_height - 21 - i, color) && pass;
        pass = piglit_probe_pixel_rgb(piglit_width - 21 - i, 20 + i, color) && pass;
        pass = piglit_probe_pixel_rgb(piglit_width - 21 - i, piglit_height - 21 - i, color) && pass;
    }

    return pass;
}

enum piglit_result piglit_display(void)
{
    GLboolean pass;

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    pass = test();

    glutSwapBuffers();

    return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}

void piglit_init(int argc, char **argv)
{
	if (piglit_get_gl_version() < 20) {
        printf("Requires OpenGL 2.0\n");
        piglit_report_result(PIGLIT_SKIP);
    }
}