Skip to content

Commit 1f19c37

Browse files
committed
wrap the UI's .update args into S_UPDATE_PARAMETERS, require only the minimum (remove optional delta in secs, 1/60 by default), get rid of our IWindow & ICursor dependency from the UI, move ImGui::Render() from .update to .render, update examples_tests submodule
1 parent 650e2e4 commit 1f19c37

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

examples_tests

include/nbl/ext/ImGui/ImGui.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,35 @@ class UI final : public core::IReferenceCounted
2525
nbl::core::smart_refctd_ptr<typename COMPOSE_T> streamingTDBufferST; // composed buffer layout is [EBC_DRAW_INDIRECT_STRUCTURES] [EBC_ELEMENT_STRUCTURES] [EBC_INDEX_BUFFERS] [EBC_VERTEX_BUFFERS]
2626
};
2727

28+
//! parameters which may change every frame, used with the .update call to interact with ImGuiIO; we require a very *required* minimum - if you need to cover more IO options simply get the IO with ImGui::GetIO() to customize them (they all have default values you can change before calling the .update)
29+
struct S_UPDATE_PARAMETERS
30+
{
31+
//! what we pass to ImGuiIO::AddMousePosEvent
32+
nbl::hlsl::float32_t2 mousePosition,
33+
34+
//! main display size in pixels (generally == GetMainViewport()->Size)
35+
displaySize;
36+
37+
//! Nabla events you want to be handled with the backend
38+
struct S_EVENTS
39+
{
40+
core::SRange<const nbl::ui::SMouseEvent> mouse;
41+
core::SRange<const nbl::ui::SKeyboardEvent> keyboard;
42+
};
43+
44+
S_EVENTS events;
45+
};
46+
2847
UI(core::smart_refctd_ptr<video::ILogicalDevice> _device, core::smart_refctd_ptr<video::IGPUDescriptorSetLayout> _descriptorSetLayout, video::IGPURenderpass* renderpass, uint32_t subpassIx, video::IGPUPipelineCache* pipelineCache = nullptr, nbl::core::smart_refctd_ptr<typename MDI::COMPOSE_T> _streamingMDIBuffer = nullptr);
2948
~UI() override;
3049

3150
//! Nabla ImGUI backend reserves this index for font atlas, any attempt to hook user defined texture within the index will cause runtime error [TODO: could have a setter & getter to control the default & currently hooked font texture ID and init 0u by default]
3251
_NBL_STATIC_INLINE_CONSTEXPR auto NBL_FONT_ATLAS_TEX_ID = 0u;
3352

34-
//! update ImGUI internal state & cpu draw command lists, call it before this->render
35-
bool update(const ui::IWindow* window, float deltaTimeInSec, const core::SRange<const nbl::ui::SMouseEvent> mouseEvents, const core::SRange<const nbl::ui::SKeyboardEvent> keyboardEvents);
53+
//! update ImGuiIO & record ImGUI *cpu* draw command lists, call it before .render
54+
bool update(const S_UPDATE_PARAMETERS params);
3655

37-
//! updates mapped mdi buffer & records draw calls
56+
//! updates mapped mdi buffer & records *gpu* draw commands, handles overflows for mdi allocation failure cases (pop & submit)
3857
bool render(nbl::video::SIntendedSubmitInfo& info, const nbl::video::IGPUDescriptorSet* const descriptorSet);
3958

