gecko/content/canvas/test/webgl/conformance/texture-formats-test.html
2010-06-04 12:03:40 -07:00

306 lines
9.3 KiB
HTML

<!--
Copyright (c) 2009 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>WebGL Texture Format Conformance Tests</title>
<link rel="stylesheet" href="../resources/js-test-style.css"/>
<script src="../resources/desktop-gl-constants.js" type="text/javascript"></script>
<script src="../../debug/webgl-debug.js" type="text/javascript"></script>
<script src="../resources/js-test-pre.js"></script>
<script src="resources/webgl-test.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<canvas id="canvas2d" width="2" height="2" style="width: 50px; height: 50px; border: 1px solid black;"></canvas>
<canvas id="canvas" width="2" height="2" style="width: 100px; height:100px; border: 1px solid black;"> </canvas>
<script id="vshader" type="x-shader/x-vertex">
attribute vec4 vPosition;
attribute vec2 texCoord0;
varying vec2 texCoord;
void main()
{
gl_Position = vPosition;
texCoord = 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>
description("This test ensures WebGL implementations allow the OpenGL ES 2.0 texture formats and do not allow DesktopGL texture formats.");
debug("");
debug("Canvas.getContext");
var gl = create3DContext(document.getElementById("canvas"));
if (!gl) {
testFailed("context does not exist");
} else {
testPassed("context exists");
WebGLDebugUtils.init(gl);
debug("");
debug("Checking texture formats.");
function createTexture(internalFormat, format, opt_border) {
var border = (opt_border === undefined) ? 0 : opt_border;
var tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texImage2D(gl.TEXTURE_2D,
0, // level
internalFormat, // internalFormat
16, // width
16, // height
border, // border
format, // format
gl.UNSIGNED_BYTE, // type
null); // data
}
function testValidFormat(internalFormat, formatName) {
createTexture(internalFormat, internalFormat);
assertMsg(gl.getError() == gl.NO_ERROR,
"was able to create texture of " + formatName);
}
function testInvalidFormat(internalFormat, formatName) {
createTexture(internalFormat, gl.RGBA);
var err = gl.getError();
if (err == gl.NO_ERROR) {
testFailed("should NOT be able to create texture of type " + formatName);
} else if (err == gl.INVALID_OPERATION) {
testFailed("should return gl.INVALID_OPERATION for type " + formatName);
} else if (err == gl.INVALID_ENUM) {
testPassed("not able to create invalid format: " + formatName);
}
}
var invalidEnums = [
'1',
'2',
'3',
'4',
'RGB4',
'RGB5',
'RGB8',
'RGB10',
'RGB12',
'RGB16',
'RGBA2',
'RGBA4',
'RGB5_A1',
'RGBA8',
'RGB10_A2',
'RGBA12',
'RGBA16',
'BGR',
'BGRA',
'ALPHA4_EXT',
'ALPHA8_EXT',
'ALPHA12_EXT',
'ALPHA16_EXT',
'COMPRESSED_ALPHA',
'COMPRESSED_LUMINANCE',
'COMPRESSED_LUMINANCE_ALPHA',
'COMPRESSED_INTENSITY',
'COMPRESSED_RGB',
'COMPRESSED_RGBA',
'DEPTH_COMPONENT16',
'DEPTH_COMPONENT24',
'DEPTH_COMPONENT32',
'LUMINANCE4_EXT',
'LUMINANCE8_EXT',
'LUMINANCE12_EXT',
'LUMINANCE16_EXT',
'LUMINANCE4_ALPHA4_EXT',
'LUMINANCE6_ALPHA2_EXT',
'LUMINANCE8_ALPHA8_EXT',
'LUMINANCE12_ALPHA4_EXT',
'LUMINANCE12_ALPHA12_EXT',
'LUMINANCE16_ALPHA16_EXT',
'INTENSITY_EXT',
'INTENSITY4_EXT',
'INTENSITY8_EXT',
'INTENSITY12_EXT',
'INTENSITY16_EXT',
'RGB4_EXT',
'RGB5_EXT',
'RGB8_EXT',
'RGB10_EXT',
'RGB12_EXT',
'RGB16_EXT',
'RGBA2_EXT',
'RGBA4_EXT',
'RGB5_A1_EXT',
'RGBA8_EXT',
'RGB10_A2_EXT',
'RGBA12_EXT',
'RGBA16_EXT',
'SLUMINANCE_EXT',
'SLUMINANCE8_EXT',
'SLUMINANCE_ALPHA_EXT',
'SLUMINANCE8_ALPHA8_EXT',
'SRGB_EXT',
'SRGB8_EXT',
'SRGB_ALPHA_EXT',
'SRGB8_ALPHA8'
];
for (var ii = 0; ii < invalidEnums.length; ++ii) {
var formatName = invalidEnums[ii]
if (desktopGL[formatName] === undefined) {
debug("bad format" + formatName)
} else {
testInvalidFormat(desktopGL[formatName], "GL_" + formatName);
}
}
var validEnums = [
'ALPHA',
'RGB',
'RGBA',
'LUMINANCE',
'LUMINANCE_ALPHA'
];
for (var ii = 0; ii < validEnums.length; ++ii) {
var formatName = validEnums[ii]
testValidFormat(gl[formatName], "gl." + formatName);
}
debug("");
debug("checking non 0 border parameter to gl.TexImage2D");
createTexture(gl['RGBA'], gl['RGBA'], 1);
assertMsg(gl.getError() == gl.INVALID_VALUE,
"non 0 border to gl.TexImage2D should return INVALID_VALUE");
function checkTypes() {
gl = initWebGL("canvas", "vshader", "fshader", [ "vPosition", "texCoord0"], [ 0, 0, 0, 1 ], 1);
gl.disable(gl.DEPTH_TEST);
gl.disable(gl.BLEND);
var vertexObject = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
gl.bufferData(gl.ARRAY_BUFFER, new WebGLFloatArray([ -1,1,0, 1,1,0, -1,-1,0,
-1,-1,0, 1,1,0, 1,-1,0
]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
var vertexObject = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
gl.bufferData(gl.ARRAY_BUFFER, new WebGLFloatArray([ 0,0, 1,0, 0,1,
0,1, 1,0, 1,1
]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(1);
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
var tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
var loc = gl.getUniformLocation(gl.program, "tex");
gl.uniform1i(loc, 0);
function checkType(r, g, b, a, type, format, buf) {
var typeName = WebGLDebugUtils.glEnumToString(type);
debug("");
debug("checking gl.texImage2D with type: " + typeName);
// Check that an NPOT texture not on level 0 generates INVALID_VALUE
gl.texImage2D(gl.TEXTURE_2D,
0, // level
format, // internalFormat
2, // width
2, // height
0, // border
format, // format
type, // type
buf); // data
assertMsg(gl.getError() == gl.NO_ERROR,
"gl.texImage2D with " + typeName + " should generate NO_ERROR");
checkBuffer(r,g,b,a, "texture type " + typeName + " should draw with " +
r + ", " + g + ", " + b + ", " + a);
function checkBuffer(r, g, b, a, msg) {
gl.clearColor(1,0,0,1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 6);
gl.flush();
var buf = gl.readPixels(0, 0, 2, 2, gl.RGBA, gl.UNSIGNED_BYTE);
for (var i = 0; i < 2 * 2; ++i) {
var offset = i * 4;
if (buf[offset + 0] != r ||
buf[offset + 1] != g ||
buf[offset + 2] != b ||
buf[offset + 3] != a) {
debug('expected:' + r + ', ' + g + ', ' + b + ', ' + a +
' was: ' +
buf[offset + 0] + ', ' +
buf[offset + 1] + ', ' +
buf[offset + 2] + ', ' +
buf[offset + 3]);
testFailed(msg);
return;
}
}
testPassed(msg);
}
}
checkType(0, 255, 0, 255, gl.UNSIGNED_BYTE, gl.RGBA,
new WebGLUnsignedByteArray([
0, 255, 0, 255,
0, 255, 0, 255,
0, 255, 0, 255,
0, 255, 0, 255]));
checkType(0, 0, 255, 255, gl.UNSIGNED_SHORT_4_4_4_4, gl.RGBA,
new WebGLUnsignedShortArray([
255, 255,
255, 255,
255, 255,
255, 255]));
checkType(0, 255, 0, 255, gl.UNSIGNED_SHORT_5_6_5, gl.RGB,
new WebGLUnsignedShortArray([
2016, 2016,
2016, 2016,
2016, 2016,
2016, 2016]));
checkType(0, 0, 255, 255, gl.UNSIGNED_SHORT_5_5_5_1, gl.RGBA,
new WebGLUnsignedShortArray([
63, 63,
63, 63,
63, 63,
63, 63]));
}
checkTypes();
}
debug("");
successfullyParsed = true;
</script>
<script src="../resources/js-test-post.js"></script>
<script>
</script>
</body>
</html>