dispatch.html 12.1 KB
Newer Older
1 2 3 4
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
5
  <title>GL Dispatch</title>
6 7 8
  <link rel="stylesheet" type="text/css" href="mesa.css">
</head>
<body>
9 10

<div class="header">
11
  The Mesa 3D Graphics Library
12 13 14 15 16
</div>

<iframe src="contents.html"></iframe>
<div class="content">

17
<h1>GL Dispatch</h1>
Ian Romanick's avatar
Ian Romanick committed
18 19 20 21

<p>Several factors combine to make efficient dispatch of OpenGL functions
fairly complicated.  This document attempts to explain some of the issues
and introduce the reader to Mesa's implementation.  Readers already familiar
22 23
with the issues around GL dispatch can safely skip ahead to the <a
href="#overview">overview of Mesa's implementation</a>.</p>
Ian Romanick's avatar
Ian Romanick committed
24

25
<h2>1. Complexity of GL Dispatch</h2>
Ian Romanick's avatar
Ian Romanick committed
26 27

<p>Every GL application has at least one object called a GL <em>context</em>.
Nathan Kidd's avatar
Nathan Kidd committed
28
This object, which is an implicit parameter to every GL function, stores all
Ian Romanick's avatar
Ian Romanick committed
29 30 31 32
of the GL related state for the application.  Every texture, every buffer
object, every enable, and much, much more is stored in the context.  Since
an application can have more than one context, the context to be used is
selected by a window-system dependent function such as
33
<code>glXMakeContextCurrent</code>.</p>
Ian Romanick's avatar
Ian Romanick committed
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
Ian Romanick's avatar
Ian Romanick committed
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>
Ian Romanick's avatar
Ian Romanick committed
39 40 41 42 43 44

<p>This creates the first bit of dispatch complexity.  An application can
have two GL contexts.  One context is a direct rendering context where
function calls are routed directly to a driver loaded within the
application's address space.  The other context is an indirect rendering
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
Ian Romanick's avatar
Ian Romanick committed
46 47 48 49
on which context is current.</p>

<p>Highly optimized drivers or GLX protocol implementations may want to
change the behavior of GL functions depending on current state.  For
50
example, <code>glFogCoordf</code> may operate differently depending on whether
Ian Romanick's avatar
Ian Romanick committed
51 52 53
or not fog is enabled.</p>

<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>
Ian Romanick's avatar
Ian Romanick committed
55 56 57
has to know which GL context is current in the thread where it is being
called.</p>

58
<h2 id="overview">2. Overview of Mesa's Implementation</h2>
Ian Romanick's avatar
Ian Romanick committed
59 60 61 62 63 64 65 66

<p>Mesa uses two per-thread pointers.  The first pointer stores the address
of the context current in the thread, and the second pointer stores the
address of the <em>dispatch table</em> associated with that context.  The
dispatch table stores pointers to functions that actually implement
specific GL functions.  Each time a new context is made current in a thread,
these pointers a updated.</p>

67
<p>The implementation of functions such as <code>glVertex3fv</code> becomes
Ian Romanick's avatar
Ian Romanick committed
68 69 70 71
conceptually simple:</p>

<ul>
<li>Fetch the current dispatch table pointer.</li>
72
<li>Fetch the pointer to the real <code>glVertex3fv</code> function from the
Ian Romanick's avatar
Ian Romanick committed
73 74 75 76 77
table.</li>
<li>Call the real function.</li>
</ul>

<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>
Ian Romanick's avatar
Ian Romanick committed
79 80 81 82 83 84 85

<blockquote>
<table border="1">
<tr><td><pre>
void glVertex3f(GLfloat x, GLfloat y, GLfloat z)
{
    const struct _glapi_table * const dispatch = GET_DISPATCH();
Andreas Boll's avatar
Andreas Boll committed
86

Ian Romanick's avatar
Ian Romanick committed
87 88 89 90 91 92 93 94
    (*dispatch-&gt;Vertex3f)(x, y, z);
}</pre></td></tr>
<tr><td>Sample dispatch function</td></tr></table>
</blockquote>

<p>The problem with this simple implementation is the large amount of
overhead that it adds to every GL function call.</p>

Homer Hsing's avatar
Homer Hsing committed
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
Ian Romanick's avatar
Ian Romanick committed
97
similar function.  Mesa provides a wrapper function called
98
<code>_glapi_get_dispatch</code> that is used by default.</p>
Ian Romanick's avatar
Ian Romanick committed
99

100
<h2>3. Optimizations</h2>
Ian Romanick's avatar
Ian Romanick committed
101 102 103 104 105 106

<p>A number of optimizations have been made over the years to diminish the
performance hit imposed by GL dispatch.  This section describes these
optimizations.  The benefits of each optimization and the situations where
each can or cannot be used are listed.</p>

107
<h3>3.1. Dual dispatch table pointers</h3>
Ian Romanick's avatar
Ian Romanick committed
108 109 110 111

