switch: use new gfx (#26)

This commit is contained in:
Cpasjuste 2019-01-07 16:46:03 +01:00 committed by Dave Murphy
parent 6748b87af8
commit 45b864bb7b
5 changed files with 122 additions and 89 deletions

View File

@ -421,7 +421,6 @@ GLES2_CacheProgram(GLES2_RenderData *data, GLES2_ShaderCacheEntry *vertex,
GLES2_ShaderCacheEntry *shaderEntry; GLES2_ShaderCacheEntry *shaderEntry;
GLint linkSuccessful; GLint linkSuccessful;
<<<<<<< HEAD
/* Check if we've already cached this program */ /* Check if we've already cached this program */
entry = data->program_cache.head; entry = data->program_cache.head;
while (entry) { while (entry) {

View File

@ -47,10 +47,8 @@ SWITCH_GLES_LoadLibrary(_THIS, const char *path)
void void
SWITCH_GLES_GetDrawableSize(_THIS, SDL_Window *window, int *w, int *h) SWITCH_GLES_GetDrawableSize(_THIS, SDL_Window *window, int *w, int *h)
{ {
SDL_DisplayMode mode = {0, 0, 0, 0, 0}; *w = 1920;
SDL_GetCurrentDisplayMode(0, &mode); *h = 1080;
*w = mode.w;
*h = mode.h;
} }
SDL_EGL_CreateContext_impl(SWITCH) SDL_EGL_CreateContext_impl(SWITCH)

View File

@ -23,8 +23,6 @@
#if SDL_VIDEO_DRIVER_SWITCH #if SDL_VIDEO_DRIVER_SWITCH
#include <switch.h>
#include "../SDL_sysvideo.h" #include "../SDL_sysvideo.h"
#include "../../render/SDL_sysrender.h" #include "../../render/SDL_sysrender.h"
#include "../../events/SDL_keyboard_c.h" #include "../../events/SDL_keyboard_c.h"
@ -121,14 +119,14 @@ SWITCH_VideoInit(_THIS)
SDL_DisplayMode current_mode; SDL_DisplayMode current_mode;
SDL_DisplayData *data; SDL_DisplayData *data;
SDL_DisplayModeData *mdata; SDL_DisplayModeData *mdata;
Result rc;
SDL_zero(current_mode); SDL_zero(current_mode);
current_mode.w = 1280; current_mode.w = 1920;
current_mode.h = 720; current_mode.h = 1080;
current_mode.refresh_rate = 60; current_mode.refresh_rate = 60;
current_mode.format = SDL_PIXELFORMAT_RGBA8888; current_mode.format = SDL_PIXELFORMAT_RGBA8888;
mdata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData)); mdata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
mdata->padding = 0;
current_mode.driverdata = mdata; current_mode.driverdata = mdata;
SDL_zero(display); SDL_zero(display);
@ -140,20 +138,38 @@ SWITCH_VideoInit(_THIS)
if (data == NULL) { if (data == NULL) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
data->egl_display = EGL_DEFAULT_DISPLAY;
display.driverdata = data;
data->egl_display = EGL_DEFAULT_DISPLAY;
// init vi
rc = viInitialize(ViServiceType_Default);
if (R_FAILED(rc)) {
return SDL_SetError("Could not initialize vi service: 0x%x", rc);
}
rc = viOpenDefaultDisplay(&data->viDisplay);
if (R_FAILED(rc)) {
return SDL_SetError("Could not open default display: 0x%x", rc);
}
display.driverdata = data;
SDL_AddVideoDisplay(&display); SDL_AddVideoDisplay(&display);
// init touch // init touch
SWITCH_InitTouch(); SWITCH_InitTouch();
return 1; return 0;
} }
void void
SWITCH_VideoQuit(_THIS) SWITCH_VideoQuit(_THIS)
{ {
SDL_DisplayData *data = SDL_GetDisplayDriverData(0);
if (data) {
viCloseDisplay(&data->viDisplay);
}
viExit();
// exit touch // exit touch
SWITCH_QuitTouch(); SWITCH_QuitTouch();
} }
@ -164,52 +180,16 @@ SWITCH_GetDisplayModes(_THIS, SDL_VideoDisplay *display)
SDL_DisplayMode mode; SDL_DisplayMode mode;
SDL_DisplayModeData *data; SDL_DisplayModeData *data;
// 1920x1080 (16/9) 16RGBA8888 // 1920x1080 RGBA8888, default mode
if (appletGetOperationMode() == AppletOperationMode_Docked) {
SDL_zero(mode);
mode.w = 1920;
mode.h = 1080;
mode.refresh_rate = 60;
mode.format = SDL_PIXELFORMAT_RGBA8888;
data = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
data->padding = 0;
mode.driverdata = data;
SDL_AddDisplayMode(display, &mode);
}
// 1280x720 (16/9) RGBA8888
SDL_AddDisplayMode(display, &display->current_mode); SDL_AddDisplayMode(display, &display->current_mode);
// 960x540 (16/9) RGBA8888 // 1280x720 RGBA8888
SDL_zero(mode); SDL_zero(mode);
mode.w = 960; mode.w = 1280;
mode.h = 540; mode.h = 720;
mode.refresh_rate = 60; mode.refresh_rate = 60;
mode.format = SDL_PIXELFORMAT_RGBA8888; mode.format = SDL_PIXELFORMAT_RGBA8888;
data = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData)); data = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
data->padding = 0;
mode.driverdata = data;
SDL_AddDisplayMode(display, &mode);
// 800x600 (4/3) RGBA8888
SDL_zero(mode);
mode.w = 800;
mode.h = 600;
mode.refresh_rate = 60;
mode.format = SDL_PIXELFORMAT_RGBA8888;
data = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
data->padding = (int) ((600.0f * 1.7774f) - 800.0f) / 2;
mode.driverdata = data;
SDL_AddDisplayMode(display, &mode);
// 640x480 (4/3) RGBA8888
SDL_zero(mode);
mode.w = 640;
mode.h = 480;
mode.refresh_rate = 60;
mode.format = SDL_PIXELFORMAT_RGBA8888;
data = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
data->padding = (int) ((480.0f * 1.7774f) - 640.0f) / 2;
mode.driverdata = data; mode.driverdata = data;
SDL_AddDisplayMode(display, &mode); SDL_AddDisplayMode(display, &mode);
} }
@ -217,19 +197,22 @@ SWITCH_GetDisplayModes(_THIS, SDL_VideoDisplay *display)
int int
SWITCH_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode) SWITCH_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
{ {
SDL_Renderer *renderer = SDL_GetRenderer(_this->windows); SDL_WindowData *data;
SDL_DisplayModeData *data = (SDL_DisplayModeData *) mode->driverdata; Result rc;
if (!data) { if (display->fullscreen_window) {
return -1; data = (SDL_WindowData *) display->fullscreen_window->driverdata;
}
else {
if (!SDL_GetFocusWindow()) {
return SDL_SetError("Could not get window focus");
}
data = (SDL_WindowData *) SDL_GetFocusWindow()->driverdata;
} }
gfxConfigureResolution(mode->w + data->padding * 2, mode->h); rc = nwindowSetCrop(&data->nWindow, 0, 0, mode->w, mode->h);
display->current_mode = *mode; if (rc) {
_this->windows->w = mode->w; return SDL_SetError("Could not set NWindow crop: 0x%x", rc);
_this->windows->h = mode->h;
if (renderer) {
renderer->UpdateViewport(renderer);
} }
return 0; return 0;
@ -238,23 +221,52 @@ SWITCH_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
int int
SWITCH_CreateWindow(_THIS, SDL_Window *window) SWITCH_CreateWindow(_THIS, SDL_Window *window)
{ {
SDL_WindowData *wdata; Result rc;
//SDL_VideoDisplay *display; SDL_DisplayData *ddata = SDL_GetDisplayDriverData(0);
SDL_WindowData *wdata = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
/* Allocate window internal data */
wdata = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
if (wdata == NULL) { if (wdata == NULL) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
window->flags |= SDL_WINDOW_FULLSCREEN;
if (!_this->egl_data) { if (!_this->egl_data) {
return SDL_SetError("SWITCH_CreateWindow: EGL not initialized"); return SDL_SetError("EGL not initialized");
} }
wdata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) &wdata->egl_surface);
rc = viCreateLayer(&ddata->viDisplay, &wdata->viLayer);
if (R_FAILED(rc)) {
return SDL_SetError("Could not create vi layer: 0x%x", rc);
}
rc = viSetLayerScalingMode(&wdata->viLayer, ViScalingMode_FitToLayer);
if (R_FAILED(rc)) {
viCloseLayer(&wdata->viLayer);
return SDL_SetError("Could not set vi scaling mode: 0x%x", rc);
}
rc = nwindowCreateFromLayer(&wdata->nWindow, &wdata->viLayer);
if (R_FAILED(rc)) {
viCloseLayer(&wdata->viLayer);
return SDL_SetError("Could not create NWindow from layer: 0x%x", rc);
}
rc = nwindowSetDimensions(&wdata->nWindow, 1920, 1080);
if (R_FAILED(rc)) {
nwindowClose(&wdata->nWindow);
viCloseLayer(&wdata->viLayer);
return SDL_SetError("Could not set NWindow dimensions: 0x%x", rc);
}
rc = nwindowSetCrop(&wdata->nWindow, 0, 0, window->w, window->h);
if (R_FAILED(rc)) {
nwindowClose(&wdata->nWindow);
viCloseLayer(&wdata->viLayer);
return SDL_SetError("Could not set NWindow crop: 0x%x", rc);
}
wdata->egl_surface = SDL_EGL_CreateSurface(_this, &wdata->nWindow);
if (wdata->egl_surface == EGL_NO_SURFACE) { if (wdata->egl_surface == EGL_NO_SURFACE) {
nwindowClose(&wdata->nWindow);
viCloseLayer(&wdata->viLayer);
return SDL_SetError("Could not create GLES window surface"); return SDL_SetError("Could not create GLES window surface");
} }
@ -278,6 +290,8 @@ SWITCH_DestroyWindow(_THIS, SDL_Window *window)
if (data->egl_surface != EGL_NO_SURFACE) { if (data->egl_surface != EGL_NO_SURFACE) {
SDL_EGL_DestroySurface(_this, data->egl_surface); SDL_EGL_DestroySurface(_this, data->egl_surface);
} }
nwindowClose(&data->nWindow);
viCloseLayer(&data->viLayer);
SDL_free(data); SDL_free(data);
window->driverdata = NULL; window->driverdata = NULL;
} }
@ -303,6 +317,8 @@ SWITCH_SetWindowPosition(_THIS, SDL_Window *window)
void void
SWITCH_SetWindowSize(_THIS, SDL_Window *window) SWITCH_SetWindowSize(_THIS, SDL_Window *window)
{ {
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
nwindowSetCrop(&data->nWindow, 0, 0, window->w, window->h);
} }
void void
SWITCH_ShowWindow(_THIS, SDL_Window *window) SWITCH_ShowWindow(_THIS, SDL_Window *window)
@ -331,7 +347,6 @@ SWITCH_RestoreWindow(_THIS, SDL_Window *window)
void void
SWITCH_SetWindowGrab(_THIS, SDL_Window *window, SDL_bool grabbed) SWITCH_SetWindowGrab(_THIS, SDL_Window *window, SDL_bool grabbed)
{ {
} }
void void

View File

@ -24,6 +24,8 @@
#if SDL_VIDEO_DRIVER_SWITCH #if SDL_VIDEO_DRIVER_SWITCH
#include <switch.h>
#include "../../SDL_internal.h" #include "../../SDL_internal.h"
#include "../SDL_sysvideo.h" #include "../SDL_sysvideo.h"
@ -31,16 +33,18 @@
typedef struct SDL_DisplayData typedef struct SDL_DisplayData
{ {
ViDisplay viDisplay;
EGLDisplay egl_display; EGLDisplay egl_display;
} SDL_DisplayData; } SDL_DisplayData;
typedef struct SDL_DisplayModeData typedef struct SDL_DisplayModeData
{ {
int padding;
} SDL_DisplayModeData; } SDL_DisplayModeData;
typedef struct SDL_WindowData typedef struct SDL_WindowData
{ {
ViLayer viLayer;
NWindow nWindow;
EGLSurface egl_surface; EGLSurface egl_surface;
} SDL_WindowData; } SDL_WindowData;

View File

@ -70,7 +70,7 @@ int main(int argc, char *argv[])
SDL_Event event; SDL_Event event;
SDL_Window *window; SDL_Window *window;
SDL_Renderer *renderer; SDL_Renderer *renderer;
int done = 0, x = 0, w, h; int done = 0, x = 0, w = 0, h = 0;
// mandatory at least on switch, else gfx is not properly closed // mandatory at least on switch, else gfx is not properly closed
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
@ -78,14 +78,15 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
// create a window (OpenGL always enabled) /// create a window (OpenGL always enabled)
// available switch SDL2 video modes : /// available switch SDL2 video modes :
// 1920 x 1080 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888) (docked only) /// 1920 x 1080 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888)
// 1280 x 720 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888) /// 1280 x 720 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888)
// 960 x 540 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888) ///
// 800 x 600 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888) /// SDL_SetWindowSize to change window size when SDL_WINDOW_FULLSCREEN is NOT used (preferably)
// 640 x 480 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888) /// SDL_SetDisplayMode to change display size after SDL_CreateWindow called with SDL_WINDOW_FULLSCREEN
window = SDL_CreateWindow("sdl2_gles2", 0, 0, 640, 480, SDL_WINDOW_FULLSCREEN); /// (this means window size won't change, you'll need to handle that, as any SDL2 app)
window = SDL_CreateWindow("sdl2_gles2", 0, 0, 1280, 720, 0);
if (!window) { if (!window) {
SDL_Log("SDL_CreateWindow: %s\n", SDL_GetError()); SDL_Log("SDL_CreateWindow: %s\n", SDL_GetError());
SDL_Quit(); SDL_Quit();
@ -109,6 +110,10 @@ int main(int argc, char *argv[])
SDL_DisplayMode mode; SDL_DisplayMode mode;
SDL_GetDisplayMode(0, i, &mode); SDL_GetDisplayMode(0, i, &mode);
modes[i] = mode; modes[i] = mode;
SDL_Log("found display mode: %i x %i @ %i bpp (%s)",
mode.w, mode.h,
SDL_BITSPERPIXEL(mode.format),
SDL_GetPixelFormatName(mode.format));
} }
// open CONTROLLER_PLAYER_1 and CONTROLLER_PLAYER_2 // open CONTROLLER_PLAYER_1 and CONTROLLER_PLAYER_2
@ -138,13 +143,25 @@ int main(int argc, char *argv[])
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
SDL_Log("Joystick %d button %d down\n", SDL_Log("Joystick %d button %d down\n",
event.jbutton.which, event.jbutton.button); event.jbutton.which, event.jbutton.button);
// seek for joystick #0 down (A)
// https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L52 // https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L52
if (event.jbutton.which == 0 && event.jbutton.button == 0) { if (event.jbutton.which == 0) {
if (event.jbutton.button == 0) {
// joystick #0 down (A)
change_mode(window); change_mode(window);
print_info(window, renderer); print_info(window, renderer);
} }
// seek for joystick #0 down (B) else if (event.jbutton.button == 2) {
// joystick #0 down (X)
if (w == 1920) {
SDL_SetWindowSize(window, 1280, 720);
}
else {
SDL_SetWindowSize(window, 1920, 1080);
}
print_info(window, renderer);
}
}
// joystick #0 down (B)
if (event.jbutton.which == 0 && event.jbutton.button == 1) { if (event.jbutton.which == 0 && event.jbutton.button == 1) {
done = 1; done = 1;
} }
@ -160,7 +177,7 @@ int main(int argc, char *argv[])
// Fill renderer bounds // Fill renderer bounds
SDL_SetRenderDrawColor(renderer, 111, 111, 111, 255); SDL_SetRenderDrawColor(renderer, 111, 111, 111, 255);
SDL_GetRendererOutputSize(renderer, &w, &h); SDL_GetWindowSize(window, &w, &h);
SDL_Rect f = {0, 0, w, h}; SDL_Rect f = {0, 0, w, h};
SDL_RenderFillRect(renderer, &f); SDL_RenderFillRect(renderer, &f);