summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/transform_feedback/simultaneous_binding.html
blob: 11d1eaa82999f2a9a3e7e875c89f89dde35478a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simultaneous binding</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
</head>
<body>
<div id="description"></div>
<canvas id="canvas" style="width: 50px; height: 50px;"> </canvas>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">#version 300 es
in float in_value;
in float in_value2;
out float out_value;

void main() {
   out_value = in_value * 2.;
}
</script>
<script id="fshader" type="x-shader/x-fragment">#version 300 es
precision mediump float;
out vec4 dummy;
uniform UniformBlock {
  float fragment_value;
};
void main() {
  dummy = vec4(fragment_value);
}
</script>
<script>
"use strict";
description("This test verifies that access to a buffer simultaneously bound to a transform feedback object and a non-transform-feedback binding point is forbidden.");

debug("");

var wtu = WebGLTestUtils;
var canvas = document.getElementById("canvas");
var gl = wtu.create3DContext(canvas, null, 2);

if (!gl) {
    testFailed("WebGL context does not exist");
} else {
    testPassed("WebGL context exists");
}

function drawWithFeedbackBound(gl, drawFunction, prog, vao, tf, enableFeedback) {
  gl.useProgram(prog);
  gl.bindVertexArray(vao);
  gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, tf);
  if (enableFeedback) gl.beginTransformFeedback(gl.POINTS);
  let error = gl.getError();
  if (error != gl.NO_ERROR) testFailed("Unexpected error before drawing: " +  error)
  drawFunction();
  if (enableFeedback) gl.endTransformFeedback();
  gl.bindVertexArray(null);
  gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, null);
}

function createBuffer(gl, dataOrSize) {
  const buf = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, buf);
  gl.bufferData(gl.ARRAY_BUFFER, dataOrSize, gl.STATIC_DRAW);
  gl.bindBuffer(gl.ARRAY_BUFFER, null);
  return buf;
}

function createVAO(gl, vertexBuffer, vertexBuffer2, indexBuffer) {
  const vao = gl.createVertexArray();
  gl.bindVertexArray(vao);
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  gl.enableVertexAttribArray(0);
  gl.vertexAttribPointer(0, 1, gl.FLOAT, false, 0, 0);
  gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer2);
  gl.enableVertexAttribArray(1);
  gl.vertexAttribPointer(1, 1, gl.FLOAT, false, 0, 0);
  gl.vertexAttribDivisor(1, 2);
  gl.bindBuffer(gl.ARRAY_BUFFER, null);
  gl.bindVertexArray(null);
  return vao;
}

const prog = wtu.setupTransformFeedbackProgram(gl, ["vshader", "fshader"],
    ["out_value"], gl.SEPARATE_ATTRIBS,
    ["in_value", "in_value2"]);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "linking transform feedback shader should not set an error");

const vertexBuffer = createBuffer(gl, new Float32Array([1, 2, 3, 4]));
const vertexBuffer2 = createBuffer(gl, new Float32Array([1, 2, 3, 4]));
const vertexBuffer3 = createBuffer(gl, new Float32Array([1, 2, 3, 4]));

const indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Int16Array([0, 1, 2, 3]), gl.STATIC_DRAW);

const tfBuffer = createBuffer(gl, new Float32Array([0, 0, 0, 0]));

const vao = createVAO(gl, vertexBuffer, vertexBuffer2, indexBuffer);
// This tests that having a transform feedback buffer bound in an unbound VAO
// does not affect anything.
const unboundVao = createVAO(gl, tfBuffer, tfBuffer, indexBuffer);

const tf = gl.createTransformFeedback();
gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, tf);
gl.useProgram(prog);
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, tfBuffer);
// this binds the default (id = 0) TRANSFORM_FEEBACK buffer
gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, null);

const uniformBuffer = createBuffer(gl, new Float32Array([1, 0, 0, 0]));
const ubi = gl.getUniformBlockIndex(prog, "UniformBlock");
gl.uniformBlockBinding(prog, ubi, 0);
gl.bindBufferBase(gl.UNIFORM_BUFFER, 0, uniformBuffer);

