summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/textures/misc/texture-attachment-formats.html
blob: b569ca061df9ef83cf10dcbaf90ad60901b18e6f (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
174
175
176
<!--
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 Texture Attachment Format Conformance Tests</title>
<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>
<div id="console"></div>
<canvas id="canvas" width="2" height="2" style="width: 100px; height:100px; border: 1px solid black;"> </canvas>
<script>
"use strict";
description();

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

  debug("");
  debug("Checking texture formats.");

  var numValidFormats = 0;
  var clearColor = [0.25, 0.5, 0.75, 0.25];

  var floatToBits = function(value, bits) {
    var range = (1 << bits) - 1;
    var result = 0;
    if (range > 0) {
      result = Math.floor(Math.floor(value * range) * 255 / range);
    }

    //debug("v = " + value + ", bits = " + bits + ", range = " + range  + ", result = " + result);
    return result;
  }

  var testFormat = function(info) {
    debug("");
    debug("testing: " + info.format + ", " + info.type);

    var format = gl[info.format];
    var type = gl[info.type];

    gl.texImage2D(gl.TEXTURE_2D,
                  0,                 // level
                  format,            // internalFormat
                  16,                // width
                  16,                // height
                  0,                 // border
                  format,            // format
                  type,              // type
                  null);             // data
    var fbStatus = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
    debug(wtu.glEnumToString(gl, fbStatus));
    if (fbStatus != gl.FRAMEBUFFER_COMPLETE) {
      debug("format unsupported");
      if (info.mustBeFramebufferComplete) {
        testFailed(info.format + " must be FRAMEBUFFER_COMPLETE");
      }
      return;
    }

    ++numValidFormats;

    var startExpected = [0, 0, 0, info.channels[3] < 0 ? 255 : 0];

    var expected = [];
    var tolerance = [];
    for (var ii = 0; ii < 4; ++ii) {
      var color = 0;
      var channel = info.channels[ii];
      if (channel < 0) {
        color = ii < 3 ? 0 : 255
      } else {
        color = floatToBits(clearColor[channel], info.bits[ii]);
      }
      expected.push(color);
      tolerance.push(channel < 0 ? 0 : (1 + (1 << (8 - info.bits[ii]))));
    }

    wtu.checkCanvas(gl, startExpected, "should be " + startExpected);
    gl.clear(gl.COLOR_BUFFER_BIT);
    wtu.checkCanvas(gl, expected, "should be " + expected + " with tolerance " + tolerance, tolerance);
  }

  var validFormats = [
    { format: 'RGBA',
      type:  'UNSIGNED_BYTE',
      channels: [0, 1, 2, 3],
      bits: [8, 8, 8, 8],
      mustBeFramebufferComplete: true
    },
    { format: 'ALPHA',
      type:  'UNSIGNED_BYTE',
      channels: [-1, -1, -1, 3],
      bits: [0, 0, 0, 8],
      mustBeFramebufferComplete: false
    },
    { format: 'RGB',
      type:  'UNSIGNED_BYTE',
      channels: [0, 1, 2, -1],
      bits: [8, 8, 8, 0],
      mustBeFramebufferComplete: false
    },
    { format: 'RGB',
      type:  'UNSIGNED_SHORT_5_6_5',
      channels: [0, 1, 2, -1],
      bits: [5, 6, 5, 0],
      mustBeFramebufferComplete: false
    },
    { format: 'RGBA',
      type:  'UNSIGNED_SHORT_5_5_5_1',
      channels: [0, 1, 2, 3],
      bits: [5, 5, 5, 1],
      mustBeFramebufferComplete: false
    },
    { format: 'RGBA',
      type:  'UNSIGNED_SHORT_4_4_4_4',
      channels: [0, 1, 2, 3],
      bits: [4, 4, 4, 4],
      mustBeFramebufferComplete: false
    },
    { format: 'LUMINANCE',
      type:  'UNSIGNED_BYTE',
      channels: [0, 0, 0, -1],
      bits: [8, 8, 8, -1],
      mustBeFramebufferComplete: false
    },
    { format: 'LUMINANCE_ALPHA',
      type:  'UNSIGNED_BYTE',
      channels: [0, 0, 0, 3],
      bits: [8, 8, 8, 8],
      mustBeFramebufferComplete: false
    }
  ];

  gl.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
  var fbo = gl.createFramebuffer();
  gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
  var tex = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_2D, tex);
  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.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  gl.framebufferTexture2D(
      gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);

  for (var ii = 0; ii < validFormats.length; ++ii) {
    var info = validFormats[ii];
    testFormat(info);
  }

  debug("");
  shouldBeTrue("numValidFormats > 0");
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
}

debug("");
var successfullyParsed = true;
</script>
<script src="../../../js/js-test-post.js"></script>

</body>
</html>