summaryrefslogtreecommitdiffstats
path: root/gfx/angle/src/tests/compiler_tests/ShaderVariable_test.cpp
blob: 6b90f1f5d3e7365f6233d77f3237a3ccb6c36cbb (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//
// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// CollectVariables_test.cpp:
//   Some tests for shader inspection
//

#include "angle_gl.h"
#include "compiler/translator/Compiler.h"
#include "gtest/gtest.h"
#include "GLSLANG/ShaderLang.h"

namespace sh
{

TEST(ShaderVariableTest, FindInfoByMappedName)
{
    // struct A {
    //   float x[2];
    //   vec3 y;
    // };
    // struct B {
    //   A a[3];
    // };
    // B uni[2];
    ShaderVariable uni;
    uni.arraySize = 2;
    uni.name = "uni";
    uni.mappedName = "m_uni";
    uni.structName = "B";
    {
        ShaderVariable a;
        a.arraySize = 3;
        a.name = "a";
        a.mappedName = "m_a";
        a.structName = "A";
        {
            ShaderVariable x(GL_FLOAT, 2);
            x.name = "x";
            x.mappedName = "m_x";
            a.fields.push_back(x);

            ShaderVariable y(GL_FLOAT_VEC3, 0);
            y.name = "y";
            y.mappedName = "m_y";
            a.fields.push_back(y);
        }
        uni.fields.push_back(a);
    }

    const ShaderVariable *leafVar = nullptr;
    std::string originalFullName;

    std::string mappedFullName = "wrongName";
    EXPECT_FALSE(uni.findInfoByMappedName(
        mappedFullName, &leafVar, &originalFullName));

    mappedFullName = "m_uni";
    EXPECT_TRUE(uni.findInfoByMappedName(
        mappedFullName, &leafVar, &originalFullName));
    EXPECT_EQ(&uni, leafVar);
    EXPECT_STREQ("uni", originalFullName.c_str());

    mappedFullName = "m_uni[0].m_a[1].wrongName";
    EXPECT_FALSE(uni.findInfoByMappedName(
        mappedFullName, &leafVar, &originalFullName));

    mappedFullName = "m_uni[0].m_a[1].m_x";
    EXPECT_TRUE(uni.findInfoByMappedName(
        mappedFullName, &leafVar, &originalFullName));
    EXPECT_EQ(&(uni.fields[0].fields[0]), leafVar);
    EXPECT_STREQ("uni[0].a[1].x", originalFullName.c_str());

    mappedFullName = "m_uni[0].m_a[1].m_x[0]";
    EXPECT_TRUE(uni.findInfoByMappedName(
        mappedFullName, &leafVar, &originalFullName));
    EXPECT_EQ(&(uni.fields[0].fields[0]), leafVar);
    EXPECT_STREQ("uni[0].a[1].x[0]", originalFullName.c_str());

    mappedFullName = "m_uni[0].m_a[1].m_y";
    EXPECT_TRUE(uni.findInfoByMappedName(
        mappedFullName, &leafVar, &originalFullName));
    EXPECT_EQ(&(uni.fields[0].fields[1]), leafVar);
    EXPECT_STREQ("uni[0].a[1].y", originalFullName.c_str());
}

TEST(ShaderVariableTest, IsSameUniformWithDifferentFieldOrder)
{
    // struct A {
    //   float x;
    //   float y;
    // };
    // uniform A uni;
    Uniform vx_a;
    vx_a.arraySize = 0;
    vx_a.name = "uni";
    vx_a.mappedName = "m_uni";
    vx_a.structName = "A";
    {
        ShaderVariable x(GL_FLOAT, 0);
        x.name = "x";
        x.mappedName = "m_x";
        vx_a.fields.push_back(x);

        ShaderVariable y(GL_FLOAT, 0);
        y.name = "y";
        y.mappedName = "m_y";
        vx_a.fields.push_back(y);
    }

    // struct A {
    //   float y;
    //   float x;
    // };
    // uniform A uni;
    Uniform fx_a;
    fx_a.arraySize = 0;
    fx_a.name = "uni";
    fx_a.mappedName = "m_uni";
    fx_a.structName = "A";
    {
        ShaderVariable y(GL_FLOAT, 0);
        y.name = "y";
        y.mappedName = "m_y";
        fx_a.fields.push_back(y);

        ShaderVariable x(GL_FLOAT, 0);
        x.name = "x";
        x.mappedName = "m_x";
        fx_a.fields.push_back(x);
    }

    EXPECT_FALSE(vx_a.isSameUniformAtLinkTime(fx_a));
}

TEST(ShaderVariableTest, IsSameUniformWithDifferentStructNames)
{
    // struct A {
    //   float x;
    //   float y;
    // };
    // uniform A uni;
    Uniform vx_a;
    vx_a.arraySize = 0;
    vx_a.name = "uni";
    vx_a.mappedName = "m_uni";
    vx_a.structName = "A";
    {
        ShaderVariable x(GL_FLOAT, 0);
        x.name = "x";
        x.mappedName = "m_x";
        vx_a.fields.push_back(x);

        ShaderVariable y(GL_FLOAT, 0);
        y.name = "y";
        y.mappedName = "m_y";
        vx_a.fields.push_back(y);
    }

    // struct B {
    //   float x;
    //   float y;
    // };
    // uniform B uni;
    Uniform fx_a;
    fx_a.arraySize = 0;
    fx_a.name = "uni";
    fx_a.mappedName = "m_uni";
    {
        ShaderVariable x(GL_FLOAT, 0);
        x.name = "x";
        x.mappedName = "m_x";
        fx_a.fields.push_back(x);

        ShaderVariable y(GL_FLOAT, 0);
        y.name = "y";
        y.mappedName = "m_y";
        fx_a.fields.push_back(y);
    }

    fx_a.structName = "B";
    EXPECT_FALSE(vx_a.isSameUniformAtLinkTime(fx_a));

    fx_a.structName = "A";
    EXPECT_TRUE(vx_a.isSameUniformAtLinkTime(fx_a));

    fx_a.structName = "";
    EXPECT_FALSE(vx_a.isSameUniformAtLinkTime(fx_a));
}

TEST(ShaderVariableTest, IsSameVaryingWithDifferentInvariance)
{
    // invariant varying float vary;
    Varying vx;
    vx.type = GL_FLOAT;
    vx.arraySize = 0;
    vx.precision = GL_MEDIUM_FLOAT;
    vx.name = "vary";
    vx.mappedName = "m_vary";
    vx.staticUse = true;
    vx.isInvariant = true;

    // varying float vary;
    Varying fx;
    fx.type = GL_FLOAT;
    fx.arraySize = 0;
    fx.precision = GL_MEDIUM_FLOAT;
    fx.name = "vary";
    fx.mappedName = "m_vary";
    fx.staticUse = true;
    fx.isInvariant = false;

    // Default to ESSL1 behavior: invariance must match
    EXPECT_FALSE(vx.isSameVaryingAtLinkTime(fx));
    EXPECT_FALSE(vx.isSameVaryingAtLinkTime(fx, 100));
    // ESSL3 behavior: invariance doesn't need to match
    EXPECT_TRUE(vx.isSameVaryingAtLinkTime(fx, 300));

    // invariant varying float vary;
    fx.isInvariant = true;
    EXPECT_TRUE(vx.isSameVaryingAtLinkTime(fx));
    EXPECT_TRUE(vx.isSameVaryingAtLinkTime(fx, 100));
    EXPECT_TRUE(vx.isSameVaryingAtLinkTime(fx, 300));
}

// Test that using invariant varyings doesn't trigger a double delete.
TEST(ShaderVariableTest, InvariantDoubleDeleteBug)
{
    ShBuiltInResources resources;
    ShInitBuiltInResources(&resources);

    ShHandle compiler = ShConstructCompiler(GL_VERTEX_SHADER, SH_GLES2_SPEC,
                                            SH_GLSL_COMPATIBILITY_OUTPUT, &resources);
    EXPECT_NE(static_cast<ShHandle>(0), compiler);

    const char *program[] =
    {
        "attribute vec4 position;\n"
        "varying float v;\n"
        "invariant v;\n"
        "void main() {\n"
        "  v = 1.0;\n"
        "  gl_Position = position;\n"
        "}"
    };

    EXPECT_TRUE(ShCompile(compiler, program, 1, SH_OBJECT_CODE));
    EXPECT_TRUE(ShCompile(compiler, program, 1, SH_OBJECT_CODE));
    ShDestruct(compiler);
}

TEST(ShaderVariableTest, IllegalInvariantVarying)
{
    ShBuiltInResources resources;
    ShInitBuiltInResources(&resources);

    ShHandle compiler = ShConstructCompiler(GL_VERTEX_SHADER, SH_GLES2_SPEC,
                                            SH_GLSL_COMPATIBILITY_OUTPUT, &resources);
    EXPECT_NE(static_cast<ShHandle>(0), compiler);

    const char *program1[] =
    {
        "void foo() {\n"
        "  vec4 v;\n"
        "}\n"
        "varying vec4 v_varying;\n"
        "invariant v_varying;\n"
        "void main() {\n"
        "  foo();\n"
        "  gl_Position = v_varying;\n"
        "}"
    };
    const char *program2[] =
    {
        "varying vec4 v_varying;\n"
        "void foo() {\n"
        "  invariant v_varying;\n"
        "}\n"
        "void main() {\n"
        "  foo();\n"
        "  gl_Position = v_varying;\n"
        "}"
    };

    EXPECT_TRUE(ShCompile(compiler, program1, 1, SH_VARIABLES));
    EXPECT_FALSE(ShCompile(compiler, program2, 1, SH_VARIABLES));
}

TEST(ShaderVariableTest, InvariantLeakAcrossShaders)
{
    ShBuiltInResources resources;
    ShInitBuiltInResources(&resources);

    ShHandle compiler = ShConstructCompiler(GL_VERTEX_SHADER, SH_GLES2_SPEC,
                                            SH_GLSL_COMPATIBILITY_OUTPUT, &resources);
    EXPECT_NE(static_cast<ShHandle>(0), compiler);

    const char *program1[] =
    {
        "varying vec4 v_varying;\n"
        "invariant v_varying;\n"
        "void main() {\n"
        "  gl_Position = v_varying;\n"
        "}"
    };
    const char *program2[] =
    {
        "varying vec4 v_varying;\n"
        "void main() {\n"
        "  gl_Position = v_varying;\n"
        "}"
    };

    EXPECT_TRUE(ShCompile(compiler, program1, 1, SH_VARIABLES));
    const std::vector<sh::Varying> *varyings = ShGetVaryings(compiler);
    for (const sh::Varying &varying : *varyings)
    {
        if (varying.name == "v_varying")
            EXPECT_TRUE(varying.isInvariant);
    }
    EXPECT_TRUE(ShCompile(compiler, program2, 1, SH_VARIABLES));
    varyings = ShGetVaryings(compiler);
    for (const sh::Varying &varying : *varyings)
    {
        if (varying.name == "v_varying")
            EXPECT_FALSE(varying.isInvariant);
    }
}

TEST(ShaderVariableTest, GlobalInvariantLeakAcrossShaders)
{
    ShBuiltInResources resources;
    ShInitBuiltInResources(&resources);

    ShHandle compiler = ShConstructCompiler(GL_VERTEX_SHADER, SH_GLES2_SPEC,
                                            SH_GLSL_COMPATIBILITY_OUTPUT, &resources);
    EXPECT_NE(static_cast<ShHandle>(0), compiler);

    const char *program1[] =
    {
        "#pragma STDGL invariant(all)\n"
        "varying vec4 v_varying;\n"
        "void main() {\n"
        "  gl_Position = v_varying;\n"
        "}"
    };
    const char *program2[] =
    {
        "varying vec4 v_varying;\n"
        "void main() {\n"
        "  gl_Position = v_varying;\n"
        "}"
    };

    EXPECT_TRUE(ShCompile(compiler, program1, 1, SH_VARIABLES));
    const std::vector<sh::Varying> *varyings = ShGetVaryings(compiler);
    for (const sh::Varying &varying : *varyings)
    {
        if (varying.name == "v_varying")
            EXPECT_TRUE(varying.isInvariant);
    }
    EXPECT_TRUE(ShCompile(compiler, program2, 1, SH_VARIABLES));
    varyings = ShGetVaryings(compiler);
    for (const sh::Varying &varying : *varyings)
    {
        if (varying.name == "v_varying")
            EXPECT_FALSE(varying.isInvariant);
    }
}

TEST(ShaderVariableTest, BuiltinInvariantVarying)
{

    ShBuiltInResources resources;
    ShInitBuiltInResources(&resources);

    ShHandle compiler = ShConstructCompiler(GL_VERTEX_SHADER, SH_GLES2_SPEC,
                                            SH_GLSL_COMPATIBILITY_OUTPUT, &resources);
    EXPECT_NE(static_cast<ShHandle>(0), compiler);

    const char *program1[] =
    {
        "invariant gl_Position;\n"
        "void main() {\n"
        "  gl_Position = vec4(0, 0, 0, 0);\n"
        "}"
    };
    const char *program2[] =
    {
        "void main() {\n"
        "  gl_Position = vec4(0, 0, 0, 0);\n"
        "}"
    };
    const char *program3[] =
    {
        "void main() {\n"
        "  invariant gl_Position;\n"
        "  gl_Position = vec4(0, 0, 0, 0);\n"
        "}"
    };

    EXPECT_TRUE(ShCompile(compiler, program1, 1, SH_VARIABLES));
    const std::vector<sh::Varying> *varyings = ShGetVaryings(compiler);
    for (const sh::Varying &varying : *varyings)
    {
        if (varying.name == "gl_Position")
            EXPECT_TRUE(varying.isInvariant);
    }
    EXPECT_TRUE(ShCompile(compiler, program2, 1, SH_VARIABLES));
    varyings = ShGetVaryings(compiler);
    for (const sh::Varying &varying : *varyings)
    {
        if (varying.name == "gl_Position")
            EXPECT_FALSE(varying.isInvariant);
    }
    EXPECT_FALSE(ShCompile(compiler, program3, 1, SH_VARIABLES));
}

}  // namespace sh