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"
27 #include "gpu_timing.h"
28 #include "gpulib_thread_if.h"
32 #define BOOL unsigned short
40 #define QUEUE_SIZE 0x2000
46 video_thread_cmd queue[QUEUE_SIZE];
51 pthread_mutex_t queue_lock;
52 pthread_cond_t cond_msg_avail;
53 pthread_cond_t cond_msg_done;
54 pthread_cond_t cond_queue_empty;
55 video_thread_queue *queue;
56 video_thread_queue *bg_queue;
60 static video_thread_state thread;
61 static video_thread_queue queues[2];
62 static int thread_rendering;
63 static BOOL hold_cmds;
64 static BOOL needs_display;
67 extern const unsigned char cmd_lengths[];
69 static void *video_thread_main(void *arg) {
70 video_thread_state *thread = (video_thread_state *)arg;
71 video_thread_cmd *cmd;
75 static int processed = 0;
79 int result, cycles_dummy = 0, last_cmd, start, end;
80 video_thread_queue *queue;
81 pthread_mutex_lock(&thread->queue_lock);
83 while (!thread->queue->used && thread->running) {
84 pthread_cond_wait(&thread->cond_msg_avail, &thread->queue_lock);
87 if (!thread->running) {
88 pthread_mutex_unlock(&thread->queue_lock);
92 queue = thread->queue;
94 end = queue->end > queue->start ? queue->end : QUEUE_SIZE;
95 queue->start = end % QUEUE_SIZE;
96 pthread_mutex_unlock(&thread->queue_lock);
98 for (i = start; i < end; i++) {
99 cmd = &queue->queue[i];
100 result = real_do_cmd_list(cmd->cmd_list, cmd->count,
101 &cycles_dummy, &cycles_dummy, &last_cmd);
102 if (result != cmd->count) {
103 fprintf(stderr, "Processed wrong cmd count: expected %d, got %d\n", cmd->count, result);
107 /* Periodically yield so as not to starve other threads */
108 processed += cmd->count;
109 if (processed >= 512) {
116 pthread_mutex_lock(&thread->queue_lock);
117 queue->used -= (end - start);
120 pthread_cond_signal(&thread->cond_queue_empty);
122 pthread_cond_signal(&thread->cond_msg_done);
123 pthread_mutex_unlock(&thread->queue_lock);
129 static void cmd_queue_swap() {
130 video_thread_queue *tmp;
131 if (!thread.bg_queue->used) return;
133 pthread_mutex_lock(&thread.queue_lock);
134 if (!thread.queue->used) {
136 thread.queue = thread.bg_queue;
137 thread.bg_queue = tmp;
138 pthread_cond_signal(&thread.cond_msg_avail);
140 pthread_mutex_unlock(&thread.queue_lock);
143 /* Waits for the main queue to completely finish. */
144 void renderer_wait() {
145 if (!thread.running) return;
147 /* Not completely safe, but should be fine since the render thread
148 * only decreases used, and we check again inside the lock. */
149 if (!thread.queue->used) {
153 pthread_mutex_lock(&thread.queue_lock);
155 while (thread.queue->used) {
156 pthread_cond_wait(&thread.cond_queue_empty, &thread.queue_lock);
159 pthread_mutex_unlock(&thread.queue_lock);
162 /* Waits for all GPU commands in both queues to finish, bringing VRAM
163 * completely up-to-date. */
164 void renderer_sync(void) {
165 if (!thread.running) return;
167 /* Not completely safe, but should be fine since the render thread
168 * only decreases used, and we check again inside the lock. */
169 if (!thread.queue->used && !thread.bg_queue->used) {
173 if (thread.bg_queue->used) {
174 /* When we flush the background queue, the vblank handler can't
175 * know that we had a frame pending, and we delay rendering too
180 /* Flush both queues. This is necessary because gpulib could be
181 * trying to process a DMA write that a command in the queue should
182 * run beforehand. For example, Xenogears sprites write a black
183 * rectangle over the to-be-DMA'd spot in VRAM -- if this write
184 * happens after the DMA, it will clear the DMA, resulting in
185 * flickering sprites. We need to be totally up-to-date. This may
193 static void video_thread_stop() {
197 if (thread.running) {
198 thread.running = FALSE;
199 pthread_cond_signal(&thread.cond_msg_avail);
200 pthread_join(thread.thread, NULL);
203 pthread_mutex_destroy(&thread.queue_lock);
204 pthread_cond_destroy(&thread.cond_msg_avail);
205 pthread_cond_destroy(&thread.cond_msg_done);
206 pthread_cond_destroy(&thread.cond_queue_empty);
208 for (i = 0; i < QUEUE_SIZE; i++) {
209 video_thread_cmd *cmd = &thread.queue->queue[i];
211 cmd->cmd_list = NULL;
214 for (i = 0; i < QUEUE_SIZE; i++) {
215 video_thread_cmd *cmd = &thread.bg_queue->queue[i];
217 cmd->cmd_list = NULL;
221 static void video_thread_start() {
222 fprintf(stdout, "Starting render thread\n");
224 if (pthread_cond_init(&thread.cond_msg_avail, NULL) ||
225 pthread_cond_init(&thread.cond_msg_done, NULL) ||
226 pthread_cond_init(&thread.cond_queue_empty, NULL) ||
227 pthread_mutex_init(&thread.queue_lock, NULL) ||
228 pthread_create(&thread.thread, NULL, video_thread_main, &thread)) {
232 thread.queue = &queues[0];
233 thread.bg_queue = &queues[1];
235 thread.running = TRUE;
239 fprintf(stderr,"Failed to start rendering thread\n");
243 static void video_thread_queue_cmd(uint32_t *list, int count, int last_cmd) {
244 video_thread_cmd *cmd;
246 video_thread_queue *queue;
249 cmd_list = (uint32_t *)calloc(count, sizeof(uint32_t));
252 /* Out of memory, disable the thread and run sync from now on */
253 fprintf(stderr,"Failed to allocate render thread command list, stopping thread\n");
257 memcpy(cmd_list, list, count * sizeof(uint32_t));
259 if (hold_cmds && thread.bg_queue->used >= QUEUE_SIZE) {
260 /* If the bg queue is full, do a full sync to empty both queues
261 * and clear space. This should be very rare, I've only seen it in
262 * Tekken 3 post-battle-replay. */
267 queue = thread.bg_queue;
270 queue = thread.queue;
275 pthread_mutex_lock(&thread.queue_lock);
277 while (queue->used >= QUEUE_SIZE) {
278 pthread_cond_wait(&thread.cond_msg_done, &thread.queue_lock);
282 cmd = &queue->queue[queue->end];
284 cmd->cmd_list = cmd_list;
286 cmd->last_cmd = last_cmd;
287 queue->end = (queue->end + 1) % QUEUE_SIZE;
291 pthread_cond_signal(&thread.cond_msg_avail);
292 pthread_mutex_unlock(&thread.queue_lock);
296 /* Slice off just the part of the list that can be handled async, and
298 static int scan_cmd_list(uint32_t *data, int count,
299 int *cycles_sum_out, int *cycles_last, int *last_cmd)
301 int cpu_cycles_sum = 0, cpu_cycles = *cycles_last;
302 int cmd = 0, pos = 0, len, v;
304 while (pos < count) {
305 uint32_t *list = data + pos;
306 short *slist = (void *)list;
307 cmd = LE32TOH(list[0]) >> 24;
308 len = 1 + cmd_lengths[cmd];
312 gput_sum(cpu_cycles_sum, cpu_cycles,
313 gput_fill(LE16TOH(slist[4]) & 0x3ff,
314 LE16TOH(slist[5]) & 0x1ff));
317 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base());
320 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_t());
321 gpu.ex_regs[1] &= ~0x1ff;
322 gpu.ex_regs[1] |= LE32TOH(list[4]) & 0x1ff;
325 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base());
328 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_t());
329 gpu.ex_regs[1] &= ~0x1ff;
330 gpu.ex_regs[1] |= LE32TOH(list[4]) & 0x1ff;
333 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_g());
336 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_gt());
337 gpu.ex_regs[1] &= ~0x1ff;
338 gpu.ex_regs[1] |= LE32TOH(list[5]) & 0x1ff;
341 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_g());
344 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_gt());
345 gpu.ex_regs[1] &= ~0x1ff;
346 gpu.ex_regs[1] |= LE32TOH(list[5]) & 0x1ff;
349 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
352 for (v = 3; pos + v < count; v++)
354 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
355 if ((list[v] & 0xf000f000) == 0x50005000)
361 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
364 for (v = 4; pos + v < count; v += 2)
366 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
367 if ((list[v] & 0xf000f000) == 0x50005000)
373 gput_sum(cpu_cycles_sum, cpu_cycles,
374 gput_sprite(LE16TOH(slist[4]) & 0x3ff,
375 LE16TOH(slist[5]) & 0x1ff));
378 gput_sum(cpu_cycles_sum, cpu_cycles,
379 gput_sprite(LE16TOH(slist[6]) & 0x3ff,
380 LE16TOH(slist[7]) & 0x1ff));
383 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(1, 1));
386 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(8, 8));
389 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(16, 16));
392 if ((cmd & 0xf8) == 0xe0)
393 gpu.ex_regs[cmd & 7] = list[0];
397 if (pos + len > count) {
399 break; /* incomplete cmd */
401 if (0x80 <= cmd && cmd <= 0xdf)
402 break; /* image i/o */
407 *cycles_sum_out += cpu_cycles_sum;
408 *cycles_last = cpu_cycles;
413 int do_cmd_list(uint32_t *list, int count,
414 int *cycles_sum, int *cycles_last, int *last_cmd)
418 if (thread.running) {
419 pos = scan_cmd_list(list, count, cycles_sum, cycles_last, last_cmd);
420 video_thread_queue_cmd(list, pos, *last_cmd);
422 pos = real_do_cmd_list(list, count, cycles_sum, cycles_last, last_cmd);
423 memcpy(gpu.ex_regs, gpu.scratch_ex_regs, sizeof(gpu.ex_regs));
428 int renderer_init(void) {
429 if (thread_rendering) {
430 video_thread_start();
432 return real_renderer_init();
435 void renderer_finish(void) {
436 real_renderer_finish();
438 if (thread_rendering && thread.running) {
443 void renderer_sync_ecmds(uint32_t * ecmds) {
444 if (thread.running) {
446 do_cmd_list(&ecmds[1], 6, &dummy, &dummy, &dummy);
448 real_renderer_sync_ecmds(ecmds);
452 void renderer_update_caches(int x, int y, int w, int h, int state_changed) {
454 real_renderer_update_caches(x, y, w, h, state_changed);
457 void renderer_flush_queues(void) {
458 /* Called during DMA and updateLace. We want to sync if it's DMA,
459 * but not if it's updateLace. Instead of syncing here, there's a
460 * renderer_sync call during DMA. */
461 real_renderer_flush_queues();
465 * Normally all GPU commands are processed before rendering the
466 * frame. For games that naturally run < 50/60fps, this is unnecessary
467 * -- it forces the game to render as if it was 60fps and leaves the
468 * GPU idle half the time on a 30fps game, for example.
470 * Allowing the renderer to wait until a frame is done before
471 * rendering it would give it double, triple, or quadruple the amount
472 * of time to finish before we have to wait for it.
474 * We can use a heuristic to figure out when to force a render.
476 * - If a frame isn't done when we're asked to render, wait for it and
477 * put future GPU commands in a separate buffer (for the next frame)
479 * - If the frame is done, and had no future GPU commands, render it.
481 * - If we do have future GPU commands, it meant the frame took too
482 * long to render and there's another frame waiting. Stop until the
483 * first frame finishes, render it, and start processing the next
486 * This may possibly add a frame or two of latency that shouldn't be
487 * different than the real device. It may skip rendering a frame
488 * entirely if a VRAM transfer happens while a frame is waiting, or in
489 * games that natively run at 60fps if frames are coming in too
490 * quickly to process. Depending on how the game treats "60fps," this
491 * may not be noticeable.
493 void renderer_notify_update_lace(int updated) {
494 if (!thread.running) return;
496 if (thread_rendering == THREAD_RENDERING_SYNC) {
506 pthread_mutex_lock(&thread.queue_lock);
507 if (thread.bg_queue->used || flushed) {
508 /* We have commands for a future frame to run. Force a wait until
509 * the current frame is finished, and start processing the next
510 * frame after it's drawn (see the `updated` clause above). */
511 pthread_mutex_unlock(&thread.queue_lock);
513 pthread_mutex_lock(&thread.queue_lock);
515 /* We are no longer holding commands back, so the next frame may
516 * get mixed into the following frame. This is usually fine, but can
517 * result in frameskip-like effects for 60fps games. */
520 needs_display = TRUE;
521 gpu.state.fb_dirty = TRUE;
522 } else if (thread.queue->used) {
523 /* We are still drawing during a vblank. Cut off the current frame
524 * by sending new commands to the background queue and skip
525 * drawing our partly rendered frame to the display. */
527 needs_display = TRUE;
528 gpu.state.fb_dirty = FALSE;
529 } else if (needs_display && !thread.queue->used) {
530 /* We have processed all commands in the queue, render the
531 * buffer. We know we have something to render, because
532 * needs_display is TRUE. */
534 needs_display = FALSE;
535 gpu.state.fb_dirty = TRUE;
537 /* Everything went normally, so do the normal thing. */
540 pthread_mutex_unlock(&thread.queue_lock);
543 void renderer_set_interlace(int enable, int is_odd) {
544 real_renderer_set_interlace(enable, is_odd);
547 void renderer_set_config(const struct rearmed_cbs *cbs) {
549 thread_rendering = cbs->thread_rendering;
550 if (!thread.running && thread_rendering != THREAD_RENDERING_OFF) {
551 video_thread_start();
552 } else if (thread.running && thread_rendering == THREAD_RENDERING_OFF) {
555 real_renderer_set_config(cbs);
558 void renderer_notify_res_change(void) {
560 real_renderer_notify_res_change();