Backed out changeset 7272b7396c78 (bug 1932758) for causing fenix debug failures...
[gecko.git] / dom / canvas / test / webgl-conf / checkout / conformance2 / buffers / buffer-copying-contents.html
blob47d11093b288f381318a5c8d89b74ff05547c15f
1 <!--
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.
5 -->
7 <!DOCTYPE html>
8 <html>
9 <head>
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>
15 </head>
16 <body>
17 <div id="description"></div>
18 <div id="console"></div>
19 <script>
20 "use strict";
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.");
28 var vertices = [
29 1.1, 1.0, 1.3,
30 -1.0, -1.0, -5.0,
31 5.3, -1.0, 1.0
34 debug("");
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"
42 else if (copyRead)
43 msg += " via COPY_READ_BUFFER"
44 else if (copyWrite)
45 msg += " via COPY_WRITE_BUFFER"
46 else
47 msg += " directly"
48 debug("")
49 debug(msg)
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);
57 if (copyRead)
58 gl.bindBuffer(gl.COPY_READ_BUFFER, srcBuffer);
60 gl.bindBuffer(dstTarget, dstBuffer);
61 gl.bufferData(dstTarget, new Float32Array(length), gl.STATIC_DRAW);
62 if (copyWrite)
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,
86 0, 0, length);
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");
96 var failed = false;
97 var retArray = new Float32Array(retBuffer.buffer);
98 for (var i = 0; i < vertices.length; i++) {
99 if (originalData[i] != retArray[i]) {
100 failed = true;
101 break;
104 if (failed)
105 testFailed("The returned array buffer fails to match original data");
106 else
107 testPassed("The returned array buffer matches original data");
108 } else {
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) {
125 switch (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";
142 default:
143 return "UNKNOWN BUFFER";
147 var targets = [
148 gl.ARRAY_BUFFER,
149 gl.ELEMENT_ARRAY_BUFFER,
150 gl.PIXEL_PACK_BUFFER,
151 gl.PIXEL_UNPACK_BUFFER,
152 gl.TRANSFORM_FEEDBACK_BUFFER,
153 gl.UNIFORM_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;
173 </script>
174 <script src="../../js/js-test-post.js"></script>
175 </body>
176 </html>