From c22ec98a144f18d7519eb4a06dd1ee8937e8c4ac Mon Sep 17 00:00:00 2001 From: havoc Date: Mon, 26 May 2003 13:25:59 +0000 Subject: [PATCH] fixed realtime lighting bugs with gl_mesh_batching 1 (... by not using batching while rendering realtime lighting - I was unable to figure out what was causing the problem) some cleanups to realtime lighting code (mostly fixing some bugs with incorrect counting of meshes/triangles (for r_speeds reports), and optimized out unnecessary vertex copying in the final lighting application pass) git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@3044 d7cf8633-e32d-0410-b094-e92efae38249 --- gl_backend.c | 58 +++++++++--- gl_backend.h | 4 +- r_shadow.c | 249 +++++++++++++++++++++++++++------------------------ r_shadow.h | 3 - 4 files changed, 184 insertions(+), 130 deletions(-) diff --git a/gl_backend.c b/gl_backend.c index 55788e89..3196022f 100644 --- a/gl_backend.c +++ b/gl_backend.c @@ -852,16 +852,6 @@ void R_Mesh_GetSpace(int numverts) } } -/* -#ifdef MESH_BATCH - if (gl_mesh_batching.integer) - { - if (gl_batchvertexcount == 0) - gl_batchvertexfirst = varray_offset; - gl_batchvertexcount += numverts; - } -#endif -*/ varray_vertex3f = varray_buf_vertex3f + varray_offset * 3; varray_color4f = varray_buf_color4f + varray_offset * 4; for (i = 0;i < backendunits;i++) @@ -952,6 +942,54 @@ void R_Mesh_Draw(int numverts, int numtriangles, const int *elements) } } +// renders triangles using vertices from the most recent GetSpace call +// (can be multiple Draw calls per GetSpace) +void R_Mesh_Draw_NoBatching(int numverts, int numtriangles, const int *elements) +{ + int numelements = numtriangles * 3; + if (numtriangles == 0 || numverts == 0) + { + Con_Printf("R_Mesh_Draw_NoBatching(%d, %d, %08p);\n", numverts, numtriangles, elements); + return; + } + c_meshs++; + c_meshelements += numelements; + CHECKGLERROR + if (gl_state.pointervertexcount) + { + if (r_render.integer) + { + GL_LockArrays(0, gl_state.pointervertexcount); + if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL) + { + qglDrawRangeElements(GL_TRIANGLES, 0, gl_state.pointervertexcount, numelements, GL_UNSIGNED_INT, elements);CHECKGLERROR + } + else + { + qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, elements);CHECKGLERROR + } + GL_LockArrays(0, 0); + } + } + else + { + GL_Backend_RenumberElements(varray_buf_elements3i, numelements, elements, varray_offset); + if (r_render.integer) + { + GL_LockArrays(varray_offset, numverts); + if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL) + { + qglDrawRangeElements(GL_TRIANGLES, varray_offset, varray_offset + numverts, numelements, GL_UNSIGNED_INT, varray_buf_elements3i);CHECKGLERROR + } + else + { + qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, varray_buf_elements3i);CHECKGLERROR + } + GL_LockArrays(0, 0); + } + } +} + // restores backend state, used when done with 3D rendering void R_Mesh_Finish(void) { diff --git a/gl_backend.h b/gl_backend.h index edcd19cd..72acc7b2 100644 --- a/gl_backend.h +++ b/gl_backend.h @@ -81,8 +81,10 @@ void R_Mesh_TextureState(const rmeshstate_t *m); void R_Mesh_EndBatch(void); // prepares varray_* buffers for rendering a mesh void R_Mesh_GetSpace(int numverts); -// renders the mesh in the varray_* buffers +// renders a mesh (optionally with batching) void R_Mesh_Draw(int numverts, int numtriangles, const int *elements); +// renders a mesh without affecting batching +void R_Mesh_Draw_NoBatching(int numverts, int numtriangles, const int *elements); // copies a vertex3f array into varray_vertex3f void R_Mesh_CopyVertex3f(const float *vertex3f, int numverts); diff --git a/r_shadow.c b/r_shadow.c index 058d5589..19e36e79 100644 --- a/r_shadow.c +++ b/r_shadow.c @@ -22,8 +22,6 @@ int maxtrianglefacinglight; qbyte *trianglefacinglight; int *trianglefacinglightlist; -int maxshadowvertices; -float *shadowvertex3f; int maxvertexupdate; int *vertexupdate; int vertexupdatenum; @@ -68,8 +66,6 @@ void r_shadow_start(void) r_shadow_mempool = Mem_AllocPool("R_Shadow"); maxshadowelements = 0; shadowelements = NULL; - maxshadowvertices = 0; - shadowvertex3f = NULL; maxvertexupdate = 0; vertexupdate = NULL; vertexupdatenum = 0; @@ -100,8 +96,6 @@ void r_shadow_shutdown(void) R_FreeTexturePool(&r_shadow_texturepool); maxshadowelements = 0; shadowelements = NULL; - maxshadowvertices = 0; - shadowvertex3f = NULL; maxvertexupdate = 0; vertexupdate = NULL; vertexupdatenum = 0; @@ -284,18 +278,6 @@ int *R_Shadow_ResizeShadowElements(int numtris) return shadowelements; } -float *R_Shadow_VertexBuffer(int numvertices) -{ - if (maxshadowvertices < numvertices) - { - maxshadowvertices = numvertices; - if (shadowvertex3f) - Mem_Free(shadowvertex3f); - shadowvertex3f = Mem_Alloc(r_shadow_mempool, maxshadowvertices * sizeof(float[3])); - } - return shadowvertex3f; -} - void R_Shadow_Volume(int numverts, int numtris, int *elements, int *neighbors, vec3_t relativelightorigin, float lightradius, float projectdistance) { int tris; @@ -350,16 +332,18 @@ void R_Shadow_Volume(int numverts, int numtris, int *elements, int *neighbors, v if (r_shadowstage == SHADOWSTAGE_STENCIL) { // increment stencil if backface is behind depthbuffer + //R_Mesh_EndBatch(); qglCullFace(GL_BACK); // quake is backwards, this culls front faces qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP); - R_Mesh_Draw(numverts * 2, tris, shadowelements); + R_Mesh_Draw_NoBatching(numverts * 2, tris, shadowelements); c_rt_shadowmeshes++; c_rt_shadowtris += numtris; // decrement stencil if frontface is behind depthbuffer + //R_Mesh_EndBatch(); qglCullFace(GL_FRONT); // quake is backwards, this culls back faces qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP); } - R_Mesh_Draw(numverts * 2, tris, shadowelements); + R_Mesh_Draw_NoBatching(numverts * 2, tris, shadowelements); c_rt_shadowmeshes++; c_rt_shadowtris += numtris; } @@ -370,17 +354,19 @@ void R_Shadow_RenderShadowMeshVolume(shadowmesh_t *firstmesh) if (r_shadowstage == SHADOWSTAGE_STENCIL) { // increment stencil if backface is behind depthbuffer + //R_Mesh_EndBatch(); qglCullFace(GL_BACK); // quake is backwards, this culls front faces qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP); for (mesh = firstmesh;mesh;mesh = mesh->next) { R_Mesh_GetSpace(mesh->numverts); R_Mesh_CopyVertex3f(mesh->vertex3f, mesh->numverts); - R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i); + R_Mesh_Draw_NoBatching(mesh->numverts, mesh->numtriangles, mesh->element3i); c_rtcached_shadowmeshes++; c_rtcached_shadowtris += mesh->numtriangles; } // decrement stencil if frontface is behind depthbuffer + //R_Mesh_EndBatch(); qglCullFace(GL_FRONT); // quake is backwards, this culls back faces qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP); } @@ -388,7 +374,7 @@ void R_Shadow_RenderShadowMeshVolume(shadowmesh_t *firstmesh) { R_Mesh_GetSpace(mesh->numverts); R_Mesh_CopyVertex3f(mesh->vertex3f, mesh->numverts); - R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i); + R_Mesh_Draw_NoBatching(mesh->numverts, mesh->numtriangles, mesh->element3i); c_rtcached_shadowmeshes++; c_rtcached_shadowtris += mesh->numtriangles; } @@ -537,6 +523,7 @@ void R_Shadow_Stage_Begin(void) || r_shadow_lightattenuationscale.value != r_shadow_attenscale) R_Shadow_MakeTextures(); + R_Mesh_EndBatch(); memset(&m, 0, sizeof(m)); m.blendfunc1 = GL_ONE; m.blendfunc2 = GL_ZERO; @@ -569,6 +556,7 @@ void R_Shadow_LoadWorldLightsIfNeeded(void) void R_Shadow_Stage_ShadowVolumes(void) { rmeshstate_t m; + //R_Mesh_EndBatch(); memset(&m, 0, sizeof(m)); R_Mesh_TextureState(&m); GL_Color(1, 1, 1, 1); @@ -596,6 +584,7 @@ void R_Shadow_Stage_ShadowVolumes(void) void R_Shadow_Stage_LightWithoutShadows(void) { rmeshstate_t m; + //R_Mesh_EndBatch(); memset(&m, 0, sizeof(m)); R_Mesh_TextureState(&m); qglActiveTexture(GL_TEXTURE0_ARB); @@ -618,6 +607,7 @@ void R_Shadow_Stage_LightWithoutShadows(void) void R_Shadow_Stage_LightWithShadows(void) { rmeshstate_t m; + //R_Mesh_EndBatch(); memset(&m, 0, sizeof(m)); R_Mesh_TextureState(&m); qglActiveTexture(GL_TEXTURE0_ARB); @@ -642,6 +632,7 @@ void R_Shadow_Stage_LightWithShadows(void) void R_Shadow_Stage_End(void) { rmeshstate_t m; + //R_Mesh_EndBatch(); // attempt to restore state to what Mesh_State thinks it is qglDisable(GL_BLEND); qglBlendFunc(GL_ONE, GL_ZERO); @@ -681,12 +672,14 @@ int R_Shadow_ScissorForBBoxAndSphere(const float *mins, const float *maxs, const && r_origin[1] >= mins[1] && r_origin[1] <= maxs[1] && r_origin[2] >= mins[2] && r_origin[2] <= maxs[2]) { + //R_Mesh_EndBatch(); qglDisable(GL_SCISSOR_TEST); return false; } VectorSubtract(r_origin, origin, v); if (DotProduct(v, v) < radius * radius) { + //R_Mesh_EndBatch(); qglDisable(GL_SCISSOR_TEST); return false; } @@ -801,6 +794,7 @@ int R_Shadow_ScissorForBBoxAndSphere(const float *mins, const float *maxs, const if (ix2 <= ix1 || iy2 <= iy1) return true; // set up the scissor rectangle + //R_Mesh_EndBatch(); qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1); qglEnable(GL_SCISSOR_TEST); c_rt_scissored++; @@ -821,6 +815,7 @@ int R_Shadow_ScissorForBBox(const float *mins, const float *maxs) // (?!? seems like a driver bug) so abort if gl_stencil is false if (!gl_stencil || BoxesOverlap(r_origin, r_origin, mins, maxs)) { + //R_Mesh_EndBatch(); qglDisable(GL_SCISSOR_TEST); return false; } @@ -974,6 +969,7 @@ int R_Shadow_ScissorForBBox(const float *mins, const float *maxs) if (ix2 <= ix1 || iy2 <= iy1) return true; // set up the scissor rectangle + //R_Mesh_EndBatch(); qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1); qglEnable(GL_SCISSOR_TEST); c_rt_scissored++; @@ -1117,6 +1113,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element if (r_shadow_texture3d.integer && r_textureunits.integer >= 4) { // 3/2 3D combine path (Geforce3, Radeon 8500) + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(bumptexture); m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture); m.tex3d[2] = R_GetTexture(r_shadow_attenuation3dtexture); @@ -1131,10 +1128,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin); R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[2], numverts, vertex3f, matrix_modeltoattenuationxyz); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(basetexture); m.tex[1] = 0; m.texcubemap[1] = R_GetTexture(lightcubemap); @@ -1145,7 +1143,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element qglColorMask(1,1,1,0); qglBlendFunc(GL_DST_ALPHA, GL_ONE); qglEnable(GL_BLEND); - + R_Mesh_GetSpace(numverts); + R_Mesh_CopyVertex3f(vertex3f, numverts); + R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); + if (lightcubemap) + R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter); VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2); for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--) { @@ -1153,12 +1155,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element color[1] = bound(0, color2[1], 1); color[2] = bound(0, color2[2], 1); GL_Color(color[0], color[1], color[2], 1); - R_Mesh_GetSpace(numverts); - R_Mesh_CopyVertex3f(vertex3f, numverts); - R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); - if (lightcubemap) - R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; } @@ -1166,6 +1163,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && lightcubemap) { // 1/2/2 3D combine path (original Radeon) + //R_Mesh_EndBatch(); m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture); R_Mesh_TextureState(&m); qglColorMask(0,0,0,1); @@ -1174,10 +1172,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element R_Mesh_GetSpace(numverts); R_Mesh_CopyVertex3f(vertex3f, numverts); R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(bumptexture); m.tex3d[0] = 0; m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture); @@ -1190,10 +1189,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element R_Mesh_CopyVertex3f(vertex3f, numverts); R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(basetexture); m.texcubemap[1] = R_GetTexture(lightcubemap); m.texcombinergb[0] = GL_MODULATE; @@ -1201,7 +1201,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element R_Mesh_TextureState(&m); qglColorMask(1,1,1,0); qglBlendFunc(GL_DST_ALPHA, GL_ONE); - + R_Mesh_GetSpace(numverts); + R_Mesh_CopyVertex3f(vertex3f, numverts); + R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); + if (lightcubemap) + R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter); VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2); for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--) { @@ -1209,12 +1213,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element color[1] = bound(0, color2[1], 1); color[2] = bound(0, color2[2], 1); GL_Color(color[0], color[1], color[2], 1); - R_Mesh_GetSpace(numverts); - R_Mesh_CopyVertex3f(vertex3f, numverts); - R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); - if (lightcubemap) - R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; } @@ -1222,6 +1221,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap) { // 2/2 3D combine path (original Radeon) + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(bumptexture); m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture); m.texcombinergb[0] = GL_REPLACE; @@ -1234,20 +1234,25 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element R_Mesh_CopyVertex3f(vertex3f, numverts); R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(basetexture); m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture); m.texcubemap[1] = 0; m.texcombinergb[0] = GL_MODULATE; m.texcombinergb[1] = GL_MODULATE; R_Mesh_TextureState(&m); + R_Mesh_GetSpace(numverts); qglColorMask(1,1,1,0); qglBlendFunc(GL_DST_ALPHA, GL_ONE); qglEnable(GL_BLEND); - + R_Mesh_GetSpace(numverts); + R_Mesh_CopyVertex3f(vertex3f, numverts); + R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); + R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz); VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2); for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--) { @@ -1255,11 +1260,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element color[1] = bound(0, color2[1], 1); color[2] = bound(0, color2[2], 1); GL_Color(color[0], color[1], color[2], 1); - R_Mesh_GetSpace(numverts); - R_Mesh_CopyVertex3f(vertex3f, numverts); - R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); - R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; } @@ -1267,6 +1268,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element else if (r_textureunits.integer >= 4) { // 4/2 2D combine path (Geforce3, Radeon 8500) + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(bumptexture); m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture); m.texcombinergb[0] = GL_REPLACE; @@ -1283,10 +1285,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin); R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[2], numverts, vertex3f, matrix_modeltoattenuationxyz); R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[3], numverts, vertex3f, matrix_modeltoattenuationz); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(basetexture); m.texcubemap[1] = R_GetTexture(lightcubemap); m.texcombinergb[0] = GL_MODULATE; @@ -1297,7 +1300,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element qglColorMask(1,1,1,0); qglBlendFunc(GL_DST_ALPHA, GL_ONE); qglEnable(GL_BLEND); - + R_Mesh_GetSpace(numverts); + R_Mesh_CopyVertex3f(vertex3f, numverts); + R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); + if (lightcubemap) + R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter); VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2); for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--) { @@ -1305,12 +1312,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element color[1] = bound(0, color2[1], 1); color[2] = bound(0, color2[2], 1); GL_Color(color[0], color[1], color[2], 1); - R_Mesh_GetSpace(numverts); - R_Mesh_CopyVertex3f(vertex3f, numverts); - R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); - if (lightcubemap) - R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; } @@ -1318,6 +1320,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element else { // 2/2/2 2D combine path (any dot3 card) + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture); m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture); R_Mesh_TextureState(&m); @@ -1328,10 +1331,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element R_Mesh_CopyVertex3f(vertex3f, numverts); R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz); R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(bumptexture); m.tex[1] = 0; m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture); @@ -1344,10 +1348,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element R_Mesh_CopyVertex3f(vertex3f, numverts); R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(basetexture); m.texcubemap[1] = R_GetTexture(lightcubemap); m.texcombinergb[0] = GL_MODULATE; @@ -1355,7 +1360,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element R_Mesh_TextureState(&m); qglColorMask(1,1,1,0); qglBlendFunc(GL_DST_ALPHA, GL_ONE); - + R_Mesh_GetSpace(numverts); + R_Mesh_CopyVertex3f(vertex3f, numverts); + R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); + if (lightcubemap) + R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter); VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2); for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--) { @@ -1363,12 +1372,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element color[1] = bound(0, color2[1], 1); color[2] = bound(0, color2[2], 1); GL_Color(color[0], color[1], color[2], 1); - R_Mesh_GetSpace(numverts); - R_Mesh_CopyVertex3f(vertex3f, numverts); - R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); - if (lightcubemap) - R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; } @@ -1379,6 +1383,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element if (r_textureunits.integer >= 2) { // voodoo2 + //R_Mesh_EndBatch(); #if 1 m.tex[0] = R_GetTexture(basetexture); m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture); @@ -1404,12 +1409,15 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationxyz); R_Shadow_VertexLightingWithXYAttenuationTexture(numverts, vertex3f, normal3f, color, matrix_modeltofilter); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); + c_rt_lightmeshes++; + c_rt_lighttris += numtriangles; } } else { // voodoo1 + //R_Mesh_EndBatch(); #if 1 m.tex[0] = R_GetTexture(basetexture); R_Mesh_TextureState(&m); @@ -1432,7 +1440,9 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element R_Mesh_CopyVertex3f(vertex3f, numverts); R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); R_Shadow_VertexLighting(numverts, vertex3f, normal3f, color, matrix_modeltofilter); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); + c_rt_lightmeshes++; + c_rt_lighttris += numtriangles; } } } @@ -1455,6 +1465,7 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare! { // 2/0/0/1/2 3D combine blendsquare path + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(bumptexture); m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture); m.texcombinergb[1] = GL_DOT3_RGBA_ARB; @@ -1468,10 +1479,11 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen R_Mesh_CopyVertex3f(vertex3f, numverts); R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); m.tex[0] = 0; m.texcubemap[1] = 0; m.texcombinergb[1] = GL_MODULATE; @@ -1482,32 +1494,38 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier) // 0.25 * 0.25 = 0.0625 (this is another pass) // 0.0625 * 0.0625 = 0.00390625 (this is another pass) - for (renders = 0;renders < 2;renders++) - { - R_Mesh_GetSpace(numverts); - R_Mesh_CopyVertex3f(vertex3f, numverts); - R_Mesh_Draw(numverts, numtriangles, elements); - } - c_rt_lightmeshes += 3; - c_rt_lighttris += numtriangles * 3; + R_Mesh_GetSpace(numverts); + R_Mesh_CopyVertex3f(vertex3f, numverts); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); + c_rt_lightmeshes++; + c_rt_lighttris += numtriangles; + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); + c_rt_lightmeshes++; + c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture); R_Mesh_TextureState(&m); qglBlendFunc(GL_DST_ALPHA, GL_ZERO); R_Mesh_GetSpace(numverts); R_Mesh_CopyVertex3f(vertex3f, numverts); R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); m.tex3d[0] = 0; m.tex[0] = R_GetTexture(glosstexture); m.texcubemap[1] = R_GetTexture(lightcubemap); R_Mesh_TextureState(&m); qglColorMask(1,1,1,0); qglBlendFunc(GL_DST_ALPHA, GL_ONE); - + R_Mesh_GetSpace(numverts); + R_Mesh_CopyVertex3f(vertex3f, numverts); + R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); + if (lightcubemap) + R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter); VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2); for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--) { @@ -1515,12 +1533,7 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen color[1] = bound(0, color2[1], 1); color[2] = bound(0, color2[2], 1); GL_Color(color[0], color[1], color[2], 1); - R_Mesh_GetSpace(numverts); - R_Mesh_CopyVertex3f(vertex3f, numverts); - R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); - if (lightcubemap) - R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; } @@ -1528,6 +1541,7 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare! { // 2/0/0/2 3D combine blendsquare path + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(bumptexture); m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture); m.texcombinergb[1] = GL_DOT3_RGBA_ARB; @@ -1541,10 +1555,11 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen R_Mesh_CopyVertex3f(vertex3f, numverts); R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); m.tex[0] = 0; m.texcubemap[1] = 0; m.texcombinergb[1] = GL_MODULATE; @@ -1555,15 +1570,17 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier) // 0.25 * 0.25 = 0.0625 (this is another pass) // 0.0625 * 0.0625 = 0.00390625 (this is another pass) - for (renders = 0;renders < 2;renders++) - { - R_Mesh_GetSpace(numverts); - R_Mesh_CopyVertex3f(vertex3f, numverts); - R_Mesh_Draw(numverts, numtriangles, elements); - } - c_rt_lightmeshes += 3; - c_rt_lighttris += numtriangles * 3; + R_Mesh_GetSpace(numverts); + R_Mesh_CopyVertex3f(vertex3f, numverts); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); + c_rt_lightmeshes++; + c_rt_lighttris += numtriangles; + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); + c_rt_lightmeshes++; + c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); + R_Mesh_GetSpace(numverts); R_Mesh_CopyVertex3f(vertex3f, numverts); R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); m.tex[0] = R_GetTexture(glosstexture); @@ -1571,9 +1588,10 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen R_Mesh_TextureState(&m); qglColorMask(1,1,1,0); qglBlendFunc(GL_DST_ALPHA, GL_ONE); - c_rt_lightmeshes++; - c_rt_lighttris += numtriangles; - + R_Mesh_GetSpace(numverts); + R_Mesh_CopyVertex3f(vertex3f, numverts); + R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); + R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz); VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2); for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--) { @@ -1581,11 +1599,7 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen color[1] = bound(0, color2[1], 1); color[2] = bound(0, color2[2], 1); GL_Color(color[0], color[1], color[2], 1); - R_Mesh_GetSpace(numverts); - R_Mesh_CopyVertex3f(vertex3f, numverts); - R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); - R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; } @@ -1593,6 +1607,7 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen else if (r_textureunits.integer >= 2 /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare! { // 2/0/0/2/2 2D combine blendsquare path + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(bumptexture); m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture); m.texcombinergb[1] = GL_DOT3_RGBA_ARB; @@ -1606,10 +1621,11 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen R_Mesh_CopyVertex3f(vertex3f, numverts); R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); m.tex[0] = 0; m.texcubemap[1] = 0; m.texcombinergb[1] = GL_MODULATE; @@ -1620,15 +1636,16 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier) // 0.25 * 0.25 = 0.0625 (this is another pass) // 0.0625 * 0.0625 = 0.00390625 (this is another pass) - for (renders = 0;renders < 2;renders++) - { - R_Mesh_GetSpace(numverts); - R_Mesh_CopyVertex3f(vertex3f, numverts); - R_Mesh_Draw(numverts, numtriangles, elements); - } - c_rt_lightmeshes += 3; - c_rt_lighttris += numtriangles * 3; + R_Mesh_GetSpace(numverts); + R_Mesh_CopyVertex3f(vertex3f, numverts); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); + c_rt_lightmeshes++; + c_rt_lighttris += numtriangles; + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); + c_rt_lightmeshes++; + c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture); m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture); R_Mesh_TextureState(&m); @@ -1637,16 +1654,21 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen R_Mesh_CopyVertex3f(vertex3f, numverts); R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz); R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; + //R_Mesh_EndBatch(); m.tex[0] = R_GetTexture(glosstexture); m.texcubemap[1] = R_GetTexture(lightcubemap); R_Mesh_TextureState(&m); qglColorMask(1,1,1,0); qglBlendFunc(GL_DST_ALPHA, GL_ONE); - + R_Mesh_GetSpace(numverts); + R_Mesh_CopyVertex3f(vertex3f, numverts); + R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); + if (lightcubemap) + R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter); VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2); for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--) { @@ -1654,12 +1676,7 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen color[1] = bound(0, color2[1], 1); color[2] = bound(0, color2[2], 1); GL_Color(color[0], color[1], color[2], 1); - R_Mesh_GetSpace(numverts); - R_Mesh_CopyVertex3f(vertex3f, numverts); - R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts); - if (lightcubemap) - R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter); - R_Mesh_Draw(numverts, numtriangles, elements); + R_Mesh_Draw_NoBatching(numverts, numtriangles, elements); c_rt_lightmeshes++; c_rt_lighttris += numtriangles; } diff --git a/r_shadow.h b/r_shadow.h index 6df1db9b..d6f13244 100644 --- a/r_shadow.h +++ b/r_shadow.h @@ -18,9 +18,6 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elements, const float *vertices, const float *svectors, const float *tvectors, const float *normals, const float *texcoords, const float *relativelightorigin, const float *relativeeyeorigin, float lightradius, const float *lightcolor, const matrix4x4_t *matrix_worldtofilter, const matrix4x4_t *matrix_worldtoattenuationxyz, const matrix4x4_t *matrix_worldtoattenuationz, rtexture_t *glosstexture, rtexture_t *bumptexture, rtexture_t *lightcubemap); void R_Shadow_ClearStencil(void); -// buffer space for the requested number of vertices, for processing -float *R_Shadow_VertexBuffer(int numvertices); - void R_Shadow_RenderShadowMeshVolume(shadowmesh_t *mesh); void R_Shadow_Stage_Begin(void); void R_Shadow_LoadWorldLightsIfNeeded(void); -- 2.39.2