]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_backend.c
new option -capturedemo to capture a demo to an AVI file;
[xonotic/darkplaces.git] / gl_backend.c
index 70af0a43c482717a30a24b54a005951fbeeefe8a..9db2543513a5b6c008e378d2953190a04dba42df 100644 (file)
@@ -14,6 +14,7 @@ cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwa
 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
 cvar_t gl_lockarrays = {0, "gl_lockarrays", "0", "enables use of glLockArraysEXT, may cause glitches with some broken drivers, and may be slower than normal"};
 cvar_t gl_lockarrays_minimumvertices = {0, "gl_lockarrays_minimumvertices", "1", "minimum number of vertices required for use of glLockArraysEXT, setting this too low may reduce performance"};
+cvar_t gl_vbo = {0, "gl_vbo", "1", "make use of GL_ARB_vertex_buffer_object extension to store static geometry in video memory for faster rendering"};
 
 int gl_maxdrawrangeelementsvertices;
 int gl_maxdrawrangeelementsindices;
@@ -143,6 +144,22 @@ void GL_Backend_FreeArrays(void)
 {
 }
 
+void GL_VBOStats_f(void)
+{
+       GL_Mesh_ListVBOs(true);
+}
+
+typedef struct gl_bufferobjectinfo_s
+{
+       int target;
+       int object;
+       size_t size;
+       char name[MAX_QPATH];
+}
+gl_bufferobjectinfo_t;
+
+memexpandablearray_t gl_bufferobjectinfoarray;
+
 static void gl_backend_start(void)
 {
        Con_Print("OpenGL Backend starting...\n");
@@ -179,6 +196,8 @@ static void gl_backend_start(void)
 
        GL_Backend_AllocArrays();
 
+       Mem_ExpandableArray_NewArray(&gl_bufferobjectinfoarray, r_main_mempool, sizeof(gl_bufferobjectinfo_t), 128);
+
        Con_Printf("OpenGL backend started.\n");
 
        CHECKGLERROR
@@ -195,6 +214,8 @@ static void gl_backend_shutdown(void)
 
        Con_Print("OpenGL Backend shutting down\n");
 
+       Mem_ExpandableArray_FreeArray(&gl_bufferobjectinfoarray);
+
        GL_Backend_FreeArrays();
 }
 
@@ -229,6 +250,7 @@ void gl_backend_init(void)
        Cvar_RegisterVariable(&gl_dither);
        Cvar_RegisterVariable(&gl_lockarrays);
        Cvar_RegisterVariable(&gl_lockarrays_minimumvertices);
+       Cvar_RegisterVariable(&gl_vbo);
        Cvar_RegisterVariable(&gl_paranoid);
        Cvar_RegisterVariable(&gl_printcheckerror);
 #ifdef NORENDER
@@ -239,6 +261,8 @@ void gl_backend_init(void)
        Cvar_RegisterVariable(&gl_mesh_testarrayelement);
        Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
 
+       Cmd_AddCommand("gl_vbostats", GL_VBOStats_f, "prints a list of all buffer objects (vertex data and triangle elements) and total video memory used by them\n");
+
        R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
 }
 
@@ -355,6 +379,7 @@ static struct gl_state_s
        GLboolean depthmask;
        int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
        int depthtest;
+       float depthrange[2];
        int alphatest;
        int scissortest;
        unsigned int unit;
@@ -629,6 +654,16 @@ void GL_DepthTest(int state)
        }
 }
 
