Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing hardware accelerated rendering

I have an OpenGL library written in c++ that is used from a C# application using C++/CLI adapters. My problem is that if the application is used on laptops with Nvidia Optimus technology the application will not use the hardware acceleration and fail.

I have tried to use the info found in Nvidias document http://developer.download.nvidia.com/devzone/devcenter/gamegraphics/files/OptimusRenderingPolicies.pdf about linking libs to my C++-dll and exporting NvOptimusEnablement from my OpenGL-library but that fails. I guess I have to do something with the .exe not with the .dlls linked to the .exe

For us it is not a good option to use profiles since we need to ensure that the nvidia hardware is used.

Is there some way a C# application can force Optimus to use the Nvidia chipset instead of the integrated Intel chipset?

like image 283
JohanR Avatar asked Jun 24 '13 07:06

JohanR


People also ask

What will happen if I Force GPU rendering?

When to Force GPU Rendering. Enabling this setting will offload window components like text, buttons and 2d graphics calculations to the GPU. This will make your device render UI animations better and feel less laggy.

Is it OK to turn on hardware accelerated GPU scheduling?

You probably won't notice a change if you switch accelerated GPU scheduling on, but that doesn't mean you shouldn't do it. Assuming you have a system that can support the upgrade, switching it on is probably a good idea since it can help reduce some of the pressure on your CPU.

Is it better to turn on hardware acceleration?

Unless you're facing an issue that you know is because of hardware acceleration, you shouldn't turn off hardware acceleration. It'll generally do more good than harm, but when you see it is causing you more harm instead, that's when you should turn it off for that one specific app.

What happens if I turn on hardware acceleration?

The term refers to boosting your PC's performance. When you have hardware acceleration turned on, certain tasks become noticeably faster. This results in programs and video games performing better.


2 Answers

A working solution. Actually all those already mentioned, but it took me a time to understand how to get it work...

[System.Runtime.InteropServices.DllImport("nvapi64.dll", EntryPoint = "fake")]
static extern int LoadNvApi64();

[System.Runtime.InteropServices.DllImport("nvapi.dll", EntryPoint = "fake")]
static extern int LoadNvApi32();

private void InitializeDedicatedGraphics()
{
    try
    {
        if (Environment.Is64BitProcess)
            LoadNvApi64();
        else
            LoadNvApi32();
    }
    catch { } // will always fail since 'fake' entry point doesn't exists
}

Important - call InitializeDedicatedGraphics() before any window is created

like image 139
igorushi Avatar answered Sep 21 '22 08:09

igorushi


I tried both options from the swine, but neither worked by themselves. I found I needed to attempt to call the imported function.

using System.Runtime.InteropServices;

class OptimusEnabler
{
    [DllImport("nvapi.dll")]
    public static extern int NvAPI_Initialize();
};

then in my app startup:

try
{
    ///Ignore any System.EntryPointNotFoundException
    ///or System.DllNotFoundException exceptions here
    OptimusEnabler.NvAPI_Initialize();
}
catch
{ }

On an nVidia Optimus system, I am getting a System.EntryPointNotFoundException, but it still works to make the application use the nVidia hardware. Tested on a system with an ATI card, I got a System.DllNotFoundException. Either way, attempting to call this and ignoring any exception here seems to work fine.

like image 43
crashmstr Avatar answered Sep 19 '22 08:09

crashmstr