summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/extensions/webgl-draw-buffers-broadcast-return.html
blob: d773ae862149958c0a2871c074fdd2efe77ecc20 (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
<!--
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 WEBGL_draw_buffers Conformance Tests</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>
<script src="../../js/tests/webgl-draw-buffers-utils.js"></script>
</head>
<body>
<div id="description"></div>
<canvas id="canvas" width="64" height="64"> </canvas>
<div id="console"></div>
<script id="fshaderRedWithReturn" type="x-shader/x-fragment">
#extension GL_EXT_draw_buffers : require
precision mediump float;
uniform float u_zero;
void main() {
    gl_FragColor = vec4(1,0,0,1);
    if (u_zero < 1.0) {
        return;
    }
    gl_FragColor = vec4(0,0,1,1);
}
</script>
<script id="fshaderWithDiscard" type="x-shader/x-fragment">
#extension GL_EXT_draw_buffers : require
precision mediump float;
uniform float u_zero;
void main() {
    gl_FragColor = vec4(1,0,0,1);
    if (u_zero < 1.0) {
        discard;
    }
    gl_FragColor = vec4(0,0,1,1);
}
</script>
<script>
"use strict";
description("This test verifies gl_FragColor being broadcasted when using WEBGL_draw_buffers extension, if it is available.");

debug("");

var wtu = WebGLTestUtils;
var canvas = document.getElementById("canvas");
var gl = wtu.create3DContext(canvas);
var ext = null;
var drawBuffersUtils;

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

  // Query the extension and store globally so shouldBe can access it
  ext = gl.getExtension("WEBGL_draw_buffers");
  if (!ext) {
    testPassed("No WEBGL_draw_buffers support -- this is legal");
  } else {
    testPassed("Successfully enabled WEBGL_draw_buffers extension");
    drawBuffersUtils = WebGLDrawBuffersUtils(gl, ext);
    runDrawTests();
  }
}

function runDrawTests() {
  debug("");
  var fb = gl.createFramebuffer();

  var maxUsable = drawBuffersUtils.getMaxUsableColorAttachments();
  var bufs = drawBuffersUtils.makeColorAttachmentArray(maxUsable);

  var width = 64;
  var height = 64;
  var attachments = [];
  for (var ii = 0; ii < maxUsable; ++ii) {
    var tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
    gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + ii, gl.TEXTURE_2D, tex, 0);
    attachments.push({
      texture: tex
    });
  }

  debug("test that gl_FragColor broadcasts if extension is enabled in fragment shader and fragment shader main returns in the middle");
  gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
  ext.drawBuffersWEBGL(bufs);
  var redProgramWithReturn = wtu.setupProgram(gl, [wtu.simpleVertexShader, "fshaderRedWithReturn"], ["vPosition"], undefined, true);
  shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE");
  gl.clearColor(0, 0, 0, 0);
  gl.clear(gl.COLOR_BUFFER_BIT);
  gl.useProgram(redProgramWithReturn);
  wtu.drawUnitQuad(gl);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after draw");

  drawBuffersUtils.checkAttachmentsForColor(attachments, [255, 0, 0, 255]);

  debug("test that none of the attachments are written in case the fragment shader discards");
  gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
  ext.drawBuffersWEBGL(bufs);
  var programWithDiscard = wtu.setupProgram(gl, [wtu.simpleVertexShader, "fshaderWithDiscard"], ["vPosition"], undefined, true);
  gl.clearColor(0, 0, 0, 0);
  gl.clear(gl.COLOR_BUFFER_BIT);
  gl.useProgram(programWithDiscard);
  wtu.drawUnitQuad(gl);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after draw");

  drawBuffersUtils.checkAttachmentsForColor(attachments, [0, 0, 0, 0]);

  gl.bindTexture(gl.TEXTURE_2D, null);
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  gl.deleteFramebuffer(fb);
  attachments.forEach(function(attachment) {
    gl.deleteTexture(attachment.texture);
  });
  gl.deleteProgram(redProgramWithReturn);
  gl.deleteProgram(programWithDiscard);
}

var successfullyParsed = true;

</script>
<script src="../../js/js-test-post.js"></script>
</body>
</html>