OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <head> |
| 4 <meta charset="utf-8"> |
| 5 <!-- |
| 6 |
| 7 /* |
| 8 ** Copyright (c) 2012 The Khronos Group Inc. |
| 9 ** |
| 10 ** Permission is hereby granted, free of charge, to any person obtaining a |
| 11 ** copy of this software and/or associated documentation files (the |
| 12 ** "Materials"), to deal in the Materials without restriction, including |
| 13 ** without limitation the rights to use, copy, modify, merge, publish, |
| 14 ** distribute, sublicense, and/or sell copies of the Materials, and to |
| 15 ** permit persons to whom the Materials are furnished to do so, subject to |
| 16 ** the following conditions: |
| 17 ** |
| 18 ** The above copyright notice and this permission notice shall be included |
| 19 ** in all copies or substantial portions of the Materials. |
| 20 ** |
| 21 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 23 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 24 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 25 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 26 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 27 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 28 */ |
| 29 |
| 30 --> |
| 31 <link rel="stylesheet" type="text/css" href="../unit.css" /> |
| 32 <script type="application/x-javascript" src="../unit.js"></script> |
| 33 <script type="application/x-javascript" src="../util.js"></script> |
| 34 |
| 35 <script type="application/x-javascript"> |
| 36 |
| 37 function assertIdxs(name, arr, length) { |
| 38 // assertOk(name+": Read with negative idx should work", function(){ return ar
r[-1] }); |
| 39 // assertOk(name+": Read with too large idx should work", function(){ return a
rr[length] }); |
| 40 // assertOk(name+": Write with negative idx should work", function(){ arr[-1]
= 0 }); |
| 41 // assertOk(name+": Write with too large idx should work", function(){ arr[len
gth] = 0 }); |
| 42 // arr[0] = 2; |
| 43 // assertEquals(name+": Test that write worked", 2, arr[0]); |
| 44 // assertOk(name+": Write with bad value should work", function(){ arr[0] = {x
:"foo"} }); |
| 45 // assertEquals(name+": Test that bad write didn't work", 2, arr[0]); |
| 46 assertOk(name+": Read and writes with OK idxs should work", function(){ |
| 47 for (var i=0; i<length; i++) arr[i] = i + 1; |
| 48 for (var i=0; i<length; i++) arr[i] = arr[i] + 1; |
| 49 for (var i=0; i<length; i++) assertEquals(name+": Test that reads and writes
work", i+2, arr[i]); |
| 50 }); |
| 51 } |
| 52 |
| 53 Tests.startUnit = function () { |
| 54 var canvas = document.getElementById('gl'); |
| 55 var gl = wrapGLContext(getGLContext(canvas)); |
| 56 prog = new Shader(gl, 'vert', 'frag'); |
| 57 prog.use(); |
| 58 prog.uniform4f('c', 255, 0, 0, 255); |
| 59 va = prog.attrib('Vertex'); |
| 60 buffer = gl.createBuffer(); |
| 61 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); |
| 62 return [gl]; |
| 63 } |
| 64 |
| 65 Tests.endUnit = function() { |
| 66 prog.destroy(); |
| 67 } |
| 68 |
| 69 Tests.testCreateFromArray = function() { |
| 70 var a = new Float32Array([1,2,3,4,5,6]); |
| 71 assertIdxs('Float', a, 6); |
| 72 var a = new Int32Array([1,2,3,4,5,6]); |
| 73 assertIdxs('Int', a, 6); |
| 74 var a = new Int16Array([1,2,3,4,5,6]); |
| 75 assertIdxs('Short', a, 6); |
| 76 var a = new Int8Array([1,2,3,4,5,6]); |
| 77 assertIdxs('Byte', a, 6); |
| 78 var a = new Uint32Array([1,2,3,4,5,6]); |
| 79 assertIdxs('UInt', a, 6); |
| 80 var a = new Uint16Array([1,2,3,4,5,6]); |
| 81 assertIdxs('UShort', a, 6); |
| 82 var a = new Uint8Array([1,2,3,4,5,6]); |
| 83 assertIdxs('UByte', a, 6); |
| 84 } |
| 85 Tests.testCreateFromCount = function() { |
| 86 var a = new Float32Array(6); |
| 87 assertIdxs('Float', a, 6); |
| 88 var a = new Int32Array(6); |
| 89 assertIdxs('Int', a, 6); |
| 90 var a = new Int16Array(6); |
| 91 assertIdxs('Short', a, 6); |
| 92 var a = new Int8Array(6); |
| 93 assertIdxs('Byte', a, 6); |
| 94 var a = new Uint32Array(6); |
| 95 assertIdxs('UInt', a, 6); |
| 96 var a = new Uint16Array(6); |
| 97 assertIdxs('UShort', a, 6); |
| 98 var a = new Uint8Array(6); |
| 99 assertIdxs('UByte', a, 6); |
| 100 } |
| 101 Tests.testCreateFromBuffer = function() { |
| 102 var sz = 24; |
| 103 var b = new ArrayBuffer(sz); |
| 104 var a = new Float32Array(b); |
| 105 assertIdxs('Float', a, sz/4); |
| 106 var a = new Int32Array(b); |
| 107 assertIdxs('Int', a, sz/4); |
| 108 var a = new Int16Array(b); |
| 109 assertIdxs('Short', a, sz/2); |
| 110 var a = new Int8Array(b); |
| 111 assertIdxs('Byte', a, sz/1); |
| 112 var a = new Uint32Array(b); |
| 113 assertIdxs('UInt', a, sz/4); |
| 114 var a = new Uint16Array(b); |
| 115 assertIdxs('UShort', a, sz/2); |
| 116 var a = new Uint8Array(b); |
| 117 assertIdxs('UByte', a, sz/1); |
| 118 } |
| 119 |
| 120 Tests.testThatWritesChangeDrawing = function(gl) { |
| 121 var verts = [ |
| 122 0,0, |
| 123 1,0, |
| 124 1,1, |
| 125 |
| 126 0,0, |
| 127 1,1, |
| 128 0,1 |
| 129 ]; |
| 130 var a = new Float32Array(verts); |
| 131 var arr = []; |
| 132 for (var i=0; i<12; i++) |
| 133 arr[i] = a[i]; |
| 134 assertEquals("Test that reads work from an array-initialized Float32Array", ar
r, verts); |
| 135 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 136 gl.bufferData(gl.ARRAY_BUFFER, a, gl.STATIC_DRAW); |
| 137 gl.vertexAttribPointer(va, 2, gl.FLOAT, false, 0, 0); |
| 138 gl.enableVertexAttribArray(va); |
| 139 |
| 140 var id = new Uint8Array(4); |
| 141 gl.readPixels(8,8,1,1,gl.RGBA, gl.UNSIGNED_BYTE, id); |
| 142 assertEquals([0, 0, 0, 0], [id[0], id[1], id[2], id[3]]); |
| 143 |
| 144 gl.drawArrays(gl.TRIANGLES, 0, 6); |
| 145 |
| 146 gl.readPixels(8,8,1,1,gl.RGBA, gl.UNSIGNED_BYTE, id); |
| 147 assertEquals([255, 0, 0, 255], [id[0], id[1], id[2], id[3]]); |
| 148 gl.readPixels(0,8,1,1,gl.RGBA, gl.UNSIGNED_BYTE, id); |
| 149 assertEquals([0, 0, 0, 0], [id[0], id[1], id[2], id[3]]); |
| 150 |
| 151 a[0] = a[6] = a[10] = -1; |
| 152 gl.bufferData(gl.ARRAY_BUFFER, a, gl.STATIC_DRAW); |
| 153 gl.vertexAttribPointer(va, 2, gl.FLOAT, false, 0, 0); |
| 154 |
| 155 gl.drawArrays(gl.TRIANGLES, 0, 6); |
| 156 |
| 157 gl.readPixels(8,8,1,1,gl.RGBA, gl.UNSIGNED_BYTE, id); |
| 158 assertEquals([255, 0, 0, 255], [id[0], id[1], id[2], id[3]]); |
| 159 gl.readPixels(0,8,1,1,gl.RGBA, gl.UNSIGNED_BYTE, id); |
| 160 assertEquals("Test that Float32Array#[]= worked and drawArrays drew a full-wid
th rectangle", |
| 161 [255, 0, 0, 255], [id[0], id[1], id[2], id[3]]); |
| 162 gl.readPixels(0,0,1,1,gl.RGBA, gl.UNSIGNED_BYTE, id); |
| 163 assertEquals([0, 0, 0, 0], [id[0], id[1], id[2], id[3]]); |
| 164 } |
| 165 |
| 166 </script> |
| 167 <script id="vert" type="x-shader/x-vertex"> |
| 168 attribute vec2 Vertex; |
| 169 void main() |
| 170 { |
| 171 gl_Position = vec4(Vertex, 0.0, 1.0); |
| 172 } |
| 173 </script> |
| 174 <script id="frag" type="x-shader/x-fragment"> |
| 175 precision mediump float; |
| 176 |
| 177 uniform vec4 c; |
| 178 void main() |
| 179 { |
| 180 gl_FragColor = c; |
| 181 } |
| 182 </script> |
| 183 <style>canvas{border: 1px solid black}</style> |
| 184 </head><body> |
| 185 <canvas id="gl" width="16" height="16"></canvas> |
| 186 </body></html> |
OLD | NEW |