progs/wgl: Get wglShareLists working in wglthreads.
authorJosé Fonseca <jfonseca@vmware.com>
Wed, 17 Jun 2009 13:48:25 +0000 (14:48 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Wed, 17 Jun 2009 14:23:25 +0000 (15:23 +0100)
wglShareLists is a little picky -- it seems to check if it has exclusive
access to a lock, and fails if it doesn't.

This allows the texture to be shared with all windows.

progs/wgl/wglthreads/wglthreads.c

index 2f67dd670f25aa53dc9cca54fd044ec0bb563cd2..405b5db884239942fff7a8e4d167636094cce11f 100644 (file)
@@ -331,7 +331,9 @@ draw_loop(struct winthread *wt)
 
       if (Locking)
          EnterCriticalSection(&Mutex);
+
       SwapBuffers(wt->hDC);
+
       if (Locking)
          LeaveCriticalSection(&Mutex);
 
@@ -481,7 +483,8 @@ create_window(struct winthread *wt, HGLRC shareCtx)
    }
 
    if (shareCtx) {
-      wglShareLists(shareCtx, ctx);
+      if(!wglShareLists(shareCtx, ctx))
+         Error("Couldn't share WGL context lists");
    }
 
    /* save the info for this window/context */
@@ -504,10 +507,22 @@ ThreadProc(void *p)
    struct winthread *wt = (struct winthread *) p;
    HGLRC share;
 
+   /* Wait for first thread context */
+   if(Texture && wt->Index > 0) {
+      WaitForSingleObject(WinThreads[0].hEventInitialised, INFINITE);
+      share = WinThreads[0].Context;
+   }
+   else
+      share = 0;
+
    share = (Texture && wt->Index > 0) ? WinThreads[0].Context : 0;
    create_window(wt, share);
    SetEvent(wt->hEventInitialised);
 
+   /* Wait for all threads to initialize otherwise wglShareLists will fail */
+   if(wt->Index < NumWinThreads - 1)
+      WaitForSingleObject(WinThreads[NumWinThreads - 1].hEventInitialised, INFINITE);
+
    draw_loop(wt);
    return 0;
 }
@@ -591,13 +606,17 @@ main(int argc, char *argv[])
 
       printf("wglthreads: creating threads\n");
 
-      /* Create the threads */
+      /* Create the events */
       for (i = 0; i < NumWinThreads; i++) {
-         DWORD id;
-
          WinThreads[i].Index = i;
          WinThreads[i].hEventInitialised = CreateEvent(NULL, TRUE, FALSE, NULL);
          WinThreads[i].hEventRedraw = CreateEvent(NULL, FALSE, FALSE, NULL);
+      }
+
+      /* Create the threads */
+      for (i = 0; i < NumWinThreads; i++) {
+         DWORD id;
+
          WinThreads[i].Thread = CreateThread(NULL,
                                              0,
                                              ThreadProc,
@@ -606,8 +625,6 @@ main(int argc, char *argv[])
                                              &id);
          printf("wglthreads: Created thread %p\n", (void *) WinThreads[i].Thread);
 
-         WaitForSingleObject(WinThreads[i].hEventInitialised, INFINITE);
-
          threads[i] = WinThreads[i].Thread;
       }