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