]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_backend.c
restructured transparent mesh processing to do conversion from straight index list...
[xonotic/darkplaces.git] / gl_backend.c
index 1b75ccb089991fb86efced0268f06efc28ef7492..783494886ff3ca928c74e3bf7ab76f5f45083a93 100644 (file)
@@ -21,8 +21,6 @@ static cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "8192"};
 static cvar_t gl_mesh_batchtriangles = {0, "gl_mesh_batchtriangles", "1024"};
 static cvar_t gl_mesh_merge = {0, "gl_mesh_merge", "1"};
 static cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "0"};
-static cvar_t gl_mesh_dupetransverts = {0, "gl_mesh_dupetransverts", "0"};
-static cvar_t gl_mesh_sorttransbymesh = {0, "gl_mesh_sorttransbymesh", "1"};
 
 typedef struct buf_mesh_s
 {
@@ -92,8 +90,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];
@@ -125,8 +124,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)
 
@@ -150,37 +150,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
@@ -245,8 +214,6 @@ void gl_backend_init(void)
        Cvar_RegisterVariable(&gl_mesh_batchtriangles);
        Cvar_RegisterVariable(&gl_mesh_merge);
        Cvar_RegisterVariable(&gl_mesh_floatcolors);
-       Cvar_RegisterVariable(&gl_mesh_dupetransverts);
-       Cvar_RegisterVariable(&gl_mesh_sorttransbymesh);
        R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
        gl_backend_bufferchanges(true);
        for (i = 0;i < 256;i++)
@@ -398,6 +365,7 @@ void R_Mesh_Render(void)
        int i, k, blendfunc1, blendfunc2, blend, depthmask, depthtest, unit = 0, clientunit = 0, firsttriangle, triangles, firstvert, lastvert, texture[MAX_TEXTUREUNITS];
        float farclip, texturergbscale[MAX_TEXTUREUNITS];
        buf_mesh_t *mesh;
+       unsigned int *index;
        // float to byte color conversion
        int *icolor;
        float *fcolor;
@@ -733,11 +701,15 @@ CHECKGLERROR
                        }
                }
 
+               index = (unsigned int *)&buf_tri[firsttriangle].index[0];
+               for (i = 0;i < triangles * 3;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, (unsigned int *)&buf_tri[firsttriangle]);
+               glDrawElements(GL_TRIANGLES, triangles * 3, GL_UNSIGNED_INT, index);
 #else
-               glDrawRangeElements(GL_TRIANGLES, firstvert, lastvert + 1, triangles * 3, GL_UNSIGNED_INT, (unsigned int *)&buf_tri[firsttriangle]);
+               glDrawRangeElements(GL_TRIANGLES, firstvert, lastvert + 1, triangles * 3, GL_UNSIGNED_INT, index);
 #endif
 CHECKGLERROR
        }
