xref: /aosp_15_r20/external/angle/src/tests/test_utils/ShaderExtensionTest.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2018 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 // ShaderExtensionTest.cpp:
7 //   Utilities for testing that GLSL extension pragma and changing the extension flag in compiler
8 //   resources has the correct effect.
9 //
10 
11 #include "GLSLANG/ShaderLang.h"
12 #include "angle_gl.h"
13 #include "gtest/gtest.h"
14 
15 using testing::Combine;
16 using testing::make_tuple;
17 using testing::Values;
18 
19 namespace sh
20 {
21 
22 const char ESSLVersion100[] = "#version 100\n";
23 const char ESSLVersion300[] = "#version 300 es\n";
24 const char ESSLVersion310[] = "#version 310 es\n";
25 
26 class ShaderExtensionTest
27     : public testing::TestWithParam<testing::tuple<ShShaderSpec, const char *, const char *>>
28 {
29   protected:
SetUp()30     void SetUp() override
31     {
32         sh::InitBuiltInResources(&mResources);
33         mCompiler                  = nullptr;
34         mCompileOptions            = {};
35         mCompileOptions.objectCode = true;
36     }
37 
TearDown()38     void TearDown() override { DestroyCompiler(); }
DestroyCompiler()39     void DestroyCompiler()
40     {
41         if (mCompiler)
42         {
43             sh::Destruct(mCompiler);
44             mCompiler = nullptr;
45         }
46     }
47 
InitializeCompiler()48     void InitializeCompiler()
49     {
50         DestroyCompiler();
51         mCompiler = sh::ConstructCompiler(GL_FRAGMENT_SHADER, testing::get<0>(GetParam()),
52                                           SH_GLSL_COMPATIBILITY_OUTPUT, &mResources);
53         ASSERT_TRUE(mCompiler != nullptr) << "Compiler could not be constructed.";
54     }
55 
TestShaderCompile(const char * pragma)56     testing::AssertionResult TestShaderCompile(const char *pragma)
57     {
58         return TestShaderCompile(testing::get<1>(GetParam()),  // Version.
59                                  pragma,
60                                  testing::get<2>(GetParam())  // Shader.
61         );
62     }
63 
TestShaderCompile(const char * version,const char * pragma,const char * shader)64     testing::AssertionResult TestShaderCompile(const char *version,
65                                                const char *pragma,
66                                                const char *shader)
67     {
68         const char *shaderStrings[] = {version, pragma, shader};
69         bool success                = sh::Compile(mCompiler, shaderStrings, 3, mCompileOptions);
70         if (success)
71         {
72             return ::testing::AssertionSuccess() << "Compilation success";
73         }
74         return ::testing::AssertionFailure() << sh::GetInfoLog(mCompiler);
75     }
76 
77   protected:
78     ShBuiltInResources mResources;
79     ShHandle mCompiler;
80     ShCompileOptions mCompileOptions;
81 };
82 
83 }  // namespace sh
84