Pdr
Topic Author
Posts: 8
Joined: 29 Sep 2025, 16:31

Stencil confusion

13 Oct 2025, 10:37

Hi

I'm integrating Noesis in our own engine currently, and are a bit confused over how the stencil buffer usage is supposed to function.
When opening up a combo-box a new Offscreen rendertarget, along with a stencil buffer is created.

Later during OnscreenRender I get first one DrawBatch which wants to draw with Stencil mode Equal_Incr, stencil ref 0, followed by a couple of draws with Equal_keep with stencil ref 1. That makes perfect sense of course, it draws the combo-background to increase the stencil ref to 1 and and draws the contents with stencil masking. check.

But there is no call to SetRenderTargets at all during the OnscreenRender scope, when I'm beginning OnscreenRender I set the rendertarget to our backbuffer as output as it says in the documentation, at that point I don't set any depthstencil target at all, I figured that was going to be handled in the OffscreenRender pass somehow as that's the only thing that sets RenderTargets (and depthstencil). So I'm just a bit confused over how it's supposed to work here, am I supposed to set and clear another depth stencil target for the OnScreenRender myself, or? We already have one of course for the rest of the frame, but in order to use it for this it would need to be cleared prior to rendering, which is an otherwise uncessesary and expensive operation in the frame.
I'm also confused over the usage of the offscreen target which is created and used during OffscreenRender, as it's never bound as input on any pass during Onscreen and seems to be completely unused.

As seen in the screenshots I'm using the xaml from one of the samples.

One thing I've yet to implement is the ResolveRendertarget function, and I really don't see the point of it currently. It is being called during OffscreenRender. But what is the purpose to resolve the Offscreen-target? That is done implicitly anyway if it was ever bound as input. No?
Attachments
Screenshot 2025-10-13 103300.jpg
Keep - test pass
Screenshot 2025-10-13 103219.jpg
Incr pass
 
User avatar
jsantos
Site Admin
Posts: 4825
Joined: 20 Jan 2012, 17:18
Contact:

Re: Stencil confusion

13 Oct 2025, 11:01

But there is no call to SetRenderTargets at all during the OnscreenRender scope
The onscreen render pass is designed to be inlined within your main render pass. The most efficient approach, consistent across renderer implementations, is to assume that the current render pass already has a stencil buffer attached and that it’s cleared to zero before UI rendering begins.
If your engine already uses a stencil buffer for other rendering tasks, clearing it should be a very fast operation and not a significant performance cost.
I'm also confused over the usage of the offscreen target which is created and used during OffscreenRender, as it's never bound as input on any pass during Onscreen and seems to be completely unused.
The offscreen target is used, but not necessarily every frame. It’s only required when certain visual effects are active, specifically, when rendering elements that have opacity < 1, or when effects like Blur or DropShadow are involved.

For example, the following XAML will always trigger offscreen rendering:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.Background>
        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
            <GradientStop Offset="0" Color="White"/>
            <GradientStop Offset="1" Color="Blue"/>
        </LinearGradientBrush>
    </Grid.Background>
    <Viewbox>
        <Grid Margin="40">
            <Rectangle Fill="Red" Width="200" Height="120"/>
            <Grid Width="280" Height="80" Background="Yellow" Opacity="0.6">
                <Rectangle Fill="Lime" Width="300" Height="50" Opacity="0.8" RenderTransformOrigin="0.5,0.5">
                    <Rectangle.RenderTransform>
                        <RotateTransform Angle="90"/>
                    </Rectangle.RenderTransform>
                </Rectangle>
            </Grid>
        </Grid>
    </Viewbox>
</Grid>
But what is the purpose to resolve the Offscreen-target?
That depends on the graphics API you’re using. The resolve step is needed to transition the resource from a render target to a texture that can be sampled later.
For example, in our D3D12 renderer implementation (D3D12RenderDevice.cpp), this is handled via resource barriers to move from D3D12_RESOURCE_STATE_RENDER_TARGET to D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE. This transition ensures that the offscreen target can be safely and correctly read as an input texture in subsequent rendering passes.
 
Pdr
Topic Author
Posts: 8
Joined: 29 Sep 2025, 16:31

Re: Stencil confusion

13 Oct 2025, 11:08

That depends on the graphics API you’re using. The resolve step is needed to transition the resource from a render target to a texture that can be sampled later.
For example, in our D3D12 renderer implementation (D3D12RenderDevice.cpp), this is handled via resource barriers to move from D3D12_RESOURCE_STATE_RENDER_TARGET to D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE. This transition ensures that the offscreen target can be safely and correctly read as an input texture in subsequent rendering passes.
Ok, thanks. Then it's not needed for us, as our render pipeline will automatically transition any render targets bound as input to the appropriate state. Thanks for clearing that up.
It was just that I saw in the D3D12Renderer that it also did any actual ID3D12Commanlist::ResolveSubresourceRegion, which I didn't quite see the point of. But maybe that's something for Tiled implementations only?
 
User avatar
jsantos
Site Admin
Posts: 4825
Joined: 20 Jan 2012, 17:18
Contact:

Re: Stencil confusion

13 Oct 2025, 11:21

It was just that I saw in the D3D12Renderer that it also did any actual ID3D12Commanlist::ResolveSubresourceRegion, which I didn't quite see the point of. But maybe that's something for Tiled implementations only?
ResolveSubresourceRegion is used in our D3D12 renderer when multisampled render targets are involved (although that’s quite uncommon).

Its purpose is to resolve a multisampled surface into a standard texture resource so that it can be used later as a shader input.
 
Pdr
Topic Author
Posts: 8
Joined: 29 Sep 2025, 16:31

Re: Stencil confusion

13 Oct 2025, 11:31

ResolveSubresourceRegion is used in our D3D12 renderer when multisampled render targets are involved (although that’s quite uncommon).

Its purpose is to resolve a multisampled surface into a standard texture resource so that it can be used later as a shader input.
Right, that explains why I'm not familiar with that. Never used multisampled targets before :)

Who is online

Users browsing this forum: No registered users and 6 guests