imgui_impl_metal.mm 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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->CmdListsCount == 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 (int n = 0; n < draw_data->CmdListsCount; n++)
  230. {
  231. const ImDrawList* draw_list = draw_data->CmdLists[n];
  232. memcpy((char*)vertexBuffer.buffer.contents + vertexBufferOffset, draw_list->VtxBuffer.Data, (size_t)draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
  233. memcpy((char*)indexBuffer.buffer.contents + indexBufferOffset, draw_list->IdxBuffer.Data, (size_t)draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
  234. for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
  235. {
  236. const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
  237. if (pcmd->UserCallback)
  238. {
  239. // User callback, registered via ImDrawList::AddCallback()
  240. // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
  241. if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
  242. ImGui_ImplMetal_SetupRenderState(draw_data, commandBuffer, commandEncoder, renderPipelineState, vertexBuffer, vertexBufferOffset);
  243. else
  244. pcmd->UserCallback(draw_list, pcmd);
  245. }
  246. else
  247. {
  248. // Project scissor/clipping rectangles into framebuffer space
  249. ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
  250. ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
  251. // Clamp to viewport as setScissorRect() won't accept values that are off bounds
  252. if (clip_min.x < 0.0f) { clip_min.x = 0.0f; }
  253. if (clip_min.y < 0.0f) { clip_min.y = 0.0f; }
  254. if (clip_max.x > fb_width) { clip_max.x = (float)fb_width; }
  255. if (clip_max.y > fb_height) { clip_max.y = (float)fb_height; }
  256. if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
  257. continue;
  258. if (pcmd->ElemCount == 0) // drawIndexedPrimitives() validation doesn't accept this
  259. continue;
  260. // Apply scissor/clipping rectangle
  261. MTLScissorRect scissorRect =
  262. {
  263. .x = NSUInteger(clip_min.x),
  264. .y = NSUInteger(clip_min.y),
  265. .width = NSUInteger(clip_max.x - clip_min.x),
  266. .height = NSUInteger(clip_max.y - clip_min.y)
  267. };
  268. [commandEncoder setScissorRect:scissorRect];
  269. // Bind texture, Draw
  270. if (ImTextureID tex_id = pcmd->GetTexID())
  271. [commandEncoder setFragmentTexture:(__bridge id<MTLTexture>)(void*)(intptr_t)(tex_id) atIndex:0];
  272. [commandEncoder setVertexBufferOffset:(vertexBufferOffset + pcmd->VtxOffset * sizeof(ImDrawVert)) atIndex:0];
  273. [commandEncoder drawIndexedPrimitives:MTLPrimitiveTypeTriangle
  274. indexCount:pcmd->ElemCount
  275. indexType:sizeof(ImDrawIdx) == 2 ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32
  276. indexBuffer:indexBuffer.buffer
  277. indexBufferOffset:indexBufferOffset + pcmd->IdxOffset * sizeof(ImDrawIdx)];
  278. }
  279. }
  280. vertexBufferOffset += (size_t)draw_list->VtxBuffer.Size * sizeof(ImDrawVert);
  281. indexBufferOffset += (size_t)draw_list->IdxBuffer.Size * sizeof(ImDrawIdx);
  282. }
  283. MetalContext* sharedMetalContext = bd->SharedMetalContext;
  284. [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer>)
  285. {
  286. dispatch_async(dispatch_get_main_queue(), ^{
  287. @synchronized(sharedMetalContext.bufferCache)
  288. {
  289. [sharedMetalContext.bufferCache addObject:vertexBuffer];
  290. [sharedMetalContext.bufferCache addObject:indexBuffer];
  291. }
  292. });
  293. }];
  294. }
  295. static void ImGui_ImplMetal_DestroyTexture(ImTextureData* tex)
  296. {
  297. MetalTexture* backend_tex = (__bridge_transfer MetalTexture*)(tex->BackendUserData);
  298. if (backend_tex == nullptr)
  299. return;
  300. IM_ASSERT(backend_tex.metalTexture == (__bridge id<MTLTexture>)(void*)(intptr_t)tex->TexID);
  301. backend_tex.metalTexture = nil;
  302. // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
  303. tex->SetTexID(ImTextureID_Invalid);
  304. tex->SetStatus(ImTextureStatus_Destroyed);
  305. tex->BackendUserData = nullptr;
  306. }
  307. void ImGui_ImplMetal_UpdateTexture(ImTextureData* tex)
  308. {
  309. ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
  310. if (tex->Status == ImTextureStatus_WantCreate)
  311. {
  312. // Create and upload new texture to graphics system
  313. //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
  314. IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
  315. IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
  316. // We are retrieving and uploading the font atlas as a 4-channels RGBA texture here.
  317. // In theory we could call GetTexDataAsAlpha8() and upload a 1-channel texture to save on memory access bandwidth.
  318. // However, using a shader designed for 1-channel texture would make it less obvious to use the ImTextureID facility to render users own textures.
  319. // You can make that change in your implementation.
  320. MTLTextureDescriptor* textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
  321. width:(NSUInteger)tex->Width
  322. height:(NSUInteger)tex->Height
  323. mipmapped:NO];
  324. textureDescriptor.usage = MTLTextureUsageShaderRead;
  325. #if TARGET_OS_OSX || TARGET_OS_MACCATALYST
  326. textureDescriptor.storageMode = MTLStorageModeManaged;
  327. #else
  328. textureDescriptor.storageMode = MTLStorageModeShared;
  329. #endif
  330. id <MTLTexture> texture = [bd->SharedMetalContext.device newTextureWithDescriptor:textureDescriptor];
  331. [texture replaceRegion:MTLRegionMake2D(0, 0, (NSUInteger)tex->Width, (NSUInteger)tex->Height) mipmapLevel:0 withBytes:tex->Pixels bytesPerRow:(NSUInteger)tex->Width * 4];
  332. MetalTexture* backend_tex = [[MetalTexture alloc] initWithTexture:texture];
  333. // Store identifiers
  334. tex->SetTexID((ImTextureID)(intptr_t)texture);
  335. tex->SetStatus(ImTextureStatus_OK);
  336. tex->BackendUserData = (__bridge_retained void*)(backend_tex);
  337. }
  338. else if (tex->Status == ImTextureStatus_WantUpdates)
  339. {
  340. // Update selected blocks. We only ever write to textures regions which have never been used before!
  341. // This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
  342. MetalTexture* backend_tex = (__bridge MetalTexture*)(tex->BackendUserData);
  343. for (ImTextureRect& r : tex->Updates)
  344. {
  345. [backend_tex.metalTexture replaceRegion:MTLRegionMake2D((NSUInteger)r.x, (NSUInteger)r.y, (NSUInteger)r.w, (NSUInteger)r.h)
  346. mipmapLevel:0
  347. withBytes:tex->GetPixelsAt(r.x, r.y)
  348. bytesPerRow:(NSUInteger)tex->Width * 4];
  349. }
  350. tex->SetStatus(ImTextureStatus_OK);
  351. }
  352. else if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
  353. {
  354. ImGui_ImplMetal_DestroyTexture(tex);
  355. }
  356. }
  357. bool ImGui_ImplMetal_CreateDeviceObjects(id<MTLDevice> device)
  358. {
  359. ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
  360. MTLDepthStencilDescriptor* depthStencilDescriptor = [[MTLDepthStencilDescriptor alloc] init];
  361. depthStencilDescriptor.depthWriteEnabled = NO;
  362. depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionAlways;
  363. bd->SharedMetalContext.depthStencilState = [device newDepthStencilStateWithDescriptor:depthStencilDescriptor];
  364. ImGui_ImplMetal_CreateDeviceObjectsForPlatformWindows();
  365. #ifdef IMGUI_IMPL_METAL_CPP
  366. [depthStencilDescriptor release];
  367. #endif
  368. return true;
  369. }
  370. void ImGui_ImplMetal_DestroyDeviceObjects()
  371. {
  372. ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
  373. // Destroy all textures
  374. for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
  375. if (tex->RefCount == 1)
  376. ImGui_ImplMetal_DestroyTexture(tex);
  377. ImGui_ImplMetal_InvalidateDeviceObjectsForPlatformWindows();
  378. [bd->SharedMetalContext.renderPipelineStateCache removeAllObjects];
  379. }
  380. #pragma mark - Multi-viewport support
  381. #import <QuartzCore/CAMetalLayer.h>
  382. #if TARGET_OS_OSX
  383. #import <Cocoa/Cocoa.h>
  384. #endif
  385. //--------------------------------------------------------------------------------------------------------
  386. // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
  387. // This is an _advanced_ and _optional_ feature, allowing the back-end to create and handle multiple viewports simultaneously.
  388. // 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..
  389. //--------------------------------------------------------------------------------------------------------
  390. struct ImGuiViewportDataMetal
  391. {
  392. CAMetalLayer* MetalLayer;
  393. id<MTLCommandQueue> CommandQueue;
  394. MTLRenderPassDescriptor* RenderPassDescriptor;
  395. void* Handle = nullptr;
  396. bool FirstFrame = true;
  397. };
  398. static void ImGui_ImplMetal_CreateWindow(ImGuiViewport* viewport)
  399. {
  400. ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
  401. ImGuiViewportDataMetal* data = IM_NEW(ImGuiViewportDataMetal)();
  402. viewport->RendererUserData = data;
  403. // PlatformHandleRaw should always be a NSWindow*, whereas PlatformHandle might be a higher-level handle (e.g. GLFWWindow*, SDL_Window*).
  404. // Some back-ends will leave PlatformHandleRaw == 0, in which case we assume PlatformHandle will contain the NSWindow*.
  405. void* handle = viewport->PlatformHandleRaw ? viewport->PlatformHandleRaw : viewport->PlatformHandle;
  406. IM_ASSERT(handle != nullptr);
  407. id<MTLDevice> device = bd->SharedMetalContext.device;
  408. CAMetalLayer* layer = [CAMetalLayer layer];
  409. layer.device = device;
  410. layer.framebufferOnly = YES;
  411. layer.pixelFormat = bd->SharedMetalContext.framebufferDescriptor.colorPixelFormat;
  412. #if TARGET_OS_OSX
  413. NSWindow* window = (__bridge NSWindow*)handle;
  414. NSView* view = window.contentView;
  415. view.layer = layer;
  416. view.wantsLayer = YES;
  417. #endif
  418. data->MetalLayer = layer;
  419. data->CommandQueue = [device newCommandQueue];
  420. data->RenderPassDescriptor = [[MTLRenderPassDescriptor alloc] init];
  421. data->Handle = handle;
  422. }
  423. static void ImGui_ImplMetal_DestroyWindow(ImGuiViewport* viewport)
  424. {
  425. // The main viewport (owned by the application) will always have RendererUserData == 0 since we didn't create the data for it.
  426. if (ImGuiViewportDataMetal* data = (ImGuiViewportDataMetal*)viewport->RendererUserData)
  427. IM_DELETE(data);
  428. viewport->RendererUserData = nullptr;
  429. }
  430. inline static CGSize MakeScaledSize(CGSize size, CGFloat scale)
  431. {
  432. return CGSizeMake(size.width * scale, size.height * scale);
  433. }
  434. static void ImGui_ImplMetal_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  435. {
  436. ImGuiViewportDataMetal* data = (ImGuiViewportDataMetal*)viewport->RendererUserData;
  437. data->MetalLayer.drawableSize = MakeScaledSize(CGSizeMake(size.x, size.y), viewport->DpiScale);
  438. }
  439. static void ImGui_ImplMetal_RenderWindow(ImGuiViewport* viewport, void*)
  440. {
  441. ImGuiViewportDataMetal* data = (ImGuiViewportDataMetal*)viewport->RendererUserData;
  442. #if TARGET_OS_OSX
  443. void* handle = viewport->PlatformHandleRaw ? viewport->PlatformHandleRaw : viewport->PlatformHandle;
  444. NSWindow* window = (__bridge NSWindow*)handle;
  445. // Always render the first frame, regardless of occlusionState, to avoid an initial flicker
  446. if ((window.occlusionState & NSWindowOcclusionStateVisible) == 0 && !data->FirstFrame)
  447. {
  448. // Do not render windows which are completely occluded. Calling -[CAMetalLayer nextDrawable] will hang for
  449. // approximately 1 second if the Metal layer is completely occluded.
  450. return;
  451. }
  452. data->FirstFrame = false;
  453. float fb_scale = (float)window.backingScaleFactor;
  454. if (data->MetalLayer.contentsScale != fb_scale)
  455. {
  456. data->MetalLayer.contentsScale = fb_scale;
  457. data->MetalLayer.drawableSize = MakeScaledSize(window.frame.size, fb_scale);
  458. }
  459. #endif
  460. id <CAMetalDrawable> drawable = [data->MetalLayer nextDrawable];
  461. if (drawable == nil)
  462. return;
  463. MTLRenderPassDescriptor* renderPassDescriptor = data->RenderPassDescriptor;
  464. renderPassDescriptor.colorAttachments[0].texture = drawable.texture;
  465. renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 0);
  466. if ((viewport->Flags & ImGuiViewportFlags_NoRendererClear) == 0)
  467. renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
  468. id <MTLCommandBuffer> commandBuffer = [data->CommandQueue commandBuffer];
  469. id <MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
  470. ImGui_ImplMetal_RenderDrawData(viewport->DrawData, commandBuffer, renderEncoder);
  471. [renderEncoder endEncoding];
  472. [commandBuffer presentDrawable:drawable];
  473. [commandBuffer commit];
  474. }
  475. static void ImGui_ImplMetal_InitMultiViewportSupport()
  476. {
  477. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  478. platform_io.Renderer_CreateWindow = ImGui_ImplMetal_CreateWindow;
  479. platform_io.Renderer_DestroyWindow = ImGui_ImplMetal_DestroyWindow;
  480. platform_io.Renderer_SetWindowSize = ImGui_ImplMetal_SetWindowSize;
  481. platform_io.Renderer_RenderWindow = ImGui_ImplMetal_RenderWindow;
  482. }
  483. static void ImGui_ImplMetal_ShutdownMultiViewportSupport()
  484. {
  485. ImGui::DestroyPlatformWindows();
  486. }
  487. static void ImGui_ImplMetal_CreateDeviceObjectsForPlatformWindows()
  488. {
  489. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  490. for (int i = 1; i < platform_io.Viewports.Size; i++)
  491. if (!platform_io.Viewports[i]->RendererUserData)
  492. ImGui_ImplMetal_CreateWindow(platform_io.Viewports[i]);
  493. }
  494. static void ImGui_ImplMetal_InvalidateDeviceObjectsForPlatformWindows()
  495. {
  496. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  497. for (int i = 1; i < platform_io.Viewports.Size; i++)
  498. if (platform_io.Viewports[i]->RendererUserData)
  499. ImGui_ImplMetal_DestroyWindow(platform_io.Viewports[i]);
  500. }
  501. #pragma mark - MetalBuffer implementation
  502. @implementation MetalBuffer
  503. - (instancetype)initWithBuffer:(id<MTLBuffer>)buffer
  504. {
  505. if ((self = [super init]))
  506. {
  507. _buffer = buffer;
  508. _lastReuseTime = GetMachAbsoluteTimeInSeconds();
  509. }
  510. return self;
  511. }
  512. @end
  513. #pragma mark - FramebufferDescriptor implementation
  514. @implementation FramebufferDescriptor
  515. - (instancetype)initWithRenderPassDescriptor:(MTLRenderPassDescriptor*)renderPassDescriptor
  516. {
  517. if ((self = [super init]))
  518. {
  519. _sampleCount = renderPassDescriptor.colorAttachments[0].texture.sampleCount;
  520. _colorPixelFormat = renderPassDescriptor.colorAttachments[0].texture.pixelFormat;
  521. _depthPixelFormat = renderPassDescriptor.depthAttachment.texture.pixelFormat;
  522. _stencilPixelFormat = renderPassDescriptor.stencilAttachment.texture.pixelFormat;
  523. }
  524. return self;
  525. }
  526. - (nonnull id)copyWithZone:(nullable NSZone*)zone
  527. {
  528. FramebufferDescriptor* copy = [[FramebufferDescriptor allocWithZone:zone] init];
  529. copy.sampleCount = self.sampleCount;
  530. copy.colorPixelFormat = self.colorPixelFormat;
  531. copy.depthPixelFormat = self.depthPixelFormat;
  532. copy.stencilPixelFormat = self.stencilPixelFormat;
  533. return copy;
  534. }
  535. - (NSUInteger)hash
  536. {
  537. NSUInteger sc = _sampleCount & 0x3;
  538. NSUInteger cf = _colorPixelFormat & 0x3FF;
  539. NSUInteger df = _depthPixelFormat & 0x3FF;
  540. NSUInteger sf = _stencilPixelFormat & 0x3FF;
  541. NSUInteger hash = (sf << 22) | (df << 12) | (cf << 2) | sc;
  542. return hash;
  543. }
  544. - (BOOL)isEqual:(id)object
  545. {
  546. FramebufferDescriptor* other = object;
  547. if (![other isKindOfClass:[FramebufferDescriptor class]])
  548. return NO;
  549. return other.sampleCount == self.sampleCount &&
  550. other.colorPixelFormat == self.colorPixelFormat &&
  551. other.depthPixelFormat == self.depthPixelFormat &&
  552. other.stencilPixelFormat == self.stencilPixelFormat;
  553. }
  554. @end
  555. #pragma mark - MetalTexture implementation
  556. @implementation MetalTexture
  557. - (instancetype)initWithTexture:(id<MTLTexture>)metalTexture
  558. {
  559. if ((self = [super init]))
  560. self.metalTexture = metalTexture;
  561. return self;
  562. }
  563. @end
  564. #pragma mark - MetalContext implementation
  565. @implementation MetalContext
  566. - (instancetype)init
  567. {
  568. if ((self = [super init]))
  569. {
  570. self.renderPipelineStateCache = [NSMutableDictionary dictionary];
  571. self.bufferCache = [NSMutableArray array];
  572. _lastBufferCachePurge = GetMachAbsoluteTimeInSeconds();
  573. }
  574. return self;
  575. }
  576. - (MetalBuffer*)dequeueReusableBufferOfLength:(NSUInteger)length device:(id<MTLDevice>)device
  577. {
  578. uint64_t now = GetMachAbsoluteTimeInSeconds();
  579. @synchronized(self.bufferCache)
  580. {
  581. // Purge old buffers that haven't been useful for a while
  582. if (now - self.lastBufferCachePurge > 1.0)
  583. {
  584. NSMutableArray* survivors = [NSMutableArray array];
  585. for (MetalBuffer* candidate in self.bufferCache)
  586. if (candidate.lastReuseTime > self.lastBufferCachePurge)
  587. [survivors addObject:candidate];
  588. self.bufferCache = [survivors mutableCopy];
  589. self.lastBufferCachePurge = now;
  590. }
  591. // See if we have a buffer we can reuse
  592. MetalBuffer* bestCandidate = nil;
  593. for (MetalBuffer* candidate in self.bufferCache)
  594. if (candidate.buffer.length >= length && (bestCandidate == nil || bestCandidate.lastReuseTime > candidate.lastReuseTime))
  595. bestCandidate = candidate;
  596. if (bestCandidate != nil)
  597. {
  598. [self.bufferCache removeObject:bestCandidate];
  599. bestCandidate.lastReuseTime = now;
  600. return bestCandidate;
  601. }
  602. }
  603. // No luck; make a new buffer
  604. id<MTLBuffer> backing = [device newBufferWithLength:length options:MTLResourceStorageModeShared];
  605. return [[MetalBuffer alloc] initWithBuffer:backing];
  606. }
  607. // Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling.
  608. - (id<MTLRenderPipelineState>)renderPipelineStateForFramebufferDescriptor:(FramebufferDescriptor*)descriptor device:(id<MTLDevice>)device
  609. {
  610. NSError* error = nil;
  611. NSString* shaderSource = @""
  612. "#include <metal_stdlib>\n"
  613. "using namespace metal;\n"
  614. "\n"
  615. "struct Uniforms {\n"
  616. " float4x4 projectionMatrix;\n"
  617. "};\n"
  618. "\n"
  619. "struct VertexIn {\n"
  620. " float2 position [[attribute(0)]];\n"
  621. " float2 texCoords [[attribute(1)]];\n"
  622. " uchar4 color [[attribute(2)]];\n"
  623. "};\n"
  624. "\n"
  625. "struct VertexOut {\n"
  626. " float4 position [[position]];\n"
  627. " float2 texCoords;\n"
  628. " float4 color;\n"
  629. "};\n"
  630. "\n"
  631. "vertex VertexOut vertex_main(VertexIn in [[stage_in]],\n"
  632. " constant Uniforms &uniforms [[buffer(1)]]) {\n"
  633. " VertexOut out;\n"
  634. " out.position = uniforms.projectionMatrix * float4(in.position, 0, 1);\n"
  635. " out.texCoords = in.texCoords;\n"
  636. " out.color = float4(in.color) / float4(255.0);\n"
  637. " return out;\n"
  638. "}\n"
  639. "\n"
  640. "fragment half4 fragment_main(VertexOut in [[stage_in]],\n"
  641. " texture2d<half, access::sample> texture [[texture(0)]]) {\n"
  642. " constexpr sampler linearSampler(coord::normalized, min_filter::linear, mag_filter::linear, mip_filter::linear);\n"
  643. " half4 texColor = texture.sample(linearSampler, in.texCoords);\n"
  644. " return half4(in.color) * texColor;\n"
  645. "}\n";
  646. id<MTLLibrary> library = [device newLibraryWithSource:shaderSource options:nil error:&error];
  647. if (library == nil)
  648. {
  649. NSLog(@"Error: failed to create Metal library: %@", error);
  650. return nil;
  651. }
  652. id<MTLFunction> vertexFunction = [library newFunctionWithName:@"vertex_main"];
  653. id<MTLFunction> fragmentFunction = [library newFunctionWithName:@"fragment_main"];
  654. if (vertexFunction == nil || fragmentFunction == nil)
  655. {
  656. NSLog(@"Error: failed to find Metal shader functions in library: %@", error);
  657. return nil;
  658. }
  659. MTLVertexDescriptor* vertexDescriptor = [MTLVertexDescriptor vertexDescriptor];
  660. vertexDescriptor.attributes[0].offset = offsetof(ImDrawVert, pos);
  661. vertexDescriptor.attributes[0].format = MTLVertexFormatFloat2; // position
  662. vertexDescriptor.attributes[0].bufferIndex = 0;
  663. vertexDescriptor.attributes[1].offset = offsetof(ImDrawVert, uv);
  664. vertexDescriptor.attributes[1].format = MTLVertexFormatFloat2; // texCoords
  665. vertexDescriptor.attributes[1].bufferIndex = 0;
  666. vertexDescriptor.attributes[2].offset = offsetof(ImDrawVert, col);
  667. vertexDescriptor.attributes[2].format = MTLVertexFormatUChar4; // color
  668. vertexDescriptor.attributes[2].bufferIndex = 0;
  669. vertexDescriptor.layouts[0].stepRate = 1;
  670. vertexDescriptor.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
  671. vertexDescriptor.layouts[0].stride = sizeof(ImDrawVert);
  672. MTLRenderPipelineDescriptor* pipelineDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
  673. pipelineDescriptor.vertexFunction = vertexFunction;
  674. pipelineDescriptor.fragmentFunction = fragmentFunction;
  675. pipelineDescriptor.vertexDescriptor = vertexDescriptor;
  676. pipelineDescriptor.rasterSampleCount = self.framebufferDescriptor.sampleCount;
  677. pipelineDescriptor.colorAttachments[0].pixelFormat = self.framebufferDescriptor.colorPixelFormat;
  678. pipelineDescriptor.colorAttachments[0].blendingEnabled = YES;
  679. pipelineDescriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;
  680. pipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha;
  681. pipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
  682. pipelineDescriptor.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd;
  683. pipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorOne;
  684. pipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
  685. pipelineDescriptor.depthAttachmentPixelFormat = self.framebufferDescriptor.depthPixelFormat;
  686. pipelineDescriptor.stencilAttachmentPixelFormat = self.framebufferDescriptor.stencilPixelFormat;
  687. id<MTLRenderPipelineState> renderPipelineState = [device newRenderPipelineStateWithDescriptor:pipelineDescriptor error:&error];
  688. if (error != nil)
  689. NSLog(@"Error: failed to create Metal pipeline state: %@", error);
  690. return renderPipelineState;
  691. }
  692. @end
  693. //-----------------------------------------------------------------------------
  694. #endif // #ifndef IMGUI_DISABLE