slipster216 / VertexPaint

Unity 5.3 Vertex Painter
Other
904 stars 153 forks source link

Very slow compile time for SplatBlend_5Layer shaders #24

Closed fdan closed 7 years ago

fdan commented 7 years ago

We have some SplatBlend_5Layer shaders in a project, which when opened by a user for the first time take a very long time to compile when the project is opened (to the Library/ShaderCache directory).

It can take over half an hour to do this for a small number of such shaders. I'm curious if we are doing something wrong, or if this is expected behaviour?

fdan commented 7 years ago

I just tried "show generated code" in Unity, it took a while, and when it opened in Visual Studio, the generated shader had over a million lines...that sounds horribly wrong.

slipster216 commented 7 years ago

It's not wrong at all, actually. When you use a shader_feature keyword in Unity, it basically writes out every possible combination of shaders that those keywords being turned on/off can produce. This is so if you set them at runtime, the shader will be pre-compiled for you. As you add more features, this number grows exponentially, and Unity has a number of these internally as well. The standard shader included in Unity produces something like 20,000 actual shaders when you include all of those internal features (mostly for lighting) as well.

The shaders included with this package optimize out various features by this mechanism, but obviously this creates a lot of shader variants. I got around this issue in MegaSplat by writing my own shader compiler, which rather than using shader_features, re-generates the shader code every time you change an option.

If you want to optimize the compile time of these shaders, you can remove the shader_feature keywords and hardcode the features you want via #define FEATURE_NAME directives. But then you won't be able to turn those features on and off and get an optimized shader. Your other option is to remove the shader features and #if directives around the various blocks, but this will mean you are running that code even if it's not used, which is less performant.

slipster216 commented 7 years ago

Also of note, use of shader variant collections is the Unity mechanism you can use to strip unneeded variants from your build..

inspirednotion commented 7 years ago

So it might be worth getting mega slat I’m guessing to resolve this?

slipster216 commented 7 years ago

MegaSplat is a different system- designed for splat mapping hundreds of textures. If you just need these shaders, you can remove versions you are not using or shader_feature keywords you don't need to speed up the compile times.

fdan commented 7 years ago

I tried commenting out some unecessary features, and removed some unused shaders, and the compile time is now much faster, barely noticeable, cheers!