gecko/content/canvas/test/webgl/conformance/bug-32888.html

191 lines
6.2 KiB
HTML
Raw Normal View History

<!--
Copyright (c) 2009 The Chromium Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<html>
<head>
<link rel="stylesheet" href="../resources/js-test-style.css"/>
<script src="../resources/js-test-pre.js"></script>
<script src="resources/webgl-test.js"></script>
<script src="resources/utils3d.js"> </script>
<script id="vshader" type="x-shader/x-vertex">
attribute vec3 g_Position;
attribute vec2 g_TexCoord0;
varying vec2 texCoord;
void main()
{
gl_Position = vec4(g_Position.x, g_Position.y, g_Position.z, 1.0);
texCoord = g_TexCoord0;
}
</script>
<script id="fshader" type="x-shader/x-fragment">
uniform sampler2D tex;
varying vec2 texCoord;
void main()
{
gl_FragColor = texture2D(tex, texCoord);
}
</script>
<script>
var gl = null;
var textureLoc = null;
var successfullyParsed = false;
function init()
{
if (window.layoutTestController) {
layoutTestController.overridePreference("WebKitWebGLEnabled", "1");
layoutTestController.dumpAsText();
layoutTestController.waitUntilDone();
}
description('Regression test for <a href="https://bugs.webkit.org/show_bug.cgi?id=32888">https://bugs.webkit.org/show_bug.cgi?id=32888</a> : <code>Garbage in transparent regions of images uploaded as textures</code>');
gl = initWebGL("example", "vshader", "fshader", [ "g_Position", "g_TexCoord0" ], [ 0, 0, 0, 1 ], 1);
gl.viewport(0, 0, 32, 32);
textureLoc = gl.getUniformLocation(gl.program, "tex");
var vertices = new WebGLFloatArray([
1.0, 1.0, 0.0,
-1.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0]);
// The input texture has 8 characters; take the leftmost one
var coeff = 1.0 / 8.0;
var texCoords = new WebGLFloatArray([
coeff, 1.0,
0.0, 1.0,
0.0, 0.0,
coeff, 1.0,
0.0, 0.0,
coeff, 0.0]);
var texCoordOffset = vertices.byteLength;
var vbo = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
gl.bufferData(gl.ARRAY_BUFFER,
texCoordOffset + texCoords.byteLength,
gl.STATIC_DRAW);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, vertices);
gl.bufferSubData(gl.ARRAY_BUFFER, texCoordOffset, texCoords);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(1);
gl.vertexAttribPointer(1, 2, gl.FLOAT, gl.FALSE, 0, texCoordOffset);
texture = loadTexture("resources/bug-32888-texture.png");
}
function loadTexture(src) {
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
var image = new Image();
image.onload = function() {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, image, true);
runTest();
};
image.src = src;
return texture;
}
// These two declarations need to be global for "shouldBe" to see them
var buf = null;
var idx = 0;
function runTest()
{
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Bind the texture to texture unit 0
gl.bindTexture(gl.TEXTURE_2D, texture);
// Point the uniform sampler to texture unit 0
gl.uniform1i(textureLoc, 0);
// Draw the triangles
gl.drawArrays(gl.TRIANGLES, 0, 6);
// Read back the rendering results
var width = 32;
var height = 32;
buf = gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE);
// Spot check a couple of 2x2 regions in the upper and lower left
// corners; they should be the background color rather than
// garbage
var queryWidth = 2;
var queryHeight = 2;
debug("Checking lower left corner");
var xoff = 1;
var yoff = height - 3;
for (var y = 0; y < queryHeight; y++) {
for (var x = 0; x < queryWidth; x++) {
idx = ((yoff + y) * width * 4 +
(xoff + x) * 4);
shouldBe("buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 0", "true");
}
}
debug("Checking upper left corner");
yoff = 1;
for (var y = 0; y < queryHeight; y++) {
for (var x = 0; x < queryWidth; x++) {
idx = ((yoff + y) * width * 4 +
(xoff + x) * 4);
shouldBe("buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 0", "true");
}
}
successfullyParsed = true;
var epilogue = document.createElement("script");
epilogue.onload = finish;
epilogue.src = "../resources/js-test-post.js";
document.body.appendChild(epilogue);
}
function finish() {
if (window.layoutTestController) {
layoutTestController.notifyDone();
}
}
</script>
</head>
<body onload="init()">
<canvas id="example" width="32px" height="32px"></canvas>
<div id="description"></div>
<div id="console"></div>
</body>
</html>