diff options
author | 2021-08-10 20:28:56 +0200 | |
---|---|---|
committer | 2021-08-10 20:28:56 +0200 | |
commit | 1a6a018fc3b393e6f769d7db3aea397175aba459 (patch) | |
tree | 1fd0418f1044b221537c682e3e7f02a4a2a72af2 /3rdparty/bgfx/examples/common/imgui/imgui.cpp | |
parent | 702c9b45cc3a0d3eaaf29cd595b7e3511316a348 (diff) |
Update bgfx to latest
Diffstat (limited to '3rdparty/bgfx/examples/common/imgui/imgui.cpp')
-rw-r--r-- | 3rdparty/bgfx/examples/common/imgui/imgui.cpp | 77 |
1 files changed, 60 insertions, 17 deletions
diff --git a/3rdparty/bgfx/examples/common/imgui/imgui.cpp b/3rdparty/bgfx/examples/common/imgui/imgui.cpp index 34558323fb9..4cf1c4f104f 100644 --- a/3rdparty/bgfx/examples/common/imgui/imgui.cpp +++ b/3rdparty/bgfx/examples/common/imgui/imgui.cpp @@ -9,6 +9,7 @@ #include <bx/math.h> #include <bx/timer.h> #include <dear-imgui/imgui.h> +#include <dear-imgui/imgui_internal.h> #include "imgui.h" #include "../bgfx_utils.h" @@ -64,9 +65,11 @@ struct OcornutImguiContext { void render(ImDrawData* _drawData) { - const ImGuiIO& io = ImGui::GetIO(); - const float width = io.DisplaySize.x; - const float height = io.DisplaySize.y; + // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) + int fb_width = (int)(_drawData->DisplaySize.x * _drawData->FramebufferScale.x); + int fb_height = (int)(_drawData->DisplaySize.y * _drawData->FramebufferScale.y); + if (fb_width <= 0 || fb_height <= 0) + return; bgfx::setViewName(m_viewId, "ImGui"); bgfx::setViewMode(m_viewId, bgfx::ViewMode::Sequential); @@ -74,11 +77,19 @@ struct OcornutImguiContext const bgfx::Caps* caps = bgfx::getCaps(); { float ortho[16]; - bx::mtxOrtho(ortho, 0.0f, width, height, 0.0f, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth); + float x = _drawData->DisplayPos.x; + float y = _drawData->DisplayPos.y; + float width = _drawData->DisplaySize.x; + float height = _drawData->DisplaySize.y; + + bx::mtxOrtho(ortho, x, x + width, y + height, y, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth); bgfx::setViewTransform(m_viewId, NULL, ortho); bgfx::setViewRect(m_viewId, 0, 0, uint16_t(width), uint16_t(height) ); } + const ImVec2 clipPos = _drawData->DisplayPos; // (0,0) unless using multi-viewports + const ImVec2 clipScale = _drawData->FramebufferScale; // (1,1) unless using retina display which are often (2,2) + // Render command lists for (int32_t ii = 0, num = _drawData->CmdListsCount; ii < num; ++ii) { @@ -96,7 +107,7 @@ struct OcornutImguiContext } bgfx::allocTransientVertexBuffer(&tvb, numVertices, m_layout); - bgfx::allocTransientIndexBuffer(&tib, numIndices); + bgfx::allocTransientIndexBuffer(&tib, numIndices, sizeof(ImDrawIdx) == 4); ImDrawVert* verts = (ImDrawVert*)tvb.data; bx::memCopy(verts, drawList->VtxBuffer.begin(), numVertices * sizeof(ImDrawVert) ); @@ -104,6 +115,8 @@ struct OcornutImguiContext ImDrawIdx* indices = (ImDrawIdx*)tib.data; bx::memCopy(indices, drawList->IdxBuffer.begin(), numIndices * sizeof(ImDrawIdx) ); + bgfx::Encoder* encoder = bgfx::begin(); + uint32_t offset = 0; for (const ImDrawCmd* cmd = drawList->CmdBuffer.begin(), *cmdEnd = drawList->CmdBuffer.end(); cmd != cmdEnd; ++cmd) { @@ -142,22 +155,37 @@ struct OcornutImguiContext state |= BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA); } - const uint16_t xx = uint16_t(bx::max(cmd->ClipRect.x, 0.0f) ); - const uint16_t yy = uint16_t(bx::max(cmd->ClipRect.y, 0.0f) ); - bgfx::setScissor(xx, yy - , uint16_t(bx::min(cmd->ClipRect.z, 65535.0f)-xx) - , uint16_t(bx::min(cmd->ClipRect.w, 65535.0f)-yy) - ); - - bgfx::setState(state); - bgfx::setTexture(0, s_tex, th); - bgfx::setVertexBuffer(0, &tvb, 0, numVertices); - bgfx::setIndexBuffer(&tib, offset, cmd->ElemCount); - bgfx::submit(m_viewId, program); + // Project scissor/clipping rectangles into framebuffer space + ImVec4 clipRect; + clipRect.x = (cmd->ClipRect.x - clipPos.x) * clipScale.x; + clipRect.y = (cmd->ClipRect.y - clipPos.y) * clipScale.y; + clipRect.z = (cmd->ClipRect.z - clipPos.x) * clipScale.x; + clipRect.w = (cmd->ClipRect.w - clipPos.y) * clipScale.y; + + if (clipRect.x < fb_width + && clipRect.y < fb_height + && clipRect.z >= 0.0f + && clipRect.w >= 0.0f) + { + const uint16_t xx = uint16_t(bx::max(clipRect.x, 0.0f) ); + const uint16_t yy = uint16_t(bx::max(clipRect.y, 0.0f) ); + encoder->setScissor(xx, yy + , uint16_t(bx::min(clipRect.z, 65535.0f)-xx) + , uint16_t(bx::min(clipRect.w, 65535.0f)-yy) + ); + + encoder->setState(state); + encoder->setTexture(0, s_tex, th); + encoder->setVertexBuffer(0, &tvb, 0, numVertices); + encoder->setIndexBuffer(&tib, offset, cmd->ElemCount); + encoder->submit(m_viewId, program); + } } offset += cmd->ElemCount; } + + bgfx::end(encoder); } } @@ -443,6 +471,21 @@ namespace ImGui { PushFont(s_ctx.m_font[_font]); } + + void PushEnabled(bool _enabled) + { + extern void PushItemFlag(int option, bool enabled); + PushItemFlag(ImGuiItemFlags_Disabled, !_enabled); + PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * (_enabled ? 1.0f : 0.5f) ); + } + + void PopEnabled() + { + extern void PopItemFlag(); + PopItemFlag(); + PopStyleVar(); + } + } // namespace ImGui BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4505); // error C4505: '' : unreferenced local function has been removed |