So by default this will only draw pixels if there’s nothing in the mask for the given layer.
Now the thing is, I’d like it to be an uber-shader so that for any material we can just set a property, and it becomes masked by objects of a given layer. So I can make the comparison function a property as well:
so that we either check against the mask with NotEqual, or we bypass it with Disabled (see Unity - Scripting API: Rendering.CompareFunction.Disabled).
Problem is, contrarily to what the docs say, either way the stencil is used, I can see plainly in GPA:
So my question is, is there a way to disable the Stencil block entirely via properties/keywords? Just want to make sure there are no hidden cost when I don’t want masking.
As far as I know, there’s no way to #define state blocks using keywords…?
I also tried stuff like
Just set everything to the default values. So mainly:
Comp Always
Pass Keep
This is the most you can do and should not have any hidden costs. If Comp is set to Always, there is no stencil read. If Pass is set to Keep, there is no stencil write.
Reviving this thread, wondering if anybody knows for a fact whether there’s a performance impact from having StencilComp=Disabled vs not having the code at all in the shader?
I just did a small profiling test myself and there seems to be up to a .5ms difference (CPU renderering time) in the scene I was testing but I’m not 100% sure if that’s it or not.
Works like a charm. Many thanks!
Further references on the Stencil Comp and Stencil Opetaions, for those who want to control different things than simply enabling / disabling: https://discussions.unity.com/t/602226
Unity 2022.3 (DX11), test using the UI/Default material in UGUI: Set the Stencil Comp to 0 (disabled). In the FrameDebugger, it can be seen that the Stencil Comp is Disabled. However, when capturing frames through RenderDoc, it is found that the StencilEnable in the state set by OMSetDepthStencilState is True.
I tested another simple Unlit shader that supports Stencil for rendering a cube.
Set the stencil comp to 0 and I saw StencilEnable true inside RenderDoc as well.