fteqw/engine/gl/gl_videgl.c
Spoike c949500da8 Implement preliminary openxr plugin. inputs are still useless (can HOPEFULLY result in some prints, but nothing else), and there's no considerations for 2d things.
Fix prediction issues with the ftenq protocol.
Fix some console image previews.
Added mod_precache cvar. set to 0 to significantly reduce memory usage in xonotic...
Prevent xonotic's random file writes from forcing full worker syncs, for faster loading.
QTV connections are now accepted only from localhost peers by default.



git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@5694 fc73d0e0-1445-4013-8a0c-d673dee63da5
2020-05-14 15:50:26 +00:00

453 lines
13 KiB
C

#include "quakedef.h"
#if defined(GLQUAKE) && defined(USE_EGL)
#include "gl_videgl.h"
#include "vr.h"
//EGL_KHR_gl_colorspace
#ifndef EGL_GL_COLORSPACE_KHR
#define EGL_GL_COLORSPACE_KHR 0x309D
#endif
#ifndef EGL_GL_COLORSPACE_SRGB_KHR
#define EGL_GL_COLORSPACE_SRGB_KHR 0x3089
#endif
#ifndef EGL_GL_COLORSPACE_LINEAR_KHR
#define EGL_GL_COLORSPACE_LINEAR_KHR 0x308A
#endif
extern cvar_t vid_vsync;
EGLContext eglctx = EGL_NO_CONTEXT;
EGLDisplay egldpy = EGL_NO_DISPLAY;
EGLSurface eglsurf = EGL_NO_SURFACE;
static dllhandle_t *egllibrary;
static dllhandle_t *eslibrary;
static EGLint (EGLAPIENTRY *qeglGetError)(void);
static EGLDisplay (EGLAPIENTRY *qeglGetPlatformDisplay)(EGLenum platform, void *native_display, const EGLAttrib *attrib_list);
static EGLDisplay (EGLAPIENTRY *qeglGetDisplay)(EGLNativeDisplayType display_id);
static EGLBoolean (EGLAPIENTRY *qeglInitialize)(EGLDisplay dpy, EGLint *major, EGLint *minor);
static EGLBoolean (EGLAPIENTRY *qeglTerminate)(EGLDisplay dpy);
static EGLBoolean (EGLAPIENTRY *qeglGetConfigs)(EGLDisplay dpy, EGLConfig *configs, EGLint config_size, EGLint *num_config);
static EGLBoolean (EGLAPIENTRY *qeglChooseConfig)(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config);
EGLBoolean (EGLAPIENTRY *qeglGetConfigAttrib)(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value);
static EGLSurface (EGLAPIENTRY *qeglCreatePlatformWindowSurface)(EGLDisplay dpy, EGLConfig config, void *native_window, const EGLAttrib *attrib_list);
static EGLSurface (EGLAPIENTRY *qeglCreateWindowSurface)(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list);
static EGLBoolean (EGLAPIENTRY *qeglDestroySurface)(EGLDisplay dpy, EGLSurface surface);
static EGLBoolean (EGLAPIENTRY *qeglQuerySurface)(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint *value);
static EGLBoolean (EGLAPIENTRY *qeglSwapBuffers)(EGLDisplay dpy, EGLSurface surface);
static EGLBoolean (EGLAPIENTRY *qeglMakeCurrent)(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
static EGLContext (EGLAPIENTRY *qeglCreateContext)(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list);
static EGLBoolean (EGLAPIENTRY *qeglDestroyContext)(EGLDisplay dpy, EGLContext ctx);
static void * (EGLAPIENTRY *qeglGetProcAddress) (const char *name);
static EGLBoolean (EGLAPIENTRY *qeglSwapInterval) (EGLDisplay display, EGLint interval);
static dllfunction_t qeglfuncs[] =
{
{(void*)&qeglGetError, "eglGetError"},
{(void*)&qeglGetDisplay, "eglGetDisplay"},
{(void*)&qeglInitialize, "eglInitialize"},
{(void*)&qeglTerminate, "eglTerminate"},
{(void*)&qeglGetConfigs, "eglGetConfigs"},
{(void*)&qeglChooseConfig, "eglChooseConfig"},
{(void*)&qeglGetConfigAttrib, "eglGetConfigAttrib"},
{(void*)&qeglCreateWindowSurface, "eglCreateWindowSurface"},
{(void*)&qeglDestroySurface, "eglDestroySurface"},
{(void*)&qeglQuerySurface, "eglQuerySurface"},
{(void*)&qeglSwapBuffers, "eglSwapBuffers"},
{(void*)&qeglMakeCurrent, "eglMakeCurrent"},
{(void*)&qeglCreateContext, "eglCreateContext"},
{(void*)&qeglDestroyContext, "eglDestroyContext"},
{(void*)&qeglGetProcAddress, "eglGetProcAddress"},
//EGL 1.1
{(void*)&qeglSwapInterval, "eglSwapInterval"},
{NULL}
};
void *EGL_Proc(char *f)
{
void *proc = NULL;
/*
char fname[512];
{
sprintf(fname, "wrap_%s", f);
f = fname;
}
*/
if (!proc)
proc = Sys_GetAddressForName(eslibrary, f);
if (!proc)
proc = Sys_GetAddressForName(egllibrary, f);
//eglGetProcAddress functions must work regardless of context...
//FIXME: this means lots of false-positives.
//as well as lots of thunks, which will result in slowdowns.
if (qeglGetProcAddress)
proc = qeglGetProcAddress(f);
return proc;
}
static const char *EGL_GetErrorString(int error)
{
switch(error)
{
case EGL_BAD_ACCESS: return "BAD_ACCESS";
case EGL_BAD_ALLOC: return "BAD_ALLOC";
case EGL_BAD_ATTRIBUTE: return "BAD_ATTRIBUTE";
case EGL_BAD_CONFIG: return "BAD_CONFIG";
case EGL_BAD_CONTEXT: return "BAD_CONEXT";
case EGL_BAD_CURRENT_SURFACE: return "BAD_CURRENT_SURFACE";
case EGL_BAD_DISPLAY: return "BAD_DISPLAY";
case EGL_BAD_MATCH: return "BAD_MATCH";
case EGL_BAD_NATIVE_PIXMAP: return "BAD_NATIVE_PIXMAP";
case EGL_BAD_NATIVE_WINDOW: return "BAD_NATIVE_WINDOW";
case EGL_BAD_PARAMETER: return "BAD_PARAMETER";
case EGL_BAD_SURFACE: return "BAD_SURFACE";
default: return va("EGL:%#x", error);
}
}
void EGL_UnloadLibrary(void)
{
if (egllibrary)
Sys_CloseLibrary(egllibrary);
if (egllibrary == eslibrary)
eslibrary = NULL;
if (eslibrary)
Sys_CloseLibrary(eslibrary);
eslibrary = egllibrary = NULL;
}
qboolean EGL_LoadLibrary(char *driver)
{
/* linux seem to load glesv2 first for dependency issues.
(most things are expected to statically link to their libs)
strictly speaking, EGL says that functions should work regardless of context.
(which of course makes portability a nightmare, especially on windows where static linking is basically impossible)
(android's EGL bugs out if you use eglGetProcAddress for core functions too)
*/
Sys_Printf("Attempting to dlopen libGLESv2... ");
eslibrary = Sys_LoadLibrary("libGLESv2", NULL);
if (!eslibrary)
{
Sys_Printf("failed\n");
// return false;
}
else
Sys_Printf("success\n");
#ifndef _WIN32
if (!eslibrary)
{
eslibrary = dlopen("libGL.so.1.2", RTLD_NOW|RTLD_GLOBAL);
if (eslibrary) Sys_Printf("Loaded libGL.so.1.2\n");
}
if (!eslibrary)
{
eslibrary = dlopen("libGL.so.1", RTLD_NOW|RTLD_GLOBAL);
if (eslibrary) Sys_Printf("Loaded libGL.so.1\n");
}
if (!eslibrary)
{
eslibrary = dlopen("libGL", RTLD_NOW|RTLD_GLOBAL);
if (eslibrary) Sys_Printf("Loaded libGL\n");
}
#endif
if (!eslibrary)
Sys_Printf("unable to load some libGL\n");
Sys_Printf("Attempting to dlopen libEGL... ");
egllibrary = Sys_LoadLibrary("libEGL", qeglfuncs);
if (!egllibrary)
{
Sys_Printf("failed\n");
Con_Printf("libEGL library not loadable\n");
/* TODO: some implementations combine EGL/GLESv2 into single library... */
Sys_CloseLibrary(eslibrary);
return false;
}
Sys_Printf("success\n");
//these are from egl1.5
qeglGetPlatformDisplay = EGL_Proc("eglGetPlatformDisplay");
qeglCreatePlatformWindowSurface = EGL_Proc("eglCreatePlatformWindowSurface");
//and in case they arn't defined...
if (!qeglGetPlatformDisplay) qeglGetPlatformDisplay = EGL_Proc("eglGetPlatformDisplayEXT");
if (!qeglCreatePlatformWindowSurface) qeglCreatePlatformWindowSurface = EGL_Proc("eglCreatePlatformWindowSurfaceEXT");
return true;
}
void EGL_Shutdown(void)
{
if (eglctx == EGL_NO_CONTEXT)
return;
qeglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
qeglDestroyContext(egldpy, eglctx);
if (eglsurf != EGL_NO_SURFACE)
qeglDestroySurface(egldpy, eglsurf);
qeglTerminate(egldpy);
eglctx = EGL_NO_CONTEXT;
egldpy = EGL_NO_DISPLAY;
eglsurf = EGL_NO_SURFACE;
}
/*static void EGL_ShowConfig(EGLDisplay egldpy, EGLConfig cfg)
{
struct
{
EGLint attr;
const char *attrname;
} eglattrs[] =
{
{EGL_ALPHA_SIZE, "EGL_ALPHA_SIZE"},
{EGL_ALPHA_MASK_SIZE, "EGL_ALPHA_MASK_SIZE"},
{EGL_BIND_TO_TEXTURE_RGB, "EGL_BIND_TO_TEXTURE_RGB"},
{EGL_BIND_TO_TEXTURE_RGBA, "EGL_BIND_TO_TEXTURE_RGBA"},
{EGL_BLUE_SIZE, "EGL_BLUE_SIZE"},
{EGL_BUFFER_SIZE, "EGL_BUFFER_SIZE"},
{EGL_COLOR_BUFFER_TYPE, "EGL_COLOR_BUFFER_TYPE"},
{EGL_CONFIG_CAVEAT, "EGL_CONFIG_CAVEAT"},
{EGL_CONFIG_ID, "EGL_CONFIG_ID"},
{EGL_CONFORMANT, "EGL_CONFORMANT"},
{EGL_DEPTH_SIZE, "EGL_DEPTH_SIZE"},
{EGL_GREEN_SIZE, "EGL_GREEN_SIZE"},
{EGL_LEVEL, "EGL_LEVEL"},
{EGL_LUMINANCE_SIZE, "EGL_LUMINANCE_SIZE"},
{EGL_MAX_PBUFFER_WIDTH, "EGL_MAX_PBUFFER_WIDTH"},
{EGL_MAX_PBUFFER_HEIGHT, "EGL_MAX_PBUFFER_HEIGHT"},
{EGL_MAX_PBUFFER_PIXELS, "EGL_MAX_PBUFFER_PIXELS"},
{EGL_MAX_SWAP_INTERVAL, "EGL_MAX_SWAP_INTERVAL"},
{EGL_MIN_SWAP_INTERVAL, "EGL_MIN_SWAP_INTERVAL"},
{EGL_NATIVE_RENDERABLE, "EGL_NATIVE_RENDERABLE"},
{EGL_NATIVE_VISUAL_ID, "EGL_NATIVE_VISUAL_ID"},
{EGL_NATIVE_VISUAL_TYPE, "EGL_NATIVE_VISUAL_TYPE"},
{EGL_RED_SIZE, "EGL_RED_SIZE"},
{EGL_RENDERABLE_TYPE, "EGL_RENDERABLE_TYPE"},
{EGL_SAMPLE_BUFFERS, "EGL_SAMPLE_BUFFERS"},
{EGL_SAMPLES, "EGL_SAMPLES"},
{EGL_STENCIL_SIZE, "EGL_STENCIL_SIZE"},
{EGL_SURFACE_TYPE, "EGL_SURFACE_TYPE"},
{EGL_TRANSPARENT_TYPE, "EGL_TRANSPARENT_TYPE"},
{EGL_TRANSPARENT_RED_VALUE, "EGL_TRANSPARENT_RED_VALUE"},
{EGL_TRANSPARENT_GREEN_VALUE, "EGL_TRANSPARENT_GREEN_VALUE"},
{EGL_TRANSPARENT_BLUE_VALUE, "EGL_TRANSPARENT_BLUE_VALUE"},
};
size_t i;
EGLint val;
for (i = 0; i < countof(eglattrs); i++)
{
if (qeglGetConfigAttrib(egldpy, cfg, eglattrs[i].attr, &val))
Con_DPrintf("%p.%s: %i\n", cfg, eglattrs[i].attrname, val);
else
Con_DPrintf("%p.%s: UNKNOWN\n", cfg, eglattrs[i].attrname);
}
}*/
static void EGL_UpdateSwapInterval(void)
{
int interval;
vid_vsync.modified = false;
if (*vid_vsync.string)
interval = vid_vsync.ival;
else
interval = 1; //default is to always vsync, according to EGL docs, so lets just do that.
if (qeglSwapInterval)
qeglSwapInterval(egldpy, interval);
}
void EGL_SwapBuffers (void)
{
if (vid_vsync.modified)
EGL_UpdateSwapInterval();
TRACE(("EGL_SwapBuffers\n"));
TRACE(("swapping buffers\n"));
qeglSwapBuffers(egldpy, eglsurf);
/* TODO: check result? */
TRACE(("EGL_SwapBuffers done\n"));
}
qboolean EGL_InitDisplay (rendererstate_t *info, int eglplat, void *ndpy, EGLNativeDisplayType dpyid, EGLConfig *outconfig)
{
EGLint numconfig=0;
EGLConfig cfg=0;
EGLint major=0, minor=0;
EGLint attrib[] =
{
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
// EGL_BUFFER_SIZE, info->bpp,
// EGL_SAMPLES, info->multisample,
// EGL_STENCIL_SIZE, 8,
EGL_ALPHA_MASK_SIZE, 0,
EGL_DEPTH_SIZE, 16,
EGL_RED_SIZE, 1,
EGL_GREEN_SIZE, 1,
EGL_BLUE_SIZE, 1,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
/* if (!EGL_LoadLibrary(""))
{
Con_Printf(CON_ERROR "EGL: unable to load library!\n");
return false;
}
*/
if (qeglGetPlatformDisplay && eglplat)
egldpy = qeglGetPlatformDisplay(eglplat, ndpy, NULL/*attribs*/);
else
{
if (eglplat == EGL_PLATFORM_WAYLAND_KHR)
Con_Printf(CON_ERROR "EGL: eglGetPlatformDisplay[EXT] not supported. Your EGL implementation is probably too old.\n");
egldpy = qeglGetDisplay(dpyid);
}
if (egldpy == EGL_NO_DISPLAY)
{
Con_Printf(CON_WARNING "EGL: creating default display\n");
egldpy = qeglGetDisplay(EGL_DEFAULT_DISPLAY);
}
if (egldpy == EGL_NO_DISPLAY)
{
Con_Printf(CON_ERROR "EGL: can't get display!\n");
return false;
}
//NOTE: mesa's egl really loves to crash on this call, and I define crash as 'anything that fails to return to caller', which fucks everything up.
if (!qeglInitialize(egldpy, &major, &minor))
{
Con_Printf(CON_ERROR "EGL: can't initialize display!\n");
return false;
}
/*
if (!qeglGetConfigs(egldpy, NULL, 0, &numconfigs) || !numconfigs)
{
Con_Printf(CON_ERROR "EGL: can't get configs!\n");
return false;
}
*/
if (!qeglChooseConfig(egldpy, attrib, &cfg, 1, &numconfig))
{
Con_Printf(CON_ERROR "EGL: can't choose config!\n");
return false;
}
if (!numconfig)
{
Con_Printf(CON_ERROR "EGL: no configs!\n");
return false;
}
// EGL_ShowConfig(egldpy, cfg);
*outconfig = cfg;
return true;
}
qboolean EGL_InitWindow (rendererstate_t *info, int eglplat, void *nwindow, EGLNativeWindowType windowid, EGLConfig cfg)
{
EGLint contextattr[] =
{
EGL_CONTEXT_CLIENT_VERSION, 2, //requires EGL 1.3
EGL_NONE, EGL_NONE
};
if (qeglCreatePlatformWindowSurface)
{
EGLAttrib wndattrib[] =
{
// EGL_GL_COLORSPACE_KHR, EGL_GL_COLORSPACE_SRGB_KHR,
EGL_NONE
};
eglsurf = qeglCreatePlatformWindowSurface(egldpy, cfg, nwindow, info->srgb?wndattrib:NULL);
}
else
{
EGLint wndattrib[] =
{
// EGL_GL_COLORSPACE_KHR, EGL_GL_COLORSPACE_SRGB_KHR,
EGL_NONE
};
eglsurf = qeglCreateWindowSurface(egldpy, cfg, windowid, info->srgb?wndattrib:NULL);
}
if (eglsurf == EGL_NO_SURFACE)
{
int err = qeglGetError();
if (eglplat == EGL_PLATFORM_WAYLAND_KHR && err == EGL_BAD_DISPLAY) //slightly more friendly error that slags off nvidia for their refusal to implement existing standards, as is apparently appropriate.
Con_Printf(CON_ERROR "EGL: eglCreateWindowSurface failed: Bad Display. Your wayland setup is probably not properly supported by your video drivers.\n");
else
Con_Printf(CON_ERROR "EGL: eglCreateWindowSurface failed: %s\n", EGL_GetErrorString(err));
return false;
}
eglctx = qeglCreateContext(egldpy, cfg, EGL_NO_SURFACE, contextattr);
if (eglctx == EGL_NO_CONTEXT)
{
Con_Printf(CON_ERROR "EGL: no context!\n");
return false;
}
if (!qeglMakeCurrent(egldpy, eglsurf, eglsurf, eglctx))
{
Con_Printf(CON_ERROR "EGL: can't make current!\n");
return false;
}
if (eglplat == EGL_PLATFORM_WAYLAND_KHR)
{ //if we don't do this, only the first frame will get displayed, and we'll lock up
qeglSwapInterval = NULL;
Con_DPrintf(CON_WARNING"Wayland: Disabling vsync controls to work around wayland bug\n");
}
EGL_UpdateSwapInterval();
if (info->vr)
{
vrsetup_t vrsetup = {sizeof(vrsetup)};
vrsetup.vrplatform = VR_EGL;
vrsetup.egl.getprocaddr = qeglGetProcAddress;
vrsetup.egl.egldisplay = egldpy;
vrsetup.egl.eglconfig = cfg;
vrsetup.egl.eglcontext = eglctx;
if (!info->vr->Prepare(&vrsetup) ||
!info->vr->Init(&vrsetup, info))
{
info->vr->Shutdown();
info->vr = NULL;
}
else
vid.vr = info->vr;
}
return true;
}
#endif