shading.html 6.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<HTML>

<TITLE>Shading Language Support</TITLE>

<link rel="stylesheet" type="text/css" href="mesa.css"></head>

<BODY>

<H1>Shading Language Support</H1>

<p>
This page describes the features and status of Mesa's support for the
<a href="http://opengl.org/documentation/glsl/" target="_parent">
OpenGL Shading Language</a>.
</p>

Brian's avatar
Brian committed
17 18 19 20
<p>
Contents
</p>
<ul>
21
<li><a href="#envvars">Environment variables</a>
Brian Paul's avatar
Brian Paul committed
22
<li><a href="#120">GLSL 1.20 support</a>
Brian's avatar
Brian committed
23
<li><a href="#unsup">Unsupported Features</a>
Brian's avatar
Brian committed
24
<li><a href="#notes">Implementation Notes</a>
Brian's avatar
Brian committed
25
<li><a href="#hints">Programming Hints</a>
26
<li><a href="#standalone">Stand-alone GLSL Compiler</a>
Brian's avatar
Brian committed
27
<li><a href="#implementation">Compiler Implementation</a>
Brian's avatar
Brian committed
28
<li><a href="#validation">Compiler Validation</a>
Brian's avatar
Brian committed
29 30 31
</ul>


Brian Paul's avatar
Brian Paul committed
32

33 34 35 36 37
<a name="envvars">
<h2>Environment Variables</h2>

<p>
The <b>MESA_GLSL</b> environment variable can be set to a comma-separated
38 39
list of keywords to control some aspects of the GLSL compiler and shader
execution.  These are generally used for debugging.
40 41
</p>
<ul>
42 43
<li><b>dump</b> - print GLSL shader code to stdout at link time
<li><b>log</b> - log all GLSL shaders to files.
44 45
    The filenames will be "shader_X.vert" or "shader_X.frag" where X
    the shader ID.
46 47 48 49
<li><b>nopt</b> - disable compiler optimizations
<li><b>opt</b> - force compiler optimizations
<li><b>uniform</b> - print message to stdout when glUniform is called
<li><b>nopvert</b> - force vertex shaders to be a simple shader that just transforms
50 51
    the vertex position with ftransform() and passes through the color and
    texcoord[0] attributes.
52
<li><b>nopfrag</b> - force fragment shader to be a simple shader that passes
53
    through the color attribute.
54
<li><b>useprog</b> - log glUseProgram calls to stderr
55 56 57 58 59 60
</ul>
<p>
Example:  export MESA_GLSL=dump,nopt
</p>


Brian Paul's avatar
Brian Paul committed
61
<a name="120">
62
<h2>GLSL Version</h2>
Brian Paul's avatar
Brian Paul committed
63 64

<p>
65 66 67 68 69 70
The GLSL compiler currently supports version 1.20 of the shading language.
</p>

<p>
Several GLSL extensions are also supported:
</p>
Brian Paul's avatar
Brian Paul committed
71
<ul>
72 73 74 75
<li>GL_ARB_draw_buffers
<li>GL_ARB_texture_rectangle
<li>GL_ARB_fragment_coord_conventions
<li>GL_EXT_texture_array
Brian Paul's avatar
Brian Paul committed
76 77 78
</ul>


Brian's avatar
Brian committed
79
<a name="unsup">
80 81
<h2>Unsupported Features</h2>

82 83
<p>XXX update this section</p>

84
<p>
85
The following features of the shading language are not yet fully supported
86 87 88 89
in Mesa:
</p>

<ul>
90 91 92
<li>Linking of multiple shaders does not always work.  Currently, linking
    is implemented through shader concatenation and re-compiling.  This
    doesn't always work because of some #pragma and preprocessor issues.
Brian's avatar
Brian committed
93
<li>gl_ClipVertex
Brian's avatar
Brian committed
94 95
<li>The gl_Color and gl_SecondaryColor varying vars are interpolated
    without perspective correction
96 97 98 99 100 101 102
</ul>

<p>
All other major features of the shading language should function.
</p>


Brian's avatar
Brian committed
103
<a name="notes">
104 105 106 107 108
<h2>Implementation Notes</h2>

<ul>
<li>Shading language programs are compiled into low-level programs
    very similar to those of GL_ARB_vertex/fragment_program.
Brian's avatar
Brian committed
109
<li>All vector types (vec2, vec3, vec4, bvec2, etc) currently occupy full
110
    float[4] registers.
Brian's avatar
Brian committed
111 112
<li>Float constants and variables are packed so that up to four floats
    can occupy one program parameter/register.
113 114 115 116 117
<li>All function calls are inlined.
<li>Shaders which use too many registers will not compile.
<li>The quality of generated code is pretty good, register usage is fair.
<li>Shader error detection and reporting of errors (InfoLog) is not
    very good yet.
