]> git.sesse.net Git - vlc/commitdiff
Win32: allow affinity changes in OpenGL output for nVidia GPU
authorDavid R Robison <drrobison@openroadsconsulting.com>
Tue, 3 Sep 2013 18:16:52 +0000 (20:16 +0200)
committerJean-Baptiste Kempf <jb@videolan.org>
Tue, 3 Sep 2013 18:20:10 +0000 (20:20 +0200)
This should allow to balance the GPU load across multiple nVidia graphics cards.
This patch introduces a new command line argument that allows the user
to set the nVidia card affinity.

Signed-off-by: Jean-Baptiste Kempf <jb@videolan.org>
modules/video_output/msw/common.h
modules/video_output/msw/glwin32.c

index 0d4ce4b6cbda431e01ae9cc49be763f5a21cc267..8abbac9a55b62c2e266701e6463e77cb270631a2 100644 (file)
@@ -174,6 +174,8 @@ struct vout_display_sys_t
         RGBQUAD    blue;
     };
 #endif
+
+    HDC affinityHDC; // DC for the selected GPU
 };
 
 /*****************************************************************************
index 89360c154adeae10963977f87f30626974cc7a86..8bd0f21887cf9ca6a54910802af2a420a1de5b7f 100644 (file)
@@ -38,6 +38,7 @@
 #   define MONITOR_DEFAULTTONEAREST 2
 #endif
 
+#define GLEW_STATIC
 #include "../opengl.h"
 #include <GL/wglew.h>
 #include "common.h"
 static int  Open (vlc_object_t *);
 static void Close(vlc_object_t *);
 
+#define HW_GPU_AFFINITY_TEXT N_("GPU affinity")
+
 vlc_module_begin()
     set_category(CAT_VIDEO)
     set_subcategory(SUBCAT_VIDEO_VOUT)
     set_shortname("OpenGL")
     set_description(N_("OpenGL video output"))
+
+    add_integer("gpu-affinity", -1, HW_GPU_AFFINITY_TEXT, HW_GPU_AFFINITY_TEXT, true)
+
     set_capability("vout display", 160)
     add_shortcut("glwin32", "opengl")
     set_callbacks(Open, Close)
@@ -70,6 +76,85 @@ static void           Manage (vout_display_t *);
 static void           Swap   (vlc_gl_t *);
 static void          *OurGetProcAddress(vlc_gl_t *, const char *);
 
+/* Create an GPU Affinity DC */
+static void CreateGPUAffinityDC(vout_display_t *vd, UINT nVidiaAffinity) {
+    PIXELFORMATDESCRIPTOR pfd;
+    memset(&pfd, 0, sizeof(pfd));
+    pfd.nSize = sizeof(pfd);
+    pfd.nVersion = 1;
+    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
+    pfd.iPixelType = PFD_TYPE_RGBA;
+    pfd.cColorBits = 24;
+    pfd.cDepthBits = 16;
+    pfd.iLayerType = PFD_MAIN_PLANE;
+
+    /* create a temporary GL context */
+    HDC winDC = GetDC(vd->sys->hvideownd);
+    SetPixelFormat(winDC, ChoosePixelFormat(winDC, &pfd), &pfd);
+    HGLRC hGLRC = wglCreateContext(winDC);
+    wglMakeCurrent(winDC, hGLRC);
+
+    /* Initialize the neccessary function pointers */
+    PFNWGLENUMGPUSNVPROC fncEnumGpusNV = (PFNWGLENUMGPUSNVPROC)wglGetProcAddress("wglEnumGpusNV");
+    PFNWGLCREATEAFFINITYDCNVPROC fncCreateAffinityDCNV = (PFNWGLCREATEAFFINITYDCNVPROC)wglGetProcAddress("wglCreateAffinityDCNV");
+
+    /* delete the temporary GL context */
+    wglDeleteContext(hGLRC);
+
+    /* see if we have the extensions */
+    if (!fncEnumGpusNV || !fncCreateAffinityDCNV) return;
+
+    /* find the graphics card */
+    HGPUNV GpuMask[2];
+    GpuMask[0] = NULL;
+    GpuMask[1] = NULL;
+    HGPUNV hGPU;
+    if (!fncEnumGpusNV(nVidiaAffinity, &hGPU)) return;
+
+    /* make the affinity DC */
+    GpuMask[0] = hGPU;
+    vd->sys->affinityHDC = fncCreateAffinityDCNV(GpuMask);
+    if (vd->sys->affinityHDC == NULL) return;
+    SetPixelFormat(vd->sys->affinityHDC,
+        ChoosePixelFormat(vd->sys->affinityHDC, &pfd), &pfd);
+
+    msg_Dbg( vd, "GPU affinity set to adapter: %d",
+                     nVidiaAffinity );
+}
+
+/* Destroy an GPU Affinity DC */
+static void DestroyGPUAffinityDC(vout_display_t *vd) {
+    if (vd->sys->affinityHDC == NULL) return;
+
+    PIXELFORMATDESCRIPTOR pfd;
+    memset(&pfd, 0, sizeof(pfd));
+    pfd.nSize = sizeof(pfd);
+    pfd.nVersion = 1;
+    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
+    pfd.iPixelType = PFD_TYPE_RGBA;
+    pfd.cColorBits = 24;
+    pfd.cDepthBits = 16;
+    pfd.iLayerType = PFD_MAIN_PLANE;
+
+    /* create a temporary GL context */
+    HDC winDC = GetDC(vd->sys->hvideownd);
+    SetPixelFormat(winDC, ChoosePixelFormat(winDC, &pfd), &pfd);
+    HGLRC hGLRC = wglCreateContext(winDC);
+    wglMakeCurrent(winDC, hGLRC);
+
+    /* Initialize the neccessary function pointers */
+    PFNWGLDELETEDCNVPROC fncDeleteDCNV = (PFNWGLDELETEDCNVPROC)wglGetProcAddress("wglDeleteDCNV");
+
+    /* delete the temporary GL context */
+    wglDeleteContext(hGLRC);
+
+    /* see if we have the extensions */
+    if (!fncDeleteDCNV) return;
+
+    /* delete the affinity DC */
+    fncDeleteDCNV(vd->sys->affinityHDC);
+}
+
 /**
  * It creates an OpenGL vout display.
  */
