blob: 6ad5ba5380367e067acf84006718351177cf59d4 [file] [log] [blame]
Jamie Madillfa05f602015-05-07 13:47:11 -04001//
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
Corentin Wallezd3970de2015-05-14 11:07:48 -04007#include "test_utils/ANGLETest.h"
Jamie Madillfa05f602015-05-07 13:47:11 -04008
Brandon Jones993d08d2014-10-10 14:32:57 -07009#include <memory>
10#include <stdint.h>
Jamie Madill7a29e4a2014-05-02 10:41:48 -040011
Austin Kinross137b1512015-06-17 16:14:53 -070012#include "EGLWindow.h"
13#include "OSWindow.h"
14#include "test_utils/angle_test_configs.h"
15
Jamie Madillfa05f602015-05-07 13:47:11 -040016using namespace angle;
Austin Kinross18b931d2014-09-29 12:58:31 -070017
Jamie Madill7a29e4a2014-05-02 10:41:48 -040018class ProgramBinaryTest : public ANGLETest
19{
Geoff Lang0d3683c2014-10-23 11:08:16 -040020 protected:
Jamie Madillfa05f602015-05-07 13:47:11 -040021 ProgramBinaryTest()
Jamie Madill7a29e4a2014-05-02 10:41:48 -040022 {
23 setWindowWidth(128);
24 setWindowHeight(128);
25 setConfigRedBits(8);
26 setConfigGreenBits(8);
27 setConfigBlueBits(8);
28 setConfigAlphaBits(8);
29 }
30
31 virtual void SetUp()
32 {
33 ANGLETest::SetUp();
34
35 const std::string vertexShaderSource = SHADER_SOURCE
36 (
37 attribute vec4 inputAttribute;
38 void main()
39 {
40 gl_Position = inputAttribute;
41 }
42 );
43
44 const std::string fragmentShaderSource = SHADER_SOURCE
45 (
46 void main()
47 {
48 gl_FragColor = vec4(1,0,0,1);
49 }
50 );
51
Jamie Madill5599c8f2014-08-26 13:16:39 -040052 mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource);
Jamie Madill7a29e4a2014-05-02 10:41:48 -040053 if (mProgram == 0)
54 {
55 FAIL() << "shader compilation failed.";
56 }
57
58 glGenBuffers(1, &mBuffer);
59 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
60 glBufferData(GL_ARRAY_BUFFER, 128, NULL, GL_STATIC_DRAW);
61 glBindBuffer(GL_ARRAY_BUFFER, 0);
62
63 ASSERT_GL_NO_ERROR();
64 }
65
66 virtual void TearDown()
67 {
68 glDeleteProgram(mProgram);
69 glDeleteBuffers(1, &mBuffer);
70
71 ANGLETest::TearDown();
72 }
73
74 GLuint mProgram;
75 GLuint mBuffer;
76};
77
78// This tests the assumption that float attribs of different size
79// should not internally cause a vertex shader recompile (for conversion).
Jamie Madillfa05f602015-05-07 13:47:11 -040080TEST_P(ProgramBinaryTest, FloatDynamicShaderSize)
Jamie Madill7a29e4a2014-05-02 10:41:48 -040081{
82 glUseProgram(mProgram);
83 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
84
85 glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 8, NULL);
86 glEnableVertexAttribArray(0);
87 glDrawArrays(GL_POINTS, 0, 1);
88
89 GLint programLength;
90 glGetProgramiv(mProgram, GL_PROGRAM_BINARY_LENGTH_OES, &programLength);
91
92 EXPECT_GL_NO_ERROR();
93
94 for (GLsizei size = 1; size <= 3; size++)
95 {
96 glVertexAttribPointer(0, size, GL_FLOAT, GL_FALSE, 8, NULL);
97 glEnableVertexAttribArray(0);
98 glDrawArrays(GL_POINTS, 0, 1);
99
100 GLint newProgramLength;
101 glGetProgramiv(mProgram, GL_PROGRAM_BINARY_LENGTH_OES, &newProgramLength);
102 EXPECT_GL_NO_ERROR();
103 EXPECT_EQ(programLength, newProgramLength);
104 }
105}
Brandon Jones993d08d2014-10-10 14:32:57 -0700106
107// This tests the ability to successfully save and load a program binary.
Jamie Madillfa05f602015-05-07 13:47:11 -0400108TEST_P(ProgramBinaryTest, SaveAndLoadBinary)
Brandon Jones993d08d2014-10-10 14:32:57 -0700109{
110 GLint programLength = 0;
111 GLint writtenLength = 0;
112 GLenum binaryFormat = 0;
113
114 glGetProgramiv(mProgram, GL_PROGRAM_BINARY_LENGTH_OES, &programLength);
115 EXPECT_GL_NO_ERROR();
116
117 std::vector<uint8_t> binary(programLength);
118 glGetProgramBinaryOES(mProgram, programLength, &writtenLength, &binaryFormat, binary.data());
119 EXPECT_GL_NO_ERROR();
120
121 // The lengths reported by glGetProgramiv and glGetProgramBinaryOES should match
122 EXPECT_EQ(programLength, writtenLength);
123
124 if (writtenLength)
125 {
126 GLuint program2 = glCreateProgram();
127 glProgramBinaryOES(program2, binaryFormat, binary.data(), writtenLength);
128
129 EXPECT_GL_NO_ERROR();
130
131 GLint linkStatus;
132 glGetProgramiv(program2, GL_LINK_STATUS, &linkStatus);
133 if (linkStatus == 0)
134 {
135 GLint infoLogLength;
136 glGetProgramiv(program2, GL_INFO_LOG_LENGTH, &infoLogLength);
137
Geoff Lang981afd72014-11-05 16:30:36 -0500138 if (infoLogLength > 0)
139 {
140 std::vector<GLchar> infoLog(infoLogLength);
141 glGetProgramInfoLog(program2, infoLog.size(), NULL, &infoLog[0]);
142 FAIL() << "program link failed: " << &infoLog[0];
143 }
144 else
145 {
146 FAIL() << "program link failed.";
147 }
Brandon Jones993d08d2014-10-10 14:32:57 -0700148 }
149 else
150 {
151 glUseProgram(program2);
152 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
153
154 glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 8, NULL);
155 glEnableVertexAttribArray(0);
156 glDrawArrays(GL_POINTS, 0, 1);
157
158 EXPECT_GL_NO_ERROR();
159 }
160
161 glDeleteProgram(program2);
162 }
163}
Jamie Madillfa05f602015-05-07 13:47:11 -0400164
165// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
166ANGLE_INSTANTIATE_TEST(ProgramBinaryTest, ES2_D3D9(), ES2_D3D11());
Austin Kinross137b1512015-06-17 16:14:53 -0700167
168// For the ProgramBinariesAcrossPlatforms tests, we need two sets of params:
169// - a set to save the program binary
170// - a set to load the program binary
171// We combine these into one struct extending PlatformParameters so we can reuse existing ANGLE test macros
172struct PlatformsWithLinkResult : PlatformParameters
173{
174 PlatformsWithLinkResult(PlatformParameters saveParams, PlatformParameters loadParamsIn, bool expectedLinkResultIn)
175 {
176 majorVersion = saveParams.majorVersion;
177 minorVersion = saveParams.minorVersion;
178 eglParameters = saveParams.eglParameters;
179 loadParams = loadParamsIn;
180 expectedLinkResult = expectedLinkResultIn;
181 }
182
183 PlatformParameters loadParams;
184 bool expectedLinkResult;
185};
186
187class ProgramBinariesAcrossPlatforms : public testing::TestWithParam<PlatformsWithLinkResult>
188{
189 public:
190 void SetUp() override
191 {
192 mOSWindow = CreateOSWindow();
193 bool result = mOSWindow->initialize("ProgramBinariesAcrossRenderersTests", 100, 100);
194
195 if (result == false)
196 {
197 FAIL() << "Failed to create OS window";
198 }
199 }
200
201 EGLWindow *createAndInitEGLWindow(angle::PlatformParameters &param)
202 {
203 EGLWindow *eglWindow = new EGLWindow(1, 1, param.majorVersion, param.eglParameters);
204 bool result = eglWindow->initializeGL(mOSWindow);
205 if (result == false)
206 {
207 SafeDelete(eglWindow);
208 eglWindow = nullptr;
209 }
210
211 return eglWindow;
212 }
213
214 void destroyEGLWindow(EGLWindow **eglWindow)
215 {
216 ASSERT(*eglWindow != nullptr);
217 (*eglWindow)->destroyGL();
218 SafeDelete(*eglWindow);
219 *eglWindow = nullptr;
220 }
221
222 GLuint createES2ProgramFromSource()
223 {
224 const std::string testVertexShaderSource = SHADER_SOURCE
225 (
226 attribute highp vec4 position;
227
228 void main(void)
229 {
230 gl_Position = position;
231 }
232 );
233
234 const std::string testFragmentShaderSource = SHADER_SOURCE
235 (
236 void main(void)
237 {
238 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
239 }
240 );
241
242 return CompileProgram(testVertexShaderSource, testFragmentShaderSource);
243 }
244
245 GLuint createES3ProgramFromSource()
246 {
247 const std::string testVertexShaderSource = SHADER_SOURCE
248 ( #version 300 es\n
249 precision highp float;
250 in highp vec4 position;
251
252 void main(void)
253 {
254 gl_Position = position;
255 }
256 );
257
258 const std::string testFragmentShaderSource = SHADER_SOURCE
259 ( #version 300 es \n
260 precision highp float;
261 out vec4 out_FragColor;
262
263 void main(void)
264 {
265 out_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
266 }
267 );
268
269 return CompileProgram(testVertexShaderSource, testFragmentShaderSource);
270 }
271
272 void drawWithProgram(GLuint program)
273 {
274 glClearColor(0, 0, 0, 1);
275 glClear(GL_COLOR_BUFFER_BIT);
276
277 GLint positionLocation = glGetAttribLocation(program, "position");
278
279 glUseProgram(program);
280
281 const GLfloat vertices[] =
282 {
283 -1.0f, 1.0f, 0.5f,
284 -1.0f, -1.0f, 0.5f,
285 1.0f, -1.0f, 0.5f,
286
287 -1.0f, 1.0f, 0.5f,
288 1.0f, -1.0f, 0.5f,
289 1.0f, 1.0f, 0.5f,
290 };
291
292 glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, vertices);
293 glEnableVertexAttribArray(positionLocation);
294
295 glDrawArrays(GL_TRIANGLES, 0, 6);
296
297 glDisableVertexAttribArray(positionLocation);
298 glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, 0, NULL);
299
300 EXPECT_PIXEL_EQ(mOSWindow->getWidth() / 2, mOSWindow->getHeight() / 2, 255, 0, 0, 255);
301 }
302
303 void TearDown() override
304 {
305 mOSWindow->destroy();
306 SafeDelete(mOSWindow);
307 }
308
309 OSWindow *mOSWindow;
310};
311
312// Tries to create a program binary using one set of platform params, then load it using a different sent of params
313TEST_P(ProgramBinariesAcrossPlatforms, CreateAndReloadBinary)
314{
315 angle::PlatformParameters firstRenderer = GetParam();
316 angle::PlatformParameters secondRenderer = GetParam().loadParams;
317 bool expectedLinkResult = GetParam().expectedLinkResult;
318
319 if (!(IsPlatformAvailable(firstRenderer)))
320 {
321 std::cout << "First renderer not supported, skipping test";
322 return;
323 }
324
325 if (!(IsPlatformAvailable(secondRenderer)))
326 {
327 std::cout << "Second renderer not supported, skipping test";
328 return;
329 }
330
331 EGLWindow *eglWindow = nullptr;
332 std::vector<uint8_t> binary(0);
333 GLuint program = 0;
334
335 GLint programLength = 0;
336 GLint writtenLength = 0;
337 GLenum binaryFormat = 0;
338
339 // Create a EGL window with the first renderer
340 eglWindow = createAndInitEGLWindow(firstRenderer);
341 if (eglWindow == nullptr)
342 {
343 FAIL() << "Failed to create EGL window";
344 return;
345 }
346
347 // If the test is trying to use both the default GPU and WARP, but the default GPU *IS* WARP,
348 // then our expectations for the test results will be invalid.
349 if (firstRenderer.eglParameters.deviceType != EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE &&
350 secondRenderer.eglParameters.deviceType == EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE)
351 {
352 std::string rendererString = std::string(reinterpret_cast<const char*>(glGetString(GL_RENDERER)));
353 std::transform(rendererString.begin(), rendererString.end(), rendererString.begin(), ::tolower);
354
355 auto basicRenderPos = rendererString.find(std::string("microsoft basic render"));
356 auto softwareAdapterPos = rendererString.find(std::string("software adapter"));
357
358 if (basicRenderPos != std::string::npos || softwareAdapterPos != std::string::npos)
359 {
360 // The first renderer is using WARP, even though we didn't explictly request it
361 // We should skip this test
362 std::cout << "Test skipped on when default GPU is WARP." << std::endl;
363 return;
364 }
365 }
366
367 // Create a program
368 if (firstRenderer.majorVersion == 3)
369 {
370 program = createES3ProgramFromSource();
371 }
372 else
373 {
374 program = createES2ProgramFromSource();
375 }
376
377 if (program == 0)
378 {
379 FAIL() << "Failed to create program from source";
380 destroyEGLWindow(&eglWindow);
381 return;
382 }
383
384 // Draw using the program to ensure it works as expected
385 drawWithProgram(program);
386 EXPECT_GL_NO_ERROR();
387
388 // Save the program binary out from this renderer
389 glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH_OES, &programLength);
390 EXPECT_GL_NO_ERROR();
391 binary.resize(programLength);
392 glGetProgramBinaryOES(program, programLength, &writtenLength, &binaryFormat, binary.data());
393 EXPECT_GL_NO_ERROR();
394
395 // Destroy the first renderer
396 glDeleteProgram(program);
397 destroyEGLWindow(&eglWindow);
398
399 // Create an EGL window with the second renderer
400 eglWindow = createAndInitEGLWindow(secondRenderer);
401 if (eglWindow == nullptr)
402 {
403 FAIL() << "Failed to create EGL window";
404 return;
405 }
406
407 program = glCreateProgram();
408 glProgramBinaryOES(program, binaryFormat, binary.data(), writtenLength);
409
410 GLint linkStatus;
411 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
412 EXPECT_EQ(expectedLinkResult, (linkStatus != 0));
413
414 if (linkStatus != 0)
415 {
416 // If the link was successful, then we should try to draw using the program to ensure it works as expected
417 drawWithProgram(program);
418 EXPECT_GL_NO_ERROR();
419 }
420
421 // Destroy the second renderer
422 glDeleteProgram(program);
423 destroyEGLWindow(&eglWindow);
424}
425
426ANGLE_INSTANTIATE_TEST(ProgramBinariesAcrossPlatforms,
427 // | Save the program | Load the program | Expected
428 // | using these params | using these params | link result
429 PlatformsWithLinkResult(ES2_D3D11(), ES2_D3D11(), true ), // Loading + reloading binary should work
430 PlatformsWithLinkResult(ES3_D3D11(), ES3_D3D11(), true ), // Loading + reloading binary should work
431 PlatformsWithLinkResult(ES2_D3D11_FL11_0(), ES2_D3D11_FL9_3(), false ), // Switching feature level shouldn't work
432 PlatformsWithLinkResult(ES2_D3D11(), ES2_D3D11_WARP(), false ), // Switching from hardware to software shouldn't work
433 PlatformsWithLinkResult(ES2_D3D11_FL9_3(), ES2_D3D11_FL9_3_WARP(), false ), // Switching from hardware to software shouldn't work for FL9 either
434 PlatformsWithLinkResult(ES2_D3D11(), ES2_D3D9(), false ), // Switching from D3D11 to D3D9 shouldn't work
435 PlatformsWithLinkResult(ES2_D3D9(), ES2_D3D11(), false ), // Switching from D3D9 to D3D11 shouldn't work
436 PlatformsWithLinkResult(ES2_D3D11(), ES3_D3D11(), true ) // Switching to newer client version should work
437
438 // TODO: ANGLE issue 523
439 // Compiling a program with client version 3, saving the binary, then loading it with client version 2 should not work
440 // PlatformsWithLinkResult(ES3_D3D11(), ES2_D3D11(), false )
441 );