]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - vid_shared.c
Enable PRVM_64 - the QuakeC VM will run as 64bit (using int64/double rather than...
[xonotic/darkplaces.git] / vid_shared.c
index c60e6efc7c4900b3088b236e98e5745c7b593f3d..0c9ee5ed8d00b3ddae7d83077316e6ea8746d185 100644 (file)
@@ -140,9 +140,6 @@ cvar_t vid_soft = {CVAR_SAVE, "vid_soft", "0", "enables use of the DarkPlaces So
 cvar_t vid_soft_threads = {CVAR_SAVE, "vid_soft_threads", "8", "the number of threads the DarkPlaces Software Rasterizer should use"}; 
 cvar_t vid_soft_interlace = {CVAR_SAVE, "vid_soft_interlace", "1", "whether the DarkPlaces Software Rasterizer should interlace the screen bands occupied by each thread"};
 
-// we don't know until we try it!
-cvar_t vid_hardwaregammasupported = {CVAR_READONLY,"vid_hardwaregammasupported","1", "indicates whether hardware gamma is supported (updated by attempts to set hardware gamma ramps)"};
-
 // VorteX: more info cvars, mostly set in VID_CheckExtensions
 cvar_t gl_info_vendor = {CVAR_READONLY, "gl_info_vendor", "", "indicates brand of graphics chip"};
 cvar_t gl_info_renderer = {CVAR_READONLY, "gl_info_renderer", "", "indicates graphics chip model and other information"};
@@ -151,13 +148,6 @@ cvar_t gl_info_extensions = {CVAR_READONLY, "gl_info_extensions", "", "indicates
 cvar_t gl_info_platform = {CVAR_READONLY, "gl_info_platform", "", "indicates GL platform: WGL, GLX, or AGL."};
 cvar_t gl_info_driver = {CVAR_READONLY, "gl_info_driver", "", "name of driver library (opengl32.dll, libGL.so.1, or whatever)."};
 
-// whether hardware gamma ramps are currently in effect
-qboolean vid_usinghwgamma = false;
-
-int vid_gammarampsize = 0;
-unsigned short *vid_gammaramps = NULL;
-unsigned short *vid_systemgammaramps = NULL;
-
 cvar_t vid_fullscreen = {CVAR_SAVE, "vid_fullscreen", "1", "use fullscreen (1) or windowed (0)"};
 cvar_t vid_width = {CVAR_SAVE, "vid_width", "640", "resolution"};
 cvar_t vid_height = {CVAR_SAVE, "vid_height", "480", "resolution"};
@@ -205,10 +195,8 @@ cvar_t v_color_grey_b = {CVAR_SAVE, "v_color_grey_b", "0.5", "desired color of g
 cvar_t v_color_white_r = {CVAR_SAVE, "v_color_white_r", "1", "desired color of white"};
 cvar_t v_color_white_g = {CVAR_SAVE, "v_color_white_g", "1", "desired color of white"};
 cvar_t v_color_white_b = {CVAR_SAVE, "v_color_white_b", "1", "desired color of white"};
-cvar_t v_hwgamma = {CVAR_SAVE, "v_hwgamma", "0", "enables use of hardware gamma correction ramps if available (note: does not work very well on Windows2000 and above), values are 0 = off, 1 = attempt to use hardware gamma, 2 = use hardware gamma whether it works or not"};
-cvar_t v_glslgamma = {CVAR_SAVE, "v_glslgamma", "1", "enables use of GLSL to apply gamma correction ramps if available (note: overrides v_hwgamma)"};
-cvar_t v_glslgamma_2d = {CVAR_SAVE, "v_glslgamma_2d", "0", "applies GLSL gamma to 2d pictures (HUD, fonts)"};
-cvar_t v_psycho = {0, "v_psycho", "0", "easter egg"};
+cvar_t v_glslgamma_2d = {CVAR_SAVE, "v_glslgamma_2d", "1", "applies GLSL gamma to 2d pictures (HUD, fonts)"};
+cvar_t v_psycho = {0, "v_psycho", "0", "easter egg - R.I.P. zinx http://obits.al.com/obituaries/birmingham/obituary.aspx?n=christopher-robert-lais&pid=186080667"};
 
 // brand of graphics chip
 const char *gl_vendor;
@@ -1071,10 +1059,7 @@ void VID_CheckExtensions(void)
        vid.support.arb_texture_cube_map = GL_CheckExtension("GL_ARB_texture_cube_map", NULL, "-nocubemap", false);
        vid.support.arb_texture_env_combine = GL_CheckExtension("GL_ARB_texture_env_combine", NULL, "-nocombine", false) || GL_CheckExtension("GL_EXT_texture_env_combine", NULL, "-nocombine", false);
        vid.support.arb_texture_gather = GL_CheckExtension("GL_ARB_texture_gather", NULL, "-notexturegather", false);
-#ifndef __APPLE__
-       // LordHavoc: too many bugs on OSX!
        vid.support.arb_texture_non_power_of_two = GL_CheckExtension("GL_ARB_texture_non_power_of_two", NULL, "-notexturenonpoweroftwo", false);
-#endif
        vid.support.arb_vertex_buffer_object = GL_CheckExtension("GL_ARB_vertex_buffer_object", vbofuncs, "-novbo", false);
        vid.support.arb_uniform_buffer_object = GL_CheckExtension("GL_ARB_uniform_buffer_object", ubofuncs, "-noubo", false);
        vid.support.ati_separate_stencil = GL_CheckExtension("separatestencil", gl2separatestencilfuncs, "-noseparatestencil", true) || GL_CheckExtension("GL_ATI_separate_stencil", atiseparatestencilfuncs, "-noseparatestencil", false);
@@ -1163,7 +1148,7 @@ void VID_CheckExtensions(void)
        vid.texunits = vid.teximageunits = vid.texarrayunits = 1;
        if (vid.support.arb_multitexture)
                qglGetIntegerv(GL_MAX_TEXTURE_UNITS, (GLint*)&vid.texunits);
-       if (vid_gl20.integer && vid.support.gl20shaders)
+       if (vid_gl20.integer && vid.support.gl20shaders && vid.support.ext_framebuffer_object && vid.support.arb_texture_non_power_of_two)
        {
                qglGetIntegerv(GL_MAX_TEXTURE_UNITS, (GLint*)&vid.texunits);
                qglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, (int *)&vid.teximageunits);CHECKGLERROR
@@ -1171,7 +1156,7 @@ void VID_CheckExtensions(void)
                vid.texunits = bound(4, vid.texunits, MAX_TEXTUREUNITS);
                vid.teximageunits = bound(16, vid.teximageunits, MAX_TEXTUREUNITS);
                vid.texarrayunits = bound(8, vid.texarrayunits, MAX_TEXTUREUNITS);
-               Con_DPrintf("Using GL2.0 rendering path - %i texture matrix, %i texture images, %i texcoords%s\n", vid.texunits, vid.teximageunits, vid.texarrayunits, vid.support.ext_framebuffer_object ? ", shadowmapping supported" : "");
+               Con_DPrintf("Using GL2.0+fbo rendering path - %i texture matrix, %i texture images, %i texcoords%s\n", vid.texunits, vid.teximageunits, vid.texarrayunits, vid.support.ext_framebuffer_object ? ", shadowmapping supported" : "");
                vid.renderpath = RENDERPATH_GL20;
                vid.sRGBcapable2D = false;
                vid.sRGBcapable3D = true;
@@ -1474,7 +1459,22 @@ static void Force_CenterView_f (void)
 
 static int gamma_forcenextframe = false;
 static float cachegamma, cachebrightness, cachecontrast, cacheblack[3], cachegrey[3], cachewhite[3], cachecontrastboost;
-static int cachecolorenable, cachehwgamma;
+static int cachecolorenable;
+
+void VID_ApplyGammaToColor(const float *rgb, float *out)
+{
+       int i;
+       if (cachecolorenable)
+       {
+               for (i = 0; i < 3; i++)
+                       out[i] = pow(cachecontrastboost * rgb[i] / ((cachecontrastboost - 1) * rgb[i] + 1), 1.0 / invpow(0.5, 1 - cachegrey[i])) * cachewhite[i] + cacheblack[i];
+       }
+       else
+       {
+               for (i = 0; i < 3; i++)
+                       out[i] = pow(cachecontrastboost * rgb[i] / ((cachecontrastboost - 1) * rgb[i] + 1), 1.0 / cachegamma) * cachecontrast + cachebrightness;
+       }
+}
 
 unsigned int vid_gammatables_serial = 0; // so other subsystems can poll if gamma parameters have changed
 qboolean vid_gammatables_trivial = true;
@@ -1503,6 +1503,8 @@ void VID_BuildGammaTables(unsigned short *ramps, int rampsize)
        // LordHavoc: this code came from Ben Winslow and Zinx Verituse, I have
        // immensely butchered it to work with variable framerates and fit in with
        // the rest of darkplaces.
+       //
+       // R.I.P. zinx http://obits.al.com/obituaries/birmingham/obituary.aspx?n=christopher-robert-lais&pid=186080667
        if (v_psycho.integer)
        {
                int x, y;
@@ -1540,36 +1542,12 @@ void VID_BuildGammaTables(unsigned short *ramps, int rampsize)
        }
 }
 
-void VID_UpdateGamma(qboolean force, int rampsize)
+void VID_UpdateGamma(void)
 {
        cvar_t *c;
        float f;
-       int wantgamma;
        qboolean gamma_changed = false;
 
-       // LordHavoc: don't mess with gamma tables if running dedicated
-       if (cls.state == ca_dedicated)
-               return;
-
-       wantgamma = v_hwgamma.integer;
-       switch(vid.renderpath)
-       {
-       case RENDERPATH_GL20:
-       case RENDERPATH_D3D9:
-       case RENDERPATH_D3D10:
-       case RENDERPATH_D3D11:
-       case RENDERPATH_SOFT:
-       case RENDERPATH_GLES2:
-               if (v_glslgamma.integer)
-                       wantgamma = 0;
-               break;
-       case RENDERPATH_GL11:
-       case RENDERPATH_GL13:
-       case RENDERPATH_GLES1:
-               break;
-       }
-       if(!vid_activewindow)
-               wantgamma = 0;
 #define BOUNDCVAR(cvar, m1, m2) c = &(cvar);f = bound(m1, c->value, m2);if (c->value != f) Cvar_SetValueQuick(c, f);
        BOUNDCVAR(v_gamma, 0.1, 5);
        BOUNDCVAR(v_contrast, 0.2, 5);
@@ -1615,6 +1593,7 @@ void VID_UpdateGamma(qboolean force, int rampsize)
                }
        }
 
+       // if any gamma settings were changed, bump vid_gammatables_serial so we regenerate the gamma ramp texture
 #define GAMMACHECK(cache, value) if (cache != (value)) gamma_changed = true;cache = (value)
        if(v_psycho.integer)
                gamma_changed = true;
@@ -1635,64 +1614,7 @@ void VID_UpdateGamma(qboolean force, int rampsize)
 
        if(gamma_changed)
                ++vid_gammatables_serial;
-
-       GAMMACHECK(cachehwgamma    , wantgamma);
 #undef GAMMACHECK
-
-       if (!force && !gamma_forcenextframe && !gamma_changed)
-               return;
-
-       gamma_forcenextframe = false;
-
-       if (cachehwgamma)
-       {
-               if (!vid_usinghwgamma)
-               {
-                       vid_usinghwgamma = true;
-                       if (vid_gammarampsize != rampsize || !vid_gammaramps)
-                       {
-                               vid_gammarampsize = rampsize;
-                               if (vid_gammaramps)
-                                       Z_Free(vid_gammaramps);
-                               vid_gammaramps = (unsigned short *)Z_Malloc(6 * vid_gammarampsize * sizeof(unsigned short));
-                               vid_systemgammaramps = vid_gammaramps + 3 * vid_gammarampsize;
-                       }
-                       VID_GetGamma(vid_systemgammaramps, vid_gammarampsize);
-               }
-
-               VID_BuildGammaTables(vid_gammaramps, vid_gammarampsize);
-
-               // set vid_hardwaregammasupported to true if VID_SetGamma succeeds, OR if vid_hwgamma is >= 2 (forced gamma - ignores driver return value)
-               Cvar_SetValueQuick(&vid_hardwaregammasupported, VID_SetGamma(vid_gammaramps, vid_gammarampsize) || cachehwgamma >= 2);
-               // if custom gamma ramps failed (Windows stupidity), restore to system gamma
-               if(!vid_hardwaregammasupported.integer)
-               {
-                       if (vid_usinghwgamma)
-                       {
-                               vid_usinghwgamma = false;
-                               VID_SetGamma(vid_systemgammaramps, vid_gammarampsize);
-                       }
-               }
-       }
-       else
-       {
-               if (vid_usinghwgamma)
-               {
-                       vid_usinghwgamma = false;
-                       VID_SetGamma(vid_systemgammaramps, vid_gammarampsize);
-               }
-       }
-}
-
-void VID_RestoreSystemGamma(void)
-{
-       if (vid_usinghwgamma)
-       {
-               vid_usinghwgamma = false;
-               Cvar_SetValueQuick(&vid_hardwaregammasupported, VID_SetGamma(vid_systemgammaramps, vid_gammarampsize));
-               // force gamma situation to be reexamined next frame
-               gamma_forcenextframe = true;
-       }
 }
 
 #ifdef WIN32
@@ -1728,7 +1650,6 @@ void VID_Shared_Init(void)
        Con_Printf("DPSOFTRAST not available (SSE2 not compiled in)\n");
 #endif
 
-       Cvar_RegisterVariable(&vid_hardwaregammasupported);
        Cvar_RegisterVariable(&gl_info_vendor);
        Cvar_RegisterVariable(&gl_info_renderer);
        Cvar_RegisterVariable(&gl_info_version);
@@ -1751,8 +1672,6 @@ void VID_Shared_Init(void)
        Cvar_RegisterVariable(&v_color_white_g);
        Cvar_RegisterVariable(&v_color_white_b);
 
-       Cvar_RegisterVariable(&v_hwgamma);
-       Cvar_RegisterVariable(&v_glslgamma);
        Cvar_RegisterVariable(&v_glslgamma_2d);
 
        Cvar_RegisterVariable(&v_psycho);
@@ -1894,7 +1813,7 @@ static int VID_Mode(int fullscreen, int width, int height, int bpp, float refres
                        (vid_sRGB_fallback.integer >= 3) // force fallback
                        ||
                        (vid_sRGB_fallback.integer >= 2 && // fallback if framebuffer is 8bit
-                               !(r_viewfbo.integer >= 2 && vid.support.ext_framebuffer_object && vid.support.arb_texture_non_power_of_two && vid.samples < 2))
+                               r_viewfbo.integer < 2)
                )
                        vid.sRGB2D = vid.sRGB3D = false;
 
@@ -1926,6 +1845,7 @@ static int VID_Mode(int fullscreen, int width, int height, int bpp, float refres
 
 static void VID_OpenSystems(void)
 {
+       Key_ReleaseAll();
        R_Modules_Start();
        S_Startup();
 }
@@ -1934,6 +1854,7 @@ static void VID_CloseSystems(void)
 {
        S_Shutdown();
        R_Modules_Shutdown();
+       Key_ReleaseAll();
 }
 
 qboolean vid_commandlinecheck = true;