summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webxr/xrWebGLLayer_framebuffer_draw.https.html
blob: eb797cf93ae9e671e92f93f7758c2c4d835132a1 (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
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/webxr_util.js"></script>
<script src="resources/webxr_test_constants.js"></script>

<script>

let testName =
  "Ensure a WebGL layer's framebuffer can only be drawn to inside a XR frame";

let fakeDeviceInitParams = TRACKED_IMMERSIVE_DEVICE;

// Very simple program setup with no error checking.
function setupProgram(gl, vertexSrc, fragmentSrc) {
  let program = gl.createProgram();

  let vertexShader = gl.createShader(gl.VERTEX_SHADER);
  gl.shaderSource(vertexShader, vertexSrc);
  gl.compileShader(vertexShader);
  gl.attachShader(program, vertexShader);

  let fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
  gl.shaderSource(fragmentShader, fragmentSrc);
  gl.compileShader(fragmentShader);
  gl.attachShader(program, fragmentShader);

  gl.linkProgram(program);
  gl.useProgram(program);

  return program;
}

let testFunction =
  (session, fakeDeviceController, t, sessionObjects) => new Promise((resolve, reject) => {
  let gl = sessionObjects.gl;
  let webglLayer = sessionObjects.glLayer;
  // Setup simple WebGL geometry to draw with.
  let program = setupProgram(gl,
    "attribute vec4 vPosition; void main() { gl_Position = vPosition; }",
    "void main() { gl_FragColor = vec4(1.0,0.0,0.0,1.0); }"
  );

  let vertexObject = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
  gl.bufferData(
    gl.ARRAY_BUFFER,
    new Float32Array([ 0,0.5,0, -0.5,-0.5,0, 0.5,-0.5,0 ]),
    gl.STATIC_DRAW);
  gl.enableVertexAttribArray(0);
  gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);

  let indexObject = gl.createBuffer();
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexObject);
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([0, 1, 2]), gl.STATIC_DRAW);

  let xrFramebuffer = webglLayer.framebuffer;

  function runDrawTests(expectedError) {
    // Make sure we're starting with a clean error slate.
    assert_equals(gl.getError(), gl.NO_ERROR);

    gl.bindFramebuffer(gl.FRAMEBUFFER, xrFramebuffer);
    assert_equals(gl.getError(), gl.NO_ERROR);

    gl.clear(gl.COLOR_BUFFER_BIT);
    assert_equals(gl.getError(), gl[expectedError]);

    gl.clear(gl.DEPTH_BUFFER_BIT);
    assert_equals(gl.getError(), gl[expectedError]);

    gl.drawArrays(gl.TRIANGLES, 0, 3);
    assert_equals(gl.getError(), gl[expectedError]);

    gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_BYTE, 0);
    assert_equals(gl.getError(), gl[expectedError]);
  }

  // Drawing operations outside of a XR frame should fail.
  runDrawTests("INVALID_FRAMEBUFFER_OPERATION");

  // Drawing operations within a XR frame should succeed.
  session.requestAnimationFrame((time, xrFrame) => {
    runDrawTests("NO_ERROR");
    resolve();
  });
});

xr_session_promise_test(
  testName, testFunction, fakeDeviceInitParams, 'immersive-vr');

</script>