blob: fd96a41e85a281fdd7e26da33e16b113b239854d [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>
Brian7eba12e2007-03-28 17:14:35 -060018Last updated on 28 March 2007.
Brian9595d192007-01-20 13:40:57 -070019</p>
20
Brian61d31ae2007-02-17 09:41:19 -070021<p>
22Contents
23</p>
24<ul>
25<li><a href="#unsup">Unsupported Features</a>
Brian07e62082007-02-27 16:45:40 -070026<li><a href="#notes">Implementation Notes</a>
Brian61d31ae2007-02-17 09:41:19 -070027<li><a href="#hints">Programming Hints</a>
28<li><a href="#standalone">Stand-alone Compiler</a>
Brian07e62082007-02-27 16:45:40 -070029<li><a href="#implementation">Compiler Implementation</a>
Brian7eba12e2007-03-28 17:14:35 -060030<li><a href="#validation">Compiler Validation</a>
Brian61d31ae2007-02-17 09:41:19 -070031</ul>
32
33
34<a name="unsup">
Brian9595d192007-01-20 13:40:57 -070035<h2>Unsupported Features</h2>
36
37<p>
38The following features of the shading language are not yet supported
39in Mesa:
40</p>
41
42<ul>
Brian6d4cf6b2007-02-21 16:07:03 -070043<li>Dereferencing arrays with non-constant indexes
Brianb67d9312007-03-26 10:23:50 -060044<li>Comparison of user-defined structs
Brian9595d192007-01-20 13:40:57 -070045<li>Linking of multiple shaders is not supported
Brianb03e1712007-03-09 09:51:55 -070046<li>gl_ClipVertex
Brian9595d192007-01-20 13:40:57 -070047</ul>
48
49<p>
50All other major features of the shading language should function.
51</p>
52
53
Brian07e62082007-02-27 16:45:40 -070054<a name="notes">
Brian9595d192007-01-20 13:40:57 -070055<h2>Implementation Notes</h2>
56
57<ul>
58<li>Shading language programs are compiled into low-level programs
59 very similar to those of GL_ARB_vertex/fragment_program.
Brianbbec2fd2007-01-28 12:11:10 -070060<li>All vector types (vec2, vec3, vec4, bvec2, etc) currently occupy full
Brian9595d192007-01-20 13:40:57 -070061 float[4] registers.
Brianbbec2fd2007-01-28 12:11:10 -070062<li>Float constants and variables are packed so that up to four floats
63 can occupy one program parameter/register.
Brian9595d192007-01-20 13:40:57 -070064<li>All function calls are inlined.
65<li>Shaders which use too many registers will not compile.
66<li>The quality of generated code is pretty good, register usage is fair.
67<li>Shader error detection and reporting of errors (InfoLog) is not
68 very good yet.
Brian8f9db0f2007-03-23 17:49:19 -060069<li>There are known memory leaks in the compiler.
Brian05e6fd82007-03-27 16:05:25 -060070<li>The ftransform() function doesn't necessarily match the results of
71 fixed-function transformation.
Brian9595d192007-01-20 13:40:57 -070072</ul>
73
74<p>
75These issues will be addressed/resolved in the future.
76</p>
77
78
Brian61d31ae2007-02-17 09:41:19 -070079<a name="hints">
Brian9595d192007-01-20 13:40:57 -070080<h2>Programming Hints</h2>
81
82<ul>
Brianbbec2fd2007-01-28 12:11:10 -070083<li>Declare <em>in</em> function parameters as <em>const</em> whenever possible.
Brian9595d192007-01-20 13:40:57 -070084 This improves the efficiency of function inlining.
85</li>
86<br>
87<li>To reduce register usage, declare variables within smaller scopes.
88 For example, the following code:
89<pre>
90 void main()
91 {
92 vec4 a1, a2, b1, b2;
93 gl_Position = expression using a1, a2.
94 gl_Color = expression using b1, b2;
95 }
96</pre>
97 Can be rewritten as follows to use half as many registers:
98<pre>
99 void main()
100 {
101 {
102 vec4 a1, a2;
103 gl_Position = expression using a1, a2.
104 }
105 {
106 vec4 b1, b2;
107 gl_Color = expression using b1, b2;
108 }
109 }
110</pre>
111 Alternately, rather than using several float variables, use
112 a vec4 instead. Use swizzling and writemasks to access the
113 components of the vec4 as floats.
114</li>
115<br>
116<li>Use the built-in library functions whenever possible.
117 For example, instead of writing this:
118<pre>
119 float x = 1.0 / sqrt(y);
120</pre>
121 Write this:
122<pre>
123 float x = inversesqrt(y);
124</pre>
Brian7eba12e2007-03-28 17:14:35 -0600125<li>
126 Use ++i when possible as it's more efficient than i++
127</li>
Brian9595d192007-01-20 13:40:57 -0700128</ul>
129
130
Brian61d31ae2007-02-17 09:41:19 -0700131<a name="standalone">
132<h2>Stand-alone Compiler</h2>
133
134<p>
135A unique stand-alone GLSL compiler driver has been added to Mesa.
136<p>
137
138<p>
139The stand-alone compiler (like a conventional command-line compiler)
140is a tool that accepts Shading Language programs and emits low-level
141GPU programs.
142</p>
143
144<p>
145This tool is useful for:
146<p>
147<ul>
148<li>Inspecting GPU code to gain insight into compilation
149<li>Generating initial GPU code for subsequent hand-tuning
150<li>Debugging the GLSL compiler itself
151</ul>
152
153<p>
Brianff0cc922007-02-22 16:29:48 -0700154To build the glslcompiler program (this will be improved someday):
Brian61d31ae2007-02-17 09:41:19 -0700155</p>
Brianff0cc922007-02-22 16:29:48 -0700156<pre>
157 cd src/mesa
158 make libmesa.a
159 cd drivers/glslcompiler
160 make
161</pre>
162
Brian61d31ae2007-02-17 09:41:19 -0700163
164<p>
165Here's an example of using the compiler to compile a vertex shader and
166emit GL_ARB_vertex_program-style instructions:
167</p>
168<pre>
169 glslcompiler --arb --linenumbers --vs vertshader.txt
170</pre>
171<p>
172The output may look similar to this:
173</p>
174<pre>
175!!ARBvp1.0
176 0: MOV result.texcoord[0], vertex.texcoord[0];
177 1: DP4 temp0.x, state.matrix.mvp.row[0], vertex.position;
178 2: DP4 temp0.y, state.matrix.mvp.row[1], vertex.position;
179 3: DP4 temp0.z, state.matrix.mvp.row[2], vertex.position;
180 4: DP4 temp0.w, state.matrix.mvp.row[3], vertex.position;
181 5: MOV result.position, temp0;
182 6: END
183</pre>
184
185<p>
186Note that some shading language constructs (such as uniform and varying
187variables) aren't expressible in ARB or NV-style programs.
188Therefore, the resulting output is not always legal by definition of
189those program languages.
190</p>
191<p>
192Also note that this compiler driver is still under development.
193Over time, the correctness of the GPU programs, with respect to the ARB
194and NV languagues, should improve.
195</p>
196
Brian07e62082007-02-27 16:45:40 -0700197
198
199<a name="implementation">
200<h2>Compiler Implementation</h2>
201
202<p>
203The source code for Mesa's shading language compiler is in the
204<code>src/mesa/shader/slang/</code> directory.
205</p>
206
207<p>
208The compiler follows a fairly standard design and basically works as follows:
209</p>
210<ul>
211<li>The input string is tokenized (see grammar.c) and parsed
212(see slang_compiler_*.c) to produce an Abstract Syntax Tree (AST).
213The nodes in this tree are slang_operation structures
214(see slang_compile_operation.h).
215The nodes are decorated with symbol table, scoping and datatype information.
216<li>The AST is converted into an Intermediate representation (IR) tree
217(see the slang_codegen.c file).
218The IR nodes represent basic GPU instructions, like add, dot product,
219move, etc.
220The IR tree is mostly a binary tree, but a few nodes have three or four
221children.
222In principle, the IR tree could be executed by doing an in-order traversal.
223<li>The IR tree is traversed in-order to emit code (see slang_emit.c).
224This is also when registers are allocated to store variables and temps.
225<li>In the future, a pattern-matching code generator-generator may be
226used for code generation.
227Programs such as L-BURG (Bottom-Up Rewrite Generator) and Twig look for
228patterns in IR trees, compute weights for subtrees and use the weights
229to select the best instructions to represent the sub-tree.
230<li>The emitted GPU instructions (see prog_instruction.h) are stored in a
231gl_program object (see mtypes.h).
232<li>When a fragment shader and vertex shader are linked (see slang_link.c)
233the varying vars are matched up, uniforms are merged, and vertex
234attributes are resolved (rewriting instructions as needed).
235</ul>
236
237<p>
238The final vertex and fragment programs may be interpreted in software
239(see prog_execute.c) or translated into a specific hardware architecture
240(see drivers/dri/i915/i915_fragprog.c for example).
241</p>
242
Brian8f9db0f2007-03-23 17:49:19 -0600243<h3>Code Generation Options</h3>
244
245<p>
246Internally, there are several options that control the compiler's code
247generation and instruction selection.
248These options are seen in the gl_shader_state struct and may be set
249by the device driver to indicate its preferences:
250
251<pre>
252struct gl_shader_state
253{
254 ...
255 /** Driver-selectable options: */
256 GLboolean EmitHighLevelInstructions;
257 GLboolean EmitCondCodes;
258 GLboolean EmitComments;
259};
260</pre>
261
262<ul>
263<li>EmitHighLevelInstructions
264<br>
265This option controls instruction selection for loops and conditionals.
266If the option is set high-level IF/ELSE/ENDIF, LOOP/ENDLOOP, CONT/BRK
267instructions will be emitted.
268Otherwise, those constructs will be implemented with BRA instructions.
269</li>
270
271<li>EmitCondCodes
272<br>
273If set, condition codes (ala GL_NV_fragment_program) will be used for
274branching and looping.
275Otherwise, ordinary registers will be used (the IF instruction will
276examine the first operand's X component and do the if-part if non-zero).
277This option is only relevant if EmitHighLevelInstructions is set.
278</li>
279
280<li>EmitComments
281<br>
282If set, instructions will be annoted with comments to help with debugging.
283Extra NOP instructions will also be inserted.
284</br>
285
286</ul>
Brian07e62082007-02-27 16:45:40 -0700287
288
Brian7eba12e2007-03-28 17:14:35 -0600289<a name="validation">
290<h2>Compiler Validation</h2>
291
292<p>
293A new <a href="http://glean.sf.net" target="_parent">Glean</a> test has
294been create to exercise the GLSL compiler.
295</p>
296<p>
297The <em>glsl1</em> test runs over 130 sub-tests to check that the language
298features and built-in functions work properly.
299This test should be run frequently while working on the compiler to catch
300regressions.
301</p>
302<p>
303The test coverage is reasonably broad and complete but additional tests
304should be added.
305</p>
306
307
Brian9595d192007-01-20 13:40:57 -0700308</BODY>
309</HTML>