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 ***************************************************************************/
23 #include "../gpulib/gpu.h"
24 #include "../../frontend/plugin_lib.h"
25 #include "gpulib_thread_if.h"
33 #define QUEUE_SIZE 0x2000
39 video_thread_cmd queue[QUEUE_SIZE];
44 pthread_mutex_t queue_lock;
45 pthread_cond_t cond_msg_avail;
46 pthread_cond_t cond_msg_done;
47 pthread_cond_t cond_queue_empty;
48 video_thread_queue *queue;
49 video_thread_queue *bg_queue;
53 static video_thread_state thread;
54 static video_thread_queue queues[2];
55 static int thread_rendering;
56 static bool hold_cmds;
57 static bool needs_display;
59 extern const unsigned char cmd_lengths[];
61 static void *video_thread_main(void *arg) {
62 video_thread_state *thread = (video_thread_state *)arg;
63 video_thread_cmd *cmd;
65 static int processed = 0;
68 int result, last_cmd, start, end;
69 video_thread_queue *queue;
70 pthread_mutex_lock(&thread->queue_lock);
72 while (!thread->queue->used && thread->running) {
73 pthread_cond_wait(&thread->cond_msg_avail, &thread->queue_lock);
76 if (!thread->running) {
77 pthread_mutex_unlock(&thread->queue_lock);
81 queue = thread->queue;
83 end = queue->end > queue->start ? queue->end : QUEUE_SIZE;
84 queue->start = end % QUEUE_SIZE;
85 pthread_mutex_unlock(&thread->queue_lock);
87 for (i = start; i < end; i++) {
88 cmd = &queue->queue[i];
89 result = real_do_cmd_list(cmd->cmd_list, cmd->count, &last_cmd);
91 if (result != cmd->count) {
92 fprintf(stderr, "Processed wrong cmd count: expected %d, got %d\n", cmd->count, result);
96 /* Periodically yield so as not to starve other threads */
97 processed += cmd->count;
98 if (processed >= 512) {
105 pthread_mutex_lock(&thread->queue_lock);
106 queue->used -= (end - start);
109 pthread_cond_signal(&thread->cond_queue_empty);
111 pthread_cond_signal(&thread->cond_msg_done);
112 pthread_mutex_unlock(&thread->queue_lock);
118 static void cmd_queue_swap() {
119 video_thread_queue *tmp;
120 if (!thread.bg_queue->used) return;
122 pthread_mutex_lock(&thread.queue_lock);
123 if (!thread.queue->used) {
125 thread.queue = thread.bg_queue;
126 thread.bg_queue = tmp;
127 needs_display = true;
128 pthread_cond_signal(&thread.cond_msg_avail);
130 pthread_mutex_unlock(&thread.queue_lock);
133 /* Waits for the main queue to completely finish. */
134 void renderer_wait() {
135 if (!thread.running) return;
137 /* Not completely safe, but should be fine since the render thread
138 * only decreases used, and we check again inside the lock. */
139 if (!thread.queue->used) {
143 pthread_mutex_lock(&thread.queue_lock);
145 while (thread.queue->used) {
146 pthread_cond_wait(&thread.cond_queue_empty, &thread.queue_lock);
149 pthread_mutex_unlock(&thread.queue_lock);
152 /* Waits for all GPU commands in both queues to finish, bringing VRAM
153 * completely up-to-date. */
154 void renderer_sync(void) {
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 && !thread.bg_queue->used) {
163 /* Flush both queues. This is necessary because gpulib could be
164 * trying to process a DMA write that a command in the queue should
165 * run beforehand. For example, Xenogears sprites write a black
166 * rectangle over the to-be-DMA'd spot in VRAM -- if this write
167 * happens after the DMA, it will clear the DMA, resulting in
168 * flickering sprites. We need to be totally up-to-date. This may
176 static void video_thread_stop() {
180 if (thread.running) {
181 thread.running = false;
182 pthread_cond_signal(&thread.cond_msg_avail);
183 pthread_join(thread.thread, NULL);
186 pthread_mutex_destroy(&thread.queue_lock);
187 pthread_cond_destroy(&thread.cond_msg_avail);
188 pthread_cond_destroy(&thread.cond_msg_done);
189 pthread_cond_destroy(&thread.cond_queue_empty);
191 for (i = 0; i < QUEUE_SIZE; i++) {
192 video_thread_cmd *cmd = &thread.queue->queue[i];
194 cmd->cmd_list = NULL;
197 for (i = 0; i < QUEUE_SIZE; i++) {
198 video_thread_cmd *cmd = &thread.bg_queue->queue[i];
200 cmd->cmd_list = NULL;
204 static void video_thread_start() {
205 fprintf(stdout, "Starting render thread\n");
207 if (pthread_cond_init(&thread.cond_msg_avail, NULL) ||
208 pthread_cond_init(&thread.cond_msg_done, NULL) ||
209 pthread_cond_init(&thread.cond_queue_empty, NULL) ||
210 pthread_mutex_init(&thread.queue_lock, NULL) ||
211 pthread_create(&thread.thread, NULL, video_thread_main, &thread)) {
215 thread.queue = &queues[0];
216 thread.bg_queue = &queues[1];
218 thread.running = true;
222 fprintf(stderr,"Failed to start rendering thread\n");
226 static void video_thread_queue_cmd(uint32_t *list, int count, int last_cmd) {
227 video_thread_cmd *cmd;
229 video_thread_queue *queue;
232 cmd_list = (uint32_t *)calloc(count, sizeof(uint32_t));
235 /* Out of memory, disable the thread and run sync from now on */
236 fprintf(stderr,"Failed to allocate render thread command list, stopping thread\n");
240 memcpy(cmd_list, list, count * sizeof(uint32_t));
242 if (hold_cmds && thread.bg_queue->used >= QUEUE_SIZE) {
243 /* If the bg queue is full, do a full sync to empty both queues
244 * and clear space. This should be very rare, I've only seen it in
245 * Tekken 3 post-battle-replay. */
250 queue = thread.bg_queue;
253 queue = thread.queue;
258 pthread_mutex_lock(&thread.queue_lock);
260 while (queue->used >= QUEUE_SIZE) {
261 pthread_cond_wait(&thread.cond_msg_done, &thread.queue_lock);
265 cmd = &queue->queue[queue->end];
267 cmd->cmd_list = cmd_list;
269 cmd->last_cmd = last_cmd;
270 queue->end = (queue->end + 1) % QUEUE_SIZE;
274 pthread_cond_signal(&thread.cond_msg_avail);
275 pthread_mutex_unlock(&thread.queue_lock);
279 /* Slice off just the part of the list that can be handled async, and
281 static int scan_cmd_list(uint32_t *data, int count, int *last_cmd)
283 int cmd = 0, pos = 0, len, v;
285 while (pos < count) {
286 uint32_t *list = data + pos;
288 len = 1 + cmd_lengths[cmd];
297 gpu.ex_regs[1] &= ~0x1ff;
298 gpu.ex_regs[1] |= list[4 + ((cmd >> 4) & 1)] & 0x1ff;
301 for (v = 3; pos + v < count; v++)
303 if ((list[v] & 0xf000f000) == 0x50005000)
309 for (v = 4; pos + v < count; v += 2)
311 if ((list[v] & 0xf000f000) == 0x50005000)
317 if ((cmd & 0xf8) == 0xe0)
318 gpu.ex_regs[cmd & 7] = list[0];
322 if (pos + len > count) {
324 break; /* incomplete cmd */
326 if (0xa0 <= cmd && cmd <= 0xdf)
327 break; /* image i/o */
336 int do_cmd_list(uint32_t *list, int count, int *last_cmd) {
339 if (thread.running) {
340 pos = scan_cmd_list(list, count, last_cmd);
341 video_thread_queue_cmd(list, pos, *last_cmd);
343 pos = real_do_cmd_list(list, count, last_cmd);
344 memcpy(gpu.ex_regs, gpu.scratch_ex_regs, sizeof(gpu.ex_regs));
349 int renderer_init(void) {
350 if (thread_rendering) {
351 video_thread_start();
353 return real_renderer_init();
356 void renderer_finish(void) {
357 real_renderer_finish();
359 if (thread_rendering && thread.running) {
364 void renderer_sync_ecmds(uint32_t * ecmds) {
365 if (thread.running) {
367 do_cmd_list(&ecmds[1], 6, &dummy);
369 real_renderer_sync_ecmds(ecmds);
373 void renderer_update_caches(int x, int y, int w, int h) {
375 real_renderer_update_caches(x, y, w, h);
378 void renderer_flush_queues(void) {
379 /* Called during DMA and updateLace. We want to sync if it's DMA,
380 * but not if it's updateLace. Instead of syncing here, there's a
381 * renderer_sync call during DMA. */
382 real_renderer_flush_queues();
386 * Normally all GPU commands are processed before rendering the
387 * frame. For games that naturally run < 50/60fps, this is unnecessary
388 * -- it forces the game to render as if it was 60fps and leaves the
389 * GPU idle half the time on a 30fps game, for example.
391 * Allowing the renderer to wait until a frame is done before
392 * rendering it would give it double, triple, or quadruple the amount
393 * of time to finish before we have to wait for it.
395 * We can use a heuristic to figure out when to force a render.
397 * - If a frame isn't done when we're asked to render, wait for it and
398 * put future GPU commands in a separate buffer (for the next frame)
400 * - If the frame is done, and had no future GPU commands, render it.
402 * - If we do have future GPU commands, it meant the frame took too
403 * long to render and there's another frame waiting. Stop until the
404 * first frame finishes, render it, and start processing the next
407 * This may possibly add a frame or two of latency that shouldn't be
408 * different than the real device. It may skip rendering a frame
409 * entirely if a VRAM transfer happens while a frame is waiting, or in
410 * games that natively run at 60fps if frames are coming in too
411 * quickly to process. Depending on how the game treats "60fps," this
412 * may not be noticeable.
414 void renderer_notify_update_lace(int updated) {
415 if (!thread.running) return;
417 if (thread_rendering == THREAD_RENDERING_SYNC) {
427 pthread_mutex_lock(&thread.queue_lock);
428 if (thread.bg_queue->used) {
429 /* We have commands for a future frame to run. Force a wait until
430 * the current frame is finished, and start processing the next
431 * frame after it's drawn (see the `updated` clause above). */
432 pthread_mutex_unlock(&thread.queue_lock);
434 pthread_mutex_lock(&thread.queue_lock);
436 /* We are no longer holding commands back, so the next frame may
437 * get mixed into the following frame. This is usually fine, but can
438 * result in frameskip-like effects for 60fps games. */
440 needs_display = true;
441 gpu.state.fb_dirty = true;
442 } else if (thread.queue->used) {
443 /* We are still drawing during a vblank. Cut off the current frame
444 * by sending new commands to the background queue and skip
445 * drawing our partly rendered frame to the display. */
447 needs_display = true;
448 gpu.state.fb_dirty = false;
449 } else if (needs_display && !thread.queue->used) {
450 /* We have processed all commands in the queue, render the
451 * buffer. We know we have something to render, because
452 * needs_display is true. */
454 needs_display = false;
455 gpu.state.fb_dirty = true;
457 /* Everything went normally, so do the normal thing. */
460 pthread_mutex_unlock(&thread.queue_lock);
463 void renderer_set_interlace(int enable, int is_odd) {
464 real_renderer_set_interlace(enable, is_odd);
467 void renderer_set_config(const struct rearmed_cbs *cbs) {
469 thread_rendering = cbs->thread_rendering;
470 if (!thread.running && thread_rendering != THREAD_RENDERING_OFF) {
471 video_thread_start();
472 } else if (thread.running && thread_rendering == THREAD_RENDERING_OFF) {
475 real_renderer_set_config(cbs);
478 void renderer_notify_res_change(void) {
480 real_renderer_notify_res_change();