blob: 6a854539844a5c70ee40ff167e9e84abeb07fcf9 [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
9#include "test_utils/ANGLETest.h"
10#include "system_utils.h"
11
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
34 void main()
35 {
36 gl_Position = vec4(position, 0.0, 1.0);
37 });
38
39 const std::string fragmentShaderSource =
40 SHADER_SOURCE(precision highp float; uniform vec4 color;
41
42 void main()
43 {
44 gl_FragColor = color;
45 });
46
47 mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource);
48 ASSERT_NE(0u, mProgram);
49
50 mColorUniformLocation = glGetUniformLocation(mProgram, "color");
51 mPositionAttributeLocation = glGetAttribLocation(mProgram, "position");
52
53 const GLfloat vertices[] =
54 {
55 -1.0f, -1.0f,
56 -1.0f, 1.0f,
57 1.0f, -1.0f,
58 1.0f, 1.0f
59 };
60 glGenBuffers(1, &mVertexBuffer);
61 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
62 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
63
64 glGenBuffers(1, &mIndexBuffer);
65 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
66 }
67
68 void TearDown() override
69 {
70 glDeleteBuffers(1, &mVertexBuffer);
71 glDeleteBuffers(1, &mIndexBuffer);
72 glDeleteProgram(mProgram);
73 ANGLETest::TearDown();
74 }
75
76 void runTest(GLenum type, int typeWidth, void *indexData)
77 {
78 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
79 glClear(GL_COLOR_BUFFER_BIT);
80
81 GLuint nullIndexData[] = {0, 0, 0, 0, 0, 0};
82
83 size_t indexDataWidth = 6 * typeWidth;
84
85 glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * indexDataWidth, nullptr, GL_DYNAMIC_DRAW);
86 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indexDataWidth, nullIndexData);
87 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, indexDataWidth, indexDataWidth, indexData);
88 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 2 * indexDataWidth, indexDataWidth, nullIndexData);
89
90 glUseProgram(mProgram);
91
92 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
93 glVertexAttribPointer(mPositionAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
94 glEnableVertexAttribArray(mPositionAttributeLocation);
95
96 glUniform4f(mColorUniformLocation, 1.0f, 0.0f, 0.0f, 1.0f);
97
98 for (int i = 0; i < 16; i++)
99 {
Jamie Madill876429b2017-04-20 15:46:24 -0400100 glDrawElements(GL_TRIANGLES, 6, type, reinterpret_cast<void *>(indexDataWidth));
Corentin Wallezd71620a2015-07-24 08:06:31 -0700101 EXPECT_PIXEL_EQ(64, 64, 255, 0, 0, 255);
102 }
103
104 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, indexDataWidth, indexDataWidth, nullIndexData);
105 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 2 * indexDataWidth, indexDataWidth, indexData);
106
107 glUniform4f(mColorUniformLocation, 0.0f, 1.0f, 0.0f, 1.0f);
Jamie Madill876429b2017-04-20 15:46:24 -0400108 glDrawElements(GL_TRIANGLES, 6, type, reinterpret_cast<void *>(indexDataWidth * 2));
Corentin Wallezd71620a2015-07-24 08:06:31 -0700109 EXPECT_PIXEL_EQ(64, 64, 0, 255, 0, 255);
110
111 EXPECT_GL_NO_ERROR();
112 swapBuffers();
113 }
114
115 GLuint mProgram;
116 GLint mColorUniformLocation;
117 GLint mPositionAttributeLocation;
118 GLuint mVertexBuffer;
119 GLuint mIndexBuffer;
120};
121
122// Test using an offset for an UInt8 index buffer
123TEST_P(IndexBufferOffsetTest, UInt8Index)
124{
125 GLubyte indexData[] = {0, 1, 2, 1, 2, 3};
126 runTest(GL_UNSIGNED_BYTE, 1, indexData);
127}
128
129// Test using an offset for an UInt16 index buffer
130TEST_P(IndexBufferOffsetTest, UInt16Index)
131{
132 GLushort indexData[] = {0, 1, 2, 1, 2, 3};
133 runTest(GL_UNSIGNED_SHORT, 2, indexData);
134}
135
136// Test using an offset for an UInt32 index buffer
137TEST_P(IndexBufferOffsetTest, UInt32Index)
138{
Martin Radev1be913c2016-07-11 17:59:16 +0300139 if (getClientMajorVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint"))
Corentin Wallezd71620a2015-07-24 08:06:31 -0700140 {
141 std::cout << "Test skipped because ES3 or GL_OES_element_index_uint is not available." << std::endl;
142 return;
143 }
144
145 GLuint indexData[] = {0, 1, 2, 1, 2, 3};
146 runTest(GL_UNSIGNED_INT, 4, indexData);
147}
148
149ANGLE_INSTANTIATE_TEST(IndexBufferOffsetTest,
150 ES2_D3D9(),
151 ES2_D3D11(),
152 ES3_D3D11(),
153 ES2_OPENGL(),
Geoff Lange0cc2a42016-01-20 10:58:17 -0500154 ES3_OPENGL(),
155 ES2_OPENGLES(),
156 ES3_OPENGLES());