summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/rendering/draw-buffers-sparse-output-locations.html
blob: 4c679df6a29409fe5066804ab14c25a7dc2873ac (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
<!--
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>WebGL Conformance Tests: Verify drawBuffers sparse output locations</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" width="1" height="1" style="width: 4px; height: 4px;"> </canvas>
<div id="console"></div>

<script id="vs" type="x-shader/x-vertex">#version 300 es
void main() {
  gl_PointSize = 100.0;
  gl_Position = vec4(0, 0, 0, 1);
}
</script>

<script id="fs" type="x-shader/x-fragment">#version 300 es
// fragment shader only outputs to attachments 1 and 3
precision highp float;
layout(location = 1) out vec4 output1;
layout(location = 3) out vec4 output2;
void main()
{
    output1 = vec4(0.0, 1.0, 0.0, 1.0);
    output2 = vec4(0.0, 0.0, 1.0, 1.0);
}

</script>
<script>
"use strict";
description("This test verifies sparse output locations of fragment shaders render correctly");

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");
  runTests();
}

function testAttachment(attachment, expected) {
  gl.readBuffer(gl.COLOR_ATTACHMENT0 + attachment);
  wtu.checkCanvas(gl, expected, `check COLOR_ATTACHMENT${attachment}`, 1);
}

function runTests() {
  var program = wtu.setupProgram(gl, ["vs", "fs"]);
  if (!program) {
    testFailed("Set up program failed");
    return;
  }
  gl.useProgram(program);

  // create a framebuffer with 4 1x1 pixel color attachments
  const fb = gl.createFramebuffer();
  gl.bindFramebuffer(gl.FRAMEBUFFER, fb);

  for (let i = 0; i < 4; ++i) {
    const tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.TEXTURE_2D, tex, 0);
  }

  // draw only to the 1st and 3rd attachments
  gl.drawBuffers([
    gl.NONE,
    gl.COLOR_ATTACHMENT1,
    gl.NONE,
    gl.COLOR_ATTACHMENT3,
  ]);

  // draw
  gl.drawArrays(gl.POINTS, 0, 1);

  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No GL error from set up");

  // check we got the correct values
  testAttachment(0, [0, 0, 0, 0]);
  testAttachment(1, [0, 255, 0, 255]);
  testAttachment(2, [0, 0, 0, 0]);
  testAttachment(3, [0, 0, 255, 255]);

  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No GL error from testing");
}

var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>

</body>
</html>