Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 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 | // validationES unit tests: |
| 7 | // Unit tests for general ES validation functions. |
| 8 | // |
| 9 | |
| 10 | #include <gmock/gmock.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
Jamie Madill | 9082b98 | 2016-04-27 15:21:51 -0400 | [diff] [blame] | 13 | #include "libANGLE/ContextState.h" |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 14 | #include "libANGLE/VaryingPacking.h" |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 15 | #include "libANGLE/renderer/FramebufferImpl_mock.h" |
| 16 | #include "libANGLE/renderer/ProgramImpl_mock.h" |
| 17 | #include "libANGLE/renderer/TextureImpl_mock.h" |
| 18 | #include "libANGLE/validationES.h" |
| 19 | #include "tests/angle_unittests_utils.h" |
| 20 | |
| 21 | using namespace gl; |
| 22 | using namespace rx; |
| 23 | using testing::_; |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 24 | using testing::NiceMock; |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 25 | using testing::Return; |
| 26 | |
| 27 | namespace |
| 28 | { |
| 29 | |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 30 | class MockValidationContext : public ValidationContext |
| 31 | { |
| 32 | public: |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 33 | MockValidationContext(const ValidationContext *shareContext, |
Geoff Lang | ce02f08 | 2017-02-06 16:46:21 -0500 | [diff] [blame^] | 34 | TextureManager *shareTextures, |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 35 | const Version &version, |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 36 | State *state, |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 37 | const Caps &caps, |
| 38 | const TextureCapsMap &textureCaps, |
| 39 | const Extensions &extensions, |
Jamie Madill | 46e6c7a | 2016-01-18 14:42:30 -0500 | [diff] [blame] | 40 | const Limitations &limitations, |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 41 | bool skipValidation) |
| 42 | : ValidationContext(shareContext, |
Geoff Lang | ce02f08 | 2017-02-06 16:46:21 -0500 | [diff] [blame^] | 43 | shareTextures, |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 44 | version, |
| 45 | state, |
| 46 | caps, |
| 47 | textureCaps, |
| 48 | extensions, |
| 49 | limitations, |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 50 | skipValidation) |
| 51 | { |
| 52 | } |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 53 | |
Jamie Madill | 437fa65 | 2016-05-03 15:13:24 -0400 | [diff] [blame] | 54 | MOCK_METHOD1(handleError, void(const Error &)); |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 55 | }; |
| 56 | |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 57 | // Test that ANGLE generates an INVALID_OPERATION when validating index data that uses a value |
| 58 | // larger than MAX_ELEMENT_INDEX. Not specified in the GLES 3 spec, it's undefined behaviour, |
| 59 | // but we want a test to ensure we maintain this behaviour. |
| 60 | TEST(ValidationESTest, DrawElementsWithMaxIndexGivesError) |
| 61 | { |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 62 | auto framebufferImpl = MakeFramebufferMock(); |
| 63 | auto programImpl = MakeProgramMock(); |
| 64 | |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 65 | // TODO(jmadill): Generalize some of this code so we can re-use it for other tests. |
Jamie Madill | 7aea7e0 | 2016-05-10 10:39:45 -0400 | [diff] [blame] | 66 | NiceMock<MockGLFactory> mockFactory; |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 67 | EXPECT_CALL(mockFactory, createFramebuffer(_)).WillOnce(Return(framebufferImpl)); |
| 68 | EXPECT_CALL(mockFactory, createProgram(_)).WillOnce(Return(programImpl)); |
| 69 | EXPECT_CALL(mockFactory, createVertexArray(_)).WillOnce(Return(nullptr)); |
| 70 | |
| 71 | State state; |
| 72 | Caps caps; |
| 73 | TextureCapsMap textureCaps; |
| 74 | Extensions extensions; |
| 75 | Limitations limitations; |
| 76 | |
| 77 | // Set some basic caps. |
| 78 | caps.maxElementIndex = 100; |
| 79 | caps.maxDrawBuffers = 1; |
| 80 | caps.maxColorAttachments = 1; |
Geoff Lang | eb66a6e | 2016-10-31 13:06:12 -0400 | [diff] [blame] | 81 | state.initialize(caps, extensions, Version(3, 0), false, true); |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 82 | |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 83 | NiceMock<MockTextureImpl> *textureImpl = new NiceMock<MockTextureImpl>(); |
Olli Etuaho | 82c47ad | 2016-04-20 18:28:47 +0300 | [diff] [blame] | 84 | EXPECT_CALL(mockFactory, createTexture(_)).WillOnce(Return(textureImpl)); |
Jamie Madill | 8897afa | 2017-02-06 17:17:23 -0500 | [diff] [blame] | 85 | EXPECT_CALL(*textureImpl, setStorage(_, _, _, _, _)).WillOnce(Return(NoError())); |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 86 | EXPECT_CALL(*textureImpl, destructor()).Times(1).RetiresOnSaturation(); |
Jamie Madill | 60ec6ea | 2016-01-22 15:27:19 -0500 | [diff] [blame] | 87 | |
Olli Etuaho | 82c47ad | 2016-04-20 18:28:47 +0300 | [diff] [blame] | 88 | Texture *texture = new Texture(&mockFactory, 0, GL_TEXTURE_2D); |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 89 | texture->addRef(); |
Jamie Madill | 8897afa | 2017-02-06 17:17:23 -0500 | [diff] [blame] | 90 | texture->setStorage(nullptr, GL_TEXTURE_2D, 1, GL_RGBA8, Extents(1, 1, 0)); |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 91 | |
| 92 | VertexArray *vertexArray = new VertexArray(&mockFactory, 0, 1); |
| 93 | Framebuffer *framebuffer = new Framebuffer(caps, &mockFactory, 1); |
| 94 | framebuffer->setAttachment(GL_FRAMEBUFFER_DEFAULT, GL_BACK, ImageIndex::Make2D(0), texture); |
| 95 | |
| 96 | Program *program = new Program(&mockFactory, nullptr, 1); |
| 97 | |
| 98 | state.setVertexArrayBinding(vertexArray); |
| 99 | state.setDrawFramebufferBinding(framebuffer); |
| 100 | state.setProgram(program); |
| 101 | |
Geoff Lang | ce02f08 | 2017-02-06 16:46:21 -0500 | [diff] [blame^] | 102 | NiceMock<MockValidationContext> testContext(nullptr, nullptr, Version(3, 0), &state, caps, |
| 103 | textureCaps, extensions, limitations, false); |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 104 | |
| 105 | // Set the expectation for the validation error here. |
| 106 | Error expectedError(GL_INVALID_OPERATION, g_ExceedsMaxElementErrorMessage); |
Jamie Madill | 437fa65 | 2016-05-03 15:13:24 -0400 | [diff] [blame] | 107 | EXPECT_CALL(testContext, handleError(expectedError)).Times(1); |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 108 | |
| 109 | // Call once with maximum index, and once with an excessive index. |
| 110 | GLuint indexData[] = {0, 1, static_cast<GLuint>(caps.maxElementIndex - 1), |
| 111 | 3, 4, static_cast<GLuint>(caps.maxElementIndex)}; |
| 112 | IndexRange indexRange; |
| 113 | EXPECT_TRUE(ValidateDrawElements(&testContext, GL_TRIANGLES, 3, GL_UNSIGNED_INT, indexData, 1, |
| 114 | &indexRange)); |
| 115 | EXPECT_FALSE(ValidateDrawElements(&testContext, GL_TRIANGLES, 6, GL_UNSIGNED_INT, indexData, 2, |
| 116 | &indexRange)); |
| 117 | |
| 118 | texture->release(); |
Jamie Madill | daa8c27 | 2015-11-18 14:13:55 -0500 | [diff] [blame] | 119 | |
| 120 | state.setVertexArrayBinding(nullptr); |
| 121 | state.setDrawFramebufferBinding(nullptr); |
| 122 | state.setProgram(nullptr); |
| 123 | |
Jamie Madill | e79b1e1 | 2015-11-04 16:36:37 -0500 | [diff] [blame] | 124 | SafeDelete(vertexArray); |
| 125 | SafeDelete(framebuffer); |
| 126 | SafeDelete(program); |
| 127 | } |
| 128 | |
| 129 | } // anonymous namespace |