Catching and Fixing Runtime Shader Compilation on PC

I’m a few weeks away from shipping my VR title and while optimizing my game Im noticing some hitches that are caused by shaders triggering createGPUProgram.

Some strategies I’ve tried to solve the issue:

1. Create a Shader Variant Collection By Playing The Game

I played through my game and allowed Unity to track the used shaders and then I saved them. I then Warm them up when the scene is loaded.

Result: Not all of the shaders are included in the Shader Variant Collection. For example, all the variants for the Standard Shader, are not included. I noticed that the ones related to a directional light causes a hitch when the player goes outside for the first time. Not sure why these are not included in the tracked shader variants.

2. Create a Shader Variant Manually

I played my game with the profiler open and paused it once I saw hitches. I then noticed in the timeline view the createGPUProgams in the profiler. If you click on it, shows you the keywords used for the shader that was created. I then created an empty ShaderVariantCollection and added that shader in so I can select the variants.

The Issue: The Standard Shader shows over 770,000 variants! I then put in all of the keywords that I saw in the profiler…this got it down to “only” 8,000 variants. So out of the roughly 20 variants that my game is creating, I need to somehow figure out which 7,980 variants are not needed, which is impossible…especially because UI doesn’t show all of them…only a few at a time.

3. Place a 2nd Camera in the scene that rotates in place

As a hack, I tried creating a 2nd camera and placing it into the scene to force createGPUProgram to trigger before I un fade the players camera.

Result: This doesn’t work, as the camera doesn’t seem to trigger anything with Single Pass Stereo Instanced. It seems that Unity doesn’t allow a second camera in the scene to use Single Pass Stereo Instanced. Therefore, I need a way to force the camera into a mode to use Single Pass Instanced to work with this camera.

Does anyone have an idea how to solve this issue?

I’m currently on Unity 2018.4.32 and using Single Pass Instanced.

Any help would be greatly appreciated.

Ok, so I learned a bit more about what is going on. It looks like createGPUProgram is compiling shaders with the keyword “STEREO_INSTANCING_ON”.

The problem is when I look at the Standard Shader, it doesn’t have “STEREO_INSTANCING_ON” inside of it as a keyword?!!

I found this out when I made a very crude tool to manually construction Shader variants and warm them up. “STEREO_INSTANCING_ON” doesn’t seem to be a valid keyword…so not sure what is going on.

Is anyone else having issue with these Standard Shader and Standard Shader (Specular) causing hitching in their game?

7536344--930809--createGPUProgram.png