Stuttering… a word that has become synonymous with a painful experience for PC gamers. Today we are diving into what shader compilation is, why it leads to hitching, what Unreal Engine and DX12 have to do with this issue, and how developers can address it.
What are shaders?
- A shader is a program that calculates various attributes of rendered graphics. It is basically a set of instructions that the CPU sends to the GPU, which affect all pixels on the screen and transform the geometry or visual traits of on-screen objects and materials.
- To get a general idea of how shaders work, you can watch a short but informative YouTube video by ProbablyMonsters senior software engineer Anne McLaughlin aka TheHappieCat.
Creating a water depth shader in Unreal Engine 4 (Source: Ben Cloward)
- Video games use dozens of shaders of varying types and complexity per frame — vertex, pixel, tessellation, ray tracing, etc. — to calculate and control parameters such as position, color, lighting, reflectivity, and texture coordinates.
- Modern engines, including Unreal and Unity, have node-based tools for building shaders with no code. This allows developers to define the final appearence of different surfaces and effects in real time by visualizing relationships between operations, applying certain settings, and then turning graphs into compiled shaders.
An example of the Master Stack node graph in Unity
What is shader compilation and why is it different on consoles and PC?
- For the hardware to “understand” human-written code, it needs to be translated into binary code. Simply put, that is what compilation is all about, and that is why shaders must be compiled to run on a specific GPU.
- When making a game for consoles, developers know exactly what’s inside every Xbox Series X, PlayStation 5, or other device. So shaders are pre-compiled and can be shipped with the game.
- Each player’s PC, on the other hand, may have its own configuration and combination of CPU, graphic cards, and other hardware. And this is where the main problems arise.
Inside the PS5 (Source: PlayStation)
- In most cases, shaders in PC games are compiled on the fly, putting more stress on the processor. But, as explained by popular tech YouTuber Linus Sebastian, “Once a shader gets recompiled, it is saved to your SSD or to your hard drive. So your CPU won’t have to do that extra work ever again.”
- The bad news is that if the player changes their PC configuration or upgrades GPU drivers, the shaders will have to be compiled again.
Shader compilation issues result in FPS drops in Elden Ring on PC (Source: Digital Foundry)
Why does shader compilation cause stuttering?
- Stuttering is an effect caused by delays between frames. This results in laggy and uncomfortable gameplay, sometimes even if your PC specs meet all system requirements.
- There are many issues that can lead to stuttering, but shader compilation is probably the most common in modern titles. If shaders are not pre-compiled, each time the player encounters a new object or effect, the game may stutter while the shader is loading and rendering a particular scene.
- That’s why replaying the same level or returning to the same location after all the shaders are compiled will give you a much smoother and more stable experience.
Running through the same place in Chernobylite twice (Source: Hardware Lab)
- To eliminate or minimize stuttering, a growing number of games today offer players to compile shaders in the main menu on first launch. This takes time — from a couple to tens of minutes — but usually really helps to solve the problem.
- As Alex Battaglia of Digital Foundry pointed out in a video about PC ports issues, shaders can also be compiled asynchronously. This increases the CPU load, but allows shaders to be compiled in the background or during the loading process.
Building shaders on first launch of Atomic Heart
Are Unreal Engine and DX12 the root of all evil?
- If you look at titles with shader compilation issues, most of them are made with Unreal Engine 4. Epic Games’ engine is definitely a great development framework, but the general trend is clear.
- The PC version of The Callisto Protocol is one of the most infamous examples of poorly optimized UE4 games in recent years. At launch, it suffered from sever stuttering, making it almost unplayable.
Massive stutters in Callisto Protocol due to shader compilation issues (Source: Digital Foundry)
- The main issue is the way shaders are compiled in UE4. The engine allows to prevent stuttering by caching shaders using the Pipeline State Object (PSO) Caching tool. It creates a list of all the shaders used in the project and then speeds up the compiling process when they first appear in the game.
Devs must run their UE4 build to generate the shaders used at runtime and then apply a special file created by the PSO Caching to it
- But that unfortunately doesn’t solve all the problems. As Hi-Fil Rush lead graphics programmer Kosuke Tanaka explained to PC Gamer, “UE4 misses some cases such as certain lighting shader combinations, computer shaders, Niagara VFX, and these may still cause hitches.”
- Another big issue here is modern graphics APIs, Microsoft’s DirectX 12 and Khronos Group’s Vulkan. Unlike their older versions, they demand more attention and additional work from developers. It is basically a double-edged sword: devs can use new features of these APIs for better performance and visuals, but the lower-level nature of DX12 and Vulkan requires more manual control.
- Although modern graphics APIs can be powerful tools in the right hands, they are much more difficult to use (since most studios still can’t handle multiple CPU cores properly). So this may result in poor optimization, especially when devs don’t pay much attention to shader compilation and other issues beforehand.
It is time for developers to treat PC ports and gamers better
- Almost every time a new big-budget title releases on PC, it comes up with optimization problems — from stuttering and visuals glitches to bugs and crashes. Of course, not every launch is as botched as Cyberpunk 2077 on past-gen consoles, but the general trend is depressing.
- The stuttering issue is far more complex and can’t be resolved at the click of a finger. However, consumers have every right to be angry when AAA publishers charge $70 for their games but ship them in a state that sometimes ruins the entire experience.
- According to Digital Foundry, the shader compilation should never be tied to the frame being currently rendered for display, because “games will stutter fiercely for every user regardless of their hardwre if shaders are set to compile at render time.”
- It is also time for every developer whose title might have these issues to enable shader pre-compilation by default. Players should be able to cancel it at any time and start playing immediately, but they also must be warned about the possible negative performance impact of canceling this process so that they can return to it later.
- This is something that users have been asking for on Reddit and forums for months now. Pre-compilation, along with better optimization work, can save studios from seeing a “Mostly Negative” rating on Steam at launch, trying to win back players’ trust with patches and quick fixes.
- Transparency and better communication, where game creators and publishers keep users informed about all the issues and try to make their experience as smooth as possible, should be the new standard. Hardly anyone likes to guess whether The Last of Us or any other anticipated title will let them through the shader building process or crash after paying $70 for it.
- As great as Elden Ring is, it is sad to see that FromSoftware still hasn’t fixed the stutter issue more than a year after launch (and it probably never will 100% fix it). And somewhat funny, it runs much smoother on Steam Deck. Yes, it has fixed hardware, unlike classic PCs, but Valve deserves credit for finding a solution by pre-caching and pre-compiling shaders on its servers.
- So it is also up to Epic Games, Microsoft, and other companies to improve their engines and tools to help developers better optimize their products. Otherwise, PC gaming risks becoming a dreadful and unpleasant experience.
Random stutters in The Witcher 3 next-gen update on PC (Source: Neon Knight)