test_without_index_validation.html (2168B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset='utf-8'> 5 <title>WebGL test: Drawing without index validation</title> 6 <script src='/tests/SimpleTest/SimpleTest.js'></script> 7 <link rel='stylesheet' href='/tests/SimpleTest/test.css'> 8 9 <script id='vertSource' type='none'> 10 void main(void) { 11 gl_PointSize = 1.0; 12 gl_Position = vec4(0, 0, 0, 1); 13 } 14 </script> 15 16 <script id='fragSource' type='none'> 17 precision mediump float; 18 19 void main(void) { 20 gl_FragColor = vec4(0, 1, 0, 1); 21 } 22 </script> 23 </head> 24 <body> 25 <script> 26 27 function test() { 28 const c = document.createElement('canvas'); 29 c.width = c.height = 1; 30 const gl = c.getContext('webgl'); 31 if (!gl) { 32 todo(false, 'WebGL is unavailable.'); 33 return; 34 } 35 document.body.appendChild(c); 36 37 const vs = gl.createShader(gl.VERTEX_SHADER); 38 gl.shaderSource(vs, vertSource.innerHTML.trim()); 39 gl.compileShader(vs); 40 const fs = gl.createShader(gl.FRAGMENT_SHADER); 41 gl.shaderSource(fs, fragSource.innerHTML.trim()); 42 gl.compileShader(fs); 43 const prog = gl.createProgram(); 44 gl.attachShader(prog, vs); 45 gl.attachShader(prog, fs); 46 gl.linkProgram(prog); 47 gl.useProgram(prog); 48 49 gl.clearColor(1,0,0,1); 50 const pixel = new Uint32Array(1); 51 const pixelData = new Uint8Array(pixel.buffer); 52 53 function expectPixel(expected, info) { 54 gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixelData); 55 ok(pixel[0] == expected, 56 '[' + info + '] Expected 0x' + expected.toString(16) + ', was 0x' + pixel[0].toString(16)); 57 } 58 59 gl.clear(gl.COLOR_BUFFER_BIT); 60 expectPixel(0xFF0000FF, 'Clear'); 61 62 gl.drawArrays(gl.POINTS, 0, 1); 63 expectPixel(0xFF00FF00, 'DrawArrays'); 64 65 const indexBuffer = gl.createBuffer(); 66 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer); 67 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([0]), gl.STATIC_DRAW); 68 69 gl.clear(gl.COLOR_BUFFER_BIT); 70 gl.drawElements(gl.POINTS, 1, gl.UNSIGNED_SHORT, 0); 71 expectPixel(0xFF00FF00, 'DrawElements'); 72 73 SimpleTest.finish(); 74 } 75 76 SimpleTest.waitForExplicitFinish(); 77 78 const prefArrArr = [ 79 ['webgl.force-index-validation', -1] 80 ]; 81 const prefEnv = {'set': prefArrArr}; 82 SpecialPowers.pushPrefEnv(prefEnv, test); 83 84 </script> 85 </body> 86 </html>