glthread: don't upload for glDraw inside a display list and always sync
[mesa.git] / src / mesa / main / glthread.h
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 struct gl_context;
57 struct gl_buffer_object;
58 struct _mesa_HashTable;
59
60 struct glthread_attrib_binding {
61 struct gl_buffer_object *buffer; /**< where non-VBO data was uploaded */
62 int offset; /**< offset to uploaded non-VBO data */
63 const void *original_pointer; /**< restore this pointer after the draw */
64 };
65
66 struct glthread_vao {
67 GLuint Name;
68 GLuint CurrentElementBufferName;
69 GLbitfield UserEnabled; /**< Vertex attrib arrays enabled by the user. */
70 GLbitfield Enabled; /**< UserEnabled with POS vs GENERIC0 aliasing resolved. */
71 GLbitfield UserPointerMask;
72 GLbitfield NonZeroDivisorMask;
73
74 struct {
75 GLuint ElementSize;
76 GLsizei Stride;
77 GLuint Divisor;
78 const void *Pointer;
79 } Attrib[VERT_ATTRIB_MAX];
80 };
81
82 /** A single batch of commands queued up for execution. */
83 struct glthread_batch
84 {
85 /** Batch fence for waiting for the execution to finish. */
86 struct util_queue_fence fence;
87
88 /** The worker thread will access the context with this. */
89 struct gl_context *ctx;
90
91 /** Amount of data used by batch commands, in bytes. */
92 int used;
93
94 /** Data contained in the command buffer. */
95 #ifdef _MSC_VER
96 __declspec(align(8))
97 #else
98 __attribute__((aligned(8)))
99 #endif
100 uint8_t buffer[MARSHAL_MAX_CMD_SIZE];
101 };
102
103 struct glthread_client_attrib {
104 struct glthread_vao VAO;
105 GLuint CurrentArrayBufferName;
106 int ClientActiveTexture;
107 GLuint RestartIndex;
108 bool PrimitiveRestart;
109 bool PrimitiveRestartFixedIndex;
110
111 /** Whether this element of the client attrib stack contains saved state. */
112 bool Valid;
113 };
114
115 struct glthread_state
116 {
117 /** Multithreaded queue. */
118 struct util_queue queue;
119
120 /** This is sent to the driver for framebuffer overlay / HUD. */
121 struct util_queue_monitoring stats;
122
123 /** Whether GLThread is enabled. */
124 bool enabled;
125
126 /** Whether GLThread is inside a display list generation. */
127 bool inside_dlist;
128
129 /** The ring of batches in memory. */
130 struct glthread_batch batches[MARSHAL_MAX_BATCHES];
131
132 /** Pointer to the batch currently being filled. */
133 struct glthread_batch *next_batch;
134
135 /** Index of the last submitted batch. */
136 unsigned last;
137
138 /** Index of the batch being filled and about to be submitted. */
139 unsigned next;
140
141 /** Upload buffer. */
142 struct gl_buffer_object *upload_buffer;
143 uint8_t *upload_ptr;
144 unsigned upload_offset;
145 int upload_buffer_private_refcount;
146
147 /** Caps. */
148 GLboolean SupportsBufferUploads;
149 GLboolean SupportsNonVBOUploads;
150
151 /** Primitive restart state. */
152 bool PrimitiveRestart;
153 bool PrimitiveRestartFixedIndex;
154 bool _PrimitiveRestart;
155 GLuint RestartIndex;
156 GLuint _RestartIndex[4]; /**< Restart index for index_size = 1,2,4. */
157
158 /** Vertex Array objects tracked by glthread independently of Mesa. */
159 struct _mesa_HashTable *VAOs;
160 struct glthread_vao *CurrentVAO;
161 struct glthread_vao *LastLookedUpVAO;
162 struct glthread_vao DefaultVAO;
163 struct glthread_client_attrib ClientAttribStack[MAX_CLIENT_ATTRIB_STACK_DEPTH];
164 int ClientAttribStackTop;
165 int ClientActiveTexture;
166
167 /** Currently-bound buffer object IDs. */
168 GLuint CurrentArrayBufferName;
169 GLuint CurrentDrawIndirectBufferName;
170 };
171
172 void _mesa_glthread_init(struct gl_context *ctx);
173 void _mesa_glthread_destroy(struct gl_context *ctx);
174
175 void _mesa_glthread_restore_dispatch(struct gl_context *ctx, const char *func);
176 void _mesa_glthread_disable(struct gl_context *ctx, const char *func);
177 void _mesa_glthread_flush_batch(struct gl_context *ctx);
178 void _mesa_glthread_finish(struct gl_context *ctx);
179 void _mesa_glthread_finish_before(struct gl_context *ctx, const char *func);
180 void _mesa_glthread_upload(struct gl_context *ctx, const void *data,
181 GLsizeiptr size, unsigned *out_offset,
182 struct gl_buffer_object **out_buffer,
183 uint8_t **out_ptr);
184 void _mesa_glthread_reset_vao(struct glthread_vao *vao);
185
186 void _mesa_glthread_BindBuffer(struct gl_context *ctx, GLenum target,
187 GLuint buffer);
188 void _mesa_glthread_DeleteBuffers(struct gl_context *ctx, GLsizei n,
189 const GLuint *buffers);
190
191 void _mesa_glthread_BindVertexArray(struct gl_context *ctx, GLuint id);
192 void _mesa_glthread_DeleteVertexArrays(struct gl_context *ctx,
193 GLsizei n, const GLuint *ids);
194 void _mesa_glthread_GenVertexArrays(struct gl_context *ctx,
195 GLsizei n, GLuint *arrays);
196 void _mesa_glthread_set_prim_restart(struct gl_context *ctx, GLenum cap,
197 bool value);
198 void _mesa_glthread_PrimitiveRestartIndex(struct gl_context *ctx, GLuint index);
199 void _mesa_glthread_ClientState(struct gl_context *ctx, GLuint *vaobj,
200 gl_vert_attrib attrib, bool enable);
201 void _mesa_glthread_AttribDivisor(struct gl_context *ctx, const GLuint *vaobj,
202 gl_vert_attrib attrib, GLuint divisor);
203 void _mesa_glthread_AttribPointer(struct gl_context *ctx, gl_vert_attrib attrib,
204 GLint size, GLenum type, GLsizei stride,
205 const void *pointer);
206 void _mesa_glthread_DSAAttribPointer(struct gl_context *ctx, GLuint vao,
207 GLuint buffer, gl_vert_attrib attrib,
208 GLint size, GLenum type, GLsizei stride,
209 GLintptr offset);
210 void _mesa_glthread_PushClientAttrib(struct gl_context *ctx, GLbitfield mask,
211 bool set_default);
212 void _mesa_glthread_PopClientAttrib(struct gl_context *ctx);
213 void _mesa_glthread_ClientAttribDefault(struct gl_context *ctx, GLbitfield mask);
214
215 #endif /* _GLTHREAD_H*/