Path: blob/main_old/src/tests/compiler_tests/APPLE_clip_distance_test.cpp
1693 views
//1// Copyright 2020 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// APPLE_clip_distance_test.cpp:6// Test for APPLE_clip_distance7//89#include "tests/test_utils/ShaderExtensionTest.h"1011namespace12{13const char EXTPragma[] = "#extension GL_APPLE_clip_distance : require\n";1415// Shader using gl_ClipDistance16const char ESSL100_APPLEClipDistanceShader1[] =17R"(18uniform vec4 uPlane;1920attribute vec4 aPosition;2122void main()23{24gl_Position = aPosition;25gl_ClipDistance[1] = dot(aPosition, uPlane);26})";2728// Shader redeclares gl_ClipDistance29const char ESSL100_APPLEClipDistanceShader2[] =30R"(31uniform vec4 uPlane;3233attribute vec4 aPosition;3435varying highp float gl_ClipDistance[4];3637void main()38{39gl_Position = aPosition;40gl_ClipDistance[gl_MaxClipDistances - 6 + 1] = dot(aPosition, uPlane);41gl_ClipDistance[gl_MaxClipDistances - int(aPosition.x)] = dot(aPosition, uPlane);42})";4344class APPLEClipDistanceTest : public sh::ShaderExtensionTest45{46public:47void InitializeCompiler() { InitializeCompiler(SH_GLSL_130_OUTPUT); }48void InitializeCompiler(ShShaderOutput shaderOutputType)49{50DestroyCompiler();5152mCompiler = sh::ConstructCompiler(GL_VERTEX_SHADER, testing::get<0>(GetParam()),53shaderOutputType, &mResources);54ASSERT_TRUE(mCompiler != nullptr) << "Compiler could not be constructed.";55}5657testing::AssertionResult TestShaderCompile(const char *pragma)58{59const char *shaderStrings[] = {testing::get<1>(GetParam()), pragma,60testing::get<2>(GetParam())};61bool success = sh::Compile(mCompiler, shaderStrings, 3, SH_VARIABLES | SH_OBJECT_CODE);62if (success)63{64return ::testing::AssertionSuccess() << "Compilation success";65}66return ::testing::AssertionFailure() << sh::GetInfoLog(mCompiler);67}68};6970// Extension flag is required to compile properly. Expect failure when it is71// not present.72TEST_P(APPLEClipDistanceTest, CompileFailsWithoutExtension)73{74mResources.APPLE_clip_distance = 0;75InitializeCompiler();76EXPECT_FALSE(TestShaderCompile(EXTPragma));77}7879// Extension directive is required to compile properly. Expect failure when80// it is not present.81TEST_P(APPLEClipDistanceTest, CompileFailsWithExtensionWithoutPragma)82{83mResources.APPLE_clip_distance = 1;84InitializeCompiler();85EXPECT_FALSE(TestShaderCompile(""));86}8788// With extension flag and extension directive, compiling succeeds.89// Also test that the extension directive state is reset correctly.90TEST_P(APPLEClipDistanceTest, CompileSucceedsWithExtensionAndPragma)91{92mResources.APPLE_clip_distance = 1;93mResources.MaxClipDistances = 8;94InitializeCompiler();95EXPECT_TRUE(TestShaderCompile(EXTPragma));96// Test reset functionality.97EXPECT_FALSE(TestShaderCompile(""));98EXPECT_TRUE(TestShaderCompile(EXTPragma));99}100101#if defined(ANGLE_ENABLE_VULKAN)102// With extension flag and extension directive, compiling using TranslatorVulkan succeeds.103TEST_P(APPLEClipDistanceTest, CompileSucceedsVulkan)104{105mResources.APPLE_clip_distance = 1;106mResources.MaxClipDistances = 8;107108InitializeCompiler(SH_SPIRV_VULKAN_OUTPUT);109EXPECT_TRUE(TestShaderCompile(EXTPragma));110}111112// Test that the SPIR-V gen path can compile a shader when this extension is not supported.113TEST_P(APPLEClipDistanceTest, CompileSucceedsWithoutExtSupportVulkan)114{115mResources.APPLE_clip_distance = 0;116mResources.MaxClipDistances = 0;117mResources.MaxCullDistances = 0;118119InitializeCompiler(SH_SPIRV_VULKAN_OUTPUT);120121constexpr char kNoClipCull[] = R"(122void main()123{124gl_Position = vec4(0);125})";126const char *shaderStrings[] = {kNoClipCull};127128bool success =129sh::Compile(mCompiler, shaderStrings, 1, SH_OBJECT_CODE | SH_GENERATE_SPIRV_DIRECTLY);130if (success)131{132::testing::AssertionSuccess() << "Compilation success";133}134else135{136::testing::AssertionFailure() << sh::GetInfoLog(mCompiler);137}138139EXPECT_TRUE(success);140}141#endif142143#if defined(ANGLE_ENABLE_METAL)144// With extension flag and extension directive, compiling using TranslatorMetal succeeds.145TEST_P(APPLEClipDistanceTest, CompileSucceedsMetal)146{147mResources.APPLE_clip_distance = 1;148mResources.MaxClipDistances = 8;149150InitializeCompiler(SH_SPIRV_METAL_OUTPUT);151EXPECT_TRUE(TestShaderCompile(EXTPragma));152}153#endif154155// The SL #version 100 shaders that are correct work similarly156// in both GL2 and GL3, with and without the version string.157INSTANTIATE_TEST_SUITE_P(CorrectESSL100Shaders,158APPLEClipDistanceTest,159Combine(Values(SH_GLES2_SPEC),160Values(sh::ESSLVersion100),161Values(ESSL100_APPLEClipDistanceShader1,162ESSL100_APPLEClipDistanceShader2)));163164} // anonymous namespace165166167