blob: 750884cf36c448c8acfb7fd98edb0d0d70bfa5e7 [file] [log] [blame]
Brian9595d192007-01-20 13:40:57 -07001<HTML>
2
3<TITLE>Shading Language Support</TITLE>
4
5<link rel="stylesheet" type="text/css" href="mesa.css"></head>
6
7<BODY>
8
9<H1>Shading Language Support</H1>
10
11<p>
12This page describes the features and status of Mesa's support for the
13<a href="http://opengl.org/documentation/glsl/" target="_parent">
14OpenGL Shading Language</a>.
15</p>
16
17<p>
Brian61d31ae2007-02-17 09:41:19 -070018Contents
19</p>
20<ul>
Brian Paul439909a2009-04-07 11:09:53 -060021<li><a href="#envvars">Environment variables</a>
Brian Paulc4341fe2008-12-15 18:30:40 -070022<li><a href="#120">GLSL 1.20 support</a>
Brian61d31ae2007-02-17 09:41:19 -070023<li><a href="#unsup">Unsupported Features</a>
Brian07e62082007-02-27 16:45:40 -070024<li><a href="#notes">Implementation Notes</a>
Brian61d31ae2007-02-17 09:41:19 -070025<li><a href="#hints">Programming Hints</a>
Brian Paul3bfedb72008-07-17 15:40:10 -060026<li><a href="#standalone">Stand-alone GLSL Compiler</a>
Brian07e62082007-02-27 16:45:40 -070027<li><a href="#implementation">Compiler Implementation</a>
Brian7eba12e2007-03-28 17:14:35 -060028<li><a href="#validation">Compiler Validation</a>
Brian61d31ae2007-02-17 09:41:19 -070029</ul>
30
31
Brian Paulc4341fe2008-12-15 18:30:40 -070032
Brian Paul439909a2009-04-07 11:09:53 -060033<a name="envvars">
34<h2>Environment Variables</h2>
35
36<p>
37The <b>MESA_GLSL</b> environment variable can be set to a comma-separated
Brian Paul85fb3e42009-10-14 11:28:28 -060038list of keywords to control some aspects of the GLSL compiler and shader
39execution. These are generally used for debugging.
Brian Paul439909a2009-04-07 11:09:53 -060040</p>
41<ul>
42<li>dump - print GLSL shader code to stdout at link time
43<li>log - log all GLSL shaders to files.
44 The filenames will be "shader_X.vert" or "shader_X.frag" where X
45 the shader ID.
46<li>nopt - disable compiler optimizations
47<li>opt - force compiler optimizations
48<li>uniform - print message to stdout when glUniform is called
Brian Paul85fb3e42009-10-14 11:28:28 -060049<li>nopvert - force vertex shaders to be a simple shader that just transforms
50 the vertex position with ftransform() and passes through the color and
51 texcoord[0] attributes.
52<li>nopfrag - force fragment shader to be a simple shader that passes
53 through the color attribute.
Brian Paul347fb372009-10-22 09:38:39 -060054<li>useprog - log glUseProgram calls to stderr
Brian Paul439909a2009-04-07 11:09:53 -060055</ul>
56<p>
57Example: export MESA_GLSL=dump,nopt
58</p>
59
60
Brian Paulc4341fe2008-12-15 18:30:40 -070061<a name="120">
62<h2>GLSL 1.20 support</h2>
63
64<p>
Brian Paul439909a2009-04-07 11:09:53 -060065GLSL version 1.20 is supported in Mesa 7.3 and later.
Brian Paulc4341fe2008-12-15 18:30:40 -070066Among the features/differences of GLSL 1.20 are:
67<ul>
68<li><code>mat2x3, mat2x4</code>, etc. types and functions
69<li><code>transpose(), outerProduct(), matrixCompMult()</code> functions
70(but untested)
71<li>precision qualifiers (lowp, mediump, highp)
72<li><code>invariant</code> qualifier
73<li><code>array.length()</code> method
74<li><code>float[5] a;</code> array syntax
75<li><code>centroid</code> qualifier
76<li>unsized array constructors
77<li>initializers for uniforms
78<li>const initializers calling built-in functions
79</ul>
80
81
82
Brian61d31ae2007-02-17 09:41:19 -070083<a name="unsup">
Brian9595d192007-01-20 13:40:57 -070084<h2>Unsupported Features</h2>
85
86<p>
Brian Paul439909a2009-04-07 11:09:53 -060087The following features of the shading language are not yet fully supported
Brian9595d192007-01-20 13:40:57 -070088in Mesa:
89</p>
90
91<ul>
Brian Paul439909a2009-04-07 11:09:53 -060092<li>Linking of multiple shaders does not always work. Currently, linking
93 is implemented through shader concatenation and re-compiling. This
94 doesn't always work because of some #pragma and preprocessor issues.
Brianb03e1712007-03-09 09:51:55 -070095<li>gl_ClipVertex
Brian4b1d1b72007-04-27 15:25:00 -060096<li>The gl_Color and gl_SecondaryColor varying vars are interpolated
97 without perspective correction
Brian9595d192007-01-20 13:40:57 -070098</ul>
99
100<p>
101All other major features of the shading language should function.
102</p>
103
104
Brian07e62082007-02-27 16:45:40 -0700105<a name="notes">
Brian9595d192007-01-20 13:40:57 -0700106<h2>Implementation Notes</h2>
107
108<ul>
109<li>Shading language programs are compiled into low-level programs
110 very similar to those of GL_ARB_vertex/fragment_program.
Brianbbec2fd2007-01-28 12:11:10 -0700111<li>All vector types (vec2, vec3, vec4, bvec2, etc) currently occupy full
Brian9595d192007-01-20 13:40:57 -0700112 float[4] registers.
Brianbbec2fd2007-01-28 12:11:10 -0700113<li>Float constants and variables are packed so that up to four floats
114 can occupy one program parameter/register.
Brian9595d192007-01-20 13:40:57 -0700115<li>All function calls are inlined.
116<li>Shaders which use too many registers will not compile.
117<li>The quality of generated code is pretty good, register usage is fair.
118<li>Shader error detection and reporting of errors (InfoLog) is not
119 very good yet.
Brian05e6fd82007-03-27 16:05:25 -0600120<li>The ftransform() function doesn't necessarily match the results of
121 fixed-function transformation.
Brian9595d192007-01-20 13:40:57 -0700122</ul>
123
124<p>
125These issues will be addressed/resolved in the future.
126</p>
127
128
Brian61d31ae2007-02-17 09:41:19 -0700129<a name="hints">
Brian9595d192007-01-20 13:40:57 -0700130<h2>Programming Hints</h2>
131
132<ul>
Brianbbec2fd2007-01-28 12:11:10 -0700133<li>Declare <em>in</em> function parameters as <em>const</em> whenever possible.
Brian9595d192007-01-20 13:40:57 -0700134 This improves the efficiency of function inlining.
135</li>
136<br>
137<li>To reduce register usage, declare variables within smaller scopes.
138 For example, the following code:
139<pre>
140 void main()
141 {
142 vec4 a1, a2, b1, b2;
143 gl_Position = expression using a1, a2.
144 gl_Color = expression using b1, b2;
145 }
146</pre>
147 Can be rewritten as follows to use half as many registers:
148<pre>
149 void main()
150 {
151 {
152 vec4 a1, a2;
153 gl_Position = expression using a1, a2.
154 }
155 {
156 vec4 b1, b2;
157 gl_Color = expression using b1, b2;
158 }
159 }
160</pre>
161 Alternately, rather than using several float variables, use
162 a vec4 instead. Use swizzling and writemasks to access the
163 components of the vec4 as floats.
164</li>
165<br>
166<li>Use the built-in library functions whenever possible.
167 For example, instead of writing this:
168<pre>
169 float x = 1.0 / sqrt(y);
170</pre>
171 Write this:
172<pre>
173 float x = inversesqrt(y);
174</pre>
Brian7eba12e2007-03-28 17:14:35 -0600175<li>
176 Use ++i when possible as it's more efficient than i++
177</li>
Brian9595d192007-01-20 13:40:57 -0700178</ul>
179
180
Brian61d31ae2007-02-17 09:41:19 -0700181<a name="standalone">
Brian Paul3bfedb72008-07-17 15:40:10 -0600182<h2>Stand-alone GLSL Compiler</h2>
Brian61d31ae2007-02-17 09:41:19 -0700183
184<p>
185A unique stand-alone GLSL compiler driver has been added to Mesa.
186<p>
187
188<p>
189The stand-alone compiler (like a conventional command-line compiler)
190is a tool that accepts Shading Language programs and emits low-level
191GPU programs.
192</p>
193
194<p>
195This tool is useful for:
196<p>
197<ul>
198<li>Inspecting GPU code to gain insight into compilation
199<li>Generating initial GPU code for subsequent hand-tuning
200<li>Debugging the GLSL compiler itself
201</ul>
202
203<p>
Brian Paul77497eb2008-07-21 09:01:21 -0600204After building Mesa, the glslcompiler can be built by manually running:
Brian61d31ae2007-02-17 09:41:19 -0700205</p>
Brianff0cc922007-02-22 16:29:48 -0700206<pre>
Brian Paul439909a2009-04-07 11:09:53 -0600207 make realclean
208 make linux
Brian Paul3bfedb72008-07-17 15:40:10 -0600209 cd src/mesa/drivers/glslcompiler
Brianff0cc922007-02-22 16:29:48 -0700210 make
211</pre>
212
Brian61d31ae2007-02-17 09:41:19 -0700213
214<p>
215Here's an example of using the compiler to compile a vertex shader and
216emit GL_ARB_vertex_program-style instructions:
217</p>
218<pre>
Brian Paul3bfedb72008-07-17 15:40:10 -0600219 bin/glslcompiler --debug --numbers --fs progs/glsl/CH06-brick.frag.txt
Brian61d31ae2007-02-17 09:41:19 -0700220</pre>
221<p>
Brian Paul3bfedb72008-07-17 15:40:10 -0600222results in:
Brian61d31ae2007-02-17 09:41:19 -0700223</p>
224<pre>
Brian Paul3bfedb72008-07-17 15:40:10 -0600225# Fragment Program/Shader
226 0: RCP TEMP[4].x, UNIFORM[2].xxxx;
227 1: RCP TEMP[4].y, UNIFORM[2].yyyy;
228 2: MUL TEMP[3].xy, VARYING[0], TEMP[4];
229 3: MOV TEMP[1], TEMP[3];
230 4: MUL TEMP[0].w, TEMP[1].yyyy, CONST[4].xxxx;
231 5: FRC TEMP[1].z, TEMP[0].wwww;
232 6: SGT.C TEMP[0].w, TEMP[1].zzzz, CONST[4].xxxx;
233 7: IF (NE.wwww); # (if false, goto 9);
234 8: ADD TEMP[1].x, TEMP[1].xxxx, CONST[4].xxxx;
235 9: ENDIF;
236 10: FRC TEMP[1].xy, TEMP[1];
237 11: SGT TEMP[2].xy, UNIFORM[3], TEMP[1];
238 12: MUL TEMP[1].z, TEMP[2].xxxx, TEMP[2].yyyy;
239 13: LRP TEMP[0], TEMP[1].zzzz, UNIFORM[0], UNIFORM[1];
240 14: MUL TEMP[0].xyz, TEMP[0], VARYING[1].xxxx;
241 15: MOV OUTPUT[0].xyz, TEMP[0];
242 16: MOV OUTPUT[0].w, CONST[4].yyyy;
243 17: END
Brian61d31ae2007-02-17 09:41:19 -0700244</pre>
245
246<p>
247Note that some shading language constructs (such as uniform and varying
248variables) aren't expressible in ARB or NV-style programs.
249Therefore, the resulting output is not always legal by definition of
250those program languages.
251</p>
252<p>
253Also note that this compiler driver is still under development.
254Over time, the correctness of the GPU programs, with respect to the ARB
255and NV languagues, should improve.
256</p>
257
Brian07e62082007-02-27 16:45:40 -0700258
259
260<a name="implementation">
261<h2>Compiler Implementation</h2>
262
263<p>
264The source code for Mesa's shading language compiler is in the
265<code>src/mesa/shader/slang/</code> directory.
266</p>
267
268<p>
269The compiler follows a fairly standard design and basically works as follows:
270</p>
271<ul>
272<li>The input string is tokenized (see grammar.c) and parsed
273(see slang_compiler_*.c) to produce an Abstract Syntax Tree (AST).
274The nodes in this tree are slang_operation structures
275(see slang_compile_operation.h).
276The nodes are decorated with symbol table, scoping and datatype information.
277<li>The AST is converted into an Intermediate representation (IR) tree
278(see the slang_codegen.c file).
279The IR nodes represent basic GPU instructions, like add, dot product,
280move, etc.
281The IR tree is mostly a binary tree, but a few nodes have three or four
282children.
283In principle, the IR tree could be executed by doing an in-order traversal.
284<li>The IR tree is traversed in-order to emit code (see slang_emit.c).
285This is also when registers are allocated to store variables and temps.
286<li>In the future, a pattern-matching code generator-generator may be
287used for code generation.
288Programs such as L-BURG (Bottom-Up Rewrite Generator) and Twig look for
289patterns in IR trees, compute weights for subtrees and use the weights
290to select the best instructions to represent the sub-tree.
291<li>The emitted GPU instructions (see prog_instruction.h) are stored in a
292gl_program object (see mtypes.h).
293<li>When a fragment shader and vertex shader are linked (see slang_link.c)
294the varying vars are matched up, uniforms are merged, and vertex
295attributes are resolved (rewriting instructions as needed).
296</ul>
297
298<p>
299The final vertex and fragment programs may be interpreted in software
300(see prog_execute.c) or translated into a specific hardware architecture
301(see drivers/dri/i915/i915_fragprog.c for example).
302</p>
303
Brian8f9db0f2007-03-23 17:49:19 -0600304<h3>Code Generation Options</h3>
305
306<p>
307Internally, there are several options that control the compiler's code
308generation and instruction selection.
309These options are seen in the gl_shader_state struct and may be set
310by the device driver to indicate its preferences:
311
312<pre>
313struct gl_shader_state
314{
315 ...
316 /** Driver-selectable options: */
317 GLboolean EmitHighLevelInstructions;
318 GLboolean EmitCondCodes;
319 GLboolean EmitComments;
320};
321</pre>
322
323<ul>
324<li>EmitHighLevelInstructions
325<br>
326This option controls instruction selection for loops and conditionals.
327If the option is set high-level IF/ELSE/ENDIF, LOOP/ENDLOOP, CONT/BRK
328instructions will be emitted.
329Otherwise, those constructs will be implemented with BRA instructions.
330</li>
331
332<li>EmitCondCodes
333<br>
334If set, condition codes (ala GL_NV_fragment_program) will be used for
335branching and looping.
336Otherwise, ordinary registers will be used (the IF instruction will
337examine the first operand's X component and do the if-part if non-zero).
338This option is only relevant if EmitHighLevelInstructions is set.
339</li>
340
341<li>EmitComments
342<br>
343If set, instructions will be annoted with comments to help with debugging.
344Extra NOP instructions will also be inserted.
345</br>
346
347</ul>
Brian07e62082007-02-27 16:45:40 -0700348
349
Brian7eba12e2007-03-28 17:14:35 -0600350<a name="validation">
351<h2>Compiler Validation</h2>
352
353<p>
Brian Paulc4341fe2008-12-15 18:30:40 -0700354A <a href="http://glean.sf.net" target="_parent">Glean</a> test has
Brian7eba12e2007-03-28 17:14:35 -0600355been create to exercise the GLSL compiler.
356</p>
357<p>
Brian Paulc4341fe2008-12-15 18:30:40 -0700358The <em>glsl1</em> test runs over 170 sub-tests to check that the language
Brian7eba12e2007-03-28 17:14:35 -0600359features and built-in functions work properly.
360This test should be run frequently while working on the compiler to catch
361regressions.
362</p>
363<p>
364The test coverage is reasonably broad and complete but additional tests
365should be added.
366</p>
367
368
Brian9595d192007-01-20 13:40:57 -0700369</BODY>
370</HTML>