Edit

kc3-lang/angle/src/tests/compiler_tests/ShaderExtension_test.cpp

Branch :

  • Show log

    Commit

  • Author : Jamie Madill
    Date : 2016-11-07 13:50:29
    Hash : acb4b81a
    Message : translator: Put ShaderLang APIs in "sh" namespace. Working with glslang in Vulkan means we are static linking libANGLE with functions that have the same name as our translator APIs. We can fix this by scoping our APIs. We don't need to scope the types of the file, since they don't conflict. This will require a follow-up patch to remove the unscoped APIs once we switch over Chromium. We also scope TCompiler and some related classes to avoid multiply defined link errors with glslang. BUG=angleproject:1576 Change-Id: I729b19467d2ff7d374a82044b16dbebdf2dc8f16 Reviewed-on: https://chromium-review.googlesource.com/408337 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>

  • src/tests/compiler_tests/ShaderExtension_test.cpp
  • //
    // 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()
        {
            sh::InitBuiltInResources(&mResources);
            mCompiler = NULL;
        }
    
        virtual void TearDown()
        {
            DestroyCompiler();
        }
    
        void DestroyCompiler()
        {
            if (mCompiler)
            {
                sh::Destruct(mCompiler);
                mCompiler = NULL;
            }
        }
    
        void InitializeCompiler()
        {
            DestroyCompiler();
            mCompiler = sh::ConstructCompiler(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                  = sh::Compile(mCompiler, shaderStrings, stringCount, 0);
            const std::string &compileLog = sh::GetInfoLog(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);
    }