N3DS port (squashed)

A dedicated renderer using Citro3D would likely allow for better
much better graphical performances.
This commit is contained in:
Pierre Wendling
2021-03-30 04:32:39 -04:00
committed by Sam Lantinga
parent 61b5360e17
commit 655275378d
50 changed files with 2663 additions and 5 deletions

View File

@@ -461,6 +461,7 @@ extern VideoBootStrap PS2_bootstrap;
extern VideoBootStrap PSP_bootstrap;
extern VideoBootStrap VITA_bootstrap;
extern VideoBootStrap RISCOS_bootstrap;
extern VideoBootStrap N3DS_bootstrap;
extern VideoBootStrap RPI_bootstrap;
extern VideoBootStrap KMSDRM_bootstrap;
extern VideoBootStrap KMSDRM_LEGACY_bootstrap;

View File

@@ -100,6 +100,9 @@ static VideoBootStrap *bootstrap[] = {
#if SDL_VIDEO_DRIVER_VITA
&VITA_bootstrap,
#endif
#if SDL_VIDEO_DRIVER_N3DS
&N3DS_bootstrap,
#endif
#if SDL_VIDEO_DRIVER_KMSDRM
&KMSDRM_bootstrap,
#endif
@@ -1498,7 +1501,7 @@ SDL_UpdateFullscreenMode(SDL_Window * window, SDL_bool fullscreen)
}
#define CREATE_FLAGS \
(SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_ALWAYS_ON_TOP | SDL_WINDOW_SKIP_TASKBAR | SDL_WINDOW_POPUP_MENU | SDL_WINDOW_UTILITY | SDL_WINDOW_TOOLTIP | SDL_WINDOW_VULKAN | SDL_WINDOW_MINIMIZED | SDL_WINDOW_METAL)
(SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_ALWAYS_ON_TOP | SDL_WINDOW_SKIP_TASKBAR | SDL_WINDOW_POPUP_MENU | SDL_WINDOW_UTILITY | SDL_WINDOW_TOOLTIP | SDL_WINDOW_VULKAN | SDL_WINDOW_MINIMIZED | SDL_WINDOW_METAL | SDL_WINDOW_N3DS_BOTTOM)
static SDL_INLINE SDL_bool
IsAcceptingDragAndDrop(void)

View File

