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

283 lines
9.3 KiB
HTML

<!--
Copyright (c) 2010 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.
-->
<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 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;
var imageData = null;
function init()
{
if (window.initNonKhronosFramework) {
window.initNonKhronosFramework(true);
}
description('Verifies texImage2D and texSubImage2D code paths taking ArrayBufferView');
debug('');
var canvas2d = document.getElementById("texcanvas");
var context2d = canvas2d.getContext("2d");
imageData = context2d.createImageData(1, 2);
var data = imageData.data;
data[0] = 255;
data[1] = 0;
data[2] = 0;
data[3] = 255;
data[4] = 0;
data[5] = 255;
data[6] = 0;
data[7] = 0;
gl = initWebGL("example", "vshader", "fshader", [ "g_Position", "g_TexCoord0" ], [ 0, 0, 0, 1 ], 1);
gl.viewport(0, 0, 1, 2);
gl.disable(gl.BLEND);
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]);
var texCoords = new WebGLFloatArray([
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 1.0,
0.0, 0.0,
1.0, 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, gl.FALSE, 0, 0);
gl.enableVertexAttribArray(1);
gl.vertexAttribPointer(1, 2, gl.FLOAT, gl.FALSE, 0, texCoordOffset);
runTest();
}
// These two declarations need to be global for "shouldBe" to see them
var buf = null;
var idx = 0;
var pixel = [0, 0, 0, 1];
var correctColor = null;
function generateRGBAData(type, unpackAlignment)
{
var sourceData = [ 255, 0, 0, 255,
0, 255, 0, 0 ];
switch (type) {
case gl.UNSIGNED_BYTE: {
var rowWidth = Math.max(unpackAlignment, 4);
var data = new Uint8Array(2 * rowWidth);
for (var y = 0; y < 2; ++y) {
var index = y * rowWidth;
for (var element = 0; element < 4; ++element) {
data[index + element] = sourceData[4 * y + element];
}
}
return data;
}
case gl.UNSIGNED_SHORT_4_4_4_4: {
var rowWidth = Math.max(unpackAlignment, 2) / 2;
var data = new Uint16Array(2 * rowWidth);
for (var y = 0; y < 2; ++y) {
var index = y * rowWidth;
data[index] = (((sourceData[4 * y] & 0xF0) << 8)
| ((sourceData[4 * y + 1] & 0xF0) << 4)
| (sourceData[4 * y + 2] & 0xF0)
| (sourceData[4 * y + 3] >> 4));
}
return data;
}
case gl.UNSIGNED_SHORT_5_5_5_1: {
var rowWidth = Math.max(unpackAlignment, 2) / 2;
var data = new Uint16Array(2 * rowWidth);
for (var y = 0; y < 2; ++y) {
var index = y * rowWidth;
data[index] = (((sourceData[4 * y] & 0xF8) << 8)
| ((sourceData[4 * y + 1] & 0xF8) << 3)
| ((sourceData[4 * y + 2] & 0xF8) >> 2)
| (sourceData[4 * y + 3] >> 7));
}
return data;
}
}
}
function typeToString(type)
{
switch (type) {
case gl.UNSIGNED_BYTE: return 'UNSIGNED_BYTE';
case gl.UNSIGNED_SHORT_5_5_5_1: return 'UNSIGNED_SHORT_5_5_5_1';
case gl.UNSIGNED_SHORT_4_4_4_4: return 'UNSIGNED_SHORT_4_4_4_4';
}
return 'Unknown type ' + type;
}
function runOneIteration(useTexSubImage2D, type, unpackAlignment, flipY, premultiplyAlpha, topColor, bottomColor)
{
debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') +
' with type=' + typeToString(type) +
', unpackAlignment=' + unpackAlignment +
', flipY=' + flipY + ', premultiplyAlpha=' + premultiplyAlpha);
gl.colorMask(true, true, true, true);
gl.clearColor(0, 0, 0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Enable writes to the RGB channels
gl.colorMask(true, true, true, false);
var texture = gl.createTexture();
// Bind the texture to texture unit 0
gl.bindTexture(gl.TEXTURE_2D, texture);
// Set up texture parameters
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
// Set up pixel store parameters
gl.pixelStorei(gl.UNPACK_ALIGNMENT, unpackAlignment);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha);
// Generate the data
var data = generateRGBAData(type, unpackAlignment);
if (gl.getError() != gl.NO_ERROR)
testFailed("GL error before texture upload");
// Upload the image into the texture
if (useTexSubImage2D) {
// Initialize the texture to black first
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 2, 0,
gl.RGBA, type, null);
if (gl.getError() != gl.NO_ERROR)
testFailed("GL error after texImage2D(null)");
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 1, 2, gl.RGBA, type, data);
if (gl.getError() != gl.NO_ERROR)
testFailed("GL error after texSubImage2D");
} else {
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 2, 0, gl.RGBA, type, data);
if (gl.getError() != gl.NO_ERROR)
testFailed("GL error after texImage2D");
}
// 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
buf = new Uint8Array(1 * 2 * 4);
gl.readPixels(0, 0, 1, 2, gl.RGBA, gl.UNSIGNED_BYTE, buf);
// Check the top pixel and bottom pixel and make sure they have
// the right color.
debug("Checking bottom pixel");
correctColor = bottomColor;
idx = 0;
pixel[0] = buf[idx];
pixel[1] = buf[idx + 1];
pixel[2] = buf[idx + 2];
pixel[3] = buf[idx + 3];
shouldBe("pixel", "correctColor");
debug("Checking top pixel");
correctColor = topColor;
idx = 4;
pixel[0] = buf[idx];
pixel[1] = buf[idx + 1];
pixel[2] = buf[idx + 2];
pixel[3] = buf[idx + 3];
shouldBe("pixel", "correctColor");
}
function runTest()
{
var red = [255, 0, 0, 255];
var green = [0, 255, 0, 255];
var redPremultiplyAlpha = [255, 0, 0, 255];
var greenPremultiplyAlpha = [0, 0, 0, 255];
var types = [ gl.UNSIGNED_BYTE, gl.UNSIGNED_SHORT_5_5_5_1, gl.UNSIGNED_SHORT_4_4_4_4 ];
var unpackAlignments = [ 1, 2, 4, 8 ];
for (var i = 0; i < types.length; ++i) {
var type = types[i];
for (var j = 0; j < unpackAlignments.length; ++j) {
var unpackAlignment = unpackAlignments[j];
runOneIteration(false, type, unpackAlignment, true, false,
red, green);
runOneIteration(false, type, unpackAlignment, false, false,
green, red);
runOneIteration(false, type, unpackAlignment, true, true,
redPremultiplyAlpha, greenPremultiplyAlpha);
runOneIteration(false, type, unpackAlignment, false, true,
greenPremultiplyAlpha, redPremultiplyAlpha);
runOneIteration(true, type, unpackAlignment, true, false,
red, green);
runOneIteration(true, type, unpackAlignment, false, false,
green, red);
runOneIteration(true, type, unpackAlignment, true, true,
redPremultiplyAlpha, greenPremultiplyAlpha);
runOneIteration(true, type, unpackAlignment, false, true,
greenPremultiplyAlpha, redPremultiplyAlpha);
}
}
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.nonKhronosFrameworkNotifyDone) {
window.nonKhronosFrameworkNotifyDone();
}
}
</script>
</head>
<body onload="init()">
<canvas id="texcanvas" width="1px" height="2px"></canvas>
<canvas id="example" width="1px" height="2px"></canvas>
<div id="description"></div>
<div id="console"></div>
</body>
</html>