Created
March 31, 2026 13:58
-
-
Save jamesu/cf35fb922b7008ed0b461001775db25b to your computer and use it in GitHub Desktop.
ImGUI Impl for Torque3D GFX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Copyright 2026 James S Urquhart | |
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), | |
| to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
| and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
| WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| */ | |
| #include "imgui.h" | |
| #include "imgui_impl.h" | |
| #include "gfx/gfxDevice.h" | |
| #include "gfx/gfxTextureHandle.h" | |
| #include "gfx/gfxDrawUtil.h" | |
| #include "gfx/gfxVertexBuffer.h" | |
| #include "gfx/gfxPrimitiveBuffer.h" | |
| #include "gfx/gfxShader.h" | |
| #include "gfx/gfxStateBlock.h" | |
| #include "gfx/gfxStructs.h" | |
| #include "core/util/tVector.h" | |
| #include "math/mRect.h" | |
| #include "platform/platformAssert.h" | |
| // Torque-specific helper state | |
| struct ImGui_ImplTorqueGFX_Texture | |
| { | |
| GFXTexHandle texture; | |
| }; | |
| struct ImGui_ImplTorqueGFX_Data | |
| { | |
| // GPU state / objects | |
| GFXStateBlockRef stateBlock; | |
| GFXVertexBufferHandle<GFXVertexPCT> vertexBuffer; | |
| GFXPrimitiveBufferHandle indexBuffer; | |
| U32 vertexBufferSize; | |
| U32 indexBufferSize; | |
| ImGui_ImplTorqueGFX_Data() : | |
| vertexBufferSize(5000), | |
| indexBufferSize(10000) | |
| { | |
| } | |
| }; | |
| static ImGui_ImplTorqueGFX_Data* ImGui_ImplTorqueGFX_GetBackendData() | |
| { | |
| return ImGui::GetCurrentContext() ? (ImGui_ImplTorqueGFX_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; | |
| } | |
| // Small helpers | |
| static void ImGui_ImplTorqueGFX_SetupRenderState(ImDrawData* draw_data) | |
| { | |
| ImGui_ImplTorqueGFX_Data* bd = ImGui_ImplTorqueGFX_GetBackendData(); | |
| GFXDevice* gfx = GFX; | |
| RectI vp(0, 0, | |
| (S32)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x), | |
| (S32)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y)); | |
| gfx->setClipRect(vp); | |
| gfx->setStateBlock(bd->stateBlock); | |
| gfx->setupGenericShaders(GFXDevice::GSModColorTexture); | |
| } | |
| static void ImGui_ImplTorqueGFX_DestroyTexture(ImTextureData* tex) | |
| { | |
| ImGui_ImplTorqueGFX_Texture* backend_tex = (ImGui_ImplTorqueGFX_Texture*)tex->BackendUserData; | |
| if (backend_tex) | |
| { | |
| AssertFatal((GFXTextureObject*)(intptr_t)tex->TexID == (GFXTextureObject*)backend_tex->texture, "mismatch"); | |
| backend_tex->texture.free(); | |
| IM_DELETE(backend_tex); | |
| tex->SetTexID(ImTextureID_Invalid); | |
| tex->BackendUserData = nullptr; | |
| } | |
| tex->SetStatus(ImTextureStatus_Destroyed); | |
| } | |
| // Texture updates | |
| void ImGui_ImplTorqueGFX_UpdateTexture(ImTextureData* tex) | |
| { | |
| if (tex->Status == ImTextureStatus_WantCreate) | |
| { | |
| AssertFatal(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr, "mismatch"); | |
| AssertFatal(tex->Format == ImTextureFormat_RGBA32, "mismatch"); | |
| ImGui_ImplTorqueGFX_Texture* backend_tex = IM_NEW(ImGui_ImplTorqueGFX_Texture)(); | |
| backend_tex->texture.set( | |
| (U32)tex->Width, | |
| (U32)tex->Height, | |
| GFXFormatR8G8B8A8, | |
| &GFXStaticTextureProfile, | |
| String::ToString("ImGuiTex_%u", tex->UniqueID), | |
| 1); | |
| AssertFatal(bool(backend_tex->texture), "Failed to create GFX texture for ImGui"); | |
| if (GFXLockedRect* lock = backend_tex->texture.lock()) | |
| { | |
| const U8* src = (const U8*)tex->GetPixels(); | |
| U8* dst = (U8*)lock->bits; | |
| const U32 src_pitch = (U32)tex->GetPitch(); | |
| const U32 dst_pitch = (U32)lock->pitch; | |
| const U32 row_bytes = (U32)tex->Width * 4; | |
| for (int y = 0; y < tex->Height; ++y) | |
| { | |
| memcpy(dst + dst_pitch * y, src + src_pitch * y, row_bytes); | |
| } | |
| backend_tex->texture.unlock(); | |
| } | |
| tex->BackendUserData = backend_tex; | |
| tex->SetTexID((ImTextureID)(intptr_t)(GFXTextureObject*)backend_tex->texture); | |
| tex->SetStatus(ImTextureStatus_OK); | |
| } | |
| else if (tex->Status == ImTextureStatus_WantUpdates) | |
| { | |
| ImGui_ImplTorqueGFX_Texture* backend_tex = (ImGui_ImplTorqueGFX_Texture*)tex->BackendUserData; | |
| AssertFatal(backend_tex != nullptr, "no tex"); | |
| if (GFXLockedRect* lock = backend_tex->texture.lock()) | |
| { | |
| const U8* src = (const U8*)tex->GetPixels(); | |
| U8* dst = (U8*)lock->bits; | |
| const U32 src_pitch = (U32)tex->GetPitch(); | |
| const U32 dst_pitch = (U32)lock->pitch; | |
| const U32 row_bytes = (U32)tex->Width * 4; | |
| for (int y = 0; y < tex->Height; ++y) | |
| { | |
| memcpy(dst + dst_pitch * y, src + src_pitch * y, row_bytes); | |
| } | |
| backend_tex->texture.unlock(); | |
| } | |
| tex->SetStatus(ImTextureStatus_OK); | |
| } | |
| if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0) | |
| { | |
| ImGui_ImplTorqueGFX_DestroyTexture(tex); | |
| } | |
| } | |
| // Device Objects | |
| bool ImGui_ImplTorqueGFX_CreateDeviceObjects() | |
| { | |
| ImGui_ImplTorqueGFX_Data* bd = ImGui_ImplTorqueGFX_GetBackendData(); | |
| if (bd == nullptr || GFX == nullptr) | |
| { | |
| return false; | |
| } | |
| ImGui_ImplTorqueGFX_InvalidateDeviceObjects(); | |
| GFXStateBlockDesc desc; | |
| desc.setCullMode(GFXCullNone); | |
| desc.setZReadWrite(false, false); | |
| desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha, GFXBlendOpAdd); | |
| desc.samplersDefined = true; | |
| desc.samplers[0].textureColorOp = GFXTOPModulate; | |
| desc.samplers[0].addressModeU = GFXAddressClamp; | |
| desc.samplers[0].addressModeV = GFXAddressClamp; | |
| desc.samplers[0].addressModeW = GFXAddressClamp; | |
| desc.samplers[0].magFilter = GFXTextureFilterLinear; | |
| desc.samplers[0].minFilter = GFXTextureFilterLinear; | |
| desc.samplers[0].mipFilter = GFXTextureFilterLinear; | |
| //desc.scissorTestEnable = true; // NOTE: Use if you have a scissor test | |
| bd->stateBlock = GFX->createStateBlock(desc); | |
| return true; | |
| } | |
| void ImGui_ImplTorqueGFX_InvalidateDeviceObjects() | |
| { | |
| ImGui_ImplTorqueGFX_Data* bd = ImGui_ImplTorqueGFX_GetBackendData(); | |
| if (!bd) | |
| { | |
| return; | |
| } | |
| for (ImTextureData* tex : ImGui::GetPlatformIO().Textures) | |
| { | |
| if (tex->RefCount == 1) | |
| { | |
| ImGui_ImplTorqueGFX_DestroyTexture(tex); | |
| } | |
| } | |
| bd->vertexBuffer = nullptr; | |
| bd->indexBuffer = nullptr; | |
| bd->stateBlock = nullptr; | |
| } | |
| // Backend | |
| bool ImGui_ImplTorqueGFX_Init() | |
| { | |
| ImGuiIO& io = ImGui::GetIO(); | |
| IMGUI_CHECKVERSION(); | |
| AssertFatal(io.BackendRendererUserData == nullptr, "Already initialized a renderer backend!"); | |
| ImGui_ImplTorqueGFX_Data* bd = IM_NEW(ImGui_ImplTorqueGFX_Data)(); | |
| io.BackendRendererUserData = bd; | |
| io.BackendRendererName = "imgui_impl_torque_gfx"; | |
| io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; | |
| io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; | |
| ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); | |
| platform_io.Renderer_TextureMaxWidth = 16384; | |
| platform_io.Renderer_TextureMaxHeight = 16384; | |
| return true; | |
| } | |
| void ImGui_ImplTorqueGFX_Shutdown() | |
| { | |
| ImGui_ImplTorqueGFX_Data* bd = ImGui_ImplTorqueGFX_GetBackendData(); | |
| AssertFatal(bd != nullptr, "Not initialized"); | |
| ImGuiIO& io = ImGui::GetIO(); | |
| ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); | |
| ImGui_ImplTorqueGFX_InvalidateDeviceObjects(); | |
| io.BackendRendererName = nullptr; | |
| io.BackendRendererUserData = nullptr; | |
| io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures); | |
| platform_io.ClearRendererHandlers(); | |
| IM_DELETE(bd); | |
| } | |
| void ImGui_ImplTorqueGFX_NewFrame() | |
| { | |
| ImGui_ImplTorqueGFX_Data* bd = ImGui_ImplTorqueGFX_GetBackendData(); | |
| AssertFatal(bd != nullptr, "Context or backend not initialized"); | |
| if (!bd->stateBlock) | |
| { | |
| if (!ImGui_ImplTorqueGFX_CreateDeviceObjects()) | |
| { | |
| AssertFatal(0, "ImGui_ImplTorqueGFX_CreateDeviceObjects() failed"); | |
| } | |
| } | |
| } | |
| // Rendering | |
| void ImGui_ImplTorqueGFX_RenderDrawData(ImDrawData* draw_data) | |
| { | |
| if (draw_data->DisplaySize.x <= 0.0f || | |
| draw_data->DisplaySize.y <= 0.0f) | |
| { | |
| return; | |
| } | |
| ImGui_ImplTorqueGFX_Data* bd = ImGui_ImplTorqueGFX_GetBackendData(); | |
| GFXDevice* gfx = GFX; | |
| if (draw_data->Textures != nullptr) | |
| { | |
| for (ImTextureData* tex : *draw_data->Textures) | |
| { | |
| if (tex->Status != ImTextureStatus_OK) | |
| { | |
| ImGui_ImplTorqueGFX_UpdateTexture(tex); | |
| } | |
| } | |
| } | |
| if (!bd->vertexBuffer.isValid() || | |
| bd->vertexBufferSize < (U32)draw_data->TotalVtxCount) | |
| { | |
| bd->vertexBufferSize = (U32)draw_data->TotalVtxCount + 5000; | |
| bd->vertexBuffer.set(GFX, bd->vertexBufferSize, GFXBufferTypeDynamic); | |
| } | |
| if (!bd->indexBuffer.isValid() || | |
| bd->indexBufferSize < (U32)draw_data->TotalIdxCount) | |
| { | |
| bd->indexBufferSize = (U32)draw_data->TotalIdxCount + 10000; | |
| bd->indexBuffer.set(GFX, bd->indexBufferSize, 0, GFXBufferTypeDynamic); | |
| } | |
| GFXVertexPCT* vtx_dst = bd->vertexBuffer.lock(); | |
| GFXPrimitive* prim = nullptr; | |
| U16* idx_dst = nullptr; | |
| bd->indexBuffer.lock(&idx_dst, &prim); | |
| for (const ImDrawList* draw_list : draw_data->CmdLists) | |
| { | |
| ImDrawVert* inVert = draw_list->VtxBuffer.Data; | |
| for (U32 i = 0; i < draw_list->VtxBuffer.Size; i++) | |
| { | |
| vtx_dst[i].point = Point3F(inVert->pos.x, inVert->pos.y, 0.0f); | |
| vtx_dst[i].color.setPacked(inVert->col); | |
| vtx_dst[i].texCoord = Point2F(inVert->uv.x, inVert->uv.y); | |
| inVert++; | |
| } | |
| memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx)); | |
| vtx_dst += draw_list->VtxBuffer.Size; | |
| idx_dst += draw_list->IdxBuffer.Size; | |
| } | |
| bd->indexBuffer.unlock(); | |
| bd->vertexBuffer.unlock(); | |
| ImGui_ImplTorqueGFX_SetupRenderState(draw_data); | |
| ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); | |
| ImGui_ImplTorqueGFX_RenderState render_state; | |
| render_state.stateBlock = bd->stateBlock; | |
| platform_io.Renderer_RenderState = &render_state; | |
| gfx->setVertexBuffer(bd->vertexBuffer); | |
| gfx->setPrimitiveBuffer(bd->indexBuffer); | |
| const ImVec2 clip_off = draw_data->DisplayPos; | |
| const ImVec2 clip_scale = draw_data->FramebufferScale; | |
| U32 global_vtx_offset = 0; | |
| U32 global_idx_offset = 0; | |
| for (const ImDrawList* draw_list : draw_data->CmdLists) | |
| { | |
| for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++) | |
| { | |
| const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i]; | |
| if (pcmd->UserCallback != nullptr) | |
| { | |
| if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) | |
| { | |
| ImGui_ImplTorqueGFX_SetupRenderState(draw_data); | |
| } | |
| else | |
| { | |
| pcmd->UserCallback(draw_list, pcmd); | |
| } | |
| continue; | |
| } | |
| ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, | |
| (pcmd->ClipRect.y - clip_off.y) * clip_scale.y); | |
| ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, | |
| (pcmd->ClipRect.w - clip_off.y) * clip_scale.y); | |
| if (clip_max.x <= clip_min.x || | |
| clip_max.y <= clip_min.y) | |
| { | |
| continue; | |
| } | |
| RectI scissor((S32)clip_min.x, | |
| (S32)clip_min.y, | |
| (S32)(clip_max.x - clip_min.x), | |
| (S32)(clip_max.y - clip_min.y)); | |
| gfx->setScissorRect(scissor); // NOTE: needed for proper clipping | |
| GFXTextureObject* texture = (GFXTextureObject*)(intptr_t)pcmd->GetTexID(); | |
| gfx->setTexture(0, texture); | |
| gfx->drawIndexedPrimitive( | |
| GFXTriangleList, | |
| global_vtx_offset + pcmd->VtxOffset, | |
| 0, | |
| draw_list->VtxBuffer.Size - pcmd->VtxOffset, | |
| global_idx_offset + pcmd->IdxOffset, | |
| pcmd->ElemCount / 3); | |
| } | |
| global_idx_offset += draw_list->IdxBuffer.Size; | |
| global_vtx_offset += draw_list->VtxBuffer.Size; | |
| } | |
| platform_io.Renderer_RenderState = nullptr; | |
| } | |
| namespace ImGuiGfxBridge | |
| { | |
| ImTextureID GetImTextureID(GFXTextureObject* tex) | |
| { | |
| return tex ? (ImTextureID)(intptr_t)tex : ImTextureID_Invalid; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Copyright 2026 James S Urquhart | |
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), | |
| to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
| and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
| WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| */ | |
| // ImgGUI Torque3D GFX impl | |
| // | |
| #pragma once | |
| #include "imgui.h" | |
| #ifndef IMGUI_DISABLE | |
| class GFXShader; | |
| class GFXShaderConstBuffer; | |
| class GFXStateBlock; | |
| class GFXTextureObject; | |
| class GFXTexHandle; | |
| IMGUI_IMPL_API bool ImGui_ImplTorqueGFX_Init(); | |
| IMGUI_IMPL_API void ImGui_ImplTorqueGFX_Shutdown(); | |
| IMGUI_IMPL_API void ImGui_ImplTorqueGFX_NewFrame(); | |
| IMGUI_IMPL_API void ImGui_ImplTorqueGFX_RenderDrawData(ImDrawData* draw_data); | |
| IMGUI_IMPL_API bool ImGui_ImplTorqueGFX_CreateDeviceObjects(); | |
| IMGUI_IMPL_API void ImGui_ImplTorqueGFX_InvalidateDeviceObjects(); | |
| IMGUI_IMPL_API void ImGui_ImplTorqueGFX_UpdateTexture(ImTextureData* tex); | |
| struct ImGui_ImplTorqueGFX_RenderState | |
| { | |
| GFXShader* Shader; | |
| GFXShaderConstBuffer* ShaderConstBuffer; | |
| GFXStateBlock* StateBlock; | |
| }; | |
| namespace ImGuiGfxBridge | |
| { | |
| ImTextureID GetImTextureID(GFXTextureObject* tex); | |
| } | |
| #endif // #ifndef IMGUI_DISABLE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment