92cac5ee401c32f57c16dbf78daff6f705aaa8f1
[mesa.git] / src / mesa / main / glthread.c
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 /** @file glthread.c
25 *
26 * Support functions for the glthread feature of Mesa.
27 *
28 * In multicore systems, many applications end up CPU-bound with about half
29 * their time spent inside their rendering thread and half inside Mesa. To
30 * alleviate this, we put a shim layer in Mesa at the GL dispatch level that
31 * quickly logs the GL commands to a buffer to be processed by a worker
32 * thread.
33 */
34
35 #include "main/mtypes.h"
36 #include "main/glthread.h"
37 #include "main/glthread_marshal.h"
38 #include "main/hash.h"
39 #include "util/u_atomic.h"
40 #include "util/u_thread.h"
41
42
43 static void
44 glthread_unmarshal_batch(void *job, int thread_index)
45 {
46 struct glthread_batch *batch = (struct glthread_batch*)job;
47 struct gl_context *ctx = batch->ctx;
48 int pos = 0;
49 int used = batch->used;
50 uint8_t *buffer = batch->buffer;
51
52 _glapi_set_dispatch(ctx->CurrentServerDispatch);
53
54 while (pos < used) {
55 const struct marshal_cmd_base *cmd =
56 (const struct marshal_cmd_base *)&buffer[pos];
57
58 _mesa_unmarshal_dispatch[cmd->cmd_id](ctx, cmd);
59 pos += cmd->cmd_size;
60 }
61
62 assert(pos == used);
63 batch->used = 0;
64 }
65
66 static void
67 glthread_thread_initialization(void *job, int thread_index)
68 {
69 struct gl_context *ctx = (struct gl_context*)job;
70
71 ctx->Driver.SetBackgroundContext(ctx, &ctx->GLThread->stats);
72 _glapi_set_context(ctx);
73 }
74
75 void
76 _mesa_glthread_init(struct gl_context *ctx)
77 {
78 struct glthread_state *glthread = calloc(1, sizeof(*glthread));
79
80 if (!glthread)
81 return;
82
83 if (!util_queue_init(&glthread->queue, "gl", MARSHAL_MAX_BATCHES - 2,
84 1, 0)) {
85 free(glthread);
86 return;
87 }
88
89 glthread->VAOs = _mesa_NewHashTable();
90 if (!glthread->VAOs) {
91 util_queue_destroy(&glthread->queue);
92 free(glthread);
93 return;
94 }
95 glthread->CurrentVAO = &glthread->DefaultVAO;
96
97 ctx->MarshalExec = _mesa_create_marshal_table(ctx);
98 if (!ctx->MarshalExec) {
99 _mesa_DeleteHashTable(glthread->VAOs);
100 util_queue_destroy(&glthread->queue);
101 free(glthread);
102 return;
103 }
104
105 for (unsigned i = 0; i < MARSHAL_MAX_BATCHES; i++) {
106 glthread->batches[i].ctx = ctx;
107 util_queue_fence_init(&glthread->batches[i].fence);
108 }
109
110 glthread->stats.queue = &glthread->queue;
111 ctx->CurrentClientDispatch = ctx->MarshalExec;
112 ctx->GLThread = glthread;
113
114 /* Execute the thread initialization function in the thread. */
115 struct util_queue_fence fence;
116 util_queue_fence_init(&fence);
117 util_queue_add_job(&glthread->queue, ctx, &fence,
118 glthread_thread_initialization, NULL, 0);
119 util_queue_fence_wait(&fence);
120 util_queue_fence_destroy(&fence);
121 }
122
123 static void
124 free_vao(GLuint key, void *data, void *userData)
125 {
126 free(data);
127 }
128
129 void
130 _mesa_glthread_destroy(struct gl_context *ctx)
131 {
132 struct glthread_state *glthread = ctx->GLThread;
133
134 if (!glthread)
135 return;
136
137 _mesa_glthread_finish(ctx);
138 util_queue_destroy(&glthread->queue);
139
140 for (unsigned i = 0; i < MARSHAL_MAX_BATCHES; i++)
141 util_queue_fence_destroy(&glthread->batches[i].fence);
142
143 _mesa_HashDeleteAll(glthread->VAOs, free_vao, NULL);
144 _mesa_DeleteHashTable(glthread->VAOs);
145
146 free(glthread);
147 ctx->GLThread = NULL;
148
149 _mesa_glthread_restore_dispatch(ctx, "destroy");
150 }
151
152 void
153 _mesa_glthread_restore_dispatch(struct gl_context *ctx, const char *func)
154 {
155 /* Remove ourselves from the dispatch table except if another ctx/thread
156 * already installed a new dispatch table.
157 *
158 * Typically glxMakeCurrent will bind a new context (install new table) then
159 * old context might be deleted.
160 */
161 if (_glapi_get_dispatch() == ctx->MarshalExec) {
162 ctx->CurrentClientDispatch = ctx->CurrentServerDispatch;
163 _glapi_set_dispatch(ctx->CurrentClientDispatch);
164 #if 0
165 printf("glthread disabled: %s\n", func);
166 #endif
167 }
168 }
169
170 void
171 _mesa_glthread_disable(struct gl_context *ctx, const char *func)
172 {
173 _mesa_glthread_finish_before(ctx, func);
174 _mesa_glthread_restore_dispatch(ctx, func);
175 }
176
177 void
178 _mesa_glthread_flush_batch(struct gl_context *ctx)
179 {
180 struct glthread_state *glthread = ctx->GLThread;
181 if (!glthread)
182 return;
183
184 struct glthread_batch *next = &glthread->batches[glthread->next];
185 if (!next->used)
186 return;
187
188 /* Debug: execute the batch immediately from this thread.
189 *
190 * Note that glthread_unmarshal_batch() changes the dispatch table so we'll
191 * need to restore it when it returns.
192 */
193 if (false) {
194 glthread_unmarshal_batch(next, 0);
195 _glapi_set_dispatch(ctx->CurrentClientDispatch);
196 return;
197 }
198
199 p_atomic_add(&glthread->stats.num_offloaded_items, next->used);
200
201 util_queue_add_job(&glthread->queue, next, &next->fence,
202 glthread_unmarshal_batch, NULL, 0);
203 glthread->last = glthread->next;
204 glthread->next = (glthread->next + 1) % MARSHAL_MAX_BATCHES;
205 }
206
207 /**
208 * Waits for all pending batches have been unmarshaled.
209 *
210 * This can be used by the main thread to synchronize access to the context,
211 * since the worker thread will be idle after this.
212 */
213 void
214 _mesa_glthread_finish(struct gl_context *ctx)
215 {
216 struct glthread_state *glthread = ctx->GLThread;
217 if (!glthread)
218 return;
219
220 /* If this is called from the worker thread, then we've hit a path that
221 * might be called from either the main thread or the worker (such as some
222 * dri interface entrypoints), in which case we don't need to actually
223 * synchronize against ourself.
224 */
225 if (u_thread_is_self(glthread->queue.threads[0]))
226 return;
227
228 struct glthread_batch *last = &glthread->batches[glthread->last];
229 struct glthread_batch *next = &glthread->batches[glthread->next];
230 bool synced = false;
231
232 if (!util_queue_fence_is_signalled(&last->fence)) {
233 util_queue_fence_wait(&last->fence);
234 synced = true;
235 }
236
237 if (next->used) {
238 p_atomic_add(&glthread->stats.num_direct_items, next->used);
239
240 /* Since glthread_unmarshal_batch changes the dispatch to direct,
241 * restore it after it's done.
242 */
243 struct _glapi_table *dispatch = _glapi_get_dispatch();
244 glthread_unmarshal_batch(next, 0);
245 _glapi_set_dispatch(dispatch);
246
247 /* It's not a sync because we don't enqueue partial batches, but
248 * it would be a sync if we did. So count it anyway.
249 */
250 synced = true;
251 }
252
253 if (synced)
254 p_atomic_inc(&glthread->stats.num_syncs);
255 }
256
257 void
258 _mesa_glthread_finish_before(struct gl_context *ctx, const char *func)
259 {
260 _mesa_glthread_finish(ctx);
261
262 /* Uncomment this if you want to know where glthread syncs. */
263 /*printf("fallback to sync: %s\n", func);*/
264 }