Skip to content

Commit cdc3ebf

Browse files
committed
Updated applications to benefit from knowing the screen's pixel format
If the app/emulator is capable of generating the correct pixel format, at no performance cost to itself, then this change might result in improve performance during scaling/blitting because of less byte swapping. The following apps have been updated: MSX, GEN/MD, DOOM, GBC, LYNX, NES, PCE, SMS. The other apps are impossible to update without impacting performance, or just not worth doing it.
1 parent a07c289 commit cdc3ebf

File tree

13 files changed

+133
-40
lines changed

13 files changed

+133
-40
lines changed

components/retro-go/drivers/display/dummy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define LCD_SCREEN_BUFFER 0
1+
#define LCD_ACCESS_MODE 0 // 0=Windowed transactions, 1=Direct full framebuffer
22
#define LCD_BUFFER_LENGTH (RG_SCREEN_WIDTH * 4) // In pixels
33

44
static uint16_t lcd_buffer[LCD_BUFFER_LENGTH];

components/retro-go/drivers/display/ili9341.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define LCD_SCREEN_BUFFER 0
1+
#define LCD_ACCESS_MODE 0 // 0=Windowed transactions, 1=Direct full framebuffer
22
#define LCD_BUFFER_LENGTH (RG_SCREEN_WIDTH * 4) // In pixels
33

44
#include <freertos/FreeRTOS.h>

components/retro-go/drivers/display/ili9341_buffered.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define LCD_SCREEN_BUFFER 1
1+
#define LCD_ACCESS_MODE 1 // 0=Windowed transactions, 1=Direct full framebuffer
22
#define LCD_BUFFER_LENGTH (RG_SCREEN_WIDTH * 4) // In pixels
33

44
#include <freertos/FreeRTOS.h>

components/retro-go/drivers/display/sdl2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define LCD_SCREEN_BUFFER 1
1+
#define LCD_ACCESS_MODE 1 // 0=Windowed transactions, 1=Direct full framebuffer
22
#define LCD_BUFFER_LENGTH (RG_SCREEN_WIDTH * 4) // In pixels
33

44
#include <SDL2/SDL.h>

components/retro-go/rg_display.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include <stdlib.h>
55
#include <string.h>
66

7-
// #define LCD_SCREEN_BUFFER 0
7+
// #define LCD_ACCESS_MODE 0 // 0=Windowed transactions, 1=Direct full framebuffer
8+
// #define LCD_PIXEL_FORMAT RG_SCREEN_PIXEL_FORMAT
89
// #define LCD_BUFFER_LENGTH (RG_SCREEN_WIDTH * 4) // In pixels
910

1011
// static rg_display_driver_t driver;
@@ -34,11 +35,11 @@ static void lcd_sync(void);
3435
static void lcd_flip(void);
3536
static void lcd_set_rotation(int rotation);
3637
static void lcd_set_backlight(float percent);
37-
// When LCD_SCREEN_BUFFER == 0:
38+
// When LCD_ACCESS_MODE == 0:
3839
static void lcd_set_window(int left, int top, int width, int height);
3940
static inline uint16_t *lcd_get_buffer(size_t length);
4041
static inline void lcd_send_buffer(uint16_t *buffer, size_t length);
41-
// When LCD_SCREEN_BUFFER == 1:
42+
// When LCD_ACCESS_MODE == 1:
4243
static inline uint16_t *lcd_get_buffer_ptr(int left, int top);
4344

4445
#if RG_SCREEN_DRIVER == 0 || RG_SCREEN_DRIVER == 1 /* ILI9341/ST7789 */
@@ -142,7 +143,7 @@ static inline void write_update(const rg_surface_t *update)
142143

143144
const int screen_left = display.screen.margins.left + draw_left;
144145
const int screen_top = display.screen.margins.top + draw_top;
145-
const bool partial_update = RG_SCREEN_PARTIAL_UPDATES && !LCD_SCREEN_BUFFER;
146+
const bool partial_update = RG_SCREEN_PARTIAL_UPDATES && LCD_ACCESS_MODE == 0;
146147
// const bool interlace = false;
147148

148149
int lines_per_buffer = LCD_BUFFER_LENGTH / draw_width;
@@ -171,10 +172,10 @@ static inline void write_update(const rg_surface_t *update)
171172
LINE_IS_REPEATED(y + lines_to_copy)))
172173
--lines_to_copy;
173174
}
174-
#if LCD_SCREEN_BUFFER
175-
uint16_t *line_buffer = lcd_get_buffer_ptr(screen_left, screen_top + y);
176-
#else
175+
#if LCD_ACCESS_MODE == 0
177176
uint16_t *line_buffer = lcd_get_buffer(LCD_BUFFER_LENGTH);
177+
#else
178+
uint16_t *line_buffer = lcd_get_buffer_ptr(screen_left, screen_top + y);
178179
#endif
179180
uint16_t *line_buffer_ptr = line_buffer;
180181

