Hash :
3e1e1087
Author :
Date :
2020-05-11T00:50:00
Metal: Use 2d array for caching index conversion's pipeline state. For index conversion utils, use 2d array for caching compute pipeline state based on DrawElementsType & source offset is aligned or not, instead of using std::map as previously. Also moved default shader's initialization to DisplayMtl. New test added: IndexBufferOffsetTest.DrawAtDifferentOffsetAlignments Bug: angleproject:2634 Change-Id: I1bd77aca88e03229ef8053e32add66733e33b06e Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2192569 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Corentin Wallez <cwallez@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
//
// Copyright 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// IndexBufferOffsetTest.cpp: Test glDrawElements with an offset and an index buffer
#include "test_utils/ANGLETest.h"
#include "util/test_utils.h"
using namespace angle;
class IndexBufferOffsetTest : public ANGLETest
{
protected:
IndexBufferOffsetTest()
{
setWindowWidth(128);
setWindowHeight(128);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
void testSetUp() override
{
constexpr char kVS[] =
R"(precision highp float;
attribute vec2 position;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
})";
constexpr char kFS[] =
R"(precision highp float;
uniform vec4 color;
void main()
{
gl_FragColor = color;
})";
mProgram = CompileProgram(kVS, kFS);
ASSERT_NE(0u, mProgram);
mColorUniformLocation = glGetUniformLocation(mProgram, "color");
mPositionAttributeLocation = glGetAttribLocation(mProgram, "position");
const GLfloat vertices[] = {-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f};
glGenBuffers(1, &mVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
glGenBuffers(1, &mIndexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
}
void testTearDown() override
{
glDeleteBuffers(1, &mVertexBuffer);
glDeleteBuffers(1, &mIndexBuffer);
glDeleteProgram(mProgram);
}
void runTest(GLenum type, int typeWidth, void *indexData)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
GLuint nullIndexData[] = {0, 0, 0, 0, 0, 0};
size_t indexDataWidth = 6 * typeWidth;
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * indexDataWidth, nullptr, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indexDataWidth, nullIndexData);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, indexDataWidth, indexDataWidth, indexData);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 2 * indexDataWidth, indexDataWidth, nullIndexData);
glUseProgram(mProgram);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glVertexAttribPointer(mPositionAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(mPositionAttributeLocation);
glUniform4f(mColorUniformLocation, 1.0f, 0.0f, 0.0f, 1.0f);
for (int i = 0; i < 16; i++)
{
glDrawElements(GL_TRIANGLES, 6, type, reinterpret_cast<void *>(indexDataWidth));
EXPECT_PIXEL_COLOR_EQ(64, 64, GLColor::red);
}
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, indexDataWidth, indexDataWidth, nullIndexData);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 2 * indexDataWidth, indexDataWidth, indexData);
glUniform4f(mColorUniformLocation, 0.0f, 1.0f, 0.0f, 1.0f);
glDrawElements(GL_TRIANGLES, 6, type, reinterpret_cast<void *>(indexDataWidth * 2));
EXPECT_PIXEL_COLOR_EQ(64, 64, GLColor::green);
EXPECT_GL_NO_ERROR();
swapBuffers();
}
GLuint mProgram;
GLint mColorUniformLocation;
GLint mPositionAttributeLocation;
GLuint mVertexBuffer;
GLuint mIndexBuffer;
};
// Test using an offset for an UInt8 index buffer
TEST_P(IndexBufferOffsetTest, UInt8Index)
{
GLubyte indexData[] = {0, 1, 2, 1, 2, 3};
runTest(GL_UNSIGNED_BYTE, 1, indexData);
}
// Test using an offset for an UInt16 index buffer
TEST_P(IndexBufferOffsetTest, UInt16Index)
{
// TODO(jie.a.chen@intel.com): Re-enable the test once the driver fix is
// available in public release.
// http://anglebug.com/2663
ANGLE_SKIP_TEST_IF(IsIntel() && IsVulkan());
GLushort indexData[] = {0, 1, 2, 1, 2, 3};
runTest(GL_UNSIGNED_SHORT, 2, indexData);
}
// Test using an offset for an UInt32 index buffer
TEST_P(IndexBufferOffsetTest, UInt32Index)
{
ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 &&
!IsGLExtensionEnabled("GL_OES_element_index_uint"));
GLuint indexData[] = {0, 1, 2, 1, 2, 3};
runTest(GL_UNSIGNED_INT, 4, indexData);
}
// Uses index buffer offset and 2 drawElement calls one of the other, makes sure the second
// drawElement call will use the correct offset.
TEST_P(IndexBufferOffsetTest, DrawAtDifferentOffsets)
{
GLushort indexData[] = {0, 1, 2, 1, 2, 3};
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
size_t indexDataWidth = 6 * sizeof(GLushort);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexDataWidth, indexData, GL_DYNAMIC_DRAW);
glUseProgram(mProgram);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glVertexAttribPointer(mPositionAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(mPositionAttributeLocation);
glUniform4f(mColorUniformLocation, 1.0f, 0.0f, 0.0f, 1.0f);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT,
reinterpret_cast<void *>(indexDataWidth / 2));
// Check the upper left triangle
EXPECT_PIXEL_COLOR_EQ(0, getWindowHeight() / 4, GLColor::red);
// Check the down right triangle
EXPECT_PIXEL_COLOR_EQ(getWindowWidth() - 1, getWindowHeight() - 1, GLColor::red);
EXPECT_GL_NO_ERROR();
}
// Uses index buffer offset and 2 drawElement calls one of the other, one has aligned
// offset and one doesn't
TEST_P(IndexBufferOffsetTest, DrawAtDifferentOffsetAlignments)
{
GLubyte indexData8[] = {0, 1, 0, 1, 2, 3};
GLushort indexData16[] = {0, 1, 2, 1, 2, 3};
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
size_t indexDataWidth16 = 6 * sizeof(GLushort);
GLuint buffer[2];
glGenBuffers(2, buffer);
glUseProgram(mProgram);
glUniform4f(mColorUniformLocation, 1.0f, 0.0f, 0.0f, 1.0f);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glVertexAttribPointer(mPositionAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(mPositionAttributeLocation);
// 8 bit index with aligned offset
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexData8), indexData8, GL_DYNAMIC_DRAW);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, reinterpret_cast<void *>(2));
// 16 bits index buffer, which unaligned offset
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexDataWidth16, indexData16, GL_DYNAMIC_DRAW);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT,
reinterpret_cast<void *>(indexDataWidth16 / 2));
// Check the upper left triangle
EXPECT_PIXEL_COLOR_EQ(0, getWindowHeight() / 4, GLColor::red);
// Check the down right triangle
EXPECT_PIXEL_COLOR_EQ(getWindowWidth() - 1, getWindowHeight() - 1, GLColor::red);
EXPECT_GL_NO_ERROR();
}
// Uses index buffer offset and 2 drawElement calls one of the other with different counts,
// makes sure the second drawElement call will have its data available.
TEST_P(IndexBufferOffsetTest, DrawWithDifferentCountsSameOffset)
{
GLubyte indexData[] = {99, 0, 1, 2, 1, 2, 3};
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
size_t indexDataWidth = 7 * sizeof(GLubyte);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexDataWidth, indexData, GL_DYNAMIC_DRAW);
glUseProgram(mProgram);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glVertexAttribPointer(mPositionAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(mPositionAttributeLocation);
glUniform4f(mColorUniformLocation, 1.0f, 0.0f, 0.0f, 1.0f);
// The first draw draws the first triangle, and the second draws a quad.
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, reinterpret_cast<void *>(1));
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, reinterpret_cast<void *>(1));
// Check the upper left triangle
EXPECT_PIXEL_COLOR_EQ(0, getWindowHeight() / 4, GLColor::red);
// Check the down right triangle
EXPECT_PIXEL_COLOR_EQ(getWindowWidth() - 1, getWindowHeight() - 1, GLColor::red);
EXPECT_GL_NO_ERROR();
}
ANGLE_INSTANTIATE_TEST(IndexBufferOffsetTest,
ES2_D3D9(),
ES2_D3D11(),
ES3_D3D11(),
ES2_METAL(),
ES2_OPENGL(),
ES3_OPENGL(),
ES2_OPENGLES(),
ES3_OPENGLES(),
ES2_VULKAN());