summaryrefslogtreecommitdiffstats
path: root/gfx/angle/src/tests/compiler_tests/ShaderExtension_test.cpp
blob: a23de3dee4705b0127bb80ffe227747dbcbc418c (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
//
// Copyright (c) 2015 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.
//
// Extension_test.cpp
//   Test that shaders need various extensions to be compiled.
//

#include "angle_gl.h"
#include "gtest/gtest.h"
#include "GLSLANG/ShaderLang.h"

static const char *FragDepthExtShdr[] =
{
    // First string of the shader. The required pragma declaration.
    "#extension GL_EXT_frag_depth : enable\n",

    // Second string of the shader. The rest of the shader body.
    "void main() { gl_FragDepthEXT = 1.0; }\n"
};

static const char *StandDerivExtShdr[] =
{
    // First string of the shader. The required pragma declaration.
    "#extension GL_OES_standard_derivatives : enable\n",

    // Second string of the shader. The rest of the shader body.
    "precision mediump float;\n"
    "varying vec2 texCoord;\n"
    "void main() { gl_FragColor = vec4(dFdx(texCoord.x), dFdy(texCoord.y), fwidth(texCoord.x), 1.0); }\n"
};

static const char *TextureLODShdr[] =
{
    // First string of the shader. The required pragma declaration.
    "#extension GL_EXT_shader_texture_lod : enable\n",

    // Second string of the shader. The rest of the shader body.
    "precision mediump float;\n"
    "varying vec2 texCoord0v;\n"
    "uniform float lod;\n"
    "uniform sampler2D tex;\n"
    "void main() { vec4 color = texture2DLodEXT(tex, texCoord0v, lod); }\n"
};

class ShaderExtensionTest : public testing::Test
{
  public:
    ShaderExtensionTest() {}

  protected:
    virtual void SetUp()
    {
        ShInitBuiltInResources(&mResources);
        mCompiler = NULL;
    }

    virtual void TearDown()
    {
        DestroyCompiler();
    }

    void DestroyCompiler()
    {
        if (mCompiler)
        {
            ShDestruct(mCompiler);
            mCompiler = NULL;
        }
    }

    void InitializeCompiler()
    {
        DestroyCompiler();
        mCompiler = ShConstructCompiler(GL_FRAGMENT_SHADER, SH_WEBGL_SPEC,
                                        SH_GLSL_COMPATIBILITY_OUTPUT, &mResources);
        ASSERT_TRUE(mCompiler != NULL) << "Compiler could not be constructed.";
    }

    void TestShaderExtension(const char **shaderStrings, int stringCount, bool expectation)
    {
        bool success = ShCompile(mCompiler, shaderStrings, stringCount, 0);
        const std::string& compileLog = ShGetInfoLog(mCompiler);
        EXPECT_EQ(expectation, success) << compileLog;
    }

  protected:
    ShBuiltInResources mResources;
    ShHandle mCompiler;
};

TEST_F(ShaderExtensionTest, FragDepthExtRequiresExt)
{
    // Extension is required to compile properly.
    mResources.EXT_frag_depth = 0;
    InitializeCompiler();
    TestShaderExtension(FragDepthExtShdr, 2, false);
}

TEST_F(ShaderExtensionTest, FragDepthExtRequiresPragma)
{
    // Pragma is required to compile properly.
    mResources.EXT_frag_depth = 1;
    InitializeCompiler();
    TestShaderExtension(&FragDepthExtShdr[1], 1, false);
}

TEST_F(ShaderExtensionTest, FragDepthExtCompiles)
{
    // Test that it compiles properly with extension enabled.
    mResources.EXT_frag_depth = 1;
    InitializeCompiler();
    TestShaderExtension(FragDepthExtShdr, 2, true);
}

TEST_F(ShaderExtensionTest, FragDepthExtResetsInternalStates)
{
    // Test that extension internal states are reset properly between compiles.
    mResources.EXT_frag_depth = 1;
    InitializeCompiler();

    TestShaderExtension(FragDepthExtShdr, 2, true);
    TestShaderExtension(&FragDepthExtShdr[1], 1, false);
    TestShaderExtension(FragDepthExtShdr, 2, true);
}

TEST_F(ShaderExtensionTest, StandDerivExtRequiresExt)
{
    // Extension is required to compile properly.
    mResources.OES_standard_derivatives = 0;
    InitializeCompiler();
    TestShaderExtension(StandDerivExtShdr, 2, false);
}

TEST_F(ShaderExtensionTest, StandDerivExtRequiresPragma)
{
    // Pragma is required to compile properly.
    mResources.OES_standard_derivatives = 1;
    InitializeCompiler();
    TestShaderExtension(&StandDerivExtShdr[1], 1, false);
}

TEST_F(ShaderExtensionTest, StandDerivExtCompiles)
{
    // Test that it compiles properly with extension enabled.
    mResources.OES_standard_derivatives = 1;
    InitializeCompiler();
    TestShaderExtension(StandDerivExtShdr, 2, true);
}

TEST_F(ShaderExtensionTest, StandDerivExtResetsInternalStates)
{
    // Test that extension internal states are reset properly between compiles.
    mResources.OES_standard_derivatives = 1;
    InitializeCompiler();

    TestShaderExtension(StandDerivExtShdr, 2, true);
    TestShaderExtension(&StandDerivExtShdr[1], 1, false);
    TestShaderExtension(StandDerivExtShdr, 2, true);
    TestShaderExtension(&StandDerivExtShdr[1], 1, false);
}

TEST_F(ShaderExtensionTest, TextureLODExtRequiresExt)
{
    // Extension is required to compile properly.
    mResources.EXT_shader_texture_lod = 0;
    InitializeCompiler();
    TestShaderExtension(TextureLODShdr, 2, false);
}

TEST_F(ShaderExtensionTest, TextureLODExtRequiresPragma)
{
    // Pragma is required to compile properly.
    mResources.EXT_shader_texture_lod = 1;
    InitializeCompiler();
    TestShaderExtension(&TextureLODShdr[1], 1, false);
}

TEST_F(ShaderExtensionTest, TextureLODExtCompiles)
{
    // Test that it compiles properly with extension enabled.
    mResources.EXT_shader_texture_lod = 1;
    InitializeCompiler();
    TestShaderExtension(TextureLODShdr, 2, true);
}

TEST_F(ShaderExtensionTest, TextureLODExtResetsInternalStates)
{
    // Test that extension internal states are reset properly between compiles.
    mResources.EXT_shader_texture_lod = 1;
    InitializeCompiler();

    TestShaderExtension(&TextureLODShdr[1], 1, false);
    TestShaderExtension(TextureLODShdr, 2, true);
    TestShaderExtension(&TextureLODShdr[1], 1, false);
    TestShaderExtension(TextureLODShdr, 2, true);
}

// Test a bug where we could modify the value of a builtin variable.
TEST_F(ShaderExtensionTest, BuiltinRewritingBug)
{
    mResources.MaxDrawBuffers = 4;
    mResources.EXT_draw_buffers = 1;
    InitializeCompiler();

    const std::string &shaderString =
        "#extension GL_EXT_draw_buffers : require\n"
        "precision mediump float;\n"
        "void main() {\n"
        "    gl_FragData[gl_MaxDrawBuffers] = vec4(0.0);\n"
        "}";

    const char *shaderStrings[] = { shaderString.c_str() };

    TestShaderExtension(shaderStrings, 1, false);
    TestShaderExtension(shaderStrings, 1, false);
}