]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - vid_shared.c
logarithm
[xonotic/darkplaces.git] / vid_shared.c
index 620a757a4d3bee2055bcce6bd6c5db03d370fbe9..2af2694351df95a99372fa80b571c21224259c77 100644 (file)
@@ -20,6 +20,7 @@ float in_windowmouse_x, in_windowmouse_y;
 int gl_max_texture_size = 0;
 int gl_max_3d_texture_size = 0;
 int gl_max_cube_map_texture_size = 0;
+int gl_max_rectangle_texture_size = 0;
 // GL_ARB_multitexture
 int gl_textureunits = 1;
 // GL_ARB_texture_env_combine or GL_EXT_texture_env_combine
@@ -34,10 +35,16 @@ int gl_stencil = false;
 int gl_texture3d = false;
 // GL_ARB_texture_cubemap
 int gl_texturecubemap = false;
+// GL_ARB_texture_rectangle
+int gl_texturerectangle = false;
 // GL_ARB_texture_non_power_of_two
 int gl_support_arb_texture_non_power_of_two = false;
 // GL_ARB_texture_env_dot3
 int gl_dot3arb = false;
+// GL_ARB_depth_texture
+int gl_depthtexture = false;
+// GL_ARB_shadow
+int gl_support_arb_shadow = false;
 // GL_SGIS_texture_edge_clamp
 int gl_support_clamptoedge = false;
 // GL_EXT_texture_filter_anisotropic
@@ -61,10 +68,16 @@ int gl_support_vertex_shader = false;
 int gl_support_fragment_shader = false;
 //GL_ARB_vertex_buffer_object
 int gl_support_arb_vertex_buffer_object = false;
+//GL_EXT_framebuffer_object
+int gl_support_ext_framebuffer_object = false;
 //GL_ARB_texture_compression
 int gl_support_texture_compression = false;
 //GL_ARB_occlusion_query
 int gl_support_arb_occlusion_query = false;
+//GL_AMD_texture_texture4
+int gl_support_amd_texture_texture4 = false;
+//GL_ARB_texture_gather
+int gl_support_arb_texture_gather = false;
 
 // LordHavoc: if window is hidden, don't update screen
 qboolean vid_hidden = true;
