//
// Copyright (c) 2016 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.
//
// SamplerMultisample_test.cpp:
// Tests compiling shaders that use gsampler2DMS types
//

#include "GLSLANG/ShaderLang.h"
#include "angle_gl.h"
#include "gtest/gtest.h"
#include "tests/test_utils/ShaderCompileTreeTest.h"

using namespace sh;

class SamplerMultisampleTest : public ShaderCompileTreeTest
{
  public:
    SamplerMultisampleTest() {}

  protected:
    ::GLenum getShaderType() const override { return GL_FRAGMENT_SHADER; }
    ShShaderSpec getShaderSpec() const override { return SH_GLES3_1_SPEC; }
};

class SamplerMultisampleArrayTest : public ShaderCompileTreeTest
{
  public:
    SamplerMultisampleArrayTest() {}

    void initResources(ShBuiltInResources *resources) override
    {
        resources->ANGLE_texture_multisample_array = 1;
    }

  protected:
    ::GLenum getShaderType() const override { return GL_FRAGMENT_SHADER; }
    ShShaderSpec getShaderSpec() const override { return SH_GLES3_1_SPEC; }
};

// Checks whether compiler has parsed the gsampler2DMS, texelfetch correctly.
TEST_F(SamplerMultisampleTest, TexelFetchSampler2DMS)
{
    const std::string &shaderString =
        R"(#version 310 es
        precision highp float;
        uniform highp sampler2DMS s;
        uniform highp isampler2DMS is;
        uniform highp usampler2DMS us;

        void main() {
            vec4 tex1 = texelFetch(s, ivec2(0, 0), 0);
            ivec4 tex2 = texelFetch(is, ivec2(0, 0), 0);
            uvec4 tex3 = texelFetch(us, ivec2(0, 0), 0);
        })";

    if (!compile(shaderString))
    {
        FAIL() << "Shader compilation failed, expecting success:\n" << mInfoLog;
    }
}

// Checks whether compiler has parsed the gsampler2DMS, textureSize correctly.
TEST_F(SamplerMultisampleTest, TextureSizeSampler2DMS)
{
    const std::string &shaderString =
        R"(#version 310 es
        precision highp float;
        uniform highp sampler2DMS s;
        uniform highp isampler2DMS is;
        uniform highp usampler2DMS us;

        void main() {
            ivec2 size = textureSize(s);
            size = textureSize(is);
            size = textureSize(us);
        })";

    if (!compile(shaderString))
    {
        FAIL() << "Shader compilation failed, expecting success:\n" << mInfoLog;
    }
}

// Check that sampler2DMS has no default precision.
TEST_F(SamplerMultisampleTest, NoPrecisionSampler2DMS)
{
    const std::string &shaderString =
        R"(#version 310 es
        precision highp float;
        uniform sampler2DMS s;

        void main() {})";

    if (compile(shaderString))
    {
        FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
    }
}

// Check that isampler2DMS has no default precision.
TEST_F(SamplerMultisampleTest, NoPrecisionISampler2DMS)
{
    const std::string &shaderString =
        R"(#version 310 es
        precision highp float;
        uniform isampler2DMS s;

        void main() {})";

    if (compile(shaderString))
    {
        FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
    }
}

// Check that usampler2DMS has no default precision.
TEST_F(SamplerMultisampleTest, NoPrecisionUSampler2DMS)
{
    const std::string &shaderString =
        R"(#version 310 es
        precision highp float;
        uniform usampler2DMS s;

        void main() {})";

    if (compile(shaderString))
    {
        FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
    }
}

// Negative test: checks that sampler2DMS is not usable in ESSL 3.00.
TEST_F(SamplerMultisampleTest, Sampler2DMSESSL300)
{
    const std::string &shaderString =
        R"(#version 300 es
        precision highp float;
        uniform highp sampler2DMS s;

        void main() {
        })";

    if (compile(shaderString))
    {
        FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
    }
}

// Negative test: checks that isampler2DMS is not usable in ESSL 3.00.
TEST_F(SamplerMultisampleTest, ISampler2DMSESSL300)
{
    const std::string &shaderString =
        R"(#version 300 es
        precision highp float;
        uniform highp isampler2DMS s;

        void main() {
        })";

    if (compile(shaderString))
    {
        FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
    }
}

