content/canvas/test/webgl-conformance/conformance/textures/tex-image-and-sub-image-2d-with-array-buffer-view.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!--
michael@0 2 Copyright (c) 2011 The Chromium Authors. All rights reserved.
michael@0 3 Use of this source code is governed by a BSD-style license that can be
michael@0 4 found in the LICENSE file.
michael@0 5 -->
michael@0 6 <!DOCTYPE html>
michael@0 7 <html>
michael@0 8 <head>
michael@0 9 <meta charset="utf-8">
michael@0 10 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
michael@0 11 <script src="../../resources/js-test-pre.js"></script>
michael@0 12 <script src="../resources/webgl-test.js"></script>
michael@0 13 <script src="../resources/webgl-test-utils.js"></script>
michael@0 14 </head>
michael@0 15 <body>
michael@0 16 <canvas id="example" width="1px" height="2px"></canvas>
michael@0 17 <div id="description"></div>
michael@0 18 <div id="console"></div>
michael@0 19 <script>
michael@0 20 description('Verifies texImage2D and texSubImage2D code paths taking ArrayBufferView');
michael@0 21
michael@0 22 var wtu = WebGLTestUtils;
michael@0 23 var gl = null;
michael@0 24 var textureLoc = null;
michael@0 25 var successfullyParsed = false;
michael@0 26
michael@0 27 // These two declarations need to be global for "shouldBe" to see them
michael@0 28 var buf = null;
michael@0 29 var idx = 0;
michael@0 30 var pixel = [0, 0, 0, 1];
michael@0 31 var correctColor = null;
michael@0 32
michael@0 33 function generateRGBAData(type, unpackAlignment)
michael@0 34 {
michael@0 35 var sourceData = [ 255, 0, 0, 255,
michael@0 36 0, 255, 0, 0 ];
michael@0 37 switch (type) {
michael@0 38 case gl.UNSIGNED_BYTE: {
michael@0 39 var rowWidth = Math.max(unpackAlignment, 4);
michael@0 40 var data = new Uint8Array(2 * rowWidth);
michael@0 41 for (var y = 0; y < 2; ++y) {
michael@0 42 var index = y * rowWidth;
michael@0 43 for (var element = 0; element < 4; ++element) {
michael@0 44 data[index + element] = sourceData[4 * y + element];
michael@0 45 }
michael@0 46 }
michael@0 47 return data;
michael@0 48 }
michael@0 49 case gl.UNSIGNED_SHORT_4_4_4_4: {
michael@0 50 var rowWidth = Math.max(unpackAlignment, 2) / 2;
michael@0 51 var data = new Uint16Array(2 * rowWidth);
michael@0 52 for (var y = 0; y < 2; ++y) {
michael@0 53 var index = y * rowWidth;
michael@0 54 data[index] = (((sourceData[4 * y] & 0xF0) << 8)
michael@0 55 | ((sourceData[4 * y + 1] & 0xF0) << 4)
michael@0 56 | (sourceData[4 * y + 2] & 0xF0)
michael@0 57 | (sourceData[4 * y + 3] >> 4));
michael@0 58 }
michael@0 59 return data;
michael@0 60 }
michael@0 61 case gl.UNSIGNED_SHORT_5_5_5_1: {
michael@0 62 var rowWidth = Math.max(unpackAlignment, 2) / 2;
michael@0 63 var data = new Uint16Array(2 * rowWidth);
michael@0 64 for (var y = 0; y < 2; ++y) {
michael@0 65 var index = y * rowWidth;
michael@0 66 data[index] = (((sourceData[4 * y] & 0xF8) << 8)
michael@0 67 | ((sourceData[4 * y + 1] & 0xF8) << 3)
michael@0 68 | ((sourceData[4 * y + 2] & 0xF8) >> 2)
michael@0 69 | (sourceData[4 * y + 3] >> 7));
michael@0 70 }
michael@0 71 return data;
michael@0 72 }
michael@0 73 }
michael@0 74 }
michael@0 75
michael@0 76 function typeToString(type)
michael@0 77 {
michael@0 78 switch (type) {
michael@0 79 case gl.UNSIGNED_BYTE: return 'UNSIGNED_BYTE';
michael@0 80 case gl.UNSIGNED_SHORT_5_5_5_1: return 'UNSIGNED_SHORT_5_5_5_1';
michael@0 81 case gl.UNSIGNED_SHORT_4_4_4_4: return 'UNSIGNED_SHORT_4_4_4_4';
michael@0 82 }
michael@0 83 return 'Unknown type ' + type;
michael@0 84 }
michael@0 85
michael@0 86 function runOneIteration(useTexSubImage2D, type, unpackAlignment, flipY, premultiplyAlpha, topColor, bottomColor)
michael@0 87 {
michael@0 88 debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') +
michael@0 89 ' with type=' + typeToString(type) +
michael@0 90 ', unpackAlignment=' + unpackAlignment +
michael@0 91 ', flipY=' + flipY + ', premultiplyAlpha=' + premultiplyAlpha);
michael@0 92 gl.colorMask(true, true, true, true);
michael@0 93 gl.clearColor(0, 0, 0, 1.0);
michael@0 94 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 95 // Enable writes to the RGB channels
michael@0 96 gl.colorMask(true, true, true, false);
michael@0 97 var texture = gl.createTexture();
michael@0 98 // Bind the texture to texture unit 0
michael@0 99 gl.bindTexture(gl.TEXTURE_2D, texture);
michael@0 100 // Set up texture parameters
michael@0 101 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
michael@0 102 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
michael@0 103 // Set up pixel store parameters
michael@0 104 gl.pixelStorei(gl.UNPACK_ALIGNMENT, unpackAlignment);
michael@0 105 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
michael@0 106 gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha);
michael@0 107 // Generate the data
michael@0 108 var data = generateRGBAData(type, unpackAlignment);
michael@0 109 if (gl.getError() != gl.NO_ERROR)
michael@0 110 testFailed("GL error before texture upload");
michael@0 111 // Upload the image into the texture
michael@0 112 if (useTexSubImage2D) {
michael@0 113 // Initialize the texture to black first
michael@0 114 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 2, 0,
michael@0 115 gl.RGBA, type, null);
michael@0 116 if (gl.getError() != gl.NO_ERROR)
michael@0 117 testFailed("GL error after texImage2D(null)");
michael@0 118 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 1, 2, gl.RGBA, type, data);
michael@0 119 if (gl.getError() != gl.NO_ERROR)
michael@0 120 testFailed("GL error after texSubImage2D");
michael@0 121 } else {
michael@0 122 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 2, 0, gl.RGBA, type, data);
michael@0 123 if (gl.getError() != gl.NO_ERROR)
michael@0 124 testFailed("GL error after texImage2D");
michael@0 125 }
michael@0 126
michael@0 127 // Point the uniform sampler to texture unit 0
michael@0 128 gl.uniform1i(textureLoc, 0);
michael@0 129 // Draw the triangles
michael@0 130 wtu.drawQuad(gl, [0, 0, 0, 255]);
michael@0 131
michael@0 132 // Read back the rendering results
michael@0 133 buf = new Uint8Array(1 * 2 * 4);
michael@0 134 gl.readPixels(0, 0, 1, 2, gl.RGBA, gl.UNSIGNED_BYTE, buf);
michael@0 135 // Check the top pixel and bottom pixel and make sure they have
michael@0 136 // the right color.
michael@0 137 debug("Checking bottom pixel");
michael@0 138 wtu.checkCanvasRect(gl, 0, 0, 1, 1, bottomColor, "shouldBe " + bottomColor);
michael@0 139 debug("Checking top pixel");
michael@0 140 wtu.checkCanvasRect(gl, 0, 1, 1, 1, topColor, "shouldBe " + topColor);
michael@0 141 }
michael@0 142
michael@0 143 function runTest()
michael@0 144 {
michael@0 145 var red = [255, 0, 0, 255];
michael@0 146 var green = [0, 255, 0, 255];
michael@0 147 var redPremultiplyAlpha = [255, 0, 0, 255];
michael@0 148 var greenPremultiplyAlpha = [0, 0, 0, 255];
michael@0 149
michael@0 150 var types = [ gl.UNSIGNED_BYTE, gl.UNSIGNED_SHORT_5_5_5_1, gl.UNSIGNED_SHORT_4_4_4_4 ];
michael@0 151 var unpackAlignments = [ 1, 2, 4, 8 ];
michael@0 152
michael@0 153 for (var i = 0; i < types.length; ++i) {
michael@0 154 var type = types[i];
michael@0 155 for (var j = 0; j < unpackAlignments.length; ++j) {
michael@0 156 var unpackAlignment = unpackAlignments[j];
michael@0 157 runOneIteration(false, type, unpackAlignment, true, false,
michael@0 158 red, green);
michael@0 159 runOneIteration(false, type, unpackAlignment, false, false,
michael@0 160 green, red);
michael@0 161 runOneIteration(false, type, unpackAlignment, true, true,
michael@0 162 redPremultiplyAlpha, greenPremultiplyAlpha);
michael@0 163 runOneIteration(false, type, unpackAlignment, false, true,
michael@0 164 greenPremultiplyAlpha, redPremultiplyAlpha);
michael@0 165 runOneIteration(true, type, unpackAlignment, true, false,
michael@0 166 red, green);
michael@0 167 runOneIteration(true, type, unpackAlignment, false, false,
michael@0 168 green, red);
michael@0 169 runOneIteration(true, type, unpackAlignment, true, true,
michael@0 170 redPremultiplyAlpha, greenPremultiplyAlpha);
michael@0 171 runOneIteration(true, type, unpackAlignment, false, true,
michael@0 172 greenPremultiplyAlpha, redPremultiplyAlpha);
michael@0 173 }
michael@0 174 }
michael@0 175
michael@0 176 }
michael@0 177
michael@0 178 gl = wtu.create3DContext("example");
michael@0 179 var program = wtu.setupTexturedQuad(gl);
michael@0 180 gl.disable(gl.BLEND);
michael@0 181
michael@0 182 gl.clearColor(0,0,0,1);
michael@0 183 gl.clearDepth(1);
michael@0 184
michael@0 185 textureLoc = gl.getUniformLocation(program, "tex");
michael@0 186
michael@0 187 runTest();
michael@0 188 glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
michael@0 189 successfullyParsed = true;
michael@0 190 </script>
michael@0 191 <script>finishTest();</script>
michael@0 192 </body>
michael@0 193 </html>

mercurial