Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

dispatch.html 12KB

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