| 1 | Notes on primus implementation |
| 2 | ============================== |
| 3 | |
| 4 | primus: quick'n'dirty OpenGL offloading |
| 5 | |
| 6 | The purpose is to redirect OpenGL rendering to another X display, and copy |
| 7 | the rendered frames to the original (as if rendering was not redirected), |
| 8 | similar to VirtualGL, but with different assumptions and design goals. |
| 9 | |
| 10 | primus makes the following assumptions: |
| 11 | |
| 12 | * both X servers are local, and direct rendering is available on both |
| 13 | * the application is "well-behaved" (uses double buffering, does not use |
| 14 | color index rendering, does not draw on top of the OpenGL drawable, etc.) |
| 15 | |
| 16 | In contrast, VirtualGL: |
| 17 | |
| 18 | * assumes that the application's X display is remote |
| 19 | * supports obscure OpenGL functionality, making it more versatile |
| 20 | |
| 21 | The design goals are: |
| 22 | |
| 23 | * simplicity/minimalism: |
| 24 | - only GLX functions are overridden |
| 25 | - only two Xlib functions are used |
| 26 | * efficiency: |
| 27 | - minimal amount of framebuffer data copying achievable from |
| 28 | application-level offloading (unfortunately OpenGL does |
| 29 | not allow to reduce beyond 2 copies, readback+display) |
| 30 | - pipelining of rendering, readback and display |
| 31 | |
| 32 | Put another way, VirtualGL is optimized for running arbitrary OpenGL |
| 33 | applications over a network, with correctness and bandwidth being primary |
| 34 | concerns, while primus is optimized for running modern games on hybrid |
| 35 | graphics hardware setups, with simplicity and performance in mind. It is only |
| 36 | needed until DMA-BUF/PRIME offloading is implemented and mature. |
| 37 | |
| 38 | This document collects various interesting issues encountered while |
| 39 | implementing primus. |
| 40 | |
| 41 | Assuming that a secondary X server driving the rendering slave card is |
| 42 | present, OpenGL offloading can be achieved by intercepting GLX calls to |
| 43 | reroute application's rendering to a pbuffer on the secondary X server, and |
| 44 | then reading back the framebuffer and displaying it on the original drawable. |
| 45 | |
| 46 | Intercepting GLX calls |
| 47 | ---------------------- |
| 48 | |
| 49 | The usual way to perform symbol interposition in Linux is to use LD_PRELOAD, |
| 50 | and that is what VirtualGL does. However, that is not enough when an |
| 51 | application uses dlopen+dlsym to access library functions. To support that, |
| 52 | VirtualGL overrides dlopen and dlsym via LD_PRELOAD as well, and it even |
| 53 | intercepts `dlopen("libdl")`. Perhaps a cleaner approach is to make the |
| 54 | wrapper provide the complete API (GLX+OpenGL in this case) and add the wrapper |
| 55 | into LD_LIBRARY_PATH instead, which is what primus implements. One problem |
| 56 | with this approach is that although primus overrides only GLX functions, it |
| 57 | has to implement trivial forwarding for OpenGL functions. |
| 58 | |
| 59 | If the underlying OpenGL library is compiled without -Bsymbolic and calls |
| 60 | GLX functions itself, those calls will invoke functions from the wrapper, |
| 61 | which will cause failures unless the wrapper is prepared for that by keeping a |
| 62 | per-thread boolean value indicating whether a wrapper function was entered but |
| 63 | not returned yet (primus does not do that yet). |
| 64 | |
| 65 | Implementing GLX redirection |
| 66 | ---------------------------- |
| 67 | |
| 68 | For each rendering context created by the application, primus creates an |
| 69 | additional slave-side context for readback and master-side context for display |
| 70 | (the latter would not be necessary if displaying the framebuffer was performed |
| 71 | by some other means than OpenGL, but it's useful for vblank synchronization). |
| 72 | |
| 73 | For each application thread that calls glXMakeCurrent, primus additionally |
| 74 | spawns a readback thread and a display thread. Rendering, readback and display |
| 75 | are pipelined: application thread regains control as soon as readback thread |
| 76 | issued an asynchronous glReadPixels into a PBO, and readback thread uses two |
| 77 | PBOs to perform readback of a new frame into one while another is used by the |
| 78 | display thread. |
| 79 | |
| 80 | Threads signal data availability/release via posix semaphores. Additionally, a |
| 81 | GL sync object is required so that readback thread does not read an incomplete |
| 82 | frame. |
| 83 | |
| 84 | The application sees slave-side FBConfig and GLXContext IDs, but master-side X |
| 85 | Visuals. |
| 86 | |
| 87 | One GLX function that is not easily redirected is glXUseXFont, as it internally |
| 88 | calls OpenGL functions on bitmap data from the X server. We need bitmap data |
| 89 | from one server, but OpenGL functions need to be called in the other server's |
| 90 | context. |
| 91 | |
| 92 | Direct Rendering on Both Servers |
| 93 | -------------------------------- |
| 94 | |
| 95 | The worker threads each get a direct rendering context. One small issue that |
| 96 | was encountered is that symbols from libglapi.so need to be loaded by primus |
| 97 | in a global namespace (`dlopen("libglapi.so.0", RTLD_GLOBAL)`), otherwise DRI |
| 98 | modules would fail to load (they need symbols from libglapi, but don't link |
| 99 | against it). Starting with Mesa 9.0, DRI modules do link against libglapi, so |
| 100 | this is not a problem. |
| 101 | |
| 102 | However, this means that without shared libglapi, it is not possible to obtain |
| 103 | direct rendering context when loading Mesa with `dlopen("libGL.so.1", |
| 104 | RTLD_LOCAL)`. |
| 105 | |
| 106 | Xlib and threads |
| 107 | ---------------- |
| 108 | |
| 109 | primus spawns additional threads for each application thread that calls |
| 110 | glXMakeCurrent: a readback thread and a display thread. The latter makes calls |
| 111 | to glXSwapBuffers after drawing a full-screen quad asynchronously with the |
| 112 | application. Initially, primus called XInitThreads to enable that, as |
| 113 | suggested by the Xlib error message. However, if an applications loads both |
| 114 | Xlib and libGL dynamically via dlopen, primus may not be able to call |
| 115 | XInitThreads early enough. It turned out that opening separate X connections |
| 116 | for primus' needs is a simpler and more robust solution. |
| 117 | |
| 118 | Forwarding OpenGL calls |
| 119 | ----------------------- |
| 120 | |
| 121 | To provide OpenGL API functions, primus contains trivial forwarding functions |
| 122 | (VirtualGL overrides some of OpenGL functions, e.g. glFinish to support |
| 123 | single-buffered applications; primus does not support those). However, it |
| 124 | would be better to rely on a dynamic linker mechanism to avoid the need to |
| 125 | provide forwarder implementations, and instead make the dynamic linker resolve |
| 126 | OpenGL functions to definitions found in a slave libGL. On Solaris, that would |
| 127 | be possible with per-symbol DT_FILTER tagging; unfortunately, this is not |
| 128 | supported in binutils and glibc. |
| 129 | |
| 130 | Another question is what functions need to be forwarded. Per OpenGL ABI |
| 131 | document, libraries need to export only up to OpenGL 1.2 and ARB_multitexture, |
| 132 | and anything above that the applications must obtain via glXGetProcAddress. In |
| 133 | reality, Mesa's libGL and nVidia's libGL export a significant (but different) |
| 134 | amount of functions implementing extensions, and there are applications either |
| 135 | linking against those (fortunately not many, Braid and Trine 2 have been found |
| 136 | guilty so far) or even trying to obtain pointers via dlsym (Enemy Territory). |
| 137 | |
| 138 | Compositing |
| 139 | ----------- |
| 140 | |
| 141 | There had been reports that using primus under a compositing WM causes |
| 142 | flickering or slow/uneven update rate. When worker threads are synchronized |
| 143 | in a way that the application's thread calling glXSwapBuffers does not regain |
| 144 | control until the display worker completed blitting and called glXSwapBuffers |
| 145 | itself, those issues are not observed. It's unclear what mechanism is causing |
| 146 | the problem. |
| 147 | |
| 148 | Waiting for the display worker to finish completely serializes |
| 149 | render-readback-display, so may cause a big degradation in performance. The |
| 150 | recommended mode is to make the display worker display the previously rendered |
| 151 | frame: this should achieve better performance at the expense of latency of |
| 152 | presentation (and black first frame unless special care is given). |
| 153 | |
| 154 | |
| 155 | Window Resizing |
| 156 | --------------- |
| 157 | |
| 158 | It is not obvious how to receive notifications of window resizing in primus. |
| 159 | At the moment, it simply calls XGetGeometry every frame (without that, the |
| 160 | only Xlib function called in primus would be XOpenDisplay). VirtualGL |
| 161 | intercepts several resizing-related functions from Xlib, and additionally |
| 162 | tries to deduce drawable size from glViewport calls. |
| 163 | |
| 164 | Window Destruction |
| 165 | ------------------ |
| 166 | |
| 167 | Since display worker runs asynchronously by default, there is a problem that |
| 168 | the window it is drawing to may be destroyed after application regained |
| 169 | control after calling glXSwapBuffers, but before the display worker completed |
| 170 | drawing and called glXSwapBuffers itself. The worker then encounters an X |
| 171 | error and causes the application to terminate. This problem does not arise |
| 172 | when worker threads are synchronized as described above. How to avoid this |
| 173 | problem while still running the display worker asynchronously? |
| 174 | |
| 175 | Multilib |
| 176 | -------- |
| 177 | |
| 178 | Linux dynamic linker expands $LIB in paths according to current architecture |
| 179 | (since 2002). This nice feature is used both in dlopen() calls in primus and |
| 180 | the wrapper script. |