]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_backend.c
cleaned up backend code a bit more, added R_Mesh_Draw_GetBuffer (untested - returns...
[xonotic/darkplaces.git] / gl_backend.c
index 4a9f251c083212d45b2adb0471b5bc6e9f933fb6..e03740b1ffef52c117c1984c6c100ded82fd9743 100644 (file)
@@ -24,7 +24,6 @@ static cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "0"};
 
 typedef struct buf_mesh_s
 {
-       //struct buf_mesh_s *next;
        int depthmask;
        int depthtest;
        int blendfunc1, blendfunc2;
@@ -33,10 +32,9 @@ typedef struct buf_mesh_s
        int firsttriangle;
        int triangles;
        int firstvert;
-       int lastvert;
+       int verts;
        struct buf_mesh_s *chain;
        struct buf_transtri_s *transchain;
-       //struct buf_transtri_s **transchainpointer;
 }
 buf_mesh_t;
 
@@ -80,7 +78,7 @@ typedef struct
 buf_texcoord_t;
 
 static float meshfarclip;
-static int currentmesh, currenttriangle, currentvertex, backendunits, backendactive, meshmerge, transranout;
+static int currentmesh, currenttriangle, currentvertex, backendunits, backendactive, transranout;
 static buf_mesh_t *buf_mesh;
 static buf_tri_t *buf_tri;
 static buf_vertex_t *buf_vertex;
@@ -90,8 +88,9 @@ static buf_texcoord_t *buf_texcoord[MAX_TEXTUREUNITS];
 
 static int currenttransmesh, currenttransvertex, currenttranstriangle;
 static buf_mesh_t *buf_transmesh;
-static buf_transtri_t *buf_transtri;
-static buf_transtri_t **buf_transtri_list;
+static buf_transtri_t *buf_sorttranstri;
+static buf_transtri_t **buf_sorttranstri_list;
+static buf_tri_t *buf_transtri;
 static buf_vertex_t *buf_transvertex;
 static buf_fcolor_t *buf_transfcolor;
 static buf_texcoord_t *buf_transtexcoord[MAX_TEXTUREUNITS];
@@ -123,8 +122,9 @@ static void gl_backend_start(void)
        BACKENDALLOC(buf_bcolor, max_verts, buf_bcolor_t)
 
        BACKENDALLOC(buf_transmesh, max_meshs, buf_mesh_t)
-       BACKENDALLOC(buf_transtri, max_meshs, buf_transtri_t)
-       BACKENDALLOC(buf_transtri_list, TRANSDEPTHRES, buf_transtri_t *)
+       BACKENDALLOC(buf_sorttranstri, max_meshs, buf_transtri_t)
+       BACKENDALLOC(buf_sorttranstri_list, TRANSDEPTHRES, buf_transtri_t *)
+       BACKENDALLOC(buf_transtri, max_meshs, buf_tri_t)
        BACKENDALLOC(buf_transvertex, max_verts, buf_vertex_t)
        BACKENDALLOC(buf_transfcolor, max_verts, buf_fcolor_t)
 
@@ -148,37 +148,6 @@ static void gl_backend_start(void)
 
 static void gl_backend_shutdown(void)
 {
-       //int i;
-       /*
-#define BACKENDFREE(var)\
-       if (var)\
-       {\
-               Mem_Free(var);\
-               var = NULL;\
-       }
-       */
-       /*
-#define BACKENDFREE(var) var = NULL;
-
-       BACKENDFREE(buf_mesh)
-       BACKENDFREE(buf_tri)
-       BACKENDFREE(buf_vertex)
-       BACKENDFREE(buf_fcolor)
-       BACKENDFREE(buf_bcolor)
-
-       BACKENDFREE(buf_transmesh)
-       BACKENDFREE(buf_transtri)
-       BACKENDFREE(buf_transtri_list)
-       BACKENDFREE(buf_transvertex)
-       BACKENDFREE(buf_transfcolor)
-
-       for (i = 0;i < MAX_TEXTUREUNITS;i++)
-       {
-               BACKENDFREE(buf_texcoord[i])
-               BACKENDFREE(buf_transtexcoord[i])
-       }
-       */
-
        if (resizingbuffers)
                Mem_EmptyPool(gl_backend_mempool);
        else
@@ -291,7 +260,7 @@ static void GL_SetupFrame (void)
        // y is weird beause OpenGL is bottom to top, we use top to bottom
        glViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);
 //     yfov = 2*atan((float)r_refdef.height/r_refdef.width)*180/M_PI;
