buffer-uninitialized.html (2851B)
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 <!DOCTYPE html> 7 <html> 8 <head> 9 <meta charset="utf-8"> 10 <link rel="stylesheet" href="../../resources/js-test-style.css"/> 11 <script src="../../js/js-test-pre.js"></script> 12 <script src="../../js/webgl-test-utils.js"></script> 13 </head> 14 <body> 15 <div id="description"></div> 16 <div id="console"></div> 17 18 <canvas id="canvas" width="1" height="1"></canvas> 19 20 <script id="vshader" type="x-shader/x-vertex"> 21 attribute float a_vertex; 22 void main() 23 { 24 gl_Position = a_vertex == 0.0 ? vec4(9, 9, 9, 1) : vec4(0.5, 0.5, 0.5, 1); 25 } 26 </script> 27 28 <script id="fshader" type="x-shader/x-fragment"> 29 void main() 30 { 31 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 32 } 33 </script> 34 35 <script> 36 "use strict"; 37 description("Tests that uninitialized WebGLBuffers are zeroed out"); 38 39 var wtu = WebGLTestUtils; 40 var gl = wtu.create3DContext(document.getElementById("canvas")); 41 var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["a_vertex"]); 42 shouldBeTrue("program != null"); 43 44 var TEST_LENGTH = 1024; 45 var TEST_BUFSIZE = TEST_LENGTH * 4; 46 var data = new Float32Array(TEST_LENGTH / 4); // this array is zeroed 47 48 var indices = new Uint16Array(TEST_LENGTH); 49 for (var i = 0; i < TEST_LENGTH; i++) { 50 indices[i] = i; 51 } 52 53 gl.clearColor(0, 1, 0, 1); 54 55 function test(initFunction) { 56 var uninitializedBuffer = gl.createBuffer(); 57 gl.bindBuffer(gl.ARRAY_BUFFER, uninitializedBuffer); 58 initFunction(); 59 60 var elements = gl.createBuffer(); 61 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elements); 62 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW); 63 64 gl.useProgram(program); 65 var vertexLoc = gl.getAttribLocation(program, "a_vertex"); 66 gl.vertexAttribPointer(vertexLoc, 1, gl.FLOAT, gl.FALSE, 0, 0); 67 gl.enableVertexAttribArray(vertexLoc); 68 69 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "no error should result from setup"); 70 71 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 72 gl.drawElements(gl.POINTS, TEST_LENGTH, gl.UNSIGNED_SHORT, 0); 73 wtu.checkCanvasRect(gl, 0, 0, 1, 1, [0, 255, 0, 255], "buffer should be initialized to zero"); 74 75 gl.deleteBuffer(uninitializedBuffer); 76 } 77 78 var REPETITIONS = 50; 79 80 var j; 81 debug(""); 82 debug("testing bufferData(..., size, ...)"); 83 for (j = 0; j < REPETITIONS; j++) { 84 test(function() { 85 gl.bufferData(gl.ARRAY_BUFFER, TEST_BUFSIZE, gl.STATIC_DRAW); 86 }); 87 } 88 89 debug(""); 90 debug("testing bufferSubData(..., offset, data) of uninitialized buffer"); 91 for (j = 0; j < REPETITIONS; j++) { 92 test(function() { 93 gl.bufferData(gl.ARRAY_BUFFER, TEST_BUFSIZE, gl.STATIC_DRAW); 94 // bufferSubData the second quarter of the buffer 95 gl.bufferSubData(gl.ARRAY_BUFFER, TEST_BUFSIZE / 4, data); 96 }); 97 } 98 99 var successfullyParsed = true; 100 </script> 101 102 <script src="../../js/js-test-post.js"></script> 103 </body> 104 </html>