content/canvas/test/webgl-conformance/extra/fbo-lost-context.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!--
michael@0 2 Copyright (c) 2009 The Chromium Authors. All rights reserved.
michael@0 3 Use of this source code is governed by a BSD-style license that can be
michael@0 4 found in the LICENSE file.
michael@0 5 -->
michael@0 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
michael@0 7 "http://www.w3.org/TR/html4/loose.dtd">
michael@0 8 <html>
michael@0 9 <head>
michael@0 10 <meta charset="utf-8">
michael@0 11 <title>WebGL FBO Lost Context Test</title>
michael@0 12 <link rel="stylesheet" href="../resources/js-test-style.css"/>
michael@0 13 <script src="../resources/desktop-gl-constants.js" type="text/javascript"></script>
michael@0 14 <script src="../../debug/webgl-debug.js"></script>
michael@0 15 <script src="../resources/js-test-pre.js"></script>
michael@0 16 <script src="../conformance/resources/webgl-test.js"></script>
michael@0 17 </head>
michael@0 18 <body>
michael@0 19 <div id="description"></div>
michael@0 20 <div id="console"></div>
michael@0 21 <script id="vshader" type="x-shader/x-vertex">
michael@0 22 attribute vec4 vPosition;
michael@0 23 attribute vec2 texCoord0;
michael@0 24 uniform mat4 world;
michael@0 25 varying vec2 texCoord;
michael@0 26 void main()
michael@0 27 {
michael@0 28 gl_Position = vPosition * world;
michael@0 29 texCoord = texCoord0;
michael@0 30 }
michael@0 31 </script>
michael@0 32
michael@0 33 <script id="fshader" type="x-shader/x-fragment">
michael@0 34 precision mediump float;
michael@0 35 uniform sampler2D tex;
michael@0 36 varying vec2 texCoord;
michael@0 37 void main()
michael@0 38 {
michael@0 39 gl_FragColor = texture2D(tex, texCoord);
michael@0 40 }
michael@0 41 </script>
michael@0 42 <canvas id="canvas" width="1024" height="1024"> </canvas>
michael@0 43 <script>
michael@0 44 description("This test is to help see if an WebGL app *can* get lost context.");
michael@0 45
michael@0 46 debug("");
michael@0 47 debug("Canvas.getContext");
michael@0 48 var g_worldLoc;
michael@0 49 var g_texLoc;
michael@0 50 var g_textures = [];
michael@0 51 gl = initWebGL("canvas", "vshader", "fshader", [ "vPosition", "texCoord0"], [ 0, 0, 0, 1 ], 1);
michael@0 52 if (!gl) {
michael@0 53 testFailed("context does not exist");
michael@0 54 } else {
michael@0 55 testPassed("context exists");
michael@0 56
michael@0 57 debug("");
michael@0 58 debug("Checking for out of memory handling.");
michael@0 59
michael@0 60 var size = gl.getParameter(gl.MAX_RENDERBUFFER_SIZE);
michael@0 61 debug("max render buffer size: " + size);
michael@0 62 size = size / 2;
michael@0 63 debug("size used: " + size);
michael@0 64
michael@0 65 var allocateFramebuffers = true;
michael@0 66 var itervalId;
michael@0 67 var count = 0;
michael@0 68
michael@0 69 gl = WebGLDebugUtils.makeDebugContext(gl, function(err, functionName, args) {
michael@0 70 window.clearInterval(intervalId);
michael@0 71 assertMsg(err == gl.OUT_OF_MEMORY,
michael@0 72 "correctly returns gl.OUT_OF_MEMORY when out of memory");
michael@0 73 finish();
michael@0 74 });
michael@0 75
michael@0 76 function createFBO() {
michael@0 77 var tex = gl.createTexture();
michael@0 78 gl.bindTexture(gl.TEXTURE_2D, tex);
michael@0 79 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
michael@0 80 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
michael@0 81 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
michael@0 82 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
michael@0 83 gl.texImage2D(gl.TEXTURE_2D,
michael@0 84 0, // level
michael@0 85 gl.RGBA, // internalFormat
michael@0 86 size, // width
michael@0 87 size, // height
michael@0 88 0, // border
michael@0 89 gl.RGBA, // format
michael@0 90 gl.UNSIGNED_BYTE, // type
michael@0 91 null); // data
michael@0 92 var fb = gl.createFramebuffer();
michael@0 93 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
michael@0 94 gl.framebufferTexture2D(
michael@0 95 gl.FRAMEBUFFER,
michael@0 96 gl.COLOR_ATTACHMENT0,
michael@0 97 gl.TEXTURE_2D,
michael@0 98 tex,
michael@0 99 0);
michael@0 100 var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
michael@0 101 if (status != gl.FRAMEBUFFER_COMPLETE) {
michael@0 102 testFailed("gl.checkFramebufferStatus() returned " + WebGLDebugUtils.glEnumToString(status));
michael@0 103 }
michael@0 104 return { fb: fb, tex: tex };
michael@0 105 }
michael@0 106
michael@0 107 gl.disable(gl.DEPTH_TEST);
michael@0 108
michael@0 109 var numFBOs = 32;
michael@0 110 for (var ii = 0; ii < numFBOs; ++ii) {
michael@0 111 createFBO();
michael@0 112 var t = createFBO();
michael@0 113 tex = t.tex;
michael@0 114 fb = t.fb;
michael@0 115
michael@0 116 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
michael@0 117 gl.scissor(0, 0, size, size);
michael@0 118 gl.clearColor(0, ii / numFBOs, 1 - ii / numFBOs, 1);
michael@0 119 gl.clear(gl.COLOR_BUFFER_BIT);
michael@0 120 g_textures.push(tex);
michael@0 121 }
michael@0 122
michael@0 123 gl.bindFramebuffer(gl.FRAMEBUFFER, null);
michael@0 124
michael@0 125 var vertexObject = gl.createBuffer();
michael@0 126 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
michael@0 127 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
michael@0 128 -1,1,0, 1,1,0, -1,-1,0,
michael@0 129 -1,-1,0, 1,1,0, 1,-1,0
michael@0 130 ]), gl.STATIC_DRAW);
michael@0 131 gl.enableVertexAttribArray(0);
michael@0 132 gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
michael@0 133
michael@0 134 var vertexObject = gl.createBuffer();
michael@0 135 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
michael@0 136 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
michael@0 137 0,0, 1,0, 0,1,
michael@0 138 0,1, 1,0, 1,1
michael@0 139 ]), gl.STATIC_DRAW);
michael@0 140 gl.enableVertexAttribArray(1);
michael@0 141 gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
michael@0 142
michael@0 143 gl.bindTexture(gl.TEXTURE_2D, tex);
michael@0 144 g_texLoc = gl.getUniformLocation(gl.program, "tex");
michael@0 145 gl.uniform1i(g_texLoc, 0);
michael@0 146 g_worldLoc = gl.getUniformLocation(gl.program, "world");
michael@0 147 gl.uniformMatrix4fv(g_worldLoc, false, [
michael@0 148 0, 0, 0, 0,
michael@0 149 0, 0, 0, 0,
michael@0 150 0, 0, 1, 0,
michael@0 151 0, 0, 0, 1]);
michael@0 152
michael@0 153 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
michael@0 154 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
michael@0 155 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
michael@0 156
michael@0 157 setInterval(render, 1000/60);
michael@0 158 }
michael@0 159
michael@0 160 var g_angle = 0;
michael@0 161 var g_texIndex = 0;
michael@0 162 function render() {
michael@0 163 g_angle += 0.1;
michael@0 164 g_texIndex++;
michael@0 165 if (g_texIndex >= g_textures.length) {
michael@0 166 g_texIndex = 0;
michael@0 167 }
michael@0 168 gl.bindTexture(gl.TEXTURE_2D, g_textures[g_texIndex]);
michael@0 169 gl.uniformMatrix4fv(g_worldLoc, false, rotationZ(g_angle));
michael@0 170 gl.clearColor(1,0,0,1);
michael@0 171 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 172 gl.drawArrays(gl.TRIANGLES, 0, 6);
michael@0 173 }
michael@0 174
michael@0 175 /**
michael@0 176 * Creates a 4-by-4 matrix which rotates around the z-axis by the given angle.
michael@0 177 * @param {number} angle The angle by which to rotate (in radians).
michael@0 178 * @return {!o3djs.math.Matrix4} The rotation matrix.
michael@0 179 */
michael@0 180 function rotationZ(angle) {
michael@0 181 var c = Math.cos(angle);
michael@0 182 var s = Math.sin(angle);
michael@0 183
michael@0 184 return [
michael@0 185 c, s, 0, 0,
michael@0 186 -s, c, 0, 0,
michael@0 187 0, 0, 1, 0,
michael@0 188 0, 0, 0, 1
michael@0 189 ];
michael@0 190 };
michael@0 191
michael@0 192 debug("");
michael@0 193 successfullyParsed = true;
michael@0 194 </script>
michael@0 195 <script>
michael@0 196 </script>
michael@0 197
michael@0 198 </body>
michael@0 199 </html>

mercurial