blob: 7a2302698952bde22c10cfe95881666816b1980b [file] [log] [blame]
Jamie Madille79b1e12015-11-04 16:36:37 -05001//
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 Madill9082b982016-04-27 15:21:51 -040013#include "libANGLE/ContextState.h"
Jamie Madill192745a2016-12-22 15:58:21 -050014#include "libANGLE/VaryingPacking.h"
Jamie Madille79b1e12015-11-04 16:36:37 -050015#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
21using namespace gl;
22using namespace rx;
23using testing::_;
Jamie Madill60ec6ea2016-01-22 15:27:19 -050024using testing::NiceMock;
Jamie Madille79b1e12015-11-04 16:36:37 -050025using testing::Return;
26
27namespace
28{
29
Jamie Madille79b1e12015-11-04 16:36:37 -050030class MockValidationContext : public ValidationContext
31{
32 public:
Geoff Lang4ddf5af2016-12-01 14:30:44 -050033 MockValidationContext(const ValidationContext *shareContext,
34 const Version &version,
Jamie Madilldfde6ab2016-06-09 07:07:18 -070035 State *state,
Jamie Madille79b1e12015-11-04 16:36:37 -050036 const Caps &caps,
37 const TextureCapsMap &textureCaps,
38 const Extensions &extensions,
Jamie Madill46e6c7a2016-01-18 14:42:30 -050039 const Limitations &limitations,
Geoff Lang4ddf5af2016-12-01 14:30:44 -050040 bool skipValidation)
41 : ValidationContext(shareContext,
42 version,
43 state,
44 caps,
45 textureCaps,
46 extensions,
47 limitations,
Geoff Lang4ddf5af2016-12-01 14:30:44 -050048 skipValidation)
49 {
50 }
Jamie Madille79b1e12015-11-04 16:36:37 -050051
Jamie Madill437fa652016-05-03 15:13:24 -040052 MOCK_METHOD1(handleError, void(const Error &));
Jamie Madille79b1e12015-11-04 16:36:37 -050053};
54
Jamie Madille79b1e12015-11-04 16:36:37 -050055// 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.
58TEST(ValidationESTest, DrawElementsWithMaxIndexGivesError)
59{
Jamie Madill60ec6ea2016-01-22 15:27:19 -050060 auto framebufferImpl = MakeFramebufferMock();
61 auto programImpl = MakeProgramMock();
62
Jamie Madille79b1e12015-11-04 16:36:37 -050063 // TODO(jmadill): Generalize some of this code so we can re-use it for other tests.
Jamie Madill7aea7e02016-05-10 10:39:45 -040064 NiceMock<MockGLFactory> mockFactory;
Jamie Madille79b1e12015-11-04 16:36:37 -050065 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 Langeb66a6e2016-10-31 13:06:12 -040079 state.initialize(caps, extensions, Version(3, 0), false, true);
Jamie Madille79b1e12015-11-04 16:36:37 -050080
Jamie Madill60ec6ea2016-01-22 15:27:19 -050081 NiceMock<MockTextureImpl> *textureImpl = new NiceMock<MockTextureImpl>();
Olli Etuaho82c47ad2016-04-20 18:28:47 +030082 EXPECT_CALL(mockFactory, createTexture(_)).WillOnce(Return(textureImpl));
Jamie Madill8897afa2017-02-06 17:17:23 -050083 EXPECT_CALL(*textureImpl, setStorage(_, _, _, _, _)).WillOnce(Return(NoError()));
Jamie Madille79b1e12015-11-04 16:36:37 -050084 EXPECT_CALL(*textureImpl, destructor()).Times(1).RetiresOnSaturation();
Jamie Madill60ec6ea2016-01-22 15:27:19 -050085
Olli Etuaho82c47ad2016-04-20 18:28:47 +030086 Texture *texture = new Texture(&mockFactory, 0, GL_TEXTURE_2D);
Jamie Madille79b1e12015-11-04 16:36:37 -050087 texture->addRef();
Jamie Madill8897afa2017-02-06 17:17:23 -050088 texture->setStorage(nullptr, GL_TEXTURE_2D, 1, GL_RGBA8, Extents(1, 1, 0));
Jamie Madille79b1e12015-11-04 16:36:37 -050089
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 Lang4ddf5af2016-12-01 14:30:44 -0500100 NiceMock<MockValidationContext> testContext(nullptr, Version(3, 0), &state, caps, textureCaps,
Geoff Lang3bf8e3a2016-12-01 17:28:52 -0500101 extensions, limitations, false);
Jamie Madille79b1e12015-11-04 16:36:37 -0500102
103 // Set the expectation for the validation error here.
104 Error expectedError(GL_INVALID_OPERATION, g_ExceedsMaxElementErrorMessage);
Jamie Madill437fa652016-05-03 15:13:24 -0400105 EXPECT_CALL(testContext, handleError(expectedError)).Times(1);
Jamie Madille79b1e12015-11-04 16:36:37 -0500106
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 Madilldaa8c272015-11-18 14:13:55 -0500117
118 state.setVertexArrayBinding(nullptr);
119 state.setDrawFramebufferBinding(nullptr);
120 state.setProgram(nullptr);
121
Jamie Madille79b1e12015-11-04 16:36:37 -0500122 SafeDelete(vertexArray);
123 SafeDelete(framebuffer);
124 SafeDelete(program);
125}
126
127} // anonymous namespace