blob: 329848a0ea82723fafa773a141be84a26988329f [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 =
32 SHADER_SOURCE(precision highp float; attribute vec2 position;
33
Jamie Madill231c7f52017-04-26 13:45:37 -040034 void main() { gl_Position = vec4(position, 0.0, 1.0); });
Corentin Wallezd71620a2015-07-24 08:06:31 -070035
36 const std::string fragmentShaderSource =
37 SHADER_SOURCE(precision highp float; uniform vec4 color;
38
Jamie Madill231c7f52017-04-26 13:45:37 -040039 void main() { gl_FragColor = color; });
Corentin Wallezd71620a2015-07-24 08:06:31 -070040
41 mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource);
42 ASSERT_NE(0u, mProgram);
43
44 mColorUniformLocation = glGetUniformLocation(mProgram, "color");
45 mPositionAttributeLocation = glGetAttribLocation(mProgram, "position");
46
Jamie Madill231c7f52017-04-26 13:45:37 -040047 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 -070048 glGenBuffers(1, &mVertexBuffer);
49 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
50 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
51
52 glGenBuffers(1, &mIndexBuffer);
53 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
54 }
55
56 void TearDown() override
57 {
58 glDeleteBuffers(1, &mVertexBuffer);
59 glDeleteBuffers(1, &mIndexBuffer);
60 glDeleteProgram(mProgram);
61 ANGLETest::TearDown();
62 }
63
64 void runTest(GLenum type, int typeWidth, void *indexData)
65 {
66 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
67 glClear(GL_COLOR_BUFFER_BIT);
68
69 GLuint nullIndexData[] = {0, 0, 0, 0, 0, 0};
70
71 size_t indexDataWidth = 6 * typeWidth;
72
73 glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * indexDataWidth, nullptr, GL_DYNAMIC_DRAW);
74 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indexDataWidth, nullIndexData);
75 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, indexDataWidth, indexDataWidth, indexData);
76 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 2 * indexDataWidth, indexDataWidth, nullIndexData);
77
78 glUseProgram(mProgram);
79
80 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
81 glVertexAttribPointer(mPositionAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
82 glEnableVertexAttribArray(mPositionAttributeLocation);
83
84 glUniform4f(mColorUniformLocation, 1.0f, 0.0f, 0.0f, 1.0f);
85
86 for (int i = 0; i < 16; i++)
87 {
Jamie Madill876429b2017-04-20 15:46:24 -040088 glDrawElements(GL_TRIANGLES, 6, type, reinterpret_cast<void *>(indexDataWidth));
Corentin Wallezd71620a2015-07-24 08:06:31 -070089 EXPECT_PIXEL_EQ(64, 64, 255, 0, 0, 255);
90 }
91
92 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, indexDataWidth, indexDataWidth, nullIndexData);
93 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 2 * indexDataWidth, indexDataWidth, indexData);
94
95 glUniform4f(mColorUniformLocation, 0.0f, 1.0f, 0.0f, 1.0f);
Jamie Madill876429b2017-04-20 15:46:24 -040096 glDrawElements(GL_TRIANGLES, 6, type, reinterpret_cast<void *>(indexDataWidth * 2));
Corentin Wallezd71620a2015-07-24 08:06:31 -070097 EXPECT_PIXEL_EQ(64, 64, 0, 255, 0, 255);
98
99 EXPECT_GL_NO_ERROR();
100 swapBuffers();
101 }
102
103 GLuint mProgram;
104 GLint mColorUniformLocation;
105 GLint mPositionAttributeLocation;
106 GLuint mVertexBuffer;
107 GLuint mIndexBuffer;
108};
109
110// Test using an offset for an UInt8 index buffer
111TEST_P(IndexBufferOffsetTest, UInt8Index)
112{
113 GLubyte indexData[] = {0, 1, 2, 1, 2, 3};
114 runTest(GL_UNSIGNED_BYTE, 1, indexData);
115}
116
117// Test using an offset for an UInt16 index buffer
118TEST_P(IndexBufferOffsetTest, UInt16Index)
119{
120 GLushort indexData[] = {0, 1, 2, 1, 2, 3};
121 runTest(GL_UNSIGNED_SHORT, 2, indexData);
122}
123
124// Test using an offset for an UInt32 index buffer
125TEST_P(IndexBufferOffsetTest, UInt32Index)
126{
Martin Radev1be913c2016-07-11 17:59:16 +0300127 if (getClientMajorVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint"))
Corentin Wallezd71620a2015-07-24 08:06:31 -0700128 {
Jamie Madill231c7f52017-04-26 13:45:37 -0400129 std::cout << "Test skipped because ES3 or GL_OES_element_index_uint is not available."
130 << std::endl;
Corentin Wallezd71620a2015-07-24 08:06:31 -0700131 return;
132 }
133
134 GLuint indexData[] = {0, 1, 2, 1, 2, 3};
135 runTest(GL_UNSIGNED_INT, 4, indexData);
136}
137
138ANGLE_INSTANTIATE_TEST(IndexBufferOffsetTest,
139 ES2_D3D9(),
140 ES2_D3D11(),
141 ES3_D3D11(),
142 ES2_OPENGL(),
Geoff Lange0cc2a42016-01-20 10:58:17 -0500143 ES3_OPENGL(),
144 ES2_OPENGLES(),
145 ES3_OPENGLES());