@@ -807,374 +779,146 @@ CHECKGLERROR
 
 void R_Mesh_AddTransparent(void)
 {
-       if (gl_mesh_sorttransbymesh.integer)
-       {
-               int i, j, k;
-               float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
-               buf_vertex_t *vert1, *vert2, *vert3;
-               buf_transtri_t *tri;
-               buf_mesh_t *mesh, *transmesh;
-
-               // process and add transparent mesh triangles
-               if (!currenttranstriangle)
-                       return;
-
-               // map farclip to 0-4095 list range
-               centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
-               viewdistcompare = viewdist + 4.0f;
+       int i, j, k, *index;
+       float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
+       buf_vertex_t *vert1, *vert2, *vert3;
+       buf_transtri_t *tri;
+       buf_mesh_t *mesh, *transmesh;
 
-               memset(buf_transtri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
+       if (!currenttransmesh)
+               return;
 
-               k = 0;
-               for (j = 0;j < currenttranstriangle;j++)
+       // convert index data to transtris for sorting
+       for (j = 0;j < currenttransmesh;j++)
+       {
+               mesh = buf_transmesh + j;
+               k = mesh->firsttriangle;
+               index = &buf_transtri[k].index[0];
+               for (i = 0;i < mesh->triangles;i++)
                {
-                       tri = &buf_transtri[j];
-
-                       vert1 = &buf_transvertex[tri->index[0]];
-                       vert2 = &buf_transvertex[tri->index[1]];
-                       vert3 = &buf_transvertex[tri->index[2]];
-
-                       dist1 = DotProduct(vert1->v, vpn);
-                       dist2 = DotProduct(vert2->v, vpn);
-                       dist3 = DotProduct(vert3->v, vpn);
-
-                       maxdist = max(dist1, max(dist2, dist3));
-                       if (maxdist < viewdistcompare)
-                               continue;
-
-                       center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
-       #if SLOWMATH
-                       i = (int) center;
-                       i = bound(0, i, (TRANSDEPTHRES - 1));
-       #else
-                       if (center < 0.0f)
-                               center = 0.0f;
-                       center += 8388608.0f;
-                       i = *((long *)&center) & 0x7FFFFF;
-                       i = min(i, (TRANSDEPTHRES - 1));
-       #endif
-                       tri->next = buf_transtri_list[i];
-                       buf_transtri_list[i] = tri;
-                       k++;
+                       tri = &buf_sorttranstri[k++];
+                       tri->mesh = mesh;
+                       tri->index[0] = *index++;
+                       tri->index[1] = *index++;
+                       tri->index[2] = *index++;
                }
+       }
 
-               #ifndef TRANSBATCH
-               if (currentmesh + k > max_meshs || currenttriangle + k > max_batch || currentvertex + currenttransvertex > max_verts)
-                       R_Mesh_Render();
-
-               // note: can't batch these because they can be rendered in any order
-               // there can never be more transparent triangles than fit in main buffers
-               memcpy(&buf_vertex[currentvertex], &buf_transvertex[0], currenttransvertex * sizeof(buf_vertex_t));
-               memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[0], currenttransvertex * sizeof(buf_fcolor_t));
-               for (i = 0;i < backendunits;i++)
-                       memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][0], currenttransvertex * sizeof(buf_texcoord_t));
-               #endif
-
-               for (i = 0;i < currenttransmesh;i++)
-               {
-                       buf_transmesh[i].transchain = NULL;
-                       //buf_transmesh[i].transchainpointer = &buf_transmesh[i].transchain;
-                       buf_transmesh[i].triangles = 0;
-               }
-               transmesh = NULL;
-               for (j = 0;j < TRANSDEPTHRES;j++)
-               {
-                       if ((tri = buf_transtri_list[j]))
-                       {
-                               for (;tri;tri = tri->next)
-                               {
-                                       if (!tri->mesh->transchain)
-                                       {
-                                               tri->mesh->chain = transmesh;
-                                               transmesh = tri->mesh;
-                                       }
-                                       tri->meshsortchain = tri->mesh->transchain;
-                                       tri->mesh->transchain = tri;
-                                       /*
-                                       *tri->mesh->transchainpointer = tri;
-                                       tri->meshsortchain = NULL;
-                                       tri->mesh->transchainpointer = &tri->meshsortchain;
-                                       */
-                                       tri->mesh->triangles++;
-                               }
-                       }
-               }
+       // map farclip to 0-4095 list range
+       centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
+       viewdistcompare = viewdist + 4.0f;
 
