summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/extensions/webgl-draw-buffers-max-draw-buffers.html
blob: 8181a6a5870b482a6fc978cfe39efb457706ddb8 (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
<!--
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 gl_FragData[gl_MaxDrawBuffers] Conformance Test</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="64" height="64"> </canvas>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">
attribute vec4 a_position;
void main() {
    gl_Position = a_position;
}
</script>
<script id="fshader" type="x-shader/x-fragment">
#extension GL_EXT_draw_buffers : require
precision mediump float;
void main() {
    gl_FragData[gl_MaxDrawBuffers] = vec4(0.0);
}
</script>
<script id="fshaderConstantIndex" type="x-shader/x-fragment">
#extension GL_EXT_draw_buffers : require
precision mediump float;
void main() {
    gl_FragData[$(gl_MaxDrawBuffers)] = vec4(0.0);
}
</script>
<script id="fshaderTestMaxDrawBuffersValue" type="x-shader/x-fragment">
#extension GL_EXT_draw_buffers : require
precision mediump float;
void main() {
    gl_FragColor = ($(gl_MaxDrawBuffers) == gl_MaxDrawBuffers) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1);
}
</script>
<script>
"use strict";
description("This test verifies that compiling the same shader using GL_EXT_draw_buffers twice will have similar results on both rounds.");

debug("");

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

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

  ext = gl.getExtension("WEBGL_draw_buffers");
  if (!ext) {
    testPassed("No WEBGL_draw_buffers support -- this is legal");
    finishTest();
  } else {
    testPassed("Successfully enabled WEBGL_draw_buffers extension");
    maxDrawBuffers = gl.getParameter(ext.MAX_DRAW_BUFFERS_WEBGL);
    runShadersTest();
    finishTest();
  }
}

function testValueOfMaxDrawBuffers() {
  debug("Test the value of gl_MaxDrawBuffers in a shader");
  var fshader = wtu.replaceParams(wtu.getScript("fshaderTestMaxDrawBuffersValue"), {"gl_MaxDrawBuffers": maxDrawBuffers});
  var program = wtu.setupProgram(gl, ["vshader", fshader], ["a_position"], undefined, true);
  expectTrue(program != null, "Test program should compile");
  wtu.setupUnitQuad(gl);
  wtu.clearAndDrawUnitQuad(gl);
  wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green to indicate that gl_MaxDrawBuffers had the right value");
  gl.deleteProgram(program);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
}

function runSingleTest(shaders, indexMsg) {
  var program = wtu.setupProgram(gl, shaders, ["a_position"], undefined, true);
  var programLinkedSuccessfully = (program != null);
  expectTrue(!programLinkedSuccessfully, "Program where gl_FragData is indexed by " + indexMsg + " should fail compilation.");
  gl.deleteProgram(program);
}

function runShadersTest() {
  debug("MAX_DRAW_BUFFERS_WEBGL is: " + maxDrawBuffers);

  // For reference, use a constant out-of-range parameter to test:
  debug("Test indexing gl_FragData with value of MAX_DRAW_BUFFERS_WEBGL");
  var fshader = wtu.replaceParams(wtu.getScript("fshaderConstantIndex"), {"gl_MaxDrawBuffers": maxDrawBuffers});
  runSingleTest(["vshader", fshader], maxDrawBuffers + " (value of MAX_DRAW_BUFFERS_WEBGL)");

  debug("");

  debug("Test indexing gl_FragData with gl_MaxDrawBuffers");
  debug("Repeat this test twice as that has revealed a bug.");
  for (var i = 0; i < 2; ++i) {
    runSingleTest(["vshader", "fshader"], "gl_MaxDrawBuffers");
  }
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");

  debug("");

  testValueOfMaxDrawBuffers();
}
</script>
</body>
</html>