test_tex_pbo.html (2694B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset='UTF-8'> 5 <script src='/tests/SimpleTest/SimpleTest.js'></script> 6 <link rel='stylesheet' href='/tests/SimpleTest/test.css'> 7 </head> 8 <body> 9 <script> 10 11 function shouldBe(testStr, refStr) { 12 ok(testStr == refStr, 'Expected "' + refStr + '", was "' + testStr + '".'); 13 } 14 15 function getErrorStr(gl, err) { 16 if (!err) return "NO_ERROR"; 17 for (const k in gl) { 18 const v = gl[k]; 19 if (v == err) { 20 return k; 21 } 22 } 23 return `<${err}>`; 24 } 25 26 function glErrorShouldBe(gl, expected, opt_info) { 27 if (opt_info) { 28 opt_info = opt_info + ': ' 29 } else { 30 opt_info = ''; 31 } 32 33 if (!expected.length) { 34 expected = [expected]; 35 } 36 expected = expected.map(x => getErrorStr(gl, x)); 37 let was = gl.getError(); 38 was = getErrorStr(gl, was); 39 console.log(expected); 40 ok(expected.includes(was), 41 `${opt_info}Expected gl.getError() in [${expected}], was ${was}.`); 42 } 43 44 (() => { 45 const gl = document.createElement('canvas').getContext('webgl2'); 46 if (!gl) { 47 todo(false, 'No webgl2, skipping...'); 48 return; 49 } 50 const pbo = gl.createBuffer(); 51 gl.bindBuffer(gl.PIXEL_UNPACK_BUFFER, pbo); 52 53 const PBO_DATA = new Uint8Array([ 54 255, 0, 0, 255, 55 0, 255, 0, 255, 56 ]); 57 gl.bufferData(gl.PIXEL_UNPACK_BUFFER, PBO_DATA, gl.STATIC_DRAW); 58 59 const tex = gl.createTexture(); 60 gl.bindTexture(gl.TEXTURE_2D, tex); 61 62 const fb = gl.createFramebuffer(); 63 gl.bindFramebuffer(gl.FRAMEBUFFER, fb); 64 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0); 65 66 const readback = new Uint8Array(4); 67 68 const PBO_LIST = [null, pbo]; 69 for (const cur of PBO_LIST) { 70 gl.bindBuffer(gl.PIXEL_UNPACK_BUFFER, cur); 71 72 function tryUpload(arg, expectedReadback) { 73 let argType = typeof(arg); 74 if (arg === null) { 75 argType = 'null'; 76 } 77 const argForPbo = (argType == 'number'); 78 const pboBound = !!cur; 79 const expectedErr = (argForPbo == pboBound) ? 0 : gl.INVALID_OPERATION; 80 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, 81 arg); 82 const with_without = pboBound ? 'with' : 'without'; 83 glErrorShouldBe(gl, expectedErr, `${with_without} pbo, texImage(..., ${argType}("${arg}"))`); 84 85 if (expectedErr) return; 86 gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, readback); 87 shouldBe(expectedReadback.toString(), readback.toString()); 88 } 89 90 const CPU_DATA = new Uint8Array([255, 255, 0, 255]); 91 92 tryUpload(null, [0,0,0,0]); 93 tryUpload(CPU_DATA, CPU_DATA); 94 tryUpload(0, PBO_DATA.slice(0,4)); 95 tryUpload(4, PBO_DATA.slice(4)); 96 } 97 })(); 98 </script> 99 </body> 100 </html>