-               #if TRANSBATCH
-               for (;transmesh;transmesh = transmesh->chain)
-               {
-                       int meshvertexadjust;
-                       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));
-
-                       mesh = &buf_mesh[currentmesh++];
-                       *mesh = *transmesh; // copy mesh properties
-                       mesh->firstvert = currentvertex;
-                       mesh->lastvert = currentvertex + numverts - 1;
-                       currentvertex += numverts;
-                       meshvertexadjust = mesh->firstvert - transmesh->firstvert;
-                       mesh->firsttriangle = currenttriangle;
-                       for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
-                       {
-                               buf_tri[currenttriangle].index[0] = tri->index[0] + meshvertexadjust;
-                               buf_tri[currenttriangle].index[1] = tri->index[1] + meshvertexadjust;
-                               buf_tri[currenttriangle].index[2] = tri->index[2] + meshvertexadjust;
-                               /*
-                               if (tri->mesh != transmesh)
-                                       Sys_Error("!?!");
-                               if ((unsigned int) buf_tri[currenttriangle].index[0] < (unsigned int) mesh->firstvert
-                                || (unsigned int) buf_tri[currenttriangle].index[0] > (unsigned int) mesh->lastvert
-                                || (unsigned int) buf_tri[currenttriangle].index[1] < (unsigned int) mesh->firstvert
-                                || (unsigned int) buf_tri[currenttriangle].index[1] > (unsigned int) mesh->lastvert
-                                || (unsigned int) buf_tri[currenttriangle].index[2] < (unsigned int) mesh->firstvert
-                                || (unsigned int) buf_tri[currenttriangle].index[2] > (unsigned int) mesh->lastvert)
-                                       Sys_Error("!?");
-                               */
-                               currenttriangle++;
-                       }
-                       /*
-                       if (mesh->triangles != currenttriangle - mesh->firsttriangle)
-                               Sys_Error("!");
-                       */
-               }
-               #else
-               for (;transmesh;transmesh = transmesh->chain)
-               {
-                       mesh = &buf_mesh[currentmesh++];
-                       *mesh = *transmesh; // copy mesh properties
-                       mesh->firstvert += currentvertex;
-                       mesh->lastvert += currentvertex;
-                       mesh->firsttriangle = currenttriangle;
-                       for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
-                       {
-                               buf_tri[currenttriangle].index[0] = tri->index[0] + currentvertex;
-                               buf_tri[currenttriangle].index[1] = tri->index[1] + currentvertex;
-                               buf_tri[currenttriangle].index[2] = tri->index[2] + currentvertex;
-                               /*
-                               if (tri->mesh != transmesh)
-                                       Sys_Error("!?!");
-                               if ((unsigned int) buf_tri[currenttriangle].index[0] < (unsigned int) mesh->firstvert
-                                || (unsigned int) buf_tri[currenttriangle].index[0] > (unsigned int) mesh->lastvert
-                                || (unsigned int) buf_tri[currenttriangle].index[1] < (unsigned int) mesh->firstvert
-                                || (unsigned int) buf_tri[currenttriangle].index[1] > (unsigned int) mesh->lastvert
-                                || (unsigned int) buf_tri[currenttriangle].index[2] < (unsigned int) mesh->firstvert
-                                || (unsigned int) buf_tri[currenttriangle].index[2] > (unsigned int) mesh->lastvert)
-                                       Sys_Error("!?");
-                               */
-                               currenttriangle++;
-                       }
-                       /*
-                       if (mesh->triangles != currenttriangle - mesh->firsttriangle)
-                               Sys_Error("!");
-                       */
-               }
-               currentvertex += currenttransvertex;
-               #endif
+       memset(buf_sorttranstri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
 
-               currenttransmesh = 0;
-               currenttranstriangle = 0;
-               currenttransvertex = 0;
-       }
-       else if (gl_mesh_dupetransverts.integer)
+       k = 0;
+       for (j = 0;j < currenttranstriangle;j++)
        {
-               int i, j, k, index;
-               float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
-               buf_vertex_t *vert1, *vert2, *vert3;
-               buf_transtri_t *tri;
-               buf_mesh_t *mesh;
-
-               // process and add transparent mesh triangles
-               if (!currenttranstriangle)
-                       return;
+               tri = &buf_sorttranstri[j];
+               i = tri->mesh->firstvert;
 
-               // map farclip to 0-4095 list range
-               centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
-               viewdistcompare = viewdist + 4.0f;
+               vert1 = &buf_transvertex[tri->index[0] + i];
+               vert2 = &buf_transvertex[tri->index[1] + i];
+               vert3 = &buf_transvertex[tri->index[2] + i];
 
-               memset(buf_transtri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
+               dist1 = DotProduct(vert1->v, vpn);
+               dist2 = DotProduct(vert2->v, vpn);
+               dist3 = DotProduct(vert3->v, vpn);
 
-               // process in reverse because transtri_list adding code is in reverse as well
-               k = 0;
-               for (j = currenttranstriangle - 1;j >= 0;j--)
-               {
-                       tri = &buf_transtri[j];
-
-                       vert1 = &buf_transvertex[tri->index[0]];
-                       vert2 = &buf_transvertex[tri->index[1]];
-                       vert3 = &buf_transvertex[tri->index[2]];
-
-                       dist1 = DotProduct(vert1->v, vpn);
-                       dist2 = DotProduct(vert2->v, vpn);
-                       dist3 = DotProduct(vert3->v, vpn);
-
-                       maxdist = max(dist1, max(dist2, dist3));
-                       if (maxdist < viewdistcompare)
-                               continue;
-
-                       center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
-       #if SLOWMATH
-                       i = (int) center;
-                       i = bound(0, i, (TRANSDEPTHRES - 1));
-       #else
-                       if (center < 0.0f)
-                               center = 0.0f;
-                       center += 8388608.0f;
-                       i = *((long *)&center) & 0x7FFFFF;
-                       i = min(i, (TRANSDEPTHRES - 1));
-       #endif
-                       tri->next = buf_transtri_list[i];
-                       buf_transtri_list[i] = tri;
-                       k++;
-               }
+               maxdist = max(dist1, max(dist2, dist3));
+               if (maxdist < viewdistcompare)
+                       continue;
 
-               if (currentmesh + k > max_meshs || currenttriangle + k > max_batch || currentvertex + k * 3 > max_verts)
-                       R_Mesh_Render();
+               center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
+#if SLOWMATH
+               i = (int) center;
+               i = bound(0, i, (TRANSDEPTHRES - 1));
+#else
+               if (center < 0.0f)
+                       center = 0.0f;
+               center += 8388608.0f;
+               i = *((long *)&center) & 0x7FFFFF;
+               i = min(i, (TRANSDEPTHRES - 1));
+#endif
+               tri->next = buf_sorttranstri_list[i];
+               buf_sorttranstri_list[i] = tri;
+               k++;
+       }
 
-               currenttransmesh = 0;
-               currenttranstriangle = 0;
-               currenttransvertex = 0;
+       //if (currentmesh + k > max_meshs || currenttriangle + k > max_batch || currentvertex + currenttransvertex > max_verts)
+       //      R_Mesh_Render();
 
-               // note: can't batch these because they can be rendered in any order
-               // there can never be more transparent triangles than fit in main buffers
-               for (j = TRANSDEPTHRES - 1;j >= 0;j--)
+       for (i = 0;i < currenttransmesh;i++)
+               buf_transmesh[i].transchain = NULL;
+       transmesh = NULL;
+       for (j = 0;j < TRANSDEPTHRES;j++)
+       {
+               if ((tri = buf_sorttranstri_list[j]))
                {
-                       if ((tri = buf_transtri_list[j]))
+                       for (;tri;tri = tri->next)
                        {
-                               while(tri)
+                               if (!tri->mesh->transchain)
                                {
-                                       mesh = &buf_mesh[currentmesh++];
-                                       *mesh = *tri->mesh; // copy mesh properties
-                                       mesh->firstvert = currentvertex;
-                                       mesh->lastvert = currentvertex + 2;
-                                       mesh->firsttriangle = currenttriangle;
-                                       mesh->triangles = 1;
-                                       for (k = 0;k < 3;k++)
-                                       {
-                                               index = tri->index[k];
-                                               buf_tri[currenttriangle].index[k] = currentvertex;
-                                               memcpy(buf_vertex[currentvertex].v, buf_transvertex[index].v, sizeof(buf_vertex_t));
-                                               memcpy(buf_fcolor[currentvertex].c, buf_transfcolor[index].c, sizeof(buf_fcolor_t));
-                                               for (i = 0;i < backendunits && tri->mesh->textures[i];i++)
-                                                       memcpy(buf_texcoord[i][currentvertex].t, buf_transtexcoord[i][index].t, sizeof(buf_texcoord_t));
-                                               currentvertex++;
-                                       }
-                                       currenttriangle++;
-                                       tri = tri->next;
+                                       tri->mesh->chain = transmesh;
+                                       transmesh = tri->mesh;
                                }
+                               tri->meshsortchain = tri->mesh->transchain;
+                               tri->mesh->transchain = tri;
                        }
                }
        }
-       else
-       {
-               int i, j, k;
-               float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
-               buf_vertex_t *vert1, *vert2, *vert3;
-               buf_transtri_t *tri;
-               buf_mesh_t *mesh;
-
-               // process and add transparent mesh triangles
-               if (!currenttranstriangle)
-                       return;
-
-               // 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 *));
 
-               // process in reverse because transtri_list adding code is in reverse as well
-               k = 0;
-               for (j = currenttranstriangle - 1;j >= 0;j--)
-               {
-                       tri = &buf_transtri[j];
-
-                       vert1 = &buf_transvertex[tri->index[0]];
-                       vert2 = &buf_transvertex[tri->index[1]];
-                       vert3 = &buf_transvertex[tri->index[2]];
-
-                       dist1 = DotProduct(vert1->v, vpn);
-                       dist2 = DotProduct(vert2->v, vpn);
-                       dist3 = DotProduct(vert3->v, vpn);
-
-                       maxdist = max(dist1, max(dist2, dist3));
-                       if (maxdist < viewdistcompare)
-                               continue;
-
-                       center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
-       #if SLOWMATH
-                       i = (int) center;
-                       i = bound(0, i, (TRANSDEPTHRES - 1));
-       #else
-                       if (center < 0.0f)
-                               center = 0.0f;
-                       center += 8388608.0f;
-                       i = *((long *)&center) & 0x7FFFFF;
-                       i = min(i, (TRANSDEPTHRES - 1));
-       #endif
-                       tri->next = buf_transtri_list[i];
-                       buf_transtri_list[i] = tri;
-                       k++;
-               }
-
-               if (currentmesh + k > max_meshs || currenttriangle + k > max_batch || currentvertex + currenttransvertex > max_verts)
+       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();
 
-               // note: can't batch these because they can be rendered in any order
-               // there can never be more transparent triangles than fit in main buffers
-               memcpy(&buf_vertex[currentvertex], &buf_transvertex[0], currenttransvertex * sizeof(buf_vertex_t));
-               memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[0], currenttransvertex * sizeof(buf_fcolor_t));
-               for (i = 0;i < backendunits;i++)
-                       memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][0], currenttransvertex * sizeof(buf_texcoord_t));
+               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));
 
