Main Menu

GPU Affinity

Started by Thomy, May 09, 2017, 12:38:08

Previous topic - Next topic

Thomy

Hi,

I would like to let my application select the high-performance GPU (if available) automatically. On my laptop I have an Intel chip and an nvidia 940m. I've recently switched from lwjgl 2.9 to 3.1.1 and ported everything to glfw. I am currently trying to achieve this on Windows.

I've found following flag for lwjgl to check if nvidia gpu affinity is supported:
GL.getCapabilitiesWGL().WGL_NV_gpu_affinity

Though this flag returns false on my laptop. I suppose it's not supported then?

I've kept Googling and found the following topic on stackoverflow: http://stackoverflow.com/questions/6036292/select-a-graphic-device-in-windows-opengl
However, how can I add this C code via lwjgl to enable that flag?
// enable optimus!
extern "C" {
    _declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
}


Or am I heading towards the wrong direction? Are there other, better ways to handle GPU affinity?

Thanks in advance,
Thomy

Kai

The exe which is used to start the process needs to export that flag in its module file (PE on Windows or ELF on Linux).
So, when you simply develop locally by using the default java.exe/javaw.exe launcher, then it would be this exact exe file which needs to have this flag exported. Now, since you cannot simply edit that exe file to make it export that symbol, there are a couple of other options:
- use the JNI Invocation API to write your own launcher
- configure the java.exe/javaw.exe in the Nvidia Control panel to use the dedicated graphics card
- configure the BIOS to disable Optimus and always use the dedicated card

The Nvidia extension you mentioned merely allows to select the graphics card to use when you have multiple dedicated graphics card, such as via SLI in a desktop computer.

Thomy

Thanks! I'll have a look at JNI Invocation API, since this is the easiest way to get it to work for all users on Windows. Maybe lwjgl will also provide functions to set these flags in the future.

Kai

Quote from: Thomy on May 09, 2017, 13:53:46
Maybe lwjgl will also provide functions to set these flags in the future.
This is not possible. Unless LWJGL itself provides a Java launcher exe.

Thomy

I see, thanks! I've found a commit for lwjgl, where this dword is set to 1 by default:
https://github.com/LWJGL/lwjgl/blob/master/src/native/windows/LWJGL.c

Doesn't this mean that it should actually already work by default?


EDIT: My mistake, this is for 2.x!