summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/textures/misc/copy-tex-image-2d-formats.html
blob: fd44f9cc73f113197f4adac0d8f50c331a604c06 (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
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
<!--
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>
<title>Verify copyTexImage2D follows format restictions</title>
<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>
</head>
<body>
<div id="description"></div>
<canvas id="canvas" width="2" height="2"></canvas>
<canvas id="canvasNoAlpha" width="2" height="2"></canvas>
<div id="console"></div>
<script>
"use strict";

function getChannelsFromFormat(format) {
  switch (gl[format]) {
  case gl.ALPHA:
    return 0x0001;
  case gl.LUMINANCE:
  case gl.RGB:
    return 0x1110;
  case gl.LUMINANCE_ALPHA:
  case gl.RGBA:
    return 0x1111;
  default:
    return 0;
  }
}

var formats = [
  'ALPHA',
  'LUMINANCE',
  'LUMINANCE_ALPHA',
  'RGB',
  'RGBA'
];

var isRenderable = {
  'ALPHA': false,
  'LUMINANCE': false,
  'LUMINANCE_ALPHA': false,
  'RGB': true,
  'RGBA': true
};

var gl = null;
var wtu = WebGLTestUtils;

description();

var canvas = document.getElementById("canvas");
var canvasNoAlpha = document.getElementById("canvasNoAlpha");
var gl = wtu.create3DContext(canvas, {alpha:true});
var glNoAlpha = wtu.create3DContext(canvasNoAlpha, {alpha:false});

debug("test with an RGBA backbuffer");
var program = wtu.setupTexturedQuad(gl);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "During Initialization");
testFormats('RGBA');

testBackbufferFormats();

debug("test with an RGB backbuffer");
var gl = glNoAlpha;
var program = wtu.setupTexturedQuad(gl);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "During Initialization");
testFormats('RGB');

function testBackbufferFormats() {
  var fbo = gl.createFramebuffer();
  gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);

  for (var ii = 0; ii < formats.length; ++ii) {
    var backFormat = formats[ii];

    var tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl[backFormat], 2, 2, 0, gl[backFormat], 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.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
    var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);

    debug('');
    if (status == gl.FRAMEBUFFER_COMPLETE) {
      if (!isRenderable[backFormat]) {
        testFailed('Creating framebuffer from ' + backFormat + ' texture succeeded even though it is not a renderable format');
      } else {
        debug('test with ' + backFormat + ' fbo');
        testFormats(backFormat);
      }
    } else {
      debug(backFormat + ' not supported as a renderbuffer attachment');
    }
  }
}

function testFormats(backFormat) {
  for (var ii = 0; ii < formats.length; ++ii) {
    testCopyTexImage2D(backFormat, formats[ii]);
  }
}

function toChannels(value) {
  return ((value & 0x1000) ? 'R' : '_') +
         ((value & 0x0100) ? 'G' : '_') +
         ((value & 0x0010) ? 'B' : '_') +
         ((value & 0x0001) ? 'A' : '_');
}

function testCopyTexImage2D(backFormat, texFormat) {
  var need = getChannelsFromFormat(texFormat);
  var have = getChannelsFromFormat(backFormat);
  var shouldPass = (need & have) == need;

  //debug("need: " + toChannels(need));
  //debug("have: " + toChannels(have));
  //debug("both: " + toChannels(have & need));

  // clear backbuffer
  gl.clearColor(0.25, 1, 0.75, 0.5);
  gl.clear(gl.COLOR_BUFFER_BIT);

  var texture = gl.createTexture();
  // Bind the texture to texture unit 0
  gl.bindTexture(gl.TEXTURE_2D, texture);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);

  gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl[texFormat], 0, 0, 2, 2, 0);
  if (!shouldPass) {
    wtu.glErrorShouldBe(
        gl, gl.INVALID_OPERATION,
        "should not be able to copyTexImage2D " + texFormat + " from " + backFormat);
    return;
  }

  wtu.glErrorShouldBe(
      gl, gl.NO_ERROR,
      "should be able to copyTexImage2D " + texFormat + " from " + backFormat);

  // Draw
  wtu.clearAndDrawUnitQuad(gl);

  var expectedColors = {
    'ALPHA': [0, 0, 0, 127],
    'LUMINANCE': [64, 64, 64, 255],
    'LUMINANCE_ALPHA': [64, 64, 64, 127],
    'RGB': [64, 255, 191, 255],
    'RGBA': [64, 255, 191, 127]
  };

  var color = expectedColors[texFormat];

  wtu.checkCanvas(gl, color, "should be " + color, 16);

  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
}
var successfullyParsed = true;
</script>
<script src="../../../js/js-test-post.js"></script>
</body>
</html>