-       MYgluPerspective (r_refdef.fov_x, r_refdef.fov_y, r_refdef.width/r_refdef.height, 4, r_farclip);
+       MYgluPerspective (r_refdef.fov_x, r_refdef.fov_y, r_refdef.width/r_refdef.height, 4.0 / 3.0, r_farclip);
 
        glCullFace(GL_FRONT);
 
@@ -339,7 +308,6 @@ void R_Mesh_Clear(void)
        currenttranstriangle = 0;
        currenttransvertex = 0;
        meshfarclip = 0;
-       meshmerge = gl_mesh_merge.integer;
        transranout = false;
        viewdist = DotProduct(r_origin, vpn);
 
@@ -391,7 +359,7 @@ int errornumber = 0;
 // renders mesh buffers, called to flush buffers when full
 void R_Mesh_Render(void)
 {
-       int i, k, blendfunc1, blendfunc2, blend, depthmask, depthtest, unit = 0, clientunit = 0, firsttriangle, triangles, firstvert, lastvert, texture[MAX_TEXTUREUNITS];
+       int i, k, blendfunc1, blendfunc2, blend, depthmask, depthtest, unit = 0, clientunit = 0, firsttriangle, endtriangle, indexcount, firstvert, endvert, texture[MAX_TEXTUREUNITS];
        float farclip, texturergbscale[MAX_TEXTUREUNITS];
        buf_mesh_t *mesh;
        unsigned int *index;
@@ -441,13 +409,13 @@ CHECKGLERROR
        glDepthMask((GLboolean) depthmask);
 CHECKGLERROR
 
-       glVertexPointer(3, GL_FLOAT, sizeof(buf_vertex_t), buf_vertex);
+       glVertexPointer(3, GL_FLOAT, sizeof(buf_vertex_t), &buf_vertex[0].v[0]);
 CHECKGLERROR
        glEnableClientState(GL_VERTEX_ARRAY);
 CHECKGLERROR
        if (gl_mesh_floatcolors.integer)
        {
-               glColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), buf_fcolor);
+               glColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), &buf_fcolor[0].c[0]);
 CHECKGLERROR
        }
        else
@@ -468,7 +436,7 @@ CHECKGLERROR
                        k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
                        k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
                }
-               glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), buf_bcolor);
+               glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), &buf_bcolor[0].c[0]);
 CHECKGLERROR
        }
        glEnableClientState(GL_COLOR_ARRAY);
@@ -697,21 +665,23 @@ CHECKGLERROR
                }
 
                firsttriangle = mesh->firsttriangle;
-               triangles = mesh->triangles;
                firstvert = mesh->firstvert;
-               lastvert = mesh->lastvert;
-               mesh = &buf_mesh[++k];
+               endtriangle = firsttriangle + mesh->triangles;
+               endvert = firstvert + mesh->verts;
 
-               if (meshmerge)
+               mesh = &buf_mesh[++k];
+               if (gl_mesh_merge.integer)
                {
                        #if MAX_TEXTUREUNITS != 4
                        #error update this code
                        #endif
                        while (k < currentmesh
+                               && mesh->firsttriangle == endtriangle
+                               && mesh->firstvert == endvert
+                               && mesh->depthmask == depthmask
+                               && mesh->depthtest == depthtest
                                && mesh->blendfunc1 == blendfunc1
                                && mesh->blendfunc2 == blendfunc2
-                               && mesh->depthtest == depthtest
-                               && mesh->depthmask == depthmask
                                && mesh->textures[0] == texture[0]
                                && mesh->textures[1] == texture[1]
                                && mesh->textures[2] == texture[2]
@@ -721,24 +691,22 @@ CHECKGLERROR
                                && mesh->texturergbscale[2] == texturergbscale[2]
                                && mesh->texturergbscale[3] == texturergbscale[3])
                        {
-                               triangles += mesh->triangles;
-                               if (firstvert > mesh->firstvert)
-                                       firstvert = mesh->firstvert;
-                               if (lastvert < mesh->lastvert)
-                                       lastvert = mesh->lastvert;
+                               endtriangle += mesh->triangles;
+                               endvert += mesh->verts;
                                mesh = &buf_mesh[++k];
                        }
                }
 
+               indexcount = (endtriangle - firsttriangle) * 3;
                index = (unsigned int *)&buf_tri[firsttriangle].index[0];
-               for (i = 0;i < triangles * 3;i++)
+               for (i = 0;i < indexcount;i++)
                        index[i] += firstvert;
 
 #ifdef WIN32
                // FIXME: dynamic link to GL so we can get DrawRangeElements on WIN32
