BorrowedTorch.Capabilities 1.0.0

Prefix Reserved
dotnet add package BorrowedTorch.Capabilities --version 1.0.0
                    
NuGet\Install-Package BorrowedTorch.Capabilities -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BorrowedTorch.Capabilities" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BorrowedTorch.Capabilities" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="BorrowedTorch.Capabilities" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BorrowedTorch.Capabilities --version 1.0.0
                    
#r "nuget: BorrowedTorch.Capabilities, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BorrowedTorch.Capabilities@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BorrowedTorch.Capabilities&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=BorrowedTorch.Capabilities&version=1.0.0
                    
Install as a Cake Tool

BorrowedTorch.Capabilities

What the machine a game runs on offers: its video cards, which one the game draws with and how to pick another, and its displays with every resolution and refresh rate each supports. For a game's settings screen, and for its start: which display to open on, at which size. Windows only; independent of any engine, MonoGame's included.

using BorrowedTorch.Capabilities;

foreach (var card in VideoCards.List())
    Console.WriteLine($"{card.Name}: {card.DedicatedVideoMemory / (1024 * 1024)} MB{(card.IsDefault ? ", drawn with" : "")}");

var display = Displays.List()[0];   // the primary display

Console.WriteLine($"{display.Name}: {display.CurrentMode}, {display.Scale:P0}");

foreach (var resolution in display.Resolutions)
    Console.WriteLine($"{resolution}: {string.Join(", ", display.RefreshRatesAt(resolution))} Hz");

Everything is read when asked and nothing is kept: a card or display plugged in, or a setting changed, shows in the next list. Any thread.

Video cards

VideoCards.List() gives the cards as DirectX knows them, in its order. A VideoCard has:

Property What it is
Name the name the driver gives it: NVIDIA GeForce RTX 3060 Laptop GPU
Vendor, VendorId, DeviceId, SubSystemId, Revision who made it (GpuVendor.Nvidia, ...) and the PCI ids it goes by
DedicatedVideoMemory its own memory, in bytes (little or none for a card built into the processor)
DedicatedSystemMemory, SharedSystemMemory the machine's memory set aside for it, and the memory it can use besides
Luid the id Windows knows it by until it restarts, as Direct3D and DXGI name it
IsSoftware the Microsoft Basic Render Driver: Direct3D drawn on the processor
IsDefault the card a device made without naming one is made on, in this process
IsPreferredForPowerSaving, IsPreferredForHighPerformance the card each graphics preference has Windows make an app's devices on
ReachedBy the preference to set to draw with this card, null when none reaches it

The Basic Render Driver isn't a card, and is left out unless asked for: VideoCards.List(includeSoftware: true). On a machine with no working card (a virtual machine, a build machine) it is the only one, and the default.

Picking the video card

Windows picks the card an app draws with by the app's graphics preference, the one Settings keeps under System, Display, Graphics: Let Windows decide, Power saving (the card built into the processor) or High performance (a card of its own). A renderer can name a card of its own choosing when it makes its device, but many don't: the MonoGame WindowsDX the BorrowedTorch editor runs on makes its device on Windows' default card, whatever GraphicsAdapter it is handed. Such a game picks its card by setting its own preference, as Settings would, and draws with it from its next start - a device in use, and everything made on it, can't move to another card:

var card = VideoCards.List().First(candidate => candidate.Vendor == GpuVendor.Nvidia);

if (card.ReachedBy is { } preference && GpuPreferences.CanSetForCurrentProcess)
{
    GpuPreferences.Set(preference);   // for this game's exe, for the current user
    // ...and offer to restart the game
}

GpuPreferences.Get() reads the preference set for the game (null for none, which works as Let Windows decide), Set(preference) sets it and Remove() takes it away. Each takes another exe's full path instead of the game's own. The preference is kept for an exe: a game started as dotnet Game.dll runs in dotnet's own process, whose preference would hold for every app started that way, so CanSetForCurrentProcess is false and Set() throws for it. Start the game from its exe, as a published game is.

A card that no preference reaches (ReachedBy null: a third card, or a second card of its own on a machine with two) is drawn with only by a renderer that names it. Windows 11 can point a preference at such a card ("specific GPU"); that choice isn't read or written here.

Windows keeps the preferences in HKEY_CURRENT_USER\Software\Microsoft\DirectX\UserGpuPreferences, a text value named by the exe's full path holding GpuPreference=2; among whatever else it keeps for the app (AppStatus=0;), which is left as it is.

Displays

Displays.List() gives the displays on the desktop, the primary one first, the others from left to right. Displays that show the same picture (duplicated) are one. A Display has:

Property What it is
DeviceName \\.\DISPLAY1: what MonoGame's GraphicsAdapter.DeviceName and Screen.DeviceName give
Name the monitor's own name (DELL U2720Q), or Windows' name for its driver
IsPrimary, IsBuiltIn the main display; a panel built into a laptop or tablet
Bounds, WorkArea where it is on the desktop, and the part of it the taskbar leaves
Dpi, Scale the DPI Windows scales it for (96 at 100 %) and Settings' scale as a factor (1.5)
RefreshRate how many times a second it is drawn now, exactly where Windows can tell (59.94)
CurrentMode, Orientation the mode it is set to; how it is turned
VideoCardLuid the Luid of the video card it is driven by
Modes every mode it can be set to, biggest first, then fastest, then deepest
Resolutions the sizes of its modes, each once, biggest first
RefreshRatesAt(resolution) the refresh rates it has at a size, fastest first

Sizes are in the displays' own pixels, whatever the game's DPI awareness: a 4K display at 150 % is 3840 wide, not 2560. Bounds is in desktop coordinates: the primary display's corner is 0, 0, and a display left of it has negative ones.

A borderless window on the display the player picked, at its full size:

var display = Displays.List().First(candidate => candidate.DeviceName == settings.Display);

Window.IsBorderless = true;
Window.Position = new Point(display.Bounds.X, display.Bounds.Y);
graphics.PreferredBackBufferWidth = display.Bounds.Width;
graphics.PreferredBackBufferHeight = display.Bounds.Height;
graphics.ApplyChanges();

Requirements

Windows 10 or later, .NET 10 (net10.0-windows). The Windows calls are made through source-generated imports and function pointers, with no runtime COM: the package works in a trimmed or native AOT game. Which card each preference reaches needs Windows 10 1803 or later; on older ones ReachedBy is null.

Building and testing

dotnet test BorrowedTorch.Capabilities.slnx

The tests in BorrowedTorch.Capabilities.Tests read the cards and displays of the machine they run on, which may have any (a build machine has the Basic Render Driver only, and maybe no display), so they check what holds on every machine, and the layout of every structure Windows fills, to the byte. The preference tests write under a key of their own, HKEY_CURRENT_USER\Software\BorrowedTorch.Capabilities.Tests, deleted after each: the user's real preferences are never touched.

Product Compatible and additional computed target framework versions.
.NET net10.0-windows7.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0-windows7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 140 10/31/2026