content/canvas/test/webgl-conformance/conformance/extensions/oes-texture-float.html

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial