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"
30 extern void SysPrintf(const char *fmt, ...);
34 #define BOOL unsigned short
42 #define QUEUE_SIZE 0x2000
48 video_thread_cmd queue[QUEUE_SIZE];
53 pthread_mutex_t queue_lock;
54 pthread_cond_t cond_msg_avail;
55 pthread_cond_t cond_msg_done;
56 pthread_cond_t cond_queue_empty;
57 video_thread_queue *queue;
58 video_thread_queue *bg_queue;
62 static video_thread_state thread;
63 static video_thread_queue queues[2];
64 static int thread_rendering;
65 static BOOL hold_cmds;
66 static BOOL needs_display;
69 extern const unsigned char cmd_lengths[];
71 static void *video_thread_main(void *arg) {
72 video_thread_cmd *cmd;
76 static int processed = 0;
79 #if defined(__arm__) && defined(__ARM_FP)
82 __asm__ volatile("vmrs %0, fpscr" : "=r"(fpscr));
84 fpscr |= 0x03000000; // DN | FZ
85 __asm__ volatile("vmsr fpscr, %0" :: "r"(fpscr));
89 int result, cycles_dummy = 0, last_cmd, start, end;
90 video_thread_queue *queue;
91 pthread_mutex_lock(&thread.queue_lock);
93 while (!thread.queue->used && thread.running) {
94 pthread_cond_wait(&thread.cond_msg_avail, &thread.queue_lock);
97 if (!thread.running) {
98 pthread_mutex_unlock(&thread.queue_lock);
102 queue = thread.queue;
103 start = queue->start;
104 end = queue->end > queue->start ? queue->end : QUEUE_SIZE;
105 queue->start = end % QUEUE_SIZE;
106 pthread_mutex_unlock(&thread.queue_lock);
108 for (i = start; i < end; i++) {
109 cmd = &queue->queue[i];
110 result = real_do_cmd_list(cmd->cmd_list, cmd->count,
111 &cycles_dummy, &cycles_dummy, &last_cmd);
112 if (result != cmd->count) {
113 fprintf(stderr, "Processed wrong cmd count: expected %d, got %d\n", cmd->count, result);
117 /* Periodically yield so as not to starve other threads */
118 processed += cmd->count;
119 if (processed >= 512) {
126 pthread_mutex_lock(&thread.queue_lock);
127 queue->used -= (end - start);
130 pthread_cond_signal(&thread.cond_queue_empty);
132 pthread_cond_signal(&thread.cond_msg_done);
133 pthread_mutex_unlock(&thread.queue_lock);
139 static void cmd_queue_swap() {
140 video_thread_queue *tmp;
141 if (!thread.bg_queue->used) return;
143 pthread_mutex_lock(&thread.queue_lock);
144 if (!thread.queue->used) {
146 thread.queue = thread.bg_queue;
147 thread.bg_queue = tmp;
148 pthread_cond_signal(&thread.cond_msg_avail);
150 pthread_mutex_unlock(&thread.queue_lock);
153 /* Waits for the main queue to completely finish. */
154 void renderer_wait() {
155 if (!thread.running) return;
157 /* Not completely safe, but should be fine since the render thread
158 * only decreases used, and we check again inside the lock. */
159 if (!thread.queue->used) {
163 pthread_mutex_lock(&thread.queue_lock);
165 while (thread.queue->used) {
166 pthread_cond_wait(&thread.cond_queue_empty, &thread.queue_lock);
169 pthread_mutex_unlock(&thread.queue_lock);
172 /* Waits for all GPU commands in both queues to finish, bringing VRAM
173 * completely up-to-date. */
174 void renderer_sync(void) {
175 if (!thread.running) return;
177 /* Not completely safe, but should be fine since the render thread
178 * only decreases used, and we check again inside the lock. */
179 if (!thread.queue->used && !thread.bg_queue->used) {
183 if (thread.bg_queue->used) {
184 /* When we flush the background queue, the vblank handler can't
185 * know that we had a frame pending, and we delay rendering too
190 /* Flush both queues. This is necessary because gpulib could be
191 * trying to process a DMA write that a command in the queue should
192 * run beforehand. For example, Xenogears sprites write a black
193 * rectangle over the to-be-DMA'd spot in VRAM -- if this write
194 * happens after the DMA, it will clear the DMA, resulting in
195 * flickering sprites. We need to be totally up-to-date. This may
203 static void video_thread_stop() {
207 if (thread.running) {
208 thread.running = FALSE;
209 pthread_cond_signal(&thread.cond_msg_avail);
210 pthread_join(thread.thread, NULL);
213 pthread_mutex_destroy(&thread.queue_lock);
214 pthread_cond_destroy(&thread.cond_msg_avail);
215 pthread_cond_destroy(&thread.cond_msg_done);
216 pthread_cond_destroy(&thread.cond_queue_empty);
218 for (i = 0; i < QUEUE_SIZE; i++) {
219 video_thread_cmd *cmd = &thread.queue->queue[i];
221 cmd->cmd_list = NULL;
224 for (i = 0; i < QUEUE_SIZE; i++) {
225 video_thread_cmd *cmd = &thread.bg_queue->queue[i];
227 cmd->cmd_list = NULL;
231 static void video_thread_start() {
232 SysPrintf("Starting render thread\n");
234 thread.queue = &queues[0];
235 thread.bg_queue = &queues[1];
236 thread.running = TRUE;
238 if (pthread_cond_init(&thread.cond_msg_avail, NULL) ||
239 pthread_cond_init(&thread.cond_msg_done, NULL) ||
240 pthread_cond_init(&thread.cond_queue_empty, NULL) ||
241 pthread_mutex_init(&thread.queue_lock, NULL) ||
242 pthread_create(&thread.thread, NULL, video_thread_main, &thread)) {
249 SysPrintf("Failed to start rendering thread\n");
250 thread.running = FALSE;
254 static void video_thread_queue_cmd(uint32_t *list, int count, int last_cmd) {
255 video_thread_cmd *cmd;
257 video_thread_queue *queue;
260 cmd_list = (uint32_t *)calloc(count, sizeof(uint32_t));
263 /* Out of memory, disable the thread and run sync from now on */
264 SysPrintf("Failed to allocate render thread command list, stopping thread\n");
268 memcpy(cmd_list, list, count * sizeof(uint32_t));
270 if (hold_cmds && thread.bg_queue->used >= QUEUE_SIZE) {
271 /* If the bg queue is full, do a full sync to empty both queues
272 * and clear space. This should be very rare, I've only seen it in
273 * Tekken 3 post-battle-replay. */
278 queue = thread.bg_queue;
281 queue = thread.queue;
286 pthread_mutex_lock(&thread.queue_lock);
288 while (queue->used >= QUEUE_SIZE) {
289 pthread_cond_wait(&thread.cond_msg_done, &thread.queue_lock);
293 cmd = &queue->queue[queue->end];
295 cmd->cmd_list = cmd_list;
297 cmd->last_cmd = last_cmd;
298 queue->end = (queue->end + 1) % QUEUE_SIZE;
302 pthread_cond_signal(&thread.cond_msg_avail);
303 pthread_mutex_unlock(&thread.queue_lock);
307 /* Slice off just the part of the list that can be handled async, and
309 static int scan_cmd_list(uint32_t *data, int count,
310 int *cycles_sum_out, int *cycles_last, int *last_cmd)
312 int cpu_cycles_sum = 0, cpu_cycles = *cycles_last;
313 int cmd = 0, pos = 0, len, v;
315 while (pos < count) {
316 uint32_t *list = data + pos;
317 short *slist = (void *)list;
318 cmd = LE32TOH(list[0]) >> 24;
319 len = 1 + cmd_lengths[cmd];
323 gput_sum(cpu_cycles_sum, cpu_cycles,
324 gput_fill(LE16TOH(slist[4]) & 0x3ff,
325 LE16TOH(slist[5]) & 0x1ff));
328 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base());
331 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_t());
332 gpu.ex_regs[1] &= ~0x1ff;
333 gpu.ex_regs[1] |= LE32TOH(list[4]) & 0x1ff;
336 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base());
339 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_t());
340 gpu.ex_regs[1] &= ~0x1ff;
341 gpu.ex_regs[1] |= LE32TOH(list[4]) & 0x1ff;
344 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_g());
347 gput_sum(cpu_cycles_sum, cpu_cycles, gput_poly_base_gt());
348 gpu.ex_regs[1] &= ~0x1ff;
349 gpu.ex_regs[1] |= LE32TOH(list[5]) & 0x1ff;
352 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_g());
355 gput_sum(cpu_cycles_sum, cpu_cycles, gput_quad_base_gt());
356 gpu.ex_regs[1] &= ~0x1ff;
357 gpu.ex_regs[1] |= LE32TOH(list[5]) & 0x1ff;
360 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
363 for (v = 3; pos + v < count; v++)
365 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
366 if ((list[v] & 0xf000f000) == 0x50005000)
372 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
375 for (v = 4; pos + v < count; v += 2)
377 gput_sum(cpu_cycles_sum, cpu_cycles, gput_line(0));
378 if ((list[v] & 0xf000f000) == 0x50005000)
384 gput_sum(cpu_cycles_sum, cpu_cycles,
385 gput_sprite(LE16TOH(slist[4]) & 0x3ff,
386 LE16TOH(slist[5]) & 0x1ff));
389 gput_sum(cpu_cycles_sum, cpu_cycles,
390 gput_sprite(LE16TOH(slist[6]) & 0x3ff,
391 LE16TOH(slist[7]) & 0x1ff));
394 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(1, 1));
397 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(8, 8));
400 gput_sum(cpu_cycles_sum, cpu_cycles, gput_sprite(16, 16));
403 if ((cmd & 0xf8) == 0xe0)
404 gpu.ex_regs[cmd & 7] = list[0];
408 if (pos + len > count) {
410 break; /* incomplete cmd */
412 if (0x80 <= cmd && cmd <= 0xdf)
413 break; /* image i/o */
418 *cycles_sum_out += cpu_cycles_sum;
419 *cycles_last = cpu_cycles;
424 int do_cmd_list(uint32_t *list, int count,
425 int *cycles_sum, int *cycles_last, int *last_cmd)
429 if (thread.running) {
430 pos = scan_cmd_list(list, count, cycles_sum, cycles_last, last_cmd);
431 video_thread_queue_cmd(list, pos, *last_cmd);
433 pos = real_do_cmd_list(list, count, cycles_sum, cycles_last, last_cmd);
434 memcpy(gpu.ex_regs, gpu.scratch_ex_regs, sizeof(gpu.ex_regs));
439 int renderer_init(void) {
440 if (thread_rendering) {
441 video_thread_start();
443 return real_renderer_init();
446 void renderer_finish(void) {
447 real_renderer_finish();
449 if (thread_rendering && thread.running) {
454 void renderer_sync_ecmds(uint32_t * ecmds) {
455 if (thread.running) {
457 do_cmd_list(&ecmds[1], 6, &dummy, &dummy, &dummy);
459 real_renderer_sync_ecmds(ecmds);
463 void renderer_update_caches(int x, int y, int w, int h, int state_changed) {
465 real_renderer_update_caches(x, y, w, h, state_changed);
468 void renderer_flush_queues(void) {
469 /* Called during DMA and updateLace. We want to sync if it's DMA,
470 * but not if it's updateLace. Instead of syncing here, there's a
471 * renderer_sync call during DMA. */
472 real_renderer_flush_queues();
476 * Normally all GPU commands are processed before rendering the
477 * frame. For games that naturally run < 50/60fps, this is unnecessary
478 * -- it forces the game to render as if it was 60fps and leaves the
479 * GPU idle half the time on a 30fps game, for example.
481 * Allowing the renderer to wait until a frame is done before
482 * rendering it would give it double, triple, or quadruple the amount
483 * of time to finish before we have to wait for it.
485 * We can use a heuristic to figure out when to force a render.
487 * - If a frame isn't done when we're asked to render, wait for it and
488 * put future GPU commands in a separate buffer (for the next frame)
490 * - If the frame is done, and had no future GPU commands, render it.
492 * - If we do have future GPU commands, it meant the frame took too
493 * long to render and there's another frame waiting. Stop until the
494 * first frame finishes, render it, and start processing the next
497 * This may possibly add a frame or two of latency that shouldn't be
498 * different than the real device. It may skip rendering a frame
499 * entirely if a VRAM transfer happens while a frame is waiting, or in
500 * games that natively run at 60fps if frames are coming in too
501 * quickly to process. Depending on how the game treats "60fps," this
502 * may not be noticeable.
504 void renderer_notify_update_lace(int updated) {
505 if (!thread.running) return;
507 if (thread_rendering == THREAD_RENDERING_SYNC) {
517 pthread_mutex_lock(&thread.queue_lock);
518 if (thread.bg_queue->used || flushed) {
519 /* We have commands for a future frame to run. Force a wait until
520 * the current frame is finished, and start processing the next
521 * frame after it's drawn (see the `updated` clause above). */
522 pthread_mutex_unlock(&thread.queue_lock);
524 pthread_mutex_lock(&thread.queue_lock);
526 /* We are no longer holding commands back, so the next frame may
527 * get mixed into the following frame. This is usually fine, but can
528 * result in frameskip-like effects for 60fps games. */
531 needs_display = TRUE;
532 gpu.state.fb_dirty = TRUE;
533 } else if (thread.queue->used) {
534 /* We are still drawing during a vblank. Cut off the current frame
535 * by sending new commands to the background queue and skip
536 * drawing our partly rendered frame to the display. */
538 needs_display = TRUE;
539 gpu.state.fb_dirty = FALSE;
540 } else if (needs_display && !thread.queue->used) {
541 /* We have processed all commands in the queue, render the
542 * buffer. We know we have something to render, because
543 * needs_display is TRUE. */
545 needs_display = FALSE;
546 gpu.state.fb_dirty = TRUE;
548 /* Everything went normally, so do the normal thing. */
551 pthread_mutex_unlock(&thread.queue_lock);
554 void renderer_set_interlace(int enable, int is_odd) {
555 real_renderer_set_interlace(enable, is_odd);
558 void renderer_set_config(const struct rearmed_cbs *cbs) {
560 thread_rendering = cbs->thread_rendering;
561 if (!thread.running && thread_rendering != THREAD_RENDERING_OFF) {
562 video_thread_start();
563 } else if (thread.running && thread_rendering == THREAD_RENDERING_OFF) {
566 real_renderer_set_config(cbs);
569 void renderer_notify_res_change(void) {
571 real_renderer_notify_res_change();