buffer-data-dynamic-delay.html (3782B)
1 <!-- 2 Copyright (c) 2019 The Khronos Group Inc. 3 Use of this source code is governed by an MIT-style license that can be 4 found in the LICENSE.txt file. 5 --> 6 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <meta charset="utf-8"> 11 <title>bufferData with DYNAMIC_DRAW and delay between updating data</title> 12 <link rel="stylesheet" href="../../resources/js-test-style.css"/> 13 <script src="../../js/js-test-pre.js"></script> 14 <script src="../../js/webgl-test-utils.js"> </script> 15 </head> 16 <body> 17 <canvas id="canvas" width="50" height="50"></canvas> 18 <div id="description"></div> 19 <div id="console"></div> 20 21 <script id="vshader" type="x-shader/x-vertex"> 22 attribute vec2 a_position; 23 attribute vec2 a_color; 24 varying vec2 v_color; 25 void main() 26 { 27 gl_Position = vec4(a_position, 0.0, 1.0); 28 v_color = a_color; 29 } 30 </script> 31 <script id="fshader" type="x-shader/x-fragment"> 32 precision mediump float; 33 varying vec2 v_color; 34 void main() 35 { 36 gl_FragColor = vec4(v_color, 0.0, 1.0); 37 } 38 </script> 39 40 <script> 41 "use strict"; 42 description("Verifies that bufferData with DYNAMIC_DRAW updates the vertex attribute when there is a significant delay between updating the buffer."); 43 var wtu = WebGLTestUtils; 44 var gl = wtu.create3DContext("canvas"); 45 var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["a_position", "a_color"]); 46 47 // Initialize position vertex attribute to draw a square covering the entire canvas. 48 var positionBuffer = gl.createBuffer(); 49 gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); 50 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 51 -1.0, 1.0, 52 1.0, 1.0, 53 -1.0, -1.0, 54 1.0, -1.0 55 ]), gl.DYNAMIC_DRAW); 56 gl.enableVertexAttribArray(0); 57 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); 58 59 // Initialize color vertex attribute to red. 60 var colorBuffer = gl.createBuffer(); 61 gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); 62 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 63 1.0, 0.0, 64 1.0, 0.0, 65 1.0, 0.0, 66 1.0, 0.0, 67 ]), gl.DYNAMIC_DRAW); 68 gl.enableVertexAttribArray(1); 69 gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0); 70 71 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No error after setup"); 72 73 // Fill the canvas with red 74 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); 75 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No error after first drawArrays"); 76 77 wtu.checkCanvasRect(gl, 0, 0, 50, 50, [255, 0, 0, 255], "Canvas should be red after the first drawArrays"); 78 79 // With the buffer set to DYNAMIC_DRAW, Angle internally changes the storage type of the vertex attribute from DYNAMIC to DIRECT 80 // if the buffer has not been updated after ~4-5 draw calls. When the buffer is eventually updated, the vertex attribute 81 // 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 82 // vertex data is not updated with the new buffer data. This test verifies that the vertex data is updated. 83 var iteration = 0; 84 function draw() { 85 // Draw 10 times to ensure that the vertex attribute storage type is changed. 86 if (iteration < 10) { 87 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 2); 88 requestAnimationFrame(draw); 89 } 90 else { 91 // Update the buffer bound to the color vertex attribute to green and draw. 92 gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); 93 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 94 0.0, 1.0, 95 0.0, 1.0, 96 0.0, 1.0, 97 0.0, 1.0, 98 ]), gl.DYNAMIC_DRAW); 99 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); 100 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No error after last drawArrays"); 101 102 wtu.checkCanvasRect(gl, 0, 0, 50, 50, [0, 255, 0, 255], "Canvas should be green after 10 frames"); 103 104 finishTest(); 105 } 106 107 iteration++; 108 } 109 110 requestAnimationFrame(draw); 111 112 </script> 113 </body> 114 </html>