1 //
2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // FragDepth_test.cpp:
7 // Test for GLES SL 3.0 gl_FragDepth variable implementation.
8 //
9
10 #include "GLSLANG/ShaderLang.h"
11 #include "angle_gl.h"
12 #include "gtest/gtest.h"
13
14 namespace
15 {
16 const char ESSLVersion100[] = "#version 100\n";
17 const char ESSLVersion300[] = "#version 300 es\n";
18 const char EXTFDPragma[] = "#extension GL_EXT_frag_depth : require\n";
19 } // namespace
20
21 class FragDepthTest : public testing::TestWithParam<bool>
22 {
23 protected:
SetUp()24 void SetUp() override
25 {
26 sh::InitBuiltInResources(&mResources);
27 mCompiler = nullptr;
28 mResources.EXT_frag_depth = GetParam();
29 }
30
TearDown()31 void TearDown() override { DestroyCompiler(); }
DestroyCompiler()32 void DestroyCompiler()
33 {
34 if (mCompiler)
35 {
36 sh::Destruct(mCompiler);
37 mCompiler = nullptr;
38 }
39 }
40
InitializeCompiler()41 void InitializeCompiler()
42 {
43 DestroyCompiler();
44 mCompiler = sh::ConstructCompiler(GL_FRAGMENT_SHADER, SH_GLES3_SPEC,
45 SH_GLSL_COMPATIBILITY_OUTPUT, &mResources);
46 ASSERT_TRUE(mCompiler != nullptr) << "Compiler could not be constructed.";
47 }
48
TestShaderCompile(const char * version,const char * pragma,const char * shader)49 testing::AssertionResult TestShaderCompile(const char *version,
50 const char *pragma,
51 const char *shader)
52 {
53 const char *shaderStrings[] = {version, pragma, shader};
54 bool success = sh::Compile(mCompiler, shaderStrings, 3, 0);
55 if (success)
56 {
57 return ::testing::AssertionSuccess() << "Compilation success";
58 }
59 return ::testing::AssertionFailure() << sh::GetInfoLog(mCompiler);
60 }
61
62 protected:
63 ShBuiltInResources mResources;
64 ShHandle mCompiler;
65 };
66
67 // The GLES SL 3.0 built-in variable gl_FragDepth fails to compile with GLES SL 1.0.
TEST_P(FragDepthTest,CompileFailsESSL100)68 TEST_P(FragDepthTest, CompileFailsESSL100)
69 {
70 static const char shaderString[] =
71 "precision mediump float;\n"
72 "void main() { \n"
73 " gl_FragDepth = 1.0;\n"
74 "}\n";
75
76 InitializeCompiler();
77 EXPECT_FALSE(TestShaderCompile(ESSLVersion100, "", shaderString));
78 EXPECT_FALSE(TestShaderCompile("", "", shaderString));
79 EXPECT_FALSE(TestShaderCompile("", EXTFDPragma, shaderString));
80 }
81
82 // The GLES SL 3.0 built-in variable gl_FragDepth compiles with GLES SL 3.0.
TEST_P(FragDepthTest,CompileSucceedsESSL300)83 TEST_P(FragDepthTest, CompileSucceedsESSL300)
84 {
85 static const char shaderString[] =
86 "precision mediump float;\n"
87 "void main() { \n"
88 " gl_FragDepth = 1.0;\n"
89 "}\n";
90 InitializeCompiler();
91 EXPECT_TRUE(TestShaderCompile(ESSLVersion300, "", shaderString));
92 }
93
94 // Using #extension GL_EXT_frag_depth in GLSL ES 3.0 shader fails to compile.
TEST_P(FragDepthTest,ExtensionFDFailsESSL300)95 TEST_P(FragDepthTest, ExtensionFDFailsESSL300)
96 {
97 static const char shaderString[] =
98 "precision mediump float;\n"
99 "out vec4 fragColor;\n"
100 "void main() { \n"
101 " fragColor = vec4(1.0);\n"
102 "}\n";
103 InitializeCompiler();
104 if (mResources.EXT_frag_depth == 1)
105 {
106 // TODO(kkinnunen, geofflang): this should fail. Extensions need to have similar level
107 // system to SymbolTable. The biggest task is to implement version-aware preprocessor, so
108 // that the extension defines can be defined depending on the version that the preprocessor
109 // saw or did not see.
110 EXPECT_TRUE(TestShaderCompile(ESSLVersion300, EXTFDPragma, shaderString));
111 }
112 else
113 {
114 EXPECT_FALSE(TestShaderCompile(ESSLVersion300, EXTFDPragma, shaderString));
115 }
116 }
117
118 // The tests should pass regardless whether the EXT_frag_depth is on or not.
119 INSTANTIATE_TEST_SUITE_P(FragDepthTests, FragDepthTest, testing::Values(false, true));
120