# Polygon draw order issue

I'm working on a software and I'd to move the rendering API from SlimDX to SharpDX. I've done that but I noticed that the solid polygon are not drawn correctly i.e., even if the polygon is at the back side of another polygon, part of it is visible, also part of the polygon lie inside terrain that is also visible. Below are the images from SlimDX implementation and SharpDX implementation for reference:

I tried to set the Stencil & ZBuffer but there are not working as expected. I've already applied DepthBias to it which worked fine with SlimDX but not with SharpDX.

Please let me know if, I'm missing something or I'm doing something wrong.

Update 1 (Polygon rendering):

// set the render state to device
device.SetRenderState(RenderState.AlphaTestEnable,
true);
device.SetRenderState(RenderState.AlphaBlendEnable,
true);
device.SetRenderState(RenderState.SourceBlend,
Blend.SourceAlpha);
device.SetRenderState(RenderState.SeparateAlphaBlendEnable,
true);
device.SetRenderState(RenderState.DestinationBlendAlpha,
Blend.One);
device.SetRenderState(RenderState.SourceBlendAlpha,
Blend.InverseDestinationAlpha);

// set the render state to the device
device.SetRenderState(RenderState.DepthBias,
-1E-06f);

this.effectVectoring.Begin(FX.None);

// binds the vertex buffer to device data stream
device.SetStreamSource(0,
this.vertexBuffer,
dataGeometry.FillVertexOffset,
stride);
// pass 1 to shader as a pass if it is 3D mode else 0
this.effectVectoring.BeginPass(is3D ? 1 : 0);
// renders the non-index geometric primitives using the data from current input stream
device.DrawPrimitives(SharpDX.Direct3D9.PrimitiveType.TriangleList,
0,
primitiveCount);
// end the pass
this.effectVectoring.EndPass();

this.effectVectoring.End();
device.SetRenderState(RenderState.AlphaBlendEnable,
false);
device.SetRenderState(RenderState.AlphaTestEnable,
false);
device.SetRenderState(RenderState.DepthBias,
0);
device.SetRenderState(RenderState.FillMode,
FillMode.Solid);
device.SetRenderState(RenderState.SeparateAlphaBlendEnable,
false);


Update 2 (Terrain rendering):

device.SetRenderState(RenderState.AlphaBlendEnable,
false);
device.SetRenderState(RenderState.AlphaTestEnable,
false);
device.SetRenderState(RenderState.CullMode,
Cull.Counterclockwise);

device.Indices = this.terrainIB;
device.SetStreamSource(0,
this.terrainVB,
0,
12);
device.VertexFormat = VertexFormat.Position;

// Set texture in effect...

effect.Begin(FX.None);

effect.BeginPass(pass);
device.DrawIndexedPrimitive(PrimitiveType.TriangleStrip,
baseVertexIndex,
0,
vertexCount,
startIndex,
primitiveCount);
effect.EndPass();

effect.End();
device.SetRenderState(RenderState.FillMode,
FillMode.Solid);
device.SetRenderState(RenderState.CullMode,
Cull.None);

• Please show us a Minimal Complete Verifiable Example of the code needed to reproduce this problem, including where you set up your ZBuffer and depth writing/depth comparison tests. With that in hand, we can test potential solutions to ensure they will work for you. Jul 10 '20 at 12:07
• @DMGregory I've updated the code snippet, please take a look. Thanks! Jul 10 '20 at 13:33
• Is there a difference between how you're drawing the terrain, versus the structure on top of it? Jul 10 '20 at 15:24
• @DMGregory it is different, above is the sample source of terrain rendering. Jul 13 '20 at 7:52
• I don't see where your terrain's depth writing is set up. Is it possible it's defaulting to not write to the depth buffer, or that something is clearing depth between drawing the terrain and drawing what's on top? (Or conversely, if you draw the stuff on top first, could the terrain be always failing the z test?) Jul 13 '20 at 11:59