conditional-discard-optimization.html (3436B)
1 <!-- 2 Copyright (c) 2019 The Khronos Group Inc. 3 Use of this source code is governed by an MIT-style license that can be 4 found in the LICENSE.txt file. 5 --> 6 7 <!-- author: Bill Baxter (wbaxter at google.com) --> 8 9 <!DOCTYPE html> 10 <html> 11 <head> 12 <meta charset="utf-8"> 13 <title>ANGLE WebGL Shader Conditionals Repro</title> 14 <link rel="stylesheet" href="../../../resources/js-test-style.css"/> 15 <script src="../../../js/js-test-pre.js"></script> 16 <script src="../../../js/webgl-test-utils.js"></script> 17 </head> 18 19 <body> 20 <canvas id="repro" style="border: none;" width="256" height="256"></canvas> 21 <div id="description"></div> 22 <div id="console"></div> 23 24 <script id="shader-vs" type="x-shader/x-vertex"> 25 attribute vec2 pos; 26 varying mediump float varA; 27 void main(void) { 28 varA = 0.; 29 gl_Position = vec4(pos, 0.0, 1.0); 30 } 31 </script> 32 33 <script id="shader-fs" type="x-shader/x-fragment"> 34 precision mediump float; 35 varying float varA; 36 void main(void) { 37 if (varA < -1. || (varA < -1. && varA > 1.)) { 38 discard; 39 } 40 gl_FragColor = vec4(0, 1, 0, 1) + 2. * varA * 2.; 41 } 42 </script> 43 44 <script id="shader-fs-mutable" type="x-shader/x-fragment"> 45 precision mediump float; 46 varying float varA; 47 void main(void) { 48 float b = varA; 49 if (varA < (b -= 1.) || (varA < b && varA > (b += 2.))) { 50 discard; 51 } 52 gl_FragColor = vec4(0, 1, 0, 1) + 2. * varA * 2.; 53 } 54 </script> 55 <script id="shader-fs-unfolded" type="x-shader/x-fragment"> 56 precision mediump float; 57 varying float varA; 58 void main(void) { 59 bool s1 = varA < -1.; 60 if (!s1) { 61 bool s2 = varA < -1.; 62 if (s2) { 63 s2 = varA > 1.; 64 } 65 s1 = s2; 66 } 67 if (s1) { 68 discard; 69 } 70 gl_FragColor = vec4(0, 1, 0, 1) + 2. * varA * 2.; 71 } 72 </script> 73 <script> 74 "use strict"; 75 76 description(); 77 debug(""); 78 debug("If things are working correctly, then there will be a green square."); 79 debug("Otherwise it will be a black void."); 80 debug("This is a repro for an issue seen on the D3D9 ANGLE implementation of WebGL on Chrome in a shader with a conditional discard, where the conditional is of the form (a || (b && c))."); 81 82 var wtu = WebGLTestUtils; 83 var canvas = document.getElementById("repro"); 84 var gl = wtu.create3DContext(canvas); 85 if (!gl) { 86 testFailed("context does not exist"); 87 } else { 88 gl.clearColor(0.0, 0.0, 0.0, 1.0); 89 wtu.setupUnitQuad(gl); 90 91 debug(""); 92 debug("Testing shader with conditional discard"); 93 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 94 var program = wtu.setupProgram(gl, ["shader-vs", "shader-fs"], ["pos"], undefined, true); 95 wtu.drawUnitQuad(gl); 96 wtu.checkCanvasRect(gl, 128, 128, 128, 128, [ 0, 255, 0, 255 ], "should be green", 1); 97 98 debug(""); 99 debug("Testing conditional discard with side-effects in conditions"); 100 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 101 var programMutable = wtu.setupProgram(gl, ["shader-vs", "shader-fs-mutable"], ["pos"], undefined, true); 102 wtu.drawUnitQuad(gl); 103 wtu.checkCanvasRect(gl, 128, 128, 128, 128, [ 0, 255, 0, 255 ], "should be green", 1); 104 105 debug(""); 106 debug("Testing conditional discard with unfolded condition logic"); 107 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 108 var programMutable = wtu.setupProgram(gl, ["shader-vs", "shader-fs-unfolded"], ["pos"], undefined, true); 109 wtu.drawUnitQuad(gl); 110 wtu.checkCanvasRect(gl, 128, 128, 128, 128, [ 0, 255, 0, 255 ], "should be green", 1); 111 } 112 113 var successfullyParsed = true; 114 </script> 115 <script src="../../../js/js-test-post.js"></script> 116 </body> 117 </html>