imgui_impl_metal.mm 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. // dear imgui: Renderer Backend for Metal
  2. // This needs to be used along with a Platform Backend (e.g. OSX)
  3. // Implemented features:
  4. // [X] Renderer: User texture binding. Use 'MTLTexture' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
  5. // [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
  6. // [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
  7. // [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
  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. // CHANGELOG
  16. // (minor and older changes stripped away, please see git history for details)
  17. // 2025-XX-XX: Metal: Added support for multiple windows via the ImGuiPlatformIO interface.
  18. // 2025-06-11: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplMetal_CreateFontsTexture() and ImGui_ImplMetal_DestroyFontsTexture().
  19. // 2025-02-03: Metal: Crash fix. (#8367)
  20. // 2025-01-08: Metal: Fixed memory leaks when using metal-cpp (#8276, #8166) or when using multiple contexts (#7419).
  21. // 2022-08-23: Metal: Update deprecated property 'sampleCount'->'rasterSampleCount'.
  22. // 2022-07-05: Metal: Add dispatch synchronization.
  23. // 2022-06-30: Metal: Use __bridge for ARC based systems.
  24. // 2022-06-01: Metal: Fixed null dereference on exit inside command buffer completion handler.
  25. // 2022-04-27: Misc: Store backend data in a per-context struct, allowing to use this backend with multiple contexts.
  26. // 2022-01-03: Metal: Ignore ImDrawCmd where ElemCount == 0 (very rare but can technically be manufactured by user code).
  27. // 2021-12-30: Metal: Added Metal C++ support. Enable with '#define IMGUI_IMPL_METAL_CPP' in your imconfig.h file.
  28. // 2021-08-24: Metal: Fixed a crash when clipping rect larger than framebuffer is submitted. (#4464)
  29. // 2021-05-19: Metal: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
  30. // 2021-02-18: Metal: Change blending equation to preserve alpha in output buffer.
  31. // 2021-01-25: Metal: Fixed texture storage mode when building on Mac Catalyst.
  32. // 2019-05-29: Metal: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
  33. // 2019-04-30: Metal: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
  34. // 2019-02-11: Metal: Projecting clipping rectangles correctly using draw_data->FramebufferScale to allow multi-viewports for retina display.
  35. // 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
  36. // 2018-07-05: Metal: Added new Metal backend implementation.
  37. #include "imgui.h"
  38. #ifndef IMGUI_DISABLE
  39. #include "imgui_impl_metal.h"
  40. #import <time.h>
  41. #import <Metal/Metal.h>
  42. // Forward Declarations
  43. static void ImGui_ImplMetal_InitMultiViewportSupport();
  44. static void ImGui_ImplMetal_ShutdownMultiViewportSupport();
  45. static void ImGui_ImplMetal_CreateDeviceObjectsForPlatformWindows();
  46. static void ImGui_ImplMetal_InvalidateDeviceObjectsForPlatformWindows();
  47. #pragma mark - Support classes
  48. // A wrapper around a MTLBuffer object that knows the last time it was reused
  49. @interface MetalBuffer : NSObject
  50. @property (nonatomic, strong) id<MTLBuffer> buffer;
  51. @property (nonatomic, assign) double lastReuseTime;
  52. - (instancetype)initWithBuffer:(id<MTLBuffer>)buffer;
  53. @end
  54. // An object that encapsulates the data necessary to uniquely identify a
  55. // render pipeline state. These are used as cache keys.
  56. @interface FramebufferDescriptor : NSObject<NSCopying>
  57. @property (nonatomic, assign) unsigned long sampleCount;
  58. @property (nonatomic, assign) MTLPixelFormat colorPixelFormat;
  59. @property (nonatomic, assign) MTLPixelFormat depthPixelFormat;
  60. @property (nonatomic, assign) MTLPixelFormat stencilPixelFormat;
  61. - (instancetype)initWithRenderPassDescriptor:(MTLRenderPassDescriptor*)renderPassDescriptor;
  62. @end
  63. @interface MetalTexture : NSObject
  64. @property (nonatomic, strong) id<MTLTexture> metalTexture;
  65. - (instancetype)initWithTexture:(id<MTLTexture>)metalTexture;
  66. @end
  67. // A singleton that stores long-lived objects that are needed by the Metal
  68. // renderer backend. Stores the render pipeline state cache and the default
  69. // font texture, and manages the reusable buffer cache.
  70. @interface MetalContext : NSObject
  71. @property (nonatomic, strong) id<MTLDevice> device;
  72. @property (nonatomic, strong) id<MTLDepthStencilState> depthStencilState;
  73. @property (nonatomic, strong) FramebufferDescriptor* framebufferDescriptor; // framebuffer descriptor for current frame; transient
  74. @property (nonatomic, strong) NSMutableDictionary* renderPipelineStateCache; // pipeline cache; keyed on framebuffer descriptors
  75. @property (nonatomic, strong) NSMutableArray<MetalBuffer*>* bufferCache;
  76. @property (nonatomic, assign) double lastBufferCachePurge;
  77. - (MetalBuffer*)dequeueReusableBufferOfLength:(NSUInteger)length device:(id<MTLDevice>)device;
  78. - (id<MTLRenderPipelineState>)renderPipelineStateForFramebufferDescriptor:(FramebufferDescriptor*)descriptor device:(id<MTLDevice>)device;
  79. @end
  80. struct ImGui_ImplMetal_Data
  81. {
  82. MetalContext* SharedMetalContext;
  83. ImGui_ImplMetal_Data() { memset((void*)this, 0, sizeof(*this)); }
  84. };
  85. static ImGui_ImplMetal_Data* ImGui_ImplMetal_GetBackendData() { return ImGui::GetCurrentContext() ? (ImGui_ImplMetal_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; }
  86. static void ImGui_ImplMetal_DestroyBackendData(){ IM_DELETE(ImGui_ImplMetal_GetBackendData()); }
  87. static inline CFTimeInterval GetMachAbsoluteTimeInSeconds() { return (CFTimeInterval)(double)(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1e9); }
  88. #ifdef IMGUI_IMPL_METAL_CPP
  89. #pragma mark - Dear ImGui Metal C++ Backend API
  90. bool ImGui_ImplMetal_Init(MTL::Device* device)
  91. {
  92. return ImGui_ImplMetal_Init((__bridge id<MTLDevice>)(device));
  93. }
  94. void ImGui_ImplMetal_NewFrame(MTL::RenderPassDescriptor* renderPassDescriptor)
  95. {
  96. ImGui_ImplMetal_NewFrame((__bridge MTLRenderPassDescriptor*)(renderPassDescriptor));
  97. }
  98. void ImGui_ImplMetal_RenderDrawData(ImDrawData* draw_data,
  99. MTL::CommandBuffer* commandBuffer,
  100. MTL::RenderCommandEncoder* commandEncoder)
  101. {
  102. ImGui_ImplMetal_RenderDrawData(draw_data,
  103. (__bridge id<MTLCommandBuffer>)(commandBuffer),
  104. (__bridge id<MTLRenderCommandEncoder>)(commandEncoder));
  105. }
  106. bool ImGui_ImplMetal_CreateDeviceObjects(MTL::Device* device)
  107. {
  108. return ImGui_ImplMetal_CreateDeviceObjects((__bridge id<MTLDevice>)(device));
  109. }
  110. #endif // #ifdef IMGUI_IMPL_METAL_CPP
  111. #pragma mark - Dear ImGui Metal Backend API
  112. bool ImGui_ImplMetal_Init(id<MTLDevice> device)
  113. {
  114. ImGuiIO& io = ImGui::GetIO();
  115. IMGUI_CHECKVERSION();
  116. IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
  117. ImGui_ImplMetal_Data* bd = IM_NEW(ImGui_ImplMetal_Data)();
  118. io.BackendRendererUserData = (void*)bd;
  119. io.BackendRendererName = "imgui_impl_metal";
  120. io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
  121. io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
  122. io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
  123. bd->SharedMetalContext = [[MetalContext alloc] init];
  124. bd->SharedMetalContext.device = device;
  125. ImGui_ImplMetal_InitMultiViewportSupport();
  126. return true;
  127. }
  128. void ImGui_ImplMetal_Shutdown()
  129. {
  130. ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
  131. IM_UNUSED(bd);
  132. IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
  133. ImGui_ImplMetal_ShutdownMultiViewportSupport();
  134. ImGui_ImplMetal_DestroyDeviceObjects();
  135. ImGui_ImplMetal_DestroyBackendData();
  136. ImGuiIO& io = ImGui::GetIO();
  137. io.BackendRendererName = nullptr;
  138. io.BackendRendererUserData = nullptr;
  139. io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports);
  140. }
  141. void ImGui_ImplMetal_NewFrame(MTLRenderPassDescriptor* renderPassDescriptor)
  142. {
  143. ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
  144. IM_ASSERT(bd != nil && "Context or backend not initialized! Did you call ImGui_ImplMetal_Init()?");
  145. #ifdef IMGUI_IMPL_METAL_CPP
  146. bd->SharedMetalContext.framebufferDescriptor = [[[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor]autorelease];
  147. #else
  148. bd->SharedMetalContext.framebufferDescriptor = [[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor];
  149. #endif
  150. if (bd->SharedMetalContext.depthStencilState == nil)
  151. ImGui_ImplMetal_CreateDeviceObjects(bd->SharedMetalContext.device);
  152. }
  153. static void ImGui_ImplMetal_SetupRenderState(ImDrawData* draw_data, id<MTLCommandBuffer> commandBuffer,
  154. id<MTLRenderCommandEncoder> commandEncoder, id<MTLRenderPipelineState> renderPipelineState,
  155. MetalBuffer* vertexBuffer, size_t vertexBufferOffset)
  156. {
  157. IM_UNUSED(commandBuffer);
  158. ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
  159. [commandEncoder setCullMode:MTLCullModeNone];
  160. [commandEncoder setDepthStencilState:bd->SharedMetalContext.depthStencilState];
  161. // Setup viewport, orthographic projection matrix
  162. // Our visible imgui space lies from draw_data->DisplayPos (top left) to
  163. // draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin is typically (0,0) for single viewport apps.
  164. MTLViewport viewport =
  165. {
  166. .originX = 0.0,
  167. .originY = 0.0,
  168. .width = (double)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x),
  169. .height = (double)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y),
  170. .znear = 0.0,
  171. .zfar = 1.0
  172. };
  173. [commandEncoder setViewport:viewport];
  174. float L = draw_data->DisplayPos.x;
  175. float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
  176. float T = draw_data->DisplayPos.y;
  177. float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
  178. float N = (float)viewport.znear;
  179. float F = (float)viewport.zfar;
  180. const float ortho_projection[4][4] =
  181. {
  182. { 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
  183. { 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
  184. { 0.0f, 0.0f, 1/(F-N), 0.0f },
  185. { (R+L)/(L-R), (T+B)/(B-T), N/(F-N), 1.0f },
  186. };
  187. [commandEncoder setVertexBytes:&ortho_projection length:sizeof(ortho_projection) atIndex:1];
  188. [commandEncoder setRenderPipelineState:renderPipelineState];
  189. [commandEncoder setVertexBuffer:vertexBuffer.buffer offset:0 atIndex:0];
  190. [commandEncoder setVertexBufferOffset:vertexBufferOffset atIndex:0];
  191. }
  192. // Metal Render function.
  193. void ImGui_ImplMetal_RenderDrawData(ImDrawData* draw_data, id<MTLCommandBuffer> commandBuffer, id<MTLRenderCommandEncoder> commandEncoder)
  194. {
  195. ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
  196. MetalContext* ctx = bd->SharedMetalContext;
  197. // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
  198. int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
  199. int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
  200. if (fb_width <= 0 || fb_height <= 0 || draw_data->CmdLists.Size == 0)
  201. return;
  202. // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
  203. // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
  204. if (draw_data->Textures != nullptr)
  205. for (ImTextureData* tex : *draw_data->Textures)
  206. if (tex->Status != ImTextureStatus_OK)
  207. ImGui_ImplMetal_UpdateTexture(tex);
  208. // Try to retrieve a render pipeline state that is compatible with the framebuffer config for this frame
  209. // The hit rate for this cache should be very near 100%.
  210. id<MTLRenderPipelineState> renderPipelineState = ctx.renderPipelineStateCache[ctx.framebufferDescriptor];
  211. if (renderPipelineState == nil)
  212. {
  213. // No luck; make a new render pipeline state
  214. renderPipelineState = [ctx renderPipelineStateForFramebufferDescriptor:ctx.framebufferDescriptor device:commandBuffer.device];
  215. // Cache render pipeline state for later reuse
  216. ctx.renderPipelineStateCache[ctx.framebufferDescriptor] = renderPipelineState;
  217. }
  218. size_t vertexBufferLength = (size_t)draw_data->TotalVtxCount * sizeof(ImDrawVert);
  219. size_t indexBufferLength = (size_t)draw_data->TotalIdxCount * sizeof(ImDrawIdx);
  220. MetalBuffer* vertexBuffer = [ctx dequeueReusableBufferOfLength:vertexBufferLength device:commandBuffer.device];
  221. MetalBuffer* indexBuffer = [ctx dequeueReusableBufferOfLength:indexBufferLength device:commandBuffer.device];
  222. ImGui_ImplMetal_SetupRenderState(draw_data, commandBuffer, commandEncoder, renderPipelineState, vertexBuffer, 0);
  223. // Will project scissor/clipping rectangles into framebuffer space
  224. ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
  225. ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
  226. // Render command lists
  227. size_t vertexBufferOffset = 0;
  228. size_t indexBufferOffset = 0;
  229. for (const ImDrawList* draw_list : draw_data->CmdLists)
  230. {
  231. memcpy((char*)vertexBuffer.buffer.contents + vertexBufferOffset, draw_list->VtxBuffer.Data, (size_t)draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
  232. memcpy((char*)indexBuffer.buffer.contents + indexBufferOffset, draw_list->IdxBuffer.Data, (size_t)draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
  233. for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
  234. {
  235. const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
  236. if (pcmd->UserCallback)
  237. {
  238. // User callback, registered via ImDrawList::AddCallback()
  239. // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
  240. if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
  241. ImGui_ImplMetal_SetupRenderState(draw_data, commandBuffer, commandEncoder, renderPipelineState, vertexBuffer, vertexBufferOffset);
  242. else
  243. pcmd->UserCallback(draw_list, pcmd);
  244. }
  245. else
  246. {
  247. // Project scissor/clipping rectangles into framebuffer space
  248. ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
  249. ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
  250. // Clamp to viewport as setScissorRect() won't accept values that are off bounds
  251. if (clip_min.x < 0.0f) { clip_min.x = 0.0f; }
  252. if (clip_min.y < 0.0f) { clip_min.y = 0.0f; }
  253. if (clip_max.x > fb_width) { clip_max.x = (float)fb_width; }
  254. if (clip_max.y > fb_height) { clip_max.y = (float)fb_height; }
  255. if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
  256. continue;
  257. if (pcmd->ElemCount == 0) // drawIndexedPrimitives() validation doesn't accept this
  258. continue;
  259. // Apply scissor/clipping rectangle
  260. MTLScissorRect scissorRect =
  261. {
  262. .x = NSUInteger(clip_min.x),
  263. .y = NSUInteger(clip_min.y),
  264. .width = NSUInteger(clip_max.x - clip_min.x),
  265. .height = NSUInteger(clip_max.y - clip_min.y)
  266. };
  267. [commandEncoder setScissorRect:scissorRect];
  268. // Bind texture, Draw
  269. if (ImTextureID tex_id = pcmd->GetTexID())
  270. [commandEncoder setFragmentTexture:(__bridge id<MTLTexture>)(void*)(intptr_t)(tex_id) atIndex:0];
  271. [commandEncoder setVertexBufferOffset:(vertexBufferOffset + pcmd->VtxOffset * sizeof(ImDrawVert)) atIndex:0];
  272. [commandEncoder drawIndexedPrimitives:MTLPrimitiveTypeTriangle
  273. indexCount:pcmd->ElemCount
  274. indexType:sizeof(ImDrawIdx) == 2 ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32
  275. indexBuffer:indexBuffer.buffer
  276. indexBufferOffset:indexBufferOffset + pcmd->IdxOffset * sizeof(ImDrawIdx)];
  277. }
  278. }
  279. vertexBufferOffset += (size_t)draw_list->VtxBuffer.Size * sizeof(ImDrawVert);
  280. indexBufferOffset += (size_t)draw_list->IdxBuffer.Size * sizeof(ImDrawIdx);
  281. }
  282. MetalContext* sharedMetalContext = bd->SharedMetalContext;
  283. [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer>)
  284. {
  285. dispatch_async(dispatch_get_main_queue(), ^{
  286. @synchronized(sharedMetalContext.bufferCache)
  287. {
  288. [sharedMetalContext.bufferCache addObject:vertexBuffer];
  289. [sharedMetalContext.bufferCache addObject:indexBuffer];
  290. }
  291. });
  292. }];
  293. }
  294. static void ImGui_ImplMetal_DestroyTexture(ImTextureData* tex)
  295. {
  296. MetalTexture* backend_tex = (__bridge_transfer MetalTexture*)(tex->BackendUserData);
  297. if (backend_tex == nullptr)
  298. return;
  299. IM_ASSERT(backend_tex.metalTexture == (__bridge id<MTLTexture>)(void*)(intptr_t)tex->TexID);
  300. backend_tex.metalTexture = nil;
  301. // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
  302. tex->SetTexID(ImTextureID_Invalid);
  303. tex->SetStatus(ImTextureStatus_Destroyed);
  304. tex->BackendUserData = nullptr;
  305. }
  306. void ImGui_ImplMetal_UpdateTexture(ImTextureData* tex)
  307. {
  308. ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
  309. if (tex->Status == ImTextureStatus_WantCreate)
  310. {
  311. // Create and upload new texture to graphics system
  312. //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
  313. IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
  314. IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
  315. // We are retrieving and uploading the font atlas as a 4-channels RGBA texture here.
  316. // In theory we could call GetTexDataAsAlpha8() and upload a 1-channel texture to save on memory access bandwidth.
  317. // However, using a shader designed for 1-channel texture would make it less obvious to use the ImTextureID facility to render users own textures.
  318. // You can make that change in your implementation.
  319. MTLTextureDescriptor* textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
  320. width:(NSUInteger)tex->Width
  321. height:(NSUInteger)tex->Height
  322. mipmapped:NO];
  323. textureDescriptor.usage = MTLTextureUsageShaderRead;
  324. #if TARGET_OS_OSX || TARGET_OS_MACCATALYST
  325. textureDescriptor.storageMode = MTLStorageModeManaged;
  326. #else
  327. textureDescriptor.storageMode = MTLStorageModeShared;
  328. #endif
  329. id <MTLTexture> texture = [bd->SharedMetalContext.device newTextureWithDescriptor:textureDescriptor];
  330. [texture replaceRegion:MTLRegionMake2D(0, 0, (NSUInteger)tex->Width, (NSUInteger)tex->Height) mipmapLevel:0 withBytes:tex->Pixels bytesPerRow:(NSUInteger)tex->Width * 4];
  331. MetalTexture* backend_tex = [[MetalTexture alloc] initWithTexture:texture];
  332. // Store identifiers
  333. tex->SetTexID((ImTextureID)(intptr_t)texture);
  334. tex->SetStatus(ImTextureStatus_OK);
  335. tex->BackendUserData = (__bridge_retained void*)(backend_tex);
  336. }
  337. else if (tex->Status == ImTextureStatus_WantUpdates)
  338. {
  339. // Update selected blocks. We only ever write to textures regions which have never been used before!
  340. // This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
  341. MetalTexture* backend_tex = (__bridge MetalTexture*)(tex->BackendUserData);
  342. for (ImTextureRect& r : tex->Updates)
  343. {
  344. [backend_tex.metalTexture replaceRegion:MTLRegionMake2D((NSUInteger)r.x, (NSUInteger)r.y, (NSUInteger)r.w, (NSUInteger)r.h)
  345. mipmapLevel:0
  346. withBytes:tex->GetPixelsAt(r.x, r.y)
  347. bytesPerRow:(NSUInteger)tex->Width * 4];
  348. }
  349. tex->SetStatus(ImTextureStatus_OK);
  350. }
  351. else if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
  352. {
  353. ImGui_ImplMetal_DestroyTexture(tex);
  354. }
  355. }
  356. bool ImGui_ImplMetal_CreateDeviceObjects(id<MTLDevice> device)
  357. {
  358. ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
  359. MTLDepthStencilDescriptor* depthStencilDescriptor = [[MTLDepthStencilDescriptor alloc] init];
  360. depthStencilDescriptor.depthWriteEnabled = NO;
  361. depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionAlways;
  362. bd->SharedMetalContext.depthStencilState = [device newDepthStencilStateWithDescriptor:depthStencilDescriptor];
  363. ImGui_ImplMetal_CreateDeviceObjectsForPlatformWindows();
  364. #ifdef IMGUI_IMPL_METAL_CPP
  365. [depthStencilDescriptor release];
  366. #endif
  367. return true;
  368. }
  369. void ImGui_ImplMetal_DestroyDeviceObjects()
  370. {
  371. ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
  372. // Destroy all textures
  373. for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
  374. if (tex->RefCount == 1)
  375. ImGui_ImplMetal_DestroyTexture(tex);
  376. ImGui_ImplMetal_InvalidateDeviceObjectsForPlatformWindows();
  377. [bd->SharedMetalContext.renderPipelineStateCache removeAllObjects];
  378. }
  379. #pragma mark - Multi-viewport support
  380. #import <QuartzCore/CAMetalLayer.h>
  381. #if TARGET_OS_OSX
  382. #import <Cocoa/Cocoa.h>
  383. #endif
  384. //--------------------------------------------------------------------------------------------------------
  385. // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
  386. // This is an _advanced_ and _optional_ feature, allowing the back-end to create and handle multiple viewports simultaneously.
  387. // If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
  388. //--------------------------------------------------------------------------------------------------------
  389. struct ImGuiViewportDataMetal
  390. {
  391. CAMetalLayer* MetalLayer;
  392. id<MTLCommandQueue> CommandQueue;
  393. MTLRenderPassDescriptor* RenderPassDescriptor;
  394. void* Handle = nullptr;
  395. bool FirstFrame = true;
  396. };
  397. static void ImGui_ImplMetal_CreateWindow(ImGuiViewport* viewport)
  398. {
  399. ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
  400. ImGuiViewportDataMetal* data = IM_NEW(ImGuiViewportDataMetal)();
  401. viewport->RendererUserData = data;
  402. // PlatformHandleRaw should always be a NSWindow*, whereas PlatformHandle might be a higher-level handle (e.g. GLFWWindow*, SDL_Window*).
  403. // Some back-ends will leave PlatformHandleRaw == 0, in which case we assume PlatformHandle will contain the NSWindow*.
  404. void* handle = viewport->PlatformHandleRaw ? viewport->PlatformHandleRaw : viewport->PlatformHandle;
  405. IM_ASSERT(handle != nullptr);
  406. id<MTLDevice> device = bd->SharedMetalContext.device;
  407. CAMetalLayer* layer = [CAMetalLayer layer];
  408. layer.device = device;
  409. layer.framebufferOnly = YES;
  410. layer.pixelFormat = bd->SharedMetalContext.framebufferDescriptor.colorPixelFormat;
  411. #if TARGET_OS_OSX
  412. NSWindow* window = (__bridge NSWindow*)handle;
  413. NSView* view = window.contentView;
  414. view.layer = layer;
  415. view.wantsLayer = YES;
  416. #endif
  417. data->MetalLayer = layer;
  418. data->CommandQueue = [device newCommandQueue];
  419. data->RenderPassDescriptor = [[MTLRenderPassDescriptor alloc] init];
  420. data->Handle = handle;
  421. }
  422. static void ImGui_ImplMetal_DestroyWindow(ImGuiViewport* viewport)
  423. {
  424. // The main viewport (owned by the application) will always have RendererUserData == 0 since we didn't create the data for it.
  425. if (ImGuiViewportDataMetal* data = (ImGuiViewportDataMetal*)viewport->RendererUserData)
  426. IM_DELETE(data);
  427. viewport->RendererUserData = nullptr;
  428. }
  429. inline static CGSize MakeScaledSize(CGSize size, CGFloat scale)
  430. {
  431. return CGSizeMake(size.width * scale, size.height * scale);
  432. }
  433. static void ImGui_ImplMetal_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  434. {
  435. ImGuiViewportDataMetal* data = (ImGuiViewportDataMetal*)viewport->RendererUserData;
  436. data->MetalLayer.drawableSize = MakeScaledSize(CGSizeMake(size.x, size.y), viewport->DpiScale);
  437. }
  438. static void ImGui_ImplMetal_RenderWindow(ImGuiViewport* viewport, void*)
  439. {
  440. ImGuiViewportDataMetal* data = (ImGuiViewportDataMetal*)viewport->RendererUserData;
  441. #if TARGET_OS_OSX
  442. void* handle = viewport->PlatformHandleRaw ? viewport->PlatformHandleRaw : viewport->PlatformHandle;
  443. NSWindow* window = (__bridge NSWindow*)handle;
  444. // Always render the first frame, regardless of occlusionState, to avoid an initial flicker
  445. if ((window.occlusionState & NSWindowOcclusionStateVisible) == 0 && !data->FirstFrame)
  446. {
  447. // Do not render windows which are completely occluded. Calling -[CAMetalLayer nextDrawable] will hang for
  448. // approximately 1 second if the Metal layer is completely occluded.
  449. return;
  450. }
  451. data->FirstFrame = false;
  452. float fb_scale = (float)window.backingScaleFactor;
  453. if (data->MetalLayer.contentsScale != fb_scale)
  454. {
  455. data->MetalLayer.contentsScale = fb_scale;
  456. data->MetalLayer.drawableSize = MakeScaledSize(window.frame.size, fb_scale);
  457. }
  458. #endif
  459. id <CAMetalDrawable> drawable = [data->MetalLayer nextDrawable];
  460. if (drawable == nil)
  461. return;
  462. MTLRenderPassDescriptor* renderPassDescriptor = data->RenderPassDescriptor;
  463. renderPassDescriptor.colorAttachments[0].texture = drawable.texture;
  464. renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 0);
  465. if ((viewport->Flags & ImGuiViewportFlags_NoRendererClear) == 0)
  466. renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
  467. id <MTLCommandBuffer> commandBuffer = [data->CommandQueue commandBuffer];
  468. id <MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
  469. ImGui_ImplMetal_RenderDrawData(viewport->DrawData, commandBuffer, renderEncoder);
  470. [renderEncoder endEncoding];
  471. [commandBuffer presentDrawable:drawable];
  472. [commandBuffer commit];
  473. }
  474. static void ImGui_ImplMetal_InitMultiViewportSupport()
  475. {
  476. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  477. platform_io.Renderer_CreateWindow = ImGui_ImplMetal_CreateWindow;
  478. platform_io.Renderer_DestroyWindow = ImGui_ImplMetal_DestroyWindow;
  479. platform_io.Renderer_SetWindowSize = ImGui_ImplMetal_SetWindowSize;
  480. platform_io.Renderer_RenderWindow = ImGui_ImplMetal_RenderWindow;
  481. }
  482. static void ImGui_ImplMetal_ShutdownMultiViewportSupport()
  483. {
  484. ImGui::DestroyPlatformWindows();
  485. }
  486. static void ImGui_ImplMetal_CreateDeviceObjectsForPlatformWindows()
  487. {
  488. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  489. for (int i = 1; i < platform_io.Viewports.Size; i++)
  490. if (!platform_io.Viewports[i]->RendererUserData)
  491. ImGui_ImplMetal_CreateWindow(platform_io.Viewports[i]);
  492. }
  493. static void ImGui_ImplMetal_InvalidateDeviceObjectsForPlatformWindows()
  494. {
  495. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  496. for (int i = 1; i < platform_io.Viewports.Size; i++)
  497. if (platform_io.Viewports[i]->RendererUserData)
  498. ImGui_ImplMetal_DestroyWindow(platform_io.Viewports[i]);
  499. }
  500. #pragma mark - MetalBuffer implementation
  501. @implementation MetalBuffer
  502. - (instancetype)initWithBuffer:(id<MTLBuffer>)buffer
  503. {
  504. if ((self = [super init]))
  505. {
  506. _buffer = buffer;
  507. _lastReuseTime = GetMachAbsoluteTimeInSeconds();
  508. }
  509. return self;
  510. }
  511. @end
  512. #pragma mark - FramebufferDescriptor implementation
  513. @implementation FramebufferDescriptor
  514. - (instancetype)initWithRenderPassDescriptor:(MTLRenderPassDescriptor*)renderPassDescriptor
  515. {
  516. if ((self = [super init]))
  517. {
  518. _sampleCount = renderPassDescriptor.colorAttachments[0].texture.sampleCount;
  519. _colorPixelFormat = renderPassDescriptor.colorAttachments[0].texture.pixelFormat;
  520. _depthPixelFormat = renderPassDescriptor.depthAttachment.texture.pixelFormat;
  521. _stencilPixelFormat = renderPassDescriptor.stencilAttachment.texture.pixelFormat;
  522. }
  523. return self;
  524. }
  525. - (nonnull id)copyWithZone:(nullable NSZone*)zone
  526. {
  527. FramebufferDescriptor* copy = [[FramebufferDescriptor allocWithZone:zone] init];
  528. copy.sampleCount = self.sampleCount;
  529. copy.colorPixelFormat = self.colorPixelFormat;
  530. copy.depthPixelFormat = self.depthPixelFormat;
  531. copy.stencilPixelFormat = self.stencilPixelFormat;
  532. return copy;
  533. }
  534. - (NSUInteger)hash
  535. {
  536. NSUInteger sc = _sampleCount & 0x3;
  537. NSUInteger cf = _colorPixelFormat & 0x3FF;
  538. NSUInteger df = _depthPixelFormat & 0x3FF;
  539. NSUInteger sf = _stencilPixelFormat & 0x3FF;
  540. NSUInteger hash = (sf << 22) | (df << 12) | (cf << 2) | sc;
  541. return hash;
  542. }
  543. - (BOOL)isEqual:(id)object
  544. {
  545. FramebufferDescriptor* other = object;
  546. if (![other isKindOfClass:[FramebufferDescriptor class]])
  547. return NO;
  548. return other.sampleCount == self.sampleCount &&
  549. other.colorPixelFormat == self.colorPixelFormat &&
  550. other.depthPixelFormat == self.depthPixelFormat &&
  551. other.stencilPixelFormat == self.stencilPixelFormat;
  552. }
  553. @end
  554. #pragma mark - MetalTexture implementation
  555. @implementation MetalTexture
  556. - (instancetype)initWithTexture:(id<MTLTexture>)metalTexture
  557. {
  558. if ((self = [super init]))
  559. self.metalTexture = metalTexture;
  560. return self;
  561. }
  562. @end
  563. #pragma mark - MetalContext implementation
  564. @implementation MetalContext
  565. - (instancetype)init
  566. {
  567. if ((self = [super init]))
  568. {
  569. self.renderPipelineStateCache = [NSMutableDictionary dictionary];
  570. self.bufferCache = [NSMutableArray array];
  571. _lastBufferCachePurge = GetMachAbsoluteTimeInSeconds();
  572. }
  573. return self;
  574. }
  575. - (MetalBuffer*)dequeueReusableBufferOfLength:(NSUInteger)length device:(id<MTLDevice>)device
  576. {
  577. uint64_t now = GetMachAbsoluteTimeInSeconds();
  578. @synchronized(self.bufferCache)
  579. {
  580. // Purge old buffers that haven't been useful for a while
  581. if (now - self.lastBufferCachePurge > 1.0)
  582. {
  583. NSMutableArray* survivors = [NSMutableArray array];
  584. for (MetalBuffer* candidate in self.bufferCache)
  585. if (candidate.lastReuseTime > self.lastBufferCachePurge)
  586. [survivors addObject:candidate];
  587. self.bufferCache = [survivors mutableCopy];
  588. self.lastBufferCachePurge = now;
  589. }
  590. // See if we have a buffer we can reuse
  591. MetalBuffer* bestCandidate = nil;
  592. for (MetalBuffer* candidate in self.bufferCache)
  593. if (candidate.buffer.length >= length && (bestCandidate == nil || bestCandidate.lastReuseTime > candidate.lastReuseTime))
  594. bestCandidate = candidate;
  595. if (bestCandidate != nil)
  596. {
  597. [self.bufferCache removeObject:bestCandidate];
  598. bestCandidate.lastReuseTime = now;
  599. return bestCandidate;
  600. }
  601. }
  602. // No luck; make a new buffer
  603. id<MTLBuffer> backing = [device newBufferWithLength:length options:MTLResourceStorageModeShared];
  604. return [[MetalBuffer alloc] initWithBuffer:backing];
  605. }
  606. // Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling.
  607. - (id<MTLRenderPipelineState>)renderPipelineStateForFramebufferDescriptor:(FramebufferDescriptor*)descriptor device:(id<MTLDevice>)device
  608. {
  609. NSError* error = nil;
  610. NSString* shaderSource = @""
  611. "#include <metal_stdlib>\n"
  612. "using namespace metal;\n"
  613. "\n"
  614. "struct Uniforms {\n"
  615. " float4x4 projectionMatrix;\n"
  616. "};\n"
  617. "\n"
  618. "struct VertexIn {\n"
  619. " float2 position [[attribute(0)]];\n"
  620. " float2 texCoords [[attribute(1)]];\n"
  621. " uchar4 color [[attribute(2)]];\n"
  622. "};\n"
  623. "\n"
  624. "struct VertexOut {\n"
  625. " float4 position [[position]];\n"
  626. " float2 texCoords;\n"
  627. " float4 color;\n"
  628. "};\n"
  629. "\n"
  630. "vertex VertexOut vertex_main(VertexIn in [[stage_in]],\n"
  631. " constant Uniforms &uniforms [[buffer(1)]]) {\n"
  632. " VertexOut out;\n"
  633. " out.position = uniforms.projectionMatrix * float4(in.position, 0, 1);\n"
  634. " out.texCoords = in.texCoords;\n"
  635. " out.color = float4(in.color) / float4(255.0);\n"
  636. " return out;\n"
  637. "}\n"
  638. "\n"
  639. "fragment half4 fragment_main(VertexOut in [[stage_in]],\n"
  640. " texture2d<half, access::sample> texture [[texture(0)]]) {\n"
  641. " constexpr sampler linearSampler(coord::normalized, min_filter::linear, mag_filter::linear, mip_filter::linear);\n"
  642. " half4 texColor = texture.sample(linearSampler, in.texCoords);\n"
  643. " return half4(in.color) * texColor;\n"
  644. "}\n";
  645. id<MTLLibrary> library = [device newLibraryWithSource:shaderSource options:nil error:&error];
  646. if (library == nil)
  647. {
  648. NSLog(@"Error: failed to create Metal library: %@", error);
  649. return nil;
  650. }
  651. id<MTLFunction> vertexFunction = [library newFunctionWithName:@"vertex_main"];
  652. id<MTLFunction> fragmentFunction = [library newFunctionWithName:@"fragment_main"];
  653. if (vertexFunction == nil || fragmentFunction == nil)
  654. {
  655. NSLog(@"Error: failed to find Metal shader functions in library: %@", error);
  656. return nil;
  657. }
  658. MTLVertexDescriptor* vertexDescriptor = [MTLVertexDescriptor vertexDescriptor];
  659. vertexDescriptor.attributes[0].offset = offsetof(ImDrawVert, pos);
  660. vertexDescriptor.attributes[0].format = MTLVertexFormatFloat2; // position
  661. vertexDescriptor.attributes[0].bufferIndex = 0;
  662. vertexDescriptor.attributes[1].offset = offsetof(ImDrawVert, uv);
  663. vertexDescriptor.attributes[1].format = MTLVertexFormatFloat2; // texCoords
  664. vertexDescriptor.attributes[1].bufferIndex = 0;
  665. vertexDescriptor.attributes[2].offset = offsetof(ImDrawVert, col);
  666. vertexDescriptor.attributes[2].format = MTLVertexFormatUChar4; // color
  667. vertexDescriptor.attributes[2].bufferIndex = 0;
  668. vertexDescriptor.layouts[0].stepRate = 1;
  669. vertexDescriptor.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
  670. vertexDescriptor.layouts[0].stride = sizeof(ImDrawVert);
  671. MTLRenderPipelineDescriptor* pipelineDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
  672. pipelineDescriptor.vertexFunction = vertexFunction;
  673. pipelineDescriptor.fragmentFunction = fragmentFunction;
  674. pipelineDescriptor.vertexDescriptor = vertexDescriptor;
  675. pipelineDescriptor.rasterSampleCount = self.framebufferDescriptor.sampleCount;
  676. pipelineDescriptor.colorAttachments[0].pixelFormat = self.framebufferDescriptor.colorPixelFormat;
  677. pipelineDescriptor.colorAttachments[0].blendingEnabled = YES;
  678. pipelineDescriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;
  679. pipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha;
  680. pipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
  681. pipelineDescriptor.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd;
  682. pipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorOne;
  683. pipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
  684. pipelineDescriptor.depthAttachmentPixelFormat = self.framebufferDescriptor.depthPixelFormat;
  685. pipelineDescriptor.stencilAttachmentPixelFormat = self.framebufferDescriptor.stencilPixelFormat;
  686. id<MTLRenderPipelineState> renderPipelineState = [device newRenderPipelineStateWithDescriptor:pipelineDescriptor error:&error];
  687. if (error != nil)
  688. NSLog(@"Error: failed to create Metal pipeline state: %@", error);
  689. return renderPipelineState;
  690. }
  691. @end
  692. //-----------------------------------------------------------------------------
  693. #endif // #ifndef IMGUI_DISABLE