// Negative test: checks that usampler2DMS is not usable in ESSL 3.00.
TEST_F(SamplerMultisampleTest, USampler2DMSESSL300)
{
    const std::string &shaderString =
        R"(#version 300 es
        precision highp float;
        uniform highp usampler2DMS s;

        void main() {
        })";

    if (compile(shaderString))
    {
        FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
    }
}

// Negative test: checks that sampler2DMSArray is not usable in ESSL 3.10 without extensions.
TEST_F(SamplerMultisampleTest, Sampler2DMSArrayNotSupported)
{
    const std::string &shaderString =
        R"(#version 310 es
        precision highp float;
        uniform highp sampler2DMSArray s;

        void main() {
        })";

    if (compile(shaderString))
    {
        FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
    }
}

// Negative test: checks that isampler2DMSArray is not usable in ESSL 3.10 without extensions.
TEST_F(SamplerMultisampleTest, ISampler2DMSArrayNotSupported)
{
    const std::string &shaderString =
        R"(#version 310 es
        precision highp float;
        uniform highp isampler2DMSArray s;

        void main() {
        })";

    if (compile(shaderString))
    {
        FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
    }
}

// Negative test: checks that usampler2DMSArray is not usable in ESSL 3.10 without extensions.
TEST_F(SamplerMultisampleTest, USampler2DMSArrayNotSupported)
{
    const std::string &shaderString =
        R"(#version 310 es
        precision highp float;
        uniform highp usampler2DMSArray s;

        void main() {
        })";

    if (compile(shaderString))
    {
        FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
    }
}

// Checks whether compiler has parsed the gsampler2DMSArray, texelfetch correctly.
TEST_F(SamplerMultisampleArrayTest, TexelFetchSampler2DMSArray)
{
    const std::string &shaderString =
        R"(#version 310 es
        #extension GL_ANGLE_texture_multisample_array : require
        precision highp float;
        uniform highp sampler2DMSArray s;
        uniform highp isampler2DMSArray is;
        uniform highp usampler2DMSArray us;

        void main() {
            vec4 tex1 = texelFetch(s, ivec3(0, 0, 0), 0);
            ivec4 tex2 = texelFetch(is, ivec3(0, 0, 0), 0);
            uvec4 tex3 = texelFetch(us, ivec3(0, 0, 0), 0);
        })";

    if (!compile(shaderString))
    {
        FAIL() << "Shader compilation failed, expecting success:\n" << mInfoLog;
    }
}

// Checks whether compiler has parsed the gsampler2DMSArray, textureSize correctly.
TEST_F(SamplerMultisampleArrayTest, TextureSizeSampler2DMSArray)
{
    const std::string &shaderString =
        R"(#version 310 es
        #extension GL_ANGLE_texture_multisample_array : require
        precision highp float;
        uniform highp sampler2DMSArray s;
        uniform highp isampler2DMSArray is;
        uniform highp usampler2DMSArray us;

        void main() {
            ivec3 size = textureSize(s);
            size = textureSize(is);
            size = textureSize(us);
        })";

    if (!compile(shaderString))
    {
        FAIL() << "Shader compilation failed, expecting success:\n" << mInfoLog;
    }
}

// Check that sampler2DMSArray has no default precision.
TEST_F(SamplerMultisampleArrayTest, NoPrecisionSampler2DMSArray)
{
    const std::string &shaderString =
        R"(#version 310 es
        #extension GL_ANGLE_texture_multisample_array : require
        precision highp float;
        uniform sampler2DMSArray s;

        void main() {})";

    if (compile(shaderString))
    {
        FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
    }
}

// Check that isampler2DMSArray has no default precision.
TEST_F(SamplerMultisampleArrayTest, NoPrecisionISampler2DMSArray)
{
    const std::string &shaderString =
        R"(#version 310 es
        #extension GL_ANGLE_texture_multisample_array : require
        precision highp float;
        uniform isampler2DMSArray s;

        void main() {})";

    if (compile(shaderString))
    {
        FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
    }
}

// Check that usampler2DMSArray has no default precision.
TEST_F(SamplerMultisampleArrayTest, NoPrecisionUSampler2DMSArray)
{
    const std::string &shaderString =
        R"(#version 310 es
        #extension GL_ANGLE_texture_multisample_array : require
        precision highp float;
        uniform usampler2DMSArray s;

        void main() {})";

    if (compile(shaderString))
    {
        FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
    }
}
