1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html lang="en"> 3<head> 4 <meta http-equiv="content-type" content="text/html; charset=utf-8"> 5 <title>Shading Language Support</title> 6 <link rel="stylesheet" type="text/css" href="mesa.css"> 7</head> 8<body> 9 10<div class="header"> 11 <h1>The Mesa 3D Graphics Library</h1> 12</div> 13 14<iframe src="contents.html"></iframe> 15<div class="content"> 16 17<h1>Shading Language Support</h1> 18 19<p> 20This page describes the features and status of Mesa's support for the 21<a href="https://opengl.org/documentation/glsl/"> 22OpenGL Shading Language</a>. 23</p> 24 25<p> 26Contents 27</p> 28<ul> 29<li><a href="#envvars">Environment variables</a> 30<li><a href="#support">GLSL 1.40 support</a> 31<li><a href="#unsup">Unsupported Features</a> 32<li><a href="#notes">Implementation Notes</a> 33<li><a href="#hints">Programming Hints</a> 34<li><a href="#standalone">Stand-alone GLSL Compiler</a> 35<li><a href="#implementation">Compiler Implementation</a> 36<li><a href="#validation">Compiler Validation</a> 37</ul> 38 39 40<h2 id="envvars">Environment Variables</h2> 41 42<p> 43The <b>MESA_GLSL</b> environment variable can be set to a comma-separated 44list of keywords to control some aspects of the GLSL compiler and shader 45execution. These are generally used for debugging. 46</p> 47<ul> 48<li><b>dump</b> - print GLSL shader code to stdout at link time 49<li><b>log</b> - log all GLSL shaders to files. 50 The filenames will be "shader_X.vert" or "shader_X.frag" where X 51 the shader ID. 52<li><b>cache_info</b> - print debug information about shader cache 53<li><b>cache_fb</b> - force cached shaders to be ignored and do a full 54 recompile via the fallback path</li> 55<li><b>uniform</b> - print message to stdout when glUniform is called 56<li><b>nopvert</b> - force vertex shaders to be a simple shader that just transforms 57 the vertex position with ftransform() and passes through the color and 58 texcoord[0] attributes. 59<li><b>nopfrag</b> - force fragment shader to be a simple shader that passes 60 through the color attribute. 61<li><b>useprog</b> - log glUseProgram calls to stderr 62<li><b>errors</b> - GLSL compilation and link errors will be reported to stderr. 63</ul> 64<p> 65Example: export MESA_GLSL=dump,nopt 66</p> 67 68<h3 id="replacement">Experimenting with Shader Replacements</h3> 69<p> 70Shaders can be dumped and replaced on runtime for debugging purposes. This 71feature is not currently supported by SCons build. 72 73This is controlled via following environment variables: 74</p> 75<ul> 76<li><b>MESA_SHADER_DUMP_PATH</b> - path where shader sources are dumped 77<li><b>MESA_SHADER_READ_PATH</b> - path where replacement shaders are read 78</ul> 79Note, path set must exist before running for dumping or replacing to work. 80When both are set, these paths should be different so the dumped shaders do 81not clobber the replacement shaders. Also, the filenames of the replacement shaders 82should match the filenames of the corresponding dumped shaders. 83 84<h3 id="capture">Capturing Shaders</h3> 85 86<p> 87Setting <b>MESA_SHADER_CAPTURE_PATH</b> to a directory will cause the compiler 88to write <tt>.shader_test</tt> files for use with 89<a href="https://gitlab.freedesktop.org/mesa/shader-db">shader-db</a>, a tool 90which compiler developers can use to gather statistics about shaders 91(instructions, cycles, memory accesses, and so on). 92</p> 93<p> 94Notably, this captures linked GLSL shaders - with all stages together - 95as well as ARB programs. 96</p> 97 98<h2 id="support">GLSL Version</h2> 99 100<p> 101The GLSL compiler currently supports version 3.30 of the shading language. 102</p> 103 104<p> 105Several GLSL extensions are also supported: 106</p> 107<ul> 108<li>GL_ARB_draw_buffers 109<li>GL_ARB_fragment_coord_conventions 110<li>GL_ARB_shader_bit_encoding 111</ul> 112 113 114<h2 id="unsup">Unsupported Features</h2> 115 116<p>XXX update this section</p> 117 118<p> 119The following features of the shading language are not yet fully supported 120in Mesa: 121</p> 122 123<ul> 124<li>Linking of multiple shaders does not always work. Currently, linking 125 is implemented through shader concatenation and re-compiling. This 126 doesn't always work because of some #pragma and preprocessor issues. 127<li>The gl_Color and gl_SecondaryColor varying vars are interpolated 128 without perspective correction 129</ul> 130 131<p> 132All other major features of the shading language should function. 133</p> 134 135 136<h2 id="notes">Implementation Notes</h2> 137 138<ul> 139<li>Shading language programs are compiled into low-level programs 140 very similar to those of GL_ARB_vertex/fragment_program. 141<li>All vector types (vec2, vec3, vec4, bvec2, etc) currently occupy full 142 float[4] registers. 143<li>Float constants and variables are packed so that up to four floats 144 can occupy one program parameter/register. 145<li>All function calls are inlined. 146<li>Shaders which use too many registers will not compile. 147<li>The quality of generated code is pretty good, register usage is fair. 148<li>Shader error detection and reporting of errors (InfoLog) is not 149 very good yet. 150<li>The ftransform() function doesn't necessarily match the results of 151 fixed-function transformation. 152</ul> 153 154<p> 155These issues will be addressed/resolved in the future. 156</p> 157 158 159<h2 id="hints">Programming Hints</h2> 160 161<ul> 162<li>Use the built-in library functions whenever possible. 163 For example, instead of writing this: 164<pre> 165 float x = 1.0 / sqrt(y); 166</pre> 167 Write this: 168<pre> 169 float x = inversesqrt(y); 170</pre> 171</li> 172</ul> 173 174 175<h2 id="standalone">Stand-alone GLSL Compiler</h2> 176 177<p> 178The stand-alone GLSL compiler program can be used to compile GLSL shaders 179into low-level GPU code. 180</p> 181 182<p> 183This tool is useful for: 184</p> 185<ul> 186<li>Inspecting GPU code to gain insight into compilation 187<li>Generating initial GPU code for subsequent hand-tuning 188<li>Debugging the GLSL compiler itself 189</ul> 190 191<p> 192After building Mesa, the compiler can be found at src/compiler/glsl/glsl_compiler 193</p> 194 195<p> 196Here's an example of using the compiler to compile a vertex shader and 197emit GL_ARB_vertex_program-style instructions: 198</p> 199<pre> 200 src/compiler/glsl/glsl_compiler --version XXX --dump-ast myshader.vert 201</pre> 202 203Options include 204<ul> 205<li><b>--dump-ast</b> - dump GPU code 206<li><b>--dump-hir</b> - dump high-level IR code 207<li><b>--dump-lir</b> - dump low-level IR code 208<li><b>--dump-builder</b> - dump GLSL IR code 209<li><b>--link</b> - link shaders 210<li><b>--just-log</b> - display only shader / linker info if exist, 211without any header or separator 212<li><b>--version</b> - [Mandatory] define the GLSL version to use 213</ul> 214 215 216<h2 id="implementation">Compiler Implementation</h2> 217 218<p> 219The source code for Mesa's shading language compiler is in the 220<code>src/compiler/glsl/</code> directory. 221</p> 222 223<p> 224XXX provide some info about the compiler.... 225</p> 226 227<p> 228The final vertex and fragment programs may be interpreted in software 229(see prog_execute.c) or translated into a specific hardware architecture 230(see drivers/dri/i915/i915_fragprog.c for example). 231</p> 232 233<h2 id="validation">Compiler Validation</h2> 234 235<p> 236Developers working on the GLSL compiler should test frequently to avoid 237regressions. 238</p> 239 240<p> 241The <a href="https://piglit.freedesktop.org/">Piglit</a> project 242has many GLSL tests. 243</p> 244 245<p> 246The Mesa demos repository also has some good GLSL tests. 247</p> 248 249</div> 250</body> 251</html> 252