info about how the compiler works
[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 17 Feb 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 </ul>
31
32
33 <a name="unsup">
34 <h2>Unsupported Features</h2>
35
36 <p>
37 The following features of the shading language are not yet supported
38 in Mesa:
39 </p>
40
41 <ul>
42 <li>Dereferencing arrays with non-constant indexes
43 <li>User-defined structs
44 <li>Linking of multiple shaders is not supported
45 <li>Integer operations are not fully implemented (most are implemented
46 as floating point).
47 </ul>
48
49 <p>
50 All other major features of the shading language should function.
51 </p>
52
53
54 <a name="notes">
55 <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.
60 <li>All vector types (vec2, vec3, vec4, bvec2, etc) currently occupy full
61 float[4] registers.
62 <li>Float constants and variables are packed so that up to four floats
63 can occupy one program parameter/register.
64 <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>
73 These issues will be addressed/resolved in the future.
74 </p>
75
76
77 <a name="hints">
78 <h2>Programming Hints</h2>
79
80 <ul>
81 <li>Declare <em>in</em> function parameters as <em>const</em> whenever possible.
82 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
126 <a name="standalone">
127 <h2>Stand-alone Compiler</h2>
128
129 <p>
130 A unique stand-alone GLSL compiler driver has been added to Mesa.
131 <p>
132
133 <p>
134 The stand-alone compiler (like a conventional command-line compiler)
135 is a tool that accepts Shading Language programs and emits low-level
136 GPU programs.
137 </p>
138
139 <p>
140 This 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>
149 To build the glslcompiler program (this will be improved someday):
150 </p>
151 <pre>
152 cd src/mesa
153 make libmesa.a
154 cd drivers/glslcompiler
155 make
156 </pre>
157
158
159 <p>
160 Here's an example of using the compiler to compile a vertex shader and
161 emit GL_ARB_vertex_program-style instructions:
162 </p>
163 <pre>
164 glslcompiler --arb --linenumbers --vs vertshader.txt
165 </pre>
166 <p>
167 The 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>
181 Note that some shading language constructs (such as uniform and varying
182 variables) aren't expressible in ARB or NV-style programs.
183 Therefore, the resulting output is not always legal by definition of
184 those program languages.
185 </p>
186 <p>
187 Also note that this compiler driver is still under development.
188 Over time, the correctness of the GPU programs, with respect to the ARB
189 and NV languagues, should improve.
190 </p>
191
192
193
194 <a name="implementation">
195 <h2>Compiler Implementation</h2>
196
197 <p>
198 The source code for Mesa's shading language compiler is in the
199 <code>src/mesa/shader/slang/</code> directory.
200 </p>
201
202 <p>
203 The 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).
208 The nodes in this tree are slang_operation structures
209 (see slang_compile_operation.h).
210 The 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).
213 The IR nodes represent basic GPU instructions, like add, dot product,
214 move, etc.
215 The IR tree is mostly a binary tree, but a few nodes have three or four
216 children.
217 In 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).
219 This is also when registers are allocated to store variables and temps.
220 <li>In the future, a pattern-matching code generator-generator may be
221 used for code generation.
222 Programs such as L-BURG (Bottom-Up Rewrite Generator) and Twig look for
223 patterns in IR trees, compute weights for subtrees and use the weights
224 to select the best instructions to represent the sub-tree.
225 <li>The emitted GPU instructions (see prog_instruction.h) are stored in a
226 gl_program object (see mtypes.h).
227 <li>When a fragment shader and vertex shader are linked (see slang_link.c)
228 the varying vars are matched up, uniforms are merged, and vertex
229 attributes are resolved (rewriting instructions as needed).
230 </ul>
231
232 <p>
233 The 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
240 </BODY>
241 </HTML>