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