blob: 48ae15f94083303753d9e63bfbdca60e0d86a989 [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 -070018Last updated on 17 Feb 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>
Brian61d31ae2007-02-17 09:41:19 -070030</ul>
31
32
33<a name="unsup">
Brian9595d192007-01-20 13:40:57 -070034<h2>Unsupported Features</h2>
35
36<p>
37The following features of the shading language are not yet supported
38in Mesa:
39</p>
40
41<ul>
Brian6d4cf6b2007-02-21 16:07:03 -070042<li>Dereferencing arrays with non-constant indexes
43<li>User-defined structs
Brian9595d192007-01-20 13:40:57 -070044<li>Linking of multiple shaders is not supported
Brian9595d192007-01-20 13:40:57 -070045<li>Integer operations are not fully implemented (most are implemented
46 as floating point).
47</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.
69<li>There are massive memory leaks in the compiler.
70</ul>
71
72<p>
73These issues will be addressed/resolved in the future.
74</p>
75
76
Brian61d31ae2007-02-17 09:41:19 -070077<a name="hints">
Brian9595d192007-01-20 13:40:57 -070078<h2>Programming Hints</h2>
79
80<ul>
Brianbbec2fd2007-01-28 12:11:10 -070081<li>Declare <em>in</em> function parameters as <em>const</em> whenever possible.
Brian9595d192007-01-20 13:40:57 -070082 This improves the efficiency of function inlining.
83</li>
84<br>
85<li>To reduce register usage, declare variables within smaller scopes.
86 For example, the following code:
87<pre>
88 void main()
89 {
90 vec4 a1, a2, b1, b2;
91 gl_Position = expression using a1, a2.
92 gl_Color = expression using b1, b2;
93 }
94</pre>
95 Can be rewritten as follows to use half as many registers:
96<pre>
97 void main()
98 {
99 {
100 vec4 a1, a2;
101 gl_Position = expression using a1, a2.
102 }
103 {
104 vec4 b1, b2;
105 gl_Color = expression using b1, b2;
106 }
107 }
108</pre>
109 Alternately, rather than using several float variables, use
110 a vec4 instead. Use swizzling and writemasks to access the
111 components of the vec4 as floats.
112</li>
113<br>
114<li>Use the built-in library functions whenever possible.
115 For example, instead of writing this:
116<pre>
117 float x = 1.0 / sqrt(y);
118</pre>
119 Write this:
120<pre>
121 float x = inversesqrt(y);
122</pre>
123</ul>
124
125
Brian61d31ae2007-02-17 09:41:19 -0700126<a name="standalone">
127<h2>Stand-alone Compiler</h2>
128
129<p>
130A unique stand-alone GLSL compiler driver has been added to Mesa.
131<p>
132
133<p>
134The stand-alone compiler (like a conventional command-line compiler)
135is a tool that accepts Shading Language programs and emits low-level
136GPU programs.
137</p>
138
139<p>
140This tool is useful for:
141<p>
142<ul>
143<li>Inspecting GPU code to gain insight into compilation
144<li>Generating initial GPU code for subsequent hand-tuning
145<li>Debugging the GLSL compiler itself
146</ul>
147
148<p>
Brianff0cc922007-02-22 16:29:48 -0700149To build the glslcompiler program (this will be improved someday):
Brian61d31ae2007-02-17 09:41:19 -0700150</p>
Brianff0cc922007-02-22 16:29:48 -0700151<pre>
152 cd src/mesa
153 make libmesa.a
154 cd drivers/glslcompiler
155 make
156</pre>
157
Brian61d31ae2007-02-17 09:41:19 -0700158
159<p>
160Here's an example of using the compiler to compile a vertex shader and
161emit GL_ARB_vertex_program-style instructions:
162</p>
163<pre>
164 glslcompiler --arb --linenumbers --vs vertshader.txt
165</pre>
166<p>
167The output may look similar to this:
168</p>
169<pre>
170!!ARBvp1.0
171 0: MOV result.texcoord[0], vertex.texcoord[0];
172 1: DP4 temp0.x, state.matrix.mvp.row[0], vertex.position;
173 2: DP4 temp0.y, state.matrix.mvp.row[1], vertex.position;
174 3: DP4 temp0.z, state.matrix.mvp.row[2], vertex.position;
175 4: DP4 temp0.w, state.matrix.mvp.row[3], vertex.position;
176 5: MOV result.position, temp0;
177 6: END
178</pre>
179
180<p>
181Note that some shading language constructs (such as uniform and varying
182variables) aren't expressible in ARB or NV-style programs.
183Therefore, the resulting output is not always legal by definition of
184those program languages.
185</p>
186<p>
187Also note that this compiler driver is still under development.
188Over time, the correctness of the GPU programs, with respect to the ARB
189and NV languagues, should improve.
190</p>
191
Brian07e62082007-02-27 16:45:40 -0700192
193
194<a name="implementation">
195<h2>Compiler Implementation</h2>
196
197<p>
198The source code for Mesa's shading language compiler is in the
199<code>src/mesa/shader/slang/</code> directory.
200</p>
201
202<p>
203The compiler follows a fairly standard design and basically works as follows:
204</p>
205<ul>
206<li>The input string is tokenized (see grammar.c) and parsed
207(see slang_compiler_*.c) to produce an Abstract Syntax Tree (AST).
208The nodes in this tree are slang_operation structures
209(see slang_compile_operation.h).
210The nodes are decorated with symbol table, scoping and datatype information.
211<li>The AST is converted into an Intermediate representation (IR) tree
212(see the slang_codegen.c file).
213The IR nodes represent basic GPU instructions, like add, dot product,
214move, etc.
215The IR tree is mostly a binary tree, but a few nodes have three or four
216children.
217In principle, the IR tree could be executed by doing an in-order traversal.
218<li>The IR tree is traversed in-order to emit code (see slang_emit.c).
219This is also when registers are allocated to store variables and temps.
220<li>In the future, a pattern-matching code generator-generator may be
221used for code generation.
222Programs such as L-BURG (Bottom-Up Rewrite Generator) and Twig look for
223patterns in IR trees, compute weights for subtrees and use the weights
224to select the best instructions to represent the sub-tree.
225<li>The emitted GPU instructions (see prog_instruction.h) are stored in a
226gl_program object (see mtypes.h).
227<li>When a fragment shader and vertex shader are linked (see slang_link.c)
228the varying vars are matched up, uniforms are merged, and vertex
229attributes are resolved (rewriting instructions as needed).
230</ul>
231
232<p>
233The final vertex and fragment programs may be interpreted in software
234(see prog_execute.c) or translated into a specific hardware architecture
235(see drivers/dri/i915/i915_fragprog.c for example).
236</p>
237
238
239
Brian9595d192007-01-20 13:40:57 -0700240</BODY>
241</HTML>