const drawFunctions = [
    [
      ()=>gl.drawArrays(gl.POINTS, 0, 4),
      ()=>gl.drawElements(gl.POINTS, 4, gl.UNSIGNED_SHORT, 0),
    ],
    [
      ()=>gl.drawArraysInstanced(gl.POINTS, 0, 4, 1),
      ()=>gl.drawElementsInstanced(gl.POINTS, 4, gl.UNSIGNED_SHORT, 0, 1),
    ],
    [
      ()=>gl.drawArrays(gl.POINTS, 0, 4),
      ()=>gl.drawRangeElements(gl.POINTS, 0, 3, 4, gl.UNSIGNED_SHORT, 0),
    ],
  ];

for (let [drawArrays, drawElements] of drawFunctions) {
  debug("<h3>With draw functions " + drawArrays + " and " + drawElements + "</h3>");
  debug("<hr/>Test baseline");
  gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, tfBuffer);
  gl.bufferData(gl.TRANSFORM_FEEDBACK_BUFFER, 16, gl.STATIC_DRAW);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "bufferData to TRANSFORM_FEEDBACK_BUFFER");
  drawWithFeedbackBound(gl, drawElements, prog, vao, tf, false);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "drawElements should be successful");
  drawWithFeedbackBound(gl, drawArrays, prog, vao, tf, true);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "transform feedback should be successful");

  const expected = [2, 4, 6, 8];
  gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, tfBuffer);
  wtu.checkFloatBuffer(gl, gl.TRANSFORM_FEEDBACK_BUFFER, expected);

  debug("<hr/>Test generic bind point set to null");
  gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, null);
  drawWithFeedbackBound(gl, drawElements, prog, vao, tf, false);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "drawElements should be successful");
  drawWithFeedbackBound(gl, drawArrays, prog, vao, tf, true);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "transform feedback should be successful");

  gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, tfBuffer);
  wtu.checkFloatBuffer(gl, gl.TRANSFORM_FEEDBACK_BUFFER, expected);

  debug("<hr/>Test generic bind point set to vertex buffer");
  // The TRANSFORM_FEEDBACK_BUFFER generic binding point is not part of the
  // transform feedback object and not written to by transform feedback. Only
  // the indexed binding points are written to. So it should be legal to draw
  // from a buffer bound to the generic binding point.
  gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, vertexBuffer);
  drawWithFeedbackBound(gl, drawElements, prog, vao, tf, false);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "drawElements should be successful");
  drawWithFeedbackBound(gl, drawArrays, prog, vao, tf, true);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "transform feedback should be successful");

  gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, tfBuffer);
  wtu.checkFloatBuffer(gl, gl.TRANSFORM_FEEDBACK_BUFFER, expected);

  debug("<hr/>Test ARRAY_BUFFER");
  // this should fail because the transform feedback's buffer #0 and the
  // badVao's buffer #0 are the same buffer
  const badVao = createVAO(gl, tfBuffer, vertexBuffer2, indexBuffer);
  drawWithFeedbackBound(gl, drawArrays, prog, badVao, tf, false);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "drawArrays: buffer used as vertex attrib and tf simultaneously");
  drawWithFeedbackBound(gl, drawElements, prog, badVao, tf, false);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "drawElements: buffer used as vertex attrib and tf simultaneously");
  drawWithFeedbackBound(gl, drawArrays, prog, badVao, tf, true);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "buffer used as vertex attrib and tf simultaneously");
  gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, tfBuffer);
  wtu.checkFloatBuffer(gl, gl.TRANSFORM_FEEDBACK_BUFFER, expected, "should be the same as before as nothing has executed");

  debug("<hr/>Test UNIFORM_BUFFER");
  gl.bindBufferBase(gl.UNIFORM_BUFFER, 0, tfBuffer);
  gl.bindBuffer(gl.UNIFORM_BUFFER, null); // tfBuffer is still bound at index 0
  drawWithFeedbackBound(gl, drawArrays, prog, vao, tf, false);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "drawArrays: buffer used as uniform buffer and tf simultaneously");
  drawWithFeedbackBound(gl, drawElements, prog, vao, tf, false);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "drawElements: buffer used as uniform buffer and tf simultaneously");
  drawWithFeedbackBound(gl, drawArrays, prog, vao, tf, true);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "buffer used as uniform buffer and tf simultaneously");
  gl.bindBufferBase(gl.UNIFORM_BUFFER, 0, uniformBuffer);
  drawWithFeedbackBound(gl, drawArrays, prog, vao, tf, false);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "drawArrays: tf buffer not used as uniform buffer anymore");
  drawWithFeedbackBound(gl, drawElements, prog, vao, tf, false);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "drawElements: tf buffer not used as uniform buffer anymore");
  drawWithFeedbackBound(gl, drawArrays, prog, vao, tf, true);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "tf buffer not used as uniform buffer anymore");

  gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, tf);
  const tfBuffer2 = createBuffer(gl, Float32Array.BYTES_PER_ELEMENT * 4);
  gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, tfBuffer2);
  drawWithFeedbackBound(gl, drawArrays, prog, badVao, tf);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "buffer is no longer bound for transform feedback");
  gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, tf);
  gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, tfBuffer);

  debug("<hr/>Test TF buffer bound to target unused by draw");
  // Even if the TF buffer is bound to a target that's not used by the draw, it's
  // still an error.
  gl.bindBuffer(gl.COPY_READ_BUFFER, tfBuffer);
  drawWithFeedbackBound(gl, drawArrays, prog, vao, tf, true);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "tf enabled");
  drawWithFeedbackBound(gl, drawArrays, prog, vao, tf, false);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "drawArrays: tf disabled");
  drawWithFeedbackBound(gl, drawElements, prog, vao, tf, false);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "drawElements: tf disabled");
  gl.bindBuffer(gl.COPY_READ_BUFFER, null);

  debug("<hr/>Test TF buffer bound to disabled vertex attrib");
  // Having a TF buffer bound to a disabled vertex attrib should not be an error
  // when TF is not enabled, because the buffer is not used.
  gl.bindVertexArray(vao);
  gl.bindBuffer(gl.ARRAY_BUFFER, tfBuffer);
  gl.vertexAttribPointer(2, 1, gl.FLOAT, false, 0, 0);
  gl.disableVertexAttribArray(2);
  gl.bindVertexArray(null);
  drawWithFeedbackBound(gl, drawArrays, prog, vao, tf, false);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "tf disabled, draw should succeed");
  drawWithFeedbackBound(gl, drawElements, prog, vao, tf, false);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "tf disabled, draw should succeed");
  // Remove the TF buffer binding from the VAO after the test.
  gl.bindVertexArray(vao);
  gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer3);
  gl.vertexAttribPointer(2, 1, gl.FLOAT, false, 0, 0);
  gl.disableVertexAttribArray(2);
  gl.bindVertexArray(null);
}

