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.
10 <meta charset=
"utf-8">
11 <title>WebGL Shader Translator
</title>
16 font-family: monospace;
25 // Needed by wtu.create3DContext():
26 var testFailed = function() {};
28 <script src=
"../js/webgl-test-utils.js"> </script>
35 var enabledExtensions
= [];
41 translateButton
= document
.getElementById('translate');
42 extButton
= document
.getElementById('getExts');
44 var wtu
= WebGLTestUtils
;
45 // Prioritize creating a WebGL 2 context if possible - if not, fall back to WebGL 1.
46 gl
= wtu
.create3DContext(undefined, undefined, 2);
47 if (!gl
|| !gl
.getExtension('WEBGL_debug_shaders'))
49 gl
= wtu
.create3DContext();
56 debugShaders
= gl
.getExtension('WEBGL_debug_shaders');
63 translateButton
.disabled
= true;
64 extButton
.disabled
= true;
65 document
.getElementById('disabled').style
.display
= 'block';
68 function getExtensions() {
71 'EXT_shader_texture_lod',
72 'OES_standard_derivatives',
77 function getExtensionSet(shaderExtensions
) {
78 for (var i
= 0; i
< shaderExtensions
.length
; ++i
) {
79 if (enabledExtensions
.indexOf(shaderExtensions
[i
]) < 0) {
80 var ext
= gl
.getExtension(shaderExtensions
[i
]);
82 enabledExtensions
.push(shaderExtensions
[i
]);
86 if (enabledExtensions
.length
> 0) {
87 document
.getElementById('enabled-extensions').textContent
= enabledExtensions
.join(', ');
91 function translateShader() {
92 var sourceElement
= document
.getElementById('original-shader');
93 var source
= sourceElement
.value
;
95 var output
= document
.getElementById('translated-shader');
96 var infoLog
= document
.getElementById('info-log');
97 var shaderType
= document
.getElementById('shader-type');
100 // Try compiling the source as both vertex and fragment shader and see if either one works
101 var tryCompile = function(type
) {
102 var shader
= gl
.createShader(type
);
103 gl
.shaderSource(shader
, source
);
104 gl
.compileShader(shader
);
106 if (type
== gl
.FRAGMENT_SHADER
) {
107 shaderTypeStr
= 'Fragment shader';
109 shaderTypeStr
= 'Vertex shader';
111 if (gl
.getShaderParameter(shader
, gl
.COMPILE_STATUS
)) {
112 shaderType
.textContent
= shaderTypeStr
;
113 var translatedSource
= debugShaders
.getTranslatedShaderSource(shader
);
114 output
.value
= translatedSource
;
115 infoLog
.value
= gl
.getShaderInfoLog(shader
);
118 infoLog
.value
+= 'Info log when compiling as ' + shaderTypeStr
+ ':\n' + gl
.getShaderInfoLog(shader
) + '\n';
123 if (!tryCompile(gl
.FRAGMENT_SHADER
) && !tryCompile(gl
.VERTEX_SHADER
)) {
124 output
.value
= 'Invalid shader, compilation failed as both fragment and vertex shader.';
125 shaderType
.textContent
= 'Shader not';
131 <h1>WebGL Shader Translator
</h1>
132 <p>This page uses the browser's built-in shader translation facilities to show how a shader
133 is changed before being passed on to the underlying platform's graphics driver.
</p>
134 <p id=
"disabled" style=
"display: none;">
135 WebGL or WEBGL_debug_shaders extension is not available on this browser configuration.
136 Use a different browser or look for other alternatives to enable the extension to be able to use this page.
137 The extension might be behind a runtime flag for privacy considerations.
139 <h2>WebGL GLSL shader
</h2>
140 <textarea id=
"original-shader"></textarea>
142 <input type=
"button" id=
"translate" value=
"Translate" onclick=
"translateShader()"></input>
143 <input type=
"button" id=
"getExts" value=
"Enable supported GLSL extensions" onclick=
"getExtensions()"></input>
145 <h2><span id=
"shader-type">Shader
</span> translated for graphics driver
</h2>
146 <textarea id=
"translated-shader"></textarea>
147 <h2>Enabled shader extensions
</h2>
148 <p id=
"enabled-extensions">None
</p>
149 <h2>Shader info log
</h2>
150 <textarea id=
"info-log"></textarea>