Commit 1934b786 by Frank Henigman Committed by Commit Bot

Refactor instancing tests.

Replace a lot of code with one function that can do all the tests and do them with any divisor and number of instances. BUG=angleproject:2672 Change-Id: I4f7f59d17dad234c524e485d9baa3117fcbb57bd Reviewed-on: https://chromium-review.googlesource.com/c/1330309 Commit-Queue: Frank Henigman <fjhenigman@chromium.org> Reviewed-by: 's avatarYuly Novikov <ynovikov@chromium.org>
parent ccad5e33
...@@ -12,7 +12,7 @@ using namespace angle; ...@@ -12,7 +12,7 @@ using namespace angle;
class InstancingTest : public ANGLETest class InstancingTest : public ANGLETest
{ {
protected: protected:
InstancingTest() : mProgram(0), mVertexBuffer(0) InstancingTest()
{ {
setWindowWidth(256); setWindowWidth(256);
setWindowHeight(256); setWindowHeight(256);
...@@ -22,233 +22,170 @@ class InstancingTest : public ANGLETest ...@@ -22,233 +22,170 @@ class InstancingTest : public ANGLETest
setConfigAlphaBits(8); setConfigAlphaBits(8);
} }
~InstancingTest() override void TearDown() override
{ {
glDeleteBuffers(1, &mVertexBuffer); glDeleteBuffers(1, &mInstanceBuffer);
glDeleteProgram(mProgram); glDeleteProgram(mProgram0);
glDeleteProgram(mProgram1);
} }
void SetUp() override void SetUp() override
{ {
ANGLETest::SetUp(); ANGLETest::SetUp();
// Initialize the vertex and index vectors for (int i = 0; i < kMaxDrawn; ++i)
constexpr GLfloat qvertex1[3] = {-quadRadius, quadRadius, 0.0f};
constexpr GLfloat qvertex2[3] = {-quadRadius, -quadRadius, 0.0f};
constexpr GLfloat qvertex3[3] = {quadRadius, -quadRadius, 0.0f};
constexpr GLfloat qvertex4[3] = {quadRadius, quadRadius, 0.0f};
mQuadVertices.insert(mQuadVertices.end(), qvertex1, qvertex1 + 3);
mQuadVertices.insert(mQuadVertices.end(), qvertex2, qvertex2 + 3);
mQuadVertices.insert(mQuadVertices.end(), qvertex3, qvertex3 + 3);
mQuadVertices.insert(mQuadVertices.end(), qvertex4, qvertex4 + 3);
constexpr GLfloat coord1[2] = {0.0f, 0.0f};
constexpr GLfloat coord2[2] = {0.0f, 1.0f};
constexpr GLfloat coord3[2] = {1.0f, 1.0f};
constexpr GLfloat coord4[2] = {1.0f, 0.0f};
mTexcoords.insert(mTexcoords.end(), coord1, coord1 + 2);
mTexcoords.insert(mTexcoords.end(), coord2, coord2 + 2);
mTexcoords.insert(mTexcoords.end(), coord3, coord3 + 2);
mTexcoords.insert(mTexcoords.end(), coord4, coord4 + 2);
mIndices.push_back(0);
mIndices.push_back(1);
mIndices.push_back(2);
mIndices.push_back(0);
mIndices.push_back(2);
mIndices.push_back(3);
for (size_t vertexIndex = 0; vertexIndex < 6; ++vertexIndex)
{ {
mNonIndexedVertices.insert(mNonIndexedVertices.end(), mInstanceData[i] = i * kDrawSize;
mQuadVertices.begin() + mIndices[vertexIndex] * 3,
mQuadVertices.begin() + mIndices[vertexIndex] * 3 + 3);
} }
glGenBuffers(1, &mInstanceBuffer);
glBindBuffer(GL_ARRAY_BUFFER, mInstanceBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(mInstanceData), mInstanceData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
for (size_t vertexIndex = 0; vertexIndex < 6; ++vertexIndex) const std::string inst = "attribute float a_instance;";
const std::string pos = "attribute vec2 a_position;";
const std::string main = R"(
void main()
{ {
mNonIndexedVertices.insert(mNonIndexedVertices.end(), gl_PointSize = 6.0;
mQuadVertices.begin() + mIndices[vertexIndex] * 3, gl_Position = vec4(a_position.x, a_position.y + a_instance, 0, 1);
mQuadVertices.begin() + mIndices[vertexIndex] * 3 + 3);
} }
)";
// Tile a 2x2 grid of the tiles // attrib 0 is instanced
for (float y = -1.0f + quadRadius; y < 1.0f - quadRadius; y += quadRadius * 3) mProgram0 = CompileProgram(inst + pos + main, essl1_shaders::fs::Red());
{ ASSERT_NE(0u, mProgram0);
for (float x = -1.0f + quadRadius; x < 1.0f - quadRadius; x += quadRadius * 3) ASSERT_EQ(0, glGetAttribLocation(mProgram0, "a_instance"));
{ ASSERT_EQ(1, glGetAttribLocation(mProgram0, "a_position"));
const GLfloat instance[3] = {x + quadRadius, y + quadRadius, 0.0f};
mInstances.insert(mInstances.end(), instance, instance + 3);
}
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glGenBuffers(1, &mVertexBuffer); // attrib 1 is instanced
mProgram1 = CompileProgram(pos + inst + main, essl1_shaders::fs::Red());
ASSERT_NE(0u, mProgram1);
ASSERT_EQ(1, glGetAttribLocation(mProgram1, "a_instance"));
ASSERT_EQ(0, glGetAttribLocation(mProgram1, "a_position"));
ASSERT_GL_NO_ERROR(); glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
} }
void setupDrawArraysTest(const std::string &vs) void runTest(
unsigned numInstance,
unsigned divisor,
bool attribZeroInstanced, // true: attrib 0 is instance, false: attrib 1 is instanced
bool points, // true: draw points, false: draw quad
bool indexed, // true: DrawElements, false: DrawArrays
bool offset, // true: pass nonzero offset to DrawArrays, false: zero offset
bool buffer) // true: use instance data in buffer, false: in client memory
{ {
mProgram = CompileProgram(vs, essl1_shaders::fs::Red()); // The window is divided into kMaxDrawn slices of size kDrawSize.
ASSERT_NE(0u, mProgram); // The slice drawn into is determined by the instance datum.
// The instance data array selects all the slices in order.
// Set the viewport // 'lastDrawn' is the index (zero-based) of the last slice into which we draw.
glViewport(0, 0, getWindowWidth(), getWindowHeight()); const unsigned lastDrawn = (numInstance - 1) / divisor;
ASSERT(lastDrawn < kMaxDrawn);
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// Use the program object
glUseProgram(mProgram);
}
void setupInstancedPointsTest() const int instanceAttrib = attribZeroInstanced ? 0 : 1;
{ const int positionAttrib = attribZeroInstanced ? 1 : 0;
mIndices.clear();
mIndices.push_back(0);
mIndices.push_back(1);
mIndices.push_back(2);
mIndices.push_back(3);
// clang-format off glUseProgram(attribZeroInstanced ? mProgram0 : mProgram1);
const std::string vs =
"attribute vec3 a_position;\n"
"attribute vec3 a_instancePos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(a_position.xyz, 1.0);\n"
" gl_Position = vec4(a_instancePos.xyz, 1.0);\n"
" gl_PointSize = 6.0;\n"
"}\n";
// clang-format on
mProgram = CompileProgram(vs, essl1_shaders::fs::Red()); glBindBuffer(GL_ARRAY_BUFFER, buffer ? mInstanceBuffer : 0);
ASSERT_NE(0u, mProgram); glVertexAttribPointer(instanceAttrib, 1, GL_FLOAT, GL_FALSE, 0,
buffer ? nullptr : mInstanceData);
glEnableVertexAttribArray(instanceAttrib);
glVertexAttribDivisorANGLE(instanceAttrib, divisor);
// Set the viewport glBindBuffer(GL_ARRAY_BUFFER, 0);
glViewport(0, 0, getWindowWidth(), getWindowHeight()); glVertexAttribPointer(positionAttrib, 2, GL_FLOAT, GL_FALSE, 0,
points ? kPointVertices : kQuadVertices);
glEnableVertexAttribArray(positionAttrib);
glVertexAttribDivisorANGLE(positionAttrib, 0);
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
// Use the program object if (points)
glUseProgram(mProgram);
}
void runDrawArraysTest(GLint first, GLsizei count, GLsizei instanceCount, const float *offset)
{ {
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer); if (indexed)
glBufferData(GL_ARRAY_BUFFER, mInstances.size() * sizeof(mInstances[0]), &mInstances[0], glDrawElementsInstancedANGLE(GL_POINTS, ArraySize(kPointIndices), GL_UNSIGNED_SHORT,
GL_STATIC_DRAW); kPointIndices, numInstance);
glBindBuffer(GL_ARRAY_BUFFER, 0); else
glDrawArraysInstancedANGLE(GL_POINTS, offset ? 2 : 0, 4, numInstance);
// Get the attribute locations
GLint positionLoc = glGetAttribLocation(mProgram, "a_position");
GLint instancePosLoc = glGetAttribLocation(mProgram, "a_instancePos");
// Load the vertex position
glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, mNonIndexedVertices.data());
glEnableVertexAttribArray(positionLoc);
// Load the instance position
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glVertexAttribPointer(instancePosLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(instancePosLoc);
// Enable instancing
glVertexAttribDivisorANGLE(instancePosLoc, 1);
// Offset
GLint uniformLoc = glGetUniformLocation(mProgram, "u_offset");
ASSERT_NE(-1, uniformLoc);
glUniform3fv(uniformLoc, 1, offset);
// Do the instanced draw
glDrawArraysInstancedANGLE(GL_TRIANGLES, first, count, instanceCount);
ASSERT_GL_NO_ERROR();
} }
else
virtual void runDrawElementsTest(std::string vs, bool shouldAttribZeroBeInstanced)
{ {
const std::string fs = if (indexed)
"precision mediump float;\n" glDrawElementsInstancedANGLE(GL_TRIANGLES, ArraySize(kQuadIndices),
"void main()\n" GL_UNSIGNED_SHORT, kQuadIndices, numInstance);
"{\n" else
" gl_FragColor = vec4(1.0, 0, 0, 1.0);\n" glDrawArraysInstancedANGLE(GL_TRIANGLES, offset ? 4 : 0, 6, numInstance);
"}\n"; }
ANGLE_GL_PROGRAM(program, vs, fs);
// Get the attribute locations
GLint positionLoc = glGetAttribLocation(program, "a_position");
GLint instancePosLoc = glGetAttribLocation(program, "a_instancePos");
// If this ASSERT fails then the vertex shader code should be refactored
ASSERT_EQ(shouldAttribZeroBeInstanced, (instancePosLoc == 0));
// Set the viewport
glViewport(0, 0, getWindowWidth(), getWindowHeight());
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// Use the program object
glUseProgram(program);
// Load the vertex position
glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, mQuadVertices.data());
glEnableVertexAttribArray(positionLoc);
// Load the instance position
glVertexAttribPointer(instancePosLoc, 3, GL_FLOAT, GL_FALSE, 0, mInstances.data());
glEnableVertexAttribArray(instancePosLoc);
// Enable instancing
glVertexAttribDivisorANGLE(instancePosLoc, 1);
// Do the instanced draw
glDrawElementsInstancedANGLE(GL_TRIANGLES, static_cast<GLsizei>(mIndices.size()),
GL_UNSIGNED_SHORT, mIndices.data(),
static_cast<GLsizei>(mInstances.size()) / 3);
ASSERT_GL_NO_ERROR(); ASSERT_GL_NO_ERROR();
checkDrawing(lastDrawn);
checkQuads();
} }
void checkQuads() void checkDrawing(unsigned lastDrawn)
{ {
// Check that various pixels are the expected color. for (unsigned i = 0; i < kMaxDrawn; ++i)
for (unsigned int quadIndex = 0; quadIndex < 4; ++quadIndex)
{ {
unsigned int baseOffset = quadIndex * 3; float y = -1.0 + kDrawSize / 2.0 + i * kDrawSize;
int iy = (y + 1.0) / 2.0 * getWindowHeight();
int quadx = for (unsigned j = 0; j < 8; j += 2)
static_cast<int>(((mInstances[baseOffset + 0]) * 0.5f + 0.5f) * getWindowWidth()); {
int quady = int ix = (kPointVertices[j] + 1.0) / 2.0 * getWindowWidth();
static_cast<int>(((mInstances[baseOffset + 1]) * 0.5f + 0.5f) * getWindowHeight()); EXPECT_PIXEL_COLOR_EQ(ix, iy, i <= lastDrawn ? GLColor::red : GLColor::blue);
}
EXPECT_PIXEL_EQ(quadx, quady, 255, 0, 0, 255);
} }
} }
// Vertex data GLuint mProgram0;
std::vector<GLfloat> mQuadVertices; GLuint mProgram1;
std::vector<GLfloat> mNonIndexedVertices; GLuint mInstanceBuffer;
std::vector<GLfloat> mTexcoords;
std::vector<GLfloat> mInstances;
std::vector<GLushort> mIndices;
static constexpr GLfloat quadRadius = 0.30f; static constexpr int kMaxDrawn = 4;
static constexpr float kDrawSize = 2.0 / kMaxDrawn;
GLfloat mInstanceData[kMaxDrawn];
GLuint mProgram; // clang-format off
GLuint mVertexBuffer;
// Vertices 0-5 are two triangles that form a quad filling the first "slice" of the window.
// See above about slices. Vertices 4-9 are the same two triangles.
static constexpr GLfloat kQuadVertices[] = {
-1, -1,
1, -1,
-1, -1 + kDrawSize,
1, -1,
1, -1 + kDrawSize,
-1, -1 + kDrawSize,
1, -1,
1, -1,
-1, -1 + kDrawSize,
-1, -1,
};
// Points 0-3 are spread across the first "slice."
// Points 2-4 are the same.
static constexpr GLfloat kPointVertices[] = {
-0.6f, -1 + kDrawSize / 2.0,
-0.2f, -1 + kDrawSize / 2.0,
0.2f, -1 + kDrawSize / 2.0,
0.6f, -1 + kDrawSize / 2.0,
-0.2f, -1 + kDrawSize / 2.0,
-0.6f, -1 + kDrawSize / 2.0,
};
// clang-format on
// Same two triangles as described above.
static constexpr GLushort kQuadIndices[] = {2, 9, 7, 5, 6, 4};
// Same four points as described above.
static constexpr GLushort kPointIndices[] = {1, 5, 3, 2};
}; };
constexpr GLfloat InstancingTest::kQuadVertices[];
constexpr GLfloat InstancingTest::kPointVertices[];
constexpr GLushort InstancingTest::kQuadIndices[];
constexpr GLushort InstancingTest::kPointIndices[];
class InstancingTestAllConfigs : public InstancingTest class InstancingTestAllConfigs : public InstancingTest
{ {
protected: protected:
...@@ -274,15 +211,8 @@ TEST_P(InstancingTestAllConfigs, AttributeZeroInstanced) ...@@ -274,15 +211,8 @@ TEST_P(InstancingTestAllConfigs, AttributeZeroInstanced)
{ {
ANGLE_SKIP_TEST_IF(!extensionEnabled("GL_ANGLE_instanced_arrays")); ANGLE_SKIP_TEST_IF(!extensionEnabled("GL_ANGLE_instanced_arrays"));
const std::string vs = runTest(4, 1, true /* attrib 0 instanced */, false /* quads */, true /* DrawElements */,
"attribute vec3 a_instancePos;\n" false /* N/A */, false /* no buffer */);
"attribute vec3 a_position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(a_position.xyz + a_instancePos.xyz, 1.0);\n"
"}\n";
runDrawElementsTest(vs, true);
} }
// Same as AttributeZeroInstanced, but attribute zero is not instanced. // Same as AttributeZeroInstanced, but attribute zero is not instanced.
...@@ -292,15 +222,8 @@ TEST_P(InstancingTestAllConfigs, AttributeZeroNotInstanced) ...@@ -292,15 +222,8 @@ TEST_P(InstancingTestAllConfigs, AttributeZeroNotInstanced)
{ {
ANGLE_SKIP_TEST_IF(!extensionEnabled("GL_ANGLE_instanced_arrays")); ANGLE_SKIP_TEST_IF(!extensionEnabled("GL_ANGLE_instanced_arrays"));
const std::string vs = runTest(4, 1, false /* attrib 1 instanced */, false /* quads */, true /* DrawElements */,
"attribute vec3 a_position;\n" false /* N/A */, false /* no buffer */);
"attribute vec3 a_instancePos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(a_position.xyz + a_instancePos.xyz, 1.0);\n"
"}\n";
runDrawElementsTest(vs, false);
} }
// Tests that the "first" parameter to glDrawArraysInstancedANGLE is only an offset into // Tests that the "first" parameter to glDrawArraysInstancedANGLE is only an offset into
...@@ -309,24 +232,8 @@ TEST_P(InstancingTestNo9_3, DrawArraysWithOffset) ...@@ -309,24 +232,8 @@ TEST_P(InstancingTestNo9_3, DrawArraysWithOffset)
{ {
ANGLE_SKIP_TEST_IF(!extensionEnabled("GL_ANGLE_instanced_arrays")); ANGLE_SKIP_TEST_IF(!extensionEnabled("GL_ANGLE_instanced_arrays"));
const std::string vs = runTest(4, 1, false /* attribute 1 instanced */, false /* quads */, false /* DrawArrays */,
"attribute vec3 a_position;\n" true /* offset>0 */, true /* buffer */);
"attribute vec3 a_instancePos;\n"
"uniform vec3 u_offset;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(a_position.xyz + a_instancePos.xyz + u_offset, 1.0);\n"
"}\n";
setupDrawArraysTest(vs);
constexpr float offset1[3] = {0, 0, 0};
runDrawArraysTest(0, 6, 2, offset1);
constexpr float offset2[3] = {0.0f, 1.0f, 0};
runDrawArraysTest(6, 6, 2, offset2);
checkQuads();
} }
// This test verifies instancing with GL_POINTS with glDrawArraysInstanced works. // This test verifies instancing with GL_POINTS with glDrawArraysInstanced works.
...@@ -340,37 +247,8 @@ TEST_P(InstancingTestPoints, DrawArrays) ...@@ -340,37 +247,8 @@ TEST_P(InstancingTestPoints, DrawArrays)
// This doesn't occur on Windows 10 (Version 1511) though. // This doesn't occur on Windows 10 (Version 1511) though.
ignoreD3D11SDKLayersWarnings(); ignoreD3D11SDKLayersWarnings();
setupInstancedPointsTest(); runTest(4, 1, false /* attrib 1 instanced */, true /* points */, false /* DrawArrays */,
false /* offset=0 */, true /* buffer */);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, mInstances.size() * sizeof(mInstances[0]), &mInstances[0],
GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Get the attribute locations
GLint positionLoc = glGetAttribLocation(mProgram, "a_position");
GLint instancePosLoc = glGetAttribLocation(mProgram, "a_instancePos");
// Load the vertex position
constexpr GLfloat pos[3] = {0, 0, 0};
glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, pos);
glEnableVertexAttribArray(positionLoc);
// Load the instance position
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glVertexAttribPointer(instancePosLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(instancePosLoc);
// Enable instancing
glVertexAttribDivisorANGLE(instancePosLoc, 1);
// Do the instanced draw
glDrawArraysInstancedANGLE(GL_POINTS, 0, 1, static_cast<GLsizei>(mInstances.size()) / 3);
ASSERT_GL_NO_ERROR();
checkQuads();
} }
// This test verifies instancing with GL_POINTS with glDrawElementsInstanced works. // This test verifies instancing with GL_POINTS with glDrawElementsInstanced works.
...@@ -384,39 +262,8 @@ TEST_P(InstancingTestPoints, DrawElements) ...@@ -384,39 +262,8 @@ TEST_P(InstancingTestPoints, DrawElements)
// This doesn't occur on Windows 10 (Version 1511) though. // This doesn't occur on Windows 10 (Version 1511) though.
ignoreD3D11SDKLayersWarnings(); ignoreD3D11SDKLayersWarnings();
setupInstancedPointsTest(); runTest(4, 1, false /* attrib 1 instanced */, true /* points */, true /* DrawElements */,
false /* N/A */, true /* buffer */);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, mInstances.size() * sizeof(mInstances[0]), &mInstances[0],
GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Get the attribute locations
GLint positionLoc = glGetAttribLocation(mProgram, "a_position");
GLint instancePosLoc = glGetAttribLocation(mProgram, "a_instancePos");
// Load the vertex position
const Vector3 pos[] = {Vector3(0), Vector3(0), Vector3(0), Vector3(0)};
glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, pos);
glEnableVertexAttribArray(positionLoc);
// Load the instance position
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glVertexAttribPointer(instancePosLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(instancePosLoc);
// Enable instancing
glVertexAttribDivisorANGLE(instancePosLoc, 1);
// Do the instanced draw
glDrawElementsInstancedANGLE(GL_POINTS, static_cast<GLsizei>(mIndices.size()),
GL_UNSIGNED_SHORT, mIndices.data(),
static_cast<GLsizei>(mInstances.size()) / 3);
ASSERT_GL_NO_ERROR();
checkQuads();
} }
class InstancingTestES3 : public InstancingTest class InstancingTestES3 : public InstancingTest
...@@ -436,29 +283,11 @@ TEST_P(InstancingTestES31, UpdateAttribBindingByVertexAttribDivisor) ...@@ -436,29 +283,11 @@ TEST_P(InstancingTestES31, UpdateAttribBindingByVertexAttribDivisor)
{ {
ANGLE_SKIP_TEST_IF(!extensionEnabled("GL_ANGLE_instanced_arrays")); ANGLE_SKIP_TEST_IF(!extensionEnabled("GL_ANGLE_instanced_arrays"));
const std::string vs = glUseProgram(mProgram0);
"attribute vec3 a_instancePos;\n"
"attribute vec3 a_position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(a_position.xyz + a_instancePos.xyz, 1.0);\n"
"}\n";
const std::string fs =
"precision mediump float;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0, 0, 0, 1.0);\n"
"}\n";
constexpr GLsizei kFloatStride = 4;
ANGLE_GL_PROGRAM(program, vs, fs);
glUseProgram(program);
// Get the attribute locations // Get the attribute locations
GLint positionLoc = glGetAttribLocation(program, "a_position"); GLint positionLoc = glGetAttribLocation(mProgram0, "a_position");
GLint instancePosLoc = glGetAttribLocation(program, "a_instancePos"); GLint instancePosLoc = glGetAttribLocation(mProgram0, "a_instance");
ASSERT_NE(-1, positionLoc); ASSERT_NE(-1, positionLoc);
ASSERT_NE(-1, instancePosLoc); ASSERT_NE(-1, instancePosLoc);
ASSERT_GL_NO_ERROR(); ASSERT_GL_NO_ERROR();
...@@ -469,16 +298,16 @@ TEST_P(InstancingTestES31, UpdateAttribBindingByVertexAttribDivisor) ...@@ -469,16 +298,16 @@ TEST_P(InstancingTestES31, UpdateAttribBindingByVertexAttribDivisor)
GLBuffer quadBuffer; GLBuffer quadBuffer;
glBindBuffer(GL_ARRAY_BUFFER, quadBuffer); glBindBuffer(GL_ARRAY_BUFFER, quadBuffer);
glBufferData(GL_ARRAY_BUFFER, mQuadVertices.size() * kFloatStride, mQuadVertices.data(), glBufferData(GL_ARRAY_BUFFER, sizeof(kQuadVertices), kQuadVertices, GL_STATIC_DRAW);
GL_STATIC_DRAW);
GLBuffer instancesBuffer; const unsigned numInstance = 4;
glBindBuffer(GL_ARRAY_BUFFER, instancesBuffer); const unsigned divisor = 1;
glBufferData(GL_ARRAY_BUFFER, mInstances.size() * kFloatStride, mInstances.data(), const unsigned lastDrawn = (numInstance - 1) / divisor;
GL_STATIC_DRAW); ASSERT(lastDrawn < kMaxDrawn);
// Set the formats by VertexAttribFormat // Set the formats by VertexAttribFormat
glVertexAttribFormat(positionLoc, 3, GL_FLOAT, GL_FALSE, 0); glVertexAttribFormat(positionLoc, 2, GL_FLOAT, GL_FALSE, 0);
glVertexAttribFormat(instancePosLoc, 3, GL_FLOAT, GL_FALSE, 0); glVertexAttribFormat(instancePosLoc, 1, GL_FLOAT, GL_FALSE, 0);
glEnableVertexAttribArray(positionLoc); glEnableVertexAttribArray(positionLoc);
glEnableVertexAttribArray(instancePosLoc); glEnableVertexAttribArray(instancePosLoc);
...@@ -487,8 +316,8 @@ TEST_P(InstancingTestES31, UpdateAttribBindingByVertexAttribDivisor) ...@@ -487,8 +316,8 @@ TEST_P(InstancingTestES31, UpdateAttribBindingByVertexAttribDivisor)
// Load the vertex position into the binding indexed positionBinding (== instancePosLoc) // Load the vertex position into the binding indexed positionBinding (== instancePosLoc)
// Load the instance position into the binding indexed instanceBinding (== positionLoc) // Load the instance position into the binding indexed instanceBinding (== positionLoc)
glBindVertexBuffer(positionBinding, quadBuffer, 0, kFloatStride * 3); glBindVertexBuffer(positionBinding, quadBuffer, 0, 2 * sizeof(kQuadVertices[0]));
glBindVertexBuffer(instanceBinding, instancesBuffer, 0, kFloatStride * 3); glBindVertexBuffer(instanceBinding, mInstanceBuffer, 0, sizeof(mInstanceData[0]));
// The attribute indexed positionLoc is using the binding indexed positionBinding // The attribute indexed positionLoc is using the binding indexed positionBinding
// The attribute indexed instancePosLoc is using the binding indexed instanceBinding // The attribute indexed instancePosLoc is using the binding indexed instanceBinding
...@@ -496,34 +325,34 @@ TEST_P(InstancingTestES31, UpdateAttribBindingByVertexAttribDivisor) ...@@ -496,34 +325,34 @@ TEST_P(InstancingTestES31, UpdateAttribBindingByVertexAttribDivisor)
glVertexAttribBinding(instancePosLoc, instanceBinding); glVertexAttribBinding(instancePosLoc, instanceBinding);
// Enable instancing on the binding indexed instanceBinding // Enable instancing on the binding indexed instanceBinding
glVertexBindingDivisor(instanceBinding, 1); glVertexBindingDivisor(instanceBinding, divisor);
// Do the first instanced draw // Do the first instanced draw
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glDrawElementsInstanced(GL_TRIANGLES, static_cast<GLsizei>(mIndices.size()), GL_UNSIGNED_SHORT, glDrawElementsInstanced(GL_TRIANGLES, ArraySize(kQuadIndices), GL_UNSIGNED_SHORT, kQuadIndices,
mIndices.data(), static_cast<GLsizei>(mInstances.size()) / 3); numInstance);
checkQuads(); checkDrawing(lastDrawn);
// Disable instancing. // Disable instancing.
glVertexBindingDivisor(instanceBinding, 0); glVertexBindingDivisor(instanceBinding, 0);
// Load the vertex position into the binding indexed positionLoc. // Load the vertex position into the binding indexed positionLoc.
// Load the instance position into the binding indexed instancePosLoc. // Load the instance position into the binding indexed instancePosLoc.
glBindVertexBuffer(positionLoc, quadBuffer, 0, kFloatStride * 3); glBindVertexBuffer(positionLoc, quadBuffer, 0, 2 * sizeof(kQuadVertices[0]));
glBindVertexBuffer(instancePosLoc, instancesBuffer, 0, kFloatStride * 3); glBindVertexBuffer(instancePosLoc, mInstanceBuffer, 0, sizeof(mInstanceData[0]));
// The attribute indexed positionLoc is using the binding indexed positionLoc. // The attribute indexed positionLoc is using the binding indexed positionLoc.
glVertexAttribBinding(positionLoc, positionLoc); glVertexAttribBinding(positionLoc, positionLoc);
// Call VertexAttribDivisor to both enable instancing on instancePosLoc and set the attribute // Call VertexAttribDivisor to both enable instancing on instancePosLoc and set the attribute
// indexed instancePosLoc using the binding indexed instancePosLoc. // indexed instancePosLoc using the binding indexed instancePosLoc.
glVertexAttribDivisor(instancePosLoc, 1); glVertexAttribDivisor(instancePosLoc, divisor);
// Do the second instanced draw // Do the second instanced draw
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glDrawElementsInstanced(GL_TRIANGLES, static_cast<GLsizei>(mIndices.size()), GL_UNSIGNED_SHORT, glDrawElementsInstanced(GL_TRIANGLES, ArraySize(kQuadIndices), GL_UNSIGNED_SHORT, kQuadIndices,
mIndices.data(), static_cast<GLsizei>(mInstances.size()) / 3); numInstance);
checkQuads(); checkDrawing(lastDrawn);
glDeleteVertexArrays(1, &vao); glDeleteVertexArrays(1, &vao);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment