2 Copyright (c) 2019 The Khronos Group Inc.
3 Use of this source code is governed by an MIT-style license that can be
4 found in the LICENSE.txt file.
10 <meta charset=
"utf-8">
11 <title>vertexattribpointer offsets test
</title>
12 <link rel=
"stylesheet" href=
"../../resources/js-test-style.css"/>
13 <script src=
"../../js/js-test-pre.js"></script>
14 <script src=
"../../js/webgl-test-utils.js"> </script>
17 <canvas id=
"example" width=
"50" height=
"50">
18 There is supposed to be an example drawing here, but it's not important.
20 <div id=
"description"></div>
21 <div id=
"console"></div>
22 <script id=
"vshader" type=
"x-shader/x-vertex">
23 attribute vec4 vPosition;
26 gl_Position = vPosition;
30 <script id=
"fshader" type=
"x-shader/x-fragment">
31 precision mediump float;
43 description("test vertexattribpointer offsets work");
45 var wtu
= WebGLTestUtils
;
46 var gl
= wtu
.create3DContext("example");
47 var program
= wtu
.setupProgram(gl
, ["vshader", "fshader"], ["vPosition"]);
50 { data
: new Float32Array([ 0, 1, 0, 1, 0, 0, 0, 0, 0 ]),
55 { data
: new Uint16Array([ 0, 32767, 0, 32767, 0, 0, 0, 0, 0]),
60 { data
: new Uint16Array([ 0, 65535, 0, 65535, 0, 0, 0, 0, 0 ]),
61 type
: gl
.UNSIGNED_SHORT
,
65 { data
: new Uint16Array([ 0, 1, 0, 1, 0, 0, 0, 0, 0 ]),
66 type
: gl
.UNSIGNED_SHORT
,
70 { data
: new Uint16Array([ 0, 1, 0, 1, 0, 0, 0, 0, 0 ]),
75 { data
: new Uint8Array([ 0, 127, 0, 127, 0, 0, 0, 0, 0 ]),
80 { data
: new Uint8Array([ 0, 255, 0, 255, 0, 0, 0, 0, 0 ]),
81 type
: gl
.UNSIGNED_BYTE
,
85 { data
: new Uint8Array([ 0, 1, 0, 1, 0, 0, 0, 0, 0 ]),
90 { data
: new Uint8Array([ 0, 1, 0, 1, 0, 0, 0, 0, 0 ]),
91 type
: gl
.UNSIGNED_BYTE
,
97 if (wtu
.getDefault3DContextVersion() >= 2) {
99 { data
: new Int32Array([ 0, 1, 0, 1, 0, 0, 0, 0, 0]),
104 { data
: new Int32Array([ 0, 2147483647, 0, 2147483647, 0, 0, 0, 0, 0]),
109 { data
: new Uint32Array([ 0, 1, 0, 1, 0, 0, 0, 0, 0]),
110 type
: gl
.UNSIGNED_INT
,
114 { data
: new Uint32Array([ 0, 4294967295, 0, 4294967295, 0, 0, 0, 0, 0]),
115 type
: gl
.UNSIGNED_INT
,
119 { data
: new Uint16Array([ 0, 0b11110000000000, 0, 0b11110000000000, 0, 0, 0, 0, 0]),
124 { data
: new Uint16Array([ 0, 0b11110000000000, 0, 0b11110000000000, 0, 0, 0, 0, 0]),
132 var vertexObject
= gl
.createBuffer();
133 gl
.bindBuffer(gl
.ARRAY_BUFFER
, vertexObject
);
134 gl
.bufferData(gl
.ARRAY_BUFFER
, 1024, gl
.STATIC_DRAW
);
135 gl
.enableVertexAttribArray(0);
137 var colorLoc
= gl
.getUniformLocation(program
, "color");
139 var kNumComponents
= 3;
142 for (var tt
= 0; tt
< tests
.length
; ++tt
) {
143 var test
= tests
[tt
];
144 for (var oo
= 0; oo
< 3; ++oo
) {
145 for (var ss
= 0; ss
< 3; ++ss
) {
146 var offset
= (oo
+ 1) * test
.componentSize
;
147 var color
= (count
% 2) ? [1, 0, 0, 1] : [0, 1, 0, 1];
148 var stride
= test
.componentSize
* kNumComponents
+ test
.componentSize
* ss
;
150 debug("check with " + wtu
.glEnumToString(gl
, test
.type
) + " at offset: " + offset
+ " with stride:" + stride
+ " normalize: " + test
.normalize
);
151 gl
.uniform4fv(colorLoc
, color
);
152 var data
= new Uint8Array(test
.componentSize
* kNumVerts
* kNumComponents
+ stride
* (kNumVerts
- 1));
153 var view
= new Uint8Array(test
.data
.buffer
);
154 var size
= test
.componentSize
* kNumComponents
;
155 for (var jj
= 0; jj
< kNumVerts
; ++jj
) {
156 var off1
= jj
* size
;
157 var off2
= jj
* stride
;
158 for (var zz
= 0; zz
< size
; ++zz
) {
159 data
[off2
+ zz
] = view
[off1
+ zz
];
162 gl
.bufferSubData(gl
.ARRAY_BUFFER
, offset
, data
);
163 gl
.vertexAttribPointer(0, 3, test
.type
, test
.normalize
, stride
, offset
);
164 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.DEPTH_BUFFER_BIT
);
165 gl
.drawArrays(gl
.TRIANGLES
, 0, 3);
167 var buf
= new Uint8Array(50 * 50 * 4);
168 gl
.readPixels(0, 0, 50, 50, gl
.RGBA
, gl
.UNSIGNED_BYTE
, buf
);
170 var black
= [0, 0, 0, 0];
171 var other
= [color
[0] * 255, color
[1] * 255, color
[2] * 255, color
[3] * 255];
172 var otherMsg
= "should be " + ((count
% 2) ? "red" : "green")
173 wtu
.checkCanvasRect(gl
, 0, 0, 1, 1, black
, "should be black", 0);
174 wtu
.checkCanvasRect(gl
, 0, 49, 1, 1, black
, "should be black", 0);
175 wtu
.checkCanvasRect(gl
, 26, 40, 1, 1, other
, otherMsg
, 0);
176 wtu
.checkCanvasRect(gl
, 26, 27, 1, 1, other
, otherMsg
, 0);
177 wtu
.checkCanvasRect(gl
, 40, 27, 1, 1, other
, otherMsg
, 0);
185 var successfullyParsed
= true;
187 <script src=
"../../js/js-test-post.js"></script>