summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/extra/getbuffersubdata-nonblocking-benchmark.html
blob: e6233ea83f62c8bf6dfabb1646d1d3523a00e6de (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
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
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>getBufferSubData non-blocking test</title>
<link rel="stylesheet" href="../resources/js-test-style.css"/>
<script src="../js/js-test-pre.js"></script>
<script src="../js/webgl-test-utils.js"> </script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description("Test that getBufferSubData is non-blocking when used with fenceSync");

var wtu = WebGLTestUtils;

var gl = wtu.create3DContext(undefined, undefined, 2);

const srcData = new Uint8Array([ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]);
const zeroData = new Uint8Array(8);

const srcBuffer = gl.createBuffer();
gl.bindBuffer(gl.COPY_READ_BUFFER, srcBuffer);
gl.bufferData(gl.COPY_READ_BUFFER, srcData, gl.STATIC_DRAW);

const readbackBuffer = gl.createBuffer();
gl.bindBuffer(gl.COPY_WRITE_BUFFER, readbackBuffer);
gl.bufferData(gl.COPY_WRITE_BUFFER, 8, gl.STREAM_READ);

// unrelated buffers for tests
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer()); // used as copy dst
gl.bufferData(gl.ARRAY_BUFFER, 8, gl.STATIC_DRAW);
gl.bindBuffer(gl.UNIFORM_BUFFER, gl.createBuffer()); // used as copy src
gl.bufferData(gl.UNIFORM_BUFFER, 8, gl.STATIC_DRAW);

const dest = new Uint8Array(8);

// Makes a new "resolvable" Promise
function resolvable() {
    let resolve;
    const promise = new Promise(res => { resolve = res; });
    promise.resolve = resolve;
    return promise;
}

function fence() {
    const promise = resolvable();

    const sync = gl.fenceSync(gl.SYNC_GPU_COMMANDS_COMPLETE, 0);
    gl.flush();
    function check() {
        const status = gl.clientWaitSync(sync, 0, 0);
        if (status == gl.ALREADY_SIGNALED || status == gl.CONDITION_SATISFIED) {
            gl.deleteSync(sync);
            promise.resolve();
        } else {
            setTimeout(check, 0);
        }
    }
    setTimeout(check, 0);

    return promise;
}

function writeToReadbackBuffer() {
    gl.copyBufferSubData(gl.COPY_READ_BUFFER, gl.COPY_WRITE_BUFFER, 0, 0, 8);
}

function timedGetBufferSubData() {
    dest.fill(0);
    const t0 = performance.now();
    gl.getBufferSubData(gl.COPY_WRITE_BUFFER, 0, dest);
    return (performance.now() - t0);
}

function timeBlockingReadback() {
    const promise = resolvable();
    setTimeout(() => {
        writeToReadbackBuffer();
        const tBlocking = timedGetBufferSubData();
        const tLatency = tBlocking;
        promise.resolve({latency: tLatency, blocking: tBlocking});
    }, 0);
    return promise;
}

function timeNonblockingReadback() {
    writeToReadbackBuffer();
    const tLatency0 = performance.now();
    return fence().then(() => {
        const tBlocking = timedGetBufferSubData();
        const tLatency = performance.now() - tLatency0;
        return {latency: tLatency, blocking: tBlocking};
    });
}

function timeReadbackWithUnrelatedCopy() {
    writeToReadbackBuffer();
    const tLatency0 = performance.now();
    const f = fence();
    // copy to a buffer unrelated to the readback
    gl.copyBufferSubData(gl.COPY_READ_BUFFER, gl.ARRAY_BUFFER, 0, 0, 8);
    return f.then(() => {
        const tBlocking = timedGetBufferSubData();
        const tLatency = performance.now() - tLatency0;
        return {latency: tLatency, blocking: tBlocking};
    });
}

function timeReadbackInterrupted() {
    writeToReadbackBuffer();
    const tLatency0 = performance.now();
    const f = fence();
    // interrupt the readback by inserting another write
    gl.copyBufferSubData(gl.UNIFORM_BUFFER, gl.COPY_WRITE_BUFFER, 0, 0, 8);
    return f.then(() => {
        const tBlocking = timedGetBufferSubData();
        const tLatency = performance.now() - tLatency0;
        return {latency: tLatency, blocking: tBlocking};
    });
}

function computeMean(timings) {
    let total = 0;
    for (let i = 0; i < timings.length; ++i) {
        total += timings[i];
    }
    return total / timings.length;
}

function measureMean(fn, iterations) {
    const timingsLatency = Array(iterations);
    const timingsBlocking = Array(iterations);

    // Chain together `iterations` promises to call `fn` sequentially.
    let promise = Promise.resolve();
    for (let i = 0; i < iterations; ++i) {
        promise = promise
            .then(fn)
            .then(t => {
                timingsLatency[i] = t.latency;
                timingsBlocking[i] = t.blocking;
            });
    }

    return promise.then(() => {
        const meanLatency = computeMean(timingsLatency);
        const meanBlocking = computeMean(timingsBlocking);
        return { latency: meanLatency, blocking: meanBlocking };
    });
}

let t_blocking, t_nonblocking;
let t_unrelated;
let t_interrupted;
Promise.resolve()
    .then(() => {
        let iterations = 500;
        debug(`blocking readback: mean over ${iterations} iterations...`);
        return measureMean(timeBlockingReadback, iterations);
    })
    .then(t => {
        t_blocking = t;
        debug(`... latency = ${t.latency}ms, blocking = ${t.blocking}ms`);
    })
    .then(() => shouldBeTrue("areArraysEqual(dest, srcData)"))

    .then(() => debug(""))
    .then(() => {
        let iterations = 500;
        debug(`nonblocking readback: mean over ${iterations} iterations...`);
        return measureMean(timeNonblockingReadback, iterations);
    })
    .then(t => {
        t_nonblocking = t;
        debug(`... latency = ${t.latency}ms, blocking = ${t.blocking}ms`);
    })
    .then(() => shouldBeTrue("areArraysEqual(dest, srcData)"))

    .then(() => debug(""))
    .then(() => {
        let iterations = 500;
        debug(`readback interrupted by unrelated read from copy source: mean over ${iterations} iterations...`);
        return measureMean(timeReadbackWithUnrelatedCopy, iterations);
    })
    .then(t => {
        t_unrelated = t;
        debug(`... latency = ${t.latency}ms, blocking = ${t.blocking}ms`);
    })
    .then(() => shouldBeTrue("areArraysEqual(dest, srcData)"))

    .then(() => debug(""))
    .then(() => {
        let iterations = 500;
        debug(`readback interrupted by write to readback source: mean over ${iterations} iterations...`);
        return measureMean(timeReadbackInterrupted, iterations);
    })
    .then(t => {
        t_interrupted = t;
        debug(`... latency = ${t.latency}ms, blocking = ${t.blocking}ms`);
    })
    .then(() => shouldBeTrue("areArraysEqual(dest, zeroData)"))

    .then(() => {
        debug("");
        shouldBeTrue("t_nonblocking.blocking < t_blocking.blocking");
        shouldBeTrue("t_unrelated.blocking < t_blocking.blocking");
        shouldBeTrue("t_nonblocking.blocking < t_interrupted.blocking");
    })
    .then(finishTest);

var successfullyParsed = true;
</script>
</body>
</html>