blob: 02f393cc9e5a4c6135b042fd36dd5877b5af3ab8 [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>
Brian Paul3bfedb72008-07-17 15:40:10 -060028<li><a href="#standalone">Stand-alone GLSL 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
Brianba3d3842007-04-19 14:24:29 -060047<li>The derivative functions such as dFdx() are not implemented
Brian7ff72a72007-04-27 15:23:19 -060048<li>The inverse trig functions asin(), acos(), and atan() are not implemented
Brian4b1d1b72007-04-27 15:25:00 -060049<li>The gl_Color and gl_SecondaryColor varying vars are interpolated
50 without perspective correction
Brian5ef3a2c2007-11-27 10:27:55 -070051<li>Floating point literal suffixes 'f' and 'F' aren't allowed.
Brian9595d192007-01-20 13:40:57 -070052</ul>
53
54<p>
55All other major features of the shading language should function.
56</p>
57
58
Brian07e62082007-02-27 16:45:40 -070059<a name="notes">
Brian9595d192007-01-20 13:40:57 -070060<h2>Implementation Notes</h2>
61
62<ul>
63<li>Shading language programs are compiled into low-level programs
64 very similar to those of GL_ARB_vertex/fragment_program.
Brianbbec2fd2007-01-28 12:11:10 -070065<li>All vector types (vec2, vec3, vec4, bvec2, etc) currently occupy full
Brian9595d192007-01-20 13:40:57 -070066 float[4] registers.
Brianbbec2fd2007-01-28 12:11:10 -070067<li>Float constants and variables are packed so that up to four floats
68 can occupy one program parameter/register.
Brian9595d192007-01-20 13:40:57 -070069<li>All function calls are inlined.
70<li>Shaders which use too many registers will not compile.
71<li>The quality of generated code is pretty good, register usage is fair.
72<li>Shader error detection and reporting of errors (InfoLog) is not
73 very good yet.
Brian05e6fd82007-03-27 16:05:25 -060074<li>The ftransform() function doesn't necessarily match the results of
75 fixed-function transformation.
Brian9595d192007-01-20 13:40:57 -070076</ul>
77
78<p>
79These issues will be addressed/resolved in the future.
80</p>
81
82
Brian61d31ae2007-02-17 09:41:19 -070083<a name="hints">
Brian9595d192007-01-20 13:40:57 -070084<h2>Programming Hints</h2>
85
86<ul>
Brianbbec2fd2007-01-28 12:11:10 -070087<li>Declare <em>in</em> function parameters as <em>const</em> whenever possible.
Brian9595d192007-01-20 13:40:57 -070088 This improves the efficiency of function inlining.
89</li>
90<br>
91<li>To reduce register usage, declare variables within smaller scopes.
92 For example, the following code:
93<pre>
94 void main()
95 {
96 vec4 a1, a2, b1, b2;
97 gl_Position = expression using a1, a2.
98 gl_Color = expression using b1, b2;
99 }
100</pre>
101 Can be rewritten as follows to use half as many registers:
102<pre>
103 void main()
104 {
105 {
106 vec4 a1, a2;
107 gl_Position = expression using a1, a2.
108 }
109 {
110 vec4 b1, b2;
111 gl_Color = expression using b1, b2;
112 }
113 }
114</pre>
115 Alternately, rather than using several float variables, use
116 a vec4 instead. Use swizzling and writemasks to access the
117 components of the vec4 as floats.
118</li>
119<br>
120<li>Use the built-in library functions whenever possible.
121 For example, instead of writing this:
122<pre>
123 float x = 1.0 / sqrt(y);
124</pre>
125 Write this:
126<pre>
127 float x = inversesqrt(y);
128</pre>
Brian7eba12e2007-03-28 17:14:35 -0600129<li>
130 Use ++i when possible as it's more efficient than i++
131</li>
Brian9595d192007-01-20 13:40:57 -0700132</ul>
133
134
Brian61d31ae2007-02-17 09:41:19 -0700135<a name="standalone">
Brian Paul3bfedb72008-07-17 15:40:10 -0600136<h2>Stand-alone GLSL Compiler</h2>
Brian61d31ae2007-02-17 09:41:19 -0700137
138<p>
139A unique stand-alone GLSL compiler driver has been added to Mesa.
140<p>
141
142<p>
143The stand-alone compiler (like a conventional command-line compiler)
144is a tool that accepts Shading Language programs and emits low-level
145GPU programs.
146</p>
147
148<p>
149This tool is useful for:
150<p>
151<ul>
152<li>Inspecting GPU code to gain insight into compilation
153<li>Generating initial GPU code for subsequent hand-tuning
154<li>Debugging the GLSL compiler itself
155</ul>
156
157<p>
Brian Paul77497eb2008-07-21 09:01:21 -0600158After building Mesa, the glslcompiler can be built by manually running:
Brian61d31ae2007-02-17 09:41:19 -0700159</p>
Brianff0cc922007-02-22 16:29:48 -0700160<pre>
Brian Paul3bfedb72008-07-17 15:40:10 -0600161 cd src/mesa/drivers/glslcompiler
Brianff0cc922007-02-22 16:29:48 -0700162 make
163</pre>
164
Brian61d31ae2007-02-17 09:41:19 -0700165
166<p>
167Here's an example of using the compiler to compile a vertex shader and
168emit GL_ARB_vertex_program-style instructions:
169</p>
170<pre>
Brian Paul3bfedb72008-07-17 15:40:10 -0600171 bin/glslcompiler --debug --numbers --fs progs/glsl/CH06-brick.frag.txt
Brian61d31ae2007-02-17 09:41:19 -0700172</pre>
173<p>
Brian Paul3bfedb72008-07-17 15:40:10 -0600174results in:
Brian61d31ae2007-02-17 09:41:19 -0700175</p>
176<pre>
Brian Paul3bfedb72008-07-17 15:40:10 -0600177# Fragment Program/Shader
178 0: RCP TEMP[4].x, UNIFORM[2].xxxx;
179 1: RCP TEMP[4].y, UNIFORM[2].yyyy;
180 2: MUL TEMP[3].xy, VARYING[0], TEMP[4];
181 3: MOV TEMP[1], TEMP[3];
182 4: MUL TEMP[0].w, TEMP[1].yyyy, CONST[4].xxxx;
183 5: FRC TEMP[1].z, TEMP[0].wwww;
184 6: SGT.C TEMP[0].w, TEMP[1].zzzz, CONST[4].xxxx;
185 7: IF (NE.wwww); # (if false, goto 9);
186 8: ADD TEMP[1].x, TEMP[1].xxxx, CONST[4].xxxx;
187 9: ENDIF;
188 10: FRC TEMP[1].xy, TEMP[1];
189 11: SGT TEMP[2].xy, UNIFORM[3], TEMP[1];
190 12: MUL TEMP[1].z, TEMP[2].xxxx, TEMP[2].yyyy;
191 13: LRP TEMP[0], TEMP[1].zzzz, UNIFORM[0], UNIFORM[1];
192 14: MUL TEMP[0].xyz, TEMP[0], VARYING[1].xxxx;
193 15: MOV OUTPUT[0].xyz, TEMP[0];
194 16: MOV OUTPUT[0].w, CONST[4].yyyy;
195 17: END
Brian61d31ae2007-02-17 09:41:19 -0700196</pre>
197
198<p>
199Note that some shading language constructs (such as uniform and varying
200variables) aren't expressible in ARB or NV-style programs.
201Therefore, the resulting output is not always legal by definition of
202those program languages.
203</p>
204<p>
205Also note that this compiler driver is still under development.
206Over time, the correctness of the GPU programs, with respect to the ARB
207and NV languagues, should improve.
208</p>
209
Brian07e62082007-02-27 16:45:40 -0700210
211
212<a name="implementation">
213<h2>Compiler Implementation</h2>
214
215<p>
216The source code for Mesa's shading language compiler is in the
217<code>src/mesa/shader/slang/</code> directory.
218</p>
219
220<p>
221The compiler follows a fairly standard design and basically works as follows:
222</p>
223<ul>
224<li>The input string is tokenized (see grammar.c) and parsed
225(see slang_compiler_*.c) to produce an Abstract Syntax Tree (AST).
226The nodes in this tree are slang_operation structures
227(see slang_compile_operation.h).
228The nodes are decorated with symbol table, scoping and datatype information.
229<li>The AST is converted into an Intermediate representation (IR) tree
230(see the slang_codegen.c file).
231The IR nodes represent basic GPU instructions, like add, dot product,
232move, etc.
233The IR tree is mostly a binary tree, but a few nodes have three or four
234children.
235In principle, the IR tree could be executed by doing an in-order traversal.
236<li>The IR tree is traversed in-order to emit code (see slang_emit.c).
237This is also when registers are allocated to store variables and temps.
238<li>In the future, a pattern-matching code generator-generator may be
239used for code generation.
240Programs such as L-BURG (Bottom-Up Rewrite Generator) and Twig look for
241patterns in IR trees, compute weights for subtrees and use the weights
242to select the best instructions to represent the sub-tree.
243<li>The emitted GPU instructions (see prog_instruction.h) are stored in a
244gl_program object (see mtypes.h).
245<li>When a fragment shader and vertex shader are linked (see slang_link.c)
246the varying vars are matched up, uniforms are merged, and vertex
247attributes are resolved (rewriting instructions as needed).
248</ul>
249
250<p>
251The final vertex and fragment programs may be interpreted in software
252(see prog_execute.c) or translated into a specific hardware architecture
253(see drivers/dri/i915/i915_fragprog.c for example).
254</p>
255
Brian8f9db0f2007-03-23 17:49:19 -0600256<h3>Code Generation Options</h3>
257
258<p>
259Internally, there are several options that control the compiler's code
260generation and instruction selection.
261These options are seen in the gl_shader_state struct and may be set
262by the device driver to indicate its preferences:
263
264<pre>
265struct gl_shader_state
266{
267 ...
268 /** Driver-selectable options: */
269 GLboolean EmitHighLevelInstructions;
270 GLboolean EmitCondCodes;
271 GLboolean EmitComments;
272};
273</pre>
274
275<ul>
276<li>EmitHighLevelInstructions
277<br>
278This option controls instruction selection for loops and conditionals.
279If the option is set high-level IF/ELSE/ENDIF, LOOP/ENDLOOP, CONT/BRK
280instructions will be emitted.
281Otherwise, those constructs will be implemented with BRA instructions.
282</li>
283
284<li>EmitCondCodes
285<br>
286If set, condition codes (ala GL_NV_fragment_program) will be used for
287branching and looping.
288Otherwise, ordinary registers will be used (the IF instruction will
289examine the first operand's X component and do the if-part if non-zero).
290This option is only relevant if EmitHighLevelInstructions is set.
291</li>
292
293<li>EmitComments
294<br>
295If set, instructions will be annoted with comments to help with debugging.
296Extra NOP instructions will also be inserted.
297</br>
298
299</ul>
Brian07e62082007-02-27 16:45:40 -0700300
301
Brian7eba12e2007-03-28 17:14:35 -0600302<a name="validation">
303<h2>Compiler Validation</h2>
304
305<p>
306A new <a href="http://glean.sf.net" target="_parent">Glean</a> test has
307been create to exercise the GLSL compiler.
308</p>
309<p>
Brian8598f552007-04-18 09:30:07 -0600310The <em>glsl1</em> test runs over 150 sub-tests to check that the language
Brian7eba12e2007-03-28 17:14:35 -0600311features and built-in functions work properly.
312This test should be run frequently while working on the compiler to catch
313regressions.
314</p>
315<p>
316The test coverage is reasonably broad and complete but additional tests
317should be added.
318</p>
319
320
Brian9595d192007-01-20 13:40:57 -0700321</BODY>
322</HTML>