xref: /aosp_15_r20/external/angle/src/tests/gl_tests/RendererTest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2015 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 // RendererTest:
7 //   These tests are designed to ensure that the various configurations of the test fixtures work as
8 //   expected. If one of these tests fails, then it is likely that some of the other tests are being
9 //   configured incorrectly. For example, they might be using the D3D11 renderer when the test is
10 //   meant to be using the D3D9 renderer.
11 
12 #include "common/string_utils.h"
13 #include "test_utils/ANGLETest.h"
14 #include "test_utils/gl_raii.h"
15 #include "util/shader_utils.h"
16 #include "util/test_utils.h"
17 
18 using namespace angle;
19 
20 namespace angle
21 {
22 
23 class RendererTest : public ANGLETest<>
24 {
25   protected:
RendererTest()26     RendererTest()
27     {
28         setWindowWidth(128);
29         setWindowHeight(128);
30     }
31 };
32 
33 // Print vendor, renderer, version and extension strings. Useful for debugging.
TEST_P(RendererTest,Strings)34 TEST_P(RendererTest, Strings)
35 {
36     std::cout << "Renderer: " << glGetString(GL_RENDERER) << std::endl;
37     std::cout << "Vendor: " << glGetString(GL_VENDOR) << std::endl;
38     std::cout << "Version: " << glGetString(GL_VERSION) << std::endl;
39     std::cout << "Extensions: " << glGetString(GL_EXTENSIONS) << std::endl;
40     EXPECT_GL_NO_ERROR();
41 }
42 
TEST_P(RendererTest,RequestedRendererCreated)43 TEST_P(RendererTest, RequestedRendererCreated)
44 {
45     std::string rendererString =
46         std::string(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
47     angle::ToLower(&rendererString);
48 
49     std::string versionString =
50         std::string(reinterpret_cast<const char *>(glGetString(GL_VERSION)));
51     angle::ToLower(&versionString);
52 
53     const EGLPlatformParameters &platform = GetParam().eglParameters;
54 
55     // Ensure that the renderer string contains D3D11, if we requested a D3D11 renderer.
56     if (platform.renderer == EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE)
57     {
58         ASSERT_NE(rendererString.find(std::string("direct3d11")), std::string::npos);
59     }
60 
61     // Ensure that the renderer string contains D3D9, if we requested a D3D9 renderer.
62     if (platform.renderer == EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE)
63     {
64         ASSERT_NE(rendererString.find(std::string("direct3d9")), std::string::npos);
65     }
66 
67     // Ensure that the major and minor versions trigger expected behavior in D3D11
68     if (platform.renderer == EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE)
69     {
70         // Ensure that the renderer uses WARP, if we requested it.
71         if (platform.deviceType == EGL_PLATFORM_ANGLE_DEVICE_TYPE_D3D_WARP_ANGLE)
72         {
73             auto basicRenderPos     = rendererString.find(std::string("microsoft basic render"));
74             auto softwareAdapterPos = rendererString.find(std::string("software adapter"));
75             ASSERT_TRUE(basicRenderPos != std::string::npos ||
76                         softwareAdapterPos != std::string::npos);
77         }
78 
79         std::vector<std::string> acceptableShaderModels;
80 
81         // When no specific major/minor version is requested, then ANGLE should return the highest
82         // possible feature level by default. The current hardware driver might not support Feature
83         // Level 11_0, but WARP always does. Therefore if WARP is specified but no major/minor
84         // version is specified, then we test to check that ANGLE returns FL11_0.
85         if (platform.majorVersion >= 11 || platform.majorVersion == EGL_DONT_CARE)
86         {
87             // Feature Level 10_0 corresponds to shader model 5_0
88             acceptableShaderModels.push_back("ps_5_0");
89         }
90 
91         if (platform.majorVersion >= 10 || platform.majorVersion == EGL_DONT_CARE)
92         {
93             if (platform.minorVersion >= 1 || platform.minorVersion == EGL_DONT_CARE)
94             {
95                 // Feature Level 10_1 corresponds to shader model 4_1
96                 acceptableShaderModels.push_back("ps_4_1");
97             }
98 
99             if (platform.minorVersion >= 0 || platform.minorVersion == EGL_DONT_CARE)
100             {
101                 // Feature Level 10_0 corresponds to shader model 4_0
102                 acceptableShaderModels.push_back("ps_4_0");
103             }
104         }
105 
106         if (platform.majorVersion == 9 && platform.minorVersion == 3)
107         {
108             acceptableShaderModels.push_back("ps_4_0_level_9_3");
109         }
110 
111         bool found = false;
112         for (size_t i = 0; i < acceptableShaderModels.size(); i++)
113         {
114             if (rendererString.find(acceptableShaderModels[i]) != std::string::npos)
115             {
116                 found = true;
117             }
118         }
119 
120         ASSERT_TRUE(found);
121     }
122 
123     if (platform.renderer == EGL_PLATFORM_ANGLE_TYPE_NULL_ANGLE)
124     {
125         ASSERT_TRUE(IsNULL());
126     }
127 
128     if (platform.renderer == EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE)
129     {
130         ASSERT_TRUE(IsVulkan());
131     }
132 
133     EGLint glesMajorVersion = GetParam().majorVersion;
134     EGLint glesMinorVersion = GetParam().minorVersion;
135 
136     std::ostringstream expectedVersionString;
137     expectedVersionString << "es " << glesMajorVersion << "." << glesMinorVersion;
138 
139     ASSERT_NE(versionString.find(expectedVersionString.str()), std::string::npos);
140 
141     ASSERT_GL_NO_ERROR();
142     ASSERT_EGL_SUCCESS();
143 }
144 
145 // Perform a simple operation (clear and read pixels) to verify the device is working
TEST_P(RendererTest,SimpleOperation)146 TEST_P(RendererTest, SimpleOperation)
147 {
148     if (IsNULL())
149     {
150         std::cout << "ANGLE NULL backend clears are not functional" << std::endl;
151         return;
152     }
153 
154     glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
155     glClear(GL_COLOR_BUFFER_BIT);
156     EXPECT_PIXEL_EQ(0, 0, 0, 255, 0, 255);
157 
158     ASSERT_GL_NO_ERROR();
159 }
160 
161 // Perform a simple buffer operation.
TEST_P(RendererTest,BufferData)162 TEST_P(RendererTest, BufferData)
163 {
164     constexpr size_t kBufferSize = 1024;
165     std::array<uint8_t, kBufferSize> data;
166     for (size_t i = 0; i < kBufferSize; i++)
167     {
168         data[i] = static_cast<uint8_t>(i);
169     }
170 
171     // All at once in the glBufferData call
172     {
173         GLBuffer buffer;
174         glBindBuffer(GL_ARRAY_BUFFER, buffer);
175 
176         glBufferData(GL_ARRAY_BUFFER, 1024, data.data(), GL_STATIC_DRAW);
177     }
178 
179     // Set data with sub data
180     {
181         GLBuffer buffer;
182         glBindBuffer(GL_ARRAY_BUFFER, buffer);
183 
184         glBufferData(GL_ARRAY_BUFFER, 1024, nullptr, GL_STATIC_DRAW);
185         glBufferSubData(GL_ARRAY_BUFFER, 0, kBufferSize, data.data());
186     }
187 }
188 
189 // Compile simple vertex and fragment shaders
TEST_P(RendererTest,CompileShader)190 TEST_P(RendererTest, CompileShader)
191 {
192     GLuint vs = CompileShader(GL_VERTEX_SHADER, essl1_shaders::vs::Zero());
193     EXPECT_NE(vs, 0u);
194     glDeleteShader(vs);
195 
196     GLuint fs = CompileShader(GL_FRAGMENT_SHADER, essl1_shaders::fs::Red());
197     EXPECT_NE(fs, 0u);
198     glDeleteShader(fs);
199 }
200 
201 // Link a simple program
TEST_P(RendererTest,LinkProgram)202 TEST_P(RendererTest, LinkProgram)
203 {
204     ANGLE_GL_PROGRAM(prog, essl1_shaders::vs::Zero(), essl1_shaders::fs::Red());
205 }
206 
207 // Draw a triangle using no vertex attributes
TEST_P(RendererTest,Draw)208 TEST_P(RendererTest, Draw)
209 {
210     ANGLE_GL_PROGRAM(prog, essl1_shaders::vs::Zero(), essl1_shaders::fs::Red());
211     glUseProgram(prog);
212     glDrawArrays(GL_TRIANGLES, 0, 3);
213 }
214 
215 // Select configurations (e.g. which renderer, which GLES major version) these tests should be run
216 // against.
217 // TODO(http://anglebug.com/42266907): move ES2_WEBGPU to the definition of
218 // ANGLE_ALL_TEST_PLATFORMS_ES2 once webgpu is developed enough to run more tests.
219 ANGLE_INSTANTIATE_TEST_ES2_AND_ES3_AND_ES31_AND_NULL_AND(RendererTest,
220                                                          ES2_WEBGPU());
221 }  // namespace angle
222