<p>The vast majority of OpenGL applications use the API in a single threaded
manner.  That is, the application has only one thread that makes calls into
the GL.  In these cases, not only do the calls to
112
<code>pthread_getspecific</code> hurt performance, but they are completely
Ian Romanick's avatar
Ian Romanick committed
113 114 115 116 117 118 119 120
unnecessary!  It is possible to detect this common case and avoid these
calls.</p>

<p>Each time a new dispatch table is set, Mesa examines and records the ID
of the executing thread.  If the same thread ID is always seen, Mesa knows
that the application is, from OpenGL's point of view, single threaded.</p>

<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>.
Ian Romanick's avatar
Ian Romanick committed
122
The pointer is also stored in a per-thread location via
123 124
<code>pthread_setspecific</code>.  When Mesa detects that an application has
become multithreaded, <code>NULL</code> is stored in <code>_glapi_Dispatch</code>.</p>
Ian Romanick's avatar
Ian Romanick committed
125 126

<p>Using this simple mechanism the dispatch functions can detect the
127 128 129
multithreaded case by comparing <code>_glapi_Dispatch</code> to <code>NULL</code>.
The resulting implementation of <code>GET_DISPATCH</code> is slightly more
complex, but it avoids the expensive <code>pthread_getspecific</code> call in
Ian Romanick's avatar
Ian Romanick committed
130 131 132 133 134 135 136
the common case.</p>

<blockquote>
<table border="1">
<tr><td><pre>
#define GET_DISPATCH() \
    (_glapi_Dispatch != NULL) \
137
        ? _glapi_Dispatch : pthread_getspecific(&amp;_glapi_Dispatch_key)
Ian Romanick's avatar
Ian Romanick committed
138
</pre></td></tr>
139
<tr><td>Improved <code>GET_DISPATCH</code> Implementation</td></tr></table>
Ian Romanick's avatar
Ian Romanick committed
140 141
</blockquote>

142
<h3>3.2. ELF TLS</h3>
Ian Romanick's avatar
Ian Romanick committed
143 144 145 146

<p>Starting with the 2.4.20 Linux kernel, each thread is allocated an area
of per-thread, global storage.  Variables can be put in this area using some
extensions to GCC.  By storing the dispatch table pointer in this area, the
147 148
expensive call to <code>pthread_getspecific</code> and the test of
<code>_glapi_Dispatch</code> can be avoided.</p>
Ian Romanick's avatar
Ian Romanick committed
149 150

<p>The dispatch table pointer is stored in a new variable called
151
<code>_glapi_tls_Dispatch</code>.  A new variable name is used so that a single
Ian Romanick's avatar
Ian Romanick committed
152 153
libGL can implement both interfaces.  This allows the libGL to operate with
direct rendering drivers that use either interface.  Once the pointer is
154
properly declared, <code>GET_DISPACH</code> becomes a simple variable
Ian Romanick's avatar
Ian Romanick committed
155 156 157 158 159 160 161 162 163 164
reference.</p>

<blockquote>
<table border="1">
<tr><td><pre>
extern __thread struct _glapi_table *_glapi_tls_Dispatch
    __attribute__((tls_model("initial-exec")));

#define GET_DISPATCH() _glapi_tls_Dispatch
</pre></td></tr>
165
<tr><td>TLS <code>GET_DISPATCH</code> Implementation</td></tr></table>
Ian Romanick's avatar
Ian Romanick committed
166 167 168
</blockquote>

<p>Use of this path is controlled by the preprocessor define
169
<code>GLX_USE_TLS</code>.  Any platform capable of using TLS should use this as
Ian Romanick's avatar
Ian Romanick committed
170 171
the default dispatch method.</p>

172
<h3>3.3. Assembly Language Dispatch Stubs</h3>
Ian Romanick's avatar
Ian Romanick committed
173 174 175 176 177 178 179 180

<p>Many platforms has difficulty properly optimizing the tail-call in the
dispatch stubs.  Platforms like x86 that pass parameters on the stack seem
to have even more difficulty optimizing these routines.  All of the dispatch
routines are very short, and it is trivial to create optimal assembly
language versions.  The amount of optimization provided by using assembly
stubs varies from platform to platform and application to application.
However, by using the assembly stubs, many platforms can use an additional
181
space optimization (see <a href="#fixedsize">below</a>).</p>
Ian Romanick's avatar
Ian Romanick committed
182 183 184 185 186 187

<p>The biggest hurdle to creating assembly stubs is handling the various
ways that the dispatch table pointer can be accessed.  There are four
different methods that can be used:</p>

<ol>
188
<li>Using <code>_glapi_Dispatch</code> directly in builds for non-multithreaded
189
environments.</li>
190
<li>Using <code>_glapi_Dispatch</code> and <code>_glapi_get_dispatch</code> in
Ian Romanick's avatar
Ian Romanick committed
191
multithreaded environments.</li>
192
<li>Using <code>_glapi_Dispatch</code> and <code>pthread_getspecific</code> in
Ian Romanick's avatar
Ian Romanick committed
193
multithreaded environments.</li>
194
<li>Using <code>_glapi_tls_Dispatch</code> directly in TLS enabled
Ian Romanick's avatar
Ian Romanick committed
195 196 197 198 199 200 201 202 203 204 205 206
multithreaded environments.</li>
</ol>

