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