From 6ee2df84452ab6a8fc25dfbafb993815a1d9fbf4 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Mon, 8 Jul 2019 13:40:29 +0900 Subject: [PATCH] Move vid callbacks into vid_internal Currently segfaults because viddef is an alias for *r_data->vid, which has not yet been initialized (chicken and egg). --- include/QF/vid.h | 12 +----------- include/vid_internal.h | 14 ++++++++++++++ libs/video/renderer/gl/gl_rmisc.c | 7 ++++--- libs/video/renderer/gl/gl_screen.c | 5 +++-- libs/video/renderer/gl/qfgl_ext.c | 7 ++++--- libs/video/renderer/glsl/glsl_main.c | 5 +++-- libs/video/renderer/glsl/glsl_screen.c | 3 ++- libs/video/renderer/glsl/qfglsl.c | 5 +++-- libs/video/renderer/r_init.c | 5 +++-- libs/video/renderer/sw/d_init.c | 6 +++--- libs/video/renderer/sw/draw.c | 2 +- libs/video/renderer/sw32/d_init.c | 6 +++--- libs/video/renderer/sw32/draw.c | 2 +- libs/video/renderer/vid_render_gl.c | 7 ++++--- libs/video/renderer/vid_render_glsl.c | 7 ++++--- libs/video/renderer/vid_render_vulkan.c | 7 ++++--- libs/video/targets/vid.c | 23 ++++++++++++----------- libs/video/targets/vid_sdl.c | 14 +++++++++----- libs/video/targets/vid_x11.c | 13 ++++++++----- 19 files changed, 86 insertions(+), 64 deletions(-) diff --git a/include/QF/vid.h b/include/QF/vid.h index 057414b2f..93975ced6 100644 --- a/include/QF/vid.h +++ b/include/QF/vid.h @@ -61,17 +61,7 @@ typedef struct { int conheight; byte *direct; // direct drawing to framebuffer, if not // NULL - int (*surf_cache_size)(int width, int height); - void (*flush_caches)(void); - void (*init_caches)(void *cache, int size); - void (*do_screen_buffer)(void); - void (*set_palette)(const byte *palette); - - // gl stuff - void (*load_gl)(void); - void (*init_gl)(void); - void *(*get_proc_address)(const char *name, qboolean crit); - void (*end_rendering)(void); + struct vid_internal_s *vid_internal; } viddef_t; #define viddef (*r_data->vid) diff --git a/include/vid_internal.h b/include/vid_internal.h index bf1803270..fc1538202 100644 --- a/include/vid_internal.h +++ b/include/vid_internal.h @@ -4,6 +4,20 @@ #include "QF/vid.h" #include "QF/plugin/vid_render.h" +typedef struct vid_internal_s { + int (*surf_cache_size) (int width, int height); + void (*flush_caches) (void); + void (*init_caches) (void *cache, int size); + void (*do_screen_buffer) (void); + void (*set_palette) (const byte *palette); + + // gl stuff + void (*load_gl) (void); + void (*init_gl) (void); + void *(*get_proc_address) (const char *name, qboolean crit); + void (*end_rendering) (void); +} vid_internal_t; + extern struct cvar_s *vid_fullscreen; extern struct cvar_s *vid_system_gamma; extern struct cvar_s *vid_gamma; diff --git a/libs/video/renderer/gl/gl_rmisc.c b/libs/video/renderer/gl/gl_rmisc.c index a399c26bd..e054e37bf 100644 --- a/libs/video/renderer/gl/gl_rmisc.c +++ b/libs/video/renderer/gl/gl_rmisc.c @@ -64,6 +64,7 @@ #include "mod_internal.h" #include "r_internal.h" #include "varrays.h" +#include "vid_internal.h" /* R_Envmap_f @@ -121,7 +122,7 @@ R_Envmap_f (void) gl_envmap = false; qfglDrawBuffer (GL_BACK); qfglReadBuffer (GL_BACK); - vid.end_rendering (); + vid.vid_internal->end_rendering (); } void @@ -249,13 +250,13 @@ gl_R_TimeRefresh_f (void) double start, stop, time; int i; - vid.end_rendering (); + vid.vid_internal->end_rendering (); start = Sys_DoubleTime (); for (i = 0; i < 128; i++) { r_refdef.viewangles[1] = i * (360.0 / 128.0); gl_R_RenderView (); - vid.end_rendering (); + vid.vid_internal->end_rendering (); } stop = Sys_DoubleTime (); diff --git a/libs/video/renderer/gl/gl_screen.c b/libs/video/renderer/gl/gl_screen.c index d463880d8..73674e70c 100644 --- a/libs/video/renderer/gl/gl_screen.c +++ b/libs/video/renderer/gl/gl_screen.c @@ -59,6 +59,7 @@ #include "compat.h" #include "r_internal.h" #include "sbar.h" +#include "vid_internal.h" /* SCREEN SHOTS */ @@ -206,7 +207,7 @@ gl_SCR_UpdateScreen (double realtime, SCR_Func scr_3dfunc, SCR_Func *scr_funcs) return; if (begun) - vid.end_rendering (); + vid.vid_internal->end_rendering (); vr_data.realtime = realtime; @@ -263,7 +264,7 @@ gl_SCR_UpdateScreen (double realtime, SCR_Func scr_3dfunc, SCR_Func *scr_funcs) qfglFlush (); if (gl_finish->int_val) { - vid.end_rendering (); + vid.vid_internal->end_rendering (); begun = 0; } } diff --git a/libs/video/renderer/gl/qfgl_ext.c b/libs/video/renderer/gl/qfgl_ext.c index a46472609..41fe1f52b 100644 --- a/libs/video/renderer/gl/qfgl_ext.c +++ b/libs/video/renderer/gl/qfgl_ext.c @@ -61,6 +61,7 @@ #include "QF/GL/funcs.h" #include "r_internal.h" +#include "vid_internal.h" // First we need to get all the function pointers declared. #define QFGL_WANT(ret, name, args) \ @@ -75,9 +76,9 @@ qboolean GLF_FindFunctions (void) { #define QFGL_WANT(ret, name, args) \ - qf##name = vid.get_proc_address (#name, false); + qf##name = vid.vid_internal->get_proc_address (#name, false); #define QFGL_NEED(ret, name, args) \ - qf##name = vid.get_proc_address (#name, true); + qf##name = vid.vid_internal->get_proc_address (#name, true); #include "QF/GL/qf_funcs_list.h" #undef QFGL_NEED #undef QFGL_WANT @@ -135,6 +136,6 @@ void * QFGL_ExtensionAddress (const char *name) { if (name) - return vid.get_proc_address (name, false); + return vid.vid_internal->get_proc_address (name, false); return NULL; } diff --git a/libs/video/renderer/glsl/glsl_main.c b/libs/video/renderer/glsl/glsl_main.c index 9fd3a9c0c..2bc60bdd4 100644 --- a/libs/video/renderer/glsl/glsl_main.c +++ b/libs/video/renderer/glsl/glsl_main.c @@ -58,6 +58,7 @@ #include "mod_internal.h" #include "r_internal.h" +#include "vid_internal.h" mat4_t glsl_projection; mat4_t glsl_view; @@ -304,13 +305,13 @@ glsl_R_TimeRefresh_f (void) double start, stop, time; int i; - vid.end_rendering (); + vid.vid_internal->end_rendering (); start = Sys_DoubleTime (); for (i = 0; i < 128; i++) { r_refdef.viewangles[1] = i * (360.0 / 128.0); glsl_R_RenderView (); - vid.end_rendering (); + vid.vid_internal->end_rendering (); } stop = Sys_DoubleTime (); diff --git a/libs/video/renderer/glsl/glsl_screen.c b/libs/video/renderer/glsl/glsl_screen.c index eab755325..ecdda6ec8 100644 --- a/libs/video/renderer/glsl/glsl_screen.c +++ b/libs/video/renderer/glsl/glsl_screen.c @@ -60,6 +60,7 @@ #include "QF/GLSL/qf_vid.h" #include "r_internal.h" +#include "vid_internal.h" /* Unknown renamed to GLErr_Unknown to solve conflict with winioctl.h */ static unsigned int GLErr_InvalidEnum; @@ -165,7 +166,7 @@ glsl_SCR_UpdateScreen (double realtime, SCR_Func scr_3dfunc, if (begun) { begun = 0; - vid.end_rendering (); + vid.vid_internal->end_rendering (); } vr_data.realtime = realtime; diff --git a/libs/video/renderer/glsl/qfglsl.c b/libs/video/renderer/glsl/qfglsl.c index af3a2f839..b4fd25662 100644 --- a/libs/video/renderer/glsl/qfglsl.c +++ b/libs/video/renderer/glsl/qfglsl.c @@ -60,6 +60,7 @@ #include "QF/GLSL/funcs.h" #include "r_internal.h" +#include "vid_internal.h" // First we need to get all the function pointers declared. #define QFGL_WANT(ret, name, args) \ @@ -74,9 +75,9 @@ qboolean EGLF_FindFunctions (void) { #define QFGL_WANT(ret, name, args) \ - qfe##name = vid.get_proc_address (#name, false); + qfe##name = vid.vid_internal->get_proc_address (#name, false); #define QFGL_NEED(ret, name, args) \ - qfe##name = vid.get_proc_address (#name, true); + qfe##name = vid.vid_internal->get_proc_address (#name, true); #include "QF/GLSL/qf_funcs_list.h" #undef QFGL_NEED #undef QFGL_WANT diff --git a/libs/video/renderer/r_init.c b/libs/video/renderer/r_init.c index d4a7e91cd..98acc39ad 100644 --- a/libs/video/renderer/r_init.c +++ b/libs/video/renderer/r_init.c @@ -46,6 +46,7 @@ #include "QF/plugin/general.h" #include "r_internal.h" +#include "vid_internal.h" cvar_t *vidrend_plugin; plugin_t *vidrendmodule = NULL; @@ -76,8 +77,8 @@ R_LoadModule (void (*load_gl)(void), void (*set_palette) (const byte *palette)) r_funcs = vidrendmodule->functions->vid_render; mod_funcs = r_funcs->model_funcs; r_data = vidrendmodule->data->vid_render; - r_data->vid->load_gl = load_gl; - r_data->vid->set_palette = set_palette; + r_data->vid->vid_internal->load_gl = load_gl; + r_data->vid->vid_internal->set_palette = set_palette; vidrendmodule->functions->general->p_Init (); } diff --git a/libs/video/renderer/sw/d_init.c b/libs/video/renderer/sw/d_init.c index 67e48e71b..00fb21a89 100644 --- a/libs/video/renderer/sw/d_init.c +++ b/libs/video/renderer/sw/d_init.c @@ -56,9 +56,9 @@ D_Init (void) r_worldpolysbacktofront = false; r_recursiveaffinetriangles = true; - vr_data.vid->surf_cache_size = D_SurfaceCacheForRes; - vr_data.vid->flush_caches = D_FlushCaches; - vr_data.vid->init_caches = D_InitCaches; + vr_data.vid->vid_internal->surf_cache_size = D_SurfaceCacheForRes; + vr_data.vid->vid_internal->flush_caches = D_FlushCaches; + vr_data.vid->vid_internal->init_caches = D_InitCaches; VID_InitBuffers (); } diff --git a/libs/video/renderer/sw/draw.c b/libs/video/renderer/sw/draw.c index e3728af66..775888a21 100644 --- a/libs/video/renderer/sw/draw.c +++ b/libs/video/renderer/sw/draw.c @@ -825,5 +825,5 @@ Draw_BlendScreen (quat_t color) newpal[2] = vid.gammatable[b]; newpal += 3; } - vid.set_palette (pal); + vid.vid_internal->set_palette (pal); } diff --git a/libs/video/renderer/sw32/d_init.c b/libs/video/renderer/sw32/d_init.c index ccb5f7392..e786cf32b 100644 --- a/libs/video/renderer/sw32/d_init.c +++ b/libs/video/renderer/sw32/d_init.c @@ -66,9 +66,9 @@ sw32_D_Init (void) sw32_d_zitable[i] = (65536.0 * 65536.0 / (double) i); } - vr_data.vid->surf_cache_size = sw32_D_SurfaceCacheForRes; - vr_data.vid->flush_caches = sw32_D_FlushCaches; - vr_data.vid->init_caches = sw32_D_InitCaches; + vr_data.vid->vid_internal->surf_cache_size = sw32_D_SurfaceCacheForRes; + vr_data.vid->vid_internal->flush_caches = sw32_D_FlushCaches; + vr_data.vid->vid_internal->init_caches = sw32_D_InitCaches; VID_InitBuffers (); VID_MakeColormaps(); diff --git a/libs/video/renderer/sw32/draw.c b/libs/video/renderer/sw32/draw.c index a420c8ac0..13454c437 100644 --- a/libs/video/renderer/sw32/draw.c +++ b/libs/video/renderer/sw32/draw.c @@ -1299,7 +1299,7 @@ sw32_Draw_BlendScreen (quat_t color) newpal[2] = vid.gammatable[b]; newpal += 3; } - vid.set_palette (pal); + vid.vid_internal->set_palette (pal); } break; case 2: diff --git a/libs/video/renderer/vid_render_gl.c b/libs/video/renderer/vid_render_gl.c index 42f5df1dc..91f4178b5 100644 --- a/libs/video/renderer/vid_render_gl.c +++ b/libs/video/renderer/vid_render_gl.c @@ -38,6 +38,7 @@ #include "mod_internal.h" #include "r_internal.h" +#include "vid_internal.h" #include "gl/namehack.h" @@ -126,9 +127,9 @@ vid_render_funcs_t gl_vid_render_funcs = { static void gl_vid_render_init (void) { - vr_data.vid->set_palette = GL_SetPalette; - vr_data.vid->init_gl = GL_Init_Common; - vr_data.vid->load_gl (); + vr_data.vid->vid_internal->set_palette = GL_SetPalette; + vr_data.vid->vid_internal->init_gl = GL_Init_Common; + vr_data.vid->vid_internal->load_gl (); vr_funcs = &gl_vid_render_funcs; m_funcs = &model_funcs; } diff --git a/libs/video/renderer/vid_render_glsl.c b/libs/video/renderer/vid_render_glsl.c index f162231d1..d1f380ae5 100644 --- a/libs/video/renderer/vid_render_glsl.c +++ b/libs/video/renderer/vid_render_glsl.c @@ -38,6 +38,7 @@ #include "mod_internal.h" #include "r_internal.h" +#include "vid_internal.h" #include "glsl/namehack.h" @@ -126,9 +127,9 @@ vid_render_funcs_t glsl_vid_render_funcs = { static void glsl_vid_render_init (void) { - vr_data.vid->set_palette = GLSL_SetPalette; - vr_data.vid->init_gl = GLSL_Init_Common; - vr_data.vid->load_gl (); + vr_data.vid->vid_internal->set_palette = GLSL_SetPalette; + vr_data.vid->vid_internal->init_gl = GLSL_Init_Common; + vr_data.vid->vid_internal->load_gl (); vr_funcs = &glsl_vid_render_funcs; m_funcs = &model_funcs; } diff --git a/libs/video/renderer/vid_render_vulkan.c b/libs/video/renderer/vid_render_vulkan.c index ec5ee4ea3..c71264fcb 100644 --- a/libs/video/renderer/vid_render_vulkan.c +++ b/libs/video/renderer/vid_render_vulkan.c @@ -38,6 +38,7 @@ #include "mod_internal.h" #include "r_internal.h" +#include "vid_internal.h" #include "vulkan/namehack.h" @@ -134,9 +135,9 @@ set_palette (const byte *palette) static void vulkan_vid_render_init (void) { - vr_data.vid->set_palette = set_palette; - vr_data.vid->init_gl = Vulkan_Init_Common; - vr_data.vid->load_gl (); + vr_data.vid->vid_internal->set_palette = set_palette; + vr_data.vid->vid_internal->init_gl = Vulkan_Init_Common; + vr_data.vid->vid_internal->load_gl (); vr_funcs = &vulkan_vid_render_funcs; m_funcs = &model_funcs; } diff --git a/libs/video/targets/vid.c b/libs/video/targets/vid.c index d7aeb3b2d..651101539 100644 --- a/libs/video/targets/vid.c +++ b/libs/video/targets/vid.c @@ -214,7 +214,7 @@ VID_UpdateGamma (cvar_t *vid_gamma) VID_BuildGammaTable (gamma); for (i = 0; i < 256 * 3; i++) viddef.palette[i] = viddef.gammatable[viddef.basepal[i]]; - viddef.set_palette (viddef.palette); // update with the new palette + viddef.vid_internal->set_palette (viddef.palette); // update with the new palette } } @@ -256,8 +256,9 @@ VID_InitBuffers (void) // Calculate the sizes we want first buffersize = viddef.rowbytes * viddef.height; zbuffersize = viddef.width * viddef.height * sizeof (*viddef.zbuffer); - if (viddef.surf_cache_size) - cachesize = viddef.surf_cache_size (viddef.width, viddef.height); + if (viddef.vid_internal->surf_cache_size) + cachesize = viddef.vid_internal->surf_cache_size (viddef.width, + viddef.height); // Free the old z-buffer if (viddef.zbuffer) { @@ -266,13 +267,13 @@ VID_InitBuffers (void) } // Free the old surface cache if (viddef.surfcache) { - if (viddef.flush_caches) - viddef.flush_caches (); + if (viddef.vid_internal->flush_caches) + viddef.vid_internal->flush_caches (); free (viddef.surfcache); viddef.surfcache = NULL; } - if (viddef.do_screen_buffer) { - viddef.do_screen_buffer (); + if (viddef.vid_internal->do_screen_buffer) { + viddef.vid_internal->do_screen_buffer (); } else { // Free the old screen buffer if (viddef.buffer) { @@ -302,13 +303,13 @@ VID_InitBuffers (void) Sys_Error ("Not enough memory for video mode"); } - if (viddef.init_caches) - viddef.init_caches (viddef.surfcache, cachesize); + if (viddef.vid_internal->init_caches) + viddef.vid_internal->init_caches (viddef.surfcache, cachesize); } void VID_ClearMemory (void) { - if (viddef.flush_caches) - viddef.flush_caches (); + if (viddef.vid_internal->flush_caches) + viddef.vid_internal->flush_caches (); } diff --git a/libs/video/targets/vid_sdl.c b/libs/video/targets/vid_sdl.c index b32efbee1..e48baea51 100644 --- a/libs/video/targets/vid_sdl.c +++ b/libs/video/targets/vid_sdl.c @@ -78,6 +78,8 @@ SDL_Surface *screen = NULL; # endif #endif +static vid_internal_t vid_internal; + static void (*set_vid_mode) (Uint32 flags); static void (GLAPIENTRY *qfglFinish) (void); @@ -157,7 +159,7 @@ sdlgl_set_vid_mode (Uint32 flags) success: viddef.numpages = 2; - viddef.init_gl (); + viddef.vid_internal->init_gl (); } static void @@ -170,8 +172,8 @@ sdlgl_end_rendering (void) static void sdl_load_gl (void) { - viddef.get_proc_address = QFGL_ProcAddress; - viddef.end_rendering = sdlgl_end_rendering; + viddef.vid_internal->get_proc_address = QFGL_ProcAddress; + viddef.vid_internal->end_rendering = sdlgl_end_rendering; set_vid_mode = sdlgl_set_vid_mode; if (SDL_GL_LoadLibrary (gl_driver->string) != 0) @@ -220,7 +222,7 @@ sdl_set_vid_mode (Uint32 flags) // now know everything we need to know about the buffer VGA_width = viddef.width; VGA_height = viddef.height; - viddef.do_screen_buffer = do_screen_buffer; + viddef.vid_internal->do_screen_buffer = do_screen_buffer; VGA_pagebase = viddef.buffer = screen->pixels; VGA_rowbytes = viddef.rowbytes = screen->pitch; viddef.conbuffer = viddef.buffer; @@ -235,6 +237,8 @@ VID_Init (byte *palette, byte *colormap) { Uint32 flags; + viddef.vid_internal = &vid_internal; + set_vid_mode = sdl_set_vid_mode; // Load the SDL library @@ -269,7 +273,7 @@ VID_Init (byte *palette, byte *colormap) VID_SDL_GammaCheck (); VID_InitGamma (palette); - viddef.set_palette (viddef.palette); + viddef.vid_internal->set_palette (viddef.palette); viddef.initialized = true; diff --git a/libs/video/targets/vid_x11.c b/libs/video/targets/vid_x11.c index 1050d6adb..905620080 100644 --- a/libs/video/targets/vid_x11.c +++ b/libs/video/targets/vid_x11.c @@ -79,6 +79,7 @@ #include "dga_check.h" #include "vid_internal.h" +static vid_internal_t vid_internal; int XShmGetEventBase (Display *x); // for broken X11 headers static GC x_gc; @@ -209,7 +210,7 @@ glx_create_context (void) XSync (x_disp, 0); ctx = qfglXCreateContext (x_disp, x_visinfo, NULL, True); qfglXMakeCurrent (x_disp, x_win, ctx); - viddef.init_gl (); + viddef.vid_internal->init_gl (); } static void @@ -227,8 +228,8 @@ glx_load_gl (void) choose_visual = glx_choose_visual; create_context = glx_create_context; - viddef.get_proc_address = QFGL_ProcAddress; - viddef.end_rendering = glx_end_rendering; + viddef.vid_internal->get_proc_address = QFGL_ProcAddress; + viddef.vid_internal->end_rendering = glx_end_rendering; #ifdef RTLD_GLOBAL flags |= RTLD_GLOBAL; @@ -623,7 +624,7 @@ x11_create_context (void) x_shmeventtype = XShmGetEventBase (x_disp) + ShmCompletion; } - viddef.do_screen_buffer = x11_init_buffers; + viddef.vid_internal->do_screen_buffer = x11_init_buffers; VID_InitBuffers (); // XSynchronize (x_disp, False); @@ -666,6 +667,8 @@ VID_SetPalette (const byte *palette) void VID_Init (byte *palette, byte *colormap) { + viddef.vid_internal = &vid_internal; + choose_visual = x11_choose_visual; create_context = x11_create_context; @@ -686,7 +689,7 @@ VID_Init (byte *palette, byte *colormap) create_context (); VID_InitGamma (palette); - viddef.set_palette (viddef.palette); + viddef.vid_internal->set_palette (viddef.palette); Sys_MaskPrintf (SYS_VID, "Video mode %dx%d initialized.\n", viddef.width, viddef.height);