<p>People wishing to implement assembly stubs for new platforms should focus
on #4 if the new platform supports TLS.  Otherwise, implement #2 followed by
#3.  Environments that do not support multithreading are uncommon and not
terribly relevant.</p>

<p>Selection of the dispatch table pointer access method is controlled by a
few preprocessor defines.</p>

<ul>
207 208
<li>If <code>GLX_USE_TLS</code> is defined, method #3 is used.</li>
<li>If <code>HAVE_PTHREAD</code> is defined, method #2 is used.</li>
Nathan Kidd's avatar
Nathan Kidd committed
209
<li>If none of the preceding are defined, method #1 is used.</li>
Ian Romanick's avatar
Ian Romanick committed
210 211 212
</ul>

<p>Two different techniques are used to handle the various different cases.
213
On x86 and SPARC, a macro called <code>GL_STUB</code> is used.  In the preamble
Ian Romanick's avatar
Ian Romanick committed
214
of the assembly source file different implementations of the macro are
Nathan Kidd's avatar
Nathan Kidd committed
215
selected based on the defined preprocessor variables.  The assembly code
Ian Romanick's avatar
Ian Romanick committed
216 217 218 219 220 221 222
then consists of a series of invocations of the macros such as:

<blockquote>
<table border="1">
<tr><td><pre>
GL_STUB(Color3fv, _gloffset_Color3fv)
</pre></td></tr>
223
<tr><td>SPARC Assembly Implementation of <code>glColor3fv</code></td></tr></table>
Ian Romanick's avatar
Ian Romanick committed
224 225 226 227 228 229 230 231 232 233
</blockquote>

<p>The benefit of this technique is that changes to the calling pattern
(i.e., addition of a new dispatch table pointer access method) require fewer
changed lines in the assembly code.</p>

<p>However, this technique can only be used on platforms where the function
implementation does not change based on the parameters passed to the
function.  For example, since x86 passes all parameters on the stack, no
additional code is needed to save and restore function parameters around a
234
call to <code>pthread_getspecific</code>.  Since x86-64 passes parameters in
Ian Romanick's avatar
Ian Romanick committed
235
registers, varying amounts of code needs to be inserted around the call to
236
<code>pthread_getspecific</code> to save and restore the GL function's
Ian Romanick's avatar
Ian Romanick committed
237 238 239
parameters.</p>

<p>The other technique, used by platforms like x86-64 that cannot use the
240
first technique, is to insert <code>#ifdef</code> within the assembly
Ian Romanick's avatar
Ian Romanick committed
241
implementation of each function.  This makes the assembly file considerably
242 243
larger (e.g., 29,332 lines for <code>glapi_x86-64.S</code> versus 1,155 lines for
<code>glapi_x86.S</code>) and causes simple changes to the function
Nathan Kidd's avatar
Nathan Kidd committed
244
implementation to generate many lines of diffs.  Since the assembly files
245
are typically generated by scripts (see <a href="#autogen">below</a>), this
Ian Romanick's avatar
Ian Romanick committed
246 247 248 249
isn't a significant problem.</p>

<p>Once a new assembly file is created, it must be inserted in the build
system.  There are two steps to this.  The file must first be added to
250 251 252
<code>src/mesa/sources</code>.  That gets the file built and linked.  The second
step is to add the correct <code>#ifdef</code> magic to
<code>src/mesa/glapi/glapi_dispatch.c</code> to prevent the C version of the
253
dispatch functions from being built.</p>
Ian Romanick's avatar
Ian Romanick committed
254

255
<h3 id="fixedsize">3.4. Fixed-Length Dispatch Stubs</h3>
Ian Romanick's avatar
Ian Romanick committed
256

257
<p>To implement <code>glXGetProcAddress</code>, Mesa stores a table that
Ian Romanick's avatar
Ian Romanick committed
258
associates function names with pointers to those functions.  This table is
259
stored in <code>src/mesa/glapi/glprocs.h</code>.  For different reasons on
Ian Romanick's avatar
Ian Romanick committed
260 261 262 263 264 265 266 267 268 269
different platforms, storing all of those pointers is inefficient.  On most
platforms, including all known platforms that support TLS, we can avoid this
added overhead.</p>

<p>If the assembly stubs are all the same size, the pointer need not be
stored for every function.  The location of the function can instead be
calculated by multiplying the size of the dispatch stub by the offset of the
function in the table.  This value is then added to the address of the first
dispatch stub.</p>

270 271
<p>This path is activated by adding the correct <code>#ifdef</code> magic to
<code>src/mesa/glapi/glapi.c</code> just before <code>glprocs.h</code> is
Ian Romanick's avatar
Ian Romanick committed
272 273
included.</p>

274
<h2 id="autogen">4. Automatic Generation of Dispatch Stubs</h2>
Ian Romanick's avatar
Ian Romanick committed
275

276
</div>
277 278
</body>
</html>