webgl-debug-shaders.html (5157B)
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 <!DOCTYPE html> 8 <html> 9 <head> 10 <meta charset="utf-8"> 11 <title>WebGL WebGL_debug_shaders Conformance Tests</title> 12 <link rel="stylesheet" href="../../resources/js-test-style.css"/> 13 <script src="../../js/js-test-pre.js"></script> 14 <script src="../../js/webgl-test-utils.js"></script> 15 </head> 16 <body> 17 <div id="description"></div> 18 <canvas id="canvas" style="width: 1px; height: 1px;"> </canvas> 19 <div id="console"></div> 20 <!-- Shaders for testing standard derivatives --> 21 22 <script> 23 "use strict"; 24 description("This test verifies the functionality of the WEBGL_debug_shaders extension, if it is available."); 25 26 debug(""); 27 28 var wtu = WebGLTestUtils; 29 var gl = wtu.create3DContext("canvas"); 30 var ext = null; 31 var shader = null; 32 var program = null; 33 var info = null; 34 var translatedSource; 35 var newTranslatedSource; 36 37 if (!gl) { 38 testFailed("WebGL context does not exist"); 39 } else { 40 testPassed("WebGL context exists"); 41 42 // Query the extension and store globally so shouldBe can access it 43 ext = gl.getExtension("WEBGL_debug_shaders"); 44 if (!ext) { 45 testPassed("No WEBGL_debug_shaders support -- this is legal"); 46 47 runSupportedTest(false); 48 } else { 49 testPassed("Successfully enabled WEBGL_debug_shaders extension"); 50 51 runSupportedTest(true); 52 runTestEnabled(); 53 } 54 } 55 56 function runSupportedTest(extensionEnabled) { 57 var supported = gl.getSupportedExtensions(); 58 if (supported.indexOf("WEBGL_debug_shaders") >= 0) { 59 if (extensionEnabled) { 60 testPassed("WEBGL_debug_shaders listed as supported and getExtension succeeded"); 61 } else { 62 testFailed("WEBGL_debug_shaders listed as supported but getExtension failed"); 63 } 64 } else { 65 if (extensionEnabled) { 66 testFailed("WEBGL_debug_shaders not listed as supported but getExtension succeeded"); 67 } else { 68 testPassed("WEBGL_debug_shaders not listed as supported and getExtension failed -- this is legal"); 69 } 70 } 71 } 72 73 function runTestEnabled() { 74 debug("Testing function with extension enabled"); 75 76 var shaderInfos = [ 77 { 78 source: "void main() { gl_Position = vec4(1.0, 0.0, 0.0, 1.0); }", 79 type: gl.VERTEX_SHADER 80 }, 81 { 82 source: "void main() { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }", 83 type: gl.FRAGMENT_SHADER 84 } 85 ]; 86 87 // Do this twice to test for caching issues. 88 for (var jj = 0; jj < 2; ++jj) { 89 debug("pass:" + (jj + 1)); 90 program = gl.createProgram(); 91 for (var ii = 0; ii < shaderInfos.length; ++ii) { 92 info = shaderInfos[ii]; 93 94 shader = gl.createShader(info.type); 95 96 // if no source has been defined or compileShader() has not been called, 97 // getTranslatedShaderSource() should return an empty string. 98 shouldBe("ext.getTranslatedShaderSource(shader)", '""'); 99 gl.shaderSource(shader, info.source); 100 shouldBe("ext.getTranslatedShaderSource(shader)", '""'); 101 gl.compileShader(shader); 102 shouldBeTrue("gl.getShaderParameter(shader, gl.COMPILE_STATUS)"); 103 translatedSource = ext.getTranslatedShaderSource(shader); 104 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No gl error should occur"); 105 if (translatedSource && translatedSource.length > 0) { 106 testPassed("Successfully called getTranslatedShaderSource()"); 107 } else { 108 testFailed("Calling getTranslatedShaderSource() failed"); 109 } 110 gl.attachShader(program, shader); 111 } 112 gl.linkProgram(program); 113 shouldBeTrue("gl.getProgramParameter(program, gl.LINK_STATUS)"); 114 } 115 116 // Test changing the source. Make sure we get the correct source each time. 117 debug("test changing source"); 118 shader = gl.createShader(gl.FRAGMENT_SHADER); 119 gl.shaderSource(shader, "void main() { gl_FragColor = vec4(gl_FragCoord.x, 0.0, 0.0, 1.0); }"); 120 gl.compileShader(shader); 121 shouldBeTrue("gl.getShaderParameter(shader, gl.COMPILE_STATUS)"); 122 shouldThrow("ext.getTranslatedShaderSource(null)"); 123 translatedSource = ext.getTranslatedShaderSource(shader); 124 shouldBeTrue('translatedSource && translatedSource.indexOf("gl_FragCoord") >= 0'); 125 // change the source but don't compile. 126 gl.shaderSource(shader, "void main() { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }"); 127 // the source should NOT change. It should be the same as the old source. 128 newTranslatedSource = ext.getTranslatedShaderSource(shader); 129 shouldBe('newTranslatedSource', 'translatedSource'); 130 // now compile. 131 gl.compileShader(shader); 132 shouldBeTrue("gl.getShaderParameter(shader, gl.COMPILE_STATUS)"); 133 // the source should have change. 134 newTranslatedSource = ext.getTranslatedShaderSource(shader); 135 shouldNotBe('newTranslatedSource', 'translatedSource'); 136 } 137 138 debug(""); 139 var successfullyParsed = true; 140 </script> 141 <script src="../../js/js-test-post.js"></script> 142 143 </body> 144 </html>