summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/extra/out-of-resources.html
blob: 8e88c1ebfd7dd532971b19361a66ebd13b7a32cf (plain)
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
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL Out Of Resources 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>
<div id="description"></div>
<div id="console"></div>
<canvas id="canvas" width="2" height="2"> </canvas>
<canvas id="canvas2" width="2" height="2"> </canvas>
<script>
"use strict";
wtu = WebGLTestUtils;
window.onload = init;
debug("Tests a WebGL program that tries to use all of vram.");

function init() {
  if (confirm(
      "After clicking OK your machine may become unresponsive or crash.")) {
    main();
  } else {
    debug("cancelled");
  }
}

function main() {
  debug("");
  debug("Canvas.getContext");

  var gl = wtu.create3DContext("canvas");
  if (!gl) {
    testFailed("context does not exist");
  } else {
    testPassed("context exists");

    var program = wtu.setupColorQuad(gl);
    gl.useProgram(program);

    debug("");
    debug("Checking for out of memory handling.");

    var size = gl.getParameter(gl.MAX_RENDERBUFFER_SIZE);
    debug("max render buffer size: " + size);

    var allocateFramebuffers = true;
    var itervalId;
    var count = 0;

    gl = WebGLDebugUtils.makeDebugContext(gl, function(err, functionName, args) {
          assertMsg(err == gl.OUT_OF_MEMORY,
                    "correctly returns gl.OUT_OF_MEMORY when out of memory");
          stop("got: " + wtu.glEnumToString(gl, err));
        });

    var fbos = [];

    intervalId = window.setInterval(function() {
      ++count;
      var mem = count * size * size * 4;
      debug("#" + count + " : memory allocated so far " + (mem / 1024 / 1024) + "MB");
      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);
      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
      var err = gl.getError();
      if (err != gl.NO_ERROR) {
        stop("got error: " + wtu.glEnumToString(gl, err));
        return;
      }
      if (allocateFramebuffers) {
        var fb = gl.createFramebuffer();
        gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
        gl.framebufferTexture2D(
            gl.FRAMEBUFFER,
            gl.COLOR_ATTACHMENT0,
            gl.TEXTURE_2D,
            tex,
            0);
        var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
        if (status != gl.FRAMEBUFFER_COMPLETE) {
          stop("got: " + wtu.glEnumToString(gl, status));
          return;
        }
        // use the framebuffer
        wtu.drawFloatColorQuad(gl, [1, Math.random(), 1, 1]);
      }
    }, 200);

    function stop(msg) {
      debug(msg);
      window.clearInterval(intervalId);
      finishTest();
    }
  }
}
</script>
</body>
</html>