Since release, the Librelancer project has been targeting OpenGL 3.3 as its minimum graphics driver requirement, with efforts made to support hardware (like the Raspberry Pi 4 and 5) that aren’t fully compliant with GL 3.3 but have the required extensions available. For the assets of Vanilla Freelancer (originally targeting DirectX 8!) this is sufficient, and the renderer can achieve better efficiency on modern machines than the original engine’s renderer.
Unfortunately, over time OpenGL drivers for older hardware across platforms have suffered from regressions and bitrot. Problems have arisen from excess CPU usage when cycling buffers, flickering when buffers are synced with GL_ARB_sync fences, and features like integer bitwise operations being entirely broken within shaders. In the current version of the renderer, these issues have been worked around and problematic OpenGL features have been avoided.
These issues have limited the Librelancer renderer for some time now, as some of these platforms (cough cough Intel HD Graphics) advertise full OpenGL 4.5 (or 3.1) support, so there is little opportunity for feature gating without starting to blacklist individual drivers - a slippery slope of heuristics I’d rather not go down. Thoughts on a Vulkan backend had been discussed multiple times, with the conclusion usually being that the effort of writing an efficient Vulkan renderer that can rival the best OpenGL drivers for correctness and performance would not be worth it.
At the beginning of 2025, SDL3 was released with the new fancy SDL_GPU API - and issue #189 was created to track if it would be a good fit or not. Creating the actual backend itself was shelved for future research while more fun features were being worked on, and I wanted to give the concept time to become battletested by other projects first.
Reading the documentation, I realised the first big step would need to be to get shaders translated and bundled for all the platforms SDL_GPU supports. For new projects designed with SDL_GPU in mind, the official supported way to do this is to use SDL_shadercross. Librelancer, however, had the extra requirement of needing to support legacy OpenGL shaders. I didn’t want to maintain two sets of shaders and bindings indefinitely, so the project of LLShaderCompiler was born, starting as a port of SDL_shadercross to C# with an added option of generating legacy GLSL.
The shader compiler is the combination of Microsoft’s DirectXShaderCompiler at the frontend, set to compile HLSL shaders into SPIR-V (the shader format for Vulkan), and SPIRV-Cross in the backend. SPIRV-Cross provides reflection information on required buffers, inputs and outputs, and then can translate the provided SPIRV back into HLSL (after applying translations), Metal Shading Language for macOS, and variants of GLSL.
The GLSL outputted by SPIRV-Cross is not always suitable for direct consumption in Librelancer, as the way it handles uniform information and storage buffers does not operate in the same way Librelancer expects shader storage to function. After translating, LLShaderCompiler applies a series of transforms to the generated GLSL (using some questionable string processing) to get that GLSL into a state where it can be used by our graphics backend.
This work was good preparation for writing the SDL_GPU backend in 2026, as a large amount of the work for preparing for a second backend had been completed with the shader translator, despite a decent amount of changes needed in LLShaderCompiler as it was no longer only being used for the GLSL translation.
When it came to write the SDL_GPU backend in 2026, inspiration was drawn from FNA3D, an implementation of the old Microsoft XNA 4.0 Graphics namespace. The SDL_GPU code here has seen some fairly significant use in commercial titles, being currently used for the Linux version of Terraria.
Fun fact: The very first attempt of starting Librelancer also was based on a different port of the XNA apis to Linux.
FNA3D’s API being primarily a preservation project does not expose all the features needed to implement Librelancer’s graphics interfaces, and it still relies on the old fxc shader compiler for D3D9. It still served as an excellent reference for implementing SDL_GPU, as to date the Librelancer code still can look XNA-shaped if you squint hard enough.
In order to get the new SDL_GPU backend and the OpenGL backend acting the same, a few choices had to be made around which conventions were going to be kept OpenGL style, and which conventions were going to be kept SDL_GPU style:
For uploading vertex buffers to the GPU, I added an option that flags whether or not the previous contents should be discarded, as overwriting an in-use vertex buffer without discarding is not supported under the SDL_GPU backend. Due to the aforementioned issues with OpenGL drivers, the GL backend ignores any discard flags and takes the synchronisation penalty.
For resource binding, we keep the model we use for OpenGL. Numbers are assigned in the shaders for texture units and storage buffers with semantic meaning, e.g. we always use the number 9 in the Librelancer code to mean the bone matrices for character skinning. SDL_GPU expects the shaders to have sequential numbers for everything starting from 0, with storage buffers sharing the same numbering as textures. When LLShaderCompiler runs, it takes the numbers we specified in our semantic space, and remaps it to the numbering SDL_GPU expects, and provides a table of numbers to map the Librelancer bindings back to SDL_GPU.
For scissor rectangles, we also keep the OpenGL (and legacy DirectX) model. In modern APIs, the scissor rectangle only affects geometry drawn by shaders, clipping the outputted pixels to only be within that rectangle. In OpenGL and DirectX 9, the scissor rectangle state also affects clear commands and framebuffer blits. To emulate this for framebuffer blits we simply clip the blit destination before sending it to SDL_GPU, achieving the same behaviour. For clears that are clipped by the scissor rectangle, we have to run a shader instead of a simple clear command as this is an operation not natively supported by several mobile GPUs, and Apple does not include the functionality at all in Metal.
Then finally, we have to wrap our heads around texture orientation once more:
Consider a simple texture. In the DirectX world where Freelancer comes from (and where we now reside with SDL_GPU), the image is defined as having the coordinates (0, 0) in the top-left of the image. When an image is uploaded to DirectX using the IDirect3DTexture8::LockRect API, assuming you copy the full image the first pixel of the image corresponds to that (0,0) coordinate.
When we get to OpenGL, we are told that the convention is flipped upside-down (and it is!). In OpenGL land, the bottom-left of the image is defined as (0, 0). When an image is uploaded to OpenGL using the glTexImage2D API, copying from the exact same buffer, the first pixel of the image corresponds to that (0, 0) coordinate.
When we use a diagram to display it in this coordinate space, our image is upside down. But you can notice that in both cases (0, 0) points to the same pixel. This means for our shaders and our models, we don’t actually need to change any of our texture mapping in order to get the same result. There is no meaningful difference here for our renderer.
Where things change is the way framebuffers are laid out in memory. Here, OpenGL manages the conversion from the 3D coordinates we have after the vertex shader runs to the screen, and OpenGL lays out memory according to its conventions. If we were to render our cat image to the screen in OpenGL, the framebuffer would be like this:
The cat is the right side up, and will display on the screen the right side up. However, if we were to read from this texture from a shader, (0, 0) would point to the bottom left. Before implementing the SDL_GPU backend, the Librelancer code simply used flipped texture coordinates when reading from a texture it had rendered to a framebuffer itself. Now, with a backend that does not have this flipped framebuffer, passing the same coordinates to the two backends would not get the same result.
For the solution, I looked to what was available to me in SPIRV-Cross, and how other translation layers like wined3d got around it. When we are rendering to the screen directly, no change needs to be made as the image will display the right way up. When we render to an offscreen framebuffer, however, we flip the Y coordinate in the vertex shader.
SPIRV-Cross offers the FlipVertexY option when translating a vertex shader to GLSL. This adds the line gl_Position.y = -gl_Position.y to the end of the shader. One option would have been to compile all the shaders with this line for off-screen rendering, and without this line for on-screen rendering. This would have added a very large amount of shader variants though, so the LLShaderCompiler tool replaces this line with gl_Position.y = gl_Position.y * __spv_vertex_flip; where __spv_vertex_flip is an added uniform that can be either 1.0 or -1.0 for on and off-screen rendering respectively.
When performing this flip, the calculations for the front facing triangles must also be reversed, otherwise the wrong triangles get culled. So when we are rendering on-screen, we stick to the regular counter-clockwise winding with glFrontFace(GL_CCW). When we are off-screen, we set glFrontFace(GL_CW). This is the method that wined3d uses as well, and transforms the OpenGL backend to use the DirectX orientation for all our framebuffers.
Additionally, if we blit an off-screen buffer to the screen, we flip it to match the OpenGL convention. We then also flip the screen buffer if we take a screenshot at the end of a frame so it is not upside-down when saved to a file.
A NOTE: It is a common misconception that the .tga loader for Freelancer flips images before uploading, this is not correct. Freelancer treats a .tga file as a simple buffer of pixels, and ignores most of the flags in the header it does not understand, including the Flip Y setting. Many of the textures embedded in the Freelancer files have this Y bit set, so in an image viewer that correctly follows the TGA specification, these images will appear upside-down compared to how Freelancer loads them.
To view these files correctly, view them with the Texture Viewer functionality of LancerEdit, or export them to .png, which removes this metadata that Freelancer ignores. Modern TGA exports from image editors do not set this Flip Y flag that Freelancer doesn’t understand, and the orientation of the image is consistent between Freelancer and your image viewer.
With all these differences reconciled, we now have a renderer backend that supports OpenGL, Vulkan, and D3D12.
Currently, the difference in render backends shouldn’t be visible when running Librelancer. If your hardware supports the minimum specifications and your system has an up-to-date version of SDL3 installed (bundled with Librelancer on windows, 3.4.0 minimum required on Linux), you will get the SDL_GPU powered renderer. Otherwise, Librelancer will seamlessly switch back to rendering with OpenGL. The OpenGL backend is going to be kept in Librelancer indefinitely, though extensions and features in the future that may require compute shaders or technologies introduced after 2011 will not be available on the OpenGL backend.
Currently supported graphics devices for SDL_GPU are:
librelancer_renderer=gl can be used to work around it in the meantime.If you find bugs when running the SDL_GPU backend, please report them on the issue tracker. If you would like to force the OpenGL backend, start Librelancer with the librelancer_renderer environment variable set to "gl".
Oh, and Merry Christmas for 2026!
~ Callum