@@ -95,7 +108,7 @@ cvar_t vid_mouse = {CVAR_SAVE, "vid_mouse", "1", "whether to use the mouse in wi
 cvar_t vid_grabkeyboard = {CVAR_SAVE, "vid_grabkeyboard", "0", "whether to grab the keyboard when mouse is active (prevents use of volume control keys, music player keys, etc on some keyboards)"};
 cvar_t vid_minwidth = {0, "vid_minwidth", "0", "minimum vid_width that is acceptable (to be set in default.cfg in mods)"};
 cvar_t vid_minheight = {0, "vid_minheight", "0", "minimum vid_height that is acceptable (to be set in default.cfg in mods)"};
-cvar_t gl_combine = {0, "gl_combine", "1", "faster rendering by using GL_ARB_texture_env_combine extension (part of OpenGL 1.3 and above)"};
+cvar_t gl_combine = {0, "gl_combine", "1", "enables faster rendering using GL_ARB_texture_env_combine extension (part of OpenGL 1.3 and above)"};
 cvar_t gl_finish = {0, "gl_finish", "0", "make the cpu wait for the graphics processor at the end of each rendered frame (can help with strange input or video lag problems on some machines)"};
 
 cvar_t vid_stick_mouse = {CVAR_SAVE, "vid_stick_mouse", "0", "have the mouse stuck in the center of the screen" };
@@ -376,6 +389,25 @@ GLboolean (GLAPIENTRY *qglUnmapBufferARB) (GLenum target);
 void (GLAPIENTRY *qglBufferDataARB) (GLenum target, GLsizeiptrARB size, const GLvoid *data, GLenum usage);
 void (GLAPIENTRY *qglBufferSubDataARB) (GLenum target, GLintptrARB offset, GLsizeiptrARB size, const GLvoid *data);
 
+//GL_EXT_framebuffer_object
+GLboolean (GLAPIENTRY *qglIsRenderbufferEXT)(GLuint renderbuffer);
+void (GLAPIENTRY *qglBindRenderbufferEXT)(GLenum target, GLuint renderbuffer);
+void (GLAPIENTRY *qglDeleteRenderbuffersEXT)(GLsizei n, const GLuint *renderbuffers);
+void (GLAPIENTRY *qglGenRenderbuffersEXT)(GLsizei n, GLuint *renderbuffers);
+void (GLAPIENTRY *qglRenderbufferStorageEXT)(GLenum target, GLenum internalformat, GLsizei width, GLsizei height);
+void (GLAPIENTRY *qglGetRenderbufferParameterivEXT)(GLenum target, GLenum pname, GLint *params);
+GLboolean (GLAPIENTRY *qglIsFramebufferEXT)(GLuint framebuffer);
+void (GLAPIENTRY *qglBindFramebufferEXT)(GLenum target, GLuint framebuffer);
+void (GLAPIENTRY *qglDeleteFramebuffersEXT)(GLsizei n, const GLuint *framebuffers);
+void (GLAPIENTRY *qglGenFramebuffersEXT)(GLsizei n, GLuint *framebuffers);
+GLenum (GLAPIENTRY *qglCheckFramebufferStatusEXT)(GLenum target);
+void (GLAPIENTRY *qglFramebufferTexture1DEXT)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level);
+void (GLAPIENTRY *qglFramebufferTexture2DEXT)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level);
+void (GLAPIENTRY *qglFramebufferTexture3DEXT)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset);
+void (GLAPIENTRY *qglFramebufferRenderbufferEXT)(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer);
+void (GLAPIENTRY *qglGetFramebufferAttachmentParameterivEXT)(GLenum target, GLenum attachment, GLenum pname, GLint *params);
+void (GLAPIENTRY *qglGenerateMipmapEXT)(GLenum target);
+
 void (GLAPIENTRY *qglCompressedTexImage3DARB)(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const void *data);
 void (GLAPIENTRY *qglCompressedTexImage2DARB)(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border,  GLsizei imageSize, const void *data);
 void (GLAPIENTRY *qglCompressedTexImage1DARB)(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLint border, GLsizei imageSize, const void *data);
@@ -393,12 +425,28 @@ void (GLAPIENTRY *qglGetQueryivARB)(GLenum target, GLenum pname, GLint *params);
 void (GLAPIENTRY *qglGetQueryObjectivARB)(GLuint qid, GLenum pname, GLint *params);
 void (GLAPIENTRY *qglGetQueryObjectuivARB)(GLuint qid, GLenum pname, GLuint *params);
 
