program-handling.html (4560B)
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 <!DOCTYPE html> 7 <html> 8 <head> 9 <meta charset="utf-8"> 10 <title>WebGL Program Handling Conformance Test</title> 11 <link rel="stylesheet" href="../../resources/js-test-style.css"/> 12 <script src="../../js/js-test-pre.js"></script> 13 <script src="../../js/desktop-gl-constants.js"></script> 14 <script src="../../js/webgl-test-utils.js"></script> 15 </head> 16 <body> 17 <script id="vshader" type="x-shader/x-vertex"> 18 attribute vec4 a_position; 19 uniform vec4 u_color; 20 varying vec4 v_color; 21 void main() 22 { 23 v_color = u_color; 24 gl_Position = a_position; 25 } 26 </script> 27 <script id="fshader" type="x-shader/x-fragment"> 28 precision mediump float; 29 varying vec4 v_color; 30 void main() 31 { 32 gl_FragColor = v_color; 33 } 34 </script> 35 <script id="fshader-not-link" type="x-shader/x-fragment"> 36 precision mediump float; 37 varying vec4 foo; 38 void main() 39 { 40 gl_FragColor = foo; 41 } 42 </script> 43 44 <canvas id="canvas" width="16" height="16"> </canvas> 45 <div id="description"></div> 46 <div id="console"></div> 47 48 <script> 49 "use strict"; 50 51 function compile(gl, shader, src) { 52 var shaderSource = document.getElementById(src).text; 53 gl.shaderSource(shader, shaderSource); 54 gl.compileShader(shader); 55 } 56 57 debug(""); 58 debug("Canvas.getContext"); 59 60 var wtu = WebGLTestUtils; 61 var gl = wtu.create3DContext("canvas"); 62 if (!gl) { 63 testFailed("context does not exist"); 64 } 65 66 function testProgramInvalidation() { 67 debug(''); 68 debug('== Testing invalidation of the current program =='); 69 var vs = wtu.loadShaderFromScript(gl, "vshader"); 70 var fs = wtu.loadShaderFromScript(gl, "fshader"); 71 var prg = wtu.createProgram(gl, vs, fs); 72 gl.useProgram(prg); 73 const positionLoc = gl.getAttribLocation(prg, 'a_position'); 74 const colorLoc = gl.getUniformLocation(prg, 'u_color'); 75 76 wtu.setupUnitQuad(gl, positionLoc); 77 78 debug("Draw red with valid program"); 79 gl.uniform4fv(colorLoc, [1, 0, 0, 1]); 80 wtu.drawUnitQuad(gl); 81 wtu.checkCanvas(gl, [255, 0, 0, 255], "should be red"); 82 83 debug("Change fragment shader to one that will not link"); 84 compile(gl, fs, "fshader-not-link"); 85 debug("Draw orange"); 86 gl.uniform4fv(colorLoc, [1, 127/255, 0, 1]); 87 wtu.drawUnitQuad(gl); 88 wtu.checkCanvas(gl, [255, 127, 0, 255], "should be orange"); 89 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors before relink"); 90 91 debug("Try linking"); 92 gl.linkProgram(prg); 93 assertMsg(gl.getProgramParameter(prg, gl.LINK_STATUS) == false, "link should fail"); 94 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors after linkProgram"); 95 debug("Attempt to draw green; because link failed, in WebGL, the draw should generate INVALID_OPERATION"); 96 gl.uniform4fv(colorLoc, [0, 1, 0, 1]); 97 wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "because the current program has been invalidated, uniform* calls generate INVALID_OPERATION"); 98 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors before draw"); 99 wtu.drawUnitQuad(gl); 100 wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "draw with invalidated program should fail"); 101 wtu.checkCanvas(gl, [255, 127, 0, 255], "should still be orange"); 102 } 103 104 function testProgramShaderRemoval() { 105 debug(''); 106 debug('== Testing removal of shaders from the current program =='); 107 var vs = wtu.loadShaderFromScript(gl, "vshader"); 108 var fs = wtu.loadShaderFromScript(gl, "fshader"); 109 var prg = wtu.createProgram(gl, vs, fs); 110 gl.useProgram(prg); 111 const positionLoc = gl.getAttribLocation(prg, 'a_position'); 112 const colorLoc = gl.getUniformLocation(prg, 'u_color'); 113 114 wtu.setupUnitQuad(gl, positionLoc); 115 116 debug("Draw red with valid program"); 117 gl.uniform4fv(colorLoc, [1, 0, 0, 1]); 118 wtu.drawUnitQuad(gl); 119 wtu.checkCanvas(gl, [255, 0, 0, 255], "should be red"); 120 121 debug("Detach and delete shaders"); 122 gl.detachShader(prg, vs); 123 gl.detachShader(prg, fs); 124 gl.deleteShader(vs); 125 gl.deleteShader(fs); 126 127 debug("Draw blue to show even though shaders are gone program is still valid"); 128 gl.uniform4fv(colorLoc, [0, 0, 1, 1]); 129 wtu.drawUnitQuad(gl); 130 wtu.checkCanvas(gl, [0, 0, 255, 255], "should be blue"); 131 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 132 } 133 134 testProgramInvalidation(); 135 testProgramShaderRemoval(); 136 137 var successfullyParsed = true; 138 </script> 139 140 <script src="../../js/js-test-post.js"></script> 141 </body> 142 </html>