Skip to content

Commit f0a043b

Browse files
authored
[Platform/RGFW] add support for software rendering (#5773)
* add support for software rendering * null check on freeing * add back line * rename framebuffer to surface * add guard on free * update makefile to prevent software on web * update for mac
1 parent dde8c8e commit f0a043b

2 files changed

Lines changed: 186 additions & 10 deletions

File tree

src/Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,20 @@ ifeq ($(TARGET_PLATFORM),PLATFORM_DRM)
275275
GRAPHICS ?= GRAPHICS_API_OPENGL_ES2
276276
#GRAPHICS = GRAPHICS_API_OPENGL_SOFTWARE # Uncomment to use software rendering
277277
endif
278-
ifeq ($(TARGET_PLATFORM),$(filter $(TARGET_PLATFORM),PLATFORM_WEB PLATFORM_WEB_RGFW))
278+
ifeq ($(TARGET_PLATFORM),PLATFORM_WEB)
279279
# On HTML5 OpenGL ES 2.0 is used, emscripten translates it to WebGL 1.0
280280
GRAPHICS ?= GRAPHICS_API_OPENGL_ES2
281281
#GRAPHICS = GRAPHICS_API_OPENGL_ES3
282282
endif
283+
ifeq ($(TARGET_PLATFORM),PLATFORM_WEB_RGFW)
284+
# On HTML5 OpenGL ES 2.0 is used, emscripten translates it to WebGL 1.0
285+
GRAPHICS ?= GRAPHICS_API_OPENGL_ES2
286+
#GRAPHICS = GRAPHICS_API_OPENGL_ES3
287+
288+
ifeq ($(GRAPHICS),GRAPHICS_API_OPENGL_SOFTWARE)
289+
$(error WEB_RGFW: Software rendering not supported!)
290+
endif
291+
endif
283292
ifeq ($(TARGET_PLATFORM),PLATFORM_ANDROID)
284293
# By default use OpenGL ES 2.0 on Android
285294
GRAPHICS ?= GRAPHICS_API_OPENGL_ES2

src/platforms/rcore_desktop_rgfw.c

Lines changed: 176 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ typedef struct {
177177
RGFW_window *window; // Native display device (physical screen connection)
178178
RGFW_monitor *monitor;
179179
mg_gamepads minigamepad;
180+
181+
#if defined(GRAPHICS_API_OPENGL_SOFTWARE)
182+
RGFW_surface *surface;
183+
u8 *surfacePixels;
184+
i32 surfaceWidth;
185+
i32 surfaceHeight;
186+
#endif
180187
} PlatformData;
181188

182189
//----------------------------------------------------------------------------------
@@ -1121,7 +1128,35 @@ void DisableCursor(void)
11211128
// Swap back buffer with front buffer (screen drawing)
11221129
void SwapScreenBuffer(void)
11231130
{
1124-
RGFW_window_swapBuffers_OpenGL(platform.window);
1131+
#if defined(GRAPHICS_API_OPENGL_SOFTWARE)
1132+
{
1133+
if (platform.surface)
1134+
{
1135+
// copy rlsw pixel data to the surface framebuffer
1136+
swReadPixels(0, 0, platform.surfaceWidth, platform.surfaceHeight, SW_RGBA, SW_UNSIGNED_BYTE, platform.surfacePixels);
1137+
1138+
// Mac wants a different pixel order. I cant seem to get this to work any other way
1139+
#if defined(__APPLE__)
1140+
unsigned char temp = 0;
1141+
unsigned char *p = NULL;
1142+
for (int i = 0; i < (platform.surfaceWidth * platform.surfaceHeight); i += 1)
1143+
{
1144+
p = platform.surfacePixels + (i * 4);
1145+
temp = p[0];
1146+
p[0] = p[2];
1147+
p[2] = temp;
1148+
}
1149+
#endif
1150+
1151+
// blit surface to the window
1152+
RGFW_window_blitSurface(platform.window, platform.surface);
1153+
}
1154+
}
1155+
#else
1156+
{
1157+
RGFW_window_swapBuffers_OpenGL(platform.window);
1158+
}
1159+
#endif
11251160
}
11261161

11271162
//----------------------------------------------------------------------------------
@@ -1315,6 +1350,9 @@ void PollInputEvents(void)
13151350
// Window events are also polled (Minimized, maximized, close...)
13161351
case RGFW_windowResized:
13171352
{
1353+
// set flag that the window was resized
1354+
CORE.Window.resizedLastFrame = true;
1355+
13181356
#if defined(__APPLE__)
13191357
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
13201358
{
@@ -1363,7 +1401,42 @@ void PollInputEvents(void)
13631401
CORE.Window.currentFbo.width = CORE.Window.screen.width;
13641402
CORE.Window.currentFbo.height = CORE.Window.screen.height;
13651403
#endif
1366-
CORE.Window.resizedLastFrame = true;
1404+
1405+
#if defined(GRAPHICS_API_OPENGL_SOFTWARE)
1406+
#if defined(__APPLE__)
1407+
RGFW_monitor* currentMonitor = RGFW_window_getMonitor(platform.window);
1408+
CORE.Window.screenScale = MatrixScale(currentMonitor->pixelRatio, currentMonitor->pixelRatio, 1.0f);
1409+
SetupViewport(platform.window->w * currentMonitor->pixelRatio, platform.window->h * currentMonitor->pixelRatio);
1410+
1411+
CORE.Window.render.width = CORE.Window.screen.width * currentMonitor->pixelRatio;
1412+
CORE.Window.render.height = CORE.Window.screen.height * currentMonitor->pixelRatio;
1413+
CORE.Window.currentFbo.width = CORE.Window.render.width;
1414+
CORE.Window.currentFbo.height = CORE.Window.render.height;
1415+
#endif
1416+
platform.surfaceWidth = CORE.Window.currentFbo.width;
1417+
platform.surfaceHeight = CORE.Window.currentFbo.height;
1418+
1419+
// in software mode we dont have the viewport so we need to reverse the highdpi changes
1420+
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
1421+
{
1422+
Vector2 scaleDpi = GetWindowScaleDPI();
1423+
platform.surfaceWidth *= scaleDpi.x;
1424+
platform.surfaceHeight *= scaleDpi.y;
1425+
}
1426+
1427+
if (platform.surfacePixels != NULL)
1428+
{
1429+
RL_FREE(platform.surfacePixels);
1430+
platform.surfacePixels = RL_MALLOC(platform.surfaceWidth * platform.surfaceHeight * 4);
1431+
}
1432+
1433+
if (platform.surface != NULL)
1434+
{
1435+
RGFW_surface_free(platform.surface);
1436+
platform.surface = RGFW_window_createSurface(platform.window, platform.surfacePixels, platform.surfaceWidth, platform.surfaceHeight, RGFW_formatBGRA8);
1437+
swResize(platform.surfaceWidth, platform.surfaceHeight);
1438+
}
1439+
#endif
13671440
} break;
13681441
case RGFW_windowMaximized:
13691442
{
@@ -1641,6 +1714,12 @@ int InitPlatform(void)
16411714
hints->major = 4;
16421715
hints->minor = 3;
16431716
}
1717+
else if (rlGetVersion() == RL_OPENGL_SOFTWARE)
1718+
{
1719+
hints->major = 1;
1720+
hints->minor = 1;
1721+
hints->renderer = RGFW_glSoftware;
1722+
}
16441723

16451724
if (FLAG_IS_SET(CORE.Window.flags, FLAG_MSAA_4X_HINT)) hints->samples = 4;
16461725

@@ -1720,6 +1799,39 @@ int InitPlatform(void)
17201799
#endif
17211800
}
17221801

1802+
#if defined(GRAPHICS_API_OPENGL_SOFTWARE)
1803+
// apple always scales for retina
1804+
#if defined(__APPLE__)
1805+
RGFW_monitor* currentMonitor = RGFW_window_getMonitor(platform.window);
1806+
CORE.Window.screenScale = MatrixScale(currentMonitor->pixelRatio, currentMonitor->pixelRatio, 1.0f);
1807+
1808+
CORE.Window.render.width = CORE.Window.screen.width * currentMonitor->pixelRatio;
1809+
CORE.Window.render.height = CORE.Window.screen.height * currentMonitor->pixelRatio;
1810+
CORE.Window.currentFbo.width = CORE.Window.render.width;
1811+
CORE.Window.currentFbo.height = CORE.Window.render.height;
1812+
#endif
1813+
1814+
platform.surfaceWidth = CORE.Window.currentFbo.width;
1815+
platform.surfaceHeight = CORE.Window.currentFbo.height;
1816+
1817+
platform.surfacePixels = RL_MALLOC(platform.surfaceWidth * platform.surfaceHeight * 4);
1818+
if (platform.surfacePixels == NULL)
1819+
{
1820+
TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize software pixel buffer");
1821+
return -1;
1822+
}
1823+
1824+
platform.surface = RGFW_window_createSurface(platform.window, platform.surfacePixels, platform.surfaceWidth, platform.surfaceHeight, RGFW_formatBGRA8);
1825+
1826+
if (platform.surface == NULL)
1827+
{
1828+
RL_FREE(platform.surfacePixels);
1829+
1830+
TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize software surface");
1831+
return -1;
1832+
}
1833+
#endif
1834+
17231835
TRACELOG(LOG_INFO, "DISPLAY: Device initialized successfully %s",
17241836
FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI)? "(HighDPI)" : "");
17251837
TRACELOG(LOG_INFO, " > Display size: %i x %i", CORE.Window.display.width, CORE.Window.display.height);
@@ -1750,20 +1862,63 @@ int InitPlatform(void)
17501862
//----------------------------------------------------------------------------
17511863