-               for (j = TRANSDEPTHRES - 1;j >= 0;j--)
+               mesh = &buf_mesh[currentmesh++];
+               *mesh = *transmesh; // copy mesh properties
+               mesh->firstvert = currentvertex;
+               mesh->lastvert = currentvertex + numverts - 1;
+               currentvertex += numverts;
+               mesh->firsttriangle = currenttriangle;
+               for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
                {
-                       if ((tri = buf_transtri_list[j]))
-                       {
-                               while(tri)
-                               {
-                                       mesh = &buf_mesh[currentmesh++];
-                                       *mesh = *tri->mesh; // copy mesh properties
-                                       buf_tri[currenttriangle].index[0] = tri->index[0] + currentvertex;
-                                       buf_tri[currenttriangle].index[1] = tri->index[1] + currentvertex;
-                                       buf_tri[currenttriangle].index[2] = tri->index[2] + currentvertex;
-                                       mesh->firstvert = min(buf_tri[currenttriangle].index[0], min(buf_tri[currenttriangle].index[1], buf_tri[currenttriangle].index[2]));
-                                       mesh->lastvert = max(buf_tri[currenttriangle].index[0], max(buf_tri[currenttriangle].index[1], buf_tri[currenttriangle].index[2]));
-                                       mesh->firsttriangle = currenttriangle++;
-                                       mesh->triangles = 1;
-                                       tri = tri->next;
-                               }
-                       }
+                       buf_tri[currenttriangle].index[0] = tri->index[0];
+                       buf_tri[currenttriangle].index[1] = tri->index[1];
+                       buf_tri[currenttriangle].index[2] = tri->index[2];
+                       currenttriangle++;
                }
