The Game Mode APIs for the Universal Windows Platform (UWP) allow you to produce the most optimized gaming experience by taking advantage of Game Mode in Windows 10. These APIs are located in the <expandedresources.h> header.
These are Win32 APIs that are supported in UWP desktop and Xbox apps, as well as Win32 apps (except for ReleaseExclusiveCpuSets, which isn't supported in Win32 apps).
Game Mode provides customers with the best possible gaming experience by fully utilizing the capacity of their current hardware. It does this by granting a game exclusive or priority access to hardware resources. These resources being dedicated to the game help it hit performance targets more consistently. The performance increase that comes from Game Mode is directly related to the number and impact of other activities running on the device.
The app must be in the foreground and have focus before exclusive resources are granted.
Game Mode works by default for most Windows games, requiring no action or opt-in by the customer, and no work by the game developer. However, you can use the Game Mode API to take it a step further and programmatically query for available system resources, determining whether the operating system considers each resource as shared or exclusive. You can leverage the available system resources in a way that best fits your game design and the configuration of the customer's system.
By using the expandedResources capability, you can explicitly declare that the game will work with Game Mode. As part of launching the game, the process will go into Game Mode with a set of defaults, and you can use the APIs to see what resources are available on the customer's device.
The Game Mode API has the following members:
- GetExpandedResourceExclusiveCpuCount: Query for details of exclusive hardware, such as cache topology, in order to rank resources.
- HasExpandedResources: Know when the game enters and exits Game Mode. When the game enters Game Mode, it can provide a tailored experience. When the game exits Game Mode, it can scale back the usage of resources. The general pattern is to poll once per frame.
- ReleaseExclusiveCpuSets: Opt out of CPU exclusivity.
ReleaseExclusiveCpuSets requires the expandedResources restricted capability, which you can select by opening Package.appxmanifest in Visual Studio and navigating to the Capabilities tab. Alternatively, you can edit the file's code directly:
This capability is granted on a per-title basis; contact your account manager for more information. You can publish a UWP app with this capability to the Store if it targets desktop, but if it targets Xbox it will be rejected in certification.
Games should call HasExpandedResources once per frame or game tick to determine whether exclusive resources have been granted. When they have been granted, the game can call GetSystemCpuSetInformation to understand what cores the game is eligible to use. Using this function, deeper inspection, such as getting cache details, can be achieved to rank the cores for performance. The SYSTEM_CPU_SET_INFORMATION structure returned by GetSystemCpuSetInformation exposes details that the game can use to scale the number of threads it runs, and give threads the affinity for the appropriate cores using SetThreadSelectedCpuSets.
When exclusive resources are revoked, such as when the game loses focus, the game will discover this by polling with HasExpandedResources, and can re-scale as appropriate. Some games may reduce the level of detail or use other tactics to maintain performance.
A small number of games may choose to call GetExpandedResourceExclusiveCpuCount to determine what CPU resources are available for exclusive use. Based on the developer's judgment, they may opt-out of CPU exclusivity by calling ReleaseExclusiveCpuSets to get access to all cores, but at a higher latency due to other processes and system activities being scheduled on the same cores as the game. However, the game would still get access to other Game Mode resources, such as increased GPU prioritization. As with SetProcessDefaultCpuSets, ReleaseExclusiveCpuSets applies to the whole process.
While CPU resources may be revoked if the game exits Game Mode, memory resources, once granted, will never be revoked. Games can use APIs such as AppMemoryUsageLimit to understand what is available.
Example: Benchmark on startup
Games often do system inspection on startup to match the game experience against the system resources. Often, the methods used would involve some combination of how many CPUs are available, to scale the count of work queue threads appropriately.
Example: Game loop
HasExpandedResources should be called once per frame to detect state changes. This can be called in the main game loop that performs functions like collating inputs from devices and updating the world state.
In this section
||Gets the expected number of exclusive CPU sets that are available to the app when in Game Mode.
||Gets the current resource state (that is, whether the app is running in Game Mode or shared mode).
||Opts out of CPU exclusivity, giving the app access to all cores, but at the cost of having to share them with other processes.