MVKDevice: Only force a mux switch when a swapchain is created.

We only want the window server to use the high-performance GPU if we
will use it to present to the display. If we won't use it to present, we
can save some battery life by not using the display. I had hoped this
would help window server stability in case something goes horribly
wrong while using the GPU, but my experience has sadly not borne this
out.

My testing shows that the device returned by
`MTLCreateSystemDefaultDevice()` is exactly equal (i.e. has the same
pointer value) to one of the devices returned by `MTLCopyAllDevices()`,
so we should see no problems from doing this at swapchain create time
instead of device create time.
This commit is contained in:
Chip Davis 2020-09-03 14:46:55 -05:00
parent f319e9bd68
commit ee59948ed8
2 changed files with 21 additions and 14 deletions

View File

@ -2590,6 +2590,20 @@ void MVKDevice::destroyImageView(MVKImageView* mvkImgView,
MVKSwapchain* MVKDevice::createSwapchain(const VkSwapchainCreateInfoKHR* pCreateInfo,
const VkAllocationCallbacks* pAllocator) {
#if MVK_MACOS
// If we have selected a high-power GPU and want to force the window system
// to use it, force the window system to use a high-power GPU by calling the
// MTLCreateSystemDefaultDevice function, and if that GPU is the same as the
// selected GPU, update the MTLDevice instance used by the MVKPhysicalDevice.
id<MTLDevice> mtlDevice = _physicalDevice->getMTLDevice();
if (_pMVKConfig->switchSystemGPU && !(mtlDevice.isLowPower || mtlDevice.isHeadless) ) {
id<MTLDevice> sysMTLDevice = MTLCreateSystemDefaultDevice();
if (mvkGetRegistryID(sysMTLDevice) == mvkGetRegistryID(mtlDevice)) {
_physicalDevice->replaceMTLDevice(sysMTLDevice);
}
}
#endif
return new MVKSwapchain(this, pCreateInfo);
}
@ -3186,20 +3200,6 @@ void MVKDevice::initPhysicalDevice(MVKPhysicalDevice* physicalDevice, const VkDe
# define MVK_CONFIG_USE_COMMAND_POOLING 1
# endif
MVK_SET_FROM_ENV_OR_BUILD_BOOL(_useCommandPooling, MVK_CONFIG_USE_COMMAND_POOLING);
#if MVK_MACOS
// If we have selected a high-power GPU and want to force the window system
// to use it, force the window system to use a high-power GPU by calling the
// MTLCreateSystemDefaultDevice function, and if that GPU is the same as the
// selected GPU, update the MTLDevice instance used by the MVKPhysicalDevice.
id<MTLDevice> mtlDevice = _physicalDevice->getMTLDevice();
if (_pMVKConfig->switchSystemGPU && !(mtlDevice.isLowPower || mtlDevice.isHeadless) ) {
id<MTLDevice> sysMTLDevice = MTLCreateSystemDefaultDevice();
if (mvkGetRegistryID(sysMTLDevice) == mvkGetRegistryID(mtlDevice)) {
_physicalDevice->replaceMTLDevice(sysMTLDevice);
}
}
#endif
}
void MVKDevice::enableFeatures(const VkDeviceCreateInfo* pCreateInfo) {

View File

@ -1246,7 +1246,14 @@ void MVKPixelFormats::modifyMTLFormatCapabilities() {
if (_physicalDevice) {
modifyMTLFormatCapabilities(_physicalDevice->getMTLDevice());
} else {
#if MVK_IOS_OR_TVOS
id<MTLDevice> mtlDevice = MTLCreateSystemDefaultDevice(); // temp retained
#endif
#if MVK_MACOS
NSArray<id<MTLDevice>>* mtlDevices = MTLCopyAllDevices(); // temp retained
id<MTLDevice> mtlDevice = [mtlDevices[0] retain]; // temp retained
[mtlDevices release]; // temp release
#endif
modifyMTLFormatCapabilities(mtlDevice);
[mtlDevice release]; // release temp instance
}