Browse Source

Backends: SDL_GPU3: select between metallib and MSL shaders based on availability. (#9076)

Amend fd887f5
ocornut 3 weeks ago
parent
commit
75db81cf08
2 changed files with 29 additions and 26 deletions
  1. 26 23
      backends/imgui_impl_sdlgpu3.cpp
  2. 3 3
      docs/CHANGELOG.txt

+ 26 - 23
backends/imgui_impl_sdlgpu3.cpp

@@ -22,7 +22,7 @@
 //   Calling the function is MANDATORY, otherwise the ImGui will not upload neither the vertex nor the index buffer for the GPU. See imgui_impl_sdlgpu3.cpp for more info.
 //   Calling the function is MANDATORY, otherwise the ImGui will not upload neither the vertex nor the index buffer for the GPU. See imgui_impl_sdlgpu3.cpp for more info.
 
 
 // CHANGELOG
 // CHANGELOG
-//  2025-11-26: Use MSL shaders on macOS to support macOS 10.14+ (instead of Metallib shaders requiring macOS 14+). Requires calling SDL_CreateGPUDevice() with SDL_GPU_SHADERFORMAT_MSL.
+//  2025-11-26: macOS version can use MSL shaders in order to support macOS 10.14+ (vs Metallib shaders requiring macOS 14+). Requires calling SDL_CreateGPUDevice() with SDL_GPU_SHADERFORMAT_MSL.
 //  2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
 //  2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
 //  2025-08-20: Added ImGui_ImplSDLGPU3_InitInfo::SwapchainComposition and ImGui_ImplSDLGPU3_InitInfo::PresentMode to configure how secondary viewports are created.
 //  2025-08-20: Added ImGui_ImplSDLGPU3_InitInfo::SwapchainComposition and ImGui_ImplSDLGPU3_InitInfo::PresentMode to configure how secondary viewports are created.
 //  2025-08-08: *BREAKING* Changed ImTextureID type from SDL_GPUTextureSamplerBinding* to SDL_GPUTexture*, which is more natural and easier for user to manage. If you need to change the current sampler, you can access the ImGui_ImplSDLGPU3_RenderState struct. (#8866, #8163, #7998, #7988)
 //  2025-08-08: *BREAKING* Changed ImTextureID type from SDL_GPUTextureSamplerBinding* to SDL_GPUTexture*, which is more natural and easier for user to manage. If you need to change the current sampler, you can access the ImGui_ImplSDLGPU3_RenderState struct. (#8866, #8163, #7998, #7988)
@@ -453,28 +453,31 @@ static void ImGui_ImplSDLGPU3_CreateShaders()
 #ifdef __APPLE__
 #ifdef __APPLE__
     else
     else
     {
     {
-#include <TargetConditionals.h>
-#if TARGET_OS_OSX
-        // macOS: using MSL source
-        vertex_shader_info.entrypoint = "main0";
-        vertex_shader_info.format = SDL_GPU_SHADERFORMAT_MSL;
-        vertex_shader_info.code = msl_vertex;
-        vertex_shader_info.code_size = sizeof(msl_vertex);
-        fragment_shader_info.entrypoint = "main0";
-        fragment_shader_info.format = SDL_GPU_SHADERFORMAT_MSL;
-        fragment_shader_info.code = msl_fragment;
-        fragment_shader_info.code_size = sizeof(msl_fragment);
-#elif TARGET_OS_IPHONE
-        // iOS device: using metallib blobs
-        vertex_shader_info.entrypoint = "main0";
-        vertex_shader_info.format = SDL_GPU_SHADERFORMAT_METALLIB;
-        vertex_shader_info.code = metallib_vertex;
-        vertex_shader_info.code_size = sizeof(metallib_vertex);
-        fragment_shader_info.entrypoint = "main0";
-        fragment_shader_info.format = SDL_GPU_SHADERFORMAT_METALLIB;
-        fragment_shader_info.code = metallib_fragment;
-        fragment_shader_info.code_size = sizeof(metallib_fragment);
-#endif
+        SDL_GPUShaderFormat supported_formats = SDL_GetGPUShaderFormats(v->Device);
+        if (supported_formats & SDL_GPU_SHADERFORMAT_METALLIB)
+        {
+            // Using metallib blobs (macOS 14+, iOS)
+            vertex_shader_info.entrypoint = "main0";
+            vertex_shader_info.format = SDL_GPU_SHADERFORMAT_METALLIB;
+            vertex_shader_info.code = metallib_vertex;
+            vertex_shader_info.code_size = sizeof(metallib_vertex);
+            fragment_shader_info.entrypoint = "main0";
+            fragment_shader_info.format = SDL_GPU_SHADERFORMAT_METALLIB;
+            fragment_shader_info.code = metallib_fragment;
+            fragment_shader_info.code_size = sizeof(metallib_fragment);
+        }
+        else if (supported_formats & SDL_GPU_SHADERFORMAT_MSL)
+        {
+            // macOS: using MSL source
+            vertex_shader_info.entrypoint = "main0";
+            vertex_shader_info.format = SDL_GPU_SHADERFORMAT_MSL;
+            vertex_shader_info.code = msl_vertex;
+            vertex_shader_info.code_size = sizeof(msl_vertex);
+            fragment_shader_info.entrypoint = "main0";
+            fragment_shader_info.format = SDL_GPU_SHADERFORMAT_MSL;
+            fragment_shader_info.code = msl_fragment;
+            fragment_shader_info.code_size = sizeof(msl_fragment);
+        }
     }
     }
 #endif
 #endif
     bd->VertexShader = SDL_CreateGPUShader(v->Device, &vertex_shader_info);
     bd->VertexShader = SDL_CreateGPUShader(v->Device, &vertex_shader_info);

+ 3 - 3
docs/CHANGELOG.txt

@@ -54,9 +54,9 @@ Other Changes:
 - Scrollbar: fixed a codepath leading to a divide-by-zero (which would not be
 - Scrollbar: fixed a codepath leading to a divide-by-zero (which would not be
   noticeable by user but detected by sanitizers). (#9089) [@judicaelclair]
   noticeable by user but detected by sanitizers). (#9089) [@judicaelclair]
 - Backends:
 - Backends:
-  - SDL_GPU3: on macOS, use MSL shaders in order to support macOS 10.14+
-    (instead of Metallib shaders requiring macOS 14+). (#9076) [@Niminem]
-    Requires calling SDL_CreateGPUDevice() with SDL_GPU_SHADERFORMAT_MSL.
+  - SDL_GPU3: macOS version can use MSL shaders in order to support macOS 10.14+
+    (vs Metallib shaders requiring macOS 14+). Requires application calling
+    SDL_CreateGPUDevice() with SDL_GPU_SHADERFORMAT_MSL. (#9076) [@Niminem]
   - Vulkan: helper for creating a swapchain (used by examples and multi-viewports)
   - Vulkan: helper for creating a swapchain (used by examples and multi-viewports)
     selects `VkSwapchainCreateInfoKHR`'s `compositeAlpha` value based on
     selects `VkSwapchainCreateInfoKHR`'s `compositeAlpha` value based on
     `cap.supportedCompositeAlpha`, which seems to be required on some Android
     `cap.supportedCompositeAlpha`, which seems to be required on some Android