summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/attribs/gl-vertexattribpointer.html
blob: 1276cec7c9cc9c8c88511f7d963e7195a641ab24 (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
<!--
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 vertexAttribPointer 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"> </canvas>
<script>
"use strict";
description("This test checks vertexAttribPointer behaviors in WebGL.");

debug("");
debug("Canvas.getContext");

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

  debug("");
  debug("Checking gl.vertexAttribPointer.");

  if (!gl.FIXED) {
    gl.FIXED = 0x140C;
  }

  var vertexObject = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(0), gl.STATIC_DRAW);


  gl.bindBuffer(gl.ARRAY_BUFFER, null);
  gl.vertexAttribPointer(0, 1, gl.FLOAT, false, 0, 4);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION,
      "vertexAttribPointer should fail if no buffer is bound and `offset` is non-zero.");

  gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
  gl.vertexAttribPointer(0, 1, gl.FLOAT, false, 0, 0);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR);

  gl.bindBuffer(gl.ARRAY_BUFFER, null);
  gl.vertexAttribPointer(0, 1, gl.FLOAT, false, 0, 0);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR,
      "vertexAttribPointer should succeed if no buffer is bound and `offset` is zero.");

  gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);


  if (wtu.getDefault3DContextVersion() < 2) {
    gl.vertexAttribPointer(0, 1, gl.INT, 0, 0, 0);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM,
            "vertexAttribPointer should not support INT");
    gl.vertexAttribPointer(0, 1, gl.UNSIGNED_INT, 0, 0, 0);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM,
            "vertexAttribPointer should not support UNSIGNED_INT");
  }
  gl.vertexAttribPointer(0, 1, gl.FIXED, 0, 0, 0);
  wtu.glErrorShouldBe(gl, gl.INVALID_ENUM,
          "vertexAttribPointer should not support FIXED");

  var checkVertexAttribPointer = function(
      gl, err, reason, size, type, normalize, stride, offset) {
    gl.vertexAttribPointer(0, size, type, normalize, stride, offset);
    var succeeded = (err == gl.NO_ERROR);
    wtu.glErrorShouldBe(gl, err,
        "gl.vertexAttribPointer(0, " + size +
        ", gl." + wtu.glEnumToString(gl, type) +
        ", " + normalize +
        ", " + stride +
        ", " + offset +
        ") should " + (succeeded ? "succeed " : "fail ") + reason);
    if (succeeded) {
      shouldBe('gl.getVertexAttrib(0, gl.VERTEX_ATTRIB_ARRAY_SIZE)', size.toString());
      shouldBe('gl.getVertexAttrib(0, gl.VERTEX_ATTRIB_ARRAY_TYPE)', 'gl.' + wtu.glEnumToString(gl, type));
      shouldBe('gl.getVertexAttrib(0, gl.VERTEX_ATTRIB_ARRAY_NORMALIZED)', normalize.toString());
      shouldBe('gl.getVertexAttrib(0, gl.VERTEX_ATTRIB_ARRAY_STRIDE)', stride.toString());
      shouldBe('gl.getVertexAttribOffset(0, gl.VERTEX_ATTRIB_ARRAY_POINTER)', offset.toString());
    }
  }

  var types = [
    { type:gl.BYTE,           bytesPerComponent: 1 },
    { type:gl.UNSIGNED_BYTE,  bytesPerComponent: 1 },
    { type:gl.SHORT,          bytesPerComponent: 2 },
    { type:gl.UNSIGNED_SHORT, bytesPerComponent: 2 },
    { type:gl.FLOAT,          bytesPerComponent: 4 },
  ];

  if (wtu.getDefault3DContextVersion() >= 2) {
    types.push(...[
        { type:gl.INT,                         bytesPerComponent: 4 },
        { type:gl.UNSIGNED_INT,                bytesPerComponent: 4 },
        { type:gl.HALF_FLOAT,                  bytesPerComponent: 2 },
        { type:gl.INT_2_10_10_10_REV,          bytesPerComponent: 4, minSize: 4 },
        { type:gl.UNSIGNED_INT_2_10_10_10_REV, bytesPerComponent: 4, minSize: 4 },
      ]);
  }

  for (var ii = 0; ii < types.length; ++ii) {
    var info = types[ii];
    debug("");
    for (var size = 1; size <= 4; ++size) {
      debug("");
      debug("checking: " + wtu.glEnumToString(gl, info.type) + " with size " + size);
      var bytesPerElement = size * info.bytesPerComponent;
      var offsetSet = [
          0,
          1,
          info.bytesPerComponent - 1,
          info.bytesPerComponent,
          info.bytesPerComponent + 1,
          info.bytesPerComponent * 2];
      for (var jj = 0; jj < offsetSet.length; ++jj) {
        var offset = offsetSet[jj];
        for (var kk = 0; kk < offsetSet.length; ++kk) {
          var stride = offsetSet[kk];
          var err = gl.NO_ERROR;
          var reason = ""
          if (offset % info.bytesPerComponent != 0) {
            reason = "because offset is bad";
            err = gl.INVALID_OPERATION;
          }
          if (stride % info.bytesPerComponent != 0) {
            reason = "because stride is bad";
            err = gl.INVALID_OPERATION;
          }
          if (size < info.minSize) {
            reason = "because size < minSize";
            err = gl.INVALID_OPERATION;
          }
          checkVertexAttribPointer(
              gl, err, reason, size, info.type, false, stride, offset);
        }
        var stride = Math.floor(255 / info.bytesPerComponent) * info.bytesPerComponent;

        if (offset == 0) {
          checkVertexAttribPointer(
              gl, size < info.minSize ? gl.INVALID_OPERATION : gl.NO_ERROR, "at stride limit",
              size, info.type, false, stride, offset);
          checkVertexAttribPointer(
              gl, size < info.minSize ? [gl.INVALID_OPERATION, gl.INVALID_VALUE] : gl.INVALID_VALUE, "over stride limit",
              size, info.type, false,
              stride + info.bytesPerComponent, offset);
        }
      }
    }
  }
}

debug("");
var successfullyParsed = true;

</script>
<script src="../../js/js-test-post.js"></script>

</body>
</html>