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 buffer copying contents 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("Test copying a buffer's contents to another buffer governed by the WebGL 2 spec.");
23 var wtu
= WebGLTestUtils
;
25 var gl
= wtu
.create3DContext(undefined, undefined, 2);
26 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "Should be no errors from setup.");
35 debug("Test copying between buffers returns correct data");
37 function testCopyBuffers(srcTarget
, dstTarget
, copyRead
, copyWrite
) {
38 var msg
= "Copying from " + targetToString(gl
, srcTarget
) +
39 " to " + targetToString(gl
, dstTarget
)
40 if (copyRead
&& copyWrite
)
41 msg
+= " via COPY_READ_BUFFER and COPY_WRITE_BUFFER"
43 msg
+= " via COPY_READ_BUFFER"
45 msg
+= " via COPY_WRITE_BUFFER"
51 var srcBuffer
= gl
.createBuffer(), dstBuffer
= gl
.createBuffer();
52 var originalData
= new Float32Array(vertices
);
53 var length
= vertices
.length
* 4;
55 gl
.bindBuffer(srcTarget
, srcBuffer
);
56 gl
.bufferData(srcTarget
, originalData
, gl
.STATIC_DRAW
);
58 gl
.bindBuffer(gl
.COPY_READ_BUFFER
, srcBuffer
);
60 gl
.bindBuffer(dstTarget
, dstBuffer
);
61 gl
.bufferData(dstTarget
, new Float32Array(length
), gl
.STATIC_DRAW
);
63 gl
.bindBuffer(gl
.COPY_WRITE_BUFFER
, dstBuffer
);
65 var expectedGLError
= gl
.NO_ERROR
;
67 if (srcTarget
== dstTarget
) {
68 if (!copyRead
&& copyWrite
) {
69 // srcBuffer isn't bound to any targets because of setting up dstBuffer.
70 gl
.bindBuffer(srcTarget
, srcBuffer
);
72 if (!copyRead
&& !copyWrite
) {
73 // Same buffer object, overlapping range.
74 expectedGLError
= gl
.INVALID_VALUE
;
77 if ((srcTarget
== gl
.ELEMENT_ARRAY_BUFFER
&&
78 dstTarget
!= gl
.ELEMENT_ARRAY_BUFFER
) ||
79 (srcTarget
!= gl
.ELEMENT_ARRAY_BUFFER
&&
80 dstTarget
== gl
.ELEMENT_ARRAY_BUFFER
)) {
81 expectedGLError
= gl
.INVALID_OPERATION
;
84 gl
.copyBufferSubData(copyRead
? gl
.COPY_READ_BUFFER
: srcTarget
,
85 copyWrite
? gl
.COPY_WRITE_BUFFER
: dstTarget
,
87 if (expectedGLError
== gl
.NO_ERROR
) {
88 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "Copying should work");
90 var retBuffer
= new Uint8Array(length
);
91 gl
.getBufferSubData(dstTarget
, 0, retBuffer
);
92 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
,
93 "gl.getBufferSubData(" + targetToString(gl
, dstTarget
) +
94 ", 0, retBuffer) should work");
97 var retArray
= new Float32Array(retBuffer
.buffer
);
98 for (var i
= 0; i
< vertices
.length
; i
++) {
99 if (originalData
[i
] != retArray
[i
]) {
105 testFailed("The returned array buffer fails to match original data");
107 testPassed("The returned array buffer matches original data");
109 wtu
.glErrorShouldBe(gl
, expectedGLError
, "Copying should fail");
112 gl
.deleteBuffer(srcBuffer
);
113 gl
.deleteBuffer(dstBuffer
);
114 shouldBeNull("gl.getParameter(gl.ARRAY_BUFFER_BINDING)");
115 shouldBeNull("gl.getParameter(gl.COPY_READ_BUFFER_BINDING)");
116 shouldBeNull("gl.getParameter(gl.COPY_WRITE_BUFFER_BINDING)");
117 shouldBeNull("gl.getParameter(gl.ELEMENT_ARRAY_BUFFER_BINDING)");
118 shouldBeNull("gl.getParameter(gl.PIXEL_PACK_BUFFER_BINDING)");
119 shouldBeNull("gl.getParameter(gl.PIXEL_UNPACK_BUFFER_BINDING)");
120 shouldBeNull("gl.getParameter(gl.TRANSFORM_FEEDBACK_BUFFER_BINDING)");
121 shouldBeNull("gl.getParameter(gl.UNIFORM_BUFFER_BINDING)");
124 function targetToString(gl
, target
) {
126 case gl
.ARRAY_BUFFER
:
127 return "ARRAY_BUFFER";
128 case gl
.COPY_READ_BUFFER
:
129 return "COPY_READ_BUFFER";
130 case gl
.COPY_WRITE_BUFFER
:
131 return "COPY_WRITE_BUFFER";
132 case gl
.ELEMENT_ARRAY_BUFFER
:
133 return "ELEMENT_ARRAY_BUFFER";
134 case gl
.PIXEL_PACK_BUFFER
:
135 return "PIXEL_PACK_BUFFER";
136 case gl
.PIXEL_UNPACK_BUFFER
:
137 return "PIXEL_UNPACK_BUFFER";
138 case gl
.TRANSFORM_FEEDBACK_BUFFER
:
139 return "TRANSFORM_FEEDBACK_BUFFER";
140 case gl
.UNIFORM_BUFFER
:
141 return "UNIFORM_BUFFER";
143 return "UNKNOWN BUFFER";
149 gl
.ELEMENT_ARRAY_BUFFER
,
150 gl
.PIXEL_PACK_BUFFER
,
151 gl
.PIXEL_UNPACK_BUFFER
,
152 gl
.TRANSFORM_FEEDBACK_BUFFER
,
156 for (var srcIndex
in targets
) {
157 for (var dstIndex
in targets
) {
158 if (targets
[srcIndex
] != gl
.TRANSFORM_FEEDBACK_BUFFER
&&
159 targets
[dstIndex
] != gl
.TRANSFORM_FEEDBACK_BUFFER
) {
160 testCopyBuffers(targets
[srcIndex
], targets
[dstIndex
], true, true);
162 if (targets
[srcIndex
] != gl
.TRANSFORM_FEEDBACK_BUFFER
) {
163 testCopyBuffers(targets
[srcIndex
], targets
[dstIndex
], true, false);
165 if (targets
[dstIndex
] != gl
.TRANSFORM_FEEDBACK_BUFFER
) {
166 testCopyBuffers(targets
[srcIndex
], targets
[dstIndex
], false, true);
168 testCopyBuffers(targets
[srcIndex
], targets
[dstIndex
], false, false);
172 var successfullyParsed
= true;
174 <script src=
"../../js/js-test-post.js"></script>