From aaba2d0c048a1878ab20497e0fb62a013a4e4ad7 Mon Sep 17 00:00:00 2001 From: Thomas Tissot Date: Tue, 18 Aug 2020 17:21:12 +0200 Subject: Added support for 1+ textures to ImGui renderer This was not possible to use textures other than the font texture with the ImGui renderer. To address that limitation, this commit adds support for one or more textures by updating the SRB's variable when needed. --- Imgui/src/ImGuiDiligentRenderer.cpp | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'Imgui/src/ImGuiDiligentRenderer.cpp') diff --git a/Imgui/src/ImGuiDiligentRenderer.cpp b/Imgui/src/ImGuiDiligentRenderer.cpp index a446f5c..0ef4028 100644 --- a/Imgui/src/ImGuiDiligentRenderer.cpp +++ b/Imgui/src/ImGuiDiligentRenderer.cpp @@ -336,7 +336,7 @@ void ImGuiDiligentRenderer::CreateDeviceObjects() ShaderResourceVariableDesc Variables[] = { - {SHADER_TYPE_PIXEL, "Texture", SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE} // + {SHADER_TYPE_PIXEL, "Texture", SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC} // }; PSODesc.ResourceLayout.Variables = Variables; PSODesc.ResourceLayout.NumVariables = _countof(Variables); @@ -393,7 +393,6 @@ void ImGuiDiligentRenderer::CreateFontsTexture() m_pSRB.Release(); m_pPSO->CreateShaderResourceBinding(&m_pSRB, true); - m_pSRB->GetVariableByName(SHADER_TYPE_PIXEL, "Texture")->Set(m_pFontSRV); // Store our identifier io.Fonts->TexID = (ImTextureID)m_pFontSRV; @@ -635,7 +634,6 @@ void ImGuiDiligentRenderer::RenderDrawData(IDeviceContext* pCtx, ImDrawData* pDr pCtx->SetVertexBuffers(0, 1, pVBs, Offsets, RESOURCE_STATE_TRANSITION_MODE_TRANSITION, SET_VERTEX_BUFFERS_FLAG_RESET); pCtx->SetIndexBuffer(m_pIB, 0, RESOURCE_STATE_TRANSITION_MODE_TRANSITION); pCtx->SetPipelineState(m_pPSO); - pCtx->CommitShaderResources(m_pSRB, RESOURCE_STATE_TRANSITION_MODE_TRANSITION); const float blend_factor[4] = {0.f, 0.f, 0.f, 0.f}; pCtx->SetBlendFactors(blend_factor); @@ -656,8 +654,9 @@ void ImGuiDiligentRenderer::RenderDrawData(IDeviceContext* pCtx, ImDrawData* pDr // Render command lists // (Because we merged all buffers into a single one, we maintain our own offset into them) - int global_idx_offset = 0; - int global_vtx_offset = 0; + int global_idx_offset = 0; + int global_vtx_offset = 0; + ITextureView* pLastTextureView = nullptr; for (int n = 0; n < pDrawData->CmdListsCount; n++) { @@ -699,11 +698,17 @@ void ImGuiDiligentRenderer::RenderDrawData(IDeviceContext* pCtx, ImDrawData* pDr static_cast(m_RenderSurfaceWidth * pDrawData->FramebufferScale.x), static_cast(m_RenderSurfaceHeight * pDrawData->FramebufferScale.y)); - // Bind texture, Draw - auto* texture_srv = reinterpret_cast(pcmd->TextureId); - VERIFY_EXPR(texture_srv == m_pFontSRV); - (void)texture_srv; - //ctx->PSSetShaderResources(0, 1, &texture_srv); + // Bind texture + auto pTextureView = reinterpret_cast(pcmd->TextureId); + VERIFY_EXPR(pTextureView); + if (pTextureView != pLastTextureView) + { + pLastTextureView = pTextureView; + m_pSRB->GetVariableByName(SHADER_TYPE_PIXEL, "Texture")->Set(pTextureView); + pCtx->CommitShaderResources(m_pSRB, RESOURCE_STATE_TRANSITION_MODE_TRANSITION); + } + + // Draw DrawIndexedAttribs DrawAttrs(pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? VT_UINT16 : VT_UINT32, DRAW_FLAG_VERIFY_STATES); DrawAttrs.FirstIndexLocation = pcmd->IdxOffset + global_idx_offset; DrawAttrs.BaseVertex = pcmd->VtxOffset + global_vtx_offset; -- cgit v1.2.3