[Feat] (Diligent): add offscreen depth target and depth clear

The renderer now creates a D32_FLOAT depth target and binds it as DSV for all
render passes, so depth-test state wired earlier can actually work. Add
ClearDepth for depth clears. All Diligent local tests still pass.
This commit is contained in:
BZLZHH
2026-08-18 13:34:37 +08:00
parent 821c0e0d4e
commit cedc257566
2 changed files with 42 additions and 4 deletions
@@ -189,6 +189,8 @@ void main()
m_pPSO.Release();
m_pColorRTV.Release();
m_pColorTarget.Release();
m_pDepthDSV.Release();
m_pDepthTarget.Release();
}
Bool DiligentRenderer::Initialize(Uint32 width, Uint32 height) {
@@ -240,6 +242,29 @@ void main()
MGLOG_E("DiligentRenderer: failed to get offscreen RTV");
return false;
}
::Diligent::TextureDesc depthDesc;
depthDesc.Name = "MobileGL Diligent offscreen depth target";
depthDesc.Type = ::Diligent::RESOURCE_DIM_TEX_2D;
depthDesc.Format = ::Diligent::TEX_FORMAT_D32_FLOAT;
depthDesc.Width = m_width;
depthDesc.Height = m_height;
depthDesc.MipLevels = 1;
depthDesc.BindFlags = ::Diligent::BIND_DEPTH_STENCIL;
depthDesc.Usage = ::Diligent::USAGE_DEFAULT;
::Diligent::RefCntAutoPtr<::Diligent::ITexture> pDepthTarget;
m_pDevice->CreateTexture(depthDesc, nullptr, &pDepthTarget);
if (!pDepthTarget) {
MGLOG_E("DiligentRenderer: failed to create offscreen depth target");
return false;
}
m_pDepthTarget = pDepthTarget;
m_pDepthDSV = m_pDepthTarget->GetDefaultView(::Diligent::TEXTURE_VIEW_DEPTH_STENCIL);
if (!m_pDepthDSV) {
MGLOG_E("DiligentRenderer: failed to get offscreen DSV");
return false;
}
return true;
}
@@ -323,18 +348,28 @@ void main()
return;
}
::Diligent::ITextureView* rtvs[] = {m_pColorRTV};
m_pContext->SetRenderTargets(1, rtvs, nullptr, ::Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
m_pContext->SetRenderTargets(1, rtvs, m_pDepthDSV, ::Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
const Float clearColor[] = {r, g, b, a};
m_pContext->ClearRenderTarget(m_pColorRTV, clearColor, ::Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
}
void DiligentRenderer::ClearDepth(Float depth) {
if (!m_initialized || !m_pContext || !m_pDepthDSV) {
return;
}
::Diligent::ITextureView* rtvs[] = {m_pColorRTV};
m_pContext->SetRenderTargets(1, rtvs, m_pDepthDSV, ::Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
m_pContext->ClearDepthStencil(m_pDepthDSV, ::Diligent::CLEAR_DEPTH_FLAG, depth, 0,
::Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
}
void DiligentRenderer::DrawTriangle() {
if (!m_initialized || !m_pContext || !m_pPSO) {
return;
}
::Diligent::ITextureView* rtvs[] = {m_pColorRTV};
m_pContext->SetRenderTargets(1, rtvs, nullptr, ::Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
m_pContext->SetRenderTargets(1, rtvs, m_pDepthDSV, ::Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
::Diligent::Viewport viewport{0, 0, static_cast<Float>(m_width), static_cast<Float>(m_height), 0.0f, 1.0f};
m_pContext->SetViewports(1, &viewport, m_width, m_height);
@@ -379,7 +414,7 @@ void main()
}
::Diligent::ITextureView* rtvs[] = {m_pColorRTV};
m_pContext->SetRenderTargets(1, rtvs, nullptr, ::Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
m_pContext->SetRenderTargets(1, rtvs, m_pDepthDSV, ::Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
::Diligent::Viewport viewport{0, 0, static_cast<Float>(m_width), static_cast<Float>(m_height), 0.0f, 1.0f};
m_pContext->SetViewports(1, &viewport, m_width, m_height);
@@ -408,7 +443,7 @@ void main()
}
::Diligent::ITextureView* rtvs[] = {m_pColorRTV};
m_pContext->SetRenderTargets(1, rtvs, nullptr, ::Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
m_pContext->SetRenderTargets(1, rtvs, m_pDepthDSV, ::Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
auto glViewport = MG_State::pGLContext->GetViewport();
if (glViewport.z() <= 0 || glViewport.w() <= 0) {
@@ -41,6 +41,7 @@ namespace MobileGL::MG_Backend::DiligentBackend {
Bool Initialize(Uint32 width, Uint32 height);
void Clear(Float r, Float g, Float b, Float a);
void ClearDepth(Float depth);
void DrawTriangle();
void DrawVertices(const Float* vertices, Uint32 vertexCount);
// Draws using the live MG_State GL context: current program, VAO and
@@ -63,6 +64,8 @@ namespace MobileGL::MG_Backend::DiligentBackend {
::Diligent::IDeviceContext* m_pContext = nullptr;
::Diligent::RefCntAutoPtr<::Diligent::ITexture> m_pColorTarget;
::Diligent::RefCntAutoPtr<::Diligent::ITextureView> m_pColorRTV;
::Diligent::RefCntAutoPtr<::Diligent::ITexture> m_pDepthTarget;
::Diligent::RefCntAutoPtr<::Diligent::ITextureView> m_pDepthDSV;
::Diligent::RefCntAutoPtr<::Diligent::IPipelineState> m_pPSO;
::Diligent::RefCntAutoPtr<::Diligent::IBuffer> m_pVertexBuffer;
Uint32 m_width = 256;