glthread: don't upload for glDraw inside a display list and always sync
[mesa.git] / src / mesa / main / glthread.h
index 50c1db2548872858c7f705f5246a8c1f4c1cf8a7..34a5443a6daff75f3affb53b0fe1cd3835461df3 100644 (file)
 #ifndef _GLTHREAD_H
 #define _GLTHREAD_H
 
-#include "main/mtypes.h"
+/* The size of one batch and the maximum size of one call.
+ *
+ * This should be as low as possible, so that:
+ * - multiple synchronizations within a frame don't slow us down much
+ * - a smaller number of calls per frame can still get decent parallelism
+ * - the memory footprint of the queue is low, and with that comes a lower
+ *   chance of experiencing CPU cache thrashing
+ * but it should be high enough so that u_queue overhead remains negligible.
+ */
+#define MARSHAL_MAX_CMD_SIZE (8 * 1024)
 
-/* Command size is a number of bytes stored in a short. */
-#define MARSHAL_MAX_CMD_SIZE 65535
+/* The number of batch slots in memory.
+ *
+ * One batch is being executed, one batch is being filled, the rest are
+ * waiting batches. There must be at least 1 slot for a waiting batch,
+ * so the minimum number of batches is 3.
+ */
+#define MARSHAL_MAX_BATCHES 8
 
-#ifdef HAVE_PTHREAD
+/* Special value for glEnableClientState(GL_PRIMITIVE_RESTART_NV). */
+#define VERT_ATTRIB_PRIMITIVE_RESTART_NV -1
 
 #include <inttypes.h>
 #include <stdbool.h>
-#include <pthread.h>
-
-enum marshal_dispatch_cmd_id;
+#include "util/u_queue.h"
+#include "GL/gl.h"
+#include "compiler/shader_enums.h"
+#include "main/config.h"
+
+struct gl_context;
+struct gl_buffer_object;
+struct _mesa_HashTable;
+
+struct glthread_attrib_binding {
+   struct gl_buffer_object *buffer; /**< where non-VBO data was uploaded */
+   int offset;                      /**< offset to uploaded non-VBO data */
+   const void *original_pointer;    /**< restore this pointer after the draw */
+};
 
