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>WebGL getBufferSubData 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 <div id=
"description"></div>
18 <div id=
"console"></div>
21 description("This test makes sure that getBufferSubData acts as expected governed by WebGL 2.");
23 var wtu
= WebGLTestUtils
;
25 var gl
= wtu
.create3DContext(undefined, undefined, 2);
32 var floatArray
= new Float32Array(vertices
);
34 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "Should be no errors from setup.");
36 var buffer
= gl
.createBuffer();
37 gl
.bindBuffer(gl
.ARRAY_BUFFER
, buffer
);
38 gl
.bufferData(gl
.ARRAY_BUFFER
, floatArray
, gl
.STATIC_DRAW
);
40 var uninitializedBuffer
= gl
.createBuffer();
41 gl
.bindBuffer(gl
.ARRAY_BUFFER
, uninitializedBuffer
);
42 gl
.bufferData(gl
.ARRAY_BUFFER
, 36, gl
.STATIC_DRAW
);
43 gl
.bindBuffer(gl
.ARRAY_BUFFER
, null);
45 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "Should be no errors from buffer setup.");
47 gl
.bindBuffer(gl
.ARRAY_BUFFER
, buffer
);
50 debug("Test that getBufferSubData successfully works reading buffer data from gl.ARRAY_BUFFER");
51 var retArray
= new Float32Array(vertices
.length
);
52 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray)");
54 debug("Argument must be ArrayBufferView, not ArrayBuffer")
55 shouldThrow("gl.getBufferSubData(gl.ARRAY_BUFFER, 0, new ArrayBuffer(4))");
56 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "Should not generate GL error");
57 debug("Argument must be ArrayBufferView, not null")
58 shouldThrow("gl.getBufferSubData(gl.ARRAY_BUFFER, 0, null)");
59 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "Should not generate GL error");
61 debug("Check array data to match original data set by the buffer");
62 shouldBeTrue("areArraysEqual(retArray, floatArray)");
64 debug("Test that getBufferSubData successfully works with dstOffset");
65 retArray
= new Float32Array(vertices
.length
);
66 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray, 2)");
67 shouldBeTrue("areArraysEqual(retArray.slice(0, 2), [0, 0])");
68 shouldBeTrue("areArraysEqual(retArray.slice(2), floatArray.slice(0, floatArray.length - 2))");
70 retArray
= new Float32Array(vertices
.length
);
71 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray, retArray.length)");
72 shouldBeTrue("areArraysEqual(retArray, [0, 0, 0, 0, 0, 0, 0, 0, 0])");
74 debug("Test that getBufferSubData fails when given a dstOffset beyond the end of retArray");
75 wtu
.shouldGenerateGLError(gl
, gl
.INVALID_VALUE
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray, retArray.length + 1)");
77 debug("Test that getBufferSubData successfully works with dstOffset and length");
78 retArray
= new Float32Array(vertices
.length
);
79 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray, 2, 2)");
80 shouldBeTrue("areArraysEqual(retArray.slice(0, 2), [0, 0])");
81 shouldBeTrue("areArraysEqual(retArray.slice(2, 4), floatArray.slice(0, 2))");
82 shouldBeTrue("areArraysEqual(retArray.slice(4), [0, 0, 0, 0, 0])");
84 retArray
= new Float32Array(vertices
.length
);
85 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray, retArray.length - 1, 1)");
86 shouldBeTrue("areArraysEqual(retArray.slice(0, 8), [0, 0, 0, 0, 0, 0, 0, 0])");
87 shouldBeTrue("areArraysEqual(retArray.slice(8), floatArray.slice(0, 1))");
89 debug("Test that getBufferSubData fails when given a dstOffset+length beyond the end of retArray");
90 wtu
.shouldGenerateGLError(gl
, gl
.INVALID_VALUE
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray, retArray.length - 1, 2)");
92 debug("Test that getBufferSubData successfully works with srcByteOffset");
93 retArray
= new Float32Array(vertices
.length
- 2);
94 const float32Size
= Float32Array
.BYTES_PER_ELEMENT
;
95 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 2*float32Size, retArray)");
96 shouldBeTrue("areArraysEqual(retArray, floatArray.slice(2))");
98 retArray
= new Float32Array(vertices
.length
);
99 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 2*float32Size, retArray, 2)");
100 shouldBeTrue("areArraysEqual(retArray.slice(0, 2), [0, 0])");
101 shouldBeTrue("areArraysEqual(retArray.slice(2), floatArray.slice(2))");
103 retArray
= new Float32Array(vertices
.length
);
104 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 4*float32Size, retArray, 3, 3)");
105 shouldBeTrue("areArraysEqual(retArray.slice(0, 3), [0, 0, 0])");
106 shouldBeTrue("areArraysEqual(retArray.slice(3, 6), floatArray.slice(4, 7))");
107 shouldBeTrue("areArraysEqual(retArray.slice(6), [0, 0, 0])");
109 debug("Test that getBufferSubData fails when given a buffer with its size larger than the original data");
110 var extraLargeBuffer
= new Float32Array(vertices
.length
+ 1);
111 wtu
.shouldGenerateGLError(gl
, gl
.INVALID_VALUE
,
112 "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, extraLargeBuffer)",
113 "Extra length should generate INVALID_VALUE.");
114 extraLargeBuffer
= new Float32Array(vertices
.length
- 1);
115 wtu
.shouldGenerateGLError(gl
, gl
.INVALID_VALUE
,
116 "gl.getBufferSubData(gl.ARRAY_BUFFER, 2*float32Size, extraLargeBuffer)",
117 "Extra length should generate INVALID_VALUE.");
119 debug("Test that getBufferSubData fails when offset summed with buffer length is larger than the size of the original data size");
120 wtu
.shouldGenerateGLError(gl
, gl
.INVALID_VALUE
,
121 "gl.getBufferSubData(gl.ARRAY_BUFFER, retArray.byteLength + 1, retArray)");
122 wtu
.shouldGenerateGLError(gl
, gl
.INVALID_VALUE
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 1, retArray)");
124 debug("Test that getBufferSubData successfully works with offset view into ArrayBuffer");
125 const verticesLengthInBytes
= vertices
.length
* float32Size
;
126 const retStorage
= new ArrayBuffer(4* verticesLengthInBytes
); // Over allocate 4x
127 retArray
= new Float32Array(retStorage
, verticesLengthInBytes
, vertices
.length
);
128 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray)");
129 shouldBeTrue("areArraysEqual(retArray, floatArray)");
131 debug("Test that getBufferSubData successfully works with offset view into ArrayBuffer and dstOffset");
132 retArray
= new Float32Array(retStorage
, verticesLengthInBytes
, vertices
.length
);
134 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray, 2)");
135 shouldBeTrue("areArraysEqual(retArray.slice(0, 2), [0, 0])");
136 shouldBeTrue("areArraysEqual(retArray.slice(2), floatArray.slice(0, floatArray.length - 2))");
138 retArray
= new Float32Array(retStorage
, verticesLengthInBytes
, vertices
.length
);
140 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray, retArray.length)");
141 shouldBeTrue("areArraysEqual(retArray, [0, 0, 0, 0, 0, 0, 0, 0, 0])");
143 debug("Test that getBufferSubData fails when given a dstOffset beyond the end of offset view into ArrayBuffer");
144 wtu
.shouldGenerateGLError(gl
, gl
.INVALID_VALUE
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray, retArray.length + 1)");
146 debug("Test that getBufferSubData successfully works with offset view into ArrayBuffer and dstOffset and length");
147 retArray
= new Float32Array(retStorage
, verticesLengthInBytes
, vertices
.length
);
149 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray, 2, 2)");
150 shouldBeTrue("areArraysEqual(retArray.slice(0, 2), [0, 0])");
151 shouldBeTrue("areArraysEqual(retArray.slice(2, 4), floatArray.slice(0, 2))");
152 shouldBeTrue("areArraysEqual(retArray.slice(4), [0, 0, 0, 0, 0])");
154 retArray
= new Float32Array(retStorage
, verticesLengthInBytes
, vertices
.length
);
156 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray, retArray.length - 1, 1)");
157 shouldBeTrue("areArraysEqual(retArray.slice(0, 8), [0, 0, 0, 0, 0, 0, 0, 0])");
158 shouldBeTrue("areArraysEqual(retArray.slice(8), floatArray.slice(0, 1))");
160 debug("Test that getBufferSubData fails when given a dstOffset+length beyond the end of offset view into ArrayBuffer");
161 wtu
.shouldGenerateGLError(gl
, gl
.INVALID_VALUE
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray, retArray.length - 1, 2)");
163 debug("Test that getBufferSubData successfully works with offset view into ArrayBuffer and srcByteOffset");
164 retArray
= new Float32Array(retStorage
, verticesLengthInBytes
, vertices
.length
- 2);
166 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 2*float32Size, retArray)");
167 shouldBeTrue("areArraysEqual(retArray, floatArray.slice(2))");
169 retArray
= new Float32Array(retStorage
, verticesLengthInBytes
, vertices
.length
);
171 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 2*float32Size, retArray, 2)");
172 shouldBeTrue("areArraysEqual(retArray.slice(0, 2), [0, 0])");
173 shouldBeTrue("areArraysEqual(retArray.slice(2), floatArray.slice(2))");
175 retArray
= new Float32Array(retStorage
, verticesLengthInBytes
, vertices
.length
);
177 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.getBufferSubData(gl.ARRAY_BUFFER, 4*float32Size, retArray, 3, 3)");
178 shouldBeTrue("areArraysEqual(retArray.slice(0, 3), [0, 0, 0])");
179 shouldBeTrue("areArraysEqual(retArray.slice(3, 6), floatArray.slice(4, 7))");
180 shouldBeTrue("areArraysEqual(retArray.slice(6), [0, 0, 0])");
182 debug("Test that getBufferSubData fails when 0 is bound to the target");
183 gl
.bindBuffer(gl
.ELEMENT_ARRAY_BUFFER
, null);
184 wtu
.shouldGenerateGLError(gl
, gl
.INVALID_OPERATION
,
185 "gl.getBufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, retArray)");
187 debug("Test that getBufferSubData fails when offset is less than 0");
188 wtu
.shouldGenerateGLError(gl
, gl
.INVALID_VALUE
, "gl.getBufferSubData(gl.ARRAY_BUFFER, -1, retArray)");
191 debug("Test that getBufferSubData successfully works with uninitialized buffers");
192 retArray
= new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
193 gl
.bindBuffer(gl
.ARRAY_BUFFER
, uninitializedBuffer
);
194 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
,
195 "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray)");
196 shouldBeTrue("areArraysEqual(retArray, [0, 0, 0, 0, 0, 0, 0, 0, 0])");
199 debug("Test that getBufferSubData works when a buffer is immediately resized to be too small");
201 retArray
= new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
202 gl
.bindBuffer(gl
.ARRAY_BUFFER
, buffer
);
203 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
,
204 "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray)");
205 gl
.bufferData(gl
.ARRAY_BUFFER
, 4, gl
.STATIC_DRAW
);
206 shouldBeTrue("areArraysEqual(retArray, floatArray)");
209 debug("Test that getBufferSubData works when a buffer is immediately deleted");
210 retArray
= new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
211 gl
.bindBuffer(gl
.ARRAY_BUFFER
, buffer
);
212 gl
.bufferData(gl
.ARRAY_BUFFER
, floatArray
, gl
.STATIC_DRAW
);
213 wtu
.shouldGenerateGLError(gl
, gl
.NO_ERROR
,
214 "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray)");
215 gl
.deleteBuffer(buffer
);
216 shouldBeTrue("areArraysEqual(retArray, floatArray)");
220 var successfullyParsed
= true;