blob: 3cb5311cc3b8324098eb12df5b94c245e024414d [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:
Geoff Langeb66a6e2016-10-31 13:06:12 -040032 MockValidationContext(const Version &version,
Jamie Madilldfde6ab2016-06-09 07:07:18 -070033 State *state,
Jamie Madille79b1e12015-11-04 16:36:37 -050034 const Caps &caps,
35 const TextureCapsMap &textureCaps,
36 const Extensions &extensions,
37 const ResourceManager *resourceManager,
Jamie Madill46e6c7a2016-01-18 14:42:30 -050038 const Limitations &limitations,
39 bool skipValidation);
Jamie Madille79b1e12015-11-04 16:36:37 -050040
Jamie Madill437fa652016-05-03 15:13:24 -040041 MOCK_METHOD1(handleError, void(const Error &));
Jamie Madille79b1e12015-11-04 16:36:37 -050042};
43
Geoff Langeb66a6e2016-10-31 13:06:12 -040044MockValidationContext::MockValidationContext(const Version &version,
Jamie Madilldfde6ab2016-06-09 07:07:18 -070045 State *state,
Jamie Madille79b1e12015-11-04 16:36:37 -050046 const Caps &caps,
47 const TextureCapsMap &textureCaps,
48 const Extensions &extensions,
49 const ResourceManager *resourceManager,
Jamie Madill46e6c7a2016-01-18 14:42:30 -050050 const Limitations &limitations,
51 bool skipValidation)
Geoff Langeb66a6e2016-10-31 13:06:12 -040052 : ValidationContext(version,
Jamie Madille79b1e12015-11-04 16:36:37 -050053 state,
54 caps,
55 textureCaps,
56 extensions,
57 resourceManager,
Jamie Madill46e6c7a2016-01-18 14:42:30 -050058 limitations,
59 skipValidation)
Jamie Madille79b1e12015-11-04 16:36:37 -050060{
61}
62
63// Test that ANGLE generates an INVALID_OPERATION when validating index data that uses a value
64// larger than MAX_ELEMENT_INDEX. Not specified in the GLES 3 spec, it's undefined behaviour,
65// but we want a test to ensure we maintain this behaviour.
66TEST(ValidationESTest, DrawElementsWithMaxIndexGivesError)
67{
Jamie Madill60ec6ea2016-01-22 15:27:19 -050068 auto framebufferImpl = MakeFramebufferMock();
69 auto programImpl = MakeProgramMock();
70
Jamie Madille79b1e12015-11-04 16:36:37 -050071 // TODO(jmadill): Generalize some of this code so we can re-use it for other tests.
Jamie Madill7aea7e02016-05-10 10:39:45 -040072 NiceMock<MockGLFactory> mockFactory;
Jamie Madille79b1e12015-11-04 16:36:37 -050073 EXPECT_CALL(mockFactory, createFramebuffer(_)).WillOnce(Return(framebufferImpl));
74 EXPECT_CALL(mockFactory, createProgram(_)).WillOnce(Return(programImpl));
75 EXPECT_CALL(mockFactory, createVertexArray(_)).WillOnce(Return(nullptr));
76
77 State state;
78 Caps caps;
79 TextureCapsMap textureCaps;
80 Extensions extensions;
81 Limitations limitations;
82
83 // Set some basic caps.
84 caps.maxElementIndex = 100;
85 caps.maxDrawBuffers = 1;
86 caps.maxColorAttachments = 1;
Geoff Langeb66a6e2016-10-31 13:06:12 -040087 state.initialize(caps, extensions, Version(3, 0), false, true);
Jamie Madille79b1e12015-11-04 16:36:37 -050088
Jamie Madill60ec6ea2016-01-22 15:27:19 -050089 NiceMock<MockTextureImpl> *textureImpl = new NiceMock<MockTextureImpl>();
Olli Etuaho82c47ad2016-04-20 18:28:47 +030090 EXPECT_CALL(mockFactory, createTexture(_)).WillOnce(Return(textureImpl));
Jamie Madille79b1e12015-11-04 16:36:37 -050091 EXPECT_CALL(*textureImpl, setStorage(_, _, _, _)).WillOnce(Return(Error(GL_NO_ERROR)));
92 EXPECT_CALL(*textureImpl, destructor()).Times(1).RetiresOnSaturation();
Jamie Madill60ec6ea2016-01-22 15:27:19 -050093
Olli Etuaho82c47ad2016-04-20 18:28:47 +030094 Texture *texture = new Texture(&mockFactory, 0, GL_TEXTURE_2D);
Jamie Madille79b1e12015-11-04 16:36:37 -050095 texture->addRef();
96 texture->setStorage(GL_TEXTURE_2D, 1, GL_RGBA8, Extents(1, 1, 0));
97
98 VertexArray *vertexArray = new VertexArray(&mockFactory, 0, 1);
99 Framebuffer *framebuffer = new Framebuffer(caps, &mockFactory, 1);
100 framebuffer->setAttachment(GL_FRAMEBUFFER_DEFAULT, GL_BACK, ImageIndex::Make2D(0), texture);
101
102 Program *program = new Program(&mockFactory, nullptr, 1);
103
104 state.setVertexArrayBinding(vertexArray);
105 state.setDrawFramebufferBinding(framebuffer);
106 state.setProgram(program);
107
Geoff Langeb66a6e2016-10-31 13:06:12 -0400108 NiceMock<MockValidationContext> testContext(Version(3, 0), &state, caps, textureCaps,
109 extensions, nullptr, limitations, false);
Jamie Madille79b1e12015-11-04 16:36:37 -0500110
111 // Set the expectation for the validation error here.
112 Error expectedError(GL_INVALID_OPERATION, g_ExceedsMaxElementErrorMessage);
Jamie Madill437fa652016-05-03 15:13:24 -0400113 EXPECT_CALL(testContext, handleError(expectedError)).Times(1);
Jamie Madille79b1e12015-11-04 16:36:37 -0500114
115 // Call once with maximum index, and once with an excessive index.
116 GLuint indexData[] = {0, 1, static_cast<GLuint>(caps.maxElementIndex - 1),
117 3, 4, static_cast<GLuint>(caps.maxElementIndex)};
118 IndexRange indexRange;
119 EXPECT_TRUE(ValidateDrawElements(&testContext, GL_TRIANGLES, 3, GL_UNSIGNED_INT, indexData, 1,
120 &indexRange));
121 EXPECT_FALSE(ValidateDrawElements(&testContext, GL_TRIANGLES, 6, GL_UNSIGNED_INT, indexData, 2,
122 &indexRange));
123
124 texture->release();
Jamie Madilldaa8c272015-11-18 14:13:55 -0500125
126 state.setVertexArrayBinding(nullptr);
127 state.setDrawFramebufferBinding(nullptr);
128 state.setProgram(nullptr);
129
Jamie Madille79b1e12015-11-04 16:36:37 -0500130 SafeDelete(vertexArray);
131 SafeDelete(framebuffer);
132 SafeDelete(program);
133}
134
135} // anonymous namespace