Brian's avatar
Brian committed
118 119
<li>The ftransform() function doesn't necessarily match the results of
    fixed-function transformation.
120 121 122 123 124 125 126
</ul>

<p>
These issues will be addressed/resolved in the future.
</p>


Brian's avatar
Brian committed
127
<a name="hints">
128 129 130 131 132 133 134 135 136 137 138 139
<h2>Programming Hints</h2>

<ul>
<li>Use the built-in library functions whenever possible.
    For example, instead of writing this:
<pre>
        float x = 1.0 / sqrt(y);
</pre>
    Write this:
<pre>
        float x = inversesqrt(y);
</pre>
Brian's avatar
Brian committed
140
</li>
141 142 143
</ul>


Brian's avatar
Brian committed
144
<a name="standalone">
145
<h2>Stand-alone GLSL Compiler</h2>
Brian's avatar
Brian committed
146 147

<p>
148 149
The stand-alone GLSL compiler program can be used to compile GLSL shaders
into low-level GPU code.
Brian's avatar
Brian committed
150 151 152 153 154 155 156 157 158 159 160 161
</p>

<p>
This tool is useful for:
<p>
<ul>
<li>Inspecting GPU code to gain insight into compilation
<li>Generating initial GPU code for subsequent hand-tuning
<li>Debugging the GLSL compiler itself
</ul>

<p>
162
After building Mesa, the compiler can be found at src/glsl/glsl_compiler
Brian's avatar
Brian committed
163 164 165 166 167 168 169
</p>

<p>
Here's an example of using the compiler to compile a vertex shader and
emit GL_ARB_vertex_program-style instructions:
</p>
<pre>
Sam Hocevar's avatar
Sam Hocevar committed
170
    src/glsl/glsl_compiler --dump-ast myshader.vert
Brian's avatar
Brian committed
171 172
</pre>

173 174 175 176 177 178 179 180
Options include
<ul>
<li><b>--dump-ast</b> - dump GPU code
<li><b>--dump-hir</b> - dump high-level IR code
<li><b>--dump-lir</b> - dump low-level IR code
<li><b>--link</b> - ???
</ul>

Brian's avatar
Brian committed
181

Brian's avatar
Brian committed
182 183 184 185 186 187 188


<a name="implementation">
<h2>Compiler Implementation</h2>

<p>
The source code for Mesa's shading language compiler is in the
189
<code>src/glsl/</code> directory.
Brian's avatar
Brian committed
190 191 192
</p>

<p>
193
XXX provide some info about the compiler....
Brian's avatar
Brian committed
194 195 196 197 198 199 200 201
</p>

<p>
The final vertex and fragment programs may be interpreted in software
(see prog_execute.c) or translated into a specific hardware architecture
(see drivers/dri/i915/i915_fragprog.c for example).
</p>

Brian's avatar
Brian committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
<h3>Code Generation Options</h3>

<p>
Internally, there are several options that control the compiler's code
generation and instruction selection.
These options are seen in the gl_shader_state struct and may be set
by the device driver to indicate its preferences:

<pre>
struct gl_shader_state
{
   ...
   /** Driver-selectable options: */
   GLboolean EmitHighLevelInstructions;
   GLboolean EmitCondCodes;
   GLboolean EmitComments;
};
</pre>

<ul>
<li>EmitHighLevelInstructions
<br>
This option controls instruction selection for loops and conditionals.
If the option is set high-level IF/ELSE/ENDIF, LOOP/ENDLOOP, CONT/BRK
instructions will be emitted.
Otherwise, those constructs will be implemented with BRA instructions.
</li>

<li>EmitCondCodes
<br>
If set, condition codes (ala GL_NV_fragment_program) will be used for
branching and looping.
Otherwise, ordinary registers will be used (the IF instruction will
examine the first operand's X component and do the if-part if non-zero).
This option is only relevant if EmitHighLevelInstructions is set.
</li>

<li>EmitComments
<br>
If set, instructions will be annoted with comments to help with debugging.
Extra NOP instructions will also be inserted.
</br>

</ul>
Brian's avatar
Brian committed
246 247


Brian's avatar
Brian committed
248 249 250 251
<a name="validation">
<h2>Compiler Validation</h2>

<p>
252
Developers working on the GLSL compiler should test frequently to avoid
Brian's avatar
Brian committed
253 254
regressions.
</p>
255

Brian's avatar
Brian committed
256
<p>
257 258 259 260
The <a href="http://people.freedesktop.org/~nh/piglit/">Piglit</a> project
has many GLSL tests and the
<a href="http://glean.sf.net" target="_parent">Glean</a> glsl1 test 
tests GLSL features.
Brian's avatar
Brian committed
261 262
</p>

263 264 265
<p>
The Mesa demos repository also has some good GLSL tests.
</p>
Brian's avatar
Brian committed
266

267 268
</BODY>
</HTML>