Tim Dierks
14 posts / 1 project
Wrong depth sorting for two directions (D3D11)
Edited by Tim Dierks on
Hello,

I'm trying to play around with direct 3D 11, as I previously only used opengl.
But I have this depth sorting bug, which I am unable to solve atm and I wasted quite some time already trying to figure it out.
Here is a short showcase of what is happening:

It seems the sorting is in reverse order or something. But I cannot find a solution that worked by just searching the web.
Maybe I am doing something basic wrong.

Here is the code:

I assume I'm just missing something basic.

Ah, almost forgot. The code is based/used from here: https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052

Thanks in advance for the help!
Mārtiņš Možeiko
2548 posts / 2 projects
Wrong depth sorting for two directions (D3D11)
I don't think that has anything to do with depth sorting. It looks like polygons are culled because they are classified as back facing. Try disabling back face culling in RasterizerDesc.CullMode. Or fix the vertex order in polygon for it to not be back face.
Tim Dierks
14 posts / 1 project
Wrong depth sorting for two directions (D3D11)
Edited by Tim Dierks on

I tried your suggestions but it didn't seem to work.
When I use D3D11_CULL_NONE it looks exactly the same, with D3D11_CULL_FRONT it looks like the problems are similar, but different.

I also tried switching the triangle direction, but the result was exactly the same except inverted for the cull modes.

 1 2 3 4 5 6 7 8 9 10 11 12 13 internal index_array SwizzleIndices(index_array Indices) { For(Indices.Count/3) { i32 I = It*3; u32 Tmp = Indices.Data[I+1]; Indices.Data[I+1] = Indices.Data[I+2]; Indices.Data[I+2] = Tmp; } return Indices; }

Simon Anciaux
1335 posts
Wrong depth sorting for two directions (D3D11)
I don't know anything about DirectX but you seem to initialize the depth stencil buffer differently than in the gist code. I suppose it's because you want to do something with that, but does it work if you remove the change ?

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 D3D11_TEXTURE2D_DESC DepthBufferDesc; FrameBuffer->GetDesc(&DepthBufferDesc); // base on framebuffer properties DepthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; DepthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; ID3D11Texture2D* DepthBuffer = 0; Result.Device->CreateTexture2D(&DepthBufferDesc, 0, &DepthBuffer); D3D11_DEPTH_STENCIL_VIEW_DESC DepthStencilViewDesc; DepthStencilViewDesc.Format = DXGI_FORMAT_D32_FLOAT_S8X24_UINT; DepthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; DepthStencilViewDesc.Texture2D.MipSlice = 0; // Create the depth stencil view Result.Device->CreateDepthStencilView(DepthBuffer, // Depth stencil texture &DepthStencilViewDesc, // Depth stencil desc &Result.DepthBufferView); // [out] Depth stencil view

From github:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 D3D11_TEXTURE2D_DESC depthBufferDesc; frameBuffer->GetDesc(&depthBufferDesc); // base on framebuffer properties depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; ID3D11Texture2D* depthBuffer; device->CreateTexture2D(&depthBufferDesc, nullptr, &depthBuffer); ID3D11DepthStencilView* depthBufferView; device->CreateDepthStencilView(depthBuffer, nullptr, &depthBufferView);
Mārtiņš Možeiko
2548 posts / 2 projects
Wrong depth sorting for two directions (D3D11)
Oh, what mrmixer noticed is interesting - your depth/stencil view has DXGI_FORMAT_D32_FLOAT_S8X24_UINT format, but your actual texture DXGI_FORMAT_D24_UNORM_S8_UINT. Not sure exactly what that means for runtime... In this situation it is better either match formats, or just pass NULL when creating view, that will make it automatically use same format as texture.
Tim Dierks
14 posts / 1 project
Wrong depth sorting for two directions (D3D11)
Edited by Tim Dierks on
@mrmixer, Yes, adding that was part of me trying to find the bug. As I thought it has something to do with the depth buffer, or something like that. But in the end, it really only switches the two axes in which the rendering buggy.

@mmozeiko
Interestingly, changing the Format to DXGI_FORMAT_D24_UNORM_S8_UINT results in the same result as if I would not use the D3D11_DEPTH_STENCIL_VIEW_DESC at all.
It reverses the two axis in which you have to look to produce the visual error (from +x/+z to -x/-z, or something like that).

Man, you guys are awesome. Even if we don't find the bug, thank you for the help either way!

I put the project on github here: https://github.com/CaptainTimberTim/DungeonDirect3D
Maybe it helps to have the whole thing (Direct3D_11_TD.c/.h are the important files).
Mārtiņš Možeiko
2548 posts / 2 projects
Wrong depth sorting for two directions (D3D11)
You're missing data/Models/Room_001.obj file in repository.
Tim Dierks
14 posts / 1 project
Wrong depth sorting for two directions (D3D11)
Oh damn, somehow I had them in the .gitignore.

Now everything is there.
Simon Anciaux
1335 posts
Wrong depth sorting for two directions (D3D11)
I don't know if it helps, but when I take a capture with RenderDoc, the depth buffer isn't correct. Each draw call set's the affected pixels to 0 (the clears sets them to 1) which means nothing will be written after the pixel as been drawn once. There is no "gradient" depending on the depth.
Mārtiņš Možeiko
2548 posts / 2 projects
Wrong depth sorting for two directions (D3D11)
Edited by Mārtiņš Možeiko on
Your projection matrix is super strange. No idea why near plane is multiplied into first two diagonal components.
It looks like everything is inverted inside out.
If I change projection matrix to following:
 1 2 3 4 5 6 7 8 9 10 11 float fov = 60.f; float t = 1.f / tanf(fov); float aspect = (float)D3D11Info->DepthBufferWidth / (float)D3D11Info->DepthBufferHeight; m4 Projection = { t / aspect, 0, 0, 0, 0, t, 0, 0, 0, 0, D3D11Info->FarClipping / (D3D11Info->FarClipping - D3D11Info->NearClipping), 1, 0, 0, D3D11Info->NearClipping * D3D11Info->FarClipping / (D3D11Info->NearClipping - D3D11Info->FarClipping), 0 };

And your near/far clip planes to following:
 1 2 Result.NearClipping = 0.1f; // near Result.FarClipping = 1000.0f; // far

then everything renders as expected (aside some z-fighting).
Tim Dierks
14 posts / 1 project
Wrong depth sorting for two directions (D3D11)
Ah! Thank you!
That is something I would never have found... I am not very firm in projection matrix stuff.
Especially, because it kinda looked correct most of the time, except the weird culling thing.

I got the matrix from the same sample code as the rest of the Direct3D stuff, here:
https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052
And it looks the same.

Now I can finally actually start doing stuff, yay!

Thank you very much.