gl-vertex-attrib-zero-issues.html (3778B)
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>WebGL Enable Vertex Attrib Zero Test</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="example" width="50" height="50"> 18 </canvas> 19 <div id="description"></div> 20 <div id="console"></div> 21 <script id="vshader" type="x-shader/x-vertex"> 22 attribute vec4 vPosition; 23 void main() 24 { 25 gl_Position = vPosition; 26 } 27 </script> 28 29 <script id="fshader" type="x-shader/x-fragment"> 30 void main() 31 { 32 gl_FragColor = vec4(0.0,1.0,0.0,1.0); 33 } 34 </script> 35 36 <script> 37 "use strict"; 38 description("Test some of the issues of the difference between attrib 0 on OpenGL vs WebGL"); 39 debug(""); 40 var wtu = WebGLTestUtils; 41 var gl = wtu.create3DContext("example"); 42 var g_program; 43 var g_attribLocation; 44 function setup(attribIndex) { 45 var program = wtu.setupProgram( 46 gl, ['vshader', 'fshader'], ['vPosition'], [attribIndex]); 47 g_program = program; 48 g_attribLocation = attribIndex; 49 shouldBe("g_attribLocation", "gl.getAttribLocation(g_program, 'vPosition')"); 50 return program; 51 } 52 53 function setupVerts(numVerts) { 54 var verts = [ 55 1.0, 1.0, 0.0, 56 -1.0, 1.0, 0.0, 57 -1.0, -1.0, 0.0, 58 1.0, 1.0, 0.0, 59 -1.0, -1.0, 0.0, 60 1.0, -1.0, 0.0 61 ]; 62 var positions = new Float32Array(numVerts * 3); 63 var indices = new Uint16Array(numVerts); 64 for (var ii = 0; ii < numVerts; ++ii) { 65 var ndx = ii % 6; 66 var dst = ii * 3; 67 var src = ndx * 3; 68 for (var jj = 0; jj < 3; ++jj) { 69 positions[dst + jj] = verts[src + jj]; 70 } 71 indices[ii] = ii; 72 } 73 var vertexObject = gl.createBuffer(); 74 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); 75 gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW); 76 var indexBuffer = gl.createBuffer(); 77 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer); 78 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW); 79 } 80 81 var p0 = setup(0); 82 var p3 = setup(3); 83 setupVerts(60000); 84 85 for (var ii = 0; ii < 5; ++ii) { 86 // test drawing with attrib 0 87 gl.useProgram(p0); 88 gl.enableVertexAttribArray(0); 89 gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0); 90 gl.clear(gl.COLOR_BUFFER_BIT); 91 gl.drawElements(gl.TRIANGLES, 60000, gl.UNSIGNED_SHORT, 0); 92 wtu.glErrorShouldBe( 93 gl, gl.NO_ERROR, 94 "drawing using attrib 0 with 6 verts"); 95 wtu.checkCanvas(gl, [0, 255, 0, 255], "canvas should be green"); 96 gl.disableVertexAttribArray(0); 97 98 // test drawing without attrib 0 99 gl.useProgram(p3); 100 gl.enableVertexAttribArray(3); 101 gl.vertexAttribPointer(3, 3, gl.FLOAT, false, 0, 0); 102 gl.clear(gl.COLOR_BUFFER_BIT); 103 gl.drawArrays(gl.TRIANGLES, 0, 60000); 104 wtu.glErrorShouldBe( 105 gl, gl.NO_ERROR, 106 "drawing using attrib 3 with 60000 verts"); 107 wtu.checkCanvas(gl, [0, 255, 0, 255], "canvas should be green"); 108 gl.disableVertexAttribArray(3); 109 110 // This second test of drawing without attrib0 uncovered a bug in chrome 111 // where after the draw without attrib0 the attrib 0 emulation code disabled 112 // attrib 0 and it was never re-enabled so this next draw failed. 113 gl.useProgram(p3); 114 gl.enableVertexAttribArray(3); 115 gl.clear(gl.COLOR_BUFFER_BIT); 116 gl.drawElements(gl.TRIANGLES, 60000, gl.UNSIGNED_SHORT, 0); 117 wtu.glErrorShouldBe( 118 gl, gl.NO_ERROR, 119 "drawing using attrib 3 with 60000 verts"); 120 wtu.checkCanvas(gl, [0, 255, 0, 255], "canvas should be green"); 121 gl.disableVertexAttribArray(3); 122 } 123 124 125 var successfullyParsed = true; 126 </script> 127 <script src="../../js/js-test-post.js"></script> 128 129 </body> 130 </html>