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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
//
// 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.
//
#include "test_utils/ANGLETest.h"
using namespace angle;
class InstancingTest : public ANGLETest
{
protected:
InstancingTest() : mProgram(0), mVertexBuffer(0)
{
setWindowWidth(256);
setWindowHeight(256);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
~InstancingTest() override
{
glDeleteBuffers(1, &mVertexBuffer);
glDeleteProgram(mProgram);
}
void SetUp() override
{
ANGLETest::SetUp();
mVertexAttribDivisorANGLE = NULL;
mDrawArraysInstancedANGLE = NULL;
mDrawElementsInstancedANGLE = NULL;
char *extensionString = (char*)glGetString(GL_EXTENSIONS);
if (strstr(extensionString, "GL_ANGLE_instanced_arrays"))
{
mVertexAttribDivisorANGLE = (PFNGLVERTEXATTRIBDIVISORANGLEPROC)eglGetProcAddress("glVertexAttribDivisorANGLE");
mDrawArraysInstancedANGLE = (PFNGLDRAWARRAYSINSTANCEDANGLEPROC)eglGetProcAddress("glDrawArraysInstancedANGLE");
mDrawElementsInstancedANGLE = (PFNGLDRAWELEMENTSINSTANCEDANGLEPROC)eglGetProcAddress("glDrawElementsInstancedANGLE");
}
ASSERT_TRUE(mVertexAttribDivisorANGLE != NULL);
ASSERT_TRUE(mDrawArraysInstancedANGLE != NULL);
ASSERT_TRUE(mDrawElementsInstancedANGLE != NULL);
// Initialize the vertex and index vectors
GLfloat qvertex1[3] = {-quadRadius, quadRadius, 0.0f};
GLfloat qvertex2[3] = {-quadRadius, -quadRadius, 0.0f};
GLfloat qvertex3[3] = { quadRadius, -quadRadius, 0.0f};
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);
GLfloat coord1[2] = {0.0f, 0.0f};
GLfloat coord2[2] = {0.0f, 1.0f};
GLfloat coord3[2] = {1.0f, 1.0f};
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(),
mQuadVertices.begin() + mIndices[vertexIndex] * 3,
mQuadVertices.begin() + mIndices[vertexIndex] * 3 + 3);
}
for (size_t vertexIndex = 0; vertexIndex < 6; ++vertexIndex)
{
mNonIndexedVertices.insert(mNonIndexedVertices.end(),
mQuadVertices.begin() + mIndices[vertexIndex] * 3,
mQuadVertices.begin() + mIndices[vertexIndex] * 3 + 3);
}
// Tile a 2x2 grid of the tiles
for (float y = -1.0f + quadRadius; y < 1.0f - quadRadius; y += quadRadius * 3)
{
for (float x = -1.0f + quadRadius; x < 1.0f - quadRadius; x += quadRadius * 3)
{
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);
ASSERT_GL_NO_ERROR();
}
void setupDrawArraysTest(const std::string &vs)
{
const std::string fs = SHADER_SOURCE
(
precision mediump float;
void main()
{
gl_FragColor = vec4(1.0, 0, 0, 1.0);
}
);
mProgram = CompileProgram(vs, fs);
ASSERT_NE(0u, mProgram);
// Set the viewport
glViewport(0, 0, getWindowWidth(), getWindowHeight());
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// Use the program object
glUseProgram(mProgram);
}
void setupInstancedPointsTest()
{
mIndices.clear();
mIndices.push_back(0);
mIndices.push_back(1);
mIndices.push_back(2);
mIndices.push_back(3);
// clang-format off
const std::string vs = SHADER_SOURCE
(
attribute vec3 a_position;
attribute vec3 a_instancePos;
void main()
{
gl_Position = vec4(a_position.xyz, 1.0);
gl_Position = vec4(a_instancePos.xyz, 1.0);
gl_PointSize = 6.0;
}
);
const std::string fs = SHADER_SOURCE
(
precision mediump float;
void main()
{
gl_FragColor = vec4(1.0, 0, 0, 1.0);
}
);
// clang-format on
mProgram = CompileProgram(vs, fs);
ASSERT_NE(0u, mProgram);
// Set the viewport
glViewport(0, 0, getWindowWidth(), getWindowHeight());
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// Use the program object
glUseProgram(mProgram);
}
void runDrawArraysTest(GLint first, GLsizei count, GLsizei instanceCount, float *offset)
{
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
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
mVertexAttribDivisorANGLE(instancePosLoc, 1);
// Offset
GLint uniformLoc = glGetUniformLocation(mProgram, "u_offset");
ASSERT_NE(uniformLoc, -1);
glUniform3fv(uniformLoc, 1, offset);
// Do the instanced draw
mDrawArraysInstancedANGLE(GL_TRIANGLES, first, count, instanceCount);
ASSERT_GL_NO_ERROR();
}
virtual void runDrawElementsTest(std::string vs, bool shouldAttribZeroBeInstanced)
{
const std::string fs = SHADER_SOURCE
(
precision mediump float;
void main()
{
gl_FragColor = vec4(1.0, 0, 0, 1.0);
}
);
GLuint program = CompileProgram(vs, fs);
ASSERT_NE(program, 0u);
// 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
mVertexAttribDivisorANGLE(instancePosLoc, 1);
// Do the instanced draw
mDrawElementsInstancedANGLE(GL_TRIANGLES, static_cast<GLsizei>(mIndices.size()),
GL_UNSIGNED_SHORT, mIndices.data(),
static_cast<GLsizei>(mInstances.size()) / 3);
ASSERT_GL_NO_ERROR();
checkQuads();
}
void checkQuads()
{
// Check that various pixels are the expected color.
for (unsigned int quadIndex = 0; quadIndex < 4; ++quadIndex)
{
unsigned int baseOffset = quadIndex * 3;
int quadx = static_cast<int>(((mInstances[baseOffset + 0]) * 0.5f + 0.5f) * getWindowWidth());
int quady = static_cast<int>(((mInstances[baseOffset + 1]) * 0.5f + 0.5f) * getWindowHeight());
EXPECT_PIXEL_EQ(quadx, quady, 255, 0, 0, 255);
}
}
// Loaded entry points
PFNGLVERTEXATTRIBDIVISORANGLEPROC mVertexAttribDivisorANGLE;
PFNGLDRAWARRAYSINSTANCEDANGLEPROC mDrawArraysInstancedANGLE;
PFNGLDRAWELEMENTSINSTANCEDANGLEPROC mDrawElementsInstancedANGLE;
// Vertex data
std::vector<GLfloat> mQuadVertices;
std::vector<GLfloat> mNonIndexedVertices;
std::vector<GLfloat> mTexcoords;
std::vector<GLfloat> mInstances;
std::vector<GLushort> mIndices;
const GLfloat quadRadius = 0.30f;
GLuint mProgram;
GLuint mVertexBuffer;
};
class InstancingTestAllConfigs : public InstancingTest
{
protected:
InstancingTestAllConfigs() {}
};
class InstancingTestNo9_3 : public InstancingTest
{
protected:
InstancingTestNo9_3() {}
};
class InstancingTestPoints : public InstancingTest
{
protected:
InstancingTestPoints() {}
};
// This test uses a vertex shader with the first attribute (attribute zero) instanced.
// On D3D9 and D3D11 FL9_3, this triggers a special codepath that rearranges the input layout sent to D3D,
// to ensure that slot/stream zero of the input layout doesn't contain per-instance data.
TEST_P(InstancingTestAllConfigs, AttributeZeroInstanced)
{
const std::string vs = SHADER_SOURCE
(
attribute vec3 a_instancePos;
attribute vec3 a_position;
void main()
{
gl_Position = vec4(a_position.xyz + a_instancePos.xyz, 1.0);
}
);
runDrawElementsTest(vs, true);
}
// Same as AttributeZeroInstanced, but attribute zero is not instanced.
// This ensures the general instancing codepath (i.e. without rearranging the input layout) works as expected.
TEST_P(InstancingTestAllConfigs, AttributeZeroNotInstanced)
{
const std::string vs = SHADER_SOURCE
(
attribute vec3 a_position;
attribute vec3 a_instancePos;
void main()
{
gl_Position = vec4(a_position.xyz + a_instancePos.xyz, 1.0);
}
);
runDrawElementsTest(vs, false);
}
// Tests that the "first" parameter to glDrawArraysInstancedANGLE is only an offset into
// the non-instanced vertex attributes.
TEST_P(InstancingTestNo9_3, DrawArraysWithOffset)
{
const std::string vs = SHADER_SOURCE
(
attribute vec3 a_position;
attribute vec3 a_instancePos;
uniform vec3 u_offset;
void main()
{
gl_Position = vec4(a_position.xyz + a_instancePos.xyz + u_offset, 1.0);
}
);
setupDrawArraysTest(vs);
float offset1[3] = { 0, 0, 0 };
runDrawArraysTest(0, 6, 2, offset1);
float offset2[3] = { 0.0f, 1.0f, 0 };
runDrawArraysTest(6, 6, 2, offset2);
checkQuads();
}
// This test verifies instancing with GL_POINTS with glDrawArraysInstanced works.
// On D3D11 FL9_3, this triggers a special codepath that emulates instanced points rendering.
TEST_P(InstancingTestPoints, DrawArrays)
{
// Disable D3D11 SDK Layers warnings checks, see ANGLE issue 667 for details
// On Win7, the D3D SDK Layers emits a false warning for these tests.
// This doesn't occur on Windows 10 (Version 1511) though.
ignoreD3D11SDKLayersWarnings();
setupInstancedPointsTest();
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
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
mVertexAttribDivisorANGLE(instancePosLoc, 1);
// Do the instanced draw
mDrawArraysInstancedANGLE(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.
// On D3D11 FL9_3, this triggers a special codepath that emulates instanced points rendering.
TEST_P(InstancingTestPoints, DrawElements)
{
// Disable D3D11 SDK Layers warnings checks, see ANGLE issue 667 for details
// On Win7, the D3D SDK Layers emits a false warning for these tests.
// This doesn't occur on Windows 10 (Version 1511) though.
ignoreD3D11SDKLayersWarnings();
setupInstancedPointsTest();
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
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
mVertexAttribDivisorANGLE(instancePosLoc, 1);
// Do the instanced draw
mDrawElementsInstancedANGLE(GL_POINTS, static_cast<GLsizei>(mIndices.size()), GL_UNSIGNED_SHORT,
mIndices.data(), static_cast<GLsizei>(mInstances.size()) / 3);
ASSERT_GL_NO_ERROR();
checkQuads();
}
// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
// We test on D3D9 and D3D11 9_3 because they use special codepaths when attribute zero is instanced, unlike D3D11.
ANGLE_INSTANTIATE_TEST(InstancingTestAllConfigs,
ES2_D3D9(),
ES2_D3D11(),
ES2_D3D11_FL9_3(),
ES2_OPENGL(),
ES2_OPENGLES());
// TODO(jmadill): Figure out the situation with DrawInstanced on FL 9_3
ANGLE_INSTANTIATE_TEST(InstancingTestNo9_3, ES2_D3D9(), ES2_D3D11());
ANGLE_INSTANTIATE_TEST(InstancingTestPoints, ES2_D3D11(), ES2_D3D11_FL9_3());
|