Jump to content
  • Advertisement
Sign in to follow this  

strategy for automatic shader compilation caching

This topic is 2756 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I am trying to reduce the load time of my game.

Right now, I compile all the shader at runtime, when the game is started, which takes quite some time.

I use D3DX10CompileFromMemory to compile my shaders.

I am thinking about writing the content of the blob returned by this function on disk, the first time the shader is being compiled, and re-use it next time the game is started.

This is very straightforward.

However, what I also would like to achieve, is to automatically detect any change in the shader source, and re-compile the shader if any change occured.

So, my initial idea was to save two files, the source code version used for compilation, and the resulting compiled shader blob.
At runtime, I would load the shader source file, and compare it with the source version used for compilation. If there is no difference, i can use the pre-compiled blob, otherwise I compile the shader again.

However, this approach gets a bit more complicated when considering "#include" directives, along with the usage of a "ID3D10Include" object.

The only way I see would be to manually resolve all the #include" directives, in order to get a "#include"-free HLSL source code I could use to compare the current version with the pre-compiled cache version.

Is there any other option I haven't seen ? How do you guys handle this problem ?

Share this post

Link to post
Share on other sites
You could use a hierarchical cache - a tree (or more accurately a forest) of the dependencies. If any of the roots (included files) of any leaf (source files that include other files) change, you need to invalidate all the leafs of that root. The tree structure could be precalculated, and it is not very difficult to parse include directives to build the tree to begin with.

That said, if you are using custom ID3D10Include objects, the hierarchy determination could become unreasonably complex.

Share this post

Link to post
Share on other sites
Hi Nick,

thanks for your answer. I think I have found a good trade off solution.

Basically, I am resolving all includes by myself. This results in a big HLSL source code text with no #include directive anymore.

I then check the shader cache (which contains a pair of "hlsl source code" / "compiled byte code" files for each original shader).

If an entry already exists in the cache, I compare the current source code text with the "hlsl source code" file. If no change could be found between the two strings, I assume I can safely load the existing "compiled byte code" file and use it.

Otherwise the shader gets compiled, and the result saved on disk ("hlsl source code" / "compiled byte code" files).

It seems to work pretty well.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!