Search code examples
c#.net-coregpuvsync

Get Vsync status on GPU C#


I am developing a .net core application where I need to retrieve as much information about the system as possible. I have managed to retrieve already a lot, however, I am struggling to find out whether the GPU has Vsync turned on or not. I have tried the NvApi Wrapper for an Nvidia GPU but couldn't get it working as it throws an exception when trying to initialize it (could not load nvapi64.dll). The original NvApi is written in C and making a Wrapper just for this one specific thing seems a bit too much. I guess there must be an easier way to get this information.

I have also tried to get the value using the ManagementObjectSearcher as shown below but it doesn't return whether vsync is active or not:

var managementObjectSearcher = new ManagementObjectSearcher("select * from Win32_VideoController");
foreach (ManagementObject obj in managementObjectSearcher.Get())
{
    foreach(var prop in obj.Properties)
    {
        Console.WriteLine($"Name: {prop.Name} Value: {prop.Value}");
    }
}

Solution

  • I could get the info you need using the open source library OpenTK which provides a managed wrapper for OpenGL, for more info see here.

    First, add the NuGet package compatible with .net core:

    Install-Package OpenTK.NetStandard -Version 1.0.4 
    

    Then, you can just create a dummy class with an instance of GameWindow and you will have access to the information you are looking for (among others):

    public sealed class DummyInfoGameWindow : GameWindow
    {
        private DummyInfoGameWindow() {}
    
        public static DummyInfoGameWindow InitAndGetInfo()
        {
            return new DummyInfoGameWindow();
        }
    }
    

    Then just call the static method from your code:

    static void Main(string[] args)
    {
        var info = DummyInfoGameWindow.InitAndGetInfo();
    
        Console.WriteLine($"VSync enabled {info.VSync}");
    
        Console.Read();
    }
    

    The output is:

    VSync enabled On