Edit

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

Branch :

  • Show log

    Commit

  • Author : Yunchao He
    Date : 2017-04-21 12:15:49
    Hash : 4f285443
    Message : Refactoring: replace NULL by nullptr for pointers (2nd CL). This CL mainly handles the pointer comparisons (== or !=). BUG=angleproject:2001 Change-Id: I25ac3b61032e7ad91459a1c6541cadc87cf9b160 Reviewed-on: https://chromium-review.googlesource.com/483935 Reviewed-by: Jamie Madill <jmadill@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 = nullptr;
        }
    
        virtual void TearDown()
        {
            DestroyCompiler();
        }
    
        void DestroyCompiler()
        {
            if (mCompiler)
            {
                sh::Destruct(mCompiler);
                mCompiler = nullptr;
            }
        }
    
        void InitializeCompiler()
        {
            DestroyCompiler();
            mCompiler = sh::ConstructCompiler(GL_FRAGMENT_SHADER, SH_WEBGL_SPEC,
                                              SH_GLSL_COMPATIBILITY_OUTPUT, &mResources);
            ASSERT_TRUE(mCompiler != nullptr) << "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);
    }