1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/canvas/test/webgl-conformance/conformance/textures/tex-image-and-sub-image-2d-with-image-data.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,138 @@ 1.4 +<!-- 1.5 +Copyright (c) 2011 The Chromium Authors. All rights reserved. 1.6 +Use of this source code is governed by a BSD-style license that can be 1.7 +found in the LICENSE file. 1.8 + --> 1.9 +<!DOCTYPE html> 1.10 +<html> 1.11 +<head> 1.12 +<meta charset="utf-8"> 1.13 +<link rel="stylesheet" href="../../resources/js-test-style.css"/> 1.14 +<script src="../../resources/js-test-pre.js"></script> 1.15 +<script src="../resources/webgl-test.js"></script> 1.16 +<script src="../resources/webgl-test-utils.js"></script> 1.17 +<script> 1.18 +var wtu = WebGLTestUtils; 1.19 +var gl = null; 1.20 +var textureLoc = null; 1.21 +var successfullyParsed = false; 1.22 +var imageData = null; 1.23 + 1.24 +function init() 1.25 +{ 1.26 + if (window.initNonKhronosFramework) { 1.27 + window.initNonKhronosFramework(true); 1.28 + } 1.29 + 1.30 + description('Verify texImage2D and texSubImage2D code paths taking ImageData'); 1.31 + 1.32 + var canvas2d = document.getElementById("texcanvas"); 1.33 + var context2d = canvas2d.getContext("2d"); 1.34 + imageData = context2d.createImageData(1, 2); 1.35 + var data = imageData.data; 1.36 + data[0] = 255; 1.37 + data[1] = 0; 1.38 + data[2] = 0; 1.39 + data[3] = 255; 1.40 + data[4] = 0; 1.41 + data[5] = 255; 1.42 + data[6] = 0; 1.43 + data[7] = 0; 1.44 + 1.45 + wtu = WebGLTestUtils; 1.46 + var canvas = document.getElementById("example"); 1.47 + gl = wtu.create3DContext(canvas); 1.48 + var program = wtu.setupTexturedQuad(gl); 1.49 + gl.clearColor(0,0,0,1); 1.50 + gl.clearDepth(1); 1.51 + gl.disable(gl.BLEND); 1.52 + 1.53 + textureLoc = gl.getUniformLocation(program, "tex"); 1.54 + 1.55 + runTest(); 1.56 +} 1.57 + 1.58 +// These two declarations need to be global for "shouldBe" to see them 1.59 +var buf = null; 1.60 +var idx = 0; 1.61 +var pixel = [0, 0, 0, 1]; 1.62 +var correctColor = null; 1.63 + 1.64 +function runOneIteration(useTexSubImage2D, flipY, premultiplyAlpha, topColor, bottomColor) 1.65 +{ 1.66 + debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') + 1.67 + ' with flipY=' + flipY + ' and premultiplyAlpha=' + premultiplyAlpha); 1.68 + gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 1.69 + // Enable writes to the RGBA channels 1.70 + gl.colorMask(1, 1, 1, 0); 1.71 + var texture = gl.createTexture(); 1.72 + // Bind the texture to texture unit 0 1.73 + gl.bindTexture(gl.TEXTURE_2D, texture); 1.74 + // Set up texture parameters 1.75 + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); 1.76 + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); 1.77 + // Set up pixel store parameters 1.78 + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY); 1.79 + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha); 1.80 + // Upload the image into the texture 1.81 + if (useTexSubImage2D) { 1.82 + // Initialize the texture to black first 1.83 + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 2, 0, 1.84 + gl.RGBA, gl.UNSIGNED_BYTE, null); 1.85 + gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, imageData); 1.86 + } else { 1.87 + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, imageData); 1.88 + } 1.89 + 1.90 + // Point the uniform sampler to texture unit 0 1.91 + gl.uniform1i(textureLoc, 0); 1.92 + // Draw the triangles 1.93 + wtu.drawQuad(gl, [0, 0, 0, 255]); 1.94 + 1.95 + // Read back the rendering results 1.96 + buf = new Uint8Array(1 * 2 * 4); 1.97 + gl.readPixels(0, 0, 1, 2, gl.RGBA, gl.UNSIGNED_BYTE, buf); 1.98 + // Check the top pixel and bottom pixel and make sure they have 1.99 + // the right color. 1.100 + debug("Checking bottom pixel"); 1.101 + wtu.checkCanvasRect(gl, 0, 0, 1, 1, bottomColor, "shouldBe " + bottomColor); 1.102 + debug("Checking top pixel"); 1.103 + wtu.checkCanvasRect(gl, 0, 1, 1, 1, topColor, "shouldBe " + topColor); 1.104 +} 1.105 + 1.106 +function runTest() 1.107 +{ 1.108 + var red = [255, 0, 0, 255]; 1.109 + var green = [0, 255, 0, 255]; 1.110 + var redPremultiplyAlpha = [255, 0, 0, 255]; 1.111 + var greenPremultiplyAlpha = [0, 0, 0, 255]; 1.112 + 1.113 + runOneIteration(false, true, false, 1.114 + red, green); 1.115 + runOneIteration(false, false, false, 1.116 + green, red); 1.117 + runOneIteration(false, true, true, 1.118 + redPremultiplyAlpha, greenPremultiplyAlpha); 1.119 + runOneIteration(false, false, true, 1.120 + greenPremultiplyAlpha, redPremultiplyAlpha); 1.121 + runOneIteration(true, true, false, 1.122 + red, green); 1.123 + runOneIteration(true, false, false, 1.124 + green, red); 1.125 + runOneIteration(true, true, true, 1.126 + redPremultiplyAlpha, greenPremultiplyAlpha); 1.127 + runOneIteration(true, false, true, 1.128 + greenPremultiplyAlpha, redPremultiplyAlpha); 1.129 + 1.130 + glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 1.131 + finishTest(); 1.132 +} 1.133 +</script> 1.134 +</head> 1.135 +<body onload="init()"> 1.136 +<canvas id="texcanvas" width="1px" height="2px"></canvas> 1.137 +<canvas id="example" width="1px" height="2px"></canvas> 1.138 +<div id="description"></div> 1.139 +<div id="console"></div> 1.140 +</body> 1.141 +</html>