Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImGuiIntegration: add tests, support ImDrawCmd::VtxOffset #90

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/namespaces.dox
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ See also @ref building-integration and @ref cmake-integration. See the

Conversion of math classes and rendering of Dear ImGui using Magnum.

@section ImGuiIntegration-usage Usage

This library depends on [Dear ImGui](https://github.com/ocornut/imgui) and is
built if `WITH_IMGUI` is enabled when building Magnum Integration. To use this
library with CMake, put [FindMagnumIntegration.cmake](https://github.com/mosra/magnum-integration/blob/master/modules/FindMagnumIntegration.cmake)
Expand All @@ -390,6 +392,8 @@ set(WITH_IMGUI ON CACHE BOOL "" FORCE)
add_subdirectory(magnum-integration EXCLUDE_FROM_ALL)
@endcode

@section ImGuiIntegration-configuration Configuration

Dear ImGui configuration can normally be manipulated at compile time by
defining `IMGUI_USER_CONFIG` which is then included by the Dear ImGui library.
However, this library already uses this feature and defining
Expand Down
8 changes: 5 additions & 3 deletions package/ci/appveyor-desktop-gles.bat
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ cmake .. ^
-DTARGET_GLES=ON ^
-DTARGET_GLES2=%TARGET_GLES2% ^
-DTARGET_DESKTOP_GLES=ON ^
-DWITH_ANYIMAGEIMPORTER=ON ^
-DWITH_AUDIO=OFF ^
-DWITH_DEBUGTOOLS=OFF ^
-DWITH_MESHTOOLS=OFF ^
-DWITH_PRIMITIVES=OFF ^
-DWITH_DEBUGTOOLS=ON ^
-DWITH_MESHTOOLS=ON ^
-DWITH_PRIMITIVES=ON ^
-DWITH_SCENEGRAPH=ON ^
-DWITH_SCENETOOLS=OFF ^
-DWITH_SHADERS=ON ^
-DWITH_SHADERTOOLS=OFF ^
-DWITH_TEXT=OFF ^
-DWITH_TEXTURETOOLS=OFF ^
-DWITH_TGAIMPORTER=ON ^
-DWITH_OPENGLTESTER=ON ^
-DWITH_WINDOWLESSWGLAPPLICATION=ON ^
-DWITH_SDL2APPLICATION=ON ^
Expand Down
8 changes: 5 additions & 3 deletions package/ci/appveyor-desktop-mingw.bat
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@ cmake .. ^
-DCMAKE_BUILD_TYPE=Debug ^
-DCMAKE_INSTALL_PREFIX=%APPVEYOR_BUILD_FOLDER%/deps ^
-DCMAKE_PREFIX_PATH=%APPVEYOR_BUILD_FOLDER%/SDL ^
-DWITH_ANYIMAGEIMPORTER=ON ^
-DWITH_AUDIO=OFF ^
-DWITH_DEBUGTOOLS=OFF ^
-DWITH_MESHTOOLS=OFF ^
-DWITH_PRIMITIVES=OFF ^
-DWITH_DEBUGTOOLS=ON ^
-DWITH_MESHTOOLS=ON ^
-DWITH_PRIMITIVES=ON ^
-DWITH_SCENEGRAPH=ON ^
-DWITH_SCENETOOLS=OFF ^
-DWITH_SHADERS=ON ^
-DWITH_SHADERTOOLS=OFF ^
-DWITH_TEXT=OFF ^
-DWITH_TEXTURETOOLS=OFF ^
-DWITH_TGAIMPORTER=ON ^
-DWITH_OPENGLTESTER=ON ^
-DWITH_WINDOWLESSWGLAPPLICATION=OFF ^
-DWITH_SDL2APPLICATION=ON ^
Expand Down
8 changes: 5 additions & 3 deletions package/ci/appveyor-desktop.bat
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ cmake .. ^
-DCMAKE_BUILD_TYPE=Debug ^
-DCMAKE_INSTALL_PREFIX=%APPVEYOR_BUILD_FOLDER%/deps ^
-DCMAKE_PREFIX_PATH=%APPVEYOR_BUILD_FOLDER%/SDL ^
-DWITH_ANYIMAGEIMPORTER=ON ^
-DWITH_AUDIO=OFF ^
-DWITH_DEBUGTOOLS=OFF ^
-DWITH_MESHTOOLS=OFF ^
-DWITH_PRIMITIVES=OFF ^
-DWITH_DEBUGTOOLS=ON ^
-DWITH_MESHTOOLS=ON ^
-DWITH_PRIMITIVES=ON ^
-DWITH_SCENEGRAPH=ON ^
-DWITH_SCENETOOLS=OFF ^
-DWITH_SHADERS=ON ^
-DWITH_SHADERTOOLS=OFF ^
-DWITH_TEXT=OFF ^
-DWITH_TEXTURETOOLS=OFF ^
-DWITH_TGAIMPORTER=ON ^
-DWITH_OPENGLTESTER=ON ^
-DWITH_WINDOWLESSWGLAPPLICATION=ON ^
-DWITH_SDL2APPLICATION=ON ^
Expand Down
8 changes: 5 additions & 3 deletions package/ci/appveyor-rt.bat
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,18 @@ cmake .. ^
-DOPENGLES2_INCLUDE_DIR=%APPVEYOR_BUILD_FOLDER%/angle/include ^
-DOPENGLES3_LIBRARY=%APPVEYOR_BUILD_FOLDER%/angle/winrt/10/src/Release_x64/lib/libGLESv2.lib ^
-DOPENGLES3_INCLUDE_DIR=%APPVEYOR_BUILD_FOLDER%/angle/include ^
-DWITH_ANYIMAGEIMPORTER=ON ^
-DWITH_AUDIO=OFF ^
-DWITH_DEBUGTOOLS=OFF ^
-DWITH_MESHTOOLS=OFF ^
-DWITH_PRIMITIVES=OFF ^
-DWITH_DEBUGTOOLS=ON ^
-DWITH_MESHTOOLS=ON ^
-DWITH_PRIMITIVES=ON ^
-DWITH_SCENEGRAPH=OFF ^
-DWITH_SCENETOOLS=OFF ^
-DWITH_SHADERS=ON ^
-DWITH_SHADERTOOLS=OFF ^
-DWITH_TEXT=OFF ^
-DWITH_TEXTURETOOLS=OFF ^
-DWITH_TGAIMPORTER=ON ^
-DTARGET_GLES2=%TARGET_GLES2% ^
-DBUILD_STATIC=ON ^
-G "%GENERATOR%" -A x64 || exit /b
Expand Down
8 changes: 5 additions & 3 deletions package/ci/emscripten.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,17 @@ cmake .. \
-DCMAKE_EXE_LINKER_FLAGS_RELEASE="-O1" \
-DCMAKE_INSTALL_PREFIX=$HOME/deps \
-DCMAKE_FIND_ROOT_PATH=$HOME/deps \
-DWITH_ANYIMAGEIMPORTER=ON \
-DWITH_AUDIO=OFF \
-DWITH_DEBUGTOOLS=OFF \
-DWITH_MESHTOOLS=OFF \
-DWITH_PRIMITIVES=OFF \
-DWITH_DEBUGTOOLS=ON \
-DWITH_MESHTOOLS=ON \
-DWITH_PRIMITIVES=ON \
-DWITH_SCENEGRAPH=ON \
-DWITH_SCENETOOLS=OFF \
-DWITH_SHADERS=ON \
-DWITH_TEXT=OFF \
-DWITH_TEXTURETOOLS=OFF \
-DWITH_TGAIMPORTER=ON \
-DWITH_EMSCRIPTENAPPLICATION=ON \
-DTARGET_GLES2=$TARGET_GLES2 \
-G Ninja
Expand Down
8 changes: 5 additions & 3 deletions package/ci/travis-android-arm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ cmake .. \
-DCORRADE_RC_EXECUTABLE=$HOME/deps-native/bin/corrade-rc \
-DCMAKE_INSTALL_PREFIX=$HOME/deps \
-DCMAKE_FIND_ROOT_PATH=$HOME/deps \
-DWITH_ANYIMAGEIMPORTER=ON \
-DWITH_AUDIO=OFF \
-DWITH_ANDROIDAPPLICATION=ON \
-DWITH_DEBUGTOOLS=OFF \
-DWITH_MESHTOOLS=OFF \
-DWITH_PRIMITIVES=OFF \
-DWITH_DEBUGTOOLS=ON \
-DWITH_MESHTOOLS=ON \
-DWITH_PRIMITIVES=ON \
-DWITH_SCENEGRAPH=OFF \
-DWITH_SCENETOOLS=OFF \
-DWITH_SHADERS=ON \
-DWITH_SHADERTOOLS=OFF \
-DWITH_TEXT=OFF \
-DWITH_TEXTURETOOLS=OFF \
-DWITH_TGAIMPORTER=ON \
-DWITH_OPENGLTESTER=ON \
-DTARGET_GLES2=$TARGET_GLES2 \
-G Ninja
Expand Down
8 changes: 5 additions & 3 deletions package/ci/travis-ios-simulator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@ cmake .. \
-DCORRADE_RC_EXECUTABLE=$HOME/deps-native/bin/corrade-rc \
-DCMAKE_INSTALL_PREFIX=$HOME/deps \
-DCMAKE_PREFIX_PATH=$TRAVIS_BUILD_DIR/sdl2 \
-DWITH_ANYIMAGEIMPORTER=ON \
-DWITH_AUDIO=OFF \
-DWITH_DEBUGTOOLS=OFF \
-DWITH_MESHTOOLS=OFF \
-DWITH_PRIMITIVES=OFF \
-DWITH_DEBUGTOOLS=ON \
-DWITH_MESHTOOLS=ON \
-DWITH_PRIMITIVES=ON \
-DWITH_SCENEGRAPH=OFF \
-DWITH_SCENETOOLS=OFF \
-DWITH_SHADERS=ON \
-DWITH_SHADERTOOLS=OFF \
-DWITH_TEXT=OFF \
-DWITH_TEXTURETOOLS=OFF \
-DWITH_TGAIMPORTER=ON \
-DWITH_OPENGLTESTER=ON \
-DWITH_SDL2APPLICATION=ON \
-DTARGET_GLES2=$TARGET_GLES2 \
Expand Down
24 changes: 20 additions & 4 deletions package/ci/unix-desktop-gles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,40 @@ cmake .. \
-DTARGET_GLES=ON \
-DTARGET_GLES2=$TARGET_GLES2 \
-DWITH_AUDIO=OFF \
-DWITH_DEBUGTOOLS=OFF \
-DWITH_MESHTOOLS=OFF \
-DWITH_PRIMITIVES=OFF \
-DWITH_DEBUGTOOLS=ON \
-DWITH_MESHTOOLS=ON \
-DWITH_PRIMITIVES=ON \
-DWITH_SCENEGRAPH=ON \
-DWITH_SCENETOOLS=OFF \
-DWITH_SHADERS=ON \
-DWITH_SHADERTOOLS=OFF \
-DWITH_TEXT=OFF \
-DWITH_TEXTURETOOLS=OFF \
-DWITH_OPENGLTESTER=ON \
-DWITH_ANYIMAGEIMPORTER=OFF \
-DWITH_ANYIMAGEIMPORTER=ON \
-DWITH_TGAIMPORTER=ON \
-DWITH_SDL2APPLICATION=ON \
-DWITH_WINDOWLESS${PLATFORM_GL_API}APPLICATION=ON \
-DWITH_SDL2APPLICATION=ON \
-G Ninja
ninja install
cd ../..

# Magnum Plugins
# Required by ImGuiIntegration tests
git clone --depth 1 https://github.com/mosra/magnum-plugins.git
cd magnum-plugins
mkdir build && cd build
cmake .. \
-DCMAKE_CXX_FLAGS="$CMAKE_CXX_FLAGS" \
-DCMAKE_INSTALL_PREFIX=$HOME/deps \
-DCMAKE_INSTALL_RPATH=$HOME/deps/lib \
-DCMAKE_BUILD_TYPE=$CONFIGURATION \
-DWITH_STBIMAGEIMPORTER=ON \
-G Ninja
ninja install
cd ../..

mkdir build && cd build
cmake .. \
-DCMAKE_CXX_FLAGS="$CMAKE_CXX_FLAGS" \
Expand Down
7 changes: 4 additions & 3 deletions package/ci/unix-desktop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ cmake .. \
-DCMAKE_INSTALL_RPATH=$HOME/deps/lib \
-DCMAKE_BUILD_TYPE=$CONFIGURATION \
-DWITH_AUDIO=OFF \
-DWITH_DEBUGTOOLS=OFF \
-DWITH_MESHTOOLS=$WITH_DART \
-DWITH_PRIMITIVES=$WITH_DART \
-DWITH_DEBUGTOOLS=ON \
-DWITH_MESHTOOLS=ON \
-DWITH_PRIMITIVES=ON \
-DWITH_SCENEGRAPH=ON \
-DWITH_SCENETOOLS=OFF \
-DWITH_SHADERS=ON \
Expand All @@ -38,6 +38,7 @@ cmake .. \
-DWITH_TEXTURETOOLS=OFF \
-DWITH_OPENGLTESTER=ON \
-DWITH_ANYIMAGEIMPORTER=ON \
-DWITH_TGAIMPORTER=ON \
-DWITH_SDL2APPLICATION=ON \
-DWITH_WINDOWLESS${PLATFORM_GL_API}APPLICATION=ON \
-DWITH_SDL2APPLICATION=ON \
Expand Down
28 changes: 25 additions & 3 deletions src/Magnum/ImGuiIntegration/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ Context::Context(ImGuiContext& context, const Vector2& size, const Vector2i& win
/* Tell ImGui that changing mouse cursors is supported */
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;

/* Check if we can support base vertex > 0 in draw commands */
#if !(defined(MAGNUM_TARGET_WEBGL) && defined(MAGNUM_TARGET_GLES2))
if(
#if defined(MAGNUM_TARGET_WEBGL)
GL::Context::current().isExtensionSupported<GL::Extensions::WEBGL::draw_instanced_base_vertex_base_instance>()
#elif defined(MAGNUM_TARGET_GLES)
/* Supported since GLES 3.2 but the extensions aren't fully covered by
3.2 (and hence not in core) so we still need the version check */
GL::Context::current().isVersionSupported(GL::Version::GLES320) ||
GL::Context::current().isExtensionSupported<GL::Extensions::OES::draw_elements_base_vertex>() ||
GL::Context::current().isExtensionSupported<GL::Extensions::EXT::draw_elements_base_vertex>()
#else
/* Core since OpenGL 3.2 */
GL::Context::current().isExtensionSupported<GL::Extensions::ARB::draw_elements_base_vertex>()
#endif
) {
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset;
}
#endif

/** @todo Set clipboard text once Platform supports it */

/* Set up framebuffer sizes, font supersampling etc. and upload the glyph
Expand All @@ -117,7 +137,7 @@ Context::Context(Context&& other) noexcept: _context{other._context}, _shader{st
/* Update the pointer to _texture */
ImGuiContext* current = ImGui::GetCurrentContext();
ImGui::SetCurrentContext(_context);
ImGui::GetIO().Fonts->SetTexID(reinterpret_cast<ImTextureID>(&_texture));
ImGui::GetIO().Fonts->SetTexID(static_cast<ImTextureID>(&_texture));
ImGui::SetCurrentContext(current);
}

Expand All @@ -144,11 +164,11 @@ Context& Context::operator=(Context&& other) noexcept {
ImGuiContext* current = ImGui::GetCurrentContext();
if(_context) {
ImGui::SetCurrentContext(_context);
ImGui::GetIO().Fonts->SetTexID(reinterpret_cast<ImTextureID>(&_texture));
ImGui::GetIO().Fonts->SetTexID(static_cast<ImTextureID>(&_texture));
}
if(other._context) {
ImGui::SetCurrentContext(other._context);
ImGui::GetIO().Fonts->SetTexID(reinterpret_cast<ImTextureID>(&other._texture));
ImGui::GetIO().Fonts->SetTexID(static_cast<ImTextureID>(&other._texture));
}
ImGui::SetCurrentContext(current);

Expand Down Expand Up @@ -324,6 +344,8 @@ void Context::drawFrame() {
{pcmd->ClipRect.z, fbSize.y() - pcmd->ClipRect.y}}
.scaled(_supersamplingRatio)});

/* Only > 0 if ImGuiBackendFlags_RendererHasVtxOffset is set */
_mesh.setBaseVertex(pcmd->VtxOffset);
_mesh.setCount(pcmd->ElemCount);
_mesh.setIndexBuffer(_indexBuffer, pcmd->IdxOffset*sizeof(ImDrawIdx),
pezcode marked this conversation as resolved.
Show resolved Hide resolved
sizeof(ImDrawIdx) == 2
Expand Down
30 changes: 30 additions & 0 deletions src/Magnum/ImGuiIntegration/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,36 @@ If you don't do that, the fonts stay at the original scale, not matching the
new UI scaling anymore. If you didn't supply any custom font, the function will
reconfigure the builtin font automatically.

@section ImGuiIntegration-Context-large-meshes Large meshes

Complex user interfaces or widgets like [ImPlot](https://github.com/epezent/implot)
may end up creating large meshes with more than 65k vertices. Because ImGui
defaults to 16-bit index buffers this can lead to asserts or visual errors.

If the underlying GL context supports @ref GL::Mesh::setBaseVertex() "setting the base vertex for indexed meshes"
the rendering backend sets the @cpp ImGuiBackendFlags_RendererHasVtxOffset @ce
flag. This lets ImGui know the backend can handle per-draw vertex offsets,
removing the 65k limitation altogether. Support for that requires one of the
following:

@requires_gl32 Extension @gl_extension{ARB,draw_elements_base_vertex}
@requires_gles32 Extension @gl_extension{OES,draw_elements_base_vertex} or
@gl_extension{EXT,draw_elements_base_vertex} on OpenGL ES 3.1 and older
@requires_webgl_extension WebGL 2.0 and extension
@webgl_extension{WEBGL,draw_instanced_base_vertex_base_instance}

If you can't guarantee that the required GL versions or extensions will be
available at runtime (mostly relevant on WebGL) the next best option is to
change ImGui's index type to 32-bit by adding the following line to the
@ref ImGuiIntegration-configuration "ImGui user config":

@code{.cpp}
#define ImDrawIdx unsigned int
@endcode

This doubles the size of the index buffer but is guaranteed to work on all GL
versions.

@section ImGuiIntegration-Context-multiple-contexts Multiple contexts

Each instance of @ref Context creates a new ImGui context. You can also pass an
Expand Down
36 changes: 35 additions & 1 deletion src/Magnum/ImGuiIntegration/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,42 @@ set_target_properties(
PROPERTIES FOLDER "Magnum/ImGuiIntegration/Test")

if(BUILD_GL_TESTS)
find_package(Corrade REQUIRED PluginManager)
find_package(Magnum REQUIRED Trade DebugTools)

if(CORRADE_TARGET_EMSCRIPTEN OR CORRADE_TARGET_ANDROID)
set(IMGUIINTEGRATION_TEST_DIR ".")
else()
set(IMGUIINTEGRATION_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR})
endif()

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configure.h.cmake
${CMAKE_CURRENT_BINARY_DIR}/configure.h)

corrade_add_test(ImGuiContextGLTest ContextGLTest.cpp
LIBRARIES MagnumImGuiIntegration Magnum::OpenGLTester)
LIBRARIES
MagnumImGuiIntegration
Magnum::Trade
Magnum::DebugTools
Magnum::OpenGLTester
FILES
TestFiles/draw.png
TestFiles/drawScissor.png
TestFiles/drawTexture.png
texture.png)
target_include_directories(ImGuiContextGLTest PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
if(MAGNUM_IMGUIINTEGRATION_BUILD_STATIC)
# Not required
find_package(Magnum COMPONENTS AnyImageImporter)
find_package(MagnumPlugins COMPONENTS StbImageImporter)
if(Magnum_AnyImageImporter_FOUND)
target_link_libraries(ImGuiContextGLTest PRIVATE Magnum::AnyImageImporter)
endif()
if(MagnumPlugins_StbImageImporter_FOUND)
target_link_libraries(ImGuiContextGLTest PRIVATE MagnumPlugins::StbImageImporter)
endif()
endif()

corrade_add_test(ImGuiWidgetsGLTest WidgetsGLTest.cpp
LIBRARIES MagnumImGuiIntegration Magnum::OpenGLTester)

Expand Down
Loading