Skip to content

Latest commit

 

History

History
91 lines (60 loc) · 7.94 KB

File metadata and controls

91 lines (60 loc) · 7.94 KB

ImGui4NET multi-renderer app

A .NET 10 starter that opens the built-in Dear ImGui demo window. The renderer is selected at launch. Desktop uses GLFW; Android shares the Vulkan renderer but does not require GLFW. The desktop window title and Android launcher label follow the generated project name.

Put shared UI and application state in Application.cs. Both hosts use the IApplication contract and start with SimpleApplication, which renders the built-in demo. Program.cs hosts the desktop app, and MainActivity.cs hosts the optional Android app.

Application hook When it runs
Create() / Dispose() Once per app instance. Android pause, Surface recreation, and renderer switching do not recreate the app. No current ImGui or GPU context is guaranteed in these hooks.
Resize(width, height) Before rendering the first valid frame and whenever the framebuffer dimensions change. Dimensions are pixels; minimized or unavailable framebuffers are skipped.
BeginRender() / Render(deltaSeconds) / EndRender() Build the ImGui frame in application code. BeginRender() starts the default ImGui frame and selects render options; EndRender() returns the draw data.
GraphicsCreated / GraphicsDisposing Once per renderer session, with the GL context current or Vulkan device alive. Create backend-dependent resources here, and release them before the renderer is destroyed. The renderer waits for GPU work before GraphicsDisposing.
DrawBeforeImGui / DrawAfterImGui Issue native graphics commands before or after the renderer records ImGui draws, on the current render target.
KeyDown / KeyUp / TextInput Mapped ImGuiKey press/release events (no repeat presses), and Unicode code points from text input. Android supports hardware keyboard text and committed soft-keyboard/IME text.
PointerDown / PointerMove / PointerUp / PointerCancel / Scroll Window-local pointer coordinates as used by ImGui, or scroll deltas. Android touch uses button 0 for the primary pointer.

Input and resize hooks can update shared state; submit ImGui widgets in Render. The app also receives events handled by ImGui.

To skip the built-in color clear, return new RenderOptions(ClearColor: false) from SimpleApplication.BeginRender(). Pixels the app and ImGui do not draw are then undefined; this does not preserve the previous swapchain image. To skip ImGui submission for a frame, return null from EndRender() after finishing any ImGui frame that was started. The native draw hooks still run.

  • OpenGL / OpenGL ES: the GL context is current in the native draw hooks. Use the corresponding API and restore the window framebuffer if you temporarily change it. The host still swaps buffers.
  • Vulkan: context.Graphics.Vulkan provides the device and graphics queue, and context.Vulkan provides the recording command buffer, active render pass, framebuffer, and color format. Both draw hooks run inside the same color pass. Bind your own graphics state; don't begin/end the pass, submit that command buffer, or present. Treat the command buffer and framebuffer as borrowed for the callback, and build pipelines compatible with the current pass and color format (which can change on swapchain recreation).

Native triangle example

Set EnableTriangle to true in Application.cs to draw a native RGB triangle behind the ImGui demo on OpenGL, OpenGL ES, or Vulkan. The example is included in every generated project but disabled by default.

  • GlTriangle builds GLSL and a VAO when the context is created, then calls glDrawArrays in DrawBeforeImGui.
  • VulkanTriangle builds its pipeline with the active Vulkan render pass and records CmdDraw in DrawBeforeImGui. Its SPIR-V is embedded, so running the example does not require a shader compiler.

Both release their GPU resources in GraphicsDisposing. Keep the default clear enabled because the triangle does not cover the whole target.

Platform OpenGL 4.1 (opengl, desktop default) OpenGL ES 3.0 (gles) Vulkan (vulkan)
Windows x64 Yes Requires a driver with WGL ES profile support Requires a Vulkan loader and driver
Linux x64 Yes Requires EGL and a GLES 3 driver Requires a Vulkan loader and ICD
macOS x64 / arm64 Yes No native GLFW ES context Requires a Vulkan loader and MoltenVK
Android arm64, API 24+ No Yes, on GLES 3 devices Yes, on Vulkan devices

Run from the repository

dotnet run --project .\samples\ImGui4NET.Template.MultiPlatforms -- --renderer opengl
dotnet run --project .\samples\ImGui4NET.Template.MultiPlatforms -- --renderer gles
dotnet run --project .\samples\ImGui4NET.Template.MultiPlatforms -- --renderer vulkan

Use --help for the desktop command-line options.

Generate a standalone app

Install the template from NuGet with the .NET 10 SDK, then generate the desktop app:

dotnet new install ImGui4NET.Template.MultiPlatforms
dotnet new imgui4net-app -n MyApp

To try changes from a checkout instead, install from the repository root with dotnet new install .\samples\ImGui4NET.Template.MultiPlatforms.

Use --android to generate the desktop and Android projects together. To choose a shared C# root namespace and an Android package name:

dotnet new imgui4net-app -n MyApp --android --rootNamespace Contoso.Tools.MyApp --appId com.contoso.myapp

--rootNamespace updates both projects and their source namespaces without renaming the project files. It defaults to the project name converted to a valid namespace. --appId is Android-only and sets both the package name and the prefix of the launcher activity name. Use a valid Android package name with at least two dot-separated segments, such as com.contoso.myapp. It defaults to com.imgui4net.sample.

From the generated root, run dotnet run -- --renderer vulkan (or use opengl / gles). The desktop build never needs the Android workload, even when both projects are generated.

macOS Vulkan

Install the LunarG Vulkan SDK or a compatible Vulkan loader plus the MoltenVK ICD. Make the loader and ICD discoverable to the process; if using the SDK outside its system install, source the SDK's setup-env.sh before starting the app. The runtime enables Vulkan portability enumeration and MoltenVK's portability subset when advertised.

The generated project uses the Vulkan backend package, which ships the native ImGui backend for both macOS x64 and arm64. The Vulkan loader and MoltenVK are still required separately.

Android

Install the .NET 10 android workload, the Android SDK/JDK it requires, and the Android NDK (r27c or newer). Connect an arm64 device with USB debugging enabled (or use an arm64 emulator with the required graphics support). Build or deploy the optional project explicitly:

dotnet build .\Android\MyApp.Android.csproj -p:AndroidNdkDirectory=C:\Android\Sdk\ndk\27.2.12479018
dotnet build .\Android\MyApp.Android.csproj -t:Run -p:AndroidNdkDirectory=C:\Android\Sdk\ndk\27.2.12479018

Use your installed NDK path. Android starts with OpenGL ES 3.0 by default. Use Choose renderer to switch to Vulkan, or pass the string intent extra renderer with value gles or vulkan for scripted launches.

Tap an editable ImGui text field to open the Android keyboard. Drag to select text, or long-press to select a word, to show Android’s floating Cut, Copy, Paste, and Select all menu. Read-only fields only offer Copy and Select all; password fields do not show the selection menu. IME composition stays in the keyboard until committed to ImGui.

Change ApplicationId before distributing your own app. The pinned ImGui4NET Android packages contain 4 KiB-aligned native libraries; use 16 KiB-compatible builds of those libraries if you target 16 KiB devices or distribute through Google Play.