1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
<!--
Copyright (c) 2011 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>
<html>
<head>
<meta charset="utf-8">
<title>WebGL OES_texture_float 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="../../resources/js-test-pre.js"></script>
<script src="../resources/webgl-test.js"></script>
<script src="../resources/webgl-test-utils.js"></script>
</head>
<body>
<div id="description"></div>
<canvas id="canvas" style="width: 50px; height: 50px;"> </canvas>
<div id="console"></div>
<!-- Shaders for testing floating-point textures -->
<script id="testFragmentShader" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D tex;
varying vec2 texCoord;
void main()
{
vec4 color = texture2D(tex, texCoord);
if (abs(color.r - 10000.0) +
abs(color.g - 10000.0) +
abs(color.b - 10000.0) +
abs(color.a - 10000.0) < 8.0) {
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
} else {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
}
</script>
<!-- Shaders for testing floating-point render targets -->
<script id="positionVertexShader" type="x-shader/x-vertex">
attribute vec4 vPosition;
void main()
{
gl_Position = vPosition;
}
</script>
<script id="floatingPointFragmentShader" type="x-shader/x-fragment">
void main()
{
gl_FragColor = vec4(10000.0, 10000.0, 10000.0, 10000.0);
}
</script>
<script>
description("This test verifies the functionality of the OES_texture_float extension, if it is available.");
debug("");
var wtu = WebGLTestUtils;
var canvas = document.getElementById("canvas");
var gl = create3DContext(canvas);
if (!gl) {
testFailed("WebGL context does not exist");
} else {
testPassed("WebGL context exists");
var texturedShaders = [
wtu.setupSimpleTextureVertexShader(gl),
"testFragmentShader"
];
var testProgram =
wtu.setupProgram(gl,
texturedShaders,
['vPosition', 'texCoord0'],
[0, 1]);
var quadParameters = wtu.setupUnitQuad(gl, 0, 1);
// First verify that allocation of floating-point textures fails if
// the extension has not been enabled yet.
runTextureCreationTest(testProgram, false);
if (!gl.getExtension("OES_texture_float")) {
testPassed("No OES_texture_float support -- this is legal");
} else {
testPassed("Successfully enabled OES_texture_float extension");
runTextureCreationTest(testProgram, true);
runRenderTargetTest(testProgram);
runUniqueObjectTest();
runReferenceCycleTest();
}
}
// Needs to be global for shouldBe to see it.
var pixels;
function allocateTexture()
{
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);
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);
glErrorShouldBe(gl, gl.NO_ERROR, "texture parameter setup should succeed");
return texture;
}
function checkRenderingResults()
{
pixels = new Uint8Array(4);
gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
// Outputs green if OK, red if not.
shouldBe("pixels[0]", "0");
shouldBe("pixels[1]", "255");
shouldBe("pixels[2]", "0");
shouldBe("pixels[3]", "255");
}
function runTextureCreationTest(testProgram, extensionEnabled)
{
var expectFailure = !extensionEnabled;
var texture = allocateTexture();
// Generate data.
var width = 2;
var height = 2;
var numberOfChannels = 4;
var data = new Float32Array(width * height * numberOfChannels);
for (var ii = 0; ii < data.length; ++ii) {
data[ii] = 10000;
}
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.FLOAT, data);
if (expectFailure) {
wtu.glErrorShouldBeIn(gl, [gl.INVALID_ENUM, gl.INVALID_OPERATION],
"floating-point texture allocation must be disallowed if OES_texture_float isn't enabled");
return;
} else {
glErrorShouldBe(gl, gl.NO_ERROR, "floating-point texture allocation should succeed if OES_texture_float is enabled");
}
// Verify that the texture actually works for sampling and contains the expected data.
gl.uniform1i(gl.getUniformLocation(testProgram, "tex"), 0);
wtu.drawQuad(gl);
checkRenderingResults();
}
function runRenderTargetTest(testProgram)
{
var texture = allocateTexture();
var width = 2;
var height = 2;
var numberOfChannels = 4;
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.FLOAT, null);
glErrorShouldBe(gl, gl.NO_ERROR, "floating-point texture allocation should succeed if OES_texture_float is enabled");
// Use this texture as a render target.
var fbo = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
gl.bindTexture(gl.TEXTURE_2D, null);
// Enabling OES_texture_float will either implicitly enable the entirety of
// WEBGL_color_buffer_float, or it will not enable any of it. If it is not
// enabled, we'll get an FB_INCOMPLETE_ATTACHMENT here.
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT");
return;
}
shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE");
var renderProgram =
wtu.setupProgram(gl,
["positionVertexShader", "floatingPointFragmentShader"],
['vPosition'],
[0]);
wtu.drawQuad(gl);
glErrorShouldBe(gl, gl.NO_ERROR, "rendering to floating-point texture should succeed");
// Now sample from the floating-point texture and verify we got the correct values.
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.useProgram(testProgram);
gl.uniform1i(gl.getUniformLocation(testProgram, "tex"), 0);
wtu.drawQuad(gl);
glErrorShouldBe(gl, gl.NO_ERROR, "rendering from floating-point texture should succeed");
checkRenderingResults();
}
function runUniqueObjectTest()
{
debug("Testing that getExtension() returns the same object each time");
gl.getExtension("OES_texture_float").myProperty = 2;
gc();
shouldBe('gl.getExtension("OES_texture_float").myProperty', '2');
}
function runReferenceCycleTest()
{
// create some reference cycles. The goal is to see if they cause leaks. The point is that
// some browser test runners have instrumentation to detect leaked refcounted objects.
debug("Testing reference cycles between context and extension objects");
var ext = gl.getExtension("OES_texture_float");
// create cycle between extension and context, since the context has to hold a reference to the extension
ext.context = gl;
// create a self-cycle on the extension object
ext.ext = ext;
}
debug("");
successfullyParsed = true;
</script>
<script>finishTest();</script>
</body>
</html>
|