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
|
<!--
/*
** Copyright (c) 2016 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL Instanced Arrays Conformance Tests</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/desktop-gl-constants.js"></script>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
</head>
<body>
<div id="description"></div>
<canvas id="canvas" style="width: 128px; height: 128px;"> </canvas>
<div id="console"></div>
<script id="outputVertexShader" type="x-shader/x-vertex">#version 300 es
in highp vec2 aPosition;
in highp float aOffset;
in highp float aColor;
out mediump float vColor;
void main() {
gl_Position = vec4(aPosition, 0.0, 1.0) + vec4(aOffset, 0.0, 0.0, 0.0);
vColor = aColor;
}
</script>
<script id="outputFragmentShader" type="x-shader/x-fragment">#version 300 es
layout(location = 0) out mediump vec4 oColor;
in mediump float vColor;
void main() {
oColor = vec4(vColor, 0.0, 0.0, 1.0);
}
</script>
<script>
"use strict";
description("This test verifies a bug related with instanced rendering on Mac AMD.");
debug("http://crbug.com/645298");
debug("");
var wtu = WebGLTestUtils;
var canvas = document.getElementById("canvas");
var gl = wtu.create3DContext(canvas, null, 2);
// The second and fourth test cases fail - it seems if the divisor doesn't change,
// the next instanced draw call behaves incorrectly.
// Also note that if we don't perform a readPixels (in wtu.checkCanvasRect), the bug
// isn't triggered.
var testCases = [
{ instanceCount: 8, divisor: 4 },
{ instanceCount: 6, divisor: 4 },
{ instanceCount: 6, divisor: 3 },
{ instanceCount: 8, divisor: 3 },
];
if (!gl) {
testFailed("WebGL context does not exist");
} else {
testPassed("WebGL context exists");
for (var ii = 0; ii < testCases.length; ++ii) {
runDrawArraysTest(testCases[ii].instanceCount, testCases[ii].divisor);
}
for (var ii = 0; ii < testCases.length; ++ii) {
runDrawElementsTest(testCases[ii].instanceCount, testCases[ii].divisor);
}
}
function runDrawArraysTest(instanceCount, divisor) {
debug("");
debug("Testing drawArraysInstanced: instanceCount = " + instanceCount + ", divisor = " + divisor);
gl.viewport(0, 0, canvas.width, canvas.height);
var vao = gl.createVertexArray();
gl.bindVertexArray(vao);
var program = wtu.setupProgram(gl, ["outputVertexShader", "outputFragmentShader"]);
var positionLoc = gl.getAttribLocation(program, "aPosition");
var offsetLoc = gl.getAttribLocation(program, "aOffset");
var colorLoc = gl.getAttribLocation(program, "aColor");
if (!program || positionLoc < 0 || offsetLoc < 0 || colorLoc < 0) {
testFailed("Set up program failed");
return;
}
testPassed("Set up program succeeded");
var scale = 1.0 / instanceCount;
gl.enableVertexAttribArray(positionLoc);
gl.vertexAttribDivisor(positionLoc, 0);
var positions = new Float32Array([
1.0 * scale, 1.0,
-1.0 * scale, 1.0,
-1.0 * scale, -1.0,
1.0 * scale, 1.0,
-1.0 * scale, -1.0,
1.0 * scale, -1.0,
]);
var positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
gl.vertexAttribPointer(positionLoc, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(offsetLoc);
gl.vertexAttribDivisor(offsetLoc, 1);
var offsets = new Float32Array(instanceCount);
for (var ii = 0; ii < instanceCount; ++ii) {
offsets[ii] = scale * (1 - instanceCount + ii * 2);
}
var offsetBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, offsetBuffer);
gl.bufferData(gl.ARRAY_BUFFER, offsets, gl.STATIC_DRAW);
gl.vertexAttribPointer(offsetLoc, 1, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(colorLoc);
gl.vertexAttribDivisor(colorLoc, divisor);
var colorCount = instanceCount / divisor;
if ((instanceCount % divisor) != 0)
colorCount++;
var colors = new Float32Array(colorCount);
for (var ii = 0; ii < colorCount; ++ii) {
colors[ii] = 1.0 / colorCount * (ii + 1);
}
var colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW);
gl.vertexAttribPointer(colorLoc, 1, gl.FLOAT, false, 0, 0);
gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, instanceCount);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "drawArraysInstanced should succeed");
var colorIndex = -1;
for (var ii = 0; ii < instanceCount; ++ii) {
if ((ii % divisor) == 0)
colorIndex++;
var refColor = [ Math.floor(colors[colorIndex] * 255), 0, 0, 255 ];
wtu.checkCanvasRect(gl, Math.floor(canvas.width / instanceCount * ii) + 1, 0, 1, canvas.height, refColor,
"instance " + ii + " should be " + refColor, 2);
}
gl.deleteBuffer(positionBuffer);
gl.deleteBuffer(offsetBuffer);
gl.deleteBuffer(colorBuffer);
gl.deleteProgram(program);
gl.deleteVertexArray(vao);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.useProgram(null);
gl.bindVertexArray(null);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "clean up should succeed");
}
function runDrawElementsTest(instanceCount, divisor) {
debug("");
debug("Testing drawElementsInstanced: instanceCount = " + instanceCount + ", divisor = " + divisor);
gl.viewport(0, 0, canvas.width, canvas.height);
var vao = gl.createVertexArray();
gl.bindVertexArray(vao);
var program = wtu.setupProgram(gl, ["outputVertexShader", "outputFragmentShader"]);
var positionLoc = gl.getAttribLocation(program, "aPosition");
var offsetLoc = gl.getAttribLocation(program, "aOffset");
var colorLoc = gl.getAttribLocation(program, "aColor");
if (!program || positionLoc < 0 || offsetLoc < 0 || colorLoc < 0) {
testFailed("Set up program failed");
return;
}
testPassed("Set up program succeeded");
var scale = 1.0 / instanceCount;
gl.enableVertexAttribArray(positionLoc);
gl.vertexAttribDivisor(positionLoc, 0);
var positions = new Float32Array([
1.0 * scale, 1.0,
-1.0 * scale, 1.0,
-1.0 * scale, -1.0,
1.0 * scale, -1.0,
]);
var positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
gl.vertexAttribPointer(positionLoc, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(offsetLoc);
gl.vertexAttribDivisor(offsetLoc, 1);
var offsets = new Float32Array(instanceCount);
for (var ii = 0; ii < instanceCount; ++ii) {
offsets[ii] = scale * (1 - instanceCount + ii * 2);
}
var offsetBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, offsetBuffer);
gl.bufferData(gl.ARRAY_BUFFER, offsets, gl.STATIC_DRAW);
gl.vertexAttribPointer(offsetLoc, 1, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(colorLoc);
gl.vertexAttribDivisor(colorLoc, divisor);
var colorCount = instanceCount / divisor;
if ((instanceCount % divisor) != 0)
colorCount++;
var colors = new Float32Array(colorCount);
for (var ii = 0; ii < colorCount; ++ii) {
colors[ii] = 1.0 / colorCount * (ii + 1);
}
var colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW);
gl.vertexAttribPointer(colorLoc, 1, gl.FLOAT, false, 0, 0);
var indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
var indices = new Uint16Array([0, 1, 2, 0, 2, 3]);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
gl.drawElementsInstanced(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0, instanceCount);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "drawElementsInstanced should succeed");
var colorIndex = -1;
for (var ii = 0; ii < instanceCount; ++ii) {
if ((ii % divisor) == 0)
colorIndex++;
var refColor = [ Math.floor(colors[colorIndex] * 255), 0, 0, 255 ];
wtu.checkCanvasRect(gl, Math.floor(canvas.width / instanceCount * ii) + 1, 0, 1, canvas.height, refColor,
"instance " + ii + " should be " + refColor, 2);
}
gl.deleteBuffer(positionBuffer);
gl.deleteBuffer(offsetBuffer);
gl.deleteBuffer(colorBuffer);
gl.deleteBuffer(indexBuffer);
gl.deleteProgram(program);
gl.deleteVertexArray(vao);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
gl.useProgram(null);
gl.bindVertexArray(null);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "clean up should succeed");
}
debug("");
var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>
</body>
</html>
|