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