The Glium Library Showcases Rust at its Best
Rust has grown quickly since its inception, with a loyal following of dedicated users contributing to Mozilla’s OSS language. Rust has given Mozilla a new relevance and is often discussed in the same context as programming languages such as Go, which just had its 1.5 release. Rust, for that matter, just released its 1.2 release, and 1.3 as beta.
For those in game design and development, Rust opens up new possibilities for improving older projects. Tomaka has used Rust to create the Glium library, a project which wraps around the current OpenGL library with system improvements, code shortcuts, and quality of life upgrades for those working with OpenGL for game design, development or mathematical uses.
Tomaka highlights the pitfalls of the current OpenGL library in a Medium post introducing the changes that Glium will bring to those who opt to install it alongside their current OpenGL library. Tomaka has paid strict attention to performance issues present in OpenGL, alongside security concerns. OpenGL uses a global state, in addition to not enforcing memory safety since OpenGL 4. As such, Tomaka began work on Glium in an effort to bolster OpenGL’s performance and security through the use of Rust.
Programming in Rust has long been touted for its security and ease of swapping anywhere one would normally input C++. As more developers move toward Rust for its improvements to safety, speed, and compilation of code, there are more projects created which push the boundaries of what Rust is capable of. Rust’s modern development ecosystem improves quality of life for those looking to learn Rust, while the language’s compilation instills best practices for beginners and veterans alike.
Through the use of plugins and package management, Rust also stands apart from C++ in that it automates this process, freeing developers from having to manually source, download, and compile libraries themselves. Rust takes common pain points of C++, or projects written in C++, and offers new ways to address these pain points with clean, safe code.
Introducing the Glium Library
Glium hopes to solve the problems present with OpenGL 4 by wrapping around it, enabling users to work with the OpenGL they are familiar with, while adding features that save time and reduce common pain points when working in OpenGL. Tomaka notes that users will still have to manage textures, buffers and programs, but will be able to do so in a much more user-friendly environment. Glium offers users a better way to initialize context by making use of the Glutin OpenGL context creation library. This implements the
backend trait on Glutin’s current Window, making a once tedious process simpler. To initialize a window with Glium, Tomaka instructs users to run the following code:
let display = glutin::WindowBuilder::new().build_glium().unwrap();
Tomaka ensures those working with older systems that the Glium library is usable on nearly any framework, with the exception of OpenGL ES 1. Developers that wish to use Glium for mobile development can use the library with OpenGL ES 2. Older versions of OpenGL are compatible with Glium, though it only officially supports OpenGL ES 2 and 3.
As safety was a primary concern using OpenGL after memory safety was removed from OpenGL, Tomaka sought to address this head-on with a number of safety features and improvements in Glium. Glium ensures safe context management by ensuring that none of OpenGL’s objects implement the
Send function, ensuring that they cannot leave their thead. If a user calls a function, Glium calls the current context content to ensure that the context is the latest version. If this is not the case, Glium calls
MakeCurent on the specified context.
This enables users to run multiple OpenGL libraries or contexts without experiencing crashing. Users are encouraged to handle buffers manually, with Glium delivering an option for buffers with persistent mapping. Executing commands that use a buffer segment will create a sync fence that tracks access to the buffer. Persistent-mapped buffers greatly increase system performance, leading to significant FPS boosts when streaming data to be drawn.
System Performance Boosts with Glium
A highlight of Tomaka’s work with Glium is its handling of sRGB values. For those that work in game design or with mathematical graphic applications which require a strict color output, Glium uses Rust to assign sRGB and RGB by assuming that by default a fragment shader is returning RGB values. It then will ask OpenGL to convert these values to sRGB.
Tomaka also addressed core performance issues present in OpenGL when working on how Glium handles drawing process. As such, Glium provides a universal process for drawing, enabling users to free resources that would otherwise bog down CPU usage every time a call was made to OpenGL functions. This prevents unnecessary calls from being made, though Tomaka notes that users should still group their draw calls by textures, program or parameters to help improve system performance.
As memory safety was another pain point for OpenGL users since OpenGL 4, Tomaka introduced memory safety into Glium with Rust by calling
glFenceSync automatically. Tomaka notes that if an error does occur, a panic is triggered encouraging the developer to report the issue. Glium also makes use of the
EGL_KHR_create_context_no_error extensions which allow users to pass flags during context creation. Tomaka notes that while these extensions have not been approved and are currently unsupported, they have potential in other Rust applications. With Glium avoiding OpenGL errors while compiling, users can enable these flags and experience significant performance increases as a result.
Glium is still in development, with an OpenGL GitHub issue outlining new features that have yet to be implemented. Tomaka noted that using Firefox Nightly’s version of rustc should greatly improve speed and performance if one uses the library. Glium is one of the largest single-crate code projects written in Rust, showcasing the language’s strengths as more and more projects move toward Rust for secure code that lacks errors.
Tomaka has developed Glium to improve the OpenGL library, but the project stands as a testament to how one can use Rust to bolster projects once reliant on C++. The Glium GitHub repository offers a roadmap for Glium’s future improvements, along with open issues that the Rust community can contribute to.
Feature image: “Jugend Tree 2D” by Mikael Hvidtfeldt Christensen is licensed under CC BY 2.0.