-               glDrawElements(GL_TRIANGLES, triangles * 3, GL_UNSIGNED_INT, index);
+               glDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, index);
 #else
-               glDrawRangeElements(GL_TRIANGLES, firstvert, lastvert + 1, triangles * 3, GL_UNSIGNED_INT, index);
+               glDrawRangeElements(GL_TRIANGLES, firstvert, endvert, indexcount, GL_UNSIGNED_INT, index);
 #endif
 CHECKGLERROR
        }
@@ -808,7 +776,7 @@ CHECKGLERROR
 
 void R_Mesh_AddTransparent(void)
 {
-       int i, j, k;
+       int i, j, k, *index;
        float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
        buf_vertex_t *vert1, *vert2, *vert3;
        buf_transtri_t *tri;
@@ -817,34 +785,32 @@ void R_Mesh_AddTransparent(void)
        if (!currenttransmesh)
                return;
 
-       /*
        // convert index data to transtris for sorting
-       for (i = 0;i < currenttransmesh;i++)
+       for (j = 0;j < currenttransmesh;j++)
        {
-               mesh = buf_transmesh + i;
-               j = mesh->firstvert;
-               index = mesh->index;
-               for (i = 0;i < mesh->numtriangles;i++)
+               mesh = buf_transmesh + j;
+               k = mesh->firsttriangle;
+               index = &buf_transtri[k].index[0];
+               for (i = 0;i < mesh->triangles;i++)
                {
-                       tri = &buf_transtri[currenttranstriangle++];
+                       tri = &buf_sorttranstri[k++];
                        tri->mesh = mesh;
-                       tri->index[0] = *index++ + j;
-                       tri->index[1] = *index++ + j;
-                       tri->index[2] = *index++ + j;
+                       tri->index[0] = *index++;
+                       tri->index[1] = *index++;
+                       tri->index[2] = *index++;
                }
        }
-       */
 
        // map farclip to 0-4095 list range
        centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
        viewdistcompare = viewdist + 4.0f;
 
-       memset(buf_transtri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
+       memset(buf_sorttranstri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
 
        k = 0;
        for (j = 0;j < currenttranstriangle;j++)
        {
-               tri = &buf_transtri[j];
+               tri = &buf_sorttranstri[j];
                i = tri->mesh->firstvert;
 
                vert1 = &buf_transvertex[tri->index[0] + i];
@@ -870,20 +836,17 @@ void R_Mesh_AddTransparent(void)
                i = *((long *)&center) & 0x7FFFFF;
                i = min(i, (TRANSDEPTHRES - 1));
 #endif
-               tri->next = buf_transtri_list[i];
-               buf_transtri_list[i] = tri;
+               tri->next = buf_sorttranstri_list[i];
+               buf_sorttranstri_list[i] = tri;
                k++;
        }
 
-       if (currentmesh + k > max_meshs || currenttriangle + k > max_batch || currentvertex + currenttransvertex > max_verts)
-               R_Mesh_Render();
-
        for (i = 0;i < currenttransmesh;i++)
                buf_transmesh[i].transchain = NULL;
        transmesh = NULL;
        for (j = 0;j < TRANSDEPTHRES;j++)
        {
-               if ((tri = buf_transtri_list[j]))
+               if ((tri = buf_sorttranstri_list[j]))
                {
                        for (;tri;tri = tri->next)
                        {
@@ -900,20 +863,19 @@ void R_Mesh_AddTransparent(void)
 
        for (;transmesh;transmesh = transmesh->chain)
        {
-               int numverts = transmesh->lastvert - transmesh->firstvert + 1;
-               //if (currentmesh >= max_meshs || currenttriangle + transmesh->triangles > max_batch || currentvertex + numverts > max_verts)
-               //      R_Mesh_Render();
-
-               memcpy(&buf_vertex[currentvertex], &buf_transvertex[transmesh->firstvert], numverts * sizeof(buf_vertex_t));
-               memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[transmesh->firstvert], numverts * sizeof(buf_fcolor_t));
-               for (i = 0;i < backendunits && transmesh->textures[i];i++)
-                       memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][transmesh->firstvert], numverts * sizeof(buf_texcoord_t));
+               if (currentmesh >= max_meshs || currenttriangle + transmesh->triangles > max_batch || currentvertex + transmesh->verts > max_verts)
+                       R_Mesh_Render();
 
                mesh = &buf_mesh[currentmesh++];
                *mesh = *transmesh; // copy mesh properties
+
                mesh->firstvert = currentvertex;
-               mesh->lastvert = currentvertex + numverts - 1;
-               currentvertex += numverts;
+               memcpy(&buf_vertex[currentvertex], &buf_transvertex[transmesh->firstvert], transmesh->verts * sizeof(buf_vertex_t));
+               memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[transmesh->firstvert], transmesh->verts * sizeof(buf_fcolor_t));
+               for (i = 0;i < backendunits && transmesh->textures[i];i++)
+                       memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][transmesh->firstvert], transmesh->verts * sizeof(buf_texcoord_t));
+               currentvertex += mesh->verts;
+
                mesh->firsttriangle = currenttriangle;
                for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
                {
@@ -933,14 +895,13 @@ void R_Mesh_AddTransparent(void)
 void R_Mesh_Draw(const rmeshinfo_t *m)
 {
        // these are static because gcc runs out of virtual registers otherwise
-       static int i, j, *index, overbright;
+       static int i, j, overbright, *index;
        static float *in, scaler;
        static float cr, cg, cb, ca;
        static buf_mesh_t *mesh;
        static buf_vertex_t *vert;
        static buf_fcolor_t *fcolor;
        static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
-       static buf_transtri_t *tri;
 
        if (!backendactive)
                Sys_Error("R_Mesh_Draw: called when backend is not active\n");
@@ -969,22 +930,11 @@ void R_Mesh_Draw(const rmeshinfo_t *m)
        if (!backendactive)
                Sys_Error("R_Mesh_Draw: called when backend is not active\n");
 
-       scaler = 1;
-       if (m->blendfunc2 == GL_SRC_COLOR)
-       {
-               if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
-                       scaler *= 0.5f;
-       }
-       else
-       {
-               if (m->tex[0])
-               {
-                       overbright = gl_combine.integer;
-                       if (overbright)
-                               scaler *= 0.25f;
-               }
-               scaler *= overbrightscale;
-       }
+#ifdef DEBUGGL
+       for (i = 0;i < m->numtriangles * 3;i++)
+               if ((unsigned int) m->index[i] >= (unsigned int) m->numverts)
+                       Host_Error("R_Mesh_Draw: invalid index (%i of %i verts)\n", m->index, m->numverts);
+#endif
 
        if (m->transparent)
        {
@@ -1008,21 +958,11 @@ void R_Mesh_Draw(const rmeshinfo_t *m)
                // transmesh is only for storage of transparent meshs until they
                // are inserted into the main mesh array
                mesh = &buf_transmesh[currenttransmesh++];
-
-               // transparent meshs are broken up into individual triangles which can
-               // be sorted by depth
-               index = m->index;
-               for (i = 0;i < m->numtriangles;i++)
-               {
-                       tri = &buf_transtri[currenttranstriangle++];
-                       tri->mesh = mesh;
-                       tri->index[0] = *index++;
-                       tri->index[1] = *index++;
-                       tri->index[2] = *index++;
-               }
-
+               mesh->firsttriangle = currenttranstriangle;
                mesh->firstvert = currenttransvertex;
-               mesh->lastvert = currenttransvertex + m->numverts - 1;
+               index = &buf_transtri[currenttranstriangle].index[0];
+
+               currenttranstriangle += m->numtriangles;
                currenttransvertex += m->numverts;
        }
        else
@@ -1043,23 +983,44 @@ void R_Mesh_Draw(const rmeshinfo_t *m)
                for (i = 0;i < backendunits;i++)
                        texcoord[i] = &buf_texcoord[i][currentvertex];
 
-               mesh = &buf_mesh[currentmesh++];
                // opaque meshs are rendered directly
+               mesh = &buf_mesh[currentmesh++];
                mesh->firsttriangle = currenttriangle;
-               memcpy(&buf_tri[currenttriangle].index[0], m->index, sizeof(float[3]) * m->numtriangles);
-               currenttriangle += m->numtriangles;
-
                mesh->firstvert = currentvertex;
-               mesh->lastvert = currentvertex + m->numverts - 1;
+               index = &buf_tri[currenttriangle].index[0];
+
+               currenttriangle += m->numtriangles;
                currentvertex += m->numverts;
        }
 
        // code shared for transparent and opaque meshs
+       memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
        mesh->blendfunc1 = m->blendfunc1;
        mesh->blendfunc2 = m->blendfunc2;
        mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
        mesh->depthtest = !m->depthdisable;
        mesh->triangles = m->numtriangles;
+       mesh->verts = m->numverts;
+
+       overbright = false;
+       scaler = 1;
+       if (m->blendfunc2 == GL_SRC_COLOR)
+       {
+               if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
+                       scaler *= 0.5f;
+       }
+       else
+       {
+               if (m->tex[0])
+               {
+                       overbright = gl_combine.integer;
+                       if (overbright)
+                               scaler *= 0.25f;
+               }
+               scaler *= overbrightscale;
+       }
+
+
        j = -1;
        for (i = 0;i < backendunits;i++)
        {
@@ -1128,21 +1089,17 @@ void R_Mesh_Draw(const rmeshinfo_t *m)
        for (;j < backendunits;j++)
                memset(&texcoord[j][0].t[0], 0, m->numverts * sizeof(buf_texcoord_t));
        #endif
-
-       if (currenttriangle >= max_batch)
-               R_Mesh_Render();
 }
 
 void R_Mesh_Draw_NativeOnly(const rmeshinfo_t *m)
 {
        // these are static because gcc runs out of virtual registers otherwise
-       static int i, j, *index, overbright;
+       static int i, j, overbright, *index;
        static float *in, scaler;
        static buf_mesh_t *mesh;
        static buf_vertex_t *vert;
        static buf_fcolor_t *fcolor;
        static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
-       static buf_transtri_t *tri;
 
        if (!backendactive)
                Sys_Error("R_Mesh_Draw: called when backend is not active\n");
@@ -1168,23 +1125,6 @@ void R_Mesh_Draw_NativeOnly(const rmeshinfo_t *m)
                        return;
        }
 
-       scaler = 1;
-       if (m->blendfunc2 == GL_SRC_COLOR)
-       {
-               if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
-                       scaler *= 0.5f;
-       }
-       else
-       {
-               if (m->tex[0])
-               {
-                       overbright = gl_combine.integer;
-                       if (overbright)
-                               scaler *= 0.25f;
-               }
-               scaler *= overbrightscale;
-       }
-
        if (m->transparent)
        {
                if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
@@ -1207,21 +1147,10 @@ void R_Mesh_Draw_NativeOnly(const rmeshinfo_t *m)
                // transmesh is only for storage of transparent meshs until they
                // are inserted into the main mesh array
                mesh = &buf_transmesh[currenttransmesh++];
-
-               // transparent meshs are broken up into individual triangles which can
-               // be sorted by depth
-               index = m->index;
-               for (i = 0;i < m->numtriangles;i++)
-               {
-                       tri = &buf_transtri[currenttranstriangle++];
-                       tri->mesh = mesh;
-                       tri->index[0] = *index++;
-                       tri->index[1] = *index++;
-                       tri->index[2] = *index++;
-               }
-
+               mesh->firsttriangle = currenttranstriangle;
                mesh->firstvert = currenttransvertex;
-               mesh->lastvert = currenttransvertex + m->numverts - 1;
+               index = &buf_transtri[currenttranstriangle].index[0];
+               currenttranstriangle += m->numtriangles;
                currenttransvertex += m->numverts;
        }
        else
@@ -1242,23 +1171,42 @@ void R_Mesh_Draw_NativeOnly(const rmeshinfo_t *m)
                for (i = 0;i < backendunits;i++)
                        texcoord[i] = &buf_texcoord[i][currentvertex];
 
-               mesh = &buf_mesh[currentmesh++];
                // opaque meshs are rendered directly
+               mesh = &buf_mesh[currentmesh++];
                mesh->firsttriangle = currenttriangle;
-               memcpy(&buf_tri[currenttriangle].index[0], m->index, sizeof(float[3]) * m->numtriangles);
-               currenttriangle += m->numtriangles;
-
                mesh->firstvert = currentvertex;
-               mesh->lastvert = currentvertex + m->numverts - 1;
+               index = &buf_tri[currenttriangle].index[0];
+               currenttriangle += m->numtriangles;
                currentvertex += m->numverts;
        }
 
        // code shared for transparent and opaque meshs
+       memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
        mesh->blendfunc1 = m->blendfunc1;
        mesh->blendfunc2 = m->blendfunc2;
        mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
        mesh->depthtest = !m->depthdisable;
        mesh->triangles = m->numtriangles;
+       mesh->verts = m->numverts;
+
+       overbright = false;
+       scaler = 1;
+       if (m->blendfunc2 == GL_SRC_COLOR)
+       {
+               if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
+                       scaler *= 0.5f;
+       }
+       else
+       {
+               if (m->tex[0])
+               {
+                       overbright = gl_combine.integer;
+                       if (overbright)
+                               scaler *= 0.25f;
+               }
+               scaler *= overbrightscale;
+       }
+
        j = -1;
        for (i = 0;i < backendunits;i++)
        {
@@ -1305,18 +1253,16 @@ void R_Mesh_Draw_NativeOnly(const rmeshinfo_t *m)
                        fcolor[i].c[2] *= scaler;
                }
        }
