1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
<!--
/*
** Copyright (c) 2012 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL Big FBO Test</title>
<link rel="stylesheet" href="../resources/js-test-style.css"/>
<script src="../../devtools/src/debug/webgl-debug.js"></script>
<script src="../js/js-test-pre.js"></script>
<script src="../js/webgl-test-utils.js"></script>
</head>
<body>
<canvas id="canvas" width="256" height="256"> </canvas>
<div id="description"></div>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">
attribute vec4 vPosition;
attribute vec2 texCoord0;
varying vec2 texCoord;
void main()
{
gl_Position = vec4(vPosition.xyz, 1.0);
texCoord = texCoord0;
}
</script>
<script id="fshader" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D tex;
varying vec2 texCoord;
void main()
{
gl_FragColor = texture2D(tex, texCoord);
}
</script>
<script>
"use strict";
window.onload = init;
var g_textures = [];
debug("Tests the performance of using lots of large FBOs");
function init() {
if (confirm(
"After clicking OK your machine may become unresponsive or crash.")) {
main();
} else {
debug("cancelled");
}
}
function checkFBOStatus(gl) {
var err = gl.getError();
if (err != gl.NO_ERROR) {
if (err != gl.OUT_OF_MEMORY)
testFailed("gl.getError returned " + err);
else
testPassed("OUT-OF-MEMORY");
return false;
}
var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
if (status != gl.FRAMEBUFFER_COMPLETE) {
testFailed("gl.checkFramebufferStatus() returned " + WebGLDebugUtils.glEnumToString(status));
return false;
}
return true;
}
function setupFBO(gl, size) {
var tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
var fb = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
gl.texImage2D(gl.TEXTURE_2D,
0, // level
gl.RGBA, // internalFormat
size, // width
size, // height
0, // border
gl.RGBA, // format
gl.UNSIGNED_BYTE, // type
null); // data
if (!checkFBOStatus(gl))
return null;
return { fb: fb, tex: tex };
}
function checkPixels(gl) {
var width = 256;
var height = 256;
var thresh = 3;
var buf = new Uint8Array(width * height * 4);
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
for (var yy = 0; yy < height; ++yy) {
for (var xx = 0; xx < width; ++xx) {
var offset = (yy * width + xx) * 4;
if (Math.abs(buf[offset] - 255) > thresh ||
Math.abs(buf[offset + 1] - 0) > thresh ||
Math.abs(buf[offset + 2] - 0) > thresh) {
testFailed("drawing results incorrect");
return false;
}
}
}
return true;
}
function handleContextLost() {
debug("context lost");
}
function main() {
debug("");
debug("Checking for out of memory handling.");
var canvas = document.getElementById("canvas");
canvas.addEventListener('webglcontextlost', handleContextLost);
var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("canvas");
var prog = wtu.setupProgram(gl, ["vshader", "fshader"], ["vPosition", "texCoord0"]);
WebGLDebugUtils.init(gl);
gl.disable(gl.DEPTH_TEST);
gl.disable(gl.BLEND);
var vertexObject = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
-1,1,0, 1,1,0, -1,-1,0,
-1,-1,0, 1,1,0, 1,-1,0
]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
var vertexObject = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,0, 1,0, 0,1,
0,1, 1,0, 1,1
]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(1);
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
var texLoc = gl.getUniformLocation(prog, "tex");
gl.uniform1i(texLoc, 0);
gl.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "setup should succeed");
var size = gl.getParameter(gl.MAX_RENDERBUFFER_SIZE);
debug("max render buffer size: " + size +
", size used: " + (size / 2));
size /= 2;
var maxFBOs = 200;
var numFBOs = 0;
allocateNextFBO();
function allocateNextFBO() {
if (numFBOs >= maxFBOs) {
phase2();
return;
}
if (!allocateFBO()) {
phase2();
return;
}
++numFBOs;
setTimeout(allocateNextFBO, 100);
}
function allocateFBO() {
debug("");
debug("trying to create fbo #" + (numFBOs + 1));
var t = setupFBO(gl, 2);
if (!t) {
return false;
}
var tex = t.tex;
var fb = t.fb;
debug("allocating fbo color buffer of size " + size + " x " + size);
gl.texImage2D(gl.TEXTURE_2D,
0, // level
gl.RGBA, // internalFormat
size, // width
size, // height
0, // border
gl.RGBA, // format
gl.UNSIGNED_BYTE, // type
null); // data
if (!checkFBOStatus(gl)) {
return false;
}
g_textures.push(tex);
debug("succeeded in creating fbo");
debug("clearing the fbo with red color");
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
gl.clearColor(1, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
debug("deleting fbo, but the now red texture should be untouched");
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.deleteFramebuffer(fb);
debug("drawing to the canvas using the red texture");
gl.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 6);
if (!checkPixels(gl)) {
return false;
}
debug("succeeded in drawing");
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "each run with no error");
return true;
}
function phase2() {
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
debug("");
debug("fbos allocated:" + numFBOs);
if (!checkPixels(gl)) {
testFailed("final check of canvas drawing buffer pixels failed");
}
debug("");
finishTest();
}
}
var successfullyParsed = true;
</script>
</body>
</html>
|