Skip to content

Commit c25a49c

Browse files
jdemeuleslouken
authored andcommitted
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). (cherry picked from commit aa7ba62)
1 parent d46fdfa commit c25a49c

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

include/SDL_hints.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,17 @@ extern "C" {
14741474
*/
14751475
#define SDL_HINT_RENDER_VSYNC "SDL_RENDER_VSYNC"
14761476

1477+
/**
1478+
* \brief A variable controlling whether the Metal render driver select low power device over default one
1479+
*
1480+
* This variable can be set to the following values:
1481+
* "0" - Use the prefered OS device
1482+
* "1" - Select a low power one
1483+
*
1484+
* By default the prefered OS device is used.
1485+
*/
1486+
#define SDL_HINT_RENDER_METAL_PREFER_LOW_POWER_DEVICE "SDL_RENDER_METAL_PREFER_LOW_POWER_DEVICE"
1487+
14771488
/**
14781489
* \brief A variable controlling if VSYNC is automatically disable if doesn't reach the enough FPS
14791490
*

src/render/metal/SDL_render_metal.m

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,8 +1654,22 @@ static SDL_MetalView GetWindowView(SDL_Window *window)
16541654
return NULL;
16551655
}
16561656

1657-
// !!! FIXME: MTLCopyAllDevices() can find other GPUs on macOS...
1658-
mtldevice = MTLCreateSystemDefaultDevice();
1657+
#ifdef __MACOSX__
1658+
if (SDL_GetHintBoolean(SDL_HINT_RENDER_METAL_PREFER_LOW_POWER_DEVICE, SDL_TRUE)) {
1659+
NSArray<id<MTLDevice>> *devices = MTLCopyAllDevices();
1660+
1661+
for (id<MTLDevice> device in devices) {
1662+
if (device.isLowPower) {
1663+
mtldevice = device;
1664+
break;
1665+
}
1666+
}
1667+
}
1668+
#endif
1669+
1670+
if (mtldevice == nil) {
1671+
mtldevice = MTLCreateSystemDefaultDevice();
1672+
}
16591673

16601674
if (mtldevice == nil) {
16611675
SDL_free(renderer);

0 commit comments

Comments
 (0)