@@ -0,0 +1,45 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#ifdef SDL_VIDEO_DRIVER_N3DS
/* Pumping the events for the Home and Power buttons. */
#include <3ds.h>
#include "../../events/SDL_events_c.h"
#include "SDL_n3dsevents_c.h"
void
N3DS_PumpEvents(_THIS)
{
if (!aptMainLoop()) {
SDL_Event ev;
ev.type = SDL_QUIT;
SDL_PushEvent(&ev);
return;
}
}
#endif /* SDL_VIDEO_DRIVER_N3DS */
/* vi: set sts=4 ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,31 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SDL_n3dsevents_c_h_
#define SDL_n3dsevents_c_h_
#include "../../SDL_internal.h"
extern void N3DS_PumpEvents(_THIS);
#endif /* SDL_n3dsevents_c_h_ */
/* vi: set sts=4 ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,148 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#ifdef SDL_VIDEO_DRIVER_N3DS
#include "../SDL_sysvideo.h"
#include "SDL_n3dsframebuffer_c.h"
#include "SDL_n3dsvideo.h"
#define N3DS_SURFACE "_SDL_N3DSSurface"
typedef struct
{
int width, height;
} Dimensions;
SDL_FORCE_INLINE void FreePreviousWindowFramebuffer(SDL_Window *window);
SDL_FORCE_INLINE SDL_Surface *CreateNewWindowFramebuffer(SDL_Window *window);
SDL_FORCE_INLINE void CopyFramebuffertoN3DS(u32 *dest, const Dimensions dest_dim, const u32 *source, const Dimensions source_dim);
SDL_FORCE_INLINE int GetDestOffset(int x, int y, int dest_width);
SDL_FORCE_INLINE int GetSourceOffset(int x, int y, int source_width);
SDL_FORCE_INLINE void FlushN3DSBuffer(const void *buffer, u32 bufsize, gfxScreen_t screen);
int
SDL_N3DS_CreateWindowFramebuffer(_THIS, SDL_Window *window, Uint32 *format, void **pixels, int *pitch)
{
SDL_Surface *framebuffer;
FreePreviousWindowFramebuffer(window);
framebuffer = CreateNewWindowFramebuffer(window);
if (!framebuffer) {
return SDL_OutOfMemory();
}
SDL_SetWindowData(window, N3DS_SURFACE, framebuffer);
*format = FRAMEBUFFER_FORMAT;
*pixels = framebuffer->pixels;
*pitch = framebuffer->pitch;
return 0;
}
SDL_FORCE_INLINE void
FreePreviousWindowFramebuffer(SDL_Window *window)
{
SDL_Surface *surface = (SDL_Surface *) SDL_GetWindowData(window, N3DS_SURFACE);
SDL_FreeSurface(surface);
}
SDL_FORCE_INLINE SDL_Surface *
CreateNewWindowFramebuffer(SDL_Window *window)
{
int w, h, bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
SDL_PixelFormatEnumToMasks(FRAMEBUFFER_FORMAT, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
SDL_GetWindowSize(window, &w, &h);
return SDL_CreateRGBSurface(0, w, h, bpp, Rmask, Gmask, Bmask, Amask);
}
int
SDL_N3DS_UpdateWindowFramebuffer(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects)
{
SDL_WindowData *drv_data = (SDL_WindowData *) window->driverdata;
SDL_Surface *surface;
u16 width, height;
u32 *framebuffer;
u32 bufsize;
surface = (SDL_Surface *) SDL_GetWindowData(window, N3DS_SURFACE);
if (!surface) {
return SDL_SetError("%s: Unable to get the window surface.", __func__);
}
/* Get the N3DS internal framebuffer and its size */
framebuffer = (u32 *) gfxGetFramebuffer(drv_data->screen, GFX_LEFT, &width, &height);
bufsize = width * height * 4;
CopyFramebuffertoN3DS(framebuffer, (Dimensions){ width, height },
surface->pixels, (Dimensions){ surface->w, surface->h });
FlushN3DSBuffer(framebuffer, bufsize, drv_data->screen);
return 0;
}
SDL_FORCE_INLINE void
CopyFramebuffertoN3DS(u32 *dest, const Dimensions dest_dim, const u32 *source, const Dimensions source_dim)
{
int rows = SDL_min(dest_dim.width, source_dim.height);
int cols = SDL_min(dest_dim.height, source_dim.width);
for (int y = 0; y < rows; ++y) {
for (int x = 0; x < cols; ++x) {
SDL_memcpy(
dest + GetDestOffset(x, y, dest_dim.width),
source + GetSourceOffset(x, y, source_dim.width),
4);
}
}
}
SDL_FORCE_INLINE int
GetDestOffset(int x, int y, int dest_width)
{
return dest_width - y - 1 + dest_width * x;
}
SDL_FORCE_INLINE int
GetSourceOffset(int x, int y, int source_width)
{
return x + y * source_width;
}
SDL_FORCE_INLINE void
FlushN3DSBuffer(const void *buffer, u32 bufsize, gfxScreen_t screen)
{
GSPGPU_FlushDataCache(buffer, bufsize);
gfxScreenSwapBuffers(screen, false);
}
void
SDL_N3DS_DestroyWindowFramebuffer(_THIS, SDL_Window *window)
{
SDL_Surface *surface;
surface = (SDL_Surface *) SDL_SetWindowData(window, N3DS_SURFACE, NULL);
SDL_FreeSurface(surface);
}
#endif /* SDL_VIDEO_DRIVER_N3DS */
/* vi: set sts=4 ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,33 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SDL_n3dsframebuffer_c_h_
#define SDL_n3dsframebuffer_c_h_
#include "../../SDL_internal.h"
int SDL_N3DS_CreateWindowFramebuffer(_THIS, SDL_Window *window, Uint32 *format, void **pixels, int *pitch);
int SDL_N3DS_UpdateWindowFramebuffer(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects);
void SDL_N3DS_DestroyWindowFramebuffer(_THIS, SDL_Window *window);
#endif /* SDL_n3dsframebuffer_c_h_ */
/* vi: set sts=4 ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,76 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#ifdef SDL_VIDEO_DRIVER_N3DS
#include <3ds.h>
#include "SDL_n3dsswkb.h"
static SwkbdState sw_keyboard;
const static size_t BUFFER_SIZE = 256;
void
N3DS_SwkbInit()
{
swkbdInit(&sw_keyboard, SWKBD_TYPE_NORMAL, 2, -1);
}
void
N3DS_SwkbPoll()
{
return;
}
void
N3DS_SwkbQuit()
{
return;
}
SDL_bool
N3DS_HasScreenKeyboardSupport(_THIS)
{
return SDL_TRUE;
}
void
N3DS_StartTextInput(_THIS)
{
char buffer[BUFFER_SIZE];
SwkbdButton button_pressed;
button_pressed = swkbdInputText(&sw_keyboard, buffer, BUFFER_SIZE);
if (button_pressed == SWKBD_BUTTON_CONFIRM) {
SDL_SendKeyboardText(buffer);
}
}
void
N3DS_StopTextInput(_THIS)
{
return;
}
#endif /* SDL_VIDEO_DRIVER_N3DS */
/* vi: set sts=4 ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,38 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SDL_n3dskeyboard_h_
#define SDL_n3dskeyboard_h_
#include "../../events/SDL_events_c.h"
void N3DS_SwkbInit();
void N3DS_SwkbPoll();
void N3DS_SwkbQuit();
SDL_bool N3DS_HasScreenKeyboardSupport(_THIS);
void N3DS_StartTextInput(_THIS);
void N3DS_StopTextInput(_THIS);
#endif /* SDL_n3dskeyboard_h_ */
/* vi: set sts=4 ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,163 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#ifdef SDL_VIDEO_DRIVER_N3DS
#include "../SDL_sysvideo.h"
#include "SDL_n3dsevents_c.h"
#include "SDL_n3dsframebuffer_c.h"
#include "SDL_n3dsswkb.h"
#include "SDL_n3dsvideo.h"
#define N3DSVID_DRIVER_NAME "n3ds"
SDL_FORCE_INLINE void AddN3DSDisplay(gfxScreen_t screen);
static int N3DS_VideoInit(_THIS);
static void N3DS_VideoQuit(_THIS);
static void N3DS_GetDisplayModes(_THIS, SDL_VideoDisplay *display);
static int N3DS_CreateWindow(_THIS, SDL_Window *window);
static void N3DS_DestroyWindow(_THIS, SDL_Window *window);
/* N3DS driver bootstrap functions */
static void
N3DS_DeleteDevice(SDL_VideoDevice *device)
{
SDL_free(device->displays);
SDL_free(device->driverdata);
SDL_free(device);
}
static SDL_VideoDevice *
N3DS_CreateDevice(void)
{
SDL_VideoDevice *device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
if (!device) {
SDL_OutOfMemory();
return (0);
}
device->VideoInit = N3DS_VideoInit;
device->VideoQuit = N3DS_VideoQuit;
device->GetDisplayModes = N3DS_GetDisplayModes;
device->CreateSDLWindow = N3DS_CreateWindow;
device->DestroyWindow = N3DS_DestroyWindow;
device->HasScreenKeyboardSupport = N3DS_HasScreenKeyboardSupport;
device->StartTextInput = N3DS_StartTextInput;
device->StopTextInput = N3DS_StopTextInput;
device->PumpEvents = N3DS_PumpEvents;
device->CreateWindowFramebuffer = SDL_N3DS_CreateWindowFramebuffer;
device->UpdateWindowFramebuffer = SDL_N3DS_UpdateWindowFramebuffer;
device->DestroyWindowFramebuffer = SDL_N3DS_DestroyWindowFramebuffer;
device->num_displays = 2;
device->free = N3DS_DeleteDevice;
return device;
}
VideoBootStrap N3DS_bootstrap = { N3DSVID_DRIVER_NAME, "N3DS Video Driver", N3DS_CreateDevice };
static int
N3DS_VideoInit(_THIS)
{
AddN3DSDisplay(GFX_TOP);
AddN3DSDisplay(GFX_BOTTOM);
N3DS_SwkbInit();
return 0;
}
SDL_FORCE_INLINE void
AddN3DSDisplay(gfxScreen_t screen)
{
SDL_DisplayMode mode;
SDL_VideoDisplay display;
SDL_zero(mode);
SDL_zero(display);
mode.w = (screen == GFX_TOP) ? GSP_SCREEN_HEIGHT_TOP : GSP_SCREEN_HEIGHT_BOTTOM;
mode.h = GSP_SCREEN_WIDTH;
mode.refresh_rate = 60;
mode.format = FRAMEBUFFER_FORMAT;
mode.driverdata = NULL;
display.desktop_mode = mode;
display.current_mode = mode;
display.driverdata = NULL;
SDL_AddVideoDisplay(&display, SDL_FALSE);
}
static void
N3DS_VideoQuit(_THIS)
{
N3DS_SwkbQuit();
return;
}
static void
N3DS_GetDisplayModes(_THIS, SDL_VideoDisplay *display)
{
/* Each display only has a single mode */
SDL_AddDisplayMode(display, &display->current_mode);
}
static int
N3DS_CreateWindow(_THIS, SDL_Window *window)
{
SDL_WindowData *drv_data = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
if (drv_data == NULL) {
return SDL_OutOfMemory();
}
if (window->flags & SDL_WINDOW_N3DS_BOTTOM) {
drv_data->screen = GFX_BOTTOM;
} else {
drv_data->screen = GFX_TOP;
}
window->driverdata = drv_data;
return 0;
}
static void
N3DS_DestroyWindow(_THIS, SDL_Window *window)
{
if (window == NULL) {
return;
}
SDL_free(window->driverdata);
}
#endif /* SDL_VIDEO_DRIVER_N3DS */
/* vi: set sts=4 ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,38 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#ifndef SDL_n3dsvideo_h_
#define SDL_n3dsvideo_h_
#include <3ds.h>
#include "../SDL_sysvideo.h"
typedef struct SDL_WindowData
{
gfxScreen_t screen; /**< Keeps track of which N3DS screen is targetted */
} SDL_WindowData;
#define FRAMEBUFFER_FORMAT SDL_PIXELFORMAT_RGBA8888
#endif /* SDL_n3dsvideo_h_ */
/* vi: set sts=4 ts=4 sw=4 expandtab: */