blob: 412c89389e51ed0d25bf8f7171575e88d0aca9c5 [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>
26<li><a href="#impl">Implementation Notes</a>
27<li><a href="#hints">Programming Hints</a>
28<li><a href="#standalone">Stand-alone Compiler</a>
29</ul>
30
31
32<a name="unsup">
Brian9595d192007-01-20 13:40:57 -070033<h2>Unsupported Features</h2>
34
35<p>
36The following features of the shading language are not yet supported
37in Mesa:
38</p>
39
40<ul>
Brian6d4cf6b2007-02-21 16:07:03 -070041<li>Dereferencing arrays with non-constant indexes
42<li>User-defined structs
Brian9595d192007-01-20 13:40:57 -070043<li>Linking of multiple shaders is not supported
Brian9595d192007-01-20 13:40:57 -070044<li>Integer operations are not fully implemented (most are implemented
45 as floating point).
46</ul>
47
48<p>
49All other major features of the shading language should function.
50</p>
51
52
Brian61d31ae2007-02-17 09:41:19 -070053<a name="impl">
Brian9595d192007-01-20 13:40:57 -070054<h2>Implementation Notes</h2>
55
56<ul>
57<li>Shading language programs are compiled into low-level programs
58 very similar to those of GL_ARB_vertex/fragment_program.
Brianbbec2fd2007-01-28 12:11:10 -070059<li>All vector types (vec2, vec3, vec4, bvec2, etc) currently occupy full
Brian9595d192007-01-20 13:40:57 -070060 float[4] registers.
Brianbbec2fd2007-01-28 12:11:10 -070061<li>Float constants and variables are packed so that up to four floats
62 can occupy one program parameter/register.
Brian9595d192007-01-20 13:40:57 -070063<li>All function calls are inlined.
64<li>Shaders which use too many registers will not compile.
65<li>The quality of generated code is pretty good, register usage is fair.
66<li>Shader error detection and reporting of errors (InfoLog) is not
67 very good yet.
68<li>There are massive memory leaks in the compiler.
69</ul>
70
71<p>
72These issues will be addressed/resolved in the future.
73</p>
74
75
Brian61d31ae2007-02-17 09:41:19 -070076<a name="hints">
Brian9595d192007-01-20 13:40:57 -070077<h2>Programming Hints</h2>
78
79<ul>
Brianbbec2fd2007-01-28 12:11:10 -070080<li>Declare <em>in</em> function parameters as <em>const</em> whenever possible.
Brian9595d192007-01-20 13:40:57 -070081 This improves the efficiency of function inlining.
82</li>
83<br>
84<li>To reduce register usage, declare variables within smaller scopes.
85 For example, the following code:
86<pre>
87 void main()
88 {
89 vec4 a1, a2, b1, b2;
90 gl_Position = expression using a1, a2.
91 gl_Color = expression using b1, b2;
92 }
93</pre>
94 Can be rewritten as follows to use half as many registers:
95<pre>
96 void main()
97 {
98 {
99 vec4 a1, a2;
100 gl_Position = expression using a1, a2.
101 }
102 {
103 vec4 b1, b2;
104 gl_Color = expression using b1, b2;
105 }
106 }
107</pre>
108 Alternately, rather than using several float variables, use
109 a vec4 instead. Use swizzling and writemasks to access the
110 components of the vec4 as floats.
111</li>
112<br>
113<li>Use the built-in library functions whenever possible.
114 For example, instead of writing this:
115<pre>
116 float x = 1.0 / sqrt(y);
117</pre>
118 Write this:
119<pre>
120 float x = inversesqrt(y);
121</pre>
122</ul>
123
124
Brian61d31ae2007-02-17 09:41:19 -0700125<a name="standalone">
126<h2>Stand-alone Compiler</h2>
127
128<p>
129A unique stand-alone GLSL compiler driver has been added to Mesa.
130<p>
131
132<p>
133The stand-alone compiler (like a conventional command-line compiler)
134is a tool that accepts Shading Language programs and emits low-level
135GPU programs.
136</p>
137
138<p>
139This tool is useful for:
140<p>
141<ul>
142<li>Inspecting GPU code to gain insight into compilation
143<li>Generating initial GPU code for subsequent hand-tuning
144<li>Debugging the GLSL compiler itself
145</ul>
146
147<p>
Brianff0cc922007-02-22 16:29:48 -0700148To build the glslcompiler program (this will be improved someday):
Brian61d31ae2007-02-17 09:41:19 -0700149</p>
Brianff0cc922007-02-22 16:29:48 -0700150<pre>
151 cd src/mesa
152 make libmesa.a
153 cd drivers/glslcompiler
154 make
155</pre>
156
Brian61d31ae2007-02-17 09:41:19 -0700157
158<p>
159Here's an example of using the compiler to compile a vertex shader and
160emit GL_ARB_vertex_program-style instructions:
161</p>
162<pre>
163 glslcompiler --arb --linenumbers --vs vertshader.txt
164</pre>
165<p>
166The output may look similar to this:
167</p>
168<pre>
169!!ARBvp1.0
170 0: MOV result.texcoord[0], vertex.texcoord[0];
171 1: DP4 temp0.x, state.matrix.mvp.row[0], vertex.position;
172 2: DP4 temp0.y, state.matrix.mvp.row[1], vertex.position;
173 3: DP4 temp0.z, state.matrix.mvp.row[2], vertex.position;
174 4: DP4 temp0.w, state.matrix.mvp.row[3], vertex.position;
175 5: MOV result.position, temp0;
176 6: END
177</pre>
178
179<p>
180Note that some shading language constructs (such as uniform and varying
181variables) aren't expressible in ARB or NV-style programs.
182Therefore, the resulting output is not always legal by definition of
183those program languages.
184</p>
185<p>
186Also note that this compiler driver is still under development.
187Over time, the correctness of the GPU programs, with respect to the ARB
188and NV languagues, should improve.
189</p>
190
Brian9595d192007-01-20 13:40:57 -0700191</BODY>
192</HTML>