]> git.sesse.net Git - pistorm/blob - platforms/amiga/rtg/rtg-output-raylib.c
Rework RTG WaitVerticalSync a bit
[pistorm] / platforms / amiga / rtg / rtg-output-raylib.c
1 // SPDX-License-Identifier: MIT
2
3 #include "config_file/config_file.h"
4 #include "emulator.h"
5 #include "rtg.h"
6
7 #include "raylib/raylib.h"
8
9 #include <pthread.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #define RTG_INIT_ERR(a) { printf(a); *data->running = 0; }
17
18 //#define DEBUG_RAYLIB_RTG
19
20 #ifdef DEBUG_RAYLIB_RTG
21 #define DEBUG printf
22 #else
23 #define DEBUG(...)
24 #endif
25
26 uint8_t busy = 0, rtg_on = 0, rtg_initialized = 0, emulator_exiting = 0, rtg_output_in_vblank = 0;
27 extern uint8_t *rtg_mem, display_enabled;
28 extern uint32_t framebuffer_addr;
29 extern uint32_t framebuffer_addr_adj;
30
31 extern uint16_t rtg_display_width, rtg_display_height;
32 extern uint16_t rtg_display_format;
33 extern uint16_t rtg_pitch, rtg_total_rows;
34 extern uint16_t rtg_offset_x, rtg_offset_y;
35
36 uint32_t cur_rtg_frame = 0;
37
38 static pthread_t thread_id;
39 static uint8_t mouse_cursor_enabled = 0, cursor_image_updated = 0, updating_screen = 0, debug_palette = 0, show_fps = 0;
40 static uint8_t mouse_cursor_w = 16, mouse_cursor_h = 16;
41 static int16_t mouse_cursor_x = 0, mouse_cursor_y = 0;
42
43 struct rtg_shared_data {
44     uint16_t *width, *height;
45     uint16_t *format, *pitch;
46     uint16_t *offset_x, *offset_y;
47     uint8_t *memory;
48     uint32_t *addr;
49     uint8_t *running;
50 };
51
52 struct rtg_shared_data rtg_share_data;
53 static uint32_t palette[256];
54 static uint32_t cursor_palette[256];
55
56 uint32_t cursor_data[256 * 256];
57
58 void rtg_update_screen() {}
59
60 uint32_t rtg_to_raylib[RTGFMT_NUM] = {
61     PIXELFORMAT_UNCOMPRESSED_GRAYSCALE,
62     PIXELFORMAT_UNCOMPRESSED_R5G6B5,
63     PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
64     PIXELFORMAT_UNCOMPRESSED_R5G5B5A1,
65 };
66
67 uint32_t rtg_pixel_size[RTGFMT_NUM] = {
68     1,
69     2,
70     4,
71     2,
72 };
73
74 void *rtgThread(void *args) {
75
76     printf("RTG thread running\n");
77     fflush(stdout);
78
79     int reinit = 0;
80     rtg_on = 1;
81
82     uint32_t *indexed_buf = NULL;
83
84     rtg_share_data.format = &rtg_display_format;
85     rtg_share_data.width = &rtg_display_width;
86     rtg_share_data.height = &rtg_display_height;
87     rtg_share_data.pitch = &rtg_pitch;
88     rtg_share_data.offset_x = &rtg_offset_x;
89     rtg_share_data.offset_y = &rtg_offset_y;
90     rtg_share_data.memory = rtg_mem;
91     rtg_share_data.running = &rtg_on;
92     rtg_share_data.addr = &framebuffer_addr_adj;
93     struct rtg_shared_data *data = &rtg_share_data;
94
95     uint16_t width = rtg_display_width;
96     uint16_t height = rtg_display_height;
97     uint16_t format = rtg_display_format;
98     uint16_t pitch = rtg_pitch;
99
100     Texture raylib_texture, raylib_cursor_texture;
101     Texture raylib_clut_texture;
102     Image raylib_fb, raylib_cursor, raylib_clut;
103
104     InitWindow(GetScreenWidth(), GetScreenHeight(), "Pistorm RTG");
105     HideCursor();
106     SetTargetFPS(60);
107
108         Color bef = { 0, 64, 128, 255 };
109     float scale_x = 1.0f, scale_y = 1.0f;
110
111     Shader clut_shader = LoadShader(NULL, "platforms/amiga/rtg/clut.shader");
112     Shader swizzle_shader = LoadShader(NULL, "platforms/amiga/rtg/argbswizzle.shader");
113     int clut_loc = GetShaderLocation(clut_shader, "texture1");
114
115     raylib_clut.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
116     raylib_clut.width = 256;
117     raylib_clut.height = 1;
118     raylib_clut.mipmaps = 1;
119     raylib_clut.data = palette;
120
121     raylib_clut_texture = LoadTextureFromImage(raylib_clut);
122
123     raylib_cursor.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
124     raylib_cursor.width = 256;
125     raylib_cursor.height = 256;
126     raylib_cursor.mipmaps = 1;
127     raylib_cursor.data = cursor_data;
128     raylib_cursor_texture = LoadTextureFromImage(raylib_cursor);
129
130     Rectangle srcrect, dstscale;
131     Vector2 origin;
132
133 reinit_raylib:;
134     if (reinit) {
135         printf("Reinitializing raylib...\n");
136         width = rtg_display_width;
137         height = rtg_display_height;
138         format = rtg_display_format;
139         pitch = rtg_pitch;
140         if (indexed_buf) {
141             free(indexed_buf);
142             indexed_buf = NULL;
143         }
144         UnloadTexture(raylib_texture);
145         reinit = 0;
146     }
147
148     printf("Creating %dx%d raylib window...\n", width, height);
149
150     printf("Setting up raylib framebuffer image.\n");
151     raylib_fb.format = rtg_to_raylib[format];
152
153     switch (format) {
154         case RTGFMT_RBG565:
155             raylib_fb.width = width;
156             indexed_buf = calloc(1, width * height * 2);
157             break;
158         default:
159             raylib_fb.width = pitch / rtg_pixel_size[format];
160             break;
161     }
162     raylib_fb.height = height;
163         raylib_fb.mipmaps = 1;
164         raylib_fb.data = &data->memory[*data->addr];
165     printf("Width: %d\nPitch: %d\nBPP: %d\n", raylib_fb.width, pitch, rtg_pixel_size[format]);
166
167     raylib_texture = LoadTextureFromImage(raylib_fb);
168
169     printf("Loaded framebuffer texture.\n");
170
171     srcrect.x = srcrect.y = 0;
172     srcrect.width = width;
173     srcrect.height = height;
174     dstscale.x = dstscale.y = 0;
175     dstscale.width = width;
176     dstscale.height = height;
177
178     if (dstscale.height * 2 <= GetScreenHeight()) {
179         if (width == 320) {
180             if (GetScreenHeight() == 720) {
181                 dstscale.width = 960;
182                 dstscale.height = 720;
183             } else if (GetScreenHeight() == 1080) {
184                 dstscale.width = 1440;
185                 dstscale.height = 1080;
186             } else if (GetScreenHeight() == 1200) {
187                 dstscale.width = 1600;
188                 dstscale.height = 1200;
189             }
190         } else {
191             while (dstscale.height + height <= GetScreenHeight()) {
192                 dstscale.height += height;
193                 dstscale.width += width;
194             }
195         }
196     } else if (dstscale.width > GetScreenWidth() || dstscale.height > GetScreenHeight()) {
197         if (dstscale.height > GetScreenHeight()) {
198             DEBUG("[H > SH]\n");
199             DEBUG("Adjusted width from %d to", (int)dstscale.width);
200             dstscale.width = dstscale.width * ((float)GetScreenHeight() / (float)height);
201             DEBUG("%d.\n", (int)dstscale.width);
202             DEBUG("Adjusted height from %d to", (int)dstscale.height);
203             dstscale.height = GetScreenHeight();
204             DEBUG("%d.\n", (int)dstscale.height);
205         }
206         if (dstscale.width > GetScreenWidth()) {
207             // First scaling attempt failed, do not double adjust, re-adjust
208             dstscale.width = width;
209             dstscale.height = height;
210             DEBUG("[W > SW]\n");
211             DEBUG("Adjusted height from %d to", (int)dstscale.height);
212             dstscale.height = dstscale.height * ((float)GetScreenWidth() / (float)width);
213             DEBUG("%d.\n", (int)dstscale.height);
214             DEBUG("Adjusted width from %d to", (int)dstscale.width);
215             dstscale.width = GetScreenWidth();
216             DEBUG("%d.\n", (int)dstscale.width);
217         }
218     }
219
220     scale_x = dstscale.width / (float)width;
221     scale_y = dstscale.height / (float)height;
222
223     origin.x = (dstscale.width - GetScreenWidth()) * 0.5;
224     origin.y = (dstscale.height - GetScreenHeight()) * 0.5;
225
226     while (1) {
227         if (rtg_on) {
228             BeginDrawing();
229             rtg_output_in_vblank = 0;
230             updating_screen = 1;
231
232             switch (format) {
233                 case RTGFMT_8BIT:
234                     UpdateTexture(raylib_clut_texture, palette);
235                     BeginShaderMode(clut_shader);
236                     SetShaderValueTexture(clut_shader, clut_loc, raylib_clut_texture);
237                     break;
238                 case RTGFMT_RGB32:
239                     BeginShaderMode(swizzle_shader);
240                     break;
241             }
242             
243             DrawTexturePro(raylib_texture, srcrect, dstscale, origin, 0.0f, RAYWHITE);
244
245             switch (format) {
246                 case RTGFMT_8BIT:
247                 case RTGFMT_RGB32:
248                     EndShaderMode();
249                     break;
250             }
251
252             if (mouse_cursor_enabled) {
253                 float mc_x = mouse_cursor_x - rtg_offset_x;
254                 float mc_y = mouse_cursor_y - rtg_offset_y;
255                 Rectangle cursor_srcrect = { 0, 0, mouse_cursor_w, mouse_cursor_h };
256                 Rectangle dstrect = { mc_x * scale_x, mc_y * scale_y, (float)mouse_cursor_w * scale_x, (float)mouse_cursor_h * scale_y };
257                 DrawTexturePro(raylib_cursor_texture, cursor_srcrect, dstrect, origin, 0.0f, RAYWHITE);
258             }
259
260             if (debug_palette) {
261                 if (format == RTGFMT_8BIT) {
262                     Rectangle srcrect = { 0, 0, 256, 1 };
263                     Rectangle dstrect = { 0, 0, 1024, 8 };
264                     DrawTexturePro(raylib_clut_texture, srcrect, dstrect, origin, 0.0f, RAYWHITE);
265                 }
266             }
267
268             if (show_fps) {
269                 DrawFPS(GetScreenWidth() - 128, 0);
270             }
271
272             EndDrawing();
273             rtg_output_in_vblank = 1;
274             cur_rtg_frame++;
275             if (format == RTGFMT_RBG565) {
276                 for (int y = 0; y < height; y++) {
277                     for (int x = 0; x < width; x++) {
278                         ((uint16_t *)indexed_buf)[x + (y * width)] = be16toh(((uint16_t *)data->memory)[(*data->addr / 2) + x + (y * (pitch / 2))]);
279                     }
280                 }
281                 UpdateTexture(raylib_texture, indexed_buf);
282             }
283             else {
284                 UpdateTexture(raylib_texture, &data->memory[*data->addr]);
285             }
286             if (cursor_image_updated) {
287                 UpdateTexture(raylib_cursor_texture, cursor_data);
288                 cursor_image_updated = 0;
289             }
290             updating_screen = 0;
291         } else {
292             BeginDrawing();
293             ClearBackground(bef);
294             DrawText("RTG is currently sleeping.", 16, 16, 12, RAYWHITE);
295             EndDrawing();
296         }
297         if (pitch != *data->pitch || height != *data->height || width != *data->width || format != *data->format) {
298             printf("Reinitializing due to something change.\n");
299             reinit = 1;
300             goto shutdown_raylib;
301         }
302         if (emulator_exiting) {
303             goto shutdown_raylib;
304         }
305     }
306
307     rtg_initialized = 0;
308     printf("RTG thread shut down.\n");
309
310 shutdown_raylib:;
311
312     if (reinit)
313         goto reinit_raylib;
314
315     if (indexed_buf)
316         free(indexed_buf);
317
318     UnloadTexture(raylib_texture);
319     UnloadShader(clut_shader);
320
321     CloseWindow();
322
323     return args;
324 }
325
326 void rtg_set_clut_entry(uint8_t index, uint32_t xrgb) {
327     //palette[index] = xrgb;
328     unsigned char *src = (unsigned char *)&xrgb;
329     unsigned char *dst = (unsigned char *)&palette[index];
330     dst[0] = src[2];
331     dst[1] = src[1];
332     dst[2] = src[0];
333     dst[3] = 0xFF;
334 }
335
336 void rtg_init_display() {
337     int err;
338     rtg_on = 1;
339
340     if (!rtg_initialized) {
341         err = pthread_create(&thread_id, NULL, &rtgThread, (void *)&rtg_share_data);
342         if (err != 0) {
343             rtg_on = 0;
344             display_enabled = 0xFF;
345             printf("can't create RTG thread :[%s]", strerror(err));
346         }
347         else {
348             rtg_initialized = 1;
349             pthread_setname_np(thread_id, "pistorm: rtg");
350             printf("RTG Thread created successfully\n");
351         }
352     }
353     printf("RTG display enabled.\n");
354 }
355
356 void rtg_shutdown_display() {
357     printf("RTG display disabled.\n");
358     rtg_on = 0;
359     display_enabled = 0xFF;
360 }
361
362 void rtg_enable_mouse_cursor() {
363     mouse_cursor_enabled = 1;
364 }
365
366 void rtg_set_mouse_cursor_pos(int16_t x, int16_t y) {
367     mouse_cursor_x = x;
368     mouse_cursor_y = y;
369     //printf("Set mouse cursor pos to %d, %d.\n", x, y);
370 }
371
372 static uint8_t clut_cursor_data[256*256];
373
374 void update_mouse_cursor(uint8_t *src) {
375     if (src != NULL) {
376         memset(clut_cursor_data, 0x00, 256*256);
377         uint8_t cur_bit = 0x80;
378         uint8_t line_pitch = (mouse_cursor_w / 8) * 2;
379
380         for (uint8_t y = 0; y < mouse_cursor_h; y++) {
381             for (uint8_t x = 0; x < mouse_cursor_w; x++) {
382                 if (src[(x / 8) + (line_pitch * y)] & cur_bit)
383                     clut_cursor_data[x + (y * 256)] |= 0x01;
384                 if (src[(x / 8) + (line_pitch * y) + (mouse_cursor_w / 8)] & cur_bit)
385                     clut_cursor_data[x + (y * 256)] |= 0x02;
386                 cur_bit >>= 1;
387                 if (cur_bit == 0x00)
388                     cur_bit = 0x80;
389             }
390             cur_bit = 0x80;
391         }
392     }
393
394     for (int y = 0; y < mouse_cursor_h; y++) {
395         for (int x = 0; x < mouse_cursor_w; x++) {
396             cursor_data[x + (y * 256)] = cursor_palette[clut_cursor_data[x + (y * 256)]];
397         }
398     }
399
400     while (rtg_on && !updating_screen)
401         usleep(0);
402     cursor_image_updated = 1;
403 }
404
405 void rtg_set_cursor_clut_entry(uint8_t r, uint8_t g, uint8_t b, uint8_t idx) {
406     uint32_t color = 0;
407     unsigned char *dst = (unsigned char *)&color;
408
409     dst[0] = r;
410     dst[1] = g;
411     dst[2] = b;
412     dst[3] = 0xFF;
413     if (cursor_palette[idx + 1] != color) {
414         cursor_palette[0] = 0;
415         cursor_palette[idx + 1] = color;
416         update_mouse_cursor(NULL);
417     }
418 }
419
420 static uint8_t old_mouse_w, old_mouse_h;
421 static uint8_t old_mouse_data[256];
422
423 void rtg_set_mouse_cursor_image(uint8_t *src, uint8_t w, uint8_t h) {
424     uint8_t new_cursor_data = 0;
425
426     mouse_cursor_w = w;
427     mouse_cursor_h = h;
428
429     if (memcmp(src, old_mouse_data, (w / 8 * h)) != 0)
430         new_cursor_data = 1;
431
432     if (old_mouse_w != w || old_mouse_h != h || new_cursor_data) {
433         old_mouse_w = w;
434         old_mouse_h = h;
435         update_mouse_cursor(src);
436     }
437 }
438
439 void rtg_show_fps(uint8_t enable) {
440     show_fps = (enable != 0);
441 }
442
443 void rtg_palette_debug(uint8_t enable) {
444     debug_palette = (enable != 0);
445 }