-               currentvertex += currenttransvertex;
-               currenttransmesh = 0;
-               currenttranstriangle = 0;
-               currenttransvertex = 0;
+               mesh->triangles = currenttriangle - mesh->firsttriangle;
        }
+
+       currenttransmesh = 0;
+       currenttranstriangle = 0;
+       currenttransvertex = 0;
 }
 
 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;
        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");
 
        if (m->index == NULL
         || !m->numtriangles
         || m->vertex == NULL
         || !m->numverts)
-               return;
+               Host_Error("R_Mesh_Draw: no triangles or verts\n");
+
        // ignore meaningless alpha meshs
        if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
        {
@@ -1232,18 +976,9 @@ 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++ + currenttransvertex;
-                       tri->index[1] = *index++ + currenttransvertex;
-                       tri->index[2] = *index++ + currenttransvertex;
-               }
+               mesh->firsttriangle = currenttranstriangle;
+               memcpy(&buf_transtri[currenttranstriangle].index[0], m->index, sizeof(int[3]) * m->numtriangles);
+               currenttranstriangle += m->numtriangles;
 
                mesh->firstvert = currenttransvertex;
                mesh->lastvert = currenttransvertex + m->numverts - 1;
@@ -1267,13 +1002,11 @@ 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
-               index = (int *)&buf_tri[currenttriangle];
+               mesh = &buf_mesh[currentmesh++];
                mesh->firsttriangle = currenttriangle;