@@ -262,7 +263,7 @@ static inline void write_update(const rg_surface_t *update)
262263
}
263264
}
264265

265-
#if !LCD_SCREEN_BUFFER
266+
#if LCD_ACCESS_MODE == 0
266267
size_t lines_to_send = 0;
267268
if (need_update)
268269
{
@@ -593,7 +594,7 @@ void rg_display_write_rect(int left, int top, int width, int height, int stride,
593594
const int screen_left = display.screen.margins.left + left;
594595
const int screen_top = display.screen.margins.top + top;
595596

596-
#if !LCD_SCREEN_BUFFER
597+
#if LCD_ACCESS_MODE == 0
597598
lcd_set_window(screen_left, screen_top, width, height);
598599

599600
for (size_t y = 0; y < height;)
@@ -645,7 +646,7 @@ void rg_display_clear_rect(int left, int top, int width, int height, uint16_t co
645646
#else /* 565_LE */
646647
const uint16_t color = color_le;
647648
#endif
648-
#if !LCD_SCREEN_BUFFER
649+
#if LCD_ACCESS_MODE == 0
649650
int pixels_remaining = width * height;
650651
if (pixels_remaining <= 0)
651652
return;

fmsx/main/main.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
#define AUDIO_SAMPLE_RATE (32000)
55
#define AUDIO_BUFFER_LENGTH (AUDIO_SAMPLE_RATE / 60 + 1)
66

7+
#if RG_SCREEN_PIXEL_FORMAT == 0
8+
#define FB_PIXEL_FORMAT RG_PIXEL_565_BE
9+
#else
10+
#define FB_PIXEL_FORMAT RG_PIXEL_565_LE
11+
#endif
12+
713
static rg_surface_t *updates[2];
814
static rg_surface_t *currentUpdate;
915
static rg_task_t *audioQueue;
@@ -212,7 +218,9 @@ int InitMachine(void)
212218
for (int J = 0; J < 256; J++)
213219
{
214220
uint16_t color = C_RGB(((J >> 2) & 0x07) * 255 / 7, ((J >> 5) & 0x07) * 255 / 7, (J & 0x03) * 255 / 3);
215-
BPal[J] = ((color >> 8) | (color << 8)) & 0xFFFF;
221+
if (FB_PIXEL_FORMAT == RG_PIXEL_565_BE)
222+
color = (color >> 8) | (color << 8);
223+
BPal[J] = color;
216224
}
217225

218226
InitSound(AUDIO_SAMPLE_RATE, 150);
@@ -232,7 +240,8 @@ void TrashMachine(void)
232240
void SetColor(byte N, byte R, byte G, byte B)
233241
{
234242
uint16_t color = C_RGB(R, G, B);
235-
color = (color >> 8) | (color << 8);
243+
if (FB_PIXEL_FORMAT == RG_PIXEL_565_BE)
244+
color = (color >> 8) | (color << 8);
236245
if (N)
237246
XPal[N] = color;
238247
else
@@ -434,8 +443,8 @@ void app_main(void)
434443
},
435444
});
436445

437-
updates[0] = rg_surface_create(WIDTH, HEIGHT, RG_PIXEL_565_BE, MEM_FAST);
438-
updates[1] = rg_surface_create(WIDTH, HEIGHT, RG_PIXEL_565_BE, MEM_FAST);
446+
updates[0] = rg_surface_create(WIDTH, HEIGHT, FB_PIXEL_FORMAT , MEM_FAST);
447+
updates[1] = rg_surface_create(WIDTH, HEIGHT, FB_PIXEL_FORMAT , MEM_FAST);
439448
currentUpdate = updates[0];
440449

441450
KeyboardEmulation = rg_settings_get_number(NS_APP, "Input", 1);

gwenesis/main/main.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
#define AUDIO_SAMPLE_RATE (53267)
1010
#define AUDIO_BUFFER_LENGTH (AUDIO_SAMPLE_RATE / 60 + 1)
1111

12+
#if RG_SCREEN_PIXEL_FORMAT == 0
13+
#define FB_PIXEL_FORMAT RG_PIXEL_PAL565_BE
14+
#else
15+
#define FB_PIXEL_FORMAT RG_PIXEL_PAL565_LE
16+
#endif
17+
1218
extern unsigned char* VRAM;
1319
extern int zclk;
1420
int system_clock;
@@ -270,8 +276,8 @@ void app_main(void)
270276
sn76489_enabled = rg_settings_get_number(NS_APP, SETTING_SN76489_EMULATION, 0);
271277
z80_enabled = rg_settings_get_number(NS_APP, SETTING_Z80_EMULATION, 1);
272278

273-
updates[0] = rg_surface_create(320, 241, RG_PIXEL_PAL565_BE, MEM_FAST);
274-
// updates[1] = rg_surface_create(320, 241, RG_PIXEL_PAL565_BE, MEM_FAST);
279+
updates[0] = rg_surface_create(320, 241, FB_PIXEL_FORMAT , MEM_FAST);
280+
// updates[1] = rg_surface_create(320, 241, FB_PIXEL_FORMAT , MEM_FAST);
275281
currentUpdate = updates[0];
276282

277283
// This is a hack because our new surface format doesn't yet support overdraw space easily
@@ -468,8 +474,15 @@ void app_main(void)
468474

469475
if (drawFrame)
470476
{
471-
for (int i = 0; i < 256; ++i)
472-
currentUpdate->palette[i] = (CRAM565[i] << 8) | (CRAM565[i] >> 8);
477+
if (FB_PIXEL_FORMAT == RG_PIXEL_PAL565_BE)
478+
{
479+
for (int i = 0; i < 256; ++i)
480+
currentUpdate->palette[i] = (CRAM565[i] << 8) | (CRAM565[i] >> 8);
481+
}
482+
else
483+
{
484+
memcpy(currentUpdate->palette, CRAM565, 512);
485+
}
473486
currentUpdate->width = screen_width;
474487
currentUpdate->height = screen_height;
475488
slowFrame = rg_display_is_busy(); // Previous frame is still not done, hence slowFrame...

prboom-go/main/main.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@
4343
#include <rg_system.h>
4444

4545
#define AUDIO_SAMPLE_RATE 22050
46-
4746
#define AUDIO_BUFFER_LENGTH (AUDIO_SAMPLE_RATE / TICRATE + 1)
4847
#define NUM_MIX_CHANNELS 8
4948

49+
#if RG_SCREEN_PIXEL_FORMAT == 0
50+
#define FB_PIXEL_FORMAT RG_PIXEL_PAL565_BE
51+
#else
52+
#define FB_PIXEL_FORMAT RG_PIXEL_PAL565_LE
53+
#endif
54+
5055
static rg_surface_t *update;
5156
static rg_app_t *app;
5257

@@ -157,7 +162,12 @@ void I_SetPalette(int pal)
157162
{
158163
uint16_t *palette = V_BuildPalette(pal, 16);
159164
for (int i = 0; i < 256; i++)
160-
update->palette[i] = palette[i] << 8 | palette[i] >> 8;
165+
{
166+
uint16_t color = palette[i];
167+
if (FB_PIXEL_FORMAT == RG_PIXEL_PAL565_BE)
168+
color = (color << 8) | (color >> 8);
169+
update->palette[i] = color;
170+
}
161171
Z_Free(palette);
162172
current_palette = pal;
163173
}
@@ -560,7 +570,7 @@ void app_main()
560570
SCREENWIDTH = RG_MIN(rg_display_get_width(), MAX_SCREENWIDTH);
561571
SCREENHEIGHT = RG_MIN(rg_display_get_height(), MAX_SCREENHEIGHT);
562572

563-
update = rg_surface_create(SCREENWIDTH, SCREENHEIGHT, RG_PIXEL_PAL565_BE, MEM_FAST);
573+
update = rg_surface_create(SCREENWIDTH, SCREENHEIGHT, FB_PIXEL_FORMAT, MEM_FAST);
564574

565575
const char *iwad = NULL;
566576
const char *pwad = NULL;

retro-core/main/main_gbc.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
#include <sys/time.h>
44
#include <gnuboy.h>
55

6+
// #define AUDIO_SAMPLE_RATE (32000)
7+
// #define AUDIO_BUFFER_LENGTH (AUDIO_SAMPLE_RATE / 50 + 1)
8+
9+
#if RG_SCREEN_PIXEL_FORMAT == 0
10+
#define FB_PIXEL_FORMAT RG_PIXEL_565_BE
11+
#else
12+
#define FB_PIXEL_FORMAT RG_PIXEL_565_LE
13+
#endif
14+
615
static int skipFrames = 0;
716
static bool slowFrame = false;
817

@@ -254,8 +263,10 @@ void gbc_main(void)
254263

255264
app = rg_system_reinit(AUDIO_SAMPLE_RATE, &handlers, NULL);
256265

257-
updates[0] = rg_surface_create(GB_WIDTH, GB_HEIGHT, RG_PIXEL_565_BE, MEM_ANY);
258-
updates[1] = rg_surface_create(GB_WIDTH, GB_HEIGHT, RG_PIXEL_565_BE, MEM_ANY);
266+
bool d565be = rg_display_get_info()->screen.format == RG_PIXEL_565_BE;
267+
268+
updates[0] = rg_surface_create(GB_WIDTH, GB_HEIGHT, FB_PIXEL_FORMAT , MEM_ANY);
269+
updates[1] = rg_surface_create(GB_WIDTH, GB_HEIGHT, FB_PIXEL_FORMAT , MEM_ANY);
259270
currentUpdate = updates[0];
260271

261272
useSystemTime = (bool)rg_settings_get_number(NS_APP, SETTING_SYSTIME, 1);
@@ -267,7 +278,8 @@ void gbc_main(void)
267278
RG_LOGE("Unable to create SRAM folder...");
268279

269280
// Initialize the emulator
270-
if (gnuboy_init(app->sampleRate, GB_AUDIO_STEREO_S16, GB_PIXEL_565_BE, &video_callback, &audio_callback) < 0)
281+
const gb_video_fmt_t video_fmt = FB_PIXEL_FORMAT == RG_PIXEL_565_BE ? GB_PIXEL_565_BE : GB_PIXEL_565_LE;
282+
if (gnuboy_init(app->sampleRate, GB_AUDIO_STEREO_S16, video_fmt, &video_callback, &audio_callback) < 0)
271283
RG_PANIC("Emulator init failed!");
272284

273285
gnuboy_set_framebuffer(currentUpdate->data);

retro-core/main/main_lynx.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ extern "C" {
77

88
#include <handy.h>
99

10+
// #define AUDIO_SAMPLE_RATE (32000)
11+
// #define AUDIO_BUFFER_LENGTH (AUDIO_SAMPLE_RATE / 50 + 1)
12+
13+
#if RG_SCREEN_PIXEL_FORMAT == 0
14+
#define FB_PIXEL_FORMAT RG_PIXEL_565_BE
15+
#else
16+
#define FB_PIXEL_FORMAT RG_PIXEL_565_LE
17+
#endif
18+
1019
static CSystem *lynx = NULL;
1120

1221
static int dpad_mapped_up;
@@ -89,17 +98,18 @@ static void set_display_mode(void)
8998

9099
static CSystem *new_lynx(void)
91100
{
101+
long displayformat = FB_PIXEL_FORMAT == RG_PIXEL_565_BE ? MIKIE_PIXEL_FORMAT_16BPP_565_BE : MIKIE_PIXEL_FORMAT_16BPP_565;
92102
if (rg_extension_match(app->romPath, "zip"))
93103
{
94104
void *data;
95105
size_t size;
96106
if (!rg_storage_unzip_file(app->romPath, NULL, &data, &size, 0))
97107
RG_PANIC("ROM file unzipping failed!");
98-
CSystem *lynx = new CSystem((UBYTE*)data, size, MIKIE_PIXEL_FORMAT_16BPP_565_BE, app->sampleRate);
108+
CSystem *lynx = new CSystem((UBYTE*)data, size, displayformat, app->sampleRate);
99109
free(data);
100110
return lynx;
101111
}
102-
return new CSystem(app->romPath, MIKIE_PIXEL_FORMAT_16BPP_565_BE, app->sampleRate);
112+
return new CSystem(app->romPath, displayformat, app->sampleRate);
103113
}
104114

105115

@@ -202,8 +212,8 @@ extern "C" void lynx_main(void)
202212
app = rg_system_reinit(AUDIO_SAMPLE_RATE, &handlers, NULL);
203213

204214
// the HANDY_SCREEN_WIDTH * HANDY_SCREEN_WIDTH is deliberate because of rotation
205-
updates[0] = rg_surface_create(HANDY_SCREEN_WIDTH, HANDY_SCREEN_WIDTH, RG_PIXEL_565_BE, MEM_FAST);
206-
updates[1] = rg_surface_create(HANDY_SCREEN_WIDTH, HANDY_SCREEN_WIDTH, RG_PIXEL_565_BE, MEM_FAST);
215+
updates[0] = rg_surface_create(HANDY_SCREEN_WIDTH, HANDY_SCREEN_WIDTH, FB_PIXEL_FORMAT , MEM_FAST);
216+
updates[1] = rg_surface_create(HANDY_SCREEN_WIDTH, HANDY_SCREEN_WIDTH, FB_PIXEL_FORMAT , MEM_FAST);
207217
currentUpdate = updates[0];
208218

209219
// Init emulator

0 commit comments

Comments
 (0)