Corentin Wallez | d71620a | 2015-07-24 08:06:31 -0700 | [diff] [blame] | 1 | // |
| 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 Wallez | d71620a | 2015-07-24 08:06:31 -0700 | [diff] [blame] | 9 | #include "system_utils.h" |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 10 | #include "test_utils/ANGLETest.h" |
Corentin Wallez | d71620a | 2015-07-24 08:06:31 -0700 | [diff] [blame] | 11 | |
| 12 | using namespace angle; |
| 13 | |
| 14 | class 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 Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 34 | void main() { gl_Position = vec4(position, 0.0, 1.0); }); |
Corentin Wallez | d71620a | 2015-07-24 08:06:31 -0700 | [diff] [blame] | 35 | |
| 36 | const std::string fragmentShaderSource = |
| 37 | SHADER_SOURCE(precision highp float; uniform vec4 color; |
| 38 | |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 39 | void main() { gl_FragColor = color; }); |
Corentin Wallez | d71620a | 2015-07-24 08:06:31 -0700 | [diff] [blame] | 40 | |
| 41 | mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource); |
| 42 | ASSERT_NE(0u, mProgram); |
| 43 | |
| 44 | mColorUniformLocation = glGetUniformLocation(mProgram, "color"); |
| 45 | mPositionAttributeLocation = glGetAttribLocation(mProgram, "position"); |
| 46 | |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 47 | const GLfloat vertices[] = {-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f}; |
Corentin Wallez | d71620a | 2015-07-24 08:06:31 -0700 | [diff] [blame] | 48 | 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 Madill | 876429b | 2017-04-20 15:46:24 -0400 | [diff] [blame] | 88 | glDrawElements(GL_TRIANGLES, 6, type, reinterpret_cast<void *>(indexDataWidth)); |
Corentin Wallez | d71620a | 2015-07-24 08:06:31 -0700 | [diff] [blame] | 89 | 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 Madill | 876429b | 2017-04-20 15:46:24 -0400 | [diff] [blame] | 96 | glDrawElements(GL_TRIANGLES, 6, type, reinterpret_cast<void *>(indexDataWidth * 2)); |
Corentin Wallez | d71620a | 2015-07-24 08:06:31 -0700 | [diff] [blame] | 97 | 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 |
| 111 | TEST_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 |
| 118 | TEST_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 |
| 125 | TEST_P(IndexBufferOffsetTest, UInt32Index) |
| 126 | { |
Martin Radev | 1be913c | 2016-07-11 17:59:16 +0300 | [diff] [blame] | 127 | if (getClientMajorVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint")) |
Corentin Wallez | d71620a | 2015-07-24 08:06:31 -0700 | [diff] [blame] | 128 | { |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 129 | std::cout << "Test skipped because ES3 or GL_OES_element_index_uint is not available." |
| 130 | << std::endl; |
Corentin Wallez | d71620a | 2015-07-24 08:06:31 -0700 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | |
| 134 | GLuint indexData[] = {0, 1, 2, 1, 2, 3}; |
| 135 | runTest(GL_UNSIGNED_INT, 4, indexData); |
| 136 | } |
| 137 | |
| 138 | ANGLE_INSTANTIATE_TEST(IndexBufferOffsetTest, |
| 139 | ES2_D3D9(), |
| 140 | ES2_D3D11(), |
| 141 | ES3_D3D11(), |
| 142 | ES2_OPENGL(), |
Geoff Lang | e0cc2a4 | 2016-01-20 10:58:17 -0500 | [diff] [blame] | 143 | ES3_OPENGL(), |
| 144 | ES2_OPENGLES(), |
| 145 | ES3_OPENGLES()); |