-int GL_CheckExtension(const char *name, const dllfunction_t *funcs, const char *disableparm, int silent)
+#if _MSC_VER >= 1400
+#define sscanf sscanf_s
+#endif
+
+int GL_CheckExtension(const char *minglver_or_ext, const dllfunction_t *funcs, const char *disableparm, int silent)
 {
        int failed = false;
        const dllfunction_t *func;
+       struct { int major, minor; } min_version, curr_version;
+       int ext;
 
-       Con_DPrintf("checking for %s...  ", name);
+       if(sscanf(minglver_or_ext, "%d.%d", &min_version.major, &min_version.minor) == 2)
+               ext = 0; // opengl version
+       else if(minglver_or_ext[0] != toupper(minglver_or_ext[0]))
+               ext = -1; // pseudo name
+       else
+               ext = 1; // extension name
+
+       if (ext)
+               Con_DPrintf("checking for %s...  ", minglver_or_ext);
+       else
+               Con_DPrintf("checking for OpenGL %s core features...  ", minglver_or_ext);
 
        for (func = funcs;func && func->name;func++)
                *func->funcvariable = NULL;
@@ -409,19 +457,40 @@ int GL_CheckExtension(const char *name, const dllfunction_t *funcs, const char *
                return false;
        }
 
-       if ((name[2] == '_' || name[3] == '_') && !strstr(gl_extensions ? gl_extensions : "", name) && !strstr(gl_platformextensions ? gl_platformextensions : "", name))
+       if (ext == 1) // opengl extension
        {
-               Con_DPrint("not detected\n");
-               return false;
+               if (!strstr(gl_extensions ? gl_extensions : "", minglver_or_ext) && !strstr(gl_platformextensions ? gl_platformextensions : "", minglver_or_ext))
+               {
+                       Con_DPrint("not detected\n");
+                       return false;
+               }
+       }
+
+       if(ext == 0) // opengl version
+       {
+               sscanf(gl_version, "%d.%d", &curr_version.major, &curr_version.minor);
+
+               if (curr_version.major < min_version.major || (curr_version.major == min_version.major && curr_version.minor < min_version.minor))
+               {
+                       Con_DPrintf("not detected (OpenGL %d.%d loaded)\n", curr_version.major, curr_version.minor);
+                       return false;
+               }
        }
 
        for (func = funcs;func && func->name != NULL;func++)
        {
+               // Con_DPrintf("\n    %s...  ", func->name);
+
                // functions are cleared before all the extensions are evaluated
                if (!(*func->funcvariable = (void *) GL_GetProcAddress(func->name)))
                {
                        if (!silent)
-                               Con_DPrintf("OpenGL extension \"%s\" is missing function \"%s\" - broken driver!\n", name, func->name);
+                       {
+                               if (ext)
+                                       Con_DPrintf("%s is missing function \"%s\" - broken driver!\n", minglver_or_ext, func->name);
+                               else
+                                       Con_DPrintf("OpenGL %s core features are missing function \"%s\" - broken driver!\n", minglver_or_ext, func->name);
+                       }
                        failed = true;
                }
        }
@@ -704,6 +773,28 @@ static dllfunction_t vbofuncs[] =
        {NULL, NULL}
 };
 