4059
//! registers lambda listener in which ImGUI calls should be recorded
@@ -57,7 +76,7 @@ class UI final : public core::IReferenceCounted
5776
void createPipeline(core::smart_refctd_ptr<video::IGPUDescriptorSetLayout> descriptorSetLayout, video::IGPURenderpass* renderpass, uint32_t subpassIx, video::IGPUPipelineCache* pipelineCache);
5877
void createMDIBuffer(nbl::core::smart_refctd_ptr<typename MDI::COMPOSE_T> _streamingMDIBuffer);
5978
video::ISemaphore::future_t<video::IQueue::RESULT> createFontAtlasTexture(video::IGPUCommandBuffer* cmdBuffer, video::IQueue* queue);
60-
void handleMouseEvents(const core::SRange<const nbl::ui::SMouseEvent>& events, const ui::IWindow* window) const;
79+
void handleMouseEvents(const core::SRange<const nbl::ui::SMouseEvent>& events, nbl::hlsl::float32_t2 mousePosition) const;
6180
void handleKeyEvents(const core::SRange<const nbl::ui::SKeyboardEvent>& events) const;
6281

6382
core::smart_refctd_ptr<system::ISystem> system;

src/nbl/ext/ImGui/ImGui.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <utility>
66

77
#include "nbl/system/IApplicationFramework.h"
8-
#include "nbl/ui/IWindow.h"
9-
#include "nbl/ui/ICursorControl.h"
108
#include "nbl/system/CStdoutLogger.h"
119
#include "nbl/ext/ImGui/ImGui.h"
1210
#include "shaders/common.hlsl"
@@ -311,14 +309,11 @@ namespace nbl::ext::imgui
311309
io.FontGlobalScale = 1.0f;
312310
}
313311

314-
void UI::handleMouseEvents(const core::SRange<const nbl::ui::SMouseEvent>& events, const ui::IWindow* window) const
312+
void UI::handleMouseEvents(const core::SRange<const nbl::ui::SMouseEvent>& events, nbl::hlsl::float32_t2 mousePosition) const
315313
{
316314
auto& io = ImGui::GetIO();
317315

318-
const auto cursorPosition = window->getCursorControl()->getPosition();
319-
const auto mousePixelPosition = nbl::hlsl::float32_t2(cursorPosition.x, cursorPosition.y) - nbl::hlsl::float32_t2(window->getX(), window->getY());
320-
321-
io.AddMousePosEvent(mousePixelPosition.x, mousePixelPosition.y);
316+
io.AddMousePosEvent(mousePosition.x, mousePosition.y);
322317

323318
for (const auto& e : events)
324319
{
@@ -704,6 +699,8 @@ namespace nbl::ext::imgui
704699
return false;
705700
}
706701

702+
ImGui::Render(); // note it doesn't touch GPU or graphics API at all, its an internal ImGUI call to update & prepare the data for rendering so we can call GetDrawData()
703+
707704
struct
708705
{
709706
const uint64_t oldie;
@@ -1007,26 +1004,20 @@ namespace nbl::ext::imgui
10071004
return true;
10081005
}
10091006

1010-
bool UI::update(const ui::IWindow* window, float const deltaTimeInSec, const core::SRange<const nbl::ui::SMouseEvent> mouseEvents, const core::SRange<const nbl::ui::SKeyboardEvent> keyboardEvents)
1007+
bool UI::update(const S_UPDATE_PARAMETERS params)
10111008
{
1012-
if (!window)
1013-
return false;
1014-
10151009
auto & io = ImGui::GetIO();
1010+
1011+
io.DisplaySize = ImVec2(params.displaySize.x, params.displaySize.y);
10161012

1017-
io.DeltaTime = deltaTimeInSec;
1018-
io.DisplaySize = ImVec2(window->getWidth(), window->getHeight());
1019-
1020-
handleMouseEvents(mouseEvents, window);
1021-
handleKeyEvents(keyboardEvents);
1013+
handleMouseEvents(params.events.mouse, params.mousePosition);
1014+
handleKeyEvents(params.events.keyboard);
10221015

10231016
ImGui::NewFrame();
10241017

10251018
for (auto const& subscriber : m_subscribers)
10261019
subscriber.listener();
10271020

1028-
ImGui::Render(); // note it doesn't touch GPU or graphics API at all, internal call for IMGUI cpu geometry buffers update
1029-
10301021
return true;
10311022
}
10321023

0 commit comments

Comments
 (0)