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