-
-       if (currenttriangle >= max_batch)
-               R_Mesh_Render();
 }
 
-/*
-void R_Mesh_Draw_GetBuffer(volatile rmeshinfo_t *m)
+// allocates space in geometry buffers, and fills in pointers to the buffers in passsed struct
+// (this is used for very high speed rendering, no copying)
+int R_Mesh_Draw_GetBuffer(rmeshbufferinfo_t *m)
 {
        // these are static because gcc runs out of virtual registers otherwise
-       static int i, j, *index, overbright;
-       static float *in, scaler;
-       static buf_transtri_t *tri;
+       int i, j, overbright;
+       float scaler;
+       buf_mesh_t *mesh;
 
        if (!backendactive)
                Sys_Error("R_Mesh_Draw: called when backend is not active\n");
@@ -1325,23 +1271,6 @@ void R_Mesh_Draw_GetBuffer(volatile rmeshinfo_t *m)
         || !m->numverts)
                Host_Error("R_Mesh_Draw: no triangles or verts\n");
 
-       scaler = 1;
-       if (m->blendfunc2 == GL_SRC_COLOR)
-       {
-               if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
-                       scaler *= 0.5f;
-       }
-       else
-       {
-               if (m->tex[0])
-               {
-                       overbright = gl_combine.integer;
-                       if (overbright)
-                               scaler *= 0.25f;
-               }
-               scaler *= overbrightscale;
-       }
-
        if (m->transparent)
        {
                if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
@@ -1351,42 +1280,31 @@ void R_Mesh_Draw_GetBuffer(volatile rmeshinfo_t *m)
                                Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
                                transranout = true;
                        }
-                       return;
+                       return false;
                }
 
                c_transmeshs++;
                c_transtris += m->numtriangles;
+               m->index = &buf_transtri[currenttranstriangle].index[0];
                m->vertex = &buf_transvertex[currenttransvertex].v[0];
                m->color = &buf_transfcolor[currenttransvertex].c[0];
                for (i = 0;i < backendunits;i++)
-                       m->texcoords[i] = &buf_transtexcoord[i][currenttransvertex].tc[0];
+                       m->texcoords[i] = &buf_transtexcoord[i][currenttransvertex].t[0];
 
                // transmesh is only for storage of transparent meshs until they
                // are inserted into the main mesh array
                mesh = &buf_transmesh[currenttransmesh++];
-
-               // transparent meshs are broken up into individual triangles which can
-               // be sorted by depth
-               index = m->index;
-               for (i = 0;i < m->numtriangles;i++)
-               {
-                       tri = &buf_transtri[currenttranstriangle++];
-                       tri->mesh = mesh;
-                       tri->index[0] = *index++;
-                       tri->index[1] = *index++;
-                       tri->index[2] = *index++;
-               }
-
+               mesh->firsttriangle = currenttranstriangle;
                mesh->firstvert = currenttransvertex;
-               mesh->lastvert = currenttransvertex + m->numverts - 1;
+               currenttranstriangle += m->numtriangles;
                currenttransvertex += m->numverts;
        }
        else
        {
                if (m->numtriangles > max_meshs || m->numverts > max_verts)
                {
-                       Con_Printf("R_Mesh_Draw_NativeOnly: mesh too big for buffers\n");
-                       return;
+                       Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for buffers\n");
+                       return false;
                }
 
                if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
@@ -1394,19 +1312,17 @@ void R_Mesh_Draw_GetBuffer(volatile rmeshinfo_t *m)
 
                c_meshs++;
                c_meshtris += m->numtriangles;
-               vert = &buf_vertex[currentvertex];
-               fcolor = &buf_fcolor[currentvertex];
+               m->index = &buf_tri[currenttriangle].index[0];
+               m->vertex = &buf_vertex[currentvertex].v[0];
+               m->color = &buf_fcolor[currentvertex].c[0];
                for (i = 0;i < backendunits;i++)
-                       texcoord[i] = &buf_texcoord[i][currentvertex];
+                       m->texcoords[i] = &buf_texcoord[i][currentvertex].t[0];
 
-               mesh = &buf_mesh[currentmesh++];
                // opaque meshs are rendered directly
+               mesh = &buf_mesh[currentmesh++];
                mesh->firsttriangle = currenttriangle;
-               memcpy(&buf_tri[currenttriangle].index[0], m->index, sizeof(float[3]) * m->numtriangles);
-               currenttriangle += m->numtriangles;
-
                mesh->firstvert = currentvertex;
-               mesh->lastvert = currentvertex + m->numverts - 1;
+               currenttriangle += m->numtriangles;
                currentvertex += m->numverts;
        }
 
@@ -1416,11 +1332,36 @@ void R_Mesh_Draw_GetBuffer(volatile rmeshinfo_t *m)
        mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
        mesh->depthtest = !m->depthdisable;
        mesh->triangles = m->numtriangles;
+       mesh->verts = m->numverts;
+
+       overbright = false;
+       scaler = 1;
+       if (m->blendfunc2 == GL_SRC_COLOR)
+       {
+               if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
+                       scaler *= 0.5f;
+       }
+       else
+       {
+               if (m->tex[0])
+               {
+                       overbright = gl_combine.integer;
+                       if (overbright)
+                               scaler *= 0.25f;
+               }
+               scaler *= overbrightscale;
+       }
+       m->colorscale = scaler;
+
        j = -1;
-       for (i = 0;i < backendunits;i++)
+       for (i = 0;i < MAX_TEXTUREUNITS;i++)
        {
                if ((mesh->textures[i] = m->tex[i]))
+               {
                        j = i;
+                       if (i >= backendunits)
+                               Sys_Error("R_Mesh_Draw_GetBuffer: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
+               }
                mesh->texturergbscale[i] = m->texrgbscale[i];
                if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
                        mesh->texturergbscale[i] = 1;
@@ -1428,45 +1369,8 @@ void R_Mesh_Draw_GetBuffer(volatile rmeshinfo_t *m)
        if (overbright && j >= 0)
                mesh->texturergbscale[j] = 4;
 
-       if (m->vertexstep != sizeof(buf_vertex_t))
-               Host_Error("R_Mesh_Draw_NativeOnly: unsupported vertexstep\n");
-       if (m->colorstep != sizeof(buf_fcolor_t))
-               Host_Error("R_Mesh_Draw_NativeOnly: unsupported colorstep\n");
-       if (m->color == NULL)
-               Host_Error("R_Mesh_Draw_NativeOnly: must provide color array\n");
-       for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
-       {
-               if (j >= backendunits)
-                       Sys_Error("R_Mesh_Draw_NativeOnly: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
-               if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
-                       Host_Error("R_Mesh_Draw_NativeOnly: unsupported texcoordstep\n");
-       }
-
-       memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
-       for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
-               memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
-       #if 0
-       for (;j < backendunits;j++)
-               memset(&texcoord[j][0].t[0], 0, m->numverts * sizeof(buf_texcoord_t));
-       #endif
-
-       memcpy(fcolor, m->color, m->numverts * sizeof(buf_fcolor_t));
-
-       // do this as a second step because memcpy preloaded the cache, which we can't easily do
-       if (scaler != 1)
-       {
-               for (i = 0;i < m->numverts;i++)
-               {
-                       fcolor[i].c[0] *= scaler;
-                       fcolor[i].c[1] *= scaler;
-                       fcolor[i].c[2] *= scaler;
-               }
-       }
-
-       if (currenttriangle >= max_batch)
-               R_Mesh_Render();
+       return true;
 }
-*/
 
 void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
 {
@@ -1486,161 +1390,6 @@ void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
        R_Mesh_Draw(m);
 }
 