+void GL_DepthRange(float nearfrac, float farfrac)
+{
+       if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
+       {
+               gl_state.depthrange[0] = nearfrac;
+               gl_state.depthrange[1] = farfrac;
+               qglDepthRange(nearfrac, farfrac);
+       }
+}
+
 void GL_CullFace(int state)
 {
        CHECKGLERROR
@@ -872,9 +907,11 @@ void R_Mesh_Draw(int firstvertex, int numvertices, int numtriangles, const int *
        unsigned int numelements = numtriangles * 3;
        if (numvertices < 3 || numtriangles < 1)
        {
-               Con_Printf("R_Mesh_Draw(%d, %d, %d, %8p);\n", firstvertex, numvertices, numtriangles, elements);
+               Con_Printf("R_Mesh_Draw(%d, %d, %d, %8p, %i, %p);\n", firstvertex, numvertices, numtriangles, elements, bufferobject, (void *)bufferoffset);
                return;
        }
+       if (!gl_vbo.integer)
+               bufferobject = 0;
        CHECKGLERROR
        r_refdef.stats.meshes++;
        r_refdef.stats.meshes_elements += numelements;
@@ -1081,6 +1118,76 @@ void R_Mesh_Finish(void)
        qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
 }
 
+int R_Mesh_CreateStaticBufferObject(unsigned int target, void *data, size_t size, const char *name)
+{
+       gl_bufferobjectinfo_t *info;
+       GLuint bufferobject;
+
+       if (!gl_vbo.integer)
+               return 0;
+
+       qglGenBuffersARB(1, &bufferobject);
+       switch(target)
+       {
+       case GL_ELEMENT_ARRAY_BUFFER_ARB: GL_BindEBO(bufferobject);break;
+       case GL_ARRAY_BUFFER_ARB: GL_BindVBO(bufferobject);break;
+       default: Sys_Error("R_Mesh_CreateStaticBufferObject: unknown target type %i\n", target);return 0;
+       }
+       qglBufferDataARB(target, size, data, GL_STATIC_DRAW_ARB);
+
+       info = Mem_ExpandableArray_AllocRecord(&gl_bufferobjectinfoarray);
+       memset(info, 0, sizeof(*info));
+       info->target = target;
+       info->object = bufferobject;
+       info->size = size;
+       strlcpy(info->name, name, sizeof(info->name));
+
+       return (int)bufferobject;
+}
+
+void R_Mesh_DestroyBufferObject(int bufferobject)
+{
+       int i, endindex;
+       gl_bufferobjectinfo_t *info;
+
+       qglDeleteBuffersARB(1, (GLuint *)&bufferobject);
+
+       endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
+       for (i = 0;i < endindex;i++)
+       {
+               info = Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
+               if (!info)
+                       continue;
+               if (info->object == bufferobject)
+               {
+                       Mem_ExpandableArray_FreeRecord(&gl_bufferobjectinfoarray, (void *)info);
+                       break;
+               }
+       }
+}
+
+void GL_Mesh_ListVBOs(qboolean printeach)
+{
+       int i, endindex;
+       size_t ebocount = 0, ebomemory = 0;
+       size_t vbocount = 0, vbomemory = 0;
+       gl_bufferobjectinfo_t *info;
+       endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
+       for (i = 0;i < endindex;i++)
+       {
+               info = Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
+               if (!info)
+                       continue;
+               switch(info->target)
+               {
+               case GL_ELEMENT_ARRAY_BUFFER_ARB: ebocount++;ebomemory += info->size;if (printeach) Con_Printf("EBO #%i %s = %i bytes\n", info->object, info->name, (int)info->size);break;
+               case GL_ARRAY_BUFFER_ARB: vbocount++;vbomemory += info->size;if (printeach) Con_Printf("VBO #%i %s = %i bytes\n", info->object, info->name, (int)info->size);break;
+               default: Con_Printf("gl_vbostats: unknown target type %i\n", info->target);break;
+               }
+       }
+       Con_Printf("vertex buffers: %i element buffers totalling %i bytes (%.3f MB), %i vertex buffers totalling %i bytes (%.3f MB), combined %i bytes (%.3fMB)\n", (int)ebocount, (int)ebomemory, ebomemory / 1048576.0, (int)vbocount, (int)vbomemory, vbomemory / 1048576.0, (int)(ebomemory + vbomemory), (ebomemory + vbomemory) / 1048576.0);
+}
+
 void R_Mesh_Matrix(const matrix4x4_t *matrix)
 {
        if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
@@ -1096,6 +1203,8 @@ void R_Mesh_Matrix(const matrix4x4_t *matrix)
 
 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
 {
+       if (!gl_vbo.integer)
+               bufferobject = 0;
        if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
        {
                gl_state.pointer_vertex = vertex3f;
@@ -1109,8 +1218,13 @@ void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t buffer
 
 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
 {
-       if (color4f || bufferobject)
+       // note: this can not rely on bufferobject to decide whether a color array
+       // is supplied, because surfmesh_t shares one vbo for all arrays, which
+       // means that a valid vbo may be supplied even if there is no color array.
+       if (color4f)
        {
+               if (!gl_vbo.integer)
+                       bufferobject = 0;
                // caller wants color array enabled
                if (!gl_state.pointer_color_enabled)
                {
@@ -1147,8 +1261,12 @@ void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, co
        gltextureunit_t *unit = gl_state.units + unitnum;
        // update array settings
        CHECKGLERROR
-       if (texcoord || bufferobject)
+       // note: there is no need to check bufferobject here because all cases
+       // that involve a valid bufferobject also supply a texcoord array
+       if (texcoord)
        {
+               if (!gl_vbo.integer)
+                       bufferobject = 0;
                // texture array unit is enabled, enable the array
                if (!unit->arrayenabled)
                {
@@ -1680,9 +1798,9 @@ void R_Mesh_TextureState(const rmeshstate_t *m)
        for (i = 0;i < backendarrayunits;i++)
        {
                if (m->pointer_texcoord3f[i])
-                       R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i], 0, 0);
+                       R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
                else
-                       R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i], 0, 0);
+                       R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
        }
        for (i = 0;i < backendunits;i++)
        {