+static dllfunction_t fbofuncs[] =
+{
+       {"glIsRenderbufferEXT"                      , (void **) &qglIsRenderbufferEXT},
+       {"glBindRenderbufferEXT"                    , (void **) &qglBindRenderbufferEXT},
+       {"glDeleteRenderbuffersEXT"                 , (void **) &qglDeleteRenderbuffersEXT},
+       {"glGenRenderbuffersEXT"                    , (void **) &qglGenRenderbuffersEXT},
+       {"glRenderbufferStorageEXT"                 , (void **) &qglRenderbufferStorageEXT},
+       {"glGetRenderbufferParameterivEXT"          , (void **) &qglGetRenderbufferParameterivEXT},
+       {"glIsFramebufferEXT"                       , (void **) &qglIsFramebufferEXT},
+       {"glBindFramebufferEXT"                     , (void **) &qglBindFramebufferEXT},
+       {"glDeleteFramebuffersEXT"                  , (void **) &qglDeleteFramebuffersEXT},
+       {"glGenFramebuffersEXT"                     , (void **) &qglGenFramebuffersEXT},
+       {"glCheckFramebufferStatusEXT"              , (void **) &qglCheckFramebufferStatusEXT},
+       {"glFramebufferTexture1DEXT"                , (void **) &qglFramebufferTexture1DEXT},
+       {"glFramebufferTexture2DEXT"                , (void **) &qglFramebufferTexture2DEXT},
+       {"glFramebufferTexture3DEXT"                , (void **) &qglFramebufferTexture3DEXT},
+       {"glFramebufferRenderbufferEXT"             , (void **) &qglFramebufferRenderbufferEXT},
+       {"glGetFramebufferAttachmentParameterivEXT" , (void **) &qglGetFramebufferAttachmentParameterivEXT},
+       {"glGenerateMipmapEXT"                      , (void **) &qglGenerateMipmapEXT},
+       {NULL, NULL}
+};
+
 static dllfunction_t texturecompressionfuncs[] =
 {
        {"glCompressedTexImage3DARB",    (void **) &qglCompressedTexImage3DARB},
@@ -743,8 +834,11 @@ void VID_CheckExtensions(void)
        gl_supportslockarrays = false;
        gl_texture3d = false;
        gl_texturecubemap = false;
+       gl_texturerectangle = false;
        gl_support_arb_texture_non_power_of_two = false;
        gl_dot3arb = false;
+       gl_depthtexture = false;
+       gl_support_arb_shadow = false;
        gl_support_clamptoedge = false;
        gl_support_anisotropy = false;
        gl_max_anisotropy = 1;
@@ -757,10 +851,13 @@ void VID_CheckExtensions(void)
        gl_support_vertex_shader = false;
        gl_support_fragment_shader = false;
        gl_support_arb_vertex_buffer_object = false;
+       gl_support_ext_framebuffer_object = false;
        gl_support_texture_compression = false;
        gl_support_arb_occlusion_query = false;
+       gl_support_amd_texture_texture4 = false;
+       gl_support_arb_texture_gather = false;
 
-       if (!GL_CheckExtension("OpenGL 1.1.0", opengl110funcs, NULL, false))
+       if (!GL_CheckExtension("1.1", opengl110funcs, NULL, false))
                Sys_Error("OpenGL 1.1.0 functions not found");
 
        CHECKGLERROR
@@ -769,7 +866,7 @@ void VID_CheckExtensions(void)
        Con_DPrint("Checking OpenGL extensions...\n");
 
 // COMMANDLINEOPTION: GL: -nodrawrangeelements disables GL_EXT_draw_range_elements (renders faster)
-       if (!GL_CheckExtension("glDrawRangeElements", drawrangeelementsfuncs, "-nodrawrangeelements", true))
+       if (!GL_CheckExtension("1.2", drawrangeelementsfuncs, "-nodrawrangeelements", true))
                GL_CheckExtension("GL_EXT_draw_range_elements", drawrangeelementsextfuncs, "-nodrawrangeelements", false);
 
 // COMMANDLINEOPTION: GL: -nomtex disables GL_ARB_multitexture (required for faster map rendering)
@@ -796,6 +893,14 @@ void VID_CheckExtensions(void)
 // COMMANDLINEOPTION: GL: -nocubemap disables GL_ARB_texture_cube_map (required for bumpmapping)
        if ((gl_texturecubemap = GL_CheckExtension("GL_ARB_texture_cube_map", NULL, "-nocubemap", false)))
                qglGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB, &gl_max_cube_map_texture_size);
+// COMMANDLINEOPTION: GL: -norectangle disables GL_ARB_texture_rectangle (required for bumpmapping)
+       if ((gl_texturerectangle = GL_CheckExtension("GL_ARB_texture_rectangle", NULL, "-norectangle", false)))
+               qglGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_ARB, &gl_max_rectangle_texture_size);
+// COMMANDLINEOPTION: GL: -nodepthtexture disables use of GL_ARB_depth_texture (required for shadowmapping)
+       gl_depthtexture = GL_CheckExtension("GL_ARB_depth_texture", NULL, "-nodepthtexture", false);
+// COMMANDLINEOPTION: GL: -noshadow disables use of GL_ARB_shadow (required for hardware shadowmap filtering)
+       gl_support_arb_shadow = GL_CheckExtension("GL_ARB_shadow", NULL, "-noshadow", false);
+
 // COMMANDLINEOPTION: GL: -notexturecompression disables GL_ARB_texture_compression (which saves video memory if it is supported, but can also degrade image quality, see gl_texturecompression cvar documentation for more information)
        gl_support_texture_compression = GL_CheckExtension("GL_ARB_texture_compression", texturecompressionfuncs, "-notexturecompression", false);
 // COMMANDLINEOPTION: GL: -nocva disables GL_EXT_compiled_vertex_array (renders faster)