-// LordHavoc: this thing is evil, but necessary because decals account for so much overhead
-void R_Mesh_DrawDecal(const rmeshinfo_t *m)
-{
-       // these are static because gcc runs out of virtual registers otherwise
-       static int i, *index, overbright;
-       static float scaler;
-       static float cr, cg, cb, ca;
-       static buf_mesh_t *mesh;
-       static buf_vertex_t *vert;
-       static buf_fcolor_t *fcolor;
-       static buf_texcoord_t *texcoord;
-       static buf_transtri_t *tri;
-
-       if (!backendactive)
-               Sys_Error("R_Mesh_Draw: called when backend is not active\n");
-
-       scaler = 1;
-       if (m->tex[0])
-       {
-               overbright = gl_combine.integer;
-               if (overbright)
-                       scaler *= 0.25f;
-       }
-       scaler *= overbrightscale;
-
-       if (m->transparent)
-       {
-               if (currenttransmesh >= max_meshs || (currenttranstriangle + 2) > max_meshs || (currenttransvertex + 4) > max_verts)
-               {
-                       if (!transranout)
-                       {
-                               Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
-                               transranout = true;
-                       }
-                       return;
-               }
-
-               c_transmeshs++;
-               c_transtris += 2;
-               vert = &buf_transvertex[currenttransvertex];
-               fcolor = &buf_transfcolor[currenttransvertex];
-               texcoord = &buf_transtexcoord[0][currenttransvertex];
-
-               // transmesh is only for storage of transparent meshs until they
-               // are inserted into the main mesh array
-               mesh = &buf_transmesh[currenttransmesh++];
-               mesh->blendfunc1 = m->blendfunc1;
-               mesh->blendfunc2 = m->blendfunc2;
-               mesh->depthmask = false;
-               mesh->depthtest = true;
-               mesh->triangles = 2;
-               mesh->textures[0] = m->tex[0];
-               mesh->texturergbscale[0] = overbright ? 4 : 1;
-               for (i = 1;i < backendunits;i++)
-               {
-                       mesh->textures[i] = 0;
-                       mesh->texturergbscale[i] = 1;
-               }
-               mesh->chain = NULL;
-
-               // transparent meshs are broken up into individual triangles which can
-               // be sorted by depth
-               index = m->index;
-               tri = &buf_transtri[currenttranstriangle++];
-               tri->mesh = mesh;
-               tri->index[0] = 0;
-               tri->index[1] = 1;
-               tri->index[2] = 2;
-               tri = &buf_transtri[currenttranstriangle++];
-               tri->mesh = mesh;
-               tri->index[0] = 0;
-               tri->index[1] = 2;
-               tri->index[2] = 3;
-
-               mesh->firstvert = currenttransvertex;
-               mesh->lastvert = currenttransvertex + 3;
-               currenttransvertex += 4;
-       }
-       else
-       {
-               if (2 > max_meshs || 4 > max_verts)
-               {
-                       Con_Printf("R_Mesh_Draw: mesh too big for buffers\n");
-                       return;
-               }
-
-               if (currentmesh >= max_meshs || (currenttriangle + 2) > max_batch || (currentvertex + 4) > max_verts)
-                       R_Mesh_Render();
-
-               c_meshs++;
-               c_meshtris += 2;
-               vert = &buf_vertex[currentvertex];
-               fcolor = &buf_fcolor[currentvertex];
-               texcoord = &buf_texcoord[0][currentvertex];
-
-               mesh = &buf_mesh[currentmesh++];
-               mesh->blendfunc1 = m->blendfunc1;
-               mesh->blendfunc2 = m->blendfunc2;
-               mesh->depthmask = false;
-               mesh->depthtest = !m->depthdisable;
-               mesh->firsttriangle = currenttriangle;
-               mesh->triangles = 2;
-               mesh->textures[0] = m->tex[0];
-               mesh->texturergbscale[0] = overbright ? 4 : 1;
-               for (i = 1;i < backendunits;i++)
-               {
-                       mesh->textures[i] = 0;
-                       mesh->texturergbscale[i] = 1;
-               }
-
-               // opaque meshs are rendered directly
-               index = &buf_tri[currenttriangle].index[0];
-               index[0] = 0;
-               index[1] = 1;
-               index[2] = 2;
-               index[3] = 0;
-               index[4] = 2;
-               index[5] = 3;
-               mesh->firstvert = currentvertex;
-               mesh->lastvert = currentvertex + 3;
-               currenttriangle += 2;
-               currentvertex += 4;
-       }
-
-       // buf_vertex_t must match the size of the decal vertex array (or vice versa)
-       memcpy(vert, m->vertex, 4 * sizeof(buf_vertex_t));
-
-       cr = m->cr * scaler;
-       cg = m->cg * scaler;
-       cb = m->cb * scaler;
-       ca = m->ca;
-       fcolor[0].c[0] = cr;
-       fcolor[0].c[1] = cg;
-       fcolor[0].c[2] = cb;
-       fcolor[0].c[3] = ca;
-       fcolor[1].c[0] = cr;
-       fcolor[1].c[1] = cg;
-       fcolor[1].c[2] = cb;
-       fcolor[1].c[3] = ca;
-       fcolor[2].c[0] = cr;
-       fcolor[2].c[1] = cg;
-       fcolor[2].c[2] = cb;
-       fcolor[2].c[3] = ca;
-       fcolor[3].c[0] = cr;
-       fcolor[3].c[1] = cg;
-       fcolor[3].c[2] = cb;
-       fcolor[3].c[3] = ca;
-
-       // buf_texcoord_t must be the same size as the decal texcoord array (or vice versa)
-       memcpy(&texcoord[0].t[0], m->texcoords[0], 4 * sizeof(buf_texcoord_t));
-
-       if (currenttriangle >= max_batch)
-               R_Mesh_Render();
-}
-
 /*
 ==============================================================================