blob: ca27a36e13f44cdbc84a91aa49b4c5a74a548c0d [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,
Geoff Langce02f082017-02-06 16:46:21 -050034 TextureManager *shareTextures,
Geoff Lang4ddf5af2016-12-01 14:30:44 -050035 const Version &version,
Jamie Madilldfde6ab2016-06-09 07:07:18 -070036 State *state,
Jamie Madille79b1e12015-11-04 16:36:37 -050037 const Caps &caps,
38 const TextureCapsMap &textureCaps,
39 const Extensions &extensions,
Jamie Madill46e6c7a2016-01-18 14:42:30 -050040 const Limitations &limitations,
Geoff Lang4ddf5af2016-12-01 14:30:44 -050041 bool skipValidation)
42 : ValidationContext(shareContext,
Geoff Langce02f082017-02-06 16:46:21 -050043 shareTextures,
Geoff Lang4ddf5af2016-12-01 14:30:44 -050044 version,
45 state,
46 caps,
47 textureCaps,
48 extensions,
49 limitations,
Geoff Lang4ddf5af2016-12-01 14:30:44 -050050 skipValidation)
51 {
52 }
Jamie Madille79b1e12015-11-04 16:36:37 -050053
Jamie Madill437fa652016-05-03 15:13:24 -040054 MOCK_METHOD1(handleError, void(const Error &));
Jamie Madille79b1e12015-11-04 16:36:37 -050055};
56
Jamie Madille79b1e12015-11-04 16:36:37 -050057// Test that ANGLE generates an INVALID_OPERATION when validating index data that uses a value
58// larger than MAX_ELEMENT_INDEX. Not specified in the GLES 3 spec, it's undefined behaviour,
59// but we want a test to ensure we maintain this behaviour.
Jamie Madillfa416b12017-04-06 11:34:17 -040060// TODO(jmadill): Re-enable when framebuffer sync state doesn't happen in validation.
Jamie Madill4928b7c2017-06-20 12:57:39 -040061// Also broken because of change of api of the state initialize method.
Jamie Madillfa416b12017-04-06 11:34:17 -040062TEST(ValidationESTest, DISABLED_DrawElementsWithMaxIndexGivesError)
Jamie Madille79b1e12015-11-04 16:36:37 -050063{
Jamie Madill60ec6ea2016-01-22 15:27:19 -050064 auto framebufferImpl = MakeFramebufferMock();
65 auto programImpl = MakeProgramMock();
66
Jamie Madille79b1e12015-11-04 16:36:37 -050067 // TODO(jmadill): Generalize some of this code so we can re-use it for other tests.
Jamie Madill7aea7e02016-05-10 10:39:45 -040068 NiceMock<MockGLFactory> mockFactory;
Jamie Madille79b1e12015-11-04 16:36:37 -050069 EXPECT_CALL(mockFactory, createFramebuffer(_)).WillOnce(Return(framebufferImpl));
70 EXPECT_CALL(mockFactory, createProgram(_)).WillOnce(Return(programImpl));
71 EXPECT_CALL(mockFactory, createVertexArray(_)).WillOnce(Return(nullptr));
72
73 State state;
74 Caps caps;
75 TextureCapsMap textureCaps;
76 Extensions extensions;
77 Limitations limitations;
78
79 // Set some basic caps.
80 caps.maxElementIndex = 100;
81 caps.maxDrawBuffers = 1;
82 caps.maxColorAttachments = 1;
Jamie Madill4928b7c2017-06-20 12:57:39 -040083 state.initialize(nullptr, false, true, true, false);
Jamie Madille79b1e12015-11-04 16:36:37 -050084
Jamie Madill60ec6ea2016-01-22 15:27:19 -050085 NiceMock<MockTextureImpl> *textureImpl = new NiceMock<MockTextureImpl>();
Olli Etuaho82c47ad2016-04-20 18:28:47 +030086 EXPECT_CALL(mockFactory, createTexture(_)).WillOnce(Return(textureImpl));
Yuly Novikovc4d18aa2017-03-09 18:45:02 -050087 EXPECT_CALL(*textureImpl, setStorage(_, _, _, _, _)).WillOnce(Return(gl::NoError()));
Jamie Madille79b1e12015-11-04 16:36:37 -050088 EXPECT_CALL(*textureImpl, destructor()).Times(1).RetiresOnSaturation();
Jamie Madill60ec6ea2016-01-22 15:27:19 -050089
Olli Etuaho82c47ad2016-04-20 18:28:47 +030090 Texture *texture = new Texture(&mockFactory, 0, GL_TEXTURE_2D);
Jamie Madille79b1e12015-11-04 16:36:37 -050091 texture->addRef();
Jamie Madill8897afa2017-02-06 17:17:23 -050092 texture->setStorage(nullptr, GL_TEXTURE_2D, 1, GL_RGBA8, Extents(1, 1, 0));
Jamie Madille79b1e12015-11-04 16:36:37 -050093
Jiawei-Shao2597fb62016-12-09 16:38:02 +080094 VertexArray *vertexArray = new VertexArray(&mockFactory, 0, 1, 1);
Jamie Madille79b1e12015-11-04 16:36:37 -050095 Framebuffer *framebuffer = new Framebuffer(caps, &mockFactory, 1);
Jamie Madilla02315b2017-02-23 14:14:47 -050096 framebuffer->setAttachment(nullptr, GL_FRAMEBUFFER_DEFAULT, GL_BACK, ImageIndex::Make2D(0),
97 texture);
Jamie Madille79b1e12015-11-04 16:36:37 -050098
99 Program *program = new Program(&mockFactory, nullptr, 1);
100
101 state.setVertexArrayBinding(vertexArray);
102 state.setDrawFramebufferBinding(framebuffer);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500103 state.setProgram(nullptr, program);
Jamie Madille79b1e12015-11-04 16:36:37 -0500104
Geoff Langce02f082017-02-06 16:46:21 -0500105 NiceMock<MockValidationContext> testContext(nullptr, nullptr, Version(3, 0), &state, caps,
106 textureCaps, extensions, limitations, false);
Jamie Madille79b1e12015-11-04 16:36:37 -0500107
108 // Set the expectation for the validation error here.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500109 Error expectedError(gl::InvalidOperation() << g_ExceedsMaxElementErrorMessage);
Jamie Madill437fa652016-05-03 15:13:24 -0400110 EXPECT_CALL(testContext, handleError(expectedError)).Times(1);
Jamie Madille79b1e12015-11-04 16:36:37 -0500111
112 // Call once with maximum index, and once with an excessive index.
113 GLuint indexData[] = {0, 1, static_cast<GLuint>(caps.maxElementIndex - 1),
114 3, 4, static_cast<GLuint>(caps.maxElementIndex)};
Jamie Madill9c9b40a2017-04-26 16:31:57 -0400115 EXPECT_TRUE(
116 ValidateDrawElementsCommon(&testContext, GL_TRIANGLES, 3, GL_UNSIGNED_INT, indexData, 1));
117 EXPECT_FALSE(
118 ValidateDrawElementsCommon(&testContext, GL_TRIANGLES, 6, GL_UNSIGNED_INT, indexData, 2));
Jamie Madille79b1e12015-11-04 16:36:37 -0500119
Jamie Madill4928b7c2017-06-20 12:57:39 -0400120 texture->release(nullptr);
Jamie Madilldaa8c272015-11-18 14:13:55 -0500121
122 state.setVertexArrayBinding(nullptr);
123 state.setDrawFramebufferBinding(nullptr);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500124 state.setProgram(nullptr, nullptr);
Jamie Madilldaa8c272015-11-18 14:13:55 -0500125
Jamie Madill4928b7c2017-06-20 12:57:39 -0400126 vertexArray->onDestroy(nullptr);
127 framebuffer->onDestroy(nullptr);
128 program->onDestroy(nullptr);
Jamie Madille79b1e12015-11-04 16:36:37 -0500129}
130
131} // anonymous namespace