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