+static float cachegamma, cachebrightness, cachecontrast, cacheblack[3], cachegrey[3], cachewhite[3];
+static int cacheoverbrightbits = -1, cachecolorenable, cachehwgamma;
+#define BOUNDCVAR(cvar, m1, m2) c = &(cvar);f = bound(m1, c->value, m2);if (c->value != f) Cvar_SetValueQuick(c, f);
+void VID_UpdateGamma(qboolean force)
+{
+ cvar_t *c;
+ float f;
+
+ // LordHavoc: don't mess with gamma tables if running dedicated
+ if (cls.state == ca_dedicated)
+ return;
+
+ if (!force
+ && vid_usinghwgamma == (vid_allowhwgamma && v_hwgamma.integer)
+ && v_overbrightbits.integer == cacheoverbrightbits
+ && v_gamma.value == cachegamma
+ && v_contrast.value == cachecontrast
+ && v_brightness.value == cachebrightness
+ && cachecolorenable == v_color_enable.integer
+ && cacheblack[0] == v_color_black_r.value
+ && cacheblack[1] == v_color_black_g.value
+ && cacheblack[2] == v_color_black_b.value
+ && cachegrey[0] == v_color_grey_r.value
+ && cachegrey[1] == v_color_grey_g.value
+ && cachegrey[2] == v_color_grey_b.value
+ && cachewhite[0] == v_color_white_r.value
+ && cachewhite[1] == v_color_white_g.value
+ && cachewhite[2] == v_color_white_b.value)
+ return;
+
+ if (vid_allowhwgamma && v_hwgamma.integer)
+ {
+ if (!vid_usinghwgamma)
+ {
+ vid_usinghwgamma = true;
+ vid_hardwaregammasupported = VID_GetGamma(vid_systemgammaramps);
+ }
+
+ BOUNDCVAR(v_gamma, 0.1, 5);cachegamma = v_gamma.value;
+ BOUNDCVAR(v_contrast, 1, 5);cachecontrast = v_contrast.value;
+ BOUNDCVAR(v_brightness, 0, 0.8);cachebrightness = v_brightness.value;
+ BOUNDCVAR(v_color_black_r, 0, 0.8);cacheblack[0] = v_color_black_r.value;
+ BOUNDCVAR(v_color_black_g, 0, 0.8);cacheblack[1] = v_color_black_g.value;
+ BOUNDCVAR(v_color_black_b, 0, 0.8);cacheblack[2] = v_color_black_b.value;
+ BOUNDCVAR(v_color_grey_r, 0, 0.95);cachegrey[0] = v_color_grey_r.value;
+ BOUNDCVAR(v_color_grey_g, 0, 0.95);cachegrey[1] = v_color_grey_g.value;
+ BOUNDCVAR(v_color_grey_b, 0, 0.95);cachegrey[2] = v_color_grey_b.value;
+ BOUNDCVAR(v_color_white_r, 1, 5);cachewhite[0] = v_color_white_r.value;
+ BOUNDCVAR(v_color_white_g, 1, 5);cachewhite[1] = v_color_white_g.value;
+ BOUNDCVAR(v_color_white_b, 1, 5);cachewhite[2] = v_color_white_b.value;
+ cachecolorenable = v_color_enable.integer;
+ cacheoverbrightbits = v_overbrightbits.integer;
+ cachehwgamma = v_hwgamma.integer;
+
+ if (cachecolorenable)
+ {
+ BuildGammaTable16((float) (1 << cacheoverbrightbits), invpow(0.5, 1 - cachegrey[0]), cachewhite[0], cacheblack[0], vid_gammaramps);
+ BuildGammaTable16((float) (1 << cacheoverbrightbits), invpow(0.5, 1 - cachegrey[1]), cachewhite[1], cacheblack[1], vid_gammaramps + 256);
+ BuildGammaTable16((float) (1 << cacheoverbrightbits), invpow(0.5, 1 - cachegrey[2]), cachewhite[2], cacheblack[2], vid_gammaramps + 512);
+ }
+ else
+ {
+ BuildGammaTable16((float) (1 << cacheoverbrightbits), cachegamma, cachecontrast, cachebrightness, vid_gammaramps);
+ BuildGammaTable16((float) (1 << cacheoverbrightbits), cachegamma, cachecontrast, cachebrightness, vid_gammaramps + 256);
+ BuildGammaTable16((float) (1 << cacheoverbrightbits), cachegamma, cachecontrast, cachebrightness, vid_gammaramps + 512);
+ }
+
+ vid_hardwaregammasupported = VID_SetGamma(vid_gammaramps);
+ }
+ else
+ {
+ if (vid_usinghwgamma)
+ {
+ vid_usinghwgamma = false;
+ vid_hardwaregammasupported = VID_SetGamma(vid_systemgammaramps);
+ }
+ }
+}
+
+void VID_RestoreSystemGamma(void)
+{
+ if (vid_usinghwgamma)
+ {
+ vid_usinghwgamma = false;
+ VID_SetGamma(vid_systemgammaramps);
+ }
+}
+