Skip to content

Commit 3ff07f5

Browse files
committed
Test for having the same read and write buffer using a transform feedback
note: there are 2 lines marked as "FIXME" that I believe should be removed assuming the spec changes so that `gl.bindBuffer` is less strict
1 parent 8c27fd4 commit 3ff07f5

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
transform_feedback.html
2+
transform_feedback_same_buffers_as_input_and_output.html
23
two-unreferenced-varyings.html
34
unwritten-output-defaults-to-zero.html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<!--
2+
3+
/*
4+
** Copyright (c) 2017 The Khronos Group Inc.
5+
**
6+
** Permission is hereby granted, free of charge, to any person obtaining a
7+
** copy of this software and/or associated documentation files (the
8+
** "Materials"), to deal in the Materials without restriction, including
9+
** without limitation the rights to use, copy, modify, merge, publish,
10+
** distribute, sublicense, and/or sell copies of the Materials, and to
11+
** permit persons to whom the Materials are furnished to do so, subject to
12+
** the following conditions:
13+
**
14+
** The above copyright notice and this permission notice shall be included
15+
** in all copies or substantial portions of the Materials.
16+
**
17+
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21+
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22+
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23+
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
24+
*/
25+
26+
-->
27+
28+
<!DOCTYPE html>
29+
<html>
30+
<head>
31+
<meta charset="utf-8">
32+
<title>WebGL Transform Feedback Conformance Test - Same Buffers as Input and Output</title>
33+
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
34+
<script src="../../js/js-test-pre.js"></script>
35+
<script src="../../js/webgl-test-utils.js"></script>
36+
</head>
37+
<body>
38+
<div id="description"></div>
39+
<canvas id="canvas" style="width: 50px; height: 50px;"> </canvas>
40+
<div id="console"></div>
41+
<script id="vshader" type="x-shader/x-vertex">#version 300 es
42+
in float in_value;
43+
out float out_value;
44+
45+
void main() {
46+
out_value = in_value * 2.;
47+
}
48+
</script>
49+
<script id="fshader" type="x-shader/x-fragment">#version 300 es
50+
precision mediump float;
51+
out vec4 dummy;
52+
void main() {
53+
dummy = vec4(1);
54+
}
55+
</script>
56+
<script>
57+
"use strict";
58+
description("This test verifies trying to read and write from the same buffers fails");
59+
60+
debug("");
61+
62+
var wtu = WebGLTestUtils;
63+
var canvas = document.getElementById("canvas");
64+
var gl = wtu.create3DContext(canvas, null, 2);
65+
66+
if (!gl) {
67+
testFailed("WebGL context does not exist");
68+
} else {
69+
testPassed("WebGL context exists");
70+
71+
runSameInputOutputTest();
72+
finishTest();
73+
}
74+
75+
function runSameInputOutputTest() {
76+
const prog = wtu.setupTransformFeedbackProgram(gl, ["vshader", "fshader"],
77+
["out_value"], gl.SEPARATE_ATTRIBS,
78+
["in_value"]);
79+
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "linking transform feedback shader should not set an error");
80+
81+
const inLoc = 0;
82+
const outLoc = 0;
83+
84+
const srcBuffer1 = createBuffer(gl, new Float32Array([1, 2, 3]));
85+
const srcVAO1 = createVAO(gl, srcBuffer1, inLoc);
86+
87+
const dstBuffer = createBuffer(gl, Float32Array.BYTES_PER_ELEMENT * 3);
88+
const srcVAO2 = createVAO(gl, dstBuffer, inLoc);
89+
90+
const tf = gl.createTransformFeedback();
91+
gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, tf);
92+
gl.useProgram(prog);
93+
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, outLoc, dstBuffer);
94+
// this binds the default (id = 0) TRANSFORM_FEEBACK buffer
95+
gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, null);
96+
97+
// FIXME: this should not be needed once the spec and implementations are fixed
98+
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, outLoc, null); // this is not needed
99+
100+
runFeedback(gl, prog, srcVAO1, tf);
101+
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
102+
103+
const expected = [2, 4, 6];
104+
gl.bindBuffer(gl.ARRAY_BUFFER, dstBuffer);
105+
wtu.checkFloatBuffer(gl, gl.ARRAY_BUFFER, expected);
106+
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
107+
108+
// this should fail because the transform feedback's buffer #0 and the
109+
// srcVAO2's buffer #0 are the same buffer
110+
runFeedback(gl, prog, srcVAO2, tf);
111+
wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "reading and writing to same buffer");
112+
113+
gl.bindBuffer(gl.ARRAY_BUFFER, dstBuffer);
114+
wtu.checkFloatBuffer(gl, gl.ARRAY_BUFFER, expected, "should be the same as before as nothing has executed");
115+
116+
}
117+
118+
function runFeedback(gl, prog, srcVAO, tf) {
119+
gl.enable(gl.RASTERIZER_DISCARD);
120+
121+
gl.useProgram(prog);
122+
gl.bindVertexArray(srcVAO);
123+
124+
gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, tf);
125+
gl.beginTransformFeedback(gl.TRIANGLES);
126+
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
127+
gl.drawArrays(gl.TRIANGLES, 0, 3);
128+
gl.endTransformFeedback();
129+
130+
// FIXME: this should not be needed once the spec and implementations are fixed
131+
gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, null); // this is not needed
132+
133+
gl.disable(gl.RASTERIZER_DISCARD);
134+
}
135+
136+
function createBuffer(gl, dataOrSize) {
137+
const buf = gl.createBuffer();
138+
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
139+
gl.bufferData(gl.ARRAY_BUFFER, dataOrSize, gl.STATIC_DRAW);
140+
// FIXME: this should not be needed once the spec and implementations are fixed
141+
gl.bindBuffer(gl.ARRAY_BUFFER, null); // this is not needed
142+
return buf;
143+
}
144+
145+
function createVAO(gl, buf, inLoc) {
146+
const vao = gl.createVertexArray();
147+
gl.bindVertexArray(vao);
148+
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
149+
gl.enableVertexAttribArray(inLoc);
150+
gl.vertexAttribPointer(inLoc, 1, gl.FLOAT, false, 0, 0);
151+
// FIXME: this should not be needed once the spec and implementations are fixed
152+
gl.bindBuffer(gl.ARRAY_BUFFER, null); // this is not needed
153+
gl.bindVertexArray(null);
154+
return vao;
155+
}
156+
</script>
157+
158+
</body>
159+
</html>

0 commit comments

Comments
 (0)