aboutsummaryrefslogtreecommitdiff
path: root/tests/shaders/glsl-routing.c
blob: c6ed396f39c04a3c003699272acfd56d36709283 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * Copyright © 2009 Marek Olšák (maraeo@gmail.com)
 *
 * 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:
 *    Marek Olšák <mareao@gmail.com>
 *
 */

/** @file glsl-routing.c
 *
 * Tests whether streams are routed in this chain correctly:
 * vertex attributes -> vertex shader -> fragment shader -> output
 * with emphasis on linking vertex and fragment shaders
 */

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

PIGLIT_GL_TEST_MAIN(
    260 /*window_width*/,
    365 /*window_height*/,
    PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE)

#define COLORS 2
#define TEXCOORDS 6
#define ATTRIBS (COLORS+TEXCOORDS)
#define BOX_SIZE 25

static char vs_code[] =
    "void main()\n"
    "{\n"
    "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
    "AA  gl_FrontColor = gl_Color;\n"
    "BB  gl_FrontSecondaryColor = gl_SecondaryColor;\n"
    "CC  gl_TexCoord[0] = gl_MultiTexCoord0;\n"
    "DD  gl_TexCoord[1] = gl_MultiTexCoord1;\n"
    "EE  gl_TexCoord[2] = gl_MultiTexCoord2;\n"
    "FF  gl_TexCoord[3] = gl_MultiTexCoord3;\n"
    "GG  gl_TexCoord[4] = gl_MultiTexCoord4;\n"
    "HH  gl_TexCoord[5] = gl_MultiTexCoord5;\n"
    "}\n";

static char fs_code[] =
    "uniform float index;\n"
    "float eq(float a, float b)\n"
    "{\n"
    "    return float(abs(a - b) < 0.01);\n"
    "}\n"
    "void main()\n"
    "{\n"
    "    vec4 r = vec4(0.0);\n"
    "    int i = 1;\n"
    "AA  r += eq(index, float(i)) * gl_Color;\n"
    "    ++i;\n"
    "BB  r += eq(index, float(i)) * gl_SecondaryColor;\n"
    "    ++i;\n"
    "CC  r += eq(index, float(i)) * gl_TexCoord[0];\n"
    "    ++i;\n"
    "DD  r += eq(index, float(i)) * gl_TexCoord[1];\n"
    "    ++i;\n"
    "EE  r += eq(index, float(i)) * gl_TexCoord[2];\n"
    "    ++i;\n"
    "FF  r += eq(index, float(i)) * gl_TexCoord[3];\n"
    "    ++i;\n"
    "GG  r += eq(index, float(i)) * gl_TexCoord[4];\n"
    "    ++i;\n"
    "HH  r += eq(index, float(i)) * gl_TexCoord[5];\n"
    "    gl_FragColor = r;\n"
    "}\n";

static GLint setup_shaders(unsigned vsbitmask, unsigned fsbitmask)
{
    GLuint vs, fs, prog;
    unsigned i;
    char fscode[1024], vscode[1024], pattern[3] = {'A', 'A', '\0'};
    char *ptr;

    memcpy(vscode, vs_code, strlen(vs_code)+1);
    memcpy(fscode, fs_code, strlen(fs_code)+1);

    for (i = 0; i < ATTRIBS; i++) {
        ptr = strstr(vscode, pattern);
        assert(ptr);

        if ((1 << i) & vsbitmask) {
            memcpy(ptr, "  ", 2);
        } else {
            memcpy(ptr, "//", 2);
        }

        ptr = strstr(fscode, pattern);
        assert(ptr);

        if ((1 << i) & fsbitmask) {
            memcpy(ptr, "  ", 2);
        } else {
            memcpy(ptr, "//", 2);
        }

        ++pattern[0];
        ++pattern[1];
    }

    vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vscode);
    fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fscode);
    prog = piglit_link_simple_program(vs, fs);

    glUseProgram(prog);
    return prog;
}

static void draw_rect(float posX, float posY, int attribIndex,
                      float attrib[ATTRIBS][16])
{
    float pos[8] = {
        posX,          posY,
        posX,          posY+BOX_SIZE,
        posX+BOX_SIZE, posY+BOX_SIZE,
        posX+BOX_SIZE, posY
    };
    int i;

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, pos);

    if (COLORS > 0) {
        glEnableClientState(GL_COLOR_ARRAY);
        glColorPointer(4, GL_FLOAT, 0, &attrib[0][0]);
    }
    if (COLORS > 1) {
        glEnableClientState(GL_SECONDARY_COLOR_ARRAY);
        glSecondaryColorPointer(3, GL_FLOAT, 4*sizeof(float), &attrib[1][0]);
    }

    for (i = 0; i < TEXCOORDS; i++) {
        glClientActiveTexture(GL_TEXTURE0 + i);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glTexCoordPointer(4, GL_FLOAT, 0, &attrib[COLORS+i][0]);
    }

    glDrawArrays(GL_QUADS, 0, 4);

    glDisableClientState(GL_VERTEX_ARRAY);
    if (COLORS > 0) {
        glDisableClientState(GL_COLOR_ARRAY);
    }
    if (COLORS > 1) {
        glDisableClientState(GL_SECONDARY_COLOR_ARRAY);
    }
    for (i = 0; i < TEXCOORDS; i++) {
        glClientActiveTexture(GL_TEXTURE0 + i);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    }
    glClientActiveTexture(GL_TEXTURE0);
}

