From ee59948ed8deb45fd68c4509fe86d3756d7e1d08 Mon Sep 17 00:00:00 2001 From: Chip Davis Date: Thu, 3 Sep 2020 14:46:55 -0500 Subject: [PATCH] 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. --- MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm | 28 +++++++++---------- .../MoltenVK/GPUObjects/MVKPixelFormats.mm | 7 +++++ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm index 232a9e24..888989d5 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm @@ -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 = _physicalDevice->getMTLDevice(); + if (_pMVKConfig->switchSystemGPU && !(mtlDevice.isLowPower || mtlDevice.isHeadless) ) { + id 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 = _physicalDevice->getMTLDevice(); - if (_pMVKConfig->switchSystemGPU && !(mtlDevice.isLowPower || mtlDevice.isHeadless) ) { - id sysMTLDevice = MTLCreateSystemDefaultDevice(); - if (mvkGetRegistryID(sysMTLDevice) == mvkGetRegistryID(mtlDevice)) { - _physicalDevice->replaceMTLDevice(sysMTLDevice); - } - } -#endif } void MVKDevice::enableFeatures(const VkDeviceCreateInfo* pCreateInfo) { diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKPixelFormats.mm b/MoltenVK/MoltenVK/GPUObjects/MVKPixelFormats.mm index d937e7b2..0a2a438c 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKPixelFormats.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKPixelFormats.mm @@ -1246,7 +1246,14 @@ void MVKPixelFormats::modifyMTLFormatCapabilities() { if (_physicalDevice) { modifyMTLFormatCapabilities(_physicalDevice->getMTLDevice()); } else { +#if MVK_IOS_OR_TVOS id mtlDevice = MTLCreateSystemDefaultDevice(); // temp retained +#endif +#if MVK_MACOS + NSArray>* mtlDevices = MTLCopyAllDevices(); // temp retained + id mtlDevice = [mtlDevices[0] retain]; // temp retained + [mtlDevices release]; // temp release +#endif modifyMTLFormatCapabilities(mtlDevice); [mtlDevice release]; // release temp instance }