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