1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/canvas/test/webgl-conformance/conformance/extensions/oes-texture-float.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,215 @@ 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 +<title>WebGL OES_texture_float Conformance Tests</title> 1.14 +<link rel="stylesheet" href="../../resources/js-test-style.css"/> 1.15 +<script src="../../resources/desktop-gl-constants.js" type="text/javascript"></script> 1.16 +<script src="../../resources/js-test-pre.js"></script> 1.17 +<script src="../resources/webgl-test.js"></script> 1.18 +<script src="../resources/webgl-test-utils.js"></script> 1.19 +</head> 1.20 +<body> 1.21 +<div id="description"></div> 1.22 +<canvas id="canvas" style="width: 50px; height: 50px;"> </canvas> 1.23 +<div id="console"></div> 1.24 +<!-- Shaders for testing floating-point textures --> 1.25 +<script id="testFragmentShader" type="x-shader/x-fragment"> 1.26 +precision mediump float; 1.27 +uniform sampler2D tex; 1.28 +varying vec2 texCoord; 1.29 +void main() 1.30 +{ 1.31 + vec4 color = texture2D(tex, texCoord); 1.32 + if (abs(color.r - 10000.0) + 1.33 + abs(color.g - 10000.0) + 1.34 + abs(color.b - 10000.0) + 1.35 + abs(color.a - 10000.0) < 8.0) { 1.36 + gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); 1.37 + } else { 1.38 + gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 1.39 + } 1.40 +} 1.41 +</script> 1.42 +<!-- Shaders for testing floating-point render targets --> 1.43 +<script id="positionVertexShader" type="x-shader/x-vertex"> 1.44 +attribute vec4 vPosition; 1.45 +void main() 1.46 +{ 1.47 + gl_Position = vPosition; 1.48 +} 1.49 +</script> 1.50 +<script id="floatingPointFragmentShader" type="x-shader/x-fragment"> 1.51 +void main() 1.52 +{ 1.53 + gl_FragColor = vec4(10000.0, 10000.0, 10000.0, 10000.0); 1.54 +} 1.55 +</script> 1.56 +<script> 1.57 +description("This test verifies the functionality of the OES_texture_float extension, if it is available."); 1.58 + 1.59 +debug(""); 1.60 + 1.61 +var wtu = WebGLTestUtils; 1.62 +var canvas = document.getElementById("canvas"); 1.63 +var gl = create3DContext(canvas); 1.64 + 1.65 +if (!gl) { 1.66 + testFailed("WebGL context does not exist"); 1.67 +} else { 1.68 + testPassed("WebGL context exists"); 1.69 + 1.70 + var texturedShaders = [ 1.71 + wtu.setupSimpleTextureVertexShader(gl), 1.72 + "testFragmentShader" 1.73 + ]; 1.74 + var testProgram = 1.75 + wtu.setupProgram(gl, 1.76 + texturedShaders, 1.77 + ['vPosition', 'texCoord0'], 1.78 + [0, 1]); 1.79 + var quadParameters = wtu.setupUnitQuad(gl, 0, 1); 1.80 + 1.81 + // First verify that allocation of floating-point textures fails if 1.82 + // the extension has not been enabled yet. 1.83 + runTextureCreationTest(testProgram, false); 1.84 + 1.85 + if (!gl.getExtension("OES_texture_float")) { 1.86 + testPassed("No OES_texture_float support -- this is legal"); 1.87 + } else { 1.88 + testPassed("Successfully enabled OES_texture_float extension"); 1.89 + runTextureCreationTest(testProgram, true); 1.90 + runRenderTargetTest(testProgram); 1.91 + runUniqueObjectTest(); 1.92 + runReferenceCycleTest(); 1.93 + } 1.94 +} 1.95 + 1.96 +// Needs to be global for shouldBe to see it. 1.97 +var pixels; 1.98 + 1.99 +function allocateTexture() 1.100 +{ 1.101 + var texture = gl.createTexture(); 1.102 + gl.bindTexture(gl.TEXTURE_2D, texture); 1.103 + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); 1.104 + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); 1.105 + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); 1.106 + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); 1.107 + glErrorShouldBe(gl, gl.NO_ERROR, "texture parameter setup should succeed"); 1.108 + return texture; 1.109 +} 1.110 + 1.111 +function checkRenderingResults() 1.112 +{ 1.113 + pixels = new Uint8Array(4); 1.114 + gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels); 1.115 + // Outputs green if OK, red if not. 1.116 + shouldBe("pixels[0]", "0"); 1.117 + shouldBe("pixels[1]", "255"); 1.118 + shouldBe("pixels[2]", "0"); 1.119 + shouldBe("pixels[3]", "255"); 1.120 +} 1.121 + 1.122 +function runTextureCreationTest(testProgram, extensionEnabled) 1.123 +{ 1.124 + var expectFailure = !extensionEnabled; 1.125 + 1.126 + var texture = allocateTexture(); 1.127 + // Generate data. 1.128 + var width = 2; 1.129 + var height = 2; 1.130 + var numberOfChannels = 4; 1.131 + var data = new Float32Array(width * height * numberOfChannels); 1.132 + for (var ii = 0; ii < data.length; ++ii) { 1.133 + data[ii] = 10000; 1.134 + } 1.135 + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.FLOAT, data); 1.136 + if (expectFailure) { 1.137 + glErrorShouldBe(gl, gl.INVALID_ENUM, "floating-point texture allocation must be disallowed if OES_texture_float isn't enabled"); 1.138 + return; 1.139 + } else { 1.140 + glErrorShouldBe(gl, gl.NO_ERROR, "floating-point texture allocation should succeed if OES_texture_float is enabled"); 1.141 + } 1.142 + // Verify that the texture actually works for sampling and contains the expected data. 1.143 + gl.uniform1i(gl.getUniformLocation(testProgram, "tex"), 0); 1.144 + wtu.drawQuad(gl); 1.145 + checkRenderingResults(); 1.146 +} 1.147 + 1.148 +function runRenderTargetTest(testProgram) 1.149 +{ 1.150 + var texture = allocateTexture(); 1.151 + var width = 2; 1.152 + var height = 2; 1.153 + var numberOfChannels = 4; 1.154 + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.FLOAT, null); 1.155 + glErrorShouldBe(gl, gl.NO_ERROR, "floating-point texture allocation should succeed if OES_texture_float is enabled"); 1.156 + 1.157 + // Use this texture as a render target. 1.158 + var fbo = gl.createFramebuffer(); 1.159 + gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); 1.160 + gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0); 1.161 + gl.bindTexture(gl.TEXTURE_2D, null); 1.162 + shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE"); 1.163 + // While strictly speaking it is probably legal for a WebGL implementation to support 1.164 + // floating-point textures but not as attachments to framebuffer objects, any such 1.165 + // implementation is so poor that it arguably should not advertise support for the 1.166 + // OES_texture_float extension. For this reason the conformance test requires that the 1.167 + // framebuffer is complete here. 1.168 + if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) 1.169 + return; 1.170 + 1.171 + var renderProgram = 1.172 + wtu.setupProgram(gl, 1.173 + ["positionVertexShader", "floatingPointFragmentShader"], 1.174 + ['vPosition'], 1.175 + [0]); 1.176 + wtu.drawQuad(gl); 1.177 + glErrorShouldBe(gl, gl.NO_ERROR, "rendering to floating-point texture should succeed"); 1.178 + 1.179 + // Now sample from the floating-point texture and verify we got the correct values. 1.180 + gl.bindFramebuffer(gl.FRAMEBUFFER, null); 1.181 + gl.bindTexture(gl.TEXTURE_2D, texture); 1.182 + gl.useProgram(testProgram); 1.183 + gl.uniform1i(gl.getUniformLocation(testProgram, "tex"), 0); 1.184 + wtu.drawQuad(gl); 1.185 + glErrorShouldBe(gl, gl.NO_ERROR, "rendering from floating-point texture should succeed"); 1.186 + checkRenderingResults(); 1.187 +} 1.188 + 1.189 +function runUniqueObjectTest() 1.190 +{ 1.191 + debug("Testing that getExtension() returns the same object each time"); 1.192 + gl.getExtension("OES_texture_float").myProperty = 2; 1.193 + gc(); 1.194 + shouldBe('gl.getExtension("OES_texture_float").myProperty', '2'); 1.195 +} 1.196 + 1.197 +function runReferenceCycleTest() 1.198 +{ 1.199 + // create some reference cycles. The goal is to see if they cause leaks. The point is that 1.200 + // some browser test runners have instrumentation to detect leaked refcounted objects. 1.201 + 1.202 + debug("Testing reference cycles between context and extension objects"); 1.203 + var ext = gl.getExtension("OES_texture_float"); 1.204 + 1.205 + // create cycle between extension and context, since the context has to hold a reference to the extension 1.206 + ext.context = gl; 1.207 + 1.208 + // create a self-cycle on the extension object 1.209 + ext.ext = ext; 1.210 +} 1.211 + 1.212 +debug(""); 1.213 +successfullyParsed = true; 1.214 +</script> 1.215 +<script>finishTest();</script> 1.216 + 1.217 +</body> 1.218 +</html>