@@ -811,7 +916,7 @@ void VID_CheckExtensions(void)
        gl_support_ext_blend_subtract = GL_CheckExtension("GL_EXT_blend_subtract", blendequationfuncs, "-noblendsubtract", false);
 
 // COMMANDLINEOPTION: GL: -noseparatestencil disables use of OpenGL2.0 glStencilOpSeparate and GL_ATI_separate_stencil extensions (which accelerate shadow rendering)
-       if (!(gl_support_separatestencil = GL_CheckExtension("glStencilOpSeparate", gl2separatestencilfuncs, "-noseparatestencil", true)))
+       if (!(gl_support_separatestencil = (GL_CheckExtension("2.0", gl2separatestencilfuncs, "-noseparatestencil", true))))
                gl_support_separatestencil = GL_CheckExtension("GL_ATI_separate_stencil", atiseparatestencilfuncs, "-noseparatestencil", false);
 // COMMANDLINEOPTION: GL: -nostenciltwoside disables GL_EXT_stencil_two_side (which accelerate shadow rendering)
        gl_support_stenciltwoside = GL_CheckExtension("GL_EXT_stencil_two_side", stenciltwosidefuncs, "-nostenciltwoside", false);
@@ -819,6 +924,9 @@ void VID_CheckExtensions(void)
 // COMMANDLINEOPTION: GL: -novbo disables GL_ARB_vertex_buffer_object (which accelerates rendering)
        gl_support_arb_vertex_buffer_object = GL_CheckExtension("GL_ARB_vertex_buffer_object", vbofuncs, "-novbo", false);
 
+// COMMANDLINEOPTION: GL: -nofbo disables GL_EXT_framebuffer_object (which accelerates rendering)
+       gl_support_ext_framebuffer_object = GL_CheckExtension("GL_EXT_framebuffer_object", fbofuncs, "-nofbo", false);
+
        // we don't care if it's an extension or not, they are identical functions, so keep it simple in the rendering code
        if (qglDrawRangeElements == NULL)
                qglDrawRangeElements = qglDrawRangeElementsEXT;
@@ -847,6 +955,11 @@ void VID_CheckExtensions(void)
 
 // COMMANDLINEOPTION: GL: -noocclusionquery disables GL_ARB_occlusion_query (which allows coronas to fade according to visibility, and potentially used for rendering optimizations)
        gl_support_arb_occlusion_query = GL_CheckExtension("GL_ARB_occlusion_query", occlusionqueryfuncs, "-noocclusionquery", false);
+
+// COMMANDLINEOPTION: GL: -notexture4 disables GL_AMD_texture_texture4 (which provides fetch4 sampling)
+    gl_support_amd_texture_texture4 = GL_CheckExtension("GL_AMD_texture_texture4", NULL, "-notexture4", false);
+// COMMANDLINEOPTION: GL: -notexturegather disables GL_ARB_texture_gather (which provides fetch4 sampling)
+    gl_support_arb_texture_gather = GL_CheckExtension("GL_ARB_texture_gather", NULL, "-notexturegather", false);
 }
 
 void Force_CenterView_f (void)
@@ -935,7 +1048,7 @@ void VID_UpdateGamma(qboolean force, int rampsize)
        BOUNDCVAR(v_gamma, 0.1, 5);
        BOUNDCVAR(v_contrast, 1, 5);
        BOUNDCVAR(v_brightness, 0, 0.8);
-       BOUNDCVAR(v_contrastboost, 0.0625, 16);
+       //BOUNDCVAR(v_contrastboost, 0.0625, 16);
        BOUNDCVAR(v_color_black_r, 0, 0.8);
        BOUNDCVAR(v_color_black_g, 0, 0.8);
        BOUNDCVAR(v_color_black_b, 0, 0.8);
