read-pixels-into-pixel-pack-buffer.html (6264B)
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 2 Conformance Test: ReadPixels Into Pixel Pack Buffer.</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="4" height="4"></canvas> 18 <div id="description"></div> 19 <div id="console"></div> 20 <script> 21 "use strict"; 22 23 function checkFormatAndType() 24 { 25 debug(""); 26 debug("check format / type"); 27 var invalidFormat = [gl.DEPTH_COMPONENT, gl.DEPTH_STENCIL, gl.R8, gl.RGBA4, gl.LUMINANCE, gl.LUMINANCE_ALPHA]; 28 var invalidType = [gl.UNSIGNED_INT_24_8]; 29 for (var ff = 0; ff < invalidFormat.length; ++ff) { 30 var format = invalidFormat[ff]; 31 gl.readPixels(0, 0, 1, 1, format, gl.UNSIGNED_BYTE, 0); 32 wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "Format should not be able to read as " + wtu.glEnumToString(gl, format)); 33 } 34 for (var tt = 0; tt < invalidType.length; ++tt) { 35 var type = invalidType[tt]; 36 gl.readPixels(0, 0, 1, 1, gl.RGBA, type, 0); 37 wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "Type should not be able to read as " + wtu.glEnumToString(gl, type)); 38 } 39 40 debug(""); 41 debug("check combinations of format and type"); 42 var combinations = [ 43 {format: gl.RGBA, type: gl.UNSIGNED_BYTE}, 44 {format: gl.RGB, type: gl.UNSIGNED_BYTE}, 45 {format: gl.RGB, type: gl.UNSIGNED_SHORT_5_6_5}, 46 {format: gl.RGBA, type: gl.UNSIGNED_SHORT_5_5_5_1}, 47 {format: gl.RGBA, type: gl.UNSIGNED_SHORT_4_4_4_4}, 48 {format: gl.ALPHA, type: gl.UNSIGNED_BYTE}, 49 {format: gl.RED, type: gl.UNSIGNED_BYTE}, 50 {format: gl.RGBA_INTEGER, type: gl.UNSIGNED_INT}, 51 {format: gl.RGBA_INTEGER, type: gl.INT} 52 ]; 53 54 var implFormat = gl.getParameter(gl.IMPLEMENTATION_COLOR_READ_FORMAT); 55 var implType = gl.getParameter(gl.IMPLEMENTATION_COLOR_READ_TYPE); 56 for (var tt = 0; tt < combinations.length; ++ tt) { 57 var info = combinations[tt]; 58 var format = info.format; 59 var type = info.type; 60 gl.readPixels(0, 0, 1, 1, format, type, 0); 61 // Only two format/type parameter pairs are accepted. GL_RGBA/GL_UNSIGNED_BYTE is always 62 // accepted on default readbuffer. The other acceptable pair can be discovered by querying 63 // GL_IMPLEMENTATION_COLOR_READ_FORMAT and GL_IMPLEMENTATION_COLOR_READ_TYPE. 64 if ((format == gl.RGBA && type == gl.UNSIGNED_BYTE) || (format == implFormat && type == implType)) { 65 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "The combination of format/type should be able to read as " + 66 wtu.glEnumToString(gl, format) + " / " + wtu.glEnumToString(gl, type)); 67 } else { 68 wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "The combination of format/type should not be able to read as " + 69 wtu.glEnumToString(gl, format) + " / " + wtu.glEnumToString(gl, type)); 70 } 71 } 72 } 73 74 function validatePixelPackBufferAndParameters(canvasWidth, canvasHeight) 75 { 76 debug(""); 77 debug("Validate PIXEL_PACK buffer and readPixels' parameters"); 78 gl.clearColor(0, 0, 0, 1); 79 gl.clear(gl.COLOR_BUFFER_BIT); 80 gl.pixelStorei(gl.PACK_ALIGNMENT, 1); 81 82 var size = canvasWidth * canvasHeight * 4; 83 var buffer = gl.createBuffer(); 84 gl.bindBuffer(gl.PIXEL_PACK_BUFFER, buffer); 85 gl.bufferData(gl.PIXEL_PACK_BUFFER, size, gl.STATIC_DRAW); 86 var array = new Uint8Array(size); 87 88 debug(""); 89 debug("PIXEL_PACK buffer is bound"); 90 gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, array); 91 wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "should generate INVALID_OPERATION if pixel pack buffer is bound"); 92 gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, 0); 93 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 94 95 debug(""); 96 debug("Validate the offset of PIXEL_PACK buffer and buffer size"); 97 gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, -1); 98 wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "offset < 0"); 99 gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, size); 100 wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "offset > buffer size"); 101 gl.readPixels(0, 0, canvasWidth + 1, canvasHeight, gl.RGBA, gl.UNSIGNED_BYTE, 0); 102 wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "pixel pack buffer is not large enough"); 103 104 debug(""); 105 debug("Validate the reading area of framebuffer"); 106 gl.readPixels(-1, -2, canvasWidth, canvasHeight, gl.RGBA, gl.UNSIGNED_BYTE, 0); 107 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "reading pixels outside of the framebuffer should succeed."); 108 gl.readPixels(2, 1, canvasWidth, canvasHeight, gl.RGBA, gl.UNSIGNED_BYTE, 0); 109 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "reading pixels outside of the framebuffer should succeed."); 110 gl.readPixels(2, 1, -1, -1, gl.RGBA, gl.UNSIGNED_BYTE, 0); 111 wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, 112 "reading pixels with negative width / height should generate INVALID_VALUE."); 113 114 checkFormatAndType(); 115 116 debug(""); 117 debug("no PIXEL_PACK buffer bound"); 118 gl.bindBuffer(gl.PIXEL_PACK_BUFFER, null); 119 gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, array); 120 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 121 gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, 0); 122 wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "no pixel pack buffer bound"); 123 124 gl.deleteBuffer(buffer); 125 } 126 127 debug(""); 128 debug("Canvas.getContext"); 129 130 var wtu = WebGLTestUtils; 131 var pixel = [0, 0, 0, 0]; 132 var expectedColor = [255, 102, 0, 255]; 133 134 var canvas = document.getElementById("example"); 135 var gl = wtu.create3DContext(canvas, undefined, 2); 136 137 if (!gl) { 138 testFailed("context does not exist"); 139 } else { 140 testPassed("context exists"); 141 142 debug(""); 143 description('ReadPixels into PIXEL_PACK buffer'); 144 validatePixelPackBufferAndParameters(4, 4); 145 } 146 147 debug(""); 148 var successfullyParsed = true; 149 </script> 150 <script src="../../js/js-test-post.js"></script> 151 </body> 152 </html>