xref: /third_party/mesa3d/src/mesa/main/glthread.h (revision bf215546)
1/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#ifndef _GLTHREAD_H
25#define _GLTHREAD_H
26
27/* The size of one batch and the maximum size of one call.
28 *
29 * This should be as low as possible, so that:
30 * - multiple synchronizations within a frame don't slow us down much
31 * - a smaller number of calls per frame can still get decent parallelism
32 * - the memory footprint of the queue is low, and with that comes a lower
33 *   chance of experiencing CPU cache thrashing
34 * but it should be high enough so that u_queue overhead remains negligible.
35 */
36#define MARSHAL_MAX_CMD_SIZE (8 * 1024)
37
38/* The number of batch slots in memory.
39 *
40 * One batch is being executed, one batch is being filled, the rest are
41 * waiting batches. There must be at least 1 slot for a waiting batch,
42 * so the minimum number of batches is 3.
43 */
44#define MARSHAL_MAX_BATCHES 8
45
46/* Special value for glEnableClientState(GL_PRIMITIVE_RESTART_NV). */
47#define VERT_ATTRIB_PRIMITIVE_RESTART_NV -1
48
49#include <inttypes.h>
50#include <stdbool.h>
51#include "util/u_queue.h"
52#include "GL/gl.h"
53#include "compiler/shader_enums.h"
54#include "main/config.h"
55
56#ifdef __cplusplus
57extern "C" {
58#endif
59
60struct gl_context;
61struct gl_buffer_object;
62struct _mesa_HashTable;
63
64struct glthread_attrib_binding {
65   struct gl_buffer_object *buffer; /**< where non-VBO data was uploaded */
66   int offset;                      /**< offset to uploaded non-VBO data */
67   const void *original_pointer;    /**< restore this pointer after the draw */
68};
69
70struct glthread_vao {
71   GLuint Name;
72   GLuint CurrentElementBufferName;
73   GLbitfield UserEnabled; /**< Vertex attribs enabled by the user. */
74   GLbitfield Enabled; /**< UserEnabled with POS vs GENERIC0 aliasing resolved. */
75   GLbitfield BufferEnabled; /**< "Enabled" converted to buffer bindings. */
76   GLbitfield BufferInterleaved; /**< Bitmask of buffers used by multiple attribs. */
77   GLbitfield UserPointerMask; /**< Bitmask of buffer bindings. */
78   GLbitfield NonZeroDivisorMask; /**< Bitmask of buffer bindings. */
79
80   struct {
81      /* Per attrib: */
82      GLuint ElementSize;
83      GLuint RelativeOffset;
84      GLuint BufferIndex; /**< Referring to Attrib[BufferIndex]. */
85
86      /* Per buffer binding: */
87      GLsizei Stride;
88      GLuint Divisor;
89      int EnabledAttribCount; /**< Number of enabled attribs using this buffer. */
90      const void *Pointer;
91   } Attrib[VERT_ATTRIB_MAX];
92};
93
94/** A single batch of commands queued up for execution. */
95struct glthread_batch
96{
97   /** Batch fence for waiting for the execution to finish. */
98   struct util_queue_fence fence;
99
100   /** The worker thread will access the context with this. */
101   struct gl_context *ctx;
102
103   /**
104    * Number of uint64_t elements filled already.
105    * This is 0 when it's being filled because glthread::used holds the real
106    * value temporarily, and glthread::used is copied to this variable when
107    * the batch is submitted.
108    */
109   unsigned used;
110
111   /** Data contained in the command buffer. */
112   uint64_t buffer[MARSHAL_MAX_CMD_SIZE / 8];
113};
114
115struct glthread_client_attrib {
116   struct glthread_vao VAO;
117   GLuint CurrentArrayBufferName;
118   int ClientActiveTexture;
119   GLuint RestartIndex;
120   bool PrimitiveRestart;
121   bool PrimitiveRestartFixedIndex;
122
123   /** Whether this element of the client attrib stack contains saved state. */
124   bool Valid;
125};
126
127/* For glPushAttrib / glPopAttrib. */
128struct glthread_attrib_node {
129   GLbitfield Mask;
130   int ActiveTexture;
131   GLenum MatrixMode;
132   bool CullFace;
133   bool DepthTest;
134};
135
136typedef enum {
137   M_MODELVIEW,
138   M_PROJECTION,
139   M_PROGRAM0,
140   M_PROGRAM_LAST = M_PROGRAM0 + MAX_PROGRAM_MATRICES - 1,
141   M_TEXTURE0,
142   M_TEXTURE_LAST = M_TEXTURE0 + MAX_TEXTURE_UNITS - 1,
143   M_DUMMY, /* used instead of reporting errors */
144   M_NUM_MATRIX_STACKS,
145} gl_matrix_index;
146
147struct glthread_state
148{
149   /** Multithreaded queue. */
150   struct util_queue queue;
151
152   /** This is sent to the driver for framebuffer overlay / HUD. */
153   struct util_queue_monitoring stats;
154
155   /** Whether GLThread is enabled. */
156   bool enabled;
157
158   /** Display lists. */
159   GLenum ListMode; /**< Zero if not inside display list, else list mode. */
160   unsigned ListBase;
161   unsigned ListCallDepth;
162
163   /** For L3 cache pinning. */
164   unsigned pin_thread_counter;
165
166   /** The ring of batches in memory. */
167   struct glthread_batch batches[MARSHAL_MAX_BATCHES];
168
169   /** Pointer to the batch currently being filled. */
170   struct glthread_batch *next_batch;
171
172   /** Index of the last submitted batch. */
173   unsigned last;
174
175   /** Index of the batch being filled and about to be submitted. */
176   unsigned next;
177
178   /** Number of uint64_t elements filled already. */
179   unsigned used;
180
181   /** Upload buffer. */
182   struct gl_buffer_object *upload_buffer;
183   uint8_t *upload_ptr;
184   unsigned upload_offset;
185   int upload_buffer_private_refcount;
186
187   /** Caps. */
188   GLboolean SupportsBufferUploads;
189   GLboolean SupportsNonVBOUploads;
190
191   /** Primitive restart state. */
192   bool PrimitiveRestart;
193   bool PrimitiveRestartFixedIndex;
194   bool _PrimitiveRestart;
195   GLuint RestartIndex;
196   GLuint _RestartIndex[4]; /**< Restart index for index_size = 1,2,4. */
197
198   /** Vertex Array objects tracked by glthread independently of Mesa. */
199   struct _mesa_HashTable *VAOs;
200   struct glthread_vao *CurrentVAO;
201   struct glthread_vao *LastLookedUpVAO;
202   struct glthread_vao DefaultVAO;
203   struct glthread_client_attrib ClientAttribStack[MAX_CLIENT_ATTRIB_STACK_DEPTH];
204   int ClientAttribStackTop;
205   int ClientActiveTexture;
206
207   /** Currently-bound buffer object IDs. */
208   GLuint CurrentArrayBufferName;
209   GLuint CurrentDrawIndirectBufferName;
210   GLuint CurrentPixelPackBufferName;
211   GLuint CurrentPixelUnpackBufferName;
212   GLuint CurrentQueryBufferName;
213
214   /**
215    * The batch index of the last occurence of glLinkProgram or
216    * glDeleteProgram or -1 if there is no such enqueued call.
217    */
218   int LastProgramChangeBatch;
219
220   /**
221    * The batch index of the last occurence of glEndList or
222    * glDeleteLists or -1 if there is no such enqueued call.
223    */
224   int LastDListChangeBatchIndex;
225
226   /** Basic matrix state tracking. */
227   int ActiveTexture;
228   GLenum MatrixMode;
229   gl_matrix_index MatrixIndex;
230   struct glthread_attrib_node AttribStack[MAX_ATTRIB_STACK_DEPTH];
231   int AttribStackDepth;
232   int MatrixStackDepth[M_NUM_MATRIX_STACKS];
233
234   /** Enable states. */
235   bool DepthTest;
236   bool CullFace;
237
238   GLuint CurrentDrawFramebuffer;
239   GLuint CurrentProgram;
240};
241
242void _mesa_glthread_init(struct gl_context *ctx);
243void _mesa_glthread_destroy(struct gl_context *ctx, const char *reason);
244
245void _mesa_glthread_flush_batch(struct gl_context *ctx);
246void _mesa_glthread_finish(struct gl_context *ctx);
247void _mesa_glthread_finish_before(struct gl_context *ctx, const char *func);
248void _mesa_glthread_upload(struct gl_context *ctx, const void *data,
249                           GLsizeiptr size, unsigned *out_offset,
250                           struct gl_buffer_object **out_buffer,
251                           uint8_t **out_ptr);
252void _mesa_glthread_reset_vao(struct glthread_vao *vao);
253void _mesa_error_glthread_safe(struct gl_context *ctx, GLenum error,
254                               bool glthread, const char *format, ...);
255void _mesa_glthread_execute_list(struct gl_context *ctx, GLuint list);
256
257void _mesa_glthread_BindBuffer(struct gl_context *ctx, GLenum target,
258                               GLuint buffer);
259void _mesa_glthread_DeleteBuffers(struct gl_context *ctx, GLsizei n,
260                                  const GLuint *buffers);
261
262void _mesa_glthread_BindVertexArray(struct gl_context *ctx, GLuint id);
263void _mesa_glthread_DeleteVertexArrays(struct gl_context *ctx,
264                                       GLsizei n, const GLuint *ids);
265void _mesa_glthread_GenVertexArrays(struct gl_context *ctx,
266                                    GLsizei n, GLuint *arrays);
267void _mesa_glthread_set_prim_restart(struct gl_context *ctx, GLenum cap,
268                                     bool value);
269void _mesa_glthread_PrimitiveRestartIndex(struct gl_context *ctx, GLuint index);
270void _mesa_glthread_ClientState(struct gl_context *ctx, GLuint *vaobj,
271                                gl_vert_attrib attrib, bool enable);
272void _mesa_glthread_AttribDivisor(struct gl_context *ctx, const GLuint *vaobj,
273                                  gl_vert_attrib attrib, GLuint divisor);
274void _mesa_glthread_AttribPointer(struct gl_context *ctx, gl_vert_attrib attrib,
275                                  GLint size, GLenum type, GLsizei stride,
276                                  const void *pointer);
277void _mesa_glthread_DSAAttribPointer(struct gl_context *ctx, GLuint vao,
278                                     GLuint buffer, gl_vert_attrib attrib,
279                                     GLint size, GLenum type, GLsizei stride,
280                                     GLintptr offset);
281void _mesa_glthread_AttribFormat(struct gl_context *ctx, GLuint attribindex,
282                                 GLint size, GLenum type,  GLuint relativeoffset);
283void _mesa_glthread_DSAAttribFormat(struct gl_context *ctx, GLuint vaobj,
284                                    GLuint attribindex, GLint size, GLenum type,
285                                    GLuint relativeoffset);
286void _mesa_glthread_VertexBuffer(struct gl_context *ctx, GLuint bindingindex,
287                                 GLuint buffer, GLintptr offset, GLsizei stride);
288void _mesa_glthread_DSAVertexBuffer(struct gl_context *ctx, GLuint vaobj,
289                                    GLuint bindingindex, GLuint buffer,
290                                    GLintptr offset, GLsizei stride);
291void _mesa_glthread_DSAVertexBuffers(struct gl_context *ctx, GLuint vaobj,
292                                     GLuint first, GLsizei count,
293                                     const GLuint *buffers,
294                                     const GLintptr *offsets,
295                                     const GLsizei *strides);
296void _mesa_glthread_BindingDivisor(struct gl_context *ctx, GLuint bindingindex,
297                                   GLuint divisor);
298void _mesa_glthread_DSABindingDivisor(struct gl_context *ctx, GLuint vaobj,
299                                      GLuint bindingindex, GLuint divisor);
300void _mesa_glthread_AttribBinding(struct gl_context *ctx, GLuint attribindex,
301                                  GLuint bindingindex);
302void _mesa_glthread_DSAAttribBinding(struct gl_context *ctx, GLuint vaobj,
303                                     GLuint attribindex, GLuint bindingindex);
304void _mesa_glthread_DSAElementBuffer(struct gl_context *ctx, GLuint vaobj,
305                                     GLuint buffer);
306void _mesa_glthread_PushClientAttrib(struct gl_context *ctx, GLbitfield mask,
307                                     bool set_default);
308void _mesa_glthread_PopClientAttrib(struct gl_context *ctx);
309void _mesa_glthread_ClientAttribDefault(struct gl_context *ctx, GLbitfield mask);
310void _mesa_glthread_InterleavedArrays(struct gl_context *ctx, GLenum format,
311                                      GLsizei stride, const GLvoid *pointer);
312void _mesa_glthread_ProgramChanged(struct gl_context *ctx);
313
314#ifdef __cplusplus
315}
316#endif
317
318#endif /* _GLTHREAD_H*/
319