Stencil Buffer

Started by
1 comment, last by Matthew Meeks 11 years, 2 months ago

I'm having issues understanding how stencil buffers work. I used them a few times in XNA but there appears to be many differences. The comparison test always seems to pass and the objects are always drawn.

Here are my depth states:


DepthStencilStateDescription dsStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled = true,
                IsStencilEnabled = false,
                DepthWriteMask = DepthWriteMask.All,
                DepthComparison = Comparison.Less
            };

            DepthState = DepthStencilState.FromDescription(Device, dsStateDesc);

            //===

            DepthStencilStateDescription dsaStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled = true,
                IsStencilEnabled = true,
                FrontFace = new DepthStencilOperationDescription()
                {
                    Comparison = Comparison.Greater,
                    FailOperation = StencilOperation.Keep,
                    PassOperation = StencilOperation.Increment,
                    DepthFailOperation = StencilOperation.Zero
                },
                BackFace = new DepthStencilOperationDescription()
                {
                    Comparison = Comparison.Always,
                    FailOperation = StencilOperation.Keep,
                    PassOperation = StencilOperation.Increment,
                    DepthFailOperation = StencilOperation.Zero
                },
                StencilWriteMask = 0,
                StencilReadMask = 1,
                DepthWriteMask = DepthWriteMask.All,
                DepthComparison = Comparison.Less
            };

            addDepthState = DepthStencilState.FromDescription(Device, dsaStateDesc);

            //===

            DepthStencilStateDescription dscStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled = true,
                IsStencilEnabled = true,
                FrontFace = new DepthStencilOperationDescription()
                {
                    Comparison = Comparison.Equal,
                    FailOperation = StencilOperation.Keep,
                    PassOperation = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Keep
                },
                BackFace = new DepthStencilOperationDescription()
                {
                    Comparison = Comparison.Equal,
                    FailOperation = StencilOperation.Keep,
                    PassOperation = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Keep
                },
                StencilReadMask = 1,
                StencilWriteMask = 1,
                DepthWriteMask = DepthWriteMask.All,
                DepthComparison = Comparison.Less
            };

            compareDepthState = DepthStencilState.FromDescription(Device, dscStateDesc);

When I draw:

  • Clear the depth/stencil buffer
  • set the first depth state
  • draw the world
  • set the second depth state
  • draw the cube to render the second view point on
  • set the third depth state
  • draw the world from the second view point

If this helps these are the XNA depth states I'm trying to mimic:


        DepthStencilState addIfPortal = new DepthStencilState()
        {
            StencilEnable = true,
            StencilFunction = CompareFunction.Always,
            StencilPass = StencilOperation.Increment

        };
        DepthStencilState checkPortal = new DepthStencilState()
        {
            StencilEnable = true,
            StencilFunction = CompareFunction.Equal,
            ReferenceStencil = 1,
            StencilPass = StencilOperation.Keep
        };

I appreciate any help and thanks for your patience smile.png

Advertisement

Are you setting your DepthStencilView with device.OutputMerger.SetTargets() ?

Yes I am

This topic is closed to new replies.

Advertisement