+               memcpy(&buf_tri[currenttriangle].index[0], m->index, sizeof(int[3]) * m->numtriangles);
                currenttriangle += m->numtriangles;
-               for (i = 0;i < m->numtriangles * 3;i++)
-                       index[i] = m->index[i] + currentvertex;
 
                mesh->firstvert = currentvertex;
                mesh->lastvert = currentvertex + m->numverts - 1;
@@ -1354,7 +1087,325 @@ 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, overbright;
+       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];
+
+       if (!backendactive)
+               Sys_Error("R_Mesh_Draw: called when backend is not active\n");
+
+       if (m->index == NULL
+        || !m->numtriangles
+        || m->vertex == NULL
+        || !m->numverts)
+               Host_Error("R_Mesh_Draw: no triangles or verts\n");
+
+       // ignore meaningless alpha meshs
+       if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
+       {
+               if (m->color)
+               {
+                       for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
+                               if (*in >= 0.01f)
+                                       break;
+                       if (i == m->numverts)
+                               return;
+               }
+               else if (m->ca < 0.01f)
+                       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)
+               {
+                       if (!transranout)
+                       {
+                               Con_Printf("R_Mesh_Draw_NativeOnly: ran out of room for transparent meshs\n");
+                               transranout = true;
+                       }
+                       return;
+               }
+
+               c_transmeshs++;
+               c_transtris += m->numtriangles;
+               vert = &buf_transvertex[currenttransvertex];
+               fcolor = &buf_transfcolor[currenttransvertex];
+               for (i = 0;i < backendunits;i++)
+                       texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
+
+               // transmesh is only for storage of transparent meshs until they
+               // are inserted into the main mesh array
+               mesh = &buf_transmesh[currenttransmesh++];
+               mesh->firsttriangle = currenttranstriangle;
+               memcpy(&buf_transtri[currenttranstriangle].index[0], m->index, sizeof(int[3]) * m->numtriangles);
+               currenttranstriangle += m->numtriangles;
+
+               mesh->firstvert = currenttransvertex;
+               mesh->lastvert = currenttransvertex + m->numverts - 1;
+               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;
+               }
+
+               if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
+                       R_Mesh_Render();
+
+               c_meshs++;
+               c_meshtris += m->numtriangles;
+               vert = &buf_vertex[currentvertex];
+               fcolor = &buf_fcolor[currentvertex];
+               for (i = 0;i < backendunits;i++)
+                       texcoord[i] = &buf_texcoord[i][currentvertex];
+
+               mesh = &buf_mesh[currentmesh++];
+               // opaque meshs are rendered directly
+               mesh->firsttriangle = currenttriangle;
+               memcpy(&buf_tri[currenttriangle].index[0], m->index, sizeof(int[3]) * m->numtriangles);
+               currenttriangle += m->numtriangles;
+
+               mesh->firstvert = currentvertex;
+               mesh->lastvert = currentvertex + m->numverts - 1;
+               currentvertex += m->numverts;
+       }
+
+       // code shared for transparent and opaque meshs
+       mesh->blendfunc1 = m->blendfunc1;
+       mesh->blendfunc2 = m->blendfunc2;
+       mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
+       mesh->depthtest = !m->depthdisable;
+       mesh->triangles = m->numtriangles;
+       j = -1;
+       for (i = 0;i < backendunits;i++)
+       {
+               if ((mesh->textures[i] = m->tex[i]))
+                       j = i;
+               mesh->texturergbscale[i] = m->texrgbscale[i];
+               if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
+                       mesh->texturergbscale[i] = 1;
+       }
+       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();
+}
+
+/*
+void R_Mesh_Draw_GetBuffer(volatile rmeshinfo_t *m)
+{
+       // these are static because gcc runs out of virtual registers otherwise
+       static int i, j, *index, overbright;
+       static float *in, scaler;
+
+       if (!backendactive)
+               Sys_Error("R_Mesh_Draw: called when backend is not active\n");
+
+       if (!m->numtriangles
+        || !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)
+               {
+                       if (!transranout)
+                       {
+                               Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
+                               transranout = true;
+                       }
+                       return;
+               }
+
+               c_transmeshs++;
+               c_transtris += m->numtriangles;
+               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];
+
+               // transmesh is only for storage of transparent meshs until they
+               // are inserted into the main mesh array
+               mesh = &buf_transmesh[currenttransmesh++];
+               mesh->firsttriangle = currenttranstriangle;
+               memcpy(&buf_transtri[currenttranstriangle].index[0], m->index, sizeof(int[3]) * m->numtriangles);
+               currenttranstriangle += m->numtriangles;
+
+               mesh->firstvert = currenttransvertex;
+               mesh->lastvert = currenttransvertex + m->numverts - 1;
+               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;
+               }
+
+               if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
+                       R_Mesh_Render();
+
+               c_meshs++;
+               c_meshtris += m->numtriangles;
+               vert = &buf_vertex[currentvertex];
+               fcolor = &buf_fcolor[currentvertex];
+               for (i = 0;i < backendunits;i++)
+                       texcoord[i] = &buf_texcoord[i][currentvertex];
+
+               // opaque meshs are rendered directly
+               mesh = &buf_mesh[currentmesh++];
+               mesh->firsttriangle = currenttriangle;
+               memcpy(&buf_tri[currenttriangle].index[0], m->index, sizeof(int[3]) * m->numtriangles);
+               currenttriangle += m->numtriangles;
+
+               mesh->firstvert = currentvertex;
+               mesh->lastvert = currentvertex + m->numverts - 1;
+               currentvertex += m->numverts;
+       }
+
+       // code shared for transparent and opaque meshs
+       mesh->blendfunc1 = m->blendfunc1;
+       mesh->blendfunc2 = m->blendfunc2;
+       mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
+       mesh->depthtest = !m->depthdisable;
+       mesh->triangles = m->numtriangles;
+       j = -1;
+       for (i = 0;i < backendunits;i++)
+       {
+               if ((mesh->textures[i] = m->tex[i]))
+                       j = i;
+               mesh->texturergbscale[i] = m->texrgbscale[i];
+               if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
+                       mesh->texturergbscale[i] = 1;
+       }
+       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();
 }
+*/
 
 void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
 {
@@ -1374,6 +1425,7 @@ 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)
 {
@@ -1424,6 +1476,7 @@ void R_Mesh_DrawDecal(const rmeshinfo_t *m)
                mesh->blendfunc2 = m->blendfunc2;
                mesh->depthmask = false;
                mesh->depthtest = true;
+               mesh->firsttriangle = currenttranstriangle;
                mesh->triangles = 2;
                mesh->textures[0] = m->tex[0];
                mesh->texturergbscale[0] = overbright ? 4 : 1;
@@ -1434,22 +1487,10 @@ void R_Mesh_DrawDecal(const rmeshinfo_t *m)
                }
                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 + currenttransvertex;
