blob: 8f950f1a374d5067e1794cecaecefecd3cf6986d [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 {
Geoff Lang5ade8452015-09-02 11:00:30 -0400204 EGLWindow *eglWindow =
205 new EGLWindow(param.majorVersion, param.minorVersion, param.eglParameters);
Austin Kinross137b1512015-06-17 16:14:53 -0700206 bool result = eglWindow->initializeGL(mOSWindow);
207 if (result == false)
208 {
209 SafeDelete(eglWindow);
210 eglWindow = nullptr;
211 }
212
213 return eglWindow;
214 }
215
216 void destroyEGLWindow(EGLWindow **eglWindow)
217 {
218 ASSERT(*eglWindow != nullptr);
219 (*eglWindow)->destroyGL();
220 SafeDelete(*eglWindow);
221 *eglWindow = nullptr;
222 }
223
224 GLuint createES2ProgramFromSource()
225 {
226 const std::string testVertexShaderSource = SHADER_SOURCE
227 (
228 attribute highp vec4 position;
229
230 void main(void)
231 {
232 gl_Position = position;
233 }
234 );
235
236 const std::string testFragmentShaderSource = SHADER_SOURCE
237 (
238 void main(void)
239 {
240 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
241 }
242 );
243
244 return CompileProgram(testVertexShaderSource, testFragmentShaderSource);
245 }
246
247 GLuint createES3ProgramFromSource()
248 {
249 const std::string testVertexShaderSource = SHADER_SOURCE
250 ( #version 300 es\n
251 precision highp float;
252 in highp vec4 position;
253
254 void main(void)
255 {
256 gl_Position = position;
257 }
258 );
259
260 const std::string testFragmentShaderSource = SHADER_SOURCE
261 ( #version 300 es \n
262 precision highp float;
263 out vec4 out_FragColor;
264
265 void main(void)
266 {
267 out_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
268 }
269 );
270
271 return CompileProgram(testVertexShaderSource, testFragmentShaderSource);
272 }
273
274 void drawWithProgram(GLuint program)
275 {
276 glClearColor(0, 0, 0, 1);
277 glClear(GL_COLOR_BUFFER_BIT);
278
279 GLint positionLocation = glGetAttribLocation(program, "position");
280
281 glUseProgram(program);
282
283 const GLfloat vertices[] =
284 {
285 -1.0f, 1.0f, 0.5f,
286 -1.0f, -1.0f, 0.5f,
287 1.0f, -1.0f, 0.5f,
288
289 -1.0f, 1.0f, 0.5f,
290 1.0f, -1.0f, 0.5f,
291 1.0f, 1.0f, 0.5f,
292 };
293
294 glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, vertices);
295 glEnableVertexAttribArray(positionLocation);
296
297 glDrawArrays(GL_TRIANGLES, 0, 6);
298
299 glDisableVertexAttribArray(positionLocation);
300 glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, 0, NULL);
301
302 EXPECT_PIXEL_EQ(mOSWindow->getWidth() / 2, mOSWindow->getHeight() / 2, 255, 0, 0, 255);
303 }
304
305 void TearDown() override
306 {
307 mOSWindow->destroy();
308 SafeDelete(mOSWindow);
309 }
310
311 OSWindow *mOSWindow;
312};
313
314// Tries to create a program binary using one set of platform params, then load it using a different sent of params
315TEST_P(ProgramBinariesAcrossPlatforms, CreateAndReloadBinary)
316{
317 angle::PlatformParameters firstRenderer = GetParam();
318 angle::PlatformParameters secondRenderer = GetParam().loadParams;
319 bool expectedLinkResult = GetParam().expectedLinkResult;
320
321 if (!(IsPlatformAvailable(firstRenderer)))
322 {
323 std::cout << "First renderer not supported, skipping test";
324 return;
325 }
326
327 if (!(IsPlatformAvailable(secondRenderer)))
328 {
329 std::cout << "Second renderer not supported, skipping test";
330 return;
331 }
332
333 EGLWindow *eglWindow = nullptr;
334 std::vector<uint8_t> binary(0);
335 GLuint program = 0;
336
337 GLint programLength = 0;
338 GLint writtenLength = 0;
339 GLenum binaryFormat = 0;
340
341 // Create a EGL window with the first renderer
342 eglWindow = createAndInitEGLWindow(firstRenderer);
343 if (eglWindow == nullptr)
344 {
345 FAIL() << "Failed to create EGL window";
346 return;
347 }
348
349 // If the test is trying to use both the default GPU and WARP, but the default GPU *IS* WARP,
350 // then our expectations for the test results will be invalid.
351 if (firstRenderer.eglParameters.deviceType != EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE &&
352 secondRenderer.eglParameters.deviceType == EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE)
353 {
354 std::string rendererString = std::string(reinterpret_cast<const char*>(glGetString(GL_RENDERER)));
355 std::transform(rendererString.begin(), rendererString.end(), rendererString.begin(), ::tolower);
356
357 auto basicRenderPos = rendererString.find(std::string("microsoft basic render"));
358 auto softwareAdapterPos = rendererString.find(std::string("software adapter"));
359
360 if (basicRenderPos != std::string::npos || softwareAdapterPos != std::string::npos)
361 {
362 // The first renderer is using WARP, even though we didn't explictly request it
363 // We should skip this test
364 std::cout << "Test skipped on when default GPU is WARP." << std::endl;
365 return;
366 }
367 }
368
369 // Create a program
370 if (firstRenderer.majorVersion == 3)
371 {
372 program = createES3ProgramFromSource();
373 }
374 else
375 {
376 program = createES2ProgramFromSource();
377 }
378
379 if (program == 0)
380 {
Austin Kinross137b1512015-06-17 16:14:53 -0700381 destroyEGLWindow(&eglWindow);
Corentin Wallezefb6ac62015-09-02 08:10:09 -0700382 FAIL() << "Failed to create program from source";
Austin Kinross137b1512015-06-17 16:14:53 -0700383 }
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 );