Bug 1932347 - Adjust positioning of tab preview for vertical tabs r=tabbrowser-review...
[gecko.git] / dom / canvas / test / webgl-conf / checkout / extra / big-fbos-example.html
blob0e0a09bcef21ec2756da236f69734712ec6aee6b
1 <!--
2 Copyright (c) 2019 The Khronos Group Inc.
3 Use of this source code is governed by an MIT-style license that can be
4 found in the LICENSE.txt file.
5 -->
7 <!DOCTYPE html>
8 <html>
9 <head>
10 <meta charset="utf-8">
11 <title>WebGL Big FBO Test</title>
12 <link rel="stylesheet" href="../resources/js-test-style.css"/>
13 <script src="../../devtools/src/debug/webgl-debug.js"></script>
14 <script src="../js/js-test-pre.js"></script>
15 <script src="../js/webgl-test-utils.js"></script>
16 </head>
17 <body>
18 <canvas id="canvas" width="256" height="256"> </canvas>
19 <div id="description"></div>
20 <div id="console"></div>
21 <script id="vshader" type="x-shader/x-vertex">
22 attribute vec4 vPosition;
23 attribute vec2 texCoord0;
24 varying vec2 texCoord;
25 void main()
27 gl_Position = vec4(vPosition.xyz, 1.0);
28 texCoord = texCoord0;
30 </script>
32 <script id="fshader" type="x-shader/x-fragment">
33 precision mediump float;
34 uniform sampler2D tex;
35 varying vec2 texCoord;
36 void main()
38 gl_FragColor = texture2D(tex, texCoord);
40 </script>
41 <script>
42 "use strict";
43 window.onload = init;
45 var g_textures = [];
47 debug("Tests the performance of using lots of large FBOs");
49 function init() {
50 if (confirm(
51 "After clicking OK your machine may become unresponsive or crash.")) {
52 main();
53 } else {
54 debug("cancelled");
58 function checkFBOStatus(gl) {
59 var err = gl.getError();
60 if (err != gl.NO_ERROR) {
61 if (err != gl.OUT_OF_MEMORY)
62 testFailed("gl.getError returned " + err);
63 else
64 testPassed("OUT-OF-MEMORY");
65 return false;
67 var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
68 if (status != gl.FRAMEBUFFER_COMPLETE) {
69 testFailed("gl.checkFramebufferStatus() returned " + WebGLTestUtils.glEnumToString(gl, status));
70 return false;
72 return true;
75 function setupFBO(gl, size) {
76 var tex = gl.createTexture();
77 gl.bindTexture(gl.TEXTURE_2D, tex);
78 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
79 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
80 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
81 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
83 var fb = gl.createFramebuffer();
84 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
85 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
87 gl.texImage2D(gl.TEXTURE_2D,
88 0, // level
89 gl.RGBA, // internalFormat
90 size, // width
91 size, // height
92 0, // border
93 gl.RGBA, // format
94 gl.UNSIGNED_BYTE, // type
95 null); // data
96 if (!checkFBOStatus(gl))
97 return null;
99 return { fb: fb, tex: tex };
102 function checkPixels(gl) {
103 var width = 256;
104 var height = 256;
106 var thresh = 3;
108 var buf = new Uint8Array(width * height * 4);
109 gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
110 for (var yy = 0; yy < height; ++yy) {
111 for (var xx = 0; xx < width; ++xx) {
112 var offset = (yy * width + xx) * 4;
113 if (Math.abs(buf[offset] - 255) > thresh ||
114 Math.abs(buf[offset + 1] - 0) > thresh ||
115 Math.abs(buf[offset + 2] - 0) > thresh) {
116 testFailed("drawing results incorrect");
117 return false;
121 return true;
124 function handleContextLost() {
125 debug("context lost");
128 function main() {
129 debug("");
130 debug("Checking for out of memory handling.");
132 var canvas = document.getElementById("canvas");
133 canvas.addEventListener('webglcontextlost', handleContextLost);
134 var wtu = WebGLTestUtils;
135 var gl = wtu.create3DContext("canvas");
136 var prog = wtu.setupProgram(gl, ["vshader", "fshader"], ["vPosition", "texCoord0"]);
138 WebGLDebugUtils.init(gl);
140 gl.disable(gl.DEPTH_TEST);
141 gl.disable(gl.BLEND);
143 var vertexObject = gl.createBuffer();
144 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
145 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
146 -1,1,0, 1,1,0, -1,-1,0,
147 -1,-1,0, 1,1,0, 1,-1,0
148 ]), gl.STATIC_DRAW);
149 gl.enableVertexAttribArray(0);
150 gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
152 var vertexObject = gl.createBuffer();
153 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
154 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,0, 1,0, 0,1,
155 0,1, 1,0, 1,1
156 ]), gl.STATIC_DRAW);
157 gl.enableVertexAttribArray(1);
158 gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
160 var texLoc = gl.getUniformLocation(prog, "tex");
161 gl.uniform1i(texLoc, 0);
163 gl.clearColor(0, 0, 0, 1);
164 gl.clear(gl.COLOR_BUFFER_BIT);
166 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "setup should succeed");
168 var size = gl.getParameter(gl.MAX_RENDERBUFFER_SIZE);
169 debug("max render buffer size: " + size +
170 ", size used: " + (size / 2));
171 size /= 2;
173 var maxFBOs = 200;
174 var numFBOs = 0;
175 allocateNextFBO();
177 function allocateNextFBO() {
178 if (numFBOs >= maxFBOs) {
179 phase2();
180 return;
182 if (!allocateFBO()) {
183 phase2();
184 return;
186 ++numFBOs;
187 setTimeout(allocateNextFBO, 100);
190 function allocateFBO() {
191 debug("");
192 debug("trying to create fbo #" + (numFBOs + 1));
193 var t = setupFBO(gl, 2);
194 if (!t) {
195 return false;
198 var tex = t.tex;
199 var fb = t.fb;
201 debug("allocating fbo color buffer of size " + size + " x " + size);
202 gl.texImage2D(gl.TEXTURE_2D,
203 0, // level
204 gl.RGBA, // internalFormat
205 size, // width
206 size, // height
207 0, // border
208 gl.RGBA, // format
209 gl.UNSIGNED_BYTE, // type
210 null); // data
211 if (!checkFBOStatus(gl)) {
212 return false;
214 g_textures.push(tex);
215 debug("succeeded in creating fbo");
217 debug("clearing the fbo with red color");
218 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
219 gl.clearColor(1, 0, 0, 1);
220 gl.clear(gl.COLOR_BUFFER_BIT);
222 debug("deleting fbo, but the now red texture should be untouched");
223 gl.bindFramebuffer(gl.FRAMEBUFFER, null);
224 gl.deleteFramebuffer(fb);
226 debug("drawing to the canvas using the red texture");
227 gl.clearColor(0, 0, 0, 1);
228 gl.clear(gl.COLOR_BUFFER_BIT);
229 gl.drawArrays(gl.TRIANGLES, 0, 6);
230 if (!checkPixels(gl)) {
231 return false;
234 debug("succeeded in drawing");
235 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "each run with no error");
236 return true;
239 function phase2() {
240 gl.bindFramebuffer(gl.FRAMEBUFFER, null);
242 debug("");
243 debug("fbos allocated:" + numFBOs);
244 if (!checkPixels(gl)) {
245 testFailed("final check of canvas drawing buffer pixels failed");
247 debug("");
248 finishTest();
252 var successfullyParsed = true;
253 </script>
255 </body>
256 </html>