summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/buffers/buffer-data-dynamic-delay.html
blob: 8e41a687bb792668d377d19d455ca694e323f94d (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
<!--
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>bufferData with DYNAMIC_DRAW and delay between updating data</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>
<canvas id="canvas" width="50" height="50"></canvas>
<div id="description"></div>
<div id="console"></div>

<script id="vshader" type="x-shader/x-vertex">
    attribute vec2 a_position;
    attribute vec2 a_color;
    varying vec2 v_color;
    void main()
    {
        gl_Position = vec4(a_position, 0.0, 1.0);
        v_color = a_color;
    }
</script>
<script id="fshader" type="x-shader/x-fragment">
    precision mediump float;
    varying vec2 v_color;
    void main()
    {
        gl_FragColor = vec4(v_color, 0.0, 1.0);
    }
</script>

<script>
"use strict";
description("Verifies that bufferData with DYNAMIC_DRAW updates the vertex attribute when there is a significant delay between updating the buffer.");
var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("canvas");
var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["a_position", "a_color"]);

// Initialize position vertex attribute to draw a square covering the entire canvas.
var positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
    -1.0,  1.0,
     1.0,  1.0,
    -1.0, -1.0,
     1.0, -1.0
    ]), gl.DYNAMIC_DRAW);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);

// Initialize color vertex attribute to red.
var colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
    1.0, 0.0,
    1.0, 0.0,
    1.0, 0.0,
    1.0, 0.0,
    ]), gl.DYNAMIC_DRAW);
gl.enableVertexAttribArray(1);
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);

wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No error after setup");

// Fill the canvas with red
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No error after first drawArrays");

wtu.checkCanvasRect(gl, 0, 0, 50, 50, [255, 0, 0, 255], "Canvas should be red after the first drawArrays");

// With the buffer set to DYNAMIC_DRAW, Angle internally changes the storage type of the vertex attribute from DYNAMIC to DIRECT
// if the buffer has not been updated after ~4-5 draw calls. When the buffer is eventually updated, the vertex attribute
// is updated back to DYNAMIC, but there was a bug in Angle where the data is not marked as dirty. The result is that the
// vertex data is not updated with the new buffer data. This test verifies that the vertex data is updated.
var iteration = 0;
function draw() {
    // Draw 10 times to ensure that the vertex attribute storage type is changed.
    if (iteration < 10) {
        gl.drawArrays(gl.TRIANGLE_STRIP, 0, 2);
        requestAnimationFrame(draw);
    }
    else {
        // Update the buffer bound to the color vertex attribute to green and draw.
        gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
            0.0, 1.0,
            0.0, 1.0,
            0.0, 1.0,
            0.0, 1.0,
            ]), gl.DYNAMIC_DRAW);
        gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No error after last drawArrays");

        wtu.checkCanvasRect(gl, 0, 0, 50, 50, [0, 255, 0, 255], "Canvas should be green after 10 frames");

        finishTest();
    }

    iteration++;
}

requestAnimationFrame(draw);

</script>
</body>
</html>