-struct glthread_state
-{
-   /** The worker thread that asynchronously processes our GL commands. */
-   pthread_t thread;
-
-   /**
-    * Mutex used for synchronizing between the main thread and the worker
-    * thread.
-    */
-   pthread_mutex_t mutex;
-
-   /** Condvar used for waking the worker thread. */
-   pthread_cond_t new_work;
-
-   /** Condvar used for waking the main thread. */
-   pthread_cond_t work_done;
-
-   /** Used to tell the worker thread to quit */
-   bool shutdown;
-
-   /** Indicates that the worker thread is currently processing a batch */
-   bool busy;
-
-   /**
-    * Singly-linked list of command batches that are awaiting execution by
-    * a thread pool task.  NULL if empty.
-    */
-   struct glthread_batch *batch_queue;
-
-   /**
-    * Tail pointer for appending batches to the end of batch_queue.  If the
-    * queue is empty, this points to batch_queue.
-    */
-   struct glthread_batch **batch_queue_tail;
-
-   /**
-    * Batch containing commands that are being prepared for insertion into
-    * batch_queue.  NULL if there are no such commands.
-    *
-    * Since this is only used by the main thread, it doesn't need the mutex to
-    * be accessed.
-    */
-   struct glthread_batch *batch;
-
-   /**
-    * Tracks on the main thread side whether the current vertex array binding
-    * is in a VBO.
-    */
-   bool vertex_array_is_vbo;
-
-   /**
-    * Tracks on the main thread side whether the current element array (index
-    * buffer) binding is in a VBO.
-    */
-   bool element_array_is_vbo;
+struct glthread_vao {
+   GLuint Name;
+   GLuint CurrentElementBufferName;
+   GLbitfield UserEnabled; /**< Vertex attrib arrays enabled by the user. */
+   GLbitfield Enabled; /**< UserEnabled with POS vs GENERIC0 aliasing resolved. */
+   GLbitfield UserPointerMask;
+   GLbitfield NonZeroDivisorMask;
+
+   struct {
+      GLuint ElementSize;
+      GLsizei Stride;
+      GLuint Divisor;
+      const void *Pointer;
+   } Attrib[VERT_ATTRIB_MAX];
 };
 
-/**
- * A single batch of commands queued up for later execution by a thread pool
- * task.
- */
+/** A single batch of commands queued up for execution. */
 struct glthread_batch
 {
-   /**
-    * Next batch of commands to execute after this batch, or NULL if this is
-    * the last set of commands queued.  Protected by ctx->Marshal.Mutex.
-    */
-   struct glthread_batch *next;
-
-   /**
-    * Amount of data used by batch commands, in bytes.
-    */
-   size_t used;
-
-   /**
-    * Data contained in the command buffer.
-    */
-   uint8_t buffer[MARSHAL_MAX_CMD_SIZE];
-};
+   /** Batch fence for waiting for the execution to finish. */
+   struct util_queue_fence fence;
 
-void _mesa_glthread_init(struct gl_context *ctx);
-void _mesa_glthread_destroy(struct gl_context *ctx);
+   /** The worker thread will access the context with this. */
+   struct gl_context *ctx;
 
-void _mesa_glthread_restore_dispatch(struct gl_context *ctx);
-void _mesa_glthread_flush_batch(struct gl_context *ctx);
-void _mesa_glthread_finish(struct gl_context *ctx);
+   /** Amount of data used by batch commands, in bytes. */
+   int used;
 
-#else /* HAVE_PTHREAD */
+   /** Data contained in the command buffer. */
+#ifdef _MSC_VER
+   __declspec(align(8))
+#else
+   __attribute__((aligned(8)))
+#endif
+   uint8_t buffer[MARSHAL_MAX_CMD_SIZE];
+};
 
-static inline void
-_mesa_glthread_init(struct gl_context *ctx)
-{
-}
+struct glthread_client_attrib {
+   struct glthread_vao VAO;
+   GLuint CurrentArrayBufferName;
+   int ClientActiveTexture;
+   GLuint RestartIndex;
+   bool PrimitiveRestart;
+   bool PrimitiveRestartFixedIndex;
 
-static inline void
-_mesa_glthread_destroy(struct gl_context *ctx)
-{
-}
+   /** Whether this element of the client attrib stack contains saved state. */
+   bool Valid;
+};
 
-static inline void
-_mesa_glthread_finish(struct gl_context *ctx)
+struct glthread_state
 {
-}
+   /** Multithreaded queue. */
+   struct util_queue queue;
+
+   /** This is sent to the driver for framebuffer overlay / HUD. */
+   struct util_queue_monitoring stats;
+
+   /** Whether GLThread is enabled. */
+   bool enabled;
+
+   /** Whether GLThread is inside a display list generation. */
+   bool inside_dlist;
+
+   /** The ring of batches in memory. */
+   struct glthread_batch batches[MARSHAL_MAX_BATCHES];
+
+   /** Pointer to the batch currently being filled. */
+   struct glthread_batch *next_batch;
+
+   /** Index of the last submitted batch. */
+   unsigned last;
+
+   /** Index of the batch being filled and about to be submitted. */
+   unsigned next;
+
+   /** Upload buffer. */
+   struct gl_buffer_object *upload_buffer;
+   uint8_t *upload_ptr;
+   unsigned upload_offset;
+   int upload_buffer_private_refcount;
+
+   /** Caps. */
+   GLboolean SupportsBufferUploads;
+   GLboolean SupportsNonVBOUploads;
+
+   /** Primitive restart state. */
+   bool PrimitiveRestart;
+   bool PrimitiveRestartFixedIndex;
+   bool _PrimitiveRestart;
+   GLuint RestartIndex;
+   GLuint _RestartIndex[4]; /**< Restart index for index_size = 1,2,4. */
+
+   /** Vertex Array objects tracked by glthread independently of Mesa. */
+   struct _mesa_HashTable *VAOs;
+   struct glthread_vao *CurrentVAO;
+   struct glthread_vao *LastLookedUpVAO;
+   struct glthread_vao DefaultVAO;
+   struct glthread_client_attrib ClientAttribStack[MAX_CLIENT_ATTRIB_STACK_DEPTH];
+   int ClientAttribStackTop;
+   int ClientActiveTexture;
+
+   /** Currently-bound buffer object IDs. */
+   GLuint CurrentArrayBufferName;
+   GLuint CurrentDrawIndirectBufferName;
+};
 
