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 <link rel=
"stylesheet" href=
"../../resources/js-test-style.css"/>
12 <script src=
"../../js/js-test-pre.js"></script>
13 <script src=
"../../js/webgl-test-utils.js"></script>
16 <div id=
"description"></div>
17 <div id=
"console"></div>
21 description("Test bufferData/bufferSubData with ArrayBufferView sub source input");
23 var wtu
= WebGLTestUtils
;
24 var gl
= wtu
.create3DContext(undefined, undefined, 2);
26 function verifyBufferData(testCase
, sourceByteOffset
, size
, data
) {
27 var readbackBuffer
= new ArrayBuffer(testCase
.size
* size
);
28 var readbackView
= new window
[testCase
.type
](readbackBuffer
);
29 gl
.getBufferSubData(gl
.ARRAY_BUFFER
, sourceByteOffset
, readbackView
);
31 var offset
= sourceByteOffset
/ testCase
.size
;
32 for (var ii
= 0; ii
< size
; ++ii
) {
33 if (readbackView
[ii
] != data
[ii
]) {
34 testFailed("expected data at " + ii
+ ": " + data
[ii
] + ", got " + readbackView
[ii
]);
39 testPassed("buffer data uploaded correctly");
43 function bufferDataTest(testCases
) {
44 for (var idx
= 0; idx
< testCases
.length
; ++idx
) {
45 var test
= testCases
[idx
];
47 debug("Test bufferData with " + test
.type
);
49 var buf
= gl
.createBuffer();
52 gl
.bindBuffer(gl
.ARRAY_BUFFER
, buf
);
53 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
);
56 var arrayBuffer
= new ArrayBuffer(test
.size
* count
);
57 var view
= new window
[test
.type
](arrayBuffer
);
58 for (var ii
= 0; ii
< count
; ++ii
) {
62 gl
.bufferData(gl
.ARRAY_BUFFER
, view
, gl
.STATIC_DRAW
, 0, count
+ 1);
63 wtu
.glErrorShouldBe(gl
, gl
.INVALID_VALUE
,
64 "calling bufferData when srcOffset + length is larger than source size");
66 gl
.bufferData(gl
.ARRAY_BUFFER
, view
, gl
.STATIC_DRAW
, count
+ 1);
67 wtu
.glErrorShouldBe(gl
, gl
.INVALID_VALUE
,
68 "calling bufferData when srcOffset + length is larger than source size");
70 gl
.bufferData(gl
.ARRAY_BUFFER
, view
, gl
.STATIC_DRAW
, count
- 1, 2);
71 wtu
.glErrorShouldBe(gl
, gl
.INVALID_VALUE
,
72 "calling bufferData when srcOffset + length is larger than source size");
75 var size
= count
- offset
;
76 gl
.bufferData(gl
.ARRAY_BUFFER
, view
, gl
.STATIC_DRAW
, offset
);
77 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
,
78 "calling bufferData with valid sub source should succeed");
79 verifyBufferData(test
, 0, size
, view
.slice(offset
, offset
+ size
));
83 gl
.bufferData(gl
.ARRAY_BUFFER
, view
, gl
.STATIC_DRAW
, offset
, size
);
84 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
,
85 "calling bufferData with valid sub source should succeed");
86 verifyBufferData(test
, 0, size
, view
.slice(offset
, offset
+ size
));
88 gl
.bufferData(gl
.ARRAY_BUFFER
, view
, gl
.STATIC_DRAW
, count
);
89 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
,
90 "calling bufferData with valid sub source of size 0 should succeed");
93 gl
.bindBuffer(gl
.ARRAY_BUFFER
, null);
95 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "no GL error");
99 function bufferSubDataTest(testCases
) {
100 for (var idx
= 0; idx
< testCases
.length
; ++idx
) {
101 var test
= testCases
[idx
];
103 debug("Test bufferSubData with " + test
.type
);
106 var totalBufferBytes
= test
.size
* count
* 2;
107 var buf
= gl
.createBuffer();
108 shouldBeNonNull(buf
);
109 gl
.bindBuffer(gl
.ARRAY_BUFFER
, buf
);
110 gl
.bufferData(gl
.ARRAY_BUFFER
, totalBufferBytes
, gl
.STATIC_DRAW
);
111 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
);
113 var arrayBuffer
= new ArrayBuffer(test
.size
* count
);
114 var view
= new window
[test
.type
](arrayBuffer
);
115 for (var ii
= 0; ii
< count
; ++ii
) {
119 var sourceByteOffset
= test
.size
* 2;
121 gl
.bufferSubData(gl
.ARRAY_BUFFER
, sourceByteOffset
, view
, 0, count
+ 1);
122 wtu
.glErrorShouldBe(gl
, gl
.INVALID_VALUE
,
123 "calling bufferSubData when srcOffset + length is larger than source size");
125 gl
.bufferSubData(gl
.ARRAY_BUFFER
, sourceByteOffset
, view
, count
+ 1);
126 wtu
.glErrorShouldBe(gl
, gl
.INVALID_VALUE
,
127 "calling bufferSubData when srcOffset + length is larger than source size");
129 gl
.bufferSubData(gl
.ARRAY_BUFFER
, sourceByteOffset
, view
, count
- 1, 2);
130 wtu
.glErrorShouldBe(gl
, gl
.INVALID_VALUE
,
131 "calling bufferSubData when srcOffset + length is larger than source size");
134 var size
= count
- offset
;
135 gl
.bufferSubData(gl
.ARRAY_BUFFER
, sourceByteOffset
, view
, offset
);
136 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
,
137 "calling bufferSubData with valid sub source should succeed");
138 verifyBufferData(test
, sourceByteOffset
, size
, view
.slice(offset
, offset
+ size
));
142 gl
.bufferSubData(gl
.ARRAY_BUFFER
, sourceByteOffset
, view
, offset
, size
);
143 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
,
144 "calling bufferSubData with valid sub source should succeed");
145 verifyBufferData(test
, sourceByteOffset
, size
, view
.slice(offset
, offset
+ size
));
147 gl
.bufferSubData(gl
.ARRAY_BUFFER
, sourceByteOffset
, view
, count
);
148 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
,
149 "calling bufferSubData with valid sub source of size 0 should succeed");
151 gl
.deleteBuffer(buf
);
152 gl
.bindBuffer(gl
.ARRAY_BUFFER
, null);
154 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "no GL error");
159 testFailed("WebGL context does not exist");
161 testPassed("WebGL context exists");
164 {type
: "Uint8Array", size
: 1},
165 {type
: "Int8Array", size
: 1},
166 {type
: "Int16Array", size
: 2},
167 {type
: "Uint16Array", size
: 2},
168 {type
: "Uint32Array", size
: 4},
169 {type
: "Int32Array", size
: 4},
170 {type
: "Float32Array", size
: 4},
171 {type
: "Float64Array", size
: 8}
174 bufferDataTest(testCases
);
175 bufferSubDataTest(testCases
);
178 var successfullyParsed
= true;
181 <script src=
"../../js/js-test-post.js"></script>