Why would I want to Disable the Stencil and Depth?
In the latest version of Unity 5.5.x, there is a new option in Player Settings to disable the stencil and depth:
I've tried disabling but noticed no difference in either performance or memory utilization on my iOS device. Why would a person want or need to disable?
Thanks, Manny
$$anonymous$$y guess would be that it is a performance optimisation on the rendering side in the case that you don't require the depth or stencil buffers. OpenGL has a nice article explaining what these are used for: https://open.gl/depthstencils
The unity manual only says: "Check this box to disable the depth and stencil buffers."
It seems like the depth buffer would be required in most scenes that need to render multiple objects though, so perhaps someone else could give examples of use cases.
Answer by Selzier · Jan 11, 2017 at 12:27 AM
Z-buffering is a way of keeping track of the depth of every pixel on the screen. https://open.gl/depthstencils
If your game does not use shaders which utilize the depth or stencil buffers, disabling them could (should) reduce the amount of workthe GPU/CPU must do and also free up a bit of memory.
Oculus recommends disabling this for Mobile VR games: "Check the Disable Depth and Stencil checkbox in the Resolution and Presentation pane in Player Settings."* https://developer3.oculus.com/documentation/game-engines/latest/concepts/unity-mobile-performance-intro/
Answer by YHS · Mar 04, 2018 at 08:20 AM
Only thing I know is, if you have canvas UI image and object that want to sort with UI, you might not want to disable this. With out depth and stencil buffer it will cause weird sorting result after you build an app. for example, in editor , cube is in front of UI image, after you build the app, cube is behind UI image if without depth and stencil buffer enable.
3D & UI stuff are not sorted using ZBuffer. They are using different rendering passes, that are sorted together. See Canvas & Camera's layers.
After a lot of optimization work on my app, I had a problem with my android build that was along the lines of what YHS is talking about. I had to search YHS's comment to confirm that it was "disable stencil and depth" that caused the problem.
I enabled it, and built it again to android. The problem is now gone.
YHS may or may not be technically correct, but the problem I had was on very similar lines to what he is saying and if he didn't say this, I'd still have the problem.
Thanks YHS
Answer by rjenman · Jun 04, 2021 at 04:47 AM
After building to android this caused my app to have a number of very serious problems, including but not limited to: -A UI text box was for some reason activated on the main scene, with text in it showing that wasn't supposed to be there unless a condition was satisfied. That condition wasn't satisfied at all. -Sprites were supposed to be masked inside a specific shape. The sprites, instead, were spilling out all over the screen and ignoring the mask.
It appeared that this caused some weird problem with sorting, images, and seemingly even more than that.
I heard about this from another article. That article is, by the way, extremely informative and helpful (link below). The article was talking about some optimization that doesn't involve "disabling it altogether." I think that there was (or is) some way to optimize apps by doing something along the lines of "disabling it" but without actually disabling it altogether. In the past this was allowed in Unity but now you only have the option to disable it. So, it seems the powers that be have now deemed this entire thing to be not worth including in Unity anymore, and my conclusion is that the "disabling" button is just some weird, extreme option that may help you in some specific weird circumstance, and is only included still in Unity for completeness. I am in no way an expert on this topic and the article I was reading is linked here:
The article: https://divillysausages.com/2016/01/21/performance-tips-for-unity-2d-mobile/