blob: c018924e139fc2e026e81e72d7b691179c2aade8 [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 Madille79b1e12015-11-04 16:36:37 -050014#include "libANGLE/renderer/FramebufferImpl_mock.h"
15#include "libANGLE/renderer/ProgramImpl_mock.h"
16#include "libANGLE/renderer/TextureImpl_mock.h"
17#include "libANGLE/validationES.h"
18#include "tests/angle_unittests_utils.h"
19
20using namespace gl;
21using namespace rx;
22using testing::_;
Jamie Madill60ec6ea2016-01-22 15:27:19 -050023using testing::NiceMock;
Jamie Madille79b1e12015-11-04 16:36:37 -050024using testing::Return;
25
26namespace
27{
28
Jamie Madille79b1e12015-11-04 16:36:37 -050029class MockValidationContext : public ValidationContext
30{
31 public:
Martin Radev1be913c2016-07-11 17:59:16 +030032 MockValidationContext(GLint majorClientVersion,
33 GLint minorClientVersion,
Jamie Madilldfde6ab2016-06-09 07:07:18 -070034 State *state,
Jamie Madille79b1e12015-11-04 16:36:37 -050035 const Caps &caps,
36 const TextureCapsMap &textureCaps,
37 const Extensions &extensions,
38 const ResourceManager *resourceManager,
Jamie Madill46e6c7a2016-01-18 14:42:30 -050039 const Limitations &limitations,
40 bool skipValidation);
Jamie Madille79b1e12015-11-04 16:36:37 -050041
Jamie Madill437fa652016-05-03 15:13:24 -040042 MOCK_METHOD1(handleError, void(const Error &));
Jamie Madille79b1e12015-11-04 16:36:37 -050043};
44
Martin Radev1be913c2016-07-11 17:59:16 +030045MockValidationContext::MockValidationContext(GLint majorClientVersion,
46 GLint minorClientVersion,
Jamie Madilldfde6ab2016-06-09 07:07:18 -070047 State *state,
Jamie Madille79b1e12015-11-04 16:36:37 -050048 const Caps &caps,
49 const TextureCapsMap &textureCaps,
50 const Extensions &extensions,
51 const ResourceManager *resourceManager,
Jamie Madill46e6c7a2016-01-18 14:42:30 -050052 const Limitations &limitations,
53 bool skipValidation)
Martin Radev1be913c2016-07-11 17:59:16 +030054 : ValidationContext(majorClientVersion,
55 minorClientVersion,
Jamie Madille79b1e12015-11-04 16:36:37 -050056 state,
57 caps,
58 textureCaps,
59 extensions,
60 resourceManager,
Jamie Madill46e6c7a2016-01-18 14:42:30 -050061 limitations,
62 skipValidation)
Jamie Madille79b1e12015-11-04 16:36:37 -050063{
64}
65
66// Test that ANGLE generates an INVALID_OPERATION when validating index data that uses a value
67// larger than MAX_ELEMENT_INDEX. Not specified in the GLES 3 spec, it's undefined behaviour,
68// but we want a test to ensure we maintain this behaviour.
69TEST(ValidationESTest, DrawElementsWithMaxIndexGivesError)
70{
Jamie Madill60ec6ea2016-01-22 15:27:19 -050071 auto framebufferImpl = MakeFramebufferMock();
72 auto programImpl = MakeProgramMock();
73
Jamie Madille79b1e12015-11-04 16:36:37 -050074 // TODO(jmadill): Generalize some of this code so we can re-use it for other tests.
Jamie Madill7aea7e02016-05-10 10:39:45 -040075 NiceMock<MockGLFactory> mockFactory;
Jamie Madille79b1e12015-11-04 16:36:37 -050076 EXPECT_CALL(mockFactory, createFramebuffer(_)).WillOnce(Return(framebufferImpl));
77 EXPECT_CALL(mockFactory, createProgram(_)).WillOnce(Return(programImpl));
78 EXPECT_CALL(mockFactory, createVertexArray(_)).WillOnce(Return(nullptr));
79
80 State state;
81 Caps caps;
82 TextureCapsMap textureCaps;
83 Extensions extensions;
84 Limitations limitations;
85
86 // Set some basic caps.
87 caps.maxElementIndex = 100;
88 caps.maxDrawBuffers = 1;
89 caps.maxColorAttachments = 1;
Geoff Langf41a7152016-09-19 15:11:17 -040090 state.initialize(caps, extensions, 3, false, true);
Jamie Madille79b1e12015-11-04 16:36:37 -050091
Jamie Madill60ec6ea2016-01-22 15:27:19 -050092 NiceMock<MockTextureImpl> *textureImpl = new NiceMock<MockTextureImpl>();
Olli Etuaho82c47ad2016-04-20 18:28:47 +030093 EXPECT_CALL(mockFactory, createTexture(_)).WillOnce(Return(textureImpl));
Jamie Madille79b1e12015-11-04 16:36:37 -050094 EXPECT_CALL(*textureImpl, setStorage(_, _, _, _)).WillOnce(Return(Error(GL_NO_ERROR)));
95 EXPECT_CALL(*textureImpl, destructor()).Times(1).RetiresOnSaturation();
Jamie Madill60ec6ea2016-01-22 15:27:19 -050096
Olli Etuaho82c47ad2016-04-20 18:28:47 +030097 Texture *texture = new Texture(&mockFactory, 0, GL_TEXTURE_2D);
Jamie Madille79b1e12015-11-04 16:36:37 -050098 texture->addRef();
99 texture->setStorage(GL_TEXTURE_2D, 1, GL_RGBA8, Extents(1, 1, 0));
100
101 VertexArray *vertexArray = new VertexArray(&mockFactory, 0, 1);
102 Framebuffer *framebuffer = new Framebuffer(caps, &mockFactory, 1);
103 framebuffer->setAttachment(GL_FRAMEBUFFER_DEFAULT, GL_BACK, ImageIndex::Make2D(0), texture);
104
105 Program *program = new Program(&mockFactory, nullptr, 1);
106
107 state.setVertexArrayBinding(vertexArray);
108 state.setDrawFramebufferBinding(framebuffer);
109 state.setProgram(program);
110
Martin Radev1be913c2016-07-11 17:59:16 +0300111 NiceMock<MockValidationContext> testContext(3, 0, &state, caps, textureCaps, extensions,
112 nullptr, limitations, false);
Jamie Madille79b1e12015-11-04 16:36:37 -0500113
114 // Set the expectation for the validation error here.
115 Error expectedError(GL_INVALID_OPERATION, g_ExceedsMaxElementErrorMessage);
Jamie Madill437fa652016-05-03 15:13:24 -0400116 EXPECT_CALL(testContext, handleError(expectedError)).Times(1);
Jamie Madille79b1e12015-11-04 16:36:37 -0500117
118 // Call once with maximum index, and once with an excessive index.
119 GLuint indexData[] = {0, 1, static_cast<GLuint>(caps.maxElementIndex - 1),
120 3, 4, static_cast<GLuint>(caps.maxElementIndex)};
121 IndexRange indexRange;
122 EXPECT_TRUE(ValidateDrawElements(&testContext, GL_TRIANGLES, 3, GL_UNSIGNED_INT, indexData, 1,
123 &indexRange));
124 EXPECT_FALSE(ValidateDrawElements(&testContext, GL_TRIANGLES, 6, GL_UNSIGNED_INT, indexData, 2,
125 &indexRange));
126
127 texture->release();
Jamie Madilldaa8c272015-11-18 14:13:55 -0500128
129 state.setVertexArrayBinding(nullptr);
130 state.setDrawFramebufferBinding(nullptr);
131 state.setProgram(nullptr);
132
Jamie Madille79b1e12015-11-04 16:36:37 -0500133 SafeDelete(vertexArray);
134 SafeDelete(framebuffer);
135 SafeDelete(program);
136}
137
138} // anonymous namespace