• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BUFFER_SIZE (8 * 1024)
37 
38 /* We need to leave 1 slot at the end to insert the END marker for unmarshal
39  * calls that look ahead to know where the batch ends.
40  */
41 #define MARSHAL_MAX_CMD_SIZE (MARSHAL_MAX_CMD_BUFFER_SIZE - 8)
42 
43 /* The number of batch slots in memory.
44  *
45  * One batch is being executed, one batch is being filled, the rest are
46  * waiting batches. There must be at least 1 slot for a waiting batch,
47  * so the minimum number of batches is 3.
48  */
49 #define MARSHAL_MAX_BATCHES 8
50 
51 /* Special value for glEnableClientState(GL_PRIMITIVE_RESTART_NV). */
52 #define VERT_ATTRIB_PRIMITIVE_RESTART_NV -1
53 
54 #include <inttypes.h>
55 #include <stdbool.h>
56 #include "util/u_queue.h"
57 #include "compiler/shader_enums.h"
58 #include "main/config.h"
59 #include "main/hash.h"
60 #include "util/glheader.h"
61 
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65 
66 struct gl_context;
67 struct gl_buffer_object;
68 struct _glapi_table;
69 
70 /* Used by both glthread and gl_context. */
71 union gl_vertex_format_user {
72    struct {
73       GLenum16 Type;        /**< datatype: GL_FLOAT, GL_INT, etc */
74       bool Bgra;            /**< true if GL_BGRA, else GL_RGBA */
75       uint8_t Size:5;       /**< components per element (1,2,3,4) */
76       bool Normalized:1;    /**< GL_ARB_vertex_program */
77       bool Integer:1;       /**< Integer-valued? */
78       bool Doubles:1;       /**< double values are not converted to floats */
79    };
80    uint32_t All;
81 };
82 
83 #define MESA_PACK_VFORMAT(type, size, normalized, integer, doubles) \
84    (union gl_vertex_format_user){{ \
85       .Type = MIN2(type, 0xffff), /* 0xffff means invalid value */ \
86       .Bgra = size == GL_BGRA, \
87       .Size = size == GL_BGRA ? 4 : MIN2(size, 5), /* 5 means invalid value */ \
88       .Normalized = normalized, \
89       .Integer = integer, \
90       .Doubles = doubles \
91    }}
92 
93 struct glthread_attrib {
94    /* Per attrib: */
95    uint8_t ElementSize;       /**< max 32 */
96    uint8_t BufferIndex;       /**< Referring to Attrib[BufferIndex]. */
97    uint16_t RelativeOffset;   /**< max 0xffff in Mesa */
98    union gl_vertex_format_user Format;
99 
100    /* Per buffer binding: */
101    GLuint Divisor;
102    int16_t Stride;            /**< max 2048 */
103    int8_t EnabledAttribCount; /**< Number of enabled attribs using this buffer. */
104    const void *Pointer;
105 };
106 
107 struct glthread_vao {
108    GLuint Name;
109    GLuint CurrentElementBufferName;
110    GLbitfield UserEnabled; /**< Vertex attribs enabled by the user. */
111    GLbitfield Enabled; /**< UserEnabled with POS vs GENERIC0 aliasing resolved. */
112    GLbitfield BufferEnabled; /**< "Enabled" converted to buffer bindings. */
113    GLbitfield BufferInterleaved; /**< Bitmask of buffers used by multiple attribs. */
114    GLbitfield UserPointerMask; /**< Bitmask of buffer bindings. */
115    GLbitfield NonNullPointerMask; /**< Bitmask of buffer bindings with non-NULL user pointers. */
116    GLbitfield NonZeroDivisorMask; /**< Bitmask of buffer bindings. */
117 
118    struct glthread_attrib Attrib[VERT_ATTRIB_MAX];
119 };
120 
121 /** A single batch of commands queued up for execution. */
122 struct glthread_batch
123 {
124    /** Batch fence for waiting for the execution to finish. */
125    struct util_queue_fence fence;
126 
127    /** The worker thread will access the context with this. */
128    struct gl_context *ctx;
129 
130    /**
131     * Number of uint64_t elements filled already.
132     * This is 0 when it's being filled because glthread::used holds the real
133     * value temporarily, and glthread::used is copied to this variable when
134     * the batch is submitted.
135     */
136    unsigned used;
137 
138    /** Data contained in the command buffer. */
139    uint64_t buffer[MARSHAL_MAX_CMD_BUFFER_SIZE / 8];
140 };
141 
142 struct glthread_client_attrib {
143    struct glthread_vao VAO;
144    GLuint CurrentArrayBufferName;
145    int ClientActiveTexture;
146    GLuint RestartIndex;
147    bool PrimitiveRestart;
148    bool PrimitiveRestartFixedIndex;
149 
150    /** Whether this element of the client attrib stack contains saved state. */
151    bool Valid;
152 };
153 
154 /* For glPushAttrib / glPopAttrib. */
155 struct glthread_attrib_node {
156    GLbitfield Mask;
157    int ActiveTexture;
158    GLenum16 MatrixMode;
159    bool Blend;
160    bool CullFace;
161    bool DepthTest;
162    bool Lighting;
163    bool PolygonStipple;
164 };
165 
166 typedef enum {
167    M_MODELVIEW,
168    M_PROJECTION,
169    M_PROGRAM0,
170    M_PROGRAM_LAST = M_PROGRAM0 + MAX_PROGRAM_MATRICES - 1,
171    M_TEXTURE0,
172    M_TEXTURE_LAST = M_TEXTURE0 + MAX_TEXTURE_UNITS - 1,
173    M_DUMMY, /* used instead of reporting errors */
174    M_NUM_MATRIX_STACKS,
175 } gl_matrix_index;
176 
177 struct glthread_state
178 {
179    /** Multithreaded queue. */
180    struct util_queue queue;
181 
182    /** This is sent to the driver for framebuffer overlay / HUD. */
183    struct util_queue_monitoring stats;
184 
185    /** Whether GLThread is enabled. */
186    bool enabled;
187    bool inside_begin_end;
188    bool thread_sched_enabled;
189 
190    /** Display lists. */
191    GLenum16 ListMode; /**< Zero if not inside display list, else list mode. */
192    unsigned ListBase;
193    unsigned ListCallDepth;
194 
195    /** For L3 cache pinning. */
196    unsigned pin_thread_counter;
197    unsigned thread_sched_state;
198 
199    /** The ring of batches in memory. */
200    struct glthread_batch batches[MARSHAL_MAX_BATCHES];
201 
202    /** Pointer to the batch currently being filled. */
203    struct glthread_batch *next_batch;
204 
205    /** Index of the last submitted batch. */
206    unsigned last;
207 
208    /** Index of the batch being filled and about to be submitted. */
209    unsigned next;
210 
211    /** Number of uint64_t elements filled already. */
212    unsigned used;
213 
214    /** Upload buffer. */
215    struct gl_buffer_object *upload_buffer;
216    uint8_t *upload_ptr;
217    unsigned upload_offset;
218    int upload_buffer_private_refcount;
219 
220    /** Primitive restart state. */
221    bool PrimitiveRestart;
222    bool PrimitiveRestartFixedIndex;
223    bool _PrimitiveRestart;
224    GLuint RestartIndex;
225    GLuint _RestartIndex[4]; /**< Restart index for index_size = 1,2,4. */
226 
227    /** Vertex Array objects tracked by glthread independently of Mesa. */
228    struct _mesa_HashTable VAOs;
229    struct glthread_vao *CurrentVAO;
230    struct glthread_vao *LastLookedUpVAO;
231    struct glthread_vao DefaultVAO;
232    struct glthread_client_attrib ClientAttribStack[MAX_CLIENT_ATTRIB_STACK_DEPTH];
233    int ClientAttribStackTop;
234    int ClientActiveTexture;
235 
236    /** Currently-bound buffer object IDs. */
237    GLuint CurrentArrayBufferName;
238    GLuint CurrentDrawIndirectBufferName;
239    GLuint CurrentPixelPackBufferName;
240    GLuint CurrentPixelUnpackBufferName;
241    GLuint CurrentQueryBufferName;
242 
243    /**
244     * The batch index of the last occurence of glLinkProgram or
245     * glDeleteProgram or -1 if there is no such enqueued call.
246     */
247    int LastProgramChangeBatch;
248 
249    /**
250     * The batch index of the last occurence of glEndList or
251     * glDeleteLists or -1 if there is no such enqueued call.
252     */
253    int LastDListChangeBatchIndex;
254 
255    /** Basic matrix state tracking. */
256    int ActiveTexture;
257    GLenum16 MatrixMode;
258    gl_matrix_index MatrixIndex;
259    struct glthread_attrib_node AttribStack[MAX_ATTRIB_STACK_DEPTH];
260    int AttribStackDepth;
261    int MatrixStackDepth[M_NUM_MATRIX_STACKS];
262 
263    /** Enable states. */
264    bool Blend;
265    bool DepthTest;
266    bool CullFace;
267    bool DebugOutputSynchronous;
268    bool Lighting;
269    bool PolygonStipple;
270 
271    GLuint CurrentDrawFramebuffer;
272    GLuint CurrentReadFramebuffer;
273    GLuint CurrentProgram;
274 
275    /** The last added call of the given function. */
276    struct marshal_cmd_CallList *LastCallList;
277    struct marshal_cmd_BindBuffer *LastBindBuffer1;
278    struct marshal_cmd_BindBuffer *LastBindBuffer2;
279 
280    /** Global mutex update info. */
281    unsigned GlobalLockUpdateBatchCounter;
282    bool LockGlobalMutexes;
283 };
284 
285 void _mesa_glthread_init(struct gl_context *ctx);
286 void _mesa_glthread_destroy(struct gl_context *ctx);
287 
288 void _mesa_glthread_init_dispatch0(struct gl_context *ctx,
289                                    struct _glapi_table *table);
290 void _mesa_glthread_init_dispatch1(struct gl_context *ctx,
291                                    struct _glapi_table *table);
292 void _mesa_glthread_init_dispatch2(struct gl_context *ctx,
293                                    struct _glapi_table *table);
294 void _mesa_glthread_init_dispatch3(struct gl_context *ctx,
295                                    struct _glapi_table *table);
296 void _mesa_glthread_init_dispatch4(struct gl_context *ctx,
297                                    struct _glapi_table *table);
298 void _mesa_glthread_init_dispatch5(struct gl_context *ctx,
299                                    struct _glapi_table *table);
300 void _mesa_glthread_init_dispatch6(struct gl_context *ctx,
301                                    struct _glapi_table *table);
302 void _mesa_glthread_init_dispatch7(struct gl_context *ctx,
303                                    struct _glapi_table *table);
304 
305 void _mesa_glthread_enable(struct gl_context *ctx);
306 void _mesa_glthread_disable(struct gl_context *ctx);
307 void _mesa_glthread_flush_batch(struct gl_context *ctx);
308 void _mesa_glthread_finish(struct gl_context *ctx);
309 void _mesa_glthread_finish_before(struct gl_context *ctx, const char *func);
310 bool _mesa_glthread_invalidate_zsbuf(struct gl_context *ctx);
311 void _mesa_glthread_release_upload_buffer(struct gl_context *ctx);
312 void _mesa_glthread_upload(struct gl_context *ctx, const void *data,
313                            GLsizeiptr size, unsigned *out_offset,
314                            struct gl_buffer_object **out_buffer,
315                            uint8_t **out_ptr,
316                            unsigned start_offset);
317 void _mesa_glthread_reset_vao(struct glthread_vao *vao);
318 void _mesa_error_glthread_safe(struct gl_context *ctx, GLenum error,
319                                bool glthread, const char *format, ...);
320 void _mesa_glthread_execute_list(struct gl_context *ctx, GLuint list);
321 
322 void _mesa_glthread_DeleteBuffers(struct gl_context *ctx, GLsizei n,
323                                   const GLuint *buffers);
324 
325 void _mesa_glthread_BindVertexArray(struct gl_context *ctx, GLuint id);
326 void _mesa_glthread_DeleteVertexArrays(struct gl_context *ctx,
327                                        GLsizei n, const GLuint *ids);
328 void _mesa_glthread_GenVertexArrays(struct gl_context *ctx,
329                                     GLsizei n, GLuint *arrays);
330 void _mesa_glthread_set_prim_restart(struct gl_context *ctx, GLenum cap,
331                                      bool value);
332 void _mesa_glthread_PrimitiveRestartIndex(struct gl_context *ctx, GLuint index);
333 void _mesa_glthread_ClientState(struct gl_context *ctx, GLuint *vaobj,
334                                 gl_vert_attrib attrib, bool enable);
335 void _mesa_glthread_AttribDivisor(struct gl_context *ctx, const GLuint *vaobj,
336                                   gl_vert_attrib attrib, GLuint divisor);
337 void _mesa_glthread_AttribPointer(struct gl_context *ctx, gl_vert_attrib attrib,
338                                   union gl_vertex_format_user format,
339                                   GLsizei stride, const void *pointer);
340 void _mesa_glthread_DSAAttribPointer(struct gl_context *ctx, GLuint vao,
341                                      GLuint buffer, gl_vert_attrib attrib,
342                                      union gl_vertex_format_user format,
343                                      GLsizei stride, GLintptr offset);
344 void _mesa_glthread_AttribFormat(struct gl_context *ctx, GLuint attribindex,
345                                  union gl_vertex_format_user format,
346                                  GLuint relativeoffset);
347 void _mesa_glthread_DSAAttribFormat(struct gl_context *ctx, GLuint vaobj,
348                                     GLuint attribindex,
349                                     union gl_vertex_format_user format,
350                                     GLuint relativeoffset);
351 void _mesa_glthread_VertexBuffer(struct gl_context *ctx, GLuint bindingindex,
352                                  GLuint buffer, GLintptr offset, GLsizei stride);
353 void _mesa_glthread_DSAVertexBuffer(struct gl_context *ctx, GLuint vaobj,
354                                     GLuint bindingindex, GLuint buffer,
355                                     GLintptr offset, GLsizei stride);
356 void _mesa_glthread_DSAVertexBuffers(struct gl_context *ctx, GLuint vaobj,
357                                      GLuint first, GLsizei count,
358                                      const GLuint *buffers,
359                                      const GLintptr *offsets,
360                                      const GLsizei *strides);
361 void _mesa_glthread_BindingDivisor(struct gl_context *ctx, GLuint bindingindex,
362                                    GLuint divisor);
363 void _mesa_glthread_DSABindingDivisor(struct gl_context *ctx, GLuint vaobj,
364                                       GLuint bindingindex, GLuint divisor);
365 void _mesa_glthread_AttribBinding(struct gl_context *ctx, GLuint attribindex,
366                                   GLuint bindingindex);
367 void _mesa_glthread_DSAAttribBinding(struct gl_context *ctx, GLuint vaobj,
368                                      GLuint attribindex, GLuint bindingindex);
369 void _mesa_glthread_DSAElementBuffer(struct gl_context *ctx, GLuint vaobj,
370                                      GLuint buffer);
371 void _mesa_glthread_PushClientAttrib(struct gl_context *ctx, GLbitfield mask,
372                                      bool set_default);
373 void _mesa_glthread_PopClientAttrib(struct gl_context *ctx);
374 void _mesa_glthread_ClientAttribDefault(struct gl_context *ctx, GLbitfield mask);
375 void _mesa_glthread_InterleavedArrays(struct gl_context *ctx, GLenum format,
376                                       GLsizei stride, const GLvoid *pointer);
377 void _mesa_glthread_ProgramChanged(struct gl_context *ctx);
378 void _mesa_glthread_UnrollDrawElements(struct gl_context *ctx,
379                                        GLenum mode, GLsizei count, GLenum type,
380                                        const GLvoid *indices, GLint basevertex);
381 void _mesa_glthread_unbind_uploaded_vbos(struct gl_context *ctx);
382 
383 #ifdef __cplusplus
384 }
385 #endif
386 
387 #endif /* _GLTHREAD_H*/
388