diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2019-11-25 02:44:26 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2019-11-25 02:44:26 +0000 |
| commit | c7319683cf2ebbe52704f3ad59d79518da7c49f3 (patch) | |
| tree | 90d61525b59845e375660df212d7cd3dbb24ce5d /Imgui/src/ImGuiImplDiligent.cpp | |
| parent | clang-formatted AssetLoader (diff) | |
| download | DiligentTools-c7319683cf2ebbe52704f3ad59d79518da7c49f3.tar.gz DiligentTools-c7319683cf2ebbe52704f3ad59d79518da7c49f3.zip | |
clang-formatted ImGuiImpl
Diffstat (limited to 'Imgui/src/ImGuiImplDiligent.cpp')
| -rw-r--r-- | Imgui/src/ImGuiImplDiligent.cpp | 158 |
1 files changed, 82 insertions, 76 deletions
diff --git a/Imgui/src/ImGuiImplDiligent.cpp b/Imgui/src/ImGuiImplDiligent.cpp index 4df6974..03e3e76 100644 --- a/Imgui/src/ImGuiImplDiligent.cpp +++ b/Imgui/src/ImGuiImplDiligent.cpp @@ -36,24 +36,26 @@ namespace Diligent class ImGuiImplDiligent_Internal { public: - ImGuiImplDiligent_Internal(IRenderDevice* pDevice, - TEXTURE_FORMAT BackBufferFmt, - TEXTURE_FORMAT DepthBufferFmt, - Uint32 InitialVertexBufferSize, - Uint32 InitialIndexBufferSize) : - m_pDevice (pDevice), - m_BackBufferFmt (BackBufferFmt), - m_DepthBufferFmt (DepthBufferFmt), - m_VertexBufferSize(InitialVertexBufferSize), - m_IndexBufferSize (InitialIndexBufferSize) + ImGuiImplDiligent_Internal(IRenderDevice* pDevice, + TEXTURE_FORMAT BackBufferFmt, + TEXTURE_FORMAT DepthBufferFmt, + Uint32 InitialVertexBufferSize, + Uint32 InitialIndexBufferSize) : + // clang-format off + m_pDevice {pDevice}, + m_BackBufferFmt {BackBufferFmt}, + m_DepthBufferFmt {DepthBufferFmt}, + m_VertexBufferSize{InitialVertexBufferSize}, + m_IndexBufferSize {InitialIndexBufferSize} + // clang-format on { // Setup back-end capabilities flags IMGUI_CHECKVERSION(); ImGui::CreateContext(); - ImGuiIO& io = ImGui::GetIO(); + ImGuiIO& io = ImGui::GetIO(); io.BackendRendererName = "ImGuiImplDiligent"; - io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. - io.IniFilename = nullptr; + io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. + io.IniFilename = nullptr; CreateDeviceObjects(); } @@ -73,17 +75,17 @@ public: private: void CreateFontsTexture(); - RefCntAutoPtr<IRenderDevice> m_pDevice; - RefCntAutoPtr<IBuffer> m_pVB; - RefCntAutoPtr<IBuffer> m_pIB; - RefCntAutoPtr<IBuffer> m_pVertexConstantBuffer; - RefCntAutoPtr<IPipelineState> m_pPSO; - RefCntAutoPtr<ITextureView> m_pFontSRV; - RefCntAutoPtr<IShaderResourceBinding> m_pSRB; - const TEXTURE_FORMAT m_BackBufferFmt; - const TEXTURE_FORMAT m_DepthBufferFmt; - Uint32 m_VertexBufferSize = 0; - Uint32 m_IndexBufferSize = 0; + RefCntAutoPtr<IRenderDevice> m_pDevice; + RefCntAutoPtr<IBuffer> m_pVB; + RefCntAutoPtr<IBuffer> m_pIB; + RefCntAutoPtr<IBuffer> m_pVertexConstantBuffer; + RefCntAutoPtr<IPipelineState> m_pPSO; + RefCntAutoPtr<ITextureView> m_pFontSRV; + RefCntAutoPtr<IShaderResourceBinding> m_pSRB; + const TEXTURE_FORMAT m_BackBufferFmt; + const TEXTURE_FORMAT m_DepthBufferFmt; + Uint32 m_VertexBufferSize = 0; + Uint32 m_IndexBufferSize = 0; }; void ImGuiImplDiligent_Internal::NewFrame() @@ -173,7 +175,7 @@ void ImGuiImplDiligent_Internal::CreateDeviceObjects() } PipelineStateDesc PSODesc; - PSODesc.Name = "ImGUI PSO"; + PSODesc.Name = "ImGUI PSO"; auto& GraphicsPipeline = PSODesc.GraphicsPipeline; GraphicsPipeline.NumRenderTargets = 1; @@ -188,7 +190,7 @@ void ImGuiImplDiligent_Internal::CreateDeviceObjects() GraphicsPipeline.RasterizerDesc.ScissorEnable = True; GraphicsPipeline.DepthStencilDesc.DepthEnable = False; - auto& RT0 = GraphicsPipeline.BlendDesc.RenderTargets[0]; + auto& RT0 = GraphicsPipeline.BlendDesc.RenderTargets[0]; RT0.BlendEnable = True; RT0.SrcBlend = BLEND_FACTOR_SRC_ALPHA; RT0.DestBlend = BLEND_FACTOR_INV_SRC_ALPHA; @@ -198,19 +200,19 @@ void ImGuiImplDiligent_Internal::CreateDeviceObjects() RT0.BlendOpAlpha = BLEND_OPERATION_ADD; RT0.RenderTargetWriteMask = COLOR_MASK_ALL; - LayoutElement VSInputs[] - { - { 0, 0, 2, VT_FLOAT32}, // pos - { 1, 0, 2, VT_FLOAT32}, // uv - { 2, 0, 4, VT_UINT8, True} // col - }; + LayoutElement VSInputs[] // + { + {0, 0, 2, VT_FLOAT32}, // pos + {1, 0, 2, VT_FLOAT32}, // uv + {2, 0, 4, VT_UINT8, True} // col + }; GraphicsPipeline.InputLayout.NumElements = _countof(VSInputs); GraphicsPipeline.InputLayout.LayoutElements = VSInputs; ShaderResourceVariableDesc Variables[] = - { - {SHADER_TYPE_PIXEL, "Texture", SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE} - }; + { + {SHADER_TYPE_PIXEL, "Texture", SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE} // + }; PSODesc.ResourceLayout.Variables = Variables; PSODesc.ResourceLayout.NumVariables = _countof(Variables); @@ -218,10 +220,10 @@ void ImGuiImplDiligent_Internal::CreateDeviceObjects() SamLinearWrap.AddressU = TEXTURE_ADDRESS_WRAP; SamLinearWrap.AddressV = TEXTURE_ADDRESS_WRAP; SamLinearWrap.AddressW = TEXTURE_ADDRESS_WRAP; - StaticSamplerDesc StaticSamplers[] = - { - {SHADER_TYPE_PIXEL, "Texture", SamLinearWrap} - }; + StaticSamplerDesc StaticSamplers[] = + { + {SHADER_TYPE_PIXEL, "Texture", SamLinearWrap} // + }; PSODesc.ResourceLayout.StaticSamplers = StaticSamplers; PSODesc.ResourceLayout.NumStaticSamplers = _countof(StaticSamplers); @@ -243,9 +245,9 @@ void ImGuiImplDiligent_Internal::CreateDeviceObjects() void ImGuiImplDiligent_Internal::CreateFontsTexture() { // Build texture atlas - ImGuiIO& io = ImGui::GetIO(); + ImGuiIO& io = ImGui::GetIO(); unsigned char* pixels = nullptr; - int width = 0, height = 0; + int width = 0, height = 0; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); TextureDesc FontTexDesc; @@ -257,8 +259,8 @@ void ImGuiImplDiligent_Internal::CreateFontsTexture() FontTexDesc.BindFlags = BIND_SHADER_RESOURCE; FontTexDesc.Usage = USAGE_STATIC; - TextureSubResData Mip0Data[] = { {pixels, FontTexDesc.Width * 4} }; - TextureData InitData(Mip0Data, _countof(Mip0Data)); + TextureSubResData Mip0Data[] = {{pixels, FontTexDesc.Width * 4}}; + TextureData InitData(Mip0Data, _countof(Mip0Data)); RefCntAutoPtr<ITexture> pFontTex; m_pDevice->CreateTexture(FontTexDesc, &InitData, &pFontTex); @@ -278,12 +280,12 @@ void ImGuiImplDiligent_Internal::RenderDrawData(IDeviceContext* pCtx, ImDrawData // Avoid rendering when minimized if (pDrawData->DisplaySize.x <= 0.0f || pDrawData->DisplaySize.y <= 0.0f) return; - + // Create and grow vertex/index buffers if needed if (!m_pVB || static_cast<int>(m_VertexBufferSize) < pDrawData->TotalVtxCount) { m_pVB.Release(); - while(static_cast<int>(m_VertexBufferSize) < pDrawData->TotalVtxCount) + while (static_cast<int>(m_VertexBufferSize) < pDrawData->TotalVtxCount) m_VertexBufferSize *= 2; BufferDesc VBDesc; @@ -294,11 +296,11 @@ void ImGuiImplDiligent_Internal::RenderDrawData(IDeviceContext* pCtx, ImDrawData VBDesc.CPUAccessFlags = CPU_ACCESS_WRITE; m_pDevice->CreateBuffer(VBDesc, nullptr, &m_pVB); } - + if (!m_pIB || static_cast<int>(m_IndexBufferSize) < pDrawData->TotalIdxCount) { m_pIB.Release(); - while(static_cast<int>(m_IndexBufferSize) < pDrawData->TotalIdxCount) + while (static_cast<int>(m_IndexBufferSize) < pDrawData->TotalIdxCount) m_IndexBufferSize *= 2; BufferDesc IBDesc; @@ -312,9 +314,10 @@ void ImGuiImplDiligent_Internal::RenderDrawData(IDeviceContext* pCtx, ImDrawData { MapHelper<ImDrawVert> Verices(pCtx, m_pVB, MAP_WRITE, MAP_FLAG_DISCARD); - MapHelper<ImDrawIdx> Indices(pCtx, m_pIB, MAP_WRITE, MAP_FLAG_DISCARD); + MapHelper<ImDrawIdx> Indices(pCtx, m_pIB, MAP_WRITE, MAP_FLAG_DISCARD); + ImDrawVert* vtx_dst = Verices; - ImDrawIdx* idx_dst = Indices; + ImDrawIdx* idx_dst = Indices; for (int n = 0; n < pDrawData->CmdListsCount; n++) { const ImDrawList* cmd_list = pDrawData->CmdLists[n]; @@ -331,32 +334,35 @@ void ImGuiImplDiligent_Internal::RenderDrawData(IDeviceContext* pCtx, ImDrawData // DisplayPos is (0,0) for single viewport apps. { MapHelper<float4x4> CBData(pCtx, m_pVertexConstantBuffer, MAP_WRITE, MAP_FLAG_DISCARD); + float L = pDrawData->DisplayPos.x; float R = pDrawData->DisplayPos.x + pDrawData->DisplaySize.x; float T = pDrawData->DisplayPos.y; float B = pDrawData->DisplayPos.y + pDrawData->DisplaySize.y; + // clang-format off *CBData = float4x4 { - 2.0f/(R-L), 0.0f, 0.0f, 0.0f, - 0.0f, 2.0f/(T-B), 0.0f, 0.0f, - 0.0f, 0.0f, 0.5f, 0.0f, - (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f + 2.0f / (R - L), 0.0f, 0.0f, 0.0f, + 0.0f, 2.0f / (T - B), 0.0f, 0.0f, + 0.0f, 0.0f, 0.5f, 0.0f, + (R + L) / (L - R), (T + B) / (B - T), 0.5f, 1.0f }; + // clang-format on } - auto DisplayWidth = static_cast<Uint32>(pDrawData->DisplaySize.x); - auto DisplayHeight = static_cast<Uint32>(pDrawData->DisplaySize.y); - auto SetupRenderState = [&]() + auto DisplayWidth = static_cast<Uint32>(pDrawData->DisplaySize.x); + auto DisplayHeight = static_cast<Uint32>(pDrawData->DisplaySize.y); + auto SetupRenderState = [&]() // { // Setup shader and vertex buffers - Uint32 Offsets[] = {0}; - IBuffer* pVBs[] = {m_pVB}; + Uint32 Offsets[] = {0}; + IBuffer* pVBs[] = {m_pVB}; 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 }; + const float blend_factor[4] = {0.f, 0.f, 0.f, 0.f}; pCtx->SetBlendFactors(blend_factor); Viewport vp; @@ -367,13 +373,14 @@ void ImGuiImplDiligent_Internal::RenderDrawData(IDeviceContext* pCtx, ImDrawData vp.TopLeftX = vp.TopLeftY = 0; pCtx->SetViewports(1, &vp, DisplayWidth, DisplayHeight); }; - + SetupRenderState(); // 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; + ImVec2 clip_off = pDrawData->DisplayPos; for (int n = 0; n < pDrawData->CmdListsCount; n++) { @@ -394,17 +401,18 @@ void ImGuiImplDiligent_Internal::RenderDrawData(IDeviceContext* pCtx, ImDrawData { // Apply scissor/clipping rectangle const Rect r = - { - static_cast<Int32>(pcmd->ClipRect.x - clip_off.x), - static_cast<Int32>(pcmd->ClipRect.y - clip_off.y), - static_cast<Int32>(pcmd->ClipRect.z - clip_off.x), - static_cast<Int32>(pcmd->ClipRect.w - clip_off.y) - }; + { + static_cast<Int32>(pcmd->ClipRect.x - clip_off.x), + static_cast<Int32>(pcmd->ClipRect.y - clip_off.y), + static_cast<Int32>(pcmd->ClipRect.z - clip_off.x), + static_cast<Int32>(pcmd->ClipRect.w - clip_off.y) // + }; pCtx->SetScissorRects(1, &r, DisplayWidth, DisplayHeight); // Bind texture, Draw auto* texture_srv = reinterpret_cast<ITextureView*>(pcmd->TextureId); - VERIFY_EXPR(texture_srv == m_pFontSRV); (void)texture_srv; + VERIFY_EXPR(texture_srv == m_pFontSRV); + (void)texture_srv; //ctx->PSSetShaderResources(0, 1, &texture_srv); DrawIndexedAttribs DrawAttrs(pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? VT_UINT16 : VT_UINT32, DRAW_FLAG_VERIFY_STATES); DrawAttrs.FirstIndexLocation = pcmd->IdxOffset + global_idx_offset; @@ -419,11 +427,11 @@ void ImGuiImplDiligent_Internal::RenderDrawData(IDeviceContext* pCtx, ImDrawData -ImGuiImplDiligent::ImGuiImplDiligent(IRenderDevice* pDevice, - TEXTURE_FORMAT BackBufferFmt, - TEXTURE_FORMAT DepthBufferFmt, - Uint32 InitialVertexBufferSize, - Uint32 InitialIndexBufferSize) : +ImGuiImplDiligent::ImGuiImplDiligent(IRenderDevice* pDevice, + TEXTURE_FORMAT BackBufferFmt, + TEXTURE_FORMAT DepthBufferFmt, + Uint32 InitialVertexBufferSize, + Uint32 InitialIndexBufferSize) : m_pImpl(new ImGuiImplDiligent_Internal(pDevice, BackBufferFmt, DepthBufferFmt, InitialVertexBufferSize, InitialIndexBufferSize)) { } @@ -444,7 +452,7 @@ void ImGuiImplDiligent::Render(IDeviceContext* pCtx) m_pImpl->RenderDrawData(pCtx, ImGui::GetDrawData()); } - // Use if you want to reset your rendering device without losing ImGui state. +// Use if you want to reset your rendering device without losing ImGui state. void ImGuiImplDiligent::InvalidateDeviceObjects() { m_pImpl->InvalidateDeviceObjects(); @@ -455,6 +463,4 @@ void ImGuiImplDiligent::CreateDeviceObjects() m_pImpl->CreateDeviceObjects(); } -} - - +} // namespace Diligent |