@@ -89,6 +174,10 @@ static int Open(vlc_object_t *object)
 
     EventThreadUpdateTitle(sys->event, VOUT_TITLE " (OpenGL output)");
 
+    /* process selected GPU affinity */
+    int nVidiaAffinity = var_InheritInteger(vd, "gpu-affinity");
+    if (nVidiaAffinity >= 0) CreateGPUAffinityDC(vd, nVidiaAffinity);
+
     /* */
     sys->hGLDC = GetDC(sys->hvideownd);
 
@@ -105,8 +194,12 @@ static int Open(vlc_object_t *object)
     SetPixelFormat(sys->hGLDC,
                    ChoosePixelFormat(sys->hGLDC, &pfd), &pfd);
 
-    /* Create and enable the render context */
-    sys->hGLRC = wglCreateContext(sys->hGLDC);
+    /*
+     * Create and enable the render context
+     * For GPU affinity, attach the window DC
+     * to the GPU affinity DC
+     */
+    sys->hGLRC = wglCreateContext((sys->affinityHDC != NULL) ? sys->affinityHDC : sys->hGLDC);
     wglMakeCurrent(sys->hGLDC, sys->hGLRC);
 
     const char *extensions = (const char*)glGetString(GL_EXTENSIONS);
@@ -171,6 +264,7 @@ static void Close(vlc_object_t *object)
         wglDeleteContext(sys->hGLRC);
     if (sys->hGLDC)
         ReleaseDC(sys->hvideownd, sys->hGLDC);
+    DestroyGPUAffinityDC(vd);
 
     CommonClean(vd);