docs: add new iframe layout
[mesa.git] / docs / shading.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2 <html lang="en">
3 <head>
4 <meta http-equiv="content-type" content="text/html; charset=utf-8">
5 <title>Shading Language Support</title>
6 <link rel="stylesheet" type="text/css" href="mesa.css">
7 </head>
8 <body>
9
10 <div class="header">
11 <h1>The Mesa 3D Graphics Library</h1>
12 </div>
13
14 <iframe src="contents.html"></iframe>
15 <div class="content">
16
17 <h1>Shading Language Support</h1>
18
19 <p>
20 This page describes the features and status of Mesa's support for the
21 <a href="http://opengl.org/documentation/glsl/" target="_parent">
22 OpenGL Shading Language</a>.
23 </p>
24
25 <p>
26 Contents
27 </p>
28 <ul>
29 <li><a href="#envvars">Environment variables</a>
30 <li><a href="#glsl120">GLSL 1.20 support</a>
31 <li><a href="#unsup">Unsupported Features</a>
32 <li><a href="#notes">Implementation Notes</a>
33 <li><a href="#hints">Programming Hints</a>
34 <li><a href="#standalone">Stand-alone GLSL Compiler</a>
35 <li><a href="#implementation">Compiler Implementation</a>
36 <li><a href="#validation">Compiler Validation</a>
37 </ul>
38
39
40 <h2 id="envvars">Environment Variables</h2>
41
42 <p>
43 The <b>MESA_GLSL</b> environment variable can be set to a comma-separated
44 list of keywords to control some aspects of the GLSL compiler and shader
45 execution. These are generally used for debugging.
46 </p>
47 <ul>
48 <li><b>dump</b> - print GLSL shader code to stdout at link time
49 <li><b>log</b> - log all GLSL shaders to files.
50 The filenames will be "shader_X.vert" or "shader_X.frag" where X
51 the shader ID.
52 <li><b>nopt</b> - disable compiler optimizations
53 <li><b>opt</b> - force compiler optimizations
54 <li><b>uniform</b> - print message to stdout when glUniform is called
55 <li><b>nopvert</b> - force vertex shaders to be a simple shader that just transforms
56 the vertex position with ftransform() and passes through the color and
57 texcoord[0] attributes.
58 <li><b>nopfrag</b> - force fragment shader to be a simple shader that passes
59 through the color attribute.
60 <li><b>useprog</b> - log glUseProgram calls to stderr
61 </ul>
62 <p>
63 Example: export MESA_GLSL=dump,nopt
64 </p>
65
66
67 <h2 id="glsl120">GLSL Version</h2>
68
69 <p>
70 The GLSL compiler currently supports version 1.20 of the shading language.
71 </p>
72
73 <p>
74 Several GLSL extensions are also supported:
75 </p>
76 <ul>
77 <li>GL_ARB_draw_buffers
78 <li>GL_ARB_texture_rectangle
79 <li>GL_ARB_fragment_coord_conventions
80 <li>GL_EXT_texture_array
81 </ul>
82
83
84 <h2 id="unsup">Unsupported Features</h2>
85
86 <p>XXX update this section</p>
87
88 <p>
89 The following features of the shading language are not yet fully supported
90 in Mesa:
91 </p>
92
93 <ul>
94 <li>Linking of multiple shaders does not always work. Currently, linking
95 is implemented through shader concatenation and re-compiling. This
96 doesn't always work because of some #pragma and preprocessor issues.
97 <li>gl_ClipVertex
98 <li>The gl_Color and gl_SecondaryColor varying vars are interpolated
99 without perspective correction
100 </ul>
101
102 <p>
103 All other major features of the shading language should function.
104 </p>
105
106
107 <h2 id="notes">Implementation Notes</h2>
108
109 <ul>
110 <li>Shading language programs are compiled into low-level programs
111 very similar to those of GL_ARB_vertex/fragment_program.
112 <li>All vector types (vec2, vec3, vec4, bvec2, etc) currently occupy full
113 float[4] registers.
114 <li>Float constants and variables are packed so that up to four floats
115 can occupy one program parameter/register.
116 <li>All function calls are inlined.
117 <li>Shaders which use too many registers will not compile.
118 <li>The quality of generated code is pretty good, register usage is fair.
119 <li>Shader error detection and reporting of errors (InfoLog) is not
120 very good yet.
121 <li>The ftransform() function doesn't necessarily match the results of
122 fixed-function transformation.
123 </ul>
124
125 <p>
126 These issues will be addressed/resolved in the future.
127 </p>
128
129
130 <h2 id="hints">Programming Hints</h2>
131
132 <ul>
133 <li>Use the built-in library functions whenever possible.
134 For example, instead of writing this:
135 <pre>
136 float x = 1.0 / sqrt(y);
137 </pre>
138 Write this:
139 <pre>
140 float x = inversesqrt(y);
141 </pre>
142 </li>
143 </ul>
144
145
146 <h2 id="standalone">Stand-alone GLSL Compiler</h2>
147
148 <p>
149 The stand-alone GLSL compiler program can be used to compile GLSL shaders
150 into low-level GPU code.
151 </p>
152
153 <p>
154 This tool is useful for:
155 </p>
156 <ul>
157 <li>Inspecting GPU code to gain insight into compilation
158 <li>Generating initial GPU code for subsequent hand-tuning
159 <li>Debugging the GLSL compiler itself
160 </ul>
161
162 <p>
163 After building Mesa, the compiler can be found at src/glsl/glsl_compiler
164 </p>
165
166 <p>
167 Here's an example of using the compiler to compile a vertex shader and
168 emit GL_ARB_vertex_program-style instructions:
169 </p>
170 <pre>
171 src/glsl/glsl_compiler --dump-ast myshader.vert
172 </pre>
173
174 Options include
175 <ul>
176 <li><b>--dump-ast</b> - dump GPU code
177 <li><b>--dump-hir</b> - dump high-level IR code
178 <li><b>--dump-lir</b> - dump low-level IR code
179 <li><b>--link</b> - ???
180 </ul>
181
182
183 <h2 id="implementation">Compiler Implementation</h2>
184
185 <p>
186 The source code for Mesa's shading language compiler is in the
187 <code>src/glsl/</code> directory.
188 </p>
189
190 <p>
191 XXX provide some info about the compiler....
192 </p>
193
194 <p>
195 The final vertex and fragment programs may be interpreted in software
196 (see prog_execute.c) or translated into a specific hardware architecture
197 (see drivers/dri/i915/i915_fragprog.c for example).
198 </p>
199
200 <h3>Code Generation Options</h3>
201
202 <p>
203 Internally, there are several options that control the compiler's code
204 generation and instruction selection.
205 These options are seen in the gl_shader_state struct and may be set
206 by the device driver to indicate its preferences:
207
208 <pre>
209 struct gl_shader_state
210 {
211 ...
212 /** Driver-selectable options: */
213 GLboolean EmitHighLevelInstructions;
214 GLboolean EmitCondCodes;
215 GLboolean EmitComments;
216 };
217 </pre>
218
219 <dl>
220 <dt>EmitHighLevelInstructions</dt>
221 <dd>
222 This option controls instruction selection for loops and conditionals.
223 If the option is set high-level IF/ELSE/ENDIF, LOOP/ENDLOOP, CONT/BRK
224 instructions will be emitted.
225 Otherwise, those constructs will be implemented with BRA instructions.
226 </dd>
227
228 <dt>EmitCondCodes</dt>
229 <dd>
230 If set, condition codes (ala GL_NV_fragment_program) will be used for
231 branching and looping.
232 Otherwise, ordinary registers will be used (the IF instruction will
233 examine the first operand's X component and do the if-part if non-zero).
234 This option is only relevant if EmitHighLevelInstructions is set.
235 </dd>
236
237 <dt>EmitComments</dt>
238 <dd>
239 If set, instructions will be annoted with comments to help with debugging.
240 Extra NOP instructions will also be inserted.
241 </dd>
242 </dl>
243
244
245 <h2 id="validation">Compiler Validation</h2>
246
247 <p>
248 Developers working on the GLSL compiler should test frequently to avoid
249 regressions.
250 </p>
251
252 <p>
253 The <a href="http://piglit.freedesktop.org/" target="_parent">Piglit</a> project
254 has many GLSL tests and the
255 <a href="http://glean.sf.net" target="_parent">Glean</a> glsl1 test
256 tests GLSL features.
257 </p>
258
259 <p>
260 The Mesa demos repository also has some good GLSL tests.
261 </p>
262
263 </div>
264 </body>
265 </html>