# Using FShaderCache on GearVR

**URL:** <https://forums.unrealengine.com/t/using-fshadercache-on-gearvr/406915>\
**Category:** XR Development\
**Tags:** opengl, GearVR, unreal-engine, Android, question\
**Created:** [October 13, 2017, 8:50am UTC](https://forums.unrealengine.com/t/using-fshadercache-on-gearvr/406915 "2017-10-13T08:50:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon63499829](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon63499829](https://forums.unrealengine.com/u/anon63499829)\
**Post date:** [October 13, 2017, 8:50am UTC](https://forums.unrealengine.com/t/using-fshadercache-on-gearvr/406915/1 "2017-10-13T08:50:26Z")

</div>

Hi,

I’m working on improving the framerate of a GearVR game that suffers from many hitches whenever the environment changes or a new object appears in the player’s field of view. The profiler shows that this is caused by OpenGL compiling and linking the shaders for the objects that just appeared.

I’d like to use Unreal Engine’s Shader Cache feature to solve that, but I’m not sure how to use the various console commands.

Our game uses OpenGL ES3.1. It contains only a few environments and doesn’t use level streaming, so ideally I’d like the engine to pre-compile all useful shaders during loadings, instead of at first use.

From what I read [in the documentation](https://docs.unrealengine.com/latest/INT/API/Runtime/ShaderCore/FShaderCacheState/index.html) I imagined I would proceed like this:

- enabling _r.UseShaderCaching_ and _r.UseShaderDrawLog_ in Development Android builds of the game
- playing through the entire game on the device to populate the cache
- locating the shader cache file on the device and moving it to the development PC inside the Content directory.
- enabling _r.UseShaderCaching_ and _r.UseShaderPredraw_ in final builds of the game
- checking that shader-related hitches are gone.

However, I ran in to several problems.

On 4.17:

- I found no easy way to get the mobile application to save the shader cache file on the sdcard. As we use the Launcher version of the engine, I ended up calling _FShaderCache::ShutdownShaderCache()_ from game code, which triggers a save, then crashes the game.
- launching the game with _r.UseShaderCaching_ and _r.UseShaderPredraw_ enabled crashes at startup in _FShaderCache::InternalLogSamplerState()_. This has been reported here: [Unreal Engine Issues and Bug Tracker (UE-47553)](https://issues.unrealengine.com/issue/UE-47553)

On 4.18 preview 4:

- reading through the code, I can see that two console variables are overriden in FShaderCache’s constructor, so I’m not sure how the shader cache is supposed to work anymore:

What workflow would you suggest to use the shader cache on Android?

Thanks,

---

<div class="post-metadata">

**Author:** ![aussieburger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@aussieburger](https://forums.unrealengine.com/u/aussieburger)\
**Post date:** [October 13, 2017, 9:22am UTC](https://forums.unrealengine.com/t/using-fshadercache-on-gearvr/406915/2 "2017-10-13T09:22:23Z")

</div>

I’ve noticed this as well and would really appreciate a way to remove those hitches!

---

<div class="post-metadata">

**Author:** ![anon63499829](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon63499829](https://forums.unrealengine.com/u/anon63499829)\
**Post date:** [October 17, 2017, 4:48pm UTC](https://forums.unrealengine.com/t/using-fshadercache-on-gearvr/406915/3 "2017-10-17T16:48:19Z")

</div>

OK, I’ve learned a bit more.

On 4.18 preview 4, running the GearVR game with only _r.UseShaderCaching_ enabled by default does create a shader cache file on the sdcard. When running the game a second time with the same configuration, I could notice the large hitches were gone. So it seems the caching mechanism worked and the shaders were compiled at load-time rather than on the fly!

I’m still puzzled, though: if I was able to populate and then use a shader cache using neither _r.UseShaderDrawLog_ nor _r.UseShaderPredraw_, does it mean that on mobile logging and pre-draw are always active, as soon as _r.UseShaderCaching_ is on?

The CPU cost of draw calls is really huge on GearVR, so I really don’t want the shipping version of the game paying the overhead associated with updating the shader cache every draw.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_32acb0c5](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_32acb0c5](https://forums.unrealengine.com/u/anonymous_user_32acb0c5)\
**Post date:** [November 24, 2017, 3:34am UTC](https://forums.unrealengine.com/t/using-fshadercache-on-gearvr/406915/4 "2017-11-24T03:34:12Z")

</div>

Thanks for sharing your findings, Julien. We’re also having the same issue on GearVR.

We found that running the game with r.UseShaderDrawLog=1 causes the game to shutdown/silently crash at startup (it barely passes the android initialization phase). There is no crash dump in the android log output either, which is quite odd. We’re still trying to figure this one out.

We make use of a separate engine system via the setting r.UseProgramBinaryCache=1 (see FOpenGLProgramBinaryCache::Initialize()). This properly caches the compiled shaders and removes the hitches on subsequent runs of the game. However, it does not solve the hitch that happens on “first encounter” when the shader  
actually gets compiled for the first time at runtime. So this is still not a complete solution for us either.

We looked into reusing the output of this binary shader cache but it’s dependent on GPU & driver version (the filename is hashed on GL\_VERSION + GL\_RENDERER) so including these in the final build is not a sensible solution either (it will work only for the device+driver combinations that you have included, and will most likely break when the game is run on newer devices or driver updates).

The OpenGLProgramBinaryCache also seems to be a completely separate system from the FShaderCache mentioned above, which seems to have first been developed and tested for Mac. My guess is that it hasn’t been fully tested on Mobile/GearVR yet (we’re sitll on 4.17 though, so maybe that’s changed in 4.18).

We’re currently in touch with Epic regarding this issue so I will update this thread once we have more information.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_32acb0c5](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_32acb0c5](https://forums.unrealengine.com/u/anonymous_user_32acb0c5)\
**Post date:** [November 27, 2017, 2:25am UTC](https://forums.unrealengine.com/t/using-fshadercache-on-gearvr/406915/5 "2017-11-27T02:25:53Z")

</div>

The new 4.18 release has a fix for the issues above and worked well for us. See Epic’s CL 3612145.

---

<div class="post-metadata">

**Author:** ![aussieburger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@aussieburger](https://forums.unrealengine.com/u/aussieburger)\
**Post date:** [November 28, 2017, 2:32pm UTC](https://forums.unrealengine.com/t/using-fshadercache-on-gearvr/406915/6 "2017-11-28T14:32:14Z")

</div>

So how do we get rid of the initial hitches on first play in 4.18?

Am I correct in saying the steps are as follows?:

- enabling r.UseShaderCaching in Development Android builds of the game
- playing through the entire game on the device to populate the cache
- locating the shader cache file on the device and moving it to the development PC inside the Content directory.
- enabling r.UseShaderCaching and r.UseShaderPredraw in final builds of the game
- rebuild game in Shipping (hoping this cache file is included in the final APK)

Are there any other steps or commands required?

**EDIT: tested this myself and it does indeed work as expected!! Thankyou for sharing this information 🙂**

Epic should really make this setting easier to find like having it in the project settings (or is it already?)
