| 1 |
GL Dispatch |
| 2 |
=========== |
| 3 |
|
| 4 |
Several factors combine to make efficient dispatch of OpenGL functions |
| 5 |
fairly complicated. This document attempts to explain some of the issues |
| 6 |
and introduce the reader to Mesa's implementation. Readers already |
| 7 |
familiar with the issues around GL dispatch can safely skip ahead to the |
| 8 |
:ref:`overview of Mesa's implementation <overview>`. |
| 9 |
|
| 10 |
1. Complexity of GL Dispatch |
| 11 |
---------------------------- |
| 12 |
|
| 13 |
Every GL application has at least one object called a GL *context*. This |
| 14 |
object, which is an implicit parameter to every GL function, stores all |
| 15 |
of the GL related state for the application. Every texture, every buffer |
| 16 |
object, every enable, and much, much more is stored in the context. |
| 17 |
Since an application can have more than one context, the context to be |
| 18 |
used is selected by a window-system dependent function such as |
| 19 |
``glXMakeContextCurrent``. |
| 20 |
|
| 21 |
In environments that implement OpenGL with X-Windows using GLX, every GL |
| 22 |
function, including the pointers returned by ``glXGetProcAddress``, are |
| 23 |
*context independent*. This means that no matter what context is |
| 24 |
currently active, the same ``glVertex3fv`` function is used. |
| 25 |
|
| 26 |
This creates the first bit of dispatch complexity. An application can |
| 27 |
have two GL contexts. One context is a direct rendering context where |
| 28 |
function calls are routed directly to a driver loaded within the |
| 29 |
application's address space. The other context is an indirect rendering |
| 30 |
context where function calls are converted to GLX protocol and sent to a |
| 31 |
server. The same ``glVertex3fv`` has to do the right thing depending on |
| 32 |
which context is current. |
| 33 |
|
| 34 |
Highly optimized drivers or GLX protocol implementations may want to |
| 35 |
change the behavior of GL functions depending on current state. For |
| 36 |
example, ``glFogCoordf`` may operate differently depending on whether or |
| 37 |
not fog is enabled. |
| 38 |
|
| 39 |
In multi-threaded environments, it is possible for each thread to have a |
| 40 |
different GL context current. This means that poor old ``glVertex3fv`` |
| 41 |
has to know which GL context is current in the thread where it is being |
| 42 |
called. |
| 43 |
|
| 44 |
.. _overview: |
| 45 |
|
| 46 |
2. Overview of Mesa's Implementation |
| 47 |
------------------------------------ |
| 48 |
|
| 49 |
Mesa uses two per-thread pointers. The first pointer stores the address |
| 50 |
of the context current in the thread, and the second pointer stores the |
| 51 |
address of the *dispatch table* associated with that context. The |
| 52 |
dispatch table stores pointers to functions that actually implement |
| 53 |
specific GL functions. Each time a new context is made current in a |
| 54 |
thread, these pointers are updated. |
| 55 |
|
| 56 |
The implementation of functions such as ``glVertex3fv`` becomes |
| 57 |
conceptually simple: |
| 58 |
|
| 59 |
- Fetch the current dispatch table pointer. |
| 60 |
- Fetch the pointer to the real ``glVertex3fv`` function from the |
| 61 |
table. |
| 62 |
- Call the real function. |
| 63 |
|
| 64 |
This can be implemented in just a few lines of C code. The file |
| 65 |
``src/mesa/glapi/glapitemp.h`` contains code very similar to this. |
| 66 |
|
| 67 |
.. code-block:: c |
| 68 |
:caption: Sample dispatch function |
| 69 |
|
| 70 |
void glVertex3f(GLfloat x, GLfloat y, GLfloat z) |
| 71 |
{ |
| 72 |
const struct _glapi_table * const dispatch = GET_DISPATCH(); |
| 73 |
|
| 74 |
dispatch->Vertex3f(x, y, z); |
| 75 |
} |
| 76 |
|
| 77 |
The problem with this simple implementation is the large amount of |
| 78 |
overhead that it adds to every GL function call. |
| 79 |
|
| 80 |
In a multithreaded environment, a naive implementation of |
| 81 |
``GET_DISPATCH()`` involves a call to ``_mesa_glapi_get_dispatch()`` or |
| 82 |
``_mesa_glapi_tls_Dispatch``. |
| 83 |
|
| 84 |
3. Optimizations |
| 85 |
---------------- |
| 86 |
|
| 87 |
A number of optimizations have been made over the years to diminish the |
| 88 |
performance hit imposed by GL dispatch. This section describes these |
| 89 |
optimizations. The benefits of each optimization and the situations |
| 90 |
where each can or cannot be used are listed. |
| 91 |
|
| 92 |
3.1. ELF TLS |
| 93 |
~~~~~~~~~~~~ |
| 94 |
|
| 95 |
Starting with the 2.4.20 Linux kernel, each thread is allocated an area |
| 96 |
of per-thread, global storage. Variables can be put in this area using |
| 97 |
some extensions to GCC that called ``ELF TLS``. By storing the dispatch table |
| 98 |
pointer in this area, the expensive call to ``pthread_getspecific`` and |
| 99 |
the test of ``_mesa_glapi_Dispatch`` can be avoided. As we don't support for |
| 100 |
Linux kernel earlier than 2.4.20, so we can always using ``ELF TLS``. |
| 101 |
|
| 102 |
The dispatch table pointer is stored in a new variable called |
| 103 |
``_mesa_glapi_tls_Dispatch``. A new variable name is used so that a single |
| 104 |
libGL can implement both interfaces. This allows the libGL to operate |
| 105 |
with direct rendering drivers that use either interface. Once the |
| 106 |
pointer is properly declared, ``GET_DISPACH`` becomes a simple variable |
| 107 |
reference. |
| 108 |
|
| 109 |
.. code-block:: c |
| 110 |
:caption: TLS ``GET_DISPATCH`` Implementation |
| 111 |
|
| 112 |
extern __THREAD_INITIAL_EXEC struct _glapi_table *_mesa_glapi_tls_Dispatch; |
| 113 |
|
| 114 |
#define GET_DISPATCH() _mesa_glapi_tls_Dispatch |
| 115 |
|
| 116 |
3.2. Assembly Language Dispatch Stubs |
| 117 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 118 |
|
| 119 |
Many platforms have difficulty properly optimizing the tail-call in the |
| 120 |
dispatch stubs. Platforms like x86 that pass parameters on the stack |
| 121 |
seem to have even more difficulty optimizing these routines. All of the |
| 122 |
dispatch routines are very short, and it is trivial to create optimal |
| 123 |
assembly language versions. The amount of optimization provided by using |
| 124 |
assembly stubs varies from platform to platform and application to |
| 125 |
application. However, by using the assembly stubs, many platforms can |
| 126 |
use an additional space optimization (see :ref:`below <fixedsize>`). |
| 127 |
|
| 128 |
The biggest hurdle to creating assembly stubs is handling the various |
| 129 |
ways that the dispatch table pointer can be accessed. There are four |
| 130 |
different methods that can be used: |
| 131 |
|
| 132 |
#. Using ``_mesa_glapi_Dispatch`` directly in builds for non-multithreaded |
| 133 |
environments. |
| 134 |
#. Using ``_mesa_glapi_Dispatch`` and ``_mesa_glapi_get_dispatch`` in |
| 135 |
multithreaded environments. |
| 136 |
#. Using ``_mesa_glapi_tls_Dispatch`` directly in TLS enabled multithreaded |
| 137 |
environments. |
| 138 |
|
| 139 |
People wishing to implement assembly stubs for new platforms should |
| 140 |
focus on #3 if the new platform supports TLS. Otherwise implement #2. |
| 141 |
Environments that do not support multithreading are |
| 142 |
uncommon and not terribly relevant. |
| 143 |
|
| 144 |
Selection of the dispatch table pointer access method is controlled by a |
| 145 |
few preprocessor defines. |
| 146 |
|
| 147 |
- If ``HAVE_PTHREAD`` is defined, method #2 is used. |
| 148 |
- If none of the preceding are defined, method #1 is used. |
| 149 |
|
| 150 |
Two different techniques are used to handle the various different cases. |
| 151 |
On x86 and SPARC, a macro called ``GL_STUB`` is used. In the preamble of |
| 152 |
the assembly source file different implementations of the macro are |
| 153 |
selected based on the defined preprocessor variables. The assembly code |
| 154 |
then consists of a series of invocations of the macros such as: |
| 155 |
|
| 156 |
.. code-block:: c |
| 157 |
:caption: SPARC Assembly Implementation of ``glColor3fv`` |
| 158 |
|
| 159 |
GL_STUB(Color3fv, _gloffset_Color3fv) |
| 160 |
|
| 161 |
The benefit of this technique is that changes to the calling pattern |
| 162 |
(i.e., addition of a new dispatch table pointer access method) require |
| 163 |
fewer changed lines in the assembly code. |
| 164 |
|
| 165 |
However, this technique can only be used on platforms where the function |
| 166 |
implementation does not change based on the parameters passed to the |
| 167 |
function. For example, since x86 passes all parameters on the stack, no |
| 168 |
additional code is needed to save and restore function parameters around |
| 169 |
a call to ``pthread_getspecific``. Since x86-64 passes parameters in |
| 170 |
registers, varying amounts of code needs to be inserted around the call |
| 171 |
to ``pthread_getspecific`` to save and restore the GL function's |
| 172 |
parameters. |
| 173 |
|
| 174 |
The other technique, used by platforms like x86-64 that cannot use the |
| 175 |
first technique, is to insert ``#ifdef`` within the assembly |
| 176 |
implementation of each function. This makes the assembly file |
| 177 |
considerably larger (e.g., 29,332 lines for ``glapi_x86-64.S`` versus |
| 178 |
1,155 lines for ``glapi_x86.S``) and causes simple changes to the |
| 179 |
function implementation to generate many lines of diffs. Since the |
| 180 |
assembly files are typically generated by scripts, this isn't a |
| 181 |
significant problem. |
| 182 |
|
| 183 |
Once a new assembly file is created, it must be inserted in the build |
| 184 |
system. There are two steps to this. The file must first be added to |
| 185 |
``src/mesa/sources``. That gets the file built and linked. The second |
| 186 |
step is to add the correct ``#ifdef`` magic to |
| 187 |
``src/mesa/glapi/glapi_dispatch.c`` to prevent the C version of the |
| 188 |
dispatch functions from being built. |
| 189 |
|
| 190 |
.. _fixedsize: |
| 191 |
|
| 192 |
3.3. Fixed-Length Dispatch Stubs |
| 193 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 194 |
|
| 195 |
To implement ``glXGetProcAddress``, Mesa stores a table that associates |
| 196 |
function names with pointers to those functions. This table is stored in |
| 197 |
``src/mesa/glapi/glprocs.h``. For different reasons on different |
| 198 |
platforms, storing all of those pointers is inefficient. On most |
| 199 |
platforms, including all known platforms that support TLS, we can avoid |
| 200 |
this added overhead. |
| 201 |
|
| 202 |
If the assembly stubs are all the same size, the pointer need not be |
| 203 |
stored for every function. The location of the function can instead be |
| 204 |
calculated by multiplying the size of the dispatch stub by the offset of |
| 205 |
the function in the table. This value is then added to the address of |
| 206 |
the first dispatch stub. |
| 207 |
|
| 208 |
This path is activated by adding the correct ``#ifdef`` magic to |
| 209 |
``src/mesa/glapi/glapi.c`` just before ``glprocs.h`` is included. |