Stencil Buffer

I’m trying to implement Alex Vlachos very clever technique of using a stencil mesh to write in the stencil buffer to save precious GPU cycles in my VR game. I’ve already done a lot of optimization to try and optimize fillrate for my game but this seems like the ideal solution to save a huge amount.

Here is the link of the technique for more info:

The whole thing is interesting but the technique I’m talking about is on slide 59.

However, I’m at a loss on how to write in the stencil buffer in UE4. I’ve been searching RHI commands and found some related to the stencil buffer but I don’t know exactly where to start to try and implement that.

If anyone can give me an hand that would be greatly appreciated.

Thanks!

Mick

Also, would that still be coherent with Time Warping ? Maybe not using a perfect-fit stencil mesh would work better?

Bump? Anyone? Epic?

I dare bump again :slight_smile:

To use stencil you have to bind a depthstencil target with something like this
SetRenderTarget(RHICmdList, FTextureRHIRef(), GetCubeShadowDepthZSurface(ShadowResolution));

Then clear
RHICmdList.ClearMRT(bClearColor, NumClearColors, Colors, bClearDepth, Depth, bClearStencil, Stencil, FIntRect());

Then render some geometry to setup the stencil mask, with something like this

// Set stencil to one.
RHICmdList.SetDepthStencilState(TStaticDepthStencilState<
false,CF_DepthNearOrEqual,
true,CF_Always,SO_Keep,SO_Keep,SO_Replace,
false,CF_Always,SO_Keep,SO_Keep,SO_Keep,
0xff,0xff
>::GetRHI(), 1);

In order to get fill rate savings during the base pass, you need to set up that stencil mask before the base pass, then setup stencil state to test against 1 with SetDepthStencilState.

Thanks, I’ll give this a try next week and let you know how it went!

Hi :

I need to render character vision bubbles on screen, and the bubbles may be partially displayed. Can you provide more details about using Stencil buffer with C++?

The following information would do big help:
- What class should I inherit while adding code to the project
- A block of code sample

Thanks

Just talked with Zak. Unfortunately, Unreal doesn’t support he feature I expected now. Will wait and see.