Skip to content

Commit 655c131

Browse files
authored
Add OSX + Metal Backend (#10)
1 parent c16629e commit 655c131

File tree

8 files changed

+1583
-0
lines changed

8 files changed

+1583
-0
lines changed

build.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub const Backend = enum {
88
win32_dx12,
99
glfw,
1010
sdl2_opengl3,
11+
osx_metal,
1112
};
1213

1314
pub fn build(b: *std.Build) void {
@@ -79,6 +80,12 @@ pub fn build(b: *std.Build) void {
7980
if (options.use_32bit_draw_idx) "-DIMGUI_USE_32BIT_DRAW_INDEX" else "",
8081
};
8182

83+
const objcflags = &.{
84+
"-Wno-deprecated",
85+
"-Wno-pedantic",
86+
"-Wno-availability",
87+
};
88+
8289
const imgui = if (options.shared) blk: {
8390
const lib = b.addSharedLibrary(.{
8491
.name = "imgui",
@@ -351,6 +358,19 @@ pub fn build(b: *std.Build) void {
351358
.flags = &(cflags.* ++ .{"-DIMGUI_IMPL_OPENGL_LOADER_CUSTOM"}),
352359
});
353360
},
361+
.osx_metal => {
362+
imgui.linkFramework("Foundation");
363+
imgui.linkFramework("Metal");
364+
imgui.linkFramework("Cocoa");
365+
imgui.linkFramework("QuartzCore");
366+
imgui.addCSourceFiles(.{
367+
.files = &.{
368+
"libs/imgui/backends/imgui_impl_osx.mm",
369+
"libs/imgui/backends/imgui_impl_metal.mm",
370+
},
371+
.flags = objcflags,
372+
});
373+
},
354374
.no_backend => {},
355375
}
356376

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// dear imgui: Renderer Backend for Metal
2+
// This needs to be used along with a Platform Backend (e.g. OSX)
3+
4+
// Implemented features:
5+
// [X] Renderer: User texture binding. Use 'MTLTexture' as ImTextureID. Read the FAQ about ImTextureID!
6+
// [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices.
7+
8+
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
9+
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
10+
// Learn about Dear ImGui:
11+
// - FAQ https://dearimgui.com/faq
12+
// - Getting Started https://dearimgui.com/getting-started
13+
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
14+
// - Introduction, links and more at the top of imgui.cpp
15+
16+
#include "imgui.h" // IMGUI_IMPL_API
17+
#ifndef IMGUI_DISABLE
18+
19+
//-----------------------------------------------------------------------------
20+
// ObjC API
21+
//-----------------------------------------------------------------------------
22+
23+
#ifdef __OBJC__
24+
25+
extern "C" {
26+
@class MTLRenderPassDescriptor;
27+
@protocol MTLDevice, MTLCommandBuffer, MTLRenderCommandEncoder;
28+
29+
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
30+
IMGUI_IMPL_API bool ImGui_ImplMetal_Init(id<MTLDevice> device);
31+
IMGUI_IMPL_API void ImGui_ImplMetal_Shutdown();
32+
IMGUI_IMPL_API void ImGui_ImplMetal_NewFrame(MTLRenderPassDescriptor* renderPassDescriptor);
33+
IMGUI_IMPL_API void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData,
34+
id<MTLCommandBuffer> commandBuffer,
35+
id<MTLRenderCommandEncoder> commandEncoder);
36+
}
37+
38+
// Called by Init/NewFrame/Shutdown
39+
IMGUI_IMPL_API bool ImGui_ImplMetal_CreateFontsTexture(id<MTLDevice> device);
40+
IMGUI_IMPL_API void ImGui_ImplMetal_DestroyFontsTexture();
41+
IMGUI_IMPL_API bool ImGui_ImplMetal_CreateDeviceObjects(id<MTLDevice> device);
42+
IMGUI_IMPL_API void ImGui_ImplMetal_DestroyDeviceObjects();
43+
44+
#endif
45+
46+
//-----------------------------------------------------------------------------
47+
// C++ API
48+
//-----------------------------------------------------------------------------
49+
50+
// Enable Metal C++ binding support with '#define IMGUI_IMPL_METAL_CPP' in your imconfig.h file
51+
// More info about using Metal from C++: https://developer.apple.com/metal/cpp/
52+
53+
#ifdef IMGUI_IMPL_METAL_CPP
54+
#include <Metal/Metal.hpp>
55+
#ifndef __OBJC__
56+
57+
extern "C" {
58+
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
59+
IMGUI_IMPL_API bool ImGui_ImplMetal_Init(MTL::Device* device);
60+
IMGUI_IMPL_API void ImGui_ImplMetal_Shutdown();
61+
IMGUI_IMPL_API void ImGui_ImplMetal_NewFrame(MTL::RenderPassDescriptor* renderPassDescriptor);
62+
IMGUI_IMPL_API void ImGui_ImplMetal_RenderDrawData(ImDrawData* draw_data,
63+
MTL::CommandBuffer* commandBuffer,
64+
MTL::RenderCommandEncoder* commandEncoder);
65+
}
66+
67+
// Called by Init/NewFrame/Shutdown
68+
IMGUI_IMPL_API bool ImGui_ImplMetal_CreateFontsTexture(MTL::Device* device);
69+
IMGUI_IMPL_API void ImGui_ImplMetal_DestroyFontsTexture();
70+
IMGUI_IMPL_API bool ImGui_ImplMetal_CreateDeviceObjects(MTL::Device* device);
71+
IMGUI_IMPL_API void ImGui_ImplMetal_DestroyDeviceObjects();
72+
73+
#endif
74+
#endif
75+
76+
//-----------------------------------------------------------------------------
77+
78+
#endif // #ifndef IMGUI_DISABLE

0 commit comments

Comments
 (0)