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 restrictions 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 buffer copying restrictions governed by the WebGL 2 spec. The test makes sure that copyBufferSubData acts as expected.");
23 var wtu
= WebGLTestUtils
;
25 var gl
= wtu
.create3DContext(undefined, undefined, 2);
27 var validTargets
= [gl
.ARRAY_BUFFER
, gl
.COPY_READ_BUFFER
, gl
.COPY_WRITE_BUFFER
,
28 gl
.ELEMENT_ARRAY_BUFFER
, gl
.PIXEL_PACK_BUFFER
,
29 gl
.PIXEL_UNPACK_BUFFER
, gl
.TRANSFORM_FEEDBACK_BUFFER
,
32 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "Should be no errors from setup.");
34 var testCopyBuffer = function(srcTarget
, dstTarget
) {
35 var srcTargetStr
= wtu
.glEnumToString(gl
, srcTarget
),
36 dstTargetStr
= wtu
.glEnumToString(gl
, dstTarget
);
37 var srcBuffer
= gl
.createBuffer(),
38 dstBuffer
= gl
.createBuffer();
39 var testCopyStr
= "copying from a gl." + wtu
.glEnumToString(gl
, srcTarget
) + " buffer to a gl."
40 + wtu
.glEnumToString(gl
, dstTarget
) + " buffer"
42 gl
.bindBuffer(srcTarget
, srcBuffer
);
43 gl
.bufferData(srcTarget
, new Float32Array(32), gl
.STATIC_DRAW
);
44 gl
.bindBuffer(dstTarget
, dstBuffer
);
45 gl
.bufferData(dstTarget
, new Float32Array(32), gl
.STATIC_DRAW
);
46 gl
.copyBufferSubData(srcTarget
, dstTarget
, 8, 0, 4);
47 if (srcTarget
== dstTarget
)
48 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, testCopyStr
+ " should work.");
49 else if (srcTarget
== gl
.ELEMENT_ARRAY_BUFFER
|| dstTarget
== gl
.ELEMENT_ARRAY_BUFFER
)
50 wtu
.glErrorShouldBe(gl
, gl
.INVALID_OPERATION
, testCopyStr
+ " should fail.");
52 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, testCopyStr
+ " should work.");
54 // Special case: COPY_READ_BUFFER and COPY_WRITE_BUFFER are compatible with ELEMENT_ARRAY_BUFFER
55 // only if the buffer had been initially bound to an ELEMENT_ARRAY_BUFFER
56 if (srcTarget
== gl
.ELEMENT_ARRAY_BUFFER
&&
57 (dstTarget
== gl
.COPY_READ_BUFFER
|| dstTarget
== gl
.COPY_WRITE_BUFFER
)) {
58 dstBuffer
= gl
.createBuffer();
59 gl
.bindBuffer(gl
.ELEMENT_ARRAY_BUFFER
, dstBuffer
);
60 } else if (dstTarget
== gl
.ELEMENT_ARRAY_BUFFER
&&
61 (srcTarget
== gl
.COPY_READ_BUFFER
|| srcTarget
== gl
.COPY_WRITE_BUFFER
)) {
62 srcBuffer
= gl
.createBuffer();
63 gl
.bindBuffer(gl
.ELEMENT_ARRAY_BUFFER
, srcBuffer
);
68 gl
.bindBuffer(srcTarget
, srcBuffer
);
69 gl
.bufferData(srcTarget
, new Float32Array(32), gl
.STATIC_DRAW
);
70 gl
.bindBuffer(dstTarget
, dstBuffer
);
71 gl
.bufferData(dstTarget
, new Float32Array(32), gl
.STATIC_DRAW
);
72 gl
.copyBufferSubData(srcTarget
, dstTarget
, 8, 0, 4);
73 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, testCopyStr
+ " should work if all buffers were initially bound to ELEMENT_ARRAY_BUFFER.");
76 for (var i
= 0; i
< validTargets
.length
; i
++) {
78 debug("Copying data from a " + wtu
.glEnumToString(gl
, validTargets
[i
])
79 + " buffer to another target");
80 for (var j
= 0; j
< validTargets
.length
; j
++)
81 testCopyBuffer(validTargets
[i
], validTargets
[j
]);
85 debug("Test copying a buffer of other data (gl.ARRAY_BUFFER) bound to gl.COPY_READ_BUFFER to a "
86 + "buffer bound to gl.ELEMENT_ARRAY_BUFFER");
87 var srcBuffer
= gl
.createBuffer(), dstBuffer
= gl
.createBuffer();
88 gl
.bindBuffer(gl
.ARRAY_BUFFER
, srcBuffer
);
89 gl
.bindBuffer(gl
.ELEMENT_ARRAY_BUFFER
, dstBuffer
);
90 gl
.bufferData(gl
.ELEMENT_ARRAY_BUFFER
, new Float32Array(32), gl
.STATIC_DRAW
);
91 gl
.bufferData(gl
.ARRAY_BUFFER
, new Float32Array(32), gl
.STATIC_DRAW
);
92 gl
.bindBuffer(gl
.COPY_READ_BUFFER
, srcBuffer
);
93 gl
.copyBufferSubData(gl
.COPY_READ_BUFFER
, gl
.ELEMENT_ARRAY_BUFFER
, 0, 0, 4);
94 wtu
.glErrorShouldBe(gl
, gl
.INVALID_OPERATION
, "Copying gl.ARRAY_BUFFER bound to "
95 + "gl.COPY_READ_BUFFER to a buffer bound to gl.ELEMENT_ARRAY_BUFFER should fail.");
99 var successfullyParsed
= true;