From aa7ba629784ebf4a253cf78d85c286c310ad0cb5 Mon Sep 17 00:00:00 2001
From: Jeremy Demeule <[EMAIL REDACTED]>
Date: Thu, 31 Aug 2023 10:23:45 +0200
Subject: [PATCH] metal: Add hint to select low power device instead of the
default one (#8182)
On some system like MacBook Pro Intel with AMD card, asking for the default device will always return the AMD GPU.
This is not an issue for 99% of the case when the renderer context is here to provide the maximum performance level like for game.
However, for video application using GPU for 1 quad and 1 texture, using the discrete GPU for that lead to an important power consumption (4 to 8W), heat increase, and fan noise.
With this patch, I successfully amend ffplay to only use the integrated GPU (i.e. the Intel one), instead of the discrete GPU (i.e. the AMD one).
---
include/SDL_hints.h | 11 +++++++++++
src/render/metal/SDL_render_metal.m | 18 ++++++++++++++++--
2 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index c808a60db562..00beef51e29e 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -1474,6 +1474,17 @@ extern "C" {
*/
#define SDL_HINT_RENDER_VSYNC "SDL_RENDER_VSYNC"
+/**
+ * \brief A variable controlling whether the Metal render driver select low power device over default one
+ *
+ * This variable can be set to the following values:
+ * "0" - Use the prefered OS device
+ * "1" - Select a low power one
+ *
+ * By default the prefered OS device is used.
+ */
+#define SDL_HINT_RENDER_METAL_PREFER_LOW_POWER_DEVICE "SDL_RENDER_METAL_PREFER_LOW_POWER_DEVICE"
+
/**
* \brief A variable controlling if VSYNC is automatically disable if doesn't reach the enough FPS
*
diff --git a/src/render/metal/SDL_render_metal.m b/src/render/metal/SDL_render_metal.m
index c2265601fa82..76d1d834e367 100644
--- a/src/render/metal/SDL_render_metal.m
+++ b/src/render/metal/SDL_render_metal.m
@@ -1654,8 +1654,22 @@ static SDL_MetalView GetWindowView(SDL_Window *window)
return NULL;
}
- // !!! FIXME: MTLCopyAllDevices() can find other GPUs on macOS...
- mtldevice = MTLCreateSystemDefaultDevice();
+#ifdef __MACOSX__
+ if (SDL_GetHintBoolean(SDL_HINT_RENDER_METAL_PREFER_LOW_POWER_DEVICE, SDL_TRUE)) {
+ NSArray<id<MTLDevice>> *devices = MTLCopyAllDevices();
+
+ for (id<MTLDevice> device in devices) {
+ if (device.isLowPower) {
+ mtldevice = device;
+ break;
+ }
+ }
+ }
+#endif
+
+ if (mtldevice == nil) {
+ mtldevice = MTLCreateSystemDefaultDevice();
+ }
if (mtldevice == nil) {
SDL_free(renderer);