static GLboolean test(unsigned vsbitmask, unsigned fsbitmask, int line)
{
    GLint prog, location;
    GLboolean pass = GL_TRUE;
    int i, j;
    float input[4*ATTRIBS] = {
        0.0, 0.1, 0.2, 0.3,
        0.1, 0.2, 0.3, 0.4,
        0.2, 0.3, 0.4, 0.5,
        0.3, 0.4, 0.5, 0.6,
        0.4, 0.5, 0.6, 0.7,
        0.5, 0.6, 0.7, 0.8,
        0.6, 0.7, 0.8, 0.9,
        0.7, 0.8, 0.9, 1.0
    };
    float attrib[ATTRIBS][16];
    float height = line * 30;

    prog = setup_shaders(vsbitmask, fsbitmask);
    location = glGetUniformLocation(prog, "index");

    for (i = 0; i < ATTRIBS; i++) {
        for (j = 0; j < 16; j++) {
            attrib[i][j] = input[i*4+(j%4)];
        }
    }

    for (i = 0; i < ATTRIBS; i++) {
        if (((1 << i) & vsbitmask) && ((1 << i) & fsbitmask)) {
            j = i+1;
            glUniform1f(location, j);
            draw_rect(5 + (i*(BOX_SIZE+5)) % 240, 5 + height, i, attrib);
        }
    }
    assert(glGetError() == 0);

    for (i = 0; i <= ATTRIBS; i++) {
        if (((1 << i) & vsbitmask) && ((1 << i) & fsbitmask)) {
            pass = piglit_probe_pixel_rgb(7 + (i*(BOX_SIZE+5)), 7 + height, &input[i*4]) && pass;
        }
    }

    return pass;
}

#define C0 1
#define C1 2
#define T0 4
#define T1 8
#define T2 16
#define T3 32
#define T4 64
#define T5 128

static void printconf(unsigned b)
{
    printf("%s %s %s %s %s %s %s %s",
           (1  & b) ? "C0" : "--",
           (2  & b) ? "C1" : "--",
           (4  & b) ? "T0" : "--",
           (8  & b) ? "T1" : "--",
           (16 & b) ? "T2" : "--",
           (32 & b) ? "T3" : "--",
           (64 & b) ? "T4" : "--",
           (128 & b) ? "T5" : "--");
}

enum piglit_result
piglit_display(void)
{
    GLboolean pass = GL_TRUE;
    unsigned i;
    static unsigned conf[][2] = {
        // All VS outputs, some FS inputs
        {
            C0 | C1 | T0 | T1 | T2 | T3 | T4 | T5,
            C0 |      T0 |      T2 |      T4 | T5
        },
        {
            C0 | C1 | T0 | T1 | T2 | T3 | T4 | T5,
                 C1 | T0 | T1 |           T4
        },
        {
            C0 | C1 | T0 | T1 | T2 | T3 | T4 | T5,
            C0 | C1 |           T2 |      T4 | T5
        },
        {
            C0 | C1 | T0 | T1 | T2 | T3 | T4 | T5,
                 C1 |           T2 | T3
        },
        // Some VS outputs, all FS inputs
        {
            C0 |      T0 |      T2 |      T4 | T5,
            C0 | C1 | T0 | T1 | T2 | T3 | T4 | T5
        },
        {
                 C1 | T0 | T1 |           T4,
            C0 | C1 | T0 | T1 | T2 | T3 | T4 | T5
        },
        {
            C0 | C1 |                T3 |      T5,
            C0 | C1 | T0 | T1 | T2 | T3 | T4 | T5
        },
        {
                 C1 |           T2 | T3 | T4,
            C0 | C1 | T0 | T1 | T2 | T3 | T4 | T5
        },
        // Some VS outputs, some FS inputs
        {
            C0 |      T0 |      T2 |      T4 | T5,
            C0 | C1 | T0 | T1 |           T4
        },
        {
            C0 | C1 |      T1 |           T4 | T5,
                 C1 |      T1 | T2 | T3 | T4
        },
        {
                 C1 | T0 |      T2 | T3 |      T5,
                 C1 |      T1 | T2 | T3 | T4 | T5
        },
        {
            C0 | C1 | T0 |      T2 | T3 | T4 | T5,
            C0 |      T0 |           T3 |      T5
        },
    };

    glClearColor(0.5, 0.5, 0.5, 0.5);
    glClear(GL_COLOR_BUFFER_BIT);

    for (i = 0; i < sizeof(conf) / sizeof(*conf); i++) {
        printf("\nTest: VS(");
        printconf(conf[i][0]);
        printf(")\n      FS(");
        printconf(conf[i][1]);
        printf(")\n");

        pass = test(conf[i][0], conf[i][1], i) && pass;
    }

    piglit_present_results();

    return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}

void piglit_init(int argc, char **argv)
{
    piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

    piglit_require_gl_version(20);
}