radeonsi: set a better NUM_PATCHES hard limit
[mesa.git] / src / gallium / drivers / swr / swr_fence.cpp
index f97ea22151bdd859f2247aab75f0acd6ed924458..b05ac8cec02fa5a3a8b922b350675c0145fd6c05 100644 (file)
 
 #include "pipe/p_screen.h"
 #include "util/u_memory.h"
-#include "os/os_time.h"
+#include "util/os_time.h"
 
 #include "swr_context.h"
 #include "swr_screen.h"
 #include "swr_fence.h"
 
+#ifdef __APPLE__
+#include <sched.h>
+#endif
+
 #if defined(PIPE_CC_MSVC) // portable thread yield
-   #define sched_yield SwitchToThread  
+   #define sched_yield SwitchToThread
 #endif
+
 /*
  * Fence callback, called by back-end thread on completion of all rendering up
  * to SwrSync call.
  */
 static void
-swr_sync_cb(UINT64 userData, UINT64 userData2, UINT64 userData3)
+swr_fence_cb(uint64_t userData, uint64_t userData2, uint64_t userData3)
 {
    struct swr_fence *fence = (struct swr_fence *)userData;
 
-   fence->read = fence->write;
+   /* Complete all work attached to the fence */
+   swr_fence_do_work(fence);
+
+   /* Correct value is in SwrSync data, and not the fence write field. */
+   fence->read = userData2;
 }
 
 /*
@@ -53,7 +62,8 @@ swr_fence_submit(struct swr_context *ctx, struct pipe_fence_handle *fh)
    struct swr_fence *fence = swr_fence(fh);
 
    fence->write++;
-   SwrSync(ctx->swrContext, swr_sync_cb, (UINT64)fence, 0, 0);
+   fence->pending = TRUE;
+   ctx->api.pfnSwrSync(ctx->swrContext, swr_fence_cb, (uint64_t)fence, fence->write, 0);
 }
 
 /*
@@ -67,9 +77,9 @@ swr_fence_create()
    if (!fence)
       return NULL;
 
-   memset(fence, 0, sizeof(*fence));
    pipe_reference_init(&fence->reference, 1);
    fence->id = fence_id++;
+   fence->work.tail = &fence->work.head;
 
    return (struct pipe_fence_handle *)fence;
 }
@@ -78,6 +88,8 @@ swr_fence_create()
 static void
 swr_fence_destroy(struct swr_fence *fence)
 {
+   /* Complete any work left if fence was not submitted */
+   swr_fence_do_work(fence);
    FREE(fence);
 }
 
@@ -99,23 +111,27 @@ swr_fence_reference(struct pipe_screen *screen,
       old = NULL;
    }
 
-   if (pipe_reference(&old->reference, &fence->reference))
+   if (pipe_reference(&old->reference, &fence->reference)) {
+      swr_fence_finish(screen, NULL, (struct pipe_fence_handle *) old, 0);
       swr_fence_destroy(old);
+   }
 }
 
+
 /*
  * Wait for the fence to finish.
  */
 boolean
 swr_fence_finish(struct pipe_screen *screen,
+                 struct pipe_context *ctx,
                  struct pipe_fence_handle *fence_handle,
                  uint64_t timeout)
 {
-   struct swr_fence *fence = swr_fence(fence_handle);
-
-   while (!swr_is_fence_done(fence))
+   while (!swr_is_fence_done(fence_handle))
       sched_yield();
 
+   swr_fence(fence_handle)->pending = FALSE;
+
    return TRUE;
 }
 
@@ -132,12 +148,10 @@ swr_fence_init(struct pipe_screen *p_screen)
 {
    p_screen->fence_reference = swr_fence_reference;
    p_screen->fence_finish = swr_fence_finish;
-
    p_screen->get_timestamp = swr_get_timestamp;
 
-   /*
-    * Create persistant "flush" fence, submitted when swr_flush is called.
-    */
+   /* Create persistant StoreTiles "flush" fence, used to signal completion
+    * of flushing tile state back to resource texture, via StoreTiles. */
    struct swr_screen *screen = swr_screen(p_screen);
    screen->flush_fence = swr_fence_create();
 }