- Home /
Improving custom shader performance?
Hello! I'm having an issue with a custom shader we've written for liquids in our game, and it's becoming a bit of a headache.
Basically, we've got 3 liquids in our game: Lava, water, and ink. We have a quad set up over the extents of each of our levels which has the liquids shader on it to display that level's liquids. The liquids are rendered based on a render texture from a secondary camera above each level that looks at a specific "liquid mask" layer under the level (Think Super Mario Sunshine style). However, this isn't what seems to be impacting fps...
When I was writing the shader, I kept hitting the 64 arithmetic instruction limit when trying to render all 3 liquids in one pass, so I tried to find a way to split the shader into 3 parts. I eventually found that I could just make multiple 'CGPROGRAM's and use one for each of the liquids. This let me do exactly what I needed to do, but cut our FPS by about 65% on our target platform (mobile).
I tried adding a "LavaEnabled", "WaterEnabled", etc. property to the shader (which will only compute the surface emission if the value is true) and turning them on and off, but the profiler shows no significant FPS boost. The only thing that seems to get FPS back is to comment out some of the CGPROGRAM blocks in the shader.
Is there a better way to skip CGPROGRAM calls in the shader when unnecessary?
(Edit: Is there a better way to make a multi-pass surface shader than by making multiple 'CGPROGRAM's?)
Why does the liquids mesh take so long to render when math isn't being called in the "surf" function of the shader (e.g. when the "Enabled" values are false)?
I'm a total beginner at shaders, so let me know if I'm doing something stupid! haha. Any help is appreciated.
Thanks in advance!
Any help? Sorry to bump but I've run out of ideas and need some input :(
i dont know too much about Shader writing .But just because you have moved the math part to Cg files doesnt mean it is not being called or optimized.
When ever an object is not enabled unity automatically stopy rendering shader passes for them.
Tip
try to move the math calculation to a Script and use System.$$anonymous$$ath to do the calculation (if posssible of course)
Curiosity what math function have you written Cause if its just a simple render as Lit there are many alternates(Very Very Efficient ones)
Well the problem is, even if I'm not doing any math at all, multiple CGPrograms is slowing down the shader. If I just have an empty CGProgram with just an empty "surf" function, it's nearly as bad as one with my math running. Only when I comment out the entire thing does my speed come back. So I guess I need to figure out how I can do a multi-pass surface shader in a more efficient way.
Ins$$anonymous$$d of using multiple CGPrograms, use multi_compile:
https://docs.unity3d.com/Documentation/Components/SL-$$anonymous$$ultipleProgramVariants.html
Hmm... That seems like it would work if I were trying to make a shader that could produce any one of the 3 liquids (lava, water, ink), but I'm not sure how I could use that to produce all 3 simultaneously.