1 /**************************************************************************
2 * Copyright (C) 2020 The RetroArch Team *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. *
18 ***************************************************************************/
24 #include "../gpulib/gpu.h"
25 #include "../../frontend/plugin_lib.h"
26 #include "gpulib_thread_if.h"
30 #define BOOL unsigned short
38 #define QUEUE_SIZE 0x2000
44 video_thread_cmd queue[QUEUE_SIZE];
49 pthread_mutex_t queue_lock;
50 pthread_cond_t cond_msg_avail;
51 pthread_cond_t cond_msg_done;
52 pthread_cond_t cond_queue_empty;
53 video_thread_queue *queue;
54 video_thread_queue *bg_queue;
58 static video_thread_state thread;
59 static video_thread_queue queues[2];
60 static int thread_rendering;
61 static BOOL hold_cmds;
62 static BOOL needs_display;
65 extern const unsigned char cmd_lengths[];
67 static void *video_thread_main(void *arg) {
68 video_thread_state *thread = (video_thread_state *)arg;
69 video_thread_cmd *cmd;
73 static int processed = 0;
77 int result, last_cmd, start, end;
78 video_thread_queue *queue;
79 pthread_mutex_lock(&thread->queue_lock);
81 while (!thread->queue->used && thread->running) {
82 pthread_cond_wait(&thread->cond_msg_avail, &thread->queue_lock);
85 if (!thread->running) {
86 pthread_mutex_unlock(&thread->queue_lock);
90 queue = thread->queue;
92 end = queue->end > queue->start ? queue->end : QUEUE_SIZE;
93 queue->start = end % QUEUE_SIZE;
94 pthread_mutex_unlock(&thread->queue_lock);
96 for (i = start; i < end; i++) {
97 cmd = &queue->queue[i];
98 result = real_do_cmd_list(cmd->cmd_list, cmd->count, &last_cmd);
100 if (result != cmd->count) {
101 fprintf(stderr, "Processed wrong cmd count: expected %d, got %d\n", cmd->count, result);
105 /* Periodically yield so as not to starve other threads */
106 processed += cmd->count;
107 if (processed >= 512) {
114 pthread_mutex_lock(&thread->queue_lock);
115 queue->used -= (end - start);
118 pthread_cond_signal(&thread->cond_queue_empty);
120 pthread_cond_signal(&thread->cond_msg_done);
121 pthread_mutex_unlock(&thread->queue_lock);
127 static void cmd_queue_swap() {
128 video_thread_queue *tmp;
129 if (!thread.bg_queue->used) return;
131 pthread_mutex_lock(&thread.queue_lock);
132 if (!thread.queue->used) {
134 thread.queue = thread.bg_queue;
135 thread.bg_queue = tmp;
136 pthread_cond_signal(&thread.cond_msg_avail);
138 pthread_mutex_unlock(&thread.queue_lock);
141 /* Waits for the main queue to completely finish. */
142 void renderer_wait() {
143 if (!thread.running) return;
145 /* Not completely safe, but should be fine since the render thread
146 * only decreases used, and we check again inside the lock. */
147 if (!thread.queue->used) {
151 pthread_mutex_lock(&thread.queue_lock);
153 while (thread.queue->used) {
154 pthread_cond_wait(&thread.cond_queue_empty, &thread.queue_lock);
157 pthread_mutex_unlock(&thread.queue_lock);
160 /* Waits for all GPU commands in both queues to finish, bringing VRAM
161 * completely up-to-date. */
162 void renderer_sync(void) {
163 if (!thread.running) return;
165 /* Not completely safe, but should be fine since the render thread
166 * only decreases used, and we check again inside the lock. */
167 if (!thread.queue->used && !thread.bg_queue->used) {
171 if (thread.bg_queue->used) {
172 /* When we flush the background queue, the vblank handler can't
173 * know that we had a frame pending, and we delay rendering too
178 /* Flush both queues. This is necessary because gpulib could be
179 * trying to process a DMA write that a command in the queue should
180 * run beforehand. For example, Xenogears sprites write a black
181 * rectangle over the to-be-DMA'd spot in VRAM -- if this write
182 * happens after the DMA, it will clear the DMA, resulting in
183 * flickering sprites. We need to be totally up-to-date. This may
191 static void video_thread_stop() {
195 if (thread.running) {
196 thread.running = FALSE;
197 pthread_cond_signal(&thread.cond_msg_avail);
198 pthread_join(thread.thread, NULL);
201 pthread_mutex_destroy(&thread.queue_lock);
202 pthread_cond_destroy(&thread.cond_msg_avail);
203 pthread_cond_destroy(&thread.cond_msg_done);
204 pthread_cond_destroy(&thread.cond_queue_empty);
206 for (i = 0; i < QUEUE_SIZE; i++) {
207 video_thread_cmd *cmd = &thread.queue->queue[i];
209 cmd->cmd_list = NULL;
212 for (i = 0; i < QUEUE_SIZE; i++) {
213 video_thread_cmd *cmd = &thread.bg_queue->queue[i];
215 cmd->cmd_list = NULL;
219 static void video_thread_start() {
220 fprintf(stdout, "Starting render thread\n");
222 if (pthread_cond_init(&thread.cond_msg_avail, NULL) ||
223 pthread_cond_init(&thread.cond_msg_done, NULL) ||
224 pthread_cond_init(&thread.cond_queue_empty, NULL) ||
225 pthread_mutex_init(&thread.queue_lock, NULL) ||
226 pthread_create(&thread.thread, NULL, video_thread_main, &thread)) {
230 thread.queue = &queues[0];
231 thread.bg_queue = &queues[1];
233 thread.running = TRUE;
237 fprintf(stderr,"Failed to start rendering thread\n");
241 static void video_thread_queue_cmd(uint32_t *list, int count, int last_cmd) {
242 video_thread_cmd *cmd;
244 video_thread_queue *queue;
247 cmd_list = (uint32_t *)calloc(count, sizeof(uint32_t));
250 /* Out of memory, disable the thread and run sync from now on */
251 fprintf(stderr,"Failed to allocate render thread command list, stopping thread\n");
255 memcpy(cmd_list, list, count * sizeof(uint32_t));
257 if (hold_cmds && thread.bg_queue->used >= QUEUE_SIZE) {
258 /* If the bg queue is full, do a full sync to empty both queues
259 * and clear space. This should be very rare, I've only seen it in
260 * Tekken 3 post-battle-replay. */
265 queue = thread.bg_queue;
268 queue = thread.queue;
273 pthread_mutex_lock(&thread.queue_lock);
275 while (queue->used >= QUEUE_SIZE) {
276 pthread_cond_wait(&thread.cond_msg_done, &thread.queue_lock);
280 cmd = &queue->queue[queue->end];
282 cmd->cmd_list = cmd_list;
284 cmd->last_cmd = last_cmd;
285 queue->end = (queue->end + 1) % QUEUE_SIZE;
289 pthread_cond_signal(&thread.cond_msg_avail);
290 pthread_mutex_unlock(&thread.queue_lock);
294 /* Slice off just the part of the list that can be handled async, and
296 static int scan_cmd_list(uint32_t *data, int count, int *last_cmd)
298 int cmd = 0, pos = 0, len, v;
300 while (pos < count) {
301 uint32_t *list = data + pos;
303 len = 1 + cmd_lengths[cmd];
312 gpu.ex_regs[1] &= ~0x1ff;
313 gpu.ex_regs[1] |= list[4 + ((cmd >> 4) & 1)] & 0x1ff;
316 for (v = 3; pos + v < count; v++)
318 if ((list[v] & 0xf000f000) == 0x50005000)
324 for (v = 4; pos + v < count; v += 2)
326 if ((list[v] & 0xf000f000) == 0x50005000)
332 if ((cmd & 0xf8) == 0xe0)
333 gpu.ex_regs[cmd & 7] = list[0];
337 if (pos + len > count) {
339 break; /* incomplete cmd */
341 if (0xa0 <= cmd && cmd <= 0xdf)
342 break; /* image i/o */
351 int do_cmd_list(uint32_t *list, int count, int *last_cmd) {
354 if (thread.running) {
355 pos = scan_cmd_list(list, count, last_cmd);
356 video_thread_queue_cmd(list, pos, *last_cmd);
358 pos = real_do_cmd_list(list, count, last_cmd);
359 memcpy(gpu.ex_regs, gpu.scratch_ex_regs, sizeof(gpu.ex_regs));
364 int renderer_init(void) {
365 if (thread_rendering) {
366 video_thread_start();
368 return real_renderer_init();
371 void renderer_finish(void) {
372 real_renderer_finish();
374 if (thread_rendering && thread.running) {
379 void renderer_sync_ecmds(uint32_t * ecmds) {
380 if (thread.running) {
382 do_cmd_list(&ecmds[1], 6, &dummy);
384 real_renderer_sync_ecmds(ecmds);
388 void renderer_update_caches(int x, int y, int w, int h) {
390 real_renderer_update_caches(x, y, w, h);
393 void renderer_flush_queues(void) {
394 /* Called during DMA and updateLace. We want to sync if it's DMA,
395 * but not if it's updateLace. Instead of syncing here, there's a
396 * renderer_sync call during DMA. */
397 real_renderer_flush_queues();
401 * Normally all GPU commands are processed before rendering the
402 * frame. For games that naturally run < 50/60fps, this is unnecessary
403 * -- it forces the game to render as if it was 60fps and leaves the
404 * GPU idle half the time on a 30fps game, for example.
406 * Allowing the renderer to wait until a frame is done before
407 * rendering it would give it double, triple, or quadruple the amount
408 * of time to finish before we have to wait for it.
410 * We can use a heuristic to figure out when to force a render.
412 * - If a frame isn't done when we're asked to render, wait for it and
413 * put future GPU commands in a separate buffer (for the next frame)
415 * - If the frame is done, and had no future GPU commands, render it.
417 * - If we do have future GPU commands, it meant the frame took too
418 * long to render and there's another frame waiting. Stop until the
419 * first frame finishes, render it, and start processing the next
422 * This may possibly add a frame or two of latency that shouldn't be
423 * different than the real device. It may skip rendering a frame
424 * entirely if a VRAM transfer happens while a frame is waiting, or in
425 * games that natively run at 60fps if frames are coming in too
426 * quickly to process. Depending on how the game treats "60fps," this
427 * may not be noticeable.
429 void renderer_notify_update_lace(int updated) {
430 if (!thread.running) return;
432 if (thread_rendering == THREAD_RENDERING_SYNC) {
442 pthread_mutex_lock(&thread.queue_lock);
443 if (thread.bg_queue->used || flushed) {
444 /* We have commands for a future frame to run. Force a wait until
445 * the current frame is finished, and start processing the next
446 * frame after it's drawn (see the `updated` clause above). */
447 pthread_mutex_unlock(&thread.queue_lock);
449 pthread_mutex_lock(&thread.queue_lock);
451 /* We are no longer holding commands back, so the next frame may
452 * get mixed into the following frame. This is usually fine, but can
453 * result in frameskip-like effects for 60fps games. */
456 needs_display = TRUE;
457 gpu.state.fb_dirty = TRUE;
458 } else if (thread.queue->used) {
459 /* We are still drawing during a vblank. Cut off the current frame
460 * by sending new commands to the background queue and skip
461 * drawing our partly rendered frame to the display. */
463 needs_display = TRUE;
464 gpu.state.fb_dirty = FALSE;
465 } else if (needs_display && !thread.queue->used) {
466 /* We have processed all commands in the queue, render the
467 * buffer. We know we have something to render, because
468 * needs_display is TRUE. */
470 needs_display = FALSE;
471 gpu.state.fb_dirty = TRUE;
473 /* Everything went normally, so do the normal thing. */
476 pthread_mutex_unlock(&thread.queue_lock);
479 void renderer_set_interlace(int enable, int is_odd) {
480 real_renderer_set_interlace(enable, is_odd);
483 void renderer_set_config(const struct rearmed_cbs *cbs) {
485 thread_rendering = cbs->thread_rendering;
486 if (!thread.running && thread_rendering != THREAD_RENDERING_OFF) {
487 video_thread_start();
488 } else if (thread.running && thread_rendering == THREAD_RENDERING_OFF) {
491 real_renderer_set_config(cbs);
494 void renderer_notify_res_change(void) {
496 real_renderer_notify_res_change();