blob: cd8e0b350c4bacdf84f212b518e9d33a635049e6 [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);
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700141 glGetProgramInfoLog(program2, static_cast<GLsizei>(infoLog.size()), NULL,
142 &infoLog[0]);
Geoff Lang981afd72014-11-05 16:30:36 -0500143 FAIL() << "program link failed: " << &infoLog[0];
144 }
145 else
146 {
147 FAIL() << "program link failed.";
148 }
Brandon Jones993d08d2014-10-10 14:32:57 -0700149 }
150 else
151 {
152 glUseProgram(program2);
153 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
154
155 glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 8, NULL);
156 glEnableVertexAttribArray(0);
157 glDrawArrays(GL_POINTS, 0, 1);
158
159 EXPECT_GL_NO_ERROR();
160 }
161
162 glDeleteProgram(program2);
163 }
164}
Jamie Madillfa05f602015-05-07 13:47:11 -0400165
166// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
167ANGLE_INSTANTIATE_TEST(ProgramBinaryTest, ES2_D3D9(), ES2_D3D11());
Austin Kinross137b1512015-06-17 16:14:53 -0700168
169// For the ProgramBinariesAcrossPlatforms tests, we need two sets of params:
170// - a set to save the program binary
171// - a set to load the program binary
172// We combine these into one struct extending PlatformParameters so we can reuse existing ANGLE test macros
173struct PlatformsWithLinkResult : PlatformParameters
174{
175 PlatformsWithLinkResult(PlatformParameters saveParams, PlatformParameters loadParamsIn, bool expectedLinkResultIn)
176 {
177 majorVersion = saveParams.majorVersion;
178 minorVersion = saveParams.minorVersion;
179 eglParameters = saveParams.eglParameters;
180 loadParams = loadParamsIn;
181 expectedLinkResult = expectedLinkResultIn;
182 }
183
184 PlatformParameters loadParams;
185 bool expectedLinkResult;
186};
187
188class ProgramBinariesAcrossPlatforms : public testing::TestWithParam<PlatformsWithLinkResult>
189{
190 public:
191 void SetUp() override
192 {
193 mOSWindow = CreateOSWindow();
194 bool result = mOSWindow->initialize("ProgramBinariesAcrossRenderersTests", 100, 100);
195
196 if (result == false)
197 {
198 FAIL() << "Failed to create OS window";
199 }
200 }
201
202 EGLWindow *createAndInitEGLWindow(angle::PlatformParameters &param)
203 {
Corentin Wallezf3357ee2015-07-22 14:10:19 -0400204 EGLWindow *eglWindow = new EGLWindow(param.majorVersion, param.eglParameters);
Austin Kinross137b1512015-06-17 16:14:53 -0700205 bool result = eglWindow->initializeGL(mOSWindow);
206 if (result == false)
207 {
208 SafeDelete(eglWindow);
209 eglWindow = nullptr;
210 }
211
212 return eglWindow;
213 }
214
215 void destroyEGLWindow(EGLWindow **eglWindow)
216 {
217 ASSERT(*eglWindow != nullptr);
218 (*eglWindow)->destroyGL();
219 SafeDelete(*eglWindow);
220 *eglWindow = nullptr;
221 }
222
223 GLuint createES2ProgramFromSource()
224 {
225 const std::string testVertexShaderSource = SHADER_SOURCE
226 (
227 attribute highp vec4 position;
228
229 void main(void)
230 {
231 gl_Position = position;
232 }
233 );
234
235 const std::string testFragmentShaderSource = SHADER_SOURCE
236 (
237 void main(void)
238 {
239 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
240 }
241 );
242
243 return CompileProgram(testVertexShaderSource, testFragmentShaderSource);
244 }
245
246 GLuint createES3ProgramFromSource()
247 {
248 const std::string testVertexShaderSource = SHADER_SOURCE
249 ( #version 300 es\n
250 precision highp float;
251 in highp vec4 position;
252
253 void main(void)
254 {
255 gl_Position = position;
256 }
257 );
258
259 const std::string testFragmentShaderSource = SHADER_SOURCE
260 ( #version 300 es \n
261 precision highp float;
262 out vec4 out_FragColor;
263
264 void main(void)
265 {
266 out_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
267 }
268 );
269
270 return CompileProgram(testVertexShaderSource, testFragmentShaderSource);
271 }
272
273 void drawWithProgram(GLuint program)
274 {
275 glClearColor(0, 0, 0, 1);
276 glClear(GL_COLOR_BUFFER_BIT);
277
278 GLint positionLocation = glGetAttribLocation(program, "position");
279
280 glUseProgram(program);
281
282 const GLfloat vertices[] =
283 {
284 -1.0f, 1.0f, 0.5f,
285 -1.0f, -1.0f, 0.5f,
286 1.0f, -1.0f, 0.5f,
287
288 -1.0f, 1.0f, 0.5f,
289 1.0f, -1.0f, 0.5f,
290 1.0f, 1.0f, 0.5f,
291 };
292
293 glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, vertices);
294 glEnableVertexAttribArray(positionLocation);
295
296 glDrawArrays(GL_TRIANGLES, 0, 6);
297
298 glDisableVertexAttribArray(positionLocation);
299 glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, 0, NULL);
300
301 EXPECT_PIXEL_EQ(mOSWindow->getWidth() / 2, mOSWindow->getHeight() / 2, 255, 0, 0, 255);
302 }
303
304 void TearDown() override
305 {
306 mOSWindow->destroy();
307 SafeDelete(mOSWindow);
308 }
309
310 OSWindow *mOSWindow;
311};
312
313// Tries to create a program binary using one set of platform params, then load it using a different sent of params
314TEST_P(ProgramBinariesAcrossPlatforms, CreateAndReloadBinary)
315{
316 angle::PlatformParameters firstRenderer = GetParam();
317 angle::PlatformParameters secondRenderer = GetParam().loadParams;
318 bool expectedLinkResult = GetParam().expectedLinkResult;
319
320 if (!(IsPlatformAvailable(firstRenderer)))
321 {
322 std::cout << "First renderer not supported, skipping test";
323 return;
324 }
325
326 if (!(IsPlatformAvailable(secondRenderer)))
327 {
328 std::cout << "Second renderer not supported, skipping test";
329 return;
330 }
331
332 EGLWindow *eglWindow = nullptr;
333 std::vector<uint8_t> binary(0);
334 GLuint program = 0;
335
336 GLint programLength = 0;
337 GLint writtenLength = 0;
338 GLenum binaryFormat = 0;
339
340 // Create a EGL window with the first renderer
341 eglWindow = createAndInitEGLWindow(firstRenderer);
342 if (eglWindow == nullptr)
343 {
344 FAIL() << "Failed to create EGL window";
345 return;
346 }
347
348 // If the test is trying to use both the default GPU and WARP, but the default GPU *IS* WARP,
349 // then our expectations for the test results will be invalid.
350 if (firstRenderer.eglParameters.deviceType != EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE &&
351 secondRenderer.eglParameters.deviceType == EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE)
352 {
353 std::string rendererString = std::string(reinterpret_cast<const char*>(glGetString(GL_RENDERER)));
354 std::transform(rendererString.begin(), rendererString.end(), rendererString.begin(), ::tolower);
355
356 auto basicRenderPos = rendererString.find(std::string("microsoft basic render"));
357 auto softwareAdapterPos = rendererString.find(std::string("software adapter"));
358
359 if (basicRenderPos != std::string::npos || softwareAdapterPos != std::string::npos)
360 {
361 // The first renderer is using WARP, even though we didn't explictly request it
362 // We should skip this test
363 std::cout << "Test skipped on when default GPU is WARP." << std::endl;
364 return;
365 }
366 }
367
368 // Create a program
369 if (firstRenderer.majorVersion == 3)
370 {
371 program = createES3ProgramFromSource();
372 }
373 else
374 {
375 program = createES2ProgramFromSource();
376 }
377
378 if (program == 0)
379 {
380 FAIL() << "Failed to create program from source";
381 destroyEGLWindow(&eglWindow);
382 return;
383 }
384
385 // Draw using the program to ensure it works as expected
386 drawWithProgram(program);
387 EXPECT_GL_NO_ERROR();
388
389 // Save the program binary out from this renderer
390 glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH_OES, &programLength);
391 EXPECT_GL_NO_ERROR();
392 binary.resize(programLength);
393 glGetProgramBinaryOES(program, programLength, &writtenLength, &binaryFormat, binary.data());
394 EXPECT_GL_NO_ERROR();
395
396 // Destroy the first renderer
397 glDeleteProgram(program);
398 destroyEGLWindow(&eglWindow);
399
400 // Create an EGL window with the second renderer
401 eglWindow = createAndInitEGLWindow(secondRenderer);
402 if (eglWindow == nullptr)
403 {
404 FAIL() << "Failed to create EGL window";
405 return;
406 }
407
408 program = glCreateProgram();
409 glProgramBinaryOES(program, binaryFormat, binary.data(), writtenLength);
410
411 GLint linkStatus;
412 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
413 EXPECT_EQ(expectedLinkResult, (linkStatus != 0));
414
415 if (linkStatus != 0)
416 {
417 // If the link was successful, then we should try to draw using the program to ensure it works as expected
418 drawWithProgram(program);
419 EXPECT_GL_NO_ERROR();
420 }
421
422 // Destroy the second renderer
423 glDeleteProgram(program);
424 destroyEGLWindow(&eglWindow);
425}
426
427ANGLE_INSTANTIATE_TEST(ProgramBinariesAcrossPlatforms,
428 // | Save the program | Load the program | Expected
429 // | using these params | using these params | link result
430 PlatformsWithLinkResult(ES2_D3D11(), ES2_D3D11(), true ), // Loading + reloading binary should work
431 PlatformsWithLinkResult(ES3_D3D11(), ES3_D3D11(), true ), // Loading + reloading binary should work
432 PlatformsWithLinkResult(ES2_D3D11_FL11_0(), ES2_D3D11_FL9_3(), false ), // Switching feature level shouldn't work
433 PlatformsWithLinkResult(ES2_D3D11(), ES2_D3D11_WARP(), false ), // Switching from hardware to software shouldn't work
434 PlatformsWithLinkResult(ES2_D3D11_FL9_3(), ES2_D3D11_FL9_3_WARP(), false ), // Switching from hardware to software shouldn't work for FL9 either
435 PlatformsWithLinkResult(ES2_D3D11(), ES2_D3D9(), false ), // Switching from D3D11 to D3D9 shouldn't work
436 PlatformsWithLinkResult(ES2_D3D9(), ES2_D3D11(), false ), // Switching from D3D9 to D3D11 shouldn't work
437 PlatformsWithLinkResult(ES2_D3D11(), ES3_D3D11(), true ) // Switching to newer client version should work
438
439 // TODO: ANGLE issue 523
440 // Compiling a program with client version 3, saving the binary, then loading it with client version 2 should not work
441 // PlatformsWithLinkResult(ES3_D3D11(), ES2_D3D11(), false )
Corentin Wallezf3357ee2015-07-22 14:10:19 -0400442 );