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 57 extern "C" { 58 #endif 59 60 struct gl_context; 61 struct gl_buffer_object; 62 struct _mesa_HashTable; 63 64 struct 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 70 struct 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. */ 95 struct 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 115 struct 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. */ 128 struct glthread_attrib_node { 129 GLbitfield Mask; 130 int ActiveTexture; 131 GLenum MatrixMode; 132 }; 133 134 typedef enum { 135 M_MODELVIEW, 136 M_PROJECTION, 137 M_PROGRAM0, 138 M_PROGRAM_LAST = M_PROGRAM0 + MAX_PROGRAM_MATRICES - 1, 139 M_TEXTURE0, 140 M_TEXTURE_LAST = M_TEXTURE0 + MAX_TEXTURE_UNITS - 1, 141 M_DUMMY, /* used instead of reporting errors */ 142 M_NUM_MATRIX_STACKS, 143 } gl_matrix_index; 144 145 struct glthread_state 146 { 147 /** Multithreaded queue. */ 148 struct util_queue queue; 149 150 /** This is sent to the driver for framebuffer overlay / HUD. */ 151 struct util_queue_monitoring stats; 152 153 /** Whether GLThread is enabled. */ 154 bool enabled; 155 156 /** Display lists. */ 157 GLenum ListMode; /**< Zero if not inside display list, else list mode. */ 158 unsigned ListBase; 159 unsigned ListCallDepth; 160 161 /** For L3 cache pinning. */ 162 unsigned pin_thread_counter; 163 164 /** The ring of batches in memory. */ 165 struct glthread_batch batches[MARSHAL_MAX_BATCHES]; 166 167 /** Pointer to the batch currently being filled. */ 168 struct glthread_batch *next_batch; 169 170 /** Index of the last submitted batch. */ 171 unsigned last; 172 173 /** Index of the batch being filled and about to be submitted. */ 174 unsigned next; 175 176 /** Number of uint64_t elements filled already. */ 177 unsigned used; 178 179 /** Upload buffer. */ 180 struct gl_buffer_object *upload_buffer; 181 uint8_t *upload_ptr; 182 unsigned upload_offset; 183 int upload_buffer_private_refcount; 184 185 /** Caps. */ 186 GLboolean SupportsBufferUploads; 187 GLboolean SupportsNonVBOUploads; 188 189 /** Primitive restart state. */ 190 bool PrimitiveRestart; 191 bool PrimitiveRestartFixedIndex; 192 bool _PrimitiveRestart; 193 GLuint RestartIndex; 194 GLuint _RestartIndex[4]; /**< Restart index for index_size = 1,2,4. */ 195 196 /** Vertex Array objects tracked by glthread independently of Mesa. */ 197 struct _mesa_HashTable *VAOs; 198 struct glthread_vao *CurrentVAO; 199 struct glthread_vao *LastLookedUpVAO; 200 struct glthread_vao DefaultVAO; 201 struct glthread_client_attrib ClientAttribStack[MAX_CLIENT_ATTRIB_STACK_DEPTH]; 202 int ClientAttribStackTop; 203 int ClientActiveTexture; 204 205 /** Currently-bound buffer object IDs. */ 206 GLuint CurrentArrayBufferName; 207 GLuint CurrentDrawIndirectBufferName; 208 GLuint CurrentPixelPackBufferName; 209 GLuint CurrentPixelUnpackBufferName; 210 211 /** 212 * The batch index of the last occurence of glLinkProgram or 213 * glDeleteProgram or -1 if there is no such enqueued call. 214 */ 215 int LastProgramChangeBatch; 216 217 /** 218 * The batch index of the last occurence of glEndList or 219 * glDeleteLists or -1 if there is no such enqueued call. 220 */ 221 int LastDListChangeBatchIndex; 222 223 /** Basic matrix state tracking. */ 224 int ActiveTexture; 225 GLenum MatrixMode; 226 gl_matrix_index MatrixIndex; 227 struct glthread_attrib_node AttribStack[MAX_ATTRIB_STACK_DEPTH]; 228 int AttribStackDepth; 229 int MatrixStackDepth[M_NUM_MATRIX_STACKS]; 230 }; 231 232 void _mesa_glthread_init(struct gl_context *ctx); 233 void _mesa_glthread_destroy(struct gl_context *ctx); 234 235 void _mesa_glthread_restore_dispatch(struct gl_context *ctx, const char *func); 236 void _mesa_glthread_disable(struct gl_context *ctx, const char *func); 237 void _mesa_glthread_flush_batch(struct gl_context *ctx); 238 void _mesa_glthread_finish(struct gl_context *ctx); 239 void _mesa_glthread_finish_before(struct gl_context *ctx, const char *func); 240 void _mesa_glthread_upload(struct gl_context *ctx, const void *data, 241 GLsizeiptr size, unsigned *out_offset, 242 struct gl_buffer_object **out_buffer, 243 uint8_t **out_ptr); 244 void _mesa_glthread_reset_vao(struct glthread_vao *vao); 245 void _mesa_error_glthread_safe(struct gl_context *ctx, GLenum error, 246 bool glthread, const char *format, ...); 247 void _mesa_glthread_execute_list(struct gl_context *ctx, GLuint list); 248 249 void _mesa_glthread_BindBuffer(struct gl_context *ctx, GLenum target, 250 GLuint buffer); 251 void _mesa_glthread_DeleteBuffers(struct gl_context *ctx, GLsizei n, 252 const GLuint *buffers); 253 254 void _mesa_glthread_BindVertexArray(struct gl_context *ctx, GLuint id); 255 void _mesa_glthread_DeleteVertexArrays(struct gl_context *ctx, 256 GLsizei n, const GLuint *ids); 257 void _mesa_glthread_GenVertexArrays(struct gl_context *ctx, 258 GLsizei n, GLuint *arrays); 259 void _mesa_glthread_set_prim_restart(struct gl_context *ctx, GLenum cap, 260 bool value); 261 void _mesa_glthread_PrimitiveRestartIndex(struct gl_context *ctx, GLuint index); 262 void _mesa_glthread_ClientState(struct gl_context *ctx, GLuint *vaobj, 263 gl_vert_attrib attrib, bool enable); 264 void _mesa_glthread_AttribDivisor(struct gl_context *ctx, const GLuint *vaobj, 265 gl_vert_attrib attrib, GLuint divisor); 266 void _mesa_glthread_AttribPointer(struct gl_context *ctx, gl_vert_attrib attrib, 267 GLint size, GLenum type, GLsizei stride, 268 const void *pointer); 269 void _mesa_glthread_DSAAttribPointer(struct gl_context *ctx, GLuint vao, 270 GLuint buffer, gl_vert_attrib attrib, 271 GLint size, GLenum type, GLsizei stride, 272 GLintptr offset); 273 void _mesa_glthread_AttribFormat(struct gl_context *ctx, GLuint attribindex, 274 GLint size, GLenum type, GLuint relativeoffset); 275 void _mesa_glthread_DSAAttribFormat(struct gl_context *ctx, GLuint vaobj, 276 GLuint attribindex, GLint size, GLenum type, 277 GLuint relativeoffset); 278 void _mesa_glthread_VertexBuffer(struct gl_context *ctx, GLuint bindingindex, 279 GLuint buffer, GLintptr offset, GLsizei stride); 280 void _mesa_glthread_DSAVertexBuffer(struct gl_context *ctx, GLuint vaobj, 281 GLuint bindingindex, GLuint buffer, 282 GLintptr offset, GLsizei stride); 283 void _mesa_glthread_DSAVertexBuffers(struct gl_context *ctx, GLuint vaobj, 284 GLuint first, GLsizei count, 285 const GLuint *buffers, 286 const GLintptr *offsets, 287 const GLsizei *strides); 288 void _mesa_glthread_BindingDivisor(struct gl_context *ctx, GLuint bindingindex, 289 GLuint divisor); 290 void _mesa_glthread_DSABindingDivisor(struct gl_context *ctx, GLuint vaobj, 291 GLuint bindingindex, GLuint divisor); 292 void _mesa_glthread_AttribBinding(struct gl_context *ctx, GLuint attribindex, 293 GLuint bindingindex); 294 void _mesa_glthread_DSAAttribBinding(struct gl_context *ctx, GLuint vaobj, 295 GLuint attribindex, GLuint bindingindex); 296 void _mesa_glthread_DSAElementBuffer(struct gl_context *ctx, GLuint vaobj, 297 GLuint buffer); 298 void _mesa_glthread_PushClientAttrib(struct gl_context *ctx, GLbitfield mask, 299 bool set_default); 300 void _mesa_glthread_PopClientAttrib(struct gl_context *ctx); 301 void _mesa_glthread_ClientAttribDefault(struct gl_context *ctx, GLbitfield mask); 302 void _mesa_glthread_InterleavedArrays(struct gl_context *ctx, GLenum format, 303 GLsizei stride, const GLvoid *pointer); 304 void _mesa_glthread_ProgramChanged(struct gl_context *ctx); 305 306 #ifdef __cplusplus 307 } 308 #endif 309 310 #endif /* _GLTHREAD_H*/ 311