debug("<h1>Non-drawing tests</h1>");

debug("<hr/>Test bufferData");

gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, tf);
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, tfBuffer);
gl.bufferData(gl.TRANSFORM_FEEDBACK_BUFFER, 16, gl.STATIC_DRAW);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "bufferData to TRANSFORM_FEEDBACK_BUFFER");
gl.bindBuffer(gl.COPY_WRITE_BUFFER, tfBuffer);
gl.bufferData(gl.TRANSFORM_FEEDBACK_BUFFER, 16, gl.STATIC_DRAW);
wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "bufferData with double bound buffer");
gl.bindBuffer(gl.COPY_WRITE_BUFFER, null);

// The value of the TRANSFORM_FEEDBACK_BUFFER generic bind point should not
// affect the legality of any operation.
let genericBindPointValues = [()=>null, ()=>tfBuffer, ()=>vertexBuffer];

for (let genericBindPointValue of genericBindPointValues) {
  gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, tf);
  debug("<h3>With TRANSFORM_FEEDBACK_BUFFER generic bind point value " + genericBindPointValue + "</h3>");
  gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, genericBindPointValue());

  debug("<hr/>Test PIXEL_UNPACK_BUFFER");
  const tex = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_2D, tex);
  gl.bindBuffer(gl.PIXEL_UNPACK_BUFFER, vertexBuffer);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, 0);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "PIXEL_UNPACK_BUFFER is not bound for transform feedback");

  gl.bindBuffer(gl.PIXEL_UNPACK_BUFFER, tfBuffer);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, 0);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "PIXEL_UNPACK_BUFFER is bound for transform feedback");
  gl.bindBuffer(gl.PIXEL_UNPACK_BUFFER, null);

  debug("<hr/>Test PIXEL_PACK_BUFFER");
  gl.bindBuffer(gl.PIXEL_PACK_BUFFER, vertexBuffer);
  gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, 0);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "PIXEL_PACK_BUFFER is not bound for transform feedback");
  gl.bindBuffer(gl.PIXEL_PACK_BUFFER, tfBuffer);
  gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, 0);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "PIXEL_PACK_BUFFER is bound for transform feedback");
  gl.bindBuffer(gl.PIXEL_PACK_BUFFER, null)

  debug("<hr/>Test bufferData family with tf object bound");

  gl.bindBuffer(gl.COPY_WRITE_BUFFER, tfBuffer);
  gl.bufferData(gl.COPY_WRITE_BUFFER, 16, gl.STATIC_DRAW);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "bufferData with double bound buffer");
  gl.bufferSubData(gl.COPY_WRITE_BUFFER, 0, new Uint8Array([0]));
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "bufferSubData with double bound buffer");
  gl.getBufferSubData(gl.COPY_WRITE_BUFFER, 0, new Uint8Array([0]), 0, 1);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "getBufferSubData with double bound buffer");

  gl.bindBuffer(gl.COPY_READ_BUFFER, vertexBuffer);
  gl.copyBufferSubData(gl.COPY_WRITE_BUFFER, gl.COPY_READ_BUFFER, 0, 0, 1);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "copyBufferSubData with double bound buffer");
  gl.copyBufferSubData(gl.COPY_READ_BUFFER, gl.COPY_WRITE_BUFFER, 0, 0, 1);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "copyBufferSubData with double bound buffer");

  debug("<hr/>Test that rejected operations do not change the bound buffer size");

  gl.bindBuffer(gl.ARRAY_BUFFER, tfBuffer);
  gl.bufferData(gl.ARRAY_BUFFER, 8, gl.STATIC_DRAW);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "bufferData with double bound buffer");

  gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, null);
  gl.bufferSubData(gl.ARRAY_BUFFER, 0, new Uint8Array(16));
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "bufferSubData should succeed");
  gl.bindBuffer(gl.ARRAY_BUFFER, null);

  debug("<hr/>Test bufferData family with tf object unbound");

  gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, null);
  gl.bindBuffer(gl.COPY_WRITE_BUFFER, tfBuffer);
  gl.bufferData(gl.COPY_WRITE_BUFFER, 16, gl.STATIC_DRAW);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "bufferData should succeed");
  gl.bufferSubData(gl.COPY_WRITE_BUFFER, 0, new Uint8Array([0]));
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "bufferSubData should succeed");
  gl.getBufferSubData(gl.COPY_WRITE_BUFFER, 0, new Uint8Array([0]), 0, 1);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "getBufferSubData should succeed");

  gl.bindBuffer(gl.COPY_READ_BUFFER, vertexBuffer);
  gl.copyBufferSubData(gl.COPY_WRITE_BUFFER, gl.COPY_READ_BUFFER, 0, 0, 1);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "copyBufferSubData should succeed");
  gl.copyBufferSubData(gl.COPY_READ_BUFFER, gl.COPY_WRITE_BUFFER, 0, 0, 1);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "copyBufferSubData should succeed");
}

finishTest();

</script>

</body>
</html>