-static inline void
-_mesa_glthread_restore_dispatch(struct gl_context *ctx)
-{
-}
+void _mesa_glthread_init(struct gl_context *ctx);
+void _mesa_glthread_destroy(struct gl_context *ctx);
 
-static inline void
-_mesa_glthread_flush_batch(struct gl_context *ctx)
-{
-}
+void _mesa_glthread_restore_dispatch(struct gl_context *ctx, const char *func);
+void _mesa_glthread_disable(struct gl_context *ctx, const char *func);
+void _mesa_glthread_flush_batch(struct gl_context *ctx);
+void _mesa_glthread_finish(struct gl_context *ctx);
+void _mesa_glthread_finish_before(struct gl_context *ctx, const char *func);
+void _mesa_glthread_upload(struct gl_context *ctx, const void *data,
+                           GLsizeiptr size, unsigned *out_offset,
+                           struct gl_buffer_object **out_buffer,
+                           uint8_t **out_ptr);
+void _mesa_glthread_reset_vao(struct glthread_vao *vao);
+
+void _mesa_glthread_BindBuffer(struct gl_context *ctx, GLenum target,
+                               GLuint buffer);
+void _mesa_glthread_DeleteBuffers(struct gl_context *ctx, GLsizei n,
+                                  const GLuint *buffers);
+
+void _mesa_glthread_BindVertexArray(struct gl_context *ctx, GLuint id);
+void _mesa_glthread_DeleteVertexArrays(struct gl_context *ctx,
+                                       GLsizei n, const GLuint *ids);
+void _mesa_glthread_GenVertexArrays(struct gl_context *ctx,
+                                    GLsizei n, GLuint *arrays);
+void _mesa_glthread_set_prim_restart(struct gl_context *ctx, GLenum cap,
+                                     bool value);
+void _mesa_glthread_PrimitiveRestartIndex(struct gl_context *ctx, GLuint index);
+void _mesa_glthread_ClientState(struct gl_context *ctx, GLuint *vaobj,
+                                gl_vert_attrib attrib, bool enable);
+void _mesa_glthread_AttribDivisor(struct gl_context *ctx, const GLuint *vaobj,
+                                  gl_vert_attrib attrib, GLuint divisor);
+void _mesa_glthread_AttribPointer(struct gl_context *ctx, gl_vert_attrib attrib,
+                                  GLint size, GLenum type, GLsizei stride,
+                                  const void *pointer);
+void _mesa_glthread_DSAAttribPointer(struct gl_context *ctx, GLuint vao,
+                                     GLuint buffer, gl_vert_attrib attrib,
+                                     GLint size, GLenum type, GLsizei stride,
+                                     GLintptr offset);
+void _mesa_glthread_PushClientAttrib(struct gl_context *ctx, GLbitfield mask,
+                                     bool set_default);
+void _mesa_glthread_PopClientAttrib(struct gl_context *ctx);
+void _mesa_glthread_ClientAttribDefault(struct gl_context *ctx, GLbitfield mask);
 
-#endif /* !HAVE_PTHREAD */
 #endif /* _GLTHREAD_H*/