docs: consolidate html header and footer
[mesa.git] / docs / dispatch.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>GL Dispatch in Mesa</title>
6 <link rel="stylesheet" type="text/css" href="mesa.css">
7 </head>
8 <body>
9 <h1>GL Dispatch in Mesa</h1>
10
11 <p>Several factors combine to make efficient dispatch of OpenGL functions
12 fairly complicated. This document attempts to explain some of the issues
13 and introduce the reader to Mesa's implementation. Readers already familiar
14 with the issues around GL dispatch can safely skip ahead to the <A
15 HREF="#overview">overview of Mesa's implementation</A>.</p>
16
17 <H2>1. Complexity of GL Dispatch</H2>
18
19 <p>Every GL application has at least one object called a GL <em>context</em>.
20 This object, which is an implicit parameter to ever GL function, stores all
21 of the GL related state for the application. Every texture, every buffer
22 object, every enable, and much, much more is stored in the context. Since
23 an application can have more than one context, the context to be used is
24 selected by a window-system dependent function such as
25 <tt>glXMakeContextCurrent</tt>.</p>
26
27 <p>In environments that implement OpenGL with X-Windows using GLX, every GL
28 function, including the pointers returned by <tt>glXGetProcAddress</tt>, are
29 <em>context independent</em>. This means that no matter what context is
30 currently active, the same <tt>glVertex3fv</tt> function is used.</p>
31
32 <p>This creates the first bit of dispatch complexity. An application can
33 have two GL contexts. One context is a direct rendering context where
34 function calls are routed directly to a driver loaded within the
35 application's address space. The other context is an indirect rendering
36 context where function calls are converted to GLX protocol and sent to a
37 server. The same <tt>glVertex3fv</tt> has to do the right thing depending
38 on which context is current.</p>
39
40 <p>Highly optimized drivers or GLX protocol implementations may want to
41 change the behavior of GL functions depending on current state. For
42 example, <tt>glFogCoordf</tt> may operate differently depending on whether
43 or not fog is enabled.</p>
44
45 <p>In multi-threaded environments, it is possible for each thread to have a
46 differnt GL context current. This means that poor old <tt>glVertex3fv</tt>
47 has to know which GL context is current in the thread where it is being
48 called.</p>
49
50 <A NAME="overview"/>
51 <H2>2. Overview of Mesa's Implementation</H2>
52
53 <p>Mesa uses two per-thread pointers. The first pointer stores the address
54 of the context current in the thread, and the second pointer stores the
55 address of the <em>dispatch table</em> associated with that context. The
56 dispatch table stores pointers to functions that actually implement
57 specific GL functions. Each time a new context is made current in a thread,
58 these pointers a updated.</p>
59
60 <p>The implementation of functions such as <tt>glVertex3fv</tt> becomes
61 conceptually simple:</p>
62
63 <ul>
64 <li>Fetch the current dispatch table pointer.</li>
65 <li>Fetch the pointer to the real <tt>glVertex3fv</tt> function from the
66 table.</li>
67 <li>Call the real function.</li>
68 </ul>
69
70 <p>This can be implemented in just a few lines of C code. The file
71 <tt>src/mesa/glapi/glapitemp.h</tt> contains code very similar to this.</p>
72
73 <blockquote>
74 <table border="1">
75 <tr><td><pre>
76 void glVertex3f(GLfloat x, GLfloat y, GLfloat z)
77 {
78 const struct _glapi_table * const dispatch = GET_DISPATCH();
79
80 (*dispatch-&gt;Vertex3f)(x, y, z);
81 }</pre></td></tr>
82 <tr><td>Sample dispatch function</td></tr></table>
83 </blockquote>
84
85 <p>The problem with this simple implementation is the large amount of
86 overhead that it adds to every GL function call.</p>
87
88 <p>In a multithreaded environment, a naive implementation of
89 <tt>GET_DISPATCH</tt> involves a call to <tt>pthread_getspecific</tt> or a
90 similar function. Mesa provides a wrapper function called
91 <tt>_glapi_get_dispatch</tt> that is used by default.</p>
92
93 <H2>3. Optimizations</H2>
94
95 <p>A number of optimizations have been made over the years to diminish the
96 performance hit imposed by GL dispatch. This section describes these
97 optimizations. The benefits of each optimization and the situations where
98 each can or cannot be used are listed.</p>
99
100 <H3>3.1. Dual dispatch table pointers</H3>
101
102 <p>The vast majority of OpenGL applications use the API in a single threaded
103 manner. That is, the application has only one thread that makes calls into
104 the GL. In these cases, not only do the calls to
105 <tt>pthread_getspecific</tt> hurt performance, but they are completely
106 unnecessary! It is possible to detect this common case and avoid these
107 calls.</p>
108
109 <p>Each time a new dispatch table is set, Mesa examines and records the ID
110 of the executing thread. If the same thread ID is always seen, Mesa knows
111 that the application is, from OpenGL's point of view, single threaded.</p>
112
113 <p>As long as an application is single threaded, Mesa stores a pointer to
114 the dispatch table in a global variable called <tt>_glapi_Dispatch</tt>.
115 The pointer is also stored in a per-thread location via
116 <tt>pthread_setspecific</tt>. When Mesa detects that an application has
117 become multithreaded, <tt>NULL</tt> is stored in <tt>_glapi_Dispatch</tt>.</p>
118
119 <p>Using this simple mechanism the dispatch functions can detect the
120 multithreaded case by comparing <tt>_glapi_Dispatch</tt> to <tt>NULL</tt>.
121 The resulting implementation of <tt>GET_DISPATCH</tt> is slightly more
122 complex, but it avoids the expensive <tt>pthread_getspecific</tt> call in
123 the common case.</p>
124
125 <blockquote>
126 <table border="1">
127 <tr><td><pre>
128 #define GET_DISPATCH() \
129 (_glapi_Dispatch != NULL) \
130 ? _glapi_Dispatch : pthread_getspecific(&_glapi_Dispatch_key)
131 </pre></td></tr>
132 <tr><td>Improved <tt>GET_DISPATCH</tt> Implementation</td></tr></table>
133 </blockquote>
134
135 <H3>3.2. ELF TLS</H3>
136
137 <p>Starting with the 2.4.20 Linux kernel, each thread is allocated an area
138 of per-thread, global storage. Variables can be put in this area using some
139 extensions to GCC. By storing the dispatch table pointer in this area, the
140 expensive call to <tt>pthread_getspecific</tt> and the test of
141 <tt>_glapi_Dispatch</tt> can be avoided.</p>
142
143 <p>The dispatch table pointer is stored in a new variable called
144 <tt>_glapi_tls_Dispatch</tt>. A new variable name is used so that a single
145 libGL can implement both interfaces. This allows the libGL to operate with
146 direct rendering drivers that use either interface. Once the pointer is
147 properly declared, <tt>GET_DISPACH</tt> becomes a simple variable
148 reference.</p>
149
150 <blockquote>
151 <table border="1">
152 <tr><td><pre>
153 extern __thread struct _glapi_table *_glapi_tls_Dispatch
154 __attribute__((tls_model("initial-exec")));
155
156 #define GET_DISPATCH() _glapi_tls_Dispatch
157 </pre></td></tr>
158 <tr><td>TLS <tt>GET_DISPATCH</tt> Implementation</td></tr></table>
159 </blockquote>
160
161 <p>Use of this path is controlled by the preprocessor define
162 <tt>GLX_USE_TLS</tt>. Any platform capable of using TLS should use this as
163 the default dispatch method.</p>
164
165 <H3>3.3. Assembly Language Dispatch Stubs</H3>
166
167 <p>Many platforms has difficulty properly optimizing the tail-call in the
168 dispatch stubs. Platforms like x86 that pass parameters on the stack seem
169 to have even more difficulty optimizing these routines. All of the dispatch
170 routines are very short, and it is trivial to create optimal assembly
171 language versions. The amount of optimization provided by using assembly
172 stubs varies from platform to platform and application to application.
173 However, by using the assembly stubs, many platforms can use an additional
174 space optimization (see <A HREF="#fixedsize">below</A>).</p>
175
176 <p>The biggest hurdle to creating assembly stubs is handling the various
177 ways that the dispatch table pointer can be accessed. There are four
178 different methods that can be used:</p>
179
180 <ol>
181 <li>Using <tt>_glapi_Dispatch</tt> directly in builds for non-multithreaded
182 environments.</li>
183 <li>Using <tt>_glapi_Dispatch</tt> and <tt>_glapi_get_dispatch</tt> in
184 multithreaded environments.</li>
185 <li>Using <tt>_glapi_Dispatch</tt> and <tt>pthread_getspecific</tt> in
186 multithreaded environments.</li>
187 <li>Using <tt>_glapi_tls_Dispatch</tt> directly in TLS enabled
188 multithreaded environments.</li>
189 </ol>
190
191 <p>People wishing to implement assembly stubs for new platforms should focus
192 on #4 if the new platform supports TLS. Otherwise, implement #2 followed by
193 #3. Environments that do not support multithreading are uncommon and not
194 terribly relevant.</p>
195
196 <p>Selection of the dispatch table pointer access method is controlled by a
197 few preprocessor defines.</p>
198
199 <ul>
200 <li>If <tt>GLX_USE_TLS</tt> is defined, method #4 is used.</li>
201 <li>If <tt>PTHREADS</tt> is defined, method #3 is used.</li>
202 <li>If <tt>WIN32_THREADS</tt> is defined, method #2 is used.</li>
203 <li>If none of the preceeding are defined, method #1 is used.</li>
204 </ul>
205
206 <p>Two different techniques are used to handle the various different cases.
207 On x86 and SPARC, a macro called <tt>GL_STUB</tt> is used. In the preamble
208 of the assembly source file different implementations of the macro are
209 selected based on the defined preprocessor variables. The assmebly code
210 then consists of a series of invocations of the macros such as:
211
212 <blockquote>
213 <table border="1">
214 <tr><td><pre>
215 GL_STUB(Color3fv, _gloffset_Color3fv)
216 </pre></td></tr>
217 <tr><td>SPARC Assembly Implementation of <tt>glColor3fv</tt></td></tr></table>
218 </blockquote>
219
220 <p>The benefit of this technique is that changes to the calling pattern
221 (i.e., addition of a new dispatch table pointer access method) require fewer
222 changed lines in the assembly code.</p>
223
224 <p>However, this technique can only be used on platforms where the function
225 implementation does not change based on the parameters passed to the
226 function. For example, since x86 passes all parameters on the stack, no
227 additional code is needed to save and restore function parameters around a
228 call to <tt>pthread_getspecific</tt>. Since x86-64 passes parameters in
229 registers, varying amounts of code needs to be inserted around the call to
230 <tt>pthread_getspecific</tt> to save and restore the GL function's
231 parameters.</p>
232
233 <p>The other technique, used by platforms like x86-64 that cannot use the
234 first technique, is to insert <tt>#ifdef</tt> within the assembly
235 implementation of each function. This makes the assembly file considerably
236 larger (e.g., 29,332 lines for <tt>glapi_x86-64.S</tt> versus 1,155 lines for
237 <tt>glapi_x86.S</tt>) and causes simple changes to the function
238 implementation to generate many lines of diffs. Since the assmebly files
239 are typically generated by scripts (see <A HREF="#autogen">below</A>), this
240 isn't a significant problem.</p>
241
242 <p>Once a new assembly file is created, it must be inserted in the build
243 system. There are two steps to this. The file must first be added to
244 <tt>src/mesa/sources</tt>. That gets the file built and linked. The second
245 step is to add the correct <tt>#ifdef</tt> magic to
246 <tt>src/mesa/glapi/glapi_dispatch.c</tt> to prevent the C version of the
247 dispatch functions from being built.</p>
248
249 <A NAME="fixedsize"/>
250 <H3>3.4. Fixed-Length Dispatch Stubs</H3>
251
252 <p>To implement <tt>glXGetProcAddress</tt>, Mesa stores a table that
253 associates function names with pointers to those functions. This table is
254 stored in <tt>src/mesa/glapi/glprocs.h</tt>. For different reasons on
255 different platforms, storing all of those pointers is inefficient. On most
256 platforms, including all known platforms that support TLS, we can avoid this
257 added overhead.</p>
258
259 <p>If the assembly stubs are all the same size, the pointer need not be
260 stored for every function. The location of the function can instead be
261 calculated by multiplying the size of the dispatch stub by the offset of the
262 function in the table. This value is then added to the address of the first
263 dispatch stub.</p>
264
265 <p>This path is activated by adding the correct <tt>#ifdef</tt> magic to
266 <tt>src/mesa/glapi/glapi.c</tt> just before <tt>glprocs.h</tt> is
267 included.</p>
268
269 <A NAME="autogen"/>
270 <H2>4. Automatic Generation of Dispatch Stubs</H2>
271
272 </body>
273 </html>