Hash :
d26fb99e
Author :
Date :
2020-04-10T16:04:26
Add DrawBaseVertexVariants Tests This tests different DrawElements*BaseVertex draw calls from different extensions including OES_draw_elements_base_vertex, EXT_draw_elements_base_vertex, and ANGLE_base_vertex_base_instance, with various combinations of base vertex, base index values. Bug: angleproject:4536 Change-Id: I3cd256522684c6040199d7704aac8575237dbd96 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2146292 Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Shrek Shao <shrekshao@google.com> Commit-Queue: Shrek Shao <shrekshao@google.com>
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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
//
// Copyright 2020 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.
//
// DrawBaseVertexVariantsTest: Tests variants of drawElements*BaseVertex* call from different
// extensions
#include "gpu_info_util/SystemInfo.h"
#include "test_utils/ANGLETest.h"
#include "test_utils/gl_raii.h"
#include <numeric>
using namespace angle;
namespace
{
// Create a kWidth * kHeight canvas equally split into kCountX * kCountY tiles
// each containing a quad partially covering each tile
constexpr uint32_t kWidth = 256;
constexpr uint32_t kHeight = 256;
constexpr uint32_t kCountX = 8;
constexpr uint32_t kCountY = 8;
constexpr int kBoxCount = kCountX * kCountY;
constexpr uint32_t kIndexPatternRepeatCount = 3;
constexpr std::array<GLfloat, 2> kTileSize = {
1.f / static_cast<GLfloat>(kCountX),
1.f / static_cast<GLfloat>(kCountY),
};
constexpr std::array<uint32_t, 2> kTilePixelSize = {kWidth / kCountX, kHeight / kCountY};
constexpr std::array<GLfloat, 2> kQuadRadius = {0.25f * kTileSize[0], 0.25f * kTileSize[1]};
constexpr std::array<uint32_t, 2> kPixelCheckSize = {
static_cast<uint32_t>(kQuadRadius[0] * kWidth),
static_cast<uint32_t>(kQuadRadius[1] * kHeight)};
constexpr std::array<GLfloat, 2> GetTileCenter(uint32_t x, uint32_t y)
{
return {
kTileSize[0] * (0.5f + static_cast<GLfloat>(x)),
kTileSize[1] * (0.5f + static_cast<GLfloat>(y)),
};
}
constexpr std::array<std::array<GLfloat, 2>, 4> GetQuadVertices(uint32_t x, uint32_t y)
{
const auto center = GetTileCenter(x, y);
return {
std::array<GLfloat, 2>{center[0] - kQuadRadius[0], center[1] - kQuadRadius[1]},
std::array<GLfloat, 2>{center[0] + kQuadRadius[0], center[1] - kQuadRadius[1]},
std::array<GLfloat, 2>{center[0] + kQuadRadius[0], center[1] + kQuadRadius[1]},
std::array<GLfloat, 2>{center[0] - kQuadRadius[0], center[1] + kQuadRadius[1]},
};
}
enum class DrawCallVariants
{
DrawElementsBaseVertex,
DrawElementsInstancedBaseVertex,
DrawRangeElementsBaseVertex,
DrawElementsInstancedBaseVertexBaseInstance
};
// These tests check correctness of variants of baseVertex draw calls from different extensions
class DrawBaseVertexVariantsTest : public ANGLETest
{
protected:
DrawBaseVertexVariantsTest()
{
setWindowWidth(kWidth);
setWindowHeight(kHeight);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
std::array<GLushort, 6> indices = {0, 1, 2, 0, 2, 3};
mIndices.resize(indices.size() * kIndexPatternRepeatCount);
for (uint32_t i = 0; i < kIndexPatternRepeatCount; i++)
{
size_t o = i * indices.size();
size_t vo = i * 4; // each quad has 4 vertices, index offset by 4
for (size_t j = 0; j < indices.size(); j++)
{
mIndices[o + j] = vo + indices[j];
}
}
mColorPalette = {GLColor(0x7f, 0x7f, 0x7f, 0xff),
GLColor::red,
GLColor::green,
GLColor::yellow,
GLColor::blue,
GLColor::magenta,
GLColor::cyan,
GLColor::white};
for (uint32_t y = 0; y < kCountY; ++y)
{
for (uint32_t x = 0; x < kCountX; ++x)
{
// v3 ---- v2
// | |
// | |
// v0 ---- v1
const auto vs = ::GetQuadVertices(x, y);
for (const auto &v : vs)
{
mVertices.insert(mVertices.end(), v.begin(), v.end());
}
const auto &colorPicked = mColorPalette[(x + y) % mColorPalette.size()];
for (int i = 0; i < 4; ++i)
{
mVertexColors.push_back(colorPicked.R);
mVertexColors.push_back(colorPicked.G);
mVertexColors.push_back(colorPicked.B);
mVertexColors.push_back(colorPicked.A);
}
}
}
mRegularIndices.resize(kCountY * kCountX * mIndices.size());
for (uint32_t y = 0; y < kCountY; y++)
{
for (uint32_t x = 0; x < kCountX; x++)
{
uint32_t i = x + y * kCountX;
uint32_t oi = 6 * i;
uint32_t ov = 4 * i;
for (uint32_t j = 0; j < 6; j++)
{
mRegularIndices[oi + j] = mIndices[j] + ov;
}
}
}
}
void setupProgram(GLProgram &program)
{
constexpr char vs[] = R"(
precision mediump float;
attribute vec2 vPosition;
attribute vec4 vColor;
varying vec4 color;
void main()
{
gl_Position = vec4(vec3(vPosition, 1.0) * 2.0 - 1.0, 1.0);
color = vColor;
})";
constexpr char fs[] = R"(
precision mediump float;
varying vec4 color;
void main()
{
gl_FragColor = color;
})";
program.makeRaster(vs, fs);
EXPECT_GL_NO_ERROR();
ASSERT_TRUE(program.valid());
glUseProgram(program.get());
mPositionLoc = glGetAttribLocation(program.get(), "vPosition");
ASSERT_NE(-1, mPositionLoc);
mColorLoc = glGetAttribLocation(program.get(), "vColor");
ASSERT_NE(-1, mColorLoc);
}
void setupIndexedBuffers(GLBuffer &vertexPositionBuffer,
GLBuffer &vertexColorBuffer,
GLBuffer &indexBuffer)
{
glBindBuffer(GL_ARRAY_BUFFER, vertexColorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLubyte) * mVertexColors.size(), mVertexColors.data(),
GL_STATIC_DRAW);
glEnableVertexAttribArray(mColorLoc);
glVertexAttribPointer(mColorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, vertexPositionBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * mVertices.size(), mVertices.data(),
GL_STATIC_DRAW);
glEnableVertexAttribArray(mPositionLoc);
glVertexAttribPointer(mPositionLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * mIndices.size(), mIndices.data(),
GL_STATIC_DRAW);
ASSERT_GL_NO_ERROR();
}
void doDrawElementsBaseVertexVariants(DrawCallVariants drawCallType)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int baseRepetition = 0;
int i = 0;
// Start at various repetitions within the patterned index buffer to exercise base
// index.
static_assert(kIndexPatternRepeatCount >= 3, "Repeat pattern count should be at least 3");
while (i < kBoxCount)
{
int repetitionCount = std::min(3 - baseRepetition, kBoxCount - i);
switch (drawCallType)
{
case DrawCallVariants::DrawElementsInstancedBaseVertexBaseInstance:
glDrawElementsInstancedBaseVertexBaseInstanceANGLE(
GL_TRIANGLES, repetitionCount * 6, GL_UNSIGNED_SHORT,
reinterpret_cast<GLvoid *>(
static_cast<uintptr_t>(baseRepetition * 6 * sizeof(GLushort))),
1, (i - baseRepetition) * 4, 0);
break;
case DrawCallVariants::DrawElementsBaseVertex:
glDrawElementsBaseVertexEXT(
GL_TRIANGLES, repetitionCount * 6, GL_UNSIGNED_SHORT,
reinterpret_cast<GLvoid *>(
static_cast<uintptr_t>(baseRepetition * 6 * sizeof(GLushort))),
(i - baseRepetition) * 4);
break;
case DrawCallVariants::DrawElementsInstancedBaseVertex:
glDrawElementsInstancedBaseVertexEXT(
GL_TRIANGLES, repetitionCount * 6, GL_UNSIGNED_SHORT,
reinterpret_cast<GLvoid *>(
static_cast<uintptr_t>(baseRepetition * 6 * sizeof(GLushort))),
1, (i - baseRepetition) * 4);
break;
case DrawCallVariants::DrawRangeElementsBaseVertex:
glDrawRangeElementsBaseVertexEXT(
GL_TRIANGLES, baseRepetition * 4,
(baseRepetition + repetitionCount) * 4 - 1, repetitionCount * 6,
GL_UNSIGNED_SHORT,
reinterpret_cast<GLvoid *>(
static_cast<uintptr_t>(baseRepetition * 6 * sizeof(GLushort))),
(i - baseRepetition) * 4);
break;
default:
EXPECT_TRUE(false);
break;
}
baseRepetition = (baseRepetition + 1) % 3;
i += repetitionCount;
}
EXPECT_GL_NO_ERROR();
checkDrawResult();
}
void checkDrawResult()
{
for (uint32_t y = 0; y < kCountY; ++y)
{
for (uint32_t x = 0; x < kCountX; ++x)
{
uint32_t center_x = x * kTilePixelSize[0] + kTilePixelSize[0] / 2;
uint32_t center_y = y * kTilePixelSize[1] + kTilePixelSize[1] / 2;
const auto &color = mColorPalette[(x + y) % mColorPalette.size()];
EXPECT_PIXEL_NEAR(center_x - kPixelCheckSize[0] / 2,
center_y - kPixelCheckSize[1] / 2, color[0], color[1], color[2],
color[3], 1);
}
}
}
bool requestAngleBaseVertexBaseInstanceExtensions()
{
if (getClientMajorVersion() <= 2)
{
if (!EnsureGLExtensionEnabled("GL_ANGLE_instanced_arrays"))
{
return false;
}
}
return EnsureGLExtensionEnabled("GL_ANGLE_base_vertex_base_instance");
}
bool requestNativeBaseVertexExtensions()
{
return (EnsureGLExtensionEnabled("GL_OES_draw_elements_base_vertex") ||
EnsureGLExtensionEnabled("GL_EXT_draw_elements_base_vertex"));
}
std::vector<GLushort> mIndices;
std::vector<GLfloat> mVertices;
std::vector<GLubyte> mVertexColors;
std::vector<GLColor> mColorPalette;
std::vector<GLushort> mRegularIndices;
GLint mPositionLoc;
GLint mColorLoc;
};
// Test drawElementsBaseVertex from OES/EXT_draw_elements_base_vertex
TEST_P(DrawBaseVertexVariantsTest, DrawElementsBaseVertex)
{
ANGLE_SKIP_TEST_IF(!requestNativeBaseVertexExtensions());
GLProgram program;
setupProgram(program);
GLBuffer indexBuffer;
GLBuffer vertexPositionBuffer;
GLBuffer vertexColorBuffer;
setupIndexedBuffers(vertexPositionBuffer, vertexColorBuffer, indexBuffer);
doDrawElementsBaseVertexVariants(DrawCallVariants::DrawElementsBaseVertex);
}
// Test drawElementsInstancedBaseVertex from OES/EXT_draw_elements_base_vertex
TEST_P(DrawBaseVertexVariantsTest, DrawElementsInstancedBaseVertex)
{
ANGLE_SKIP_TEST_IF(!requestNativeBaseVertexExtensions());
GLProgram program;
setupProgram(program);
GLBuffer indexBuffer;
GLBuffer vertexPositionBuffer;
GLBuffer vertexColorBuffer;
setupIndexedBuffers(vertexPositionBuffer, vertexColorBuffer, indexBuffer);
doDrawElementsBaseVertexVariants(DrawCallVariants::DrawElementsInstancedBaseVertex);
}
// Test drawRangeElementsBaseVertex from OES/EXT_draw_elements_base_vertex
TEST_P(DrawBaseVertexVariantsTest, DrawRangeElementsBaseVertex)
{
ANGLE_SKIP_TEST_IF(!requestNativeBaseVertexExtensions());
GLProgram program;
setupProgram(program);
GLBuffer indexBuffer;
GLBuffer vertexPositionBuffer;
GLBuffer vertexColorBuffer;
setupIndexedBuffers(vertexPositionBuffer, vertexColorBuffer, indexBuffer);
doDrawElementsBaseVertexVariants(DrawCallVariants::DrawRangeElementsBaseVertex);
}
// Test drawElementsInstancedBaseVertexBaseInstance from ANGLE_base_vertex_base_instance
TEST_P(DrawBaseVertexVariantsTest, DrawElementsInstancedBaseVertexBaseInstance)
{
ANGLE_SKIP_TEST_IF(!requestAngleBaseVertexBaseInstanceExtensions());
GLProgram program;
setupProgram(program);
GLBuffer indexBuffer;
GLBuffer vertexPositionBuffer;
GLBuffer vertexColorBuffer;
setupIndexedBuffers(vertexPositionBuffer, vertexColorBuffer, indexBuffer);
doDrawElementsBaseVertexVariants(DrawCallVariants::DrawElementsInstancedBaseVertexBaseInstance);
}
ANGLE_INSTANTIATE_TEST_ES3(DrawBaseVertexVariantsTest);
} // namespace