ac86081b679b869d1a945ba39fb79b76aae9f551
[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 = &ctx->GLThread;
79
80 assert(!glthread->enabled);
81
82 if (!util_queue_init(&glthread->queue, "gl", MARSHAL_MAX_BATCHES - 2,
83 1, 0)) {
84 return;
85 }
86
87 glthread->VAOs = _mesa_NewHashTable();
88 if (!glthread->VAOs) {
89 util_queue_destroy(&glthread->queue);
90 return;
91 }
92
93 _mesa_glthread_reset_vao(&glthread->DefaultVAO);
94 glthread->CurrentVAO = &glthread->DefaultVAO;
95
96 ctx->MarshalExec = _mesa_create_marshal_table(ctx);
97 if (!ctx->MarshalExec) {
98 _mesa_DeleteHashTable(glthread->VAOs);
99 util_queue_destroy(&glthread->queue);
100 return;
101 }
102
103 for (unsigned i = 0; i < MARSHAL_MAX_BATCHES; i++) {
104 glthread->batches[i].ctx = ctx;
105 util_queue_fence_init(&glthread->batches[i].fence);
106 }
107 glthread->next_batch = &glthread->batches[glthread->next];
108
109 glthread->enabled = true;
110 glthread->stats.queue = &glthread->queue;
111
112 glthread->SupportsBufferUploads =
113 ctx->Const.BufferCreateMapUnsynchronizedThreadSafe &&
114 ctx->Const.AllowMappedBuffersDuringExecution;
115
116 ctx->CurrentClientDispatch = ctx->MarshalExec;
117
118 /* Execute the thread initialization function in the thread. */
119 struct util_queue_fence fence;
120 util_queue_fence_init(&fence);
121 util_queue_add_job(&glthread->queue, ctx, &fence,
122 glthread_thread_initialization, NULL, 0);
123 util_queue_fence_wait(&fence);
124 util_queue_fence_destroy(&fence);
125 }
126
127 static void
128 free_vao(GLuint key, void *data, void *userData)
129 {
130 free(data);
131 }
132
133 void
134 _mesa_glthread_destroy(struct gl_context *ctx)
135 {
136 struct glthread_state *glthread = &ctx->GLThread;
137
138 if (!glthread->enabled)
139 return;
140
141 _mesa_glthread_finish(ctx);
142 util_queue_destroy(&glthread->queue);
143
144 for (unsigned i = 0; i < MARSHAL_MAX_BATCHES; i++)
145 util_queue_fence_destroy(&glthread->batches[i].fence);
146
147 _mesa_HashDeleteAll(glthread->VAOs, free_vao, NULL);
148 _mesa_DeleteHashTable(glthread->VAOs);
149
150 ctx->GLThread.enabled = false;
151
152 _mesa_glthread_restore_dispatch(ctx, "destroy");
153 }
154
155 void
156 _mesa_glthread_restore_dispatch(struct gl_context *ctx, const char *func)
157 {
158 /* Remove ourselves from the dispatch table except if another ctx/thread
159 * already installed a new dispatch table.
160 *
161 * Typically glxMakeCurrent will bind a new context (install new table) then
162 * old context might be deleted.
163 */
164 if (_glapi_get_dispatch() == ctx->MarshalExec) {
165 ctx->CurrentClientDispatch = ctx->CurrentServerDispatch;
166 _glapi_set_dispatch(ctx->CurrentClientDispatch);
167 #if 0
168 printf("glthread disabled: %s\n", func);
169 #endif
170 }
171 }
172
173 void
174 _mesa_glthread_disable(struct gl_context *ctx, const char *func)
175 {
176 _mesa_glthread_finish_before(ctx, func);
177 _mesa_glthread_restore_dispatch(ctx, func);
178 }
179
180 void
181 _mesa_glthread_flush_batch(struct gl_context *ctx)
182 {
183 struct glthread_state *glthread = &ctx->GLThread;
184 if (!glthread->enabled)
185 return;
186
187 struct glthread_batch *next = glthread->next_batch;
188 if (!next->used)
189 return;
190
191 /* Debug: execute the batch immediately from this thread.
192 *
193 * Note that glthread_unmarshal_batch() changes the dispatch table so we'll
194 * need to restore it when it returns.
195 */
196 if (false) {
197 glthread_unmarshal_batch(next, 0);
198 _glapi_set_dispatch(ctx->CurrentClientDispatch);
199 return;
200 }
201
202 p_atomic_add(&glthread->stats.num_offloaded_items, next->used);
203
204 util_queue_add_job(&glthread->queue, next, &next->fence,
205 glthread_unmarshal_batch, NULL, 0);
206 glthread->last = glthread->next;
207 glthread->next = (glthread->next + 1) % MARSHAL_MAX_BATCHES;
208 glthread->next_batch = &glthread->batches[glthread->next];
209 }
210
211 /**
212 * Waits for all pending batches have been unmarshaled.
213 *
214 * This can be used by the main thread to synchronize access to the context,
215 * since the worker thread will be idle after this.
216 */
217 void
218 _mesa_glthread_finish(struct gl_context *ctx)
219 {
220 struct glthread_state *glthread = &ctx->GLThread;
221 if (!glthread->enabled)
222 return;
223
224 /* If this is called from the worker thread, then we've hit a path that
225 * might be called from either the main thread or the worker (such as some
226 * dri interface entrypoints), in which case we don't need to actually
227 * synchronize against ourself.
228 */
229 if (u_thread_is_self(glthread->queue.threads[0]))
230 return;
231
232 struct glthread_batch *last = &glthread->batches[glthread->last];
233 struct glthread_batch *next = glthread->next_batch;
234 bool synced = false;
235
236 if (!util_queue_fence_is_signalled(&last->fence)) {
237 util_queue_fence_wait(&last->fence);
238 synced = true;
239 }
240
241 if (next->used) {
242 p_atomic_add(&glthread->stats.num_direct_items, next->used);
243
244 /* Since glthread_unmarshal_batch changes the dispatch to direct,
245 * restore it after it's done.
246 */
247 struct _glapi_table *dispatch = _glapi_get_dispatch();
248 glthread_unmarshal_batch(next, 0);
249 _glapi_set_dispatch(dispatch);
250
251 /* It's not a sync because we don't enqueue partial batches, but
252 * it would be a sync if we did. So count it anyway.
253 */
254 synced = true;
255 }
256
257 if (synced)
258 p_atomic_inc(&glthread->stats.num_syncs);
259 }
260
261 void
262 _mesa_glthread_finish_before(struct gl_context *ctx, const char *func)
263 {
264 _mesa_glthread_finish(ctx);
265
266 /* Uncomment this if you want to know where glthread syncs. */
267 /*printf("fallback to sync: %s\n", func);*/
268 }