@@ -1109,14 +1222,14 @@ int VID_Mode(int fullscreen, int width, int height, int bpp, int refreshrate, in
        Con_Printf("Initializing Video Mode: %s %dx%dx%dx%dhz%s%s\n", fullscreen ? "fullscreen" : "window", width, height, bpp, refreshrate, stereobuffer ? " stereo" : "", samples > 1 ? va(" (%ix AA)", samples) : "");
        if (VID_InitMode(fullscreen, &width, &height, bpp, vid_userefreshrate.integer ? max(1, refreshrate) : 0, stereobuffer, samples))
        {
-               vid.fullscreen = fullscreen;
+               vid.fullscreen = fullscreen != 0;
                vid.width = width;
                vid.height = height;
                vid.bitsperpixel = bpp;
                vid.samples = samples;
                vid.refreshrate = refreshrate;
-               vid.stereobuffer = stereobuffer;
-               vid.userefreshrate = vid_userefreshrate.integer;
+               vid.stereobuffer = stereobuffer != 0;
+               vid.userefreshrate = vid_userefreshrate.integer != 0;
                Cvar_SetValueQuick(&vid_fullscreen, fullscreen);
                Cvar_SetValueQuick(&vid_width, width);
                Cvar_SetValueQuick(&vid_height, height);
@@ -1230,3 +1343,65 @@ void VID_Start(void)
        VID_OpenSystems();
 }
 
+int VID_SortModes_Compare(const void *a_, const void *b_)
+{
+       vid_mode_t *a = (vid_mode_t *) a_;
+       vid_mode_t *b = (vid_mode_t *) b_;
+       if(a->width > b->width)
+               return +1;
+       if(a->width < b->width)
+               return -1;
+       if(a->height > b->height)
+               return +1;
+       if(a->height < b->height)
+               return -1;
+       if(a->refreshrate > b->refreshrate)
+               return +1;
+       if(a->refreshrate < b->refreshrate)
+               return -1;
+       if(a->bpp > b->bpp)
+               return +1;
+       if(a->bpp < b->bpp)
+               return -1;
+       if(a->pixelheight_num * b->pixelheight_denom > a->pixelheight_denom * b->pixelheight_num)
+               return +1;
+       if(a->pixelheight_num * b->pixelheight_denom < a->pixelheight_denom * b->pixelheight_num)
+               return -1;
+       return 0;
+}
+size_t VID_SortModes(vid_mode_t *modes, size_t count, qboolean usebpp, qboolean userefreshrate, qboolean useaspect)
+{
+       size_t i;
+       if(count == 0)
+               return 0;
+       // 1. sort them
+       qsort(modes, count, sizeof(*modes), VID_SortModes_Compare);
+       // 2. remove duplicates
+       for(i = 0; i < count; ++i)
+       {
+               if(modes[i].width && modes[i].height)
+               {
+                       if(i == 0)
+                               continue;
+                       if(modes[i].width != modes[i-1].width)
+                               continue;
+                       if(modes[i].height != modes[i-1].height)
+                               continue;
+                       if(userefreshrate)
+                               if(modes[i].refreshrate != modes[i-1].refreshrate)
+                                       continue;
+                       if(usebpp)
+                               if(modes[i].bpp != modes[i-1].bpp)
+                                       continue;
+                       if(useaspect)
+                               if(modes[i].pixelheight_num * modes[i-1].pixelheight_denom != modes[i].pixelheight_denom * modes[i-1].pixelheight_num)
+                                       continue;
+               }
+               // a dupe, or a bogus mode!
+               if(i < count-1)
+                       memmove(&modes[i], &modes[i+1], sizeof(*modes) * (count-1 - i));
+               --i; // check this index again, as mode i+1 is now here
+               --count;
+       }
+       return count;
+}