summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/textures/misc/copy-tex-image-and-sub-image-2d.html
blob: a75158247227c9aaa6c62a9a85a841303d71d5f7 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!--
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">
<link rel="stylesheet" href="../../../resources/js-test-style.css"/>
<script src="../../../js/js-test-pre.js"></script>
<script src="../../../js/webgl-test-utils.js"></script>

<script>
"use strict";
var successfullyParsed = false;

function init()
{
    description('Verify copyTexImage2D and copyTexSubImage2D');

    runTest();
}

var gl = null;
var wtu = WebGLTestUtils;

function runTestIteration(antialias)
{
    var canvas = document.getElementById(
        antialias ? "antialiasOn" : "antialiasOff");
    var attribs = antialias ? { antialias: true } : { antialias: false };
    gl = wtu.create3DContext(canvas, attribs);
    var program = wtu.setupTexturedQuad(gl);
    var textureLoc = gl.getUniformLocation(program, "tex");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "During Initialization");

    gl.colorMask(1, 1, 1, 1);
    gl.disable(gl.BLEND);
    debug('Testing copyTexImage2D');

    // Red canvas
    gl.clearColor(1, 0, 0, 1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

    var texture = gl.createTexture();
    // Bind the texture to texture unit 0
    gl.bindTexture(gl.TEXTURE_2D, texture);
    // Set up texture
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 2, 2, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
    gl.uniform1i(textureLoc, 0);

    var colors = [
      [1, 0, 0, 1],
      [0, 1, 0, 1],
      [0, 0, 1, 1],
      [0.5, 0.5, 0.5, 0.5],
    ];
    var data = new Uint8Array(2 * 2 * 4);
    for (var ii = 0; ii < 2 * 2 * 4; ++ii)
      data[ii] = 136; // A random number other than 0.
    var count = 0;
    for (var yy = -2; yy <= 2; ++yy) {
      for (var xx = -2; xx <= 2; ++xx) {
        for (var ii = 0; ii < 2; ++ii) {
          var texColor = colors[count];
          var clearColor = colors[(count + 1) % colors.length];
          // clear to some color
          gl.clearColor(texColor[0], texColor[1], texColor[2], texColor[3]);
          gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

          // copy that color to the texture.
          switch (ii) {
          case 0:
            gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, xx, yy, 2, 2, 0);
            wtu.glErrorShouldBe(gl, gl.NO_ERROR,
                "using copyTexImage2D: x = " + xx + ", y = " + yy);
            break;
          case 1:
            gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 2, 2, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
            gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, xx, yy, 2, 2);
            wtu.glErrorShouldBe(gl, gl.NO_ERROR,
                "using copyTexSubImage2D: x = " + xx + ", y = " + yy);
            break;
          }

          // clear to some other color.
          gl.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
          gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

          // Draw the triangles
          wtu.clearAndDrawUnitQuad(gl);

          // check the rendering results
          for (var iy = 0; iy < 2; ++iy) {
            for (var ix = 0; ix < 2; ++ix) {
              var x = xx + ix;
              var y = yy + iy;
              var expectedColor = (x < 0 || y < 0 || x >= 2 || y >= 2) ?
                  (ii == 0 ? [0, 0, 0, 0] : [136, 136, 136, 136]) :
                  [Math.floor(255 * texColor[0]),
                   Math.floor(255 * texColor[1]),
                   Math.floor(255 * texColor[2]),
                   Math.floor(255 * texColor[3])];
              wtu.checkCanvasRect(gl, ix, iy, 1, 1, expectedColor,
                  "" + ix + ", " + iy + " should render " + expectedColor + " (+/-1)", 1);
            }
          }
          count = (count + 1) % colors.length;
        }
      }
    }

    debug("");
}

function runTest(antialias)
{
    debug("Testing with antialias on");
    runTestIteration(true);
    debug("Testing with antialias off");
    runTestIteration(false);

    finishTest();
}
</script>
</head>
<body onload="init()">
<canvas id="antialiasOn" width="2" height="2"></canvas>
<canvas id="antialiasOff" width="2" height="2"></canvas>
<div id="description"></div>
<div id="console"></div>
</body>
</html>