Skip to content

Commit 4328aef

Browse files
bear24rwMax Thrun
authored and
Max Thrun
committed
Examples/Backends: Metal: Added support for large meshes (64k+ vertices) with 16-bits indices, enable 'ImGuiBackendFlags_HasVtxOffset' config flag in back-end. (ocornut#2591)
1 parent 7755cbb commit 4328aef

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

Diff for: docs/CHANGELOG.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Other Changes:
7171
- Backends: Add native Mac clipboard copy/paste default implementation in core library to match what we are
7272
dealing with Win32, and to facilitate integration in custom engines. (#2546) [@andrewwillmott]
7373
- Backends: OSX: imgui_impl_osx: Added mouse cursor support. (#2585, #1873) [@actboy168]
74-
- Examples/Backends: DirectX9/10/11/12, Vulkan, OpenGL3 (Desktop GL only): Added support for large meshes
74+
- Examples/Backends: DirectX9/10/11/12, Metal, Vulkan, OpenGL3 (Desktop GL only): Added support for large meshes
7575
(64k+ vertices) with 16-bits indices, enable 'ImGuiBackendFlags_RendererHasVtxOffset' in those back-ends.
7676
- Examples/Backends: Don't filter characters under 0x10000 before calling io.AddInputCharacter(),
7777
the filtering is done in io.AddInputCharacter() itself. This is in prevision for fuller Unicode

Diff for: examples/imgui_impl_metal.mm

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
// Implemented features:
55
// [X] Renderer: User texture binding. Use 'MTLTexture' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
6+
// [X] Renderer: Support for large meshes (64k+ vertices) with 16-bits indices.
67

78
// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
89
// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
910
// https://github.com/ocornut/imgui
1011

1112
// CHANGELOG
1213
// (minor and older changes stripped away, please see git history for details)
14+
// 2019-05-29: Metal: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_HasVtxOffset flag.
1315
// 2019-04-30: Metal: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
1416
// 2019-02-11: Metal: Projecting clipping rectangles correctly using draw_data->FramebufferScale to allow multi-viewports for retina display.
1517
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
@@ -76,6 +78,7 @@ bool ImGui_ImplMetal_Init(id<MTLDevice> device)
7678
{
7779
ImGuiIO& io = ImGui::GetIO();
7880
io.BackendRendererName = "imgui_impl_metal";
81+
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset;
7982

8083
static dispatch_once_t onceToken;
8184
dispatch_once(&onceToken, ^{
@@ -478,13 +481,10 @@ - (void)renderDrawData:(ImDrawData *)drawData
478481
for (int n = 0; n < drawData->CmdListsCount; n++)
479482
{
480483
const ImDrawList* cmd_list = drawData->CmdLists[n];
481-
ImDrawIdx idx_buffer_offset = 0;
482484

483485
memcpy((char *)vertexBuffer.buffer.contents + vertexBufferOffset, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
484486
memcpy((char *)indexBuffer.buffer.contents + indexBufferOffset, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
485487

486-
[commandEncoder setVertexBufferOffset:vertexBufferOffset atIndex:0];
487-
488488
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
489489
{
490490
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
@@ -522,14 +522,15 @@ - (void)renderDrawData:(ImDrawData *)drawData
522522
// Bind texture, Draw
523523
if (pcmd->TextureId != NULL)
524524
[commandEncoder setFragmentTexture:(__bridge id<MTLTexture>)(pcmd->TextureId) atIndex:0];
525+
526+
[commandEncoder setVertexBufferOffset:(vertexBufferOffset + pcmd->VtxOffset * sizeof(ImDrawVert)) atIndex:0];
525527
[commandEncoder drawIndexedPrimitives:MTLPrimitiveTypeTriangle
526528
indexCount:pcmd->ElemCount
527529
indexType:sizeof(ImDrawIdx) == 2 ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32
528530
indexBuffer:indexBuffer.buffer
529-
indexBufferOffset:indexBufferOffset + idx_buffer_offset];
531+
indexBufferOffset:indexBufferOffset + pcmd->IdxOffset * sizeof(ImDrawIdx)];
530532
}
531533
}
532-
idx_buffer_offset += pcmd->ElemCount * sizeof(ImDrawIdx);
533534
}
534535

535536
vertexBufferOffset += cmd_list->VtxBuffer.Size * sizeof(ImDrawVert);

0 commit comments

Comments
 (0)