content/canvas/test/webgl-conformance/extra/big-fbos-example.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 Big FBO 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 window.onload = init;
michael@0 45 debug("Tests the performance of using lots of large FBOs");
michael@0 46
michael@0 47 function init() {
michael@0 48 if (confirm(
michael@0 49 "after clicking ok your machine may be come unresponsive or crash")) {
michael@0 50 main();
michael@0 51 } else {
michael@0 52 debug("cancelled");
michael@0 53 }
michael@0 54 }
michael@0 55
michael@0 56 function main() {
michael@0 57 debug("");
michael@0 58 debug("Canvas.getContext");
michael@0 59 var g_worldLoc;
michael@0 60 var g_texLoc;
michael@0 61 var g_textures = [];
michael@0 62 gl = initWebGL("canvas", "vshader", "fshader", [ "vPosition", "texCoord0"], [ 0, 0, 0, 1 ], 1);
michael@0 63 if (!gl) {
michael@0 64 testFailed("context does not exist");
michael@0 65 } else {
michael@0 66 testPassed("context exists");
michael@0 67
michael@0 68 debug("");
michael@0 69 debug("Checking for out of memory handling.");
michael@0 70
michael@0 71 var size = gl.getParameter(gl.MAX_RENDERBUFFER_SIZE);
michael@0 72 debug("max render buffer size: " + size);
michael@0 73 size = size / 2;
michael@0 74 debug("size used: " + size);
michael@0 75
michael@0 76 var allocateFramebuffers = true;
michael@0 77 var intervalId = 0;
michael@0 78 var count = 0;
michael@0 79
michael@0 80 WebGLDebugUtils.init(gl);
michael@0 81
michael@0 82 function createFBO() {
michael@0 83 var tex = gl.createTexture();
michael@0 84 gl.bindTexture(gl.TEXTURE_2D, tex);
michael@0 85 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
michael@0 86 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
michael@0 87 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
michael@0 88 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
michael@0 89 gl.texImage2D(gl.TEXTURE_2D,
michael@0 90 0, // level
michael@0 91 gl.RGBA, // internalFormat
michael@0 92 size, // width
michael@0 93 size, // height
michael@0 94 0, // border
michael@0 95 gl.RGBA, // format
michael@0 96 gl.UNSIGNED_BYTE, // type
michael@0 97 null); // data
michael@0 98 var fb = gl.createFramebuffer();
michael@0 99 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
michael@0 100 gl.framebufferTexture2D(
michael@0 101 gl.FRAMEBUFFER,
michael@0 102 gl.COLOR_ATTACHMENT0,
michael@0 103 gl.TEXTURE_2D,
michael@0 104 tex,
michael@0 105 0);
michael@0 106 var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
michael@0 107 if (status != gl.FRAMEBUFFER_COMPLETE) {
michael@0 108 testFailed("gl.checkFramebufferStatus() returned " + WebGLDebugUtils.glEnumToString(status));
michael@0 109 return;
michael@0 110 }
michael@0 111 var err = gl.getError();
michael@0 112 if (err != gl.NO_ERROR) {
michael@0 113 if (err != gl.OUT_OF_MEMORY) {
michael@0 114 testFailed("gl.getError returned " + err);
michael@0 115 }
michael@0 116 return;
michael@0 117 }
michael@0 118 return { fb: fb, tex: tex };
michael@0 119 }
michael@0 120
michael@0 121 gl.disable(gl.DEPTH_TEST);
michael@0 122
michael@0 123 var maxFBOs = 2;
michael@0 124 for (var ii = 0; ii < maxFBOs; ++ii) {
michael@0 125 createFBO();
michael@0 126 var t = createFBO();
michael@0 127 if (!t) {
michael@0 128 break;
michael@0 129 }
michael@0 130 tex = t.tex;
michael@0 131 fb = t.fb;
michael@0 132
michael@0 133 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
michael@0 134 gl.scissor(0, 0, size, size);
michael@0 135 gl.clearColor(0, ii / maxFBOs, 1 - ii / maxFBOs, 1);
michael@0 136 gl.clear(gl.COLOR_BUFFER_BIT);
michael@0 137 g_textures.push(tex);
michael@0 138 }
michael@0 139
michael@0 140 debug("fbos allocated:" + g_textures.length);
michael@0 141 gl.scissor(0, 0, 1024, 1024);
michael@0 142 gl.bindFramebuffer(gl.FRAMEBUFFER, null);
michael@0 143
michael@0 144 var vertexObject = gl.createBuffer();
michael@0 145 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
michael@0 146 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
michael@0 147 -1,1,0, 1,1,0, -1,-1,0,
michael@0 148 -1,-1,0, 1,1,0, 1,-1,0
michael@0 149 ]), gl.STATIC_DRAW);
michael@0 150 gl.enableVertexAttribArray(0);
michael@0 151 gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
michael@0 152
michael@0 153 var vertexObject = gl.createBuffer();
michael@0 154 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
michael@0 155 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,0, 1,0, 0,1,
michael@0 156 0,1, 1,0, 1,1
michael@0 157 ]), gl.STATIC_DRAW);
michael@0 158 gl.enableVertexAttribArray(1);
michael@0 159 gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
michael@0 160
michael@0 161 g_texLoc = gl.getUniformLocation(gl.program, "tex");
michael@0 162 gl.uniform1i(g_texLoc, 0);
michael@0 163 g_worldLoc = gl.getUniformLocation(gl.program, "world");
michael@0 164 gl.uniformMatrix4fv(g_worldLoc, false, [
michael@0 165 0, 0, 0, 0,
michael@0 166 0, 0, 0, 0,
michael@0 167 0, 0, 1, 0,
michael@0 168 0, 0, 0, 1]);
michael@0 169
michael@0 170 setInterval(render, 1000/60);
michael@0 171 }
michael@0 172
michael@0 173 var g_angle = 0;
michael@0 174 var g_texIndex = 0;
michael@0 175 function render() {
michael@0 176 g_angle += 0.1;
michael@0 177 g_texIndex++;
michael@0 178 if (g_texIndex >= g_textures.length) {
michael@0 179 g_texIndex = 0;
michael@0 180 }
michael@0 181 gl.bindTexture(gl.TEXTURE_2D, g_textures[g_texIndex]);
michael@0 182 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
michael@0 183 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
michael@0 184 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
michael@0 185 gl.uniformMatrix4fv(g_worldLoc, false, rotationZ(g_angle));
michael@0 186 gl.clearColor(1,0,0,1);
michael@0 187 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 188 gl.drawArrays(gl.TRIANGLES, 0, 6);
michael@0 189 }
michael@0 190 }
michael@0 191
michael@0 192 /**
michael@0 193 * Creates a 4-by-4 matrix which rotates around the z-axis by the given angle.
michael@0 194 * @param {number} angle The angle by which to rotate (in radians).
michael@0 195 * @return {!o3djs.math.Matrix4} The rotation matrix.
michael@0 196 */
michael@0 197 function rotationZ(angle) {
michael@0 198 var c = Math.cos(angle);
michael@0 199 var s = Math.sin(angle);
michael@0 200
michael@0 201 return [
michael@0 202 c, s, 0, 0,
michael@0 203 -s, c, 0, 0,
michael@0 204 0, 0, 1, 0,
michael@0 205 0, 0, 0, 1
michael@0 206 ];
michael@0 207 };
michael@0 208
michael@0 209 debug("");
michael@0 210 successfullyParsed = true;
michael@0 211 </script>
michael@0 212 <script>
michael@0 213 </script>
michael@0 214
michael@0 215 </body>
michael@0 216 </html>

mercurial