blob: d344c17a0aec689abafef5800e3e7062f7b43349 [file] [log] [blame]
Corentin Wallezd71620a2015-07-24 08:06:31 -07001//
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
7// IndexBufferOffsetTest.cpp: Test glDrawElements with an offset and an index buffer
8
Corentin Wallezd71620a2015-07-24 08:06:31 -07009#include "system_utils.h"
Jamie Madill231c7f52017-04-26 13:45:37 -040010#include "test_utils/ANGLETest.h"
Corentin Wallezd71620a2015-07-24 08:06:31 -070011
12using namespace angle;
13
14class IndexBufferOffsetTest : public ANGLETest
15{
16 protected:
17 IndexBufferOffsetTest()
18 {
19 setWindowWidth(128);
20 setWindowHeight(128);
21 setConfigRedBits(8);
22 setConfigGreenBits(8);
23 setConfigBlueBits(8);
24 setConfigAlphaBits(8);
25 }
26
27 void SetUp() override
28 {
29 ANGLETest::SetUp();
30
31 const std::string vertexShaderSource =
Olli Etuahoa20af6d2017-09-18 13:32:29 +030032 R"(precision highp float;
33 attribute vec2 position;
Corentin Wallezd71620a2015-07-24 08:06:31 -070034
Olli Etuahoa20af6d2017-09-18 13:32:29 +030035 void main()
36 {
37 gl_Position = vec4(position, 0.0, 1.0);
38 })";
Corentin Wallezd71620a2015-07-24 08:06:31 -070039
40 const std::string fragmentShaderSource =
Olli Etuahoa20af6d2017-09-18 13:32:29 +030041 R"(precision highp float;
42 uniform vec4 color;
Corentin Wallezd71620a2015-07-24 08:06:31 -070043
Olli Etuahoa20af6d2017-09-18 13:32:29 +030044 void main()
45 {
46 gl_FragColor = color;
47 })";
Corentin Wallezd71620a2015-07-24 08:06:31 -070048
49 mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource);
50 ASSERT_NE(0u, mProgram);
51
52 mColorUniformLocation = glGetUniformLocation(mProgram, "color");
53 mPositionAttributeLocation = glGetAttribLocation(mProgram, "position");
54
Jamie Madill231c7f52017-04-26 13:45:37 -040055 const GLfloat vertices[] = {-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f};
Corentin Wallezd71620a2015-07-24 08:06:31 -070056 glGenBuffers(1, &mVertexBuffer);
57 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
58 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
59
60 glGenBuffers(1, &mIndexBuffer);
61 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
62 }
63
64 void TearDown() override
65 {
66 glDeleteBuffers(1, &mVertexBuffer);
67 glDeleteBuffers(1, &mIndexBuffer);
68 glDeleteProgram(mProgram);
69 ANGLETest::TearDown();
70 }
71
72 void runTest(GLenum type, int typeWidth, void *indexData)
73 {
74 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
75 glClear(GL_COLOR_BUFFER_BIT);
76
77 GLuint nullIndexData[] = {0, 0, 0, 0, 0, 0};
78
79 size_t indexDataWidth = 6 * typeWidth;
80
81 glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * indexDataWidth, nullptr, GL_DYNAMIC_DRAW);
82 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indexDataWidth, nullIndexData);
83 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, indexDataWidth, indexDataWidth, indexData);
84 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 2 * indexDataWidth, indexDataWidth, nullIndexData);
85
86 glUseProgram(mProgram);
87
88 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
89 glVertexAttribPointer(mPositionAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
90 glEnableVertexAttribArray(mPositionAttributeLocation);
91
92 glUniform4f(mColorUniformLocation, 1.0f, 0.0f, 0.0f, 1.0f);
93
94 for (int i = 0; i < 16; i++)
95 {
Jamie Madill876429b2017-04-20 15:46:24 -040096 glDrawElements(GL_TRIANGLES, 6, type, reinterpret_cast<void *>(indexDataWidth));
Corentin Wallezd71620a2015-07-24 08:06:31 -070097 EXPECT_PIXEL_EQ(64, 64, 255, 0, 0, 255);
98 }
99
100 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, indexDataWidth, indexDataWidth, nullIndexData);
101 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 2 * indexDataWidth, indexDataWidth, indexData);
102
103 glUniform4f(mColorUniformLocation, 0.0f, 1.0f, 0.0f, 1.0f);
Jamie Madill876429b2017-04-20 15:46:24 -0400104 glDrawElements(GL_TRIANGLES, 6, type, reinterpret_cast<void *>(indexDataWidth * 2));
Corentin Wallezd71620a2015-07-24 08:06:31 -0700105 EXPECT_PIXEL_EQ(64, 64, 0, 255, 0, 255);
106
107 EXPECT_GL_NO_ERROR();
108 swapBuffers();
109 }
110
111 GLuint mProgram;
112 GLint mColorUniformLocation;
113 GLint mPositionAttributeLocation;
114 GLuint mVertexBuffer;
115 GLuint mIndexBuffer;
116};
117
118// Test using an offset for an UInt8 index buffer
119TEST_P(IndexBufferOffsetTest, UInt8Index)
120{
121 GLubyte indexData[] = {0, 1, 2, 1, 2, 3};
122 runTest(GL_UNSIGNED_BYTE, 1, indexData);
123}
124
125// Test using an offset for an UInt16 index buffer
126TEST_P(IndexBufferOffsetTest, UInt16Index)
127{
128 GLushort indexData[] = {0, 1, 2, 1, 2, 3};
129 runTest(GL_UNSIGNED_SHORT, 2, indexData);
130}
131
132// Test using an offset for an UInt32 index buffer
133TEST_P(IndexBufferOffsetTest, UInt32Index)
134{
Martin Radev1be913c2016-07-11 17:59:16 +0300135 if (getClientMajorVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint"))
Corentin Wallezd71620a2015-07-24 08:06:31 -0700136 {
Jamie Madill231c7f52017-04-26 13:45:37 -0400137 std::cout << "Test skipped because ES3 or GL_OES_element_index_uint is not available."
138 << std::endl;
Corentin Wallezd71620a2015-07-24 08:06:31 -0700139 return;
140 }
141
142 GLuint indexData[] = {0, 1, 2, 1, 2, 3};
143 runTest(GL_UNSIGNED_INT, 4, indexData);
144}
145
146ANGLE_INSTANTIATE_TEST(IndexBufferOffsetTest,
147 ES2_D3D9(),
148 ES2_D3D11(),
149 ES3_D3D11(),
150 ES2_OPENGL(),
Geoff Lange0cc2a42016-01-20 10:58:17 -0500151 ES3_OPENGL(),
152 ES2_OPENGLES(),
153 ES3_OPENGLES());