17521864
#if defined(RGFW_WAYLAND)
1753-
if (RGFW_usingWayland()) TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - Wayland): Initialized successfully");
1754-
else TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11 (fallback)): Initialized successfully");
1865+
if (rlGetVersion() == RL_OPENGL_SOFTWARE)
1866+
{
1867+
if (RGFW_usingWayland()) TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - Wayland, Software): Initialized successfully");
1868+
else TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11, Software (fallback)): Initialized successfully");
1869+
}
1870+
else
1871+
{
1872+
if (RGFW_usingWayland()) TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - Wayland): Initialized successfully");
1873+
else TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11 (fallback)): Initialized successfully");
1874+
}
17551875
#elif defined(RGFW_X11)
17561876
#if defined(__APPLE__)
1757-
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11 (MacOS)): Initialized successfully");
1877+
if (rlGetVersion() == RL_OPENGL_SOFTWARE)
1878+
{
1879+
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11, Software, (MacOS)): Initialized successfully");
1880+
}
1881+
else
1882+
{
1883+
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11, (MacOS)): Initialized successfully");
1884+
}
17581885
#else
1759-
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11): Initialized successfully");
1886+
if (rlGetVersion() == RL_OPENGL_SOFTWARE)
1887+
{
1888+
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11, Software): Initialized successfully");
1889+
}
1890+
else
1891+
{
1892+
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11): Initialized successfully");
1893+
}
17601894
#endif
17611895
#elif defined (RGFW_WINDOWS)
1762-
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - Win32): Initialized successfully");
1896+
if (rlGetVersion() == RL_OPENGL_SOFTWARE)
1897+
{
1898+
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - Win32, Software): Initialized successfully");
1899+
}
1900+
else
1901+
{
1902+
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - Win32): Initialized successfully");
1903+
}
17631904
#elif defined(RGFW_WASM)
1764-
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - WASMs): Initialized successfully");
1905+
if (rlGetVersion() == RL_OPENGL_SOFTWARE)
1906+
{
1907+
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - WASMs, Software): Initialized successfully");
1908+
}
1909+
else
1910+
{
1911+
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - WASMs): Initialized successfully");
1912+
}
17651913
#elif defined(RGFW_MACOS)
1766-
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - MacOS): Initialized successfully");
1914+
if (rlGetVersion() == RL_OPENGL_SOFTWARE)
1915+
{
1916+
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - MacOS, Software): Initialized successfully");
1917+
}
1918+
else
1919+
{
1920+
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - MacOS): Initialized successfully");
1921+
}
17671922
#endif
17681923

17691924
mg_gamepads_init(&platform.minigamepad);
@@ -1776,6 +1931,18 @@ void ClosePlatform(void)
17761931
{
17771932
mg_gamepads_free(&platform.minigamepad);
17781933
RGFW_window_close(platform.window);
1934+
1935+
#if defined(GRAPHICS_API_OPENGL_SOFTWARE)
1936+
if (platform.surfacePixels != NULL)
1937+
{
1938+
RL_FREE(platform.surfacePixels);
1939+
}
1940+
1941+
if (platform.surface != NULL)
1942+
{
1943+
RGFW_surface_free(platform.surface);
1944+
}
1945+
#endif
17791946
}
17801947

17811948
// Keycode mapping

0 commit comments

Comments
 (0)