<p>As far as I know, this sort of thing is mainly about shader compilation. One of the main reasons why a game may experience hitches the first time something renders is that the shaders necessary to render it haven't finished compiling yet, and the driver has to finish that work before the frame can proceed.</p>

<p>A little bit of background. When you write shaders in HLSL, you compile them with Microsoft's HLSL compiler (fxc.exe), which outputs a hardware-independent bytecode. When you call CreateVertexShader etc. in D3D, that bytecode gets passed to the underlying driver, which ultimately compiles it into the machine code for the specific GPU you're running on.</p>

<p>This internal compilation step isn't trivial&mdash;it's a full-blown optimizing compiler. So it can take a little while on a complicated shader. Because of this, when you call D3D's CreateVertexShader, the driver doesn't necessarily <em>finish</em> compiling the shader right then and there; it may add it to a queue, then return control to the application. Meanwhile, a background thread (perhaps more than one) consumes the queue and compiles the shaders. (The driver may also have a disk cache of previously-compiled shaders, so that it doesn't have to redo all this work every time the game is launched.)</p>

<p>This background-thread compilation means the main game thread can get on with other work, e.g. loading other data or building a scene graph or what-have-you, so it's generally a win for overall loading times. But the consequence of it is that the shaders may not all be finished compiling when the game starts rendering. Unfortunately, there is no way with DX11 and earlier APIs for the app to tell when the shaders are done. The best you can do is to render something with the shader&mdash;this effectively makes the app wait for the compiler to finish. That's why games sometimes do this offscreen pre-rendering business.</p>

<p>I don't have any hard numbers handy, but qualitatively, one can reduce the impact of shader compilation by front-loading it during loading. Create all your shaders first, then the compiler will run through those while you load everything else. However, with real-time streaming  (e.g. open-world games) it can still be quite difficult to ensure no hitches.</p>

<p>It's also worth noting that there are some cases where state changes can cause a shader recompile. Things like vertex buffer formats, render target formats, and shader linking details (e.g. re-using the same pixel shader with different vertex shaders, or with tessellation shaders) can potentially cause the driver to have to generate different versions of the shader under the hood. The details vary by vendor and hardware. This isn't necessarily a full from-scratch recompile&mdash;often it's just sticking on some prologue or epilogue code&mdash;but it's something to be aware of.</p>

<p>This is one place where the new APIs (DX12, Metal, and Vulkan) are in a good position to solve the problem. These APIs have "pipeline state" objects where all shader stages and all possibly relevant states are rolled up into a single object, and when you create one of these, the driver has all the information it needs to actually and completely finalize all the shaders. DX12 also makes shader caching explicit. Also, at least for NVIDIA DX12 drivers (I don't know about others), shader compilation is done fully inside the pipeline state creation method, and the app can use multiple threads itself in order to parallelize shader compilation and other work. So, the practice of offscreen pre-rendering to ensure shaders are finalized should no longer be necessary in DX12.</p>

CC BY-SA 4.0
2020-04-22T03:48:10.820Z
A few years passed since this (excellent) answer, and one can find that this shift of hitches from the first drawcall to pipeline state creation wasn't a silver bullet either, because the game may not know all the state in advance (especially a highly varied / moddable game) to create it early enough...
RCL
CC BY-SA 3.0
2015-10-17T07:29:49.867Z
2015-10-17T20:38:19.193Z
2015-10-17T20:38:19.193Z
Nathan Reed
<p>In PC game development, after loading models, textures, shaders, etc in a loading screen, some games will render the models once to an off screen target to make sure the driver and gpu have done all the work required to keep there from being a hitch the first time the models render.</p>

<p>Does anyone know how much that actually helps? Does it only affect certain cards and drivers?</p>

CC BY-SA 3.0
2015-10-18T05:49:56.820Z
Is this per-frame? As in a prepass to fill the depth buffer?
geometrian
CC BY-SA 3.0
2015-10-18T14:16:14.810Z
Nope, done in the loading screen. Not talking about a depth only render!
Alan Wolfe
<gpu><real-time><direct3d><directx11>
Does prerendering actually help performance?
Alan Wolfe
# Does prerendering actually help performance?

(Asked by: username_0 on 2015-10-17)

In PC game development, after loading models, textures, shaders, etc in a loading screen, some games will render the models once to an off screen target to make sure the driver and gpu have done all the work required to keep there from being a hitch the first time the models render.

Does anyone know how much that actually helps? Does it only affect certain cards and drivers?

**Tags:** gpu, real-time, direct3d, directx11

> **Comments:**
> **username_1**: Is this per-frame? As in a prepass to fill the depth buffer?  
> **username_0**: Nope, done in the loading screen. Not talking about a depth only render!  

---

## 1 answers

### Answer by username_2 on 2015-10-17. Score: 11 | Accepted answer

As far as I know, this sort of thing is mainly about shader compilation. One of the main reasons why a game may experience hitches the first time something renders is that the shaders necessary to render it haven't finished compiling yet, and the driver has to finish that work before the frame can proceed.

A little bit of background. When you write shaders in HLSL, you compile them with Microsoft's HLSL compiler (fxc.exe), which outputs a hardware-independent bytecode. When you call CreateVertexShader etc. in D3D, that bytecode gets passed to the underlying driver, which ultimately compiles it into the machine code for the specific GPU you're running on.

This internal compilation step isn't trivial—it's a full-blown optimizing compiler. So it can take a little while on a complicated shader. Because of this, when you call D3D's CreateVertexShader, the driver doesn't necessarily *finish* compiling the shader right then and there; it may add it to a queue, then return control to the application. Meanwhile, a background thread (perhaps more than one) consumes the queue and compiles the shaders. (The driver may also have a disk cache of previously-compiled shaders, so that it doesn't have to redo all this work every time the game is launched.)

This background-thread compilation means the main game thread can get on with other work, e.g. loading other data or building a scene graph or what-have-you, so it's generally a win for overall loading times. But the consequence of it is that the shaders may not all be finished compiling when the game starts rendering. Unfortunately, there is no way with DX11 and earlier APIs for the app to tell when the shaders are done. The best you can do is to render something with the shader—this effectively makes the app wait for the compiler to finish. That's why games sometimes do this offscreen pre-rendering business.

I don't have any hard numbers handy, but qualitatively, one can reduce the impact of shader compilation by front-loading it during loading. Create all your shaders first, then the compiler will run through those while you load everything else. However, with real-time streaming (e.g. open-world games) it can still be quite difficult to ensure no hitches.

It's also worth noting that there are some cases where state changes can cause a shader recompile. Things like vertex buffer formats, render target formats, and shader linking details (e.g. re-using the same pixel shader with different vertex shaders, or with tessellation shaders) can potentially cause the driver to have to generate different versions of the shader under the hood. The details vary by vendor and hardware. This isn't necessarily a full from-scratch recompile—often it's just sticking on some prologue or epilogue code—but it's something to be aware of.

This is one place where the new APIs (DX12, Metal, and Vulkan) are in a good position to solve the problem. These APIs have "pipeline state" objects where all shader stages and all possibly relevant states are rolled up into a single object, and when you create one of these, the driver has all the information it needs to actually and completely finalize all the shaders. DX12 also makes shader caching explicit. Also, at least for NVIDIA DX12 drivers (I don't know about others), shader compilation is done fully inside the pipeline state creation method, and the app can use multiple threads itself in order to parallelize shader compilation and other work. So, the practice of offscreen pre-rendering to ensure shaders are finalized should no longer be necessary in DX12.

> **Comments:**
> **username_3**: A few years passed since this (excellent) answer, and one can find that this shift of hitches from the first drawcall to pipeline state creation wasn't a silver bullet either, because the game may not know all the state in advance (especially a highly varied / moddable game) to create it early enough...  

---

