OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style license that can be |
| 4 found in the LICENSE file. |
| 5 --> |
| 6 <!DOCTYPE html> |
| 7 <html> |
| 8 <head> |
| 9 <meta charset="utf-8"> |
| 10 <title>WebGL pixelStorei Test</title> |
| 11 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
| 12 <script src="../../resources/js-test-pre.js"></script> |
| 13 <script src="../resources/webgl-test.js"> </script> |
| 14 <script src="../../resources/desktop-gl-constants.js" type="text/javascript"></s
cript> |
| 15 </head> |
| 16 <body> |
| 17 <canvas id="example" width="50" height="50"></canvas> |
| 18 <canvas id="2d00" width="50" height="50"></canvas> |
| 19 <canvas id="2d01" width="50" height="50"></canvas> |
| 20 <canvas id="2d02" width="50" height="50"></canvas> |
| 21 <canvas id="2d03" width="50" height="50"></canvas> |
| 22 <div id="description"></div> |
| 23 <div id="console"></div> |
| 24 <script id="vshader" type="x-shader/x-vertex"> |
| 25 attribute vec4 vPosition; |
| 26 void main() { |
| 27 gl_Position = vPosition; |
| 28 } |
| 29 </script> |
| 30 |
| 31 <script id="fshader" type="x-shader/x-fragment"> |
| 32 void main() { |
| 33 gl_FragColor = vec4(1.0,0.0,0.0,1.0); |
| 34 } |
| 35 </script> |
| 36 |
| 37 <script> |
| 38 function fail(x,y, name, buf, shouldBe) { |
| 39 var i = (y*50+x) * 4; |
| 40 var reason = "pixel in "+name+" at ("+x+","+y+") is ("+buf[i]+","+buf[i+1]+","
+buf[i+2]+","+buf[i+3]+"), should be "+shouldBe; |
| 41 testFailed(reason); |
| 42 } |
| 43 |
| 44 function pass(name) { |
| 45 testPassed("drawing is correct in " + name); |
| 46 } |
| 47 |
| 48 function init() { |
| 49 debug("There should be 5 red triangles on 5 black squares above"); |
| 50 debug(""); |
| 51 |
| 52 debug("This test checks that drawImage and readPixels are not effected by gl.P
ixelstorei(gl.PACK_ALIGNMENT) and visa versa"); |
| 53 debug(""); |
| 54 |
| 55 var canvas3d = document.getElementById("example"); |
| 56 gl = initWebGL("example", "vshader", "fshader", [ "vPosition"], [ 0, 0, 0, 1 ]
, 1); |
| 57 |
| 58 var vertexObject = gl.createBuffer(); |
| 59 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); |
| 60 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,0.5,0, -0.5,-0.5,0, 0.5,-0
.5,0 ]), gl.STATIC_DRAW); |
| 61 gl.enableVertexAttribArray(0); |
| 62 gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0); |
| 63 |
| 64 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 65 gl.drawArrays(gl.TRIANGLES, 0, 3); |
| 66 |
| 67 |
| 68 function checkData(buf, name) { |
| 69 // Test several locations |
| 70 // First line should be all black |
| 71 for (var i = 0; i < 50; ++i) { |
| 72 if (buf[i*4] != 0 || buf[i*4+1] != 0 || buf[i*4+2] != 0 || buf[i*4+3] != 2
55) { |
| 73 fail(i, 0, name, buf, "(0,0,0,255)"); |
| 74 return; |
| 75 } |
| 76 } |
| 77 |
| 78 // Line 25 should be red for at least 6 red pixels starting 22 pixels in |
| 79 var offset = (25*50+22) * 4; |
| 80 for (var i = 0; i < 6; ++i) { |
| 81 if (buf[offset+i*4] != 255 || buf[offset+i*4+1] != 0 || buf[offset+i*4+2]
!= 0 || buf[offset+i*4+3] != 255) { |
| 82 fail(22 + i, 25, name, buf, "(255,0,0,255)"); |
| 83 return; |
| 84 } |
| 85 } |
| 86 |
| 87 // Last line should be all black |
| 88 offset = (49*50) * 4; |
| 89 for (var i = 0; i < 50; ++i) { |
| 90 if (buf[offset+i*4] != 0 || buf[offset+i*4+1] != 0 || buf[offset+i*4+2] !=
0 || buf[offset+i*4+3] != 255) { |
| 91 fail(i, 49, name, buf, "(0,0,0,255)"); |
| 92 return; |
| 93 } |
| 94 } |
| 95 |
| 96 pass(name); |
| 97 } |
| 98 |
| 99 function checkColors() { |
| 100 var buf = new Uint8Array(50 * 50 * 4); |
| 101 gl.readPixels(0, 0, 50, 50, gl.RGBA, gl.UNSIGNED_BYTE, buf); |
| 102 checkData(buf, "3d context"); |
| 103 var imgData = ctx2d.getImageData(0, 0, 50, 50); |
| 104 checkData(imgData.data, "2d context"); |
| 105 } |
| 106 |
| 107 var table = [1, 2, 4, 8]; |
| 108 for (var ii = 0; ii < table.length; ++ii) { |
| 109 gl.pixelStorei(gl.PACK_ALIGNMENT, table[ii]); |
| 110 ctx2d = document.getElementById("2d0" + ii).getContext("2d"); |
| 111 ctx2d.globalCompositeOperation = 'copy'; |
| 112 ctx2d.drawImage(canvas3d, 0, 0); |
| 113 checkColors(); |
| 114 assertMsg(gl.getParameter(gl.PACK_ALIGNMENT) == table[ii], |
| 115 "PACK_ALIGNMENT is " + table[ii]); |
| 116 } |
| 117 } |
| 118 |
| 119 init(); |
| 120 successfullyParsed = true; |
| 121 </script> |
| 122 <script src="../../resources/js-test-post.js"></script> |
| 123 |
| 124 </body> |
| 125 </html> |
OLD | NEW |