-               tri->index[1] = 1 + currenttransvertex;
-               tri->index[2] = 2 + currenttransvertex;
-               tri = &buf_transtri[currenttranstriangle++];
-               tri->mesh = mesh;
-               tri->index[0] = 0 + currenttransvertex;
-               tri->index[1] = 2 + currenttransvertex;
-               tri->index[2] = 3 + currenttransvertex;
-
+               index = &buf_transtri[currenttranstriangle].index[0];
                mesh->firstvert = currenttransvertex;
                mesh->lastvert = currenttransvertex + 3;
+               currenttranstriangle += 2;
                currenttransvertex += 4;
        }
        else
@@ -1485,19 +1526,20 @@ void R_Mesh_DrawDecal(const rmeshinfo_t *m)
                }
 
                // opaque meshs are rendered directly
-               index = (int *)&buf_tri[currenttriangle];
-               index[0] = 0 + currentvertex;
-               index[1] = 1 + currentvertex;
-               index[2] = 2 + currentvertex;
-               index[3] = 0 + currentvertex;
-               index[4] = 2 + currentvertex;
-               index[5] = 3 + currentvertex;
+               index = &buf_tri[currenttriangle].index[0];
                mesh->firstvert = currentvertex;
                mesh->lastvert = currentvertex + 3;
                currenttriangle += 2;
                currentvertex += 4;
        }
 
+       index[0] = 0;
+       index[1] = 1;
+       index[2] = 2;
+       index[3] = 0;
+       index[4] = 2;
+       index[5] = 3;
+
        // buf_vertex_t must match the size of the decal vertex array (or vice versa)
        memcpy(vert, m->vertex, 4 * sizeof(buf_vertex_t));
 
@@ -1524,7 +1566,11 @@ void R_Mesh_DrawDecal(const rmeshinfo_t *m)
 
        // 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();
 }
+*/
 
 /*
 ==============================================================================