summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/extra/webgl-drawelements-validation.html
blob: 74f15608a38693d5e8f9af4388a7e48c6d8329a5 (plain)
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
<!--

/*
** Copyright (c) 2014 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>
<title>Micro-benchmark for WebGL drawElements index validation</title>
<style>
canvas {
    border: 1px solid #000;
}
</style>
<script src="../js/webgl-test-utils.js"></script>
<script type="application/javascript">
"use strict";

var wtu = WebGLTestUtils;

var totalTimeTest1 = 0;
var totalTimeTest2 = 0;
var totalTimeTest3 = 0;
var iterationsLeft = 10; // How many times to run the full test.
var indexCount = 500001; // Divisible by 3.

var indices = [];
for (var i = 0; i < indexCount - 1; ++i) {
    indices.push(0);
    indices.push(1);
    indices.push(2);
    indices.push(3);
}
indices.push(4);

var fullIndicesArray = new Uint16Array(indices);

var drawIterations = 50;

var errorsCorrect = true;

var log = function(msg) {
    console.log(msg);
    var p = document.createElement('p');
    p.textContent = msg;
    document.body.appendChild(p);
};

var runTestIteration = function() {
    var canvas = document.createElement('canvas');
    canvas.width = 10;
    canvas.height = 10;
    var gl = wtu.create3DContext(canvas);
    document.body.appendChild(canvas);

    var location = 0;
    wtu.setupSimpleColorProgram(gl, location);

    var verts = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, verts);
    var vertData = new Float32Array([-1, -1,
                                     -1, 1,
                                     1, -1,
                                     1, 1]);
    gl.bufferData(gl.ARRAY_BUFFER, vertData, gl.STATIC_DRAW);
    gl.vertexAttribPointer(location, 2, gl.FLOAT, false, 0, 0);
    gl.enableVertexAttribArray(location);

    var indexBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, fullIndicesArray, gl.DYNAMIC_DRAW);
    gl.finish();

    var measureTime = function(f) {
        var startTime = new Date().getTime();
        f();
        var error = gl.getError();
        var endTime = new Date().getTime();
        errorsCorrect = errorsCorrect && error == gl.INVALID_OPERATION;
        return endTime - startTime;
    };

    // The buffer has at least one out-of-range index from the start,
    // so only validation will happen, not drawing.

    totalTimeTest1 += measureTime(function() {
        for (var i = 0; i < drawIterations; ++i) {
            // Change all data, which will cause complete revalidation of the index buffer.
            gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, fullIndicesArray);
            gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT, 0);
        }
    });

    totalTimeTest2 += measureTime(function() {
        for (var i = 0; i < drawIterations; ++i) {
            // Change only one index and vary the amount of referenced indices.
            // These should not be a big problem to a smart implementation.
            gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, Math.floor(indices.length / 2), new Uint16Array([i + 5]));
            gl.drawElements(gl.TRIANGLES, indices.length - i * 3, gl.UNSIGNED_SHORT, 0);
        }
    });

    totalTimeTest3 += measureTime(function() {
        for (var i = 0; i < drawIterations; ++i) {
            // Change data at two indices to cause completely revalidating the index buffer in
            // current implementations in Chrome and Firefox (as of March 17th 2014).
            gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, new Uint16Array([i + 5]));
            gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, indices.length - 1, new Uint16Array([i + 5]));
            gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT, 0);
        }
    });

    setTimeout(function() {
        var lose = gl.getExtension('WEBGL_lose_context');
        lose.loseContext();
    }, 40);
};

var runTest = function() {
    if (iterationsLeft > 0) {
        runTestIteration();
        --iterationsLeft;
        setTimeout(runTest, 500);
    } else {
        log("Validation returned correct results: " + errorsCorrect);
        log('1. Time spent on full buffer updates: ' + totalTimeTest1 + ' ms');
        log('Indices uploaded and referenced by draw calls processed: ' + Math.round(indices.length * drawIterations / totalTimeTest1) + ' / ms');
        log('2. Time spent on validating single index updates while range referenced also changes on every draw call: ' + totalTimeTest2 + ' ms');
        log('Indices referenced by draw calls handled: ' + Math.round(indices.length * drawIterations / totalTimeTest2) + ' / ms');
        log('3. Time spent on validating single index updates at each end of the buffer (worst case for Firefox implementation as of March 2014, not reflective of real world performance): ' + totalTimeTest3 + ' ms');
        log('Indices referenced by draw calls handled: ' + Math.round(indices.length * drawIterations / totalTimeTest3) + ' / ms');
    }
};
</script>
</head>
<body onload="setTimeout(runTest, 100)">
<h1>Micro-benchmark for WebGL drawElements index validation</h1>
<p>Note that these test cases are completely artificial, and their results only very rough indicators of the performance of a specific part of the system.</p>
<p>The benchmark does not perform any drawing, but rather measures the time the browser takes to upload indices and to check if there are out-of-range indices.</p>
</body>
</html>