Browse Source

Examples: DirectX10: Apply depth-stencil state like DirectX11 example (#640, #636)

ocornut 9 years ago
parent
commit
2ef766a1ce
1 changed files with 21 additions and 0 deletions
  1. 21 0
      examples/directx10_example/imgui_impl_dx10.cpp

+ 21 - 0
examples/directx10_example/imgui_impl_dx10.cpp

@@ -34,6 +34,7 @@ static ID3D10SamplerState*      g_pFontSampler = NULL;
 static ID3D10ShaderResourceView*g_pFontTextureView = NULL;
 static ID3D10RasterizerState*   g_pRasterizerState = NULL;
 static ID3D10BlendState*        g_pBlendState = NULL;
+static ID3D10DepthStencilState* g_pDepthStencilState = NULL;
 static int                      g_VertexBufferSize = 5000, g_IndexBufferSize = 10000;
 
 struct VERTEX_CONSTANT_BUFFER
@@ -125,6 +126,8 @@ void ImGui_ImplDX10_RenderDrawLists(ImDrawData* draw_data)
         ID3D10BlendState*           BlendState;
         FLOAT                       BlendFactor[4];
         UINT                        SampleMask;
+        UINT                        StencilRef;
+        ID3D10DepthStencilState*    DepthStencilState;
         ID3D10ShaderResourceView*   PSShaderResource;
         ID3D10SamplerState*         PSSampler;
         ID3D10PixelShader*          PS;
@@ -141,6 +144,7 @@ void ImGui_ImplDX10_RenderDrawLists(ImDrawData* draw_data)
     ctx->RSGetViewports(&old.ViewportsCount, old.Viewports);
     ctx->RSGetState(&old.RS);
     ctx->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask);
+    ctx->OMGetDepthStencilState(&old.DepthStencilState, &old.StencilRef);
     ctx->PSGetShaderResources(0, 1, &old.PSShaderResource);
     ctx->PSGetSamplers(0, 1, &old.PSSampler);
     ctx->PSGetShader(&old.PS);
@@ -176,6 +180,7 @@ void ImGui_ImplDX10_RenderDrawLists(ImDrawData* draw_data)
     // Setup render state
     const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
     ctx->OMSetBlendState(g_pBlendState, blend_factor, 0xffffffff);
+    ctx->OMSetDepthStencilState(g_pDepthStencilState, 0);
     ctx->RSSetState(g_pRasterizerState);
 
     // Render command lists
@@ -208,6 +213,7 @@ void ImGui_ImplDX10_RenderDrawLists(ImDrawData* draw_data)
     ctx->RSSetViewports(old.ViewportsCount, old.Viewports);
     ctx->RSSetState(old.RS); if (old.RS) old.RS->Release();
     ctx->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask); if (old.BlendState) old.BlendState->Release();
+    ctx->OMSetDepthStencilState(old.DepthStencilState, old.StencilRef); if (old.DepthStencilState) old.DepthStencilState->Release();
     ctx->PSSetShaderResources(0, 1, &old.PSShaderResource); if (old.PSShaderResource) old.PSShaderResource->Release();
     ctx->PSSetSamplers(0, 1, &old.PSSampler); if (old.PSSampler) old.PSSampler->Release();
     ctx->PSSetShader(old.PS); if (old.PS) old.PS->Release();
@@ -447,6 +453,20 @@ bool    ImGui_ImplDX10_CreateDeviceObjects()
         g_pd3dDevice->CreateRasterizerState(&desc, &g_pRasterizerState);
     }
 
+    // Create depth-stencil State
+    {
+        D3D10_DEPTH_STENCIL_DESC desc;
+        ZeroMemory(&desc, sizeof(desc));
+        desc.DepthEnable = false;
+        desc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ALL;
+        desc.DepthFunc = D3D10_COMPARISON_ALWAYS;
+        desc.StencilEnable = false;
+        desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
+        desc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS;
+        desc.BackFace = desc.FrontFace;
+        g_pd3dDevice->CreateDepthStencilState(&desc, &g_pDepthStencilState);
+    }
+
     ImGui_ImplDX10_CreateFontsTexture();
 
     return true;
@@ -463,6 +483,7 @@ void    ImGui_ImplDX10_InvalidateDeviceObjects()
     if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
 
     if (g_pBlendState) { g_pBlendState->Release(); g_pBlendState = NULL; }
+    if (g_pDepthStencilState) { g_pDepthStencilState->Release(); g_pDepthStencilState = NULL; }
     if (g_pRasterizerState) { g_pRasterizerState->Release(); g_pRasterizerState = NULL; }
     if (g_pPixelShader) { g_pPixelShader->Release(); g_pPixelShader = NULL; }
     if (g_pPixelShaderBlob) { g_pPixelShaderBlob->Release(); g_pPixelShaderBlob = NULL; }