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