]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rmain.c
an attempt at optimizing msurface_t dlight_cache checks by moving them
[xonotic/darkplaces.git] / gl_rmain.c
index 2ce371f1c4e37fff9d78e0b52798d00d4e3816d3..2c65e85cc20b0feb2d7f489448c81eb80f2e891b 100644 (file)
@@ -33,7 +33,7 @@ rtexturepool_t *r_main_texturepool;
 //
 r_refdef_t r_refdef;
 
-cvar_t r_depthfirst = {CVAR_SAVE, "r_depthfirst", "1", "renders a depth-only version of the scene before normal rendering begins to eliminate overdraw, values: 0 = off, 1 = world depth, 2 = world and model depth"};
+cvar_t r_depthfirst = {CVAR_SAVE, "r_depthfirst", "0", "renders a depth-only version of the scene before normal rendering begins to eliminate overdraw, values: 0 = off, 1 = world depth, 2 = world and model depth"};
 cvar_t r_useinfinitefarclip = {CVAR_SAVE, "r_useinfinitefarclip", "1", "enables use of a special kind of projection matrix that has an extremely large farclip"};
 cvar_t r_nearclip = {0, "r_nearclip", "1", "distance from camera of nearclip plane" };
 cvar_t r_showbboxes = {0, "r_showbboxes", "0", "shows bounding boxes of server entities, value controls opacity scaling (1 = 10%,  10 = 100%)"};
@@ -242,7 +242,7 @@ float FogPoint_World(const vec3_t p)
 
 float FogPoint_Model(const vec3_t p)
 {
-       return FogForDistance(VectorDistance((p), rsurface.modelorg));
+       return FogForDistance(VectorDistance((p), rsurface.modelorg) * Matrix4x4_ScaleFromMatrix(&rsurface.matrix));
 }
 
 static void R_BuildBlankTextures(void)
@@ -1736,7 +1736,7 @@ void R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, f
                else
                        qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
        }
-       if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogRangeRecip, r_refdef.fograngerecip);
+       if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogRangeRecip, r_refdef.fograngerecip * Matrix4x4_ScaleFromMatrix(&rsurface.matrix));
        if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, rsurface.texture->specularpower);
        if (r_glsl_permutation->loc_OffsetMapping_Scale >= 0) qglUniform1fARB(r_glsl_permutation->loc_OffsetMapping_Scale, r_glsl_offsetmapping_scale.value);
        CHECKGLERROR
@@ -2348,6 +2348,22 @@ GL_Init
 extern char *ENGINE_EXTENSIONS;
 void GL_Init (void)
 {
+       gl_renderer = (const char *)qglGetString(GL_RENDERER);
+       gl_vendor = (const char *)qglGetString(GL_VENDOR);
+       gl_version = (const char *)qglGetString(GL_VERSION);
+       gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
+
+       if (!gl_extensions)
+               gl_extensions = "";
+       if (!gl_platformextensions)
+               gl_platformextensions = "";
+
+       Con_Printf("GL_VENDOR: %s\n", gl_vendor);
+       Con_Printf("GL_RENDERER: %s\n", gl_renderer);
+       Con_Printf("GL_VERSION: %s\n", gl_version);
+       Con_Printf("GL_EXTENSIONS: %s\n", gl_extensions);
+       Con_Printf("%s_EXTENSIONS: %s\n", gl_platform, gl_platformextensions);
+
        VID_CheckExtensions();
 
        // LordHavoc: report supported extensions
@@ -2470,11 +2486,14 @@ static void R_View_UpdateEntityVisible (void)
        if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs)
        {
                // worldmodel can check visibility
+               memset(r_refdef.viewcache.entityvisible, 0, r_refdef.scene.numentities);
                for (i = 0;i < r_refdef.scene.numentities;i++)
                {
                        ent = r_refdef.scene.entities[i];
-                       r_refdef.viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model && ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)) || !R_CullBox(ent->mins, ent->maxs)) && ((ent->effects & EF_NODEPTHTEST) || (ent->flags & RENDER_VIEWMODEL) || r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.scene.worldmodel, r_refdef.viewcache.world_leafvisible, ent->mins, ent->maxs));
-
+                       if (!(ent->flags & renderimask))
+                       if (!R_CullBox(ent->mins, ent->maxs) || (ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)))
+                       if ((ent->effects & EF_NODEPTHTEST) || (ent->flags & RENDER_VIEWMODEL) || r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.scene.worldmodel, r_refdef.viewcache.world_leafvisible, ent->mins, ent->maxs))
+                               r_refdef.viewcache.entityvisible[i] = true;
                }
                if(r_cullentities_trace.integer && r_refdef.scene.worldmodel->brush.TraceLineOfSight)
                {
@@ -3404,6 +3423,9 @@ static void R_BlendView(void)
                if (r_glsl_permutation->loc_UserVec1 >= 0)
                {
                        float a=0, b=0, c=0, d=0;
+#if _MSC_VER >= 1400
+#define sscanf sscanf_s
+#endif
                        sscanf(r_glsl_postprocess_uservec1.string, "%f %f %f %f", &a, &b, &c, &d);
                        qglUniform4fARB(r_glsl_permutation->loc_UserVec1, a, b, c, d);
                }
@@ -4011,11 +4033,16 @@ static void R_DrawEntityBBoxes_Callback(const entity_render_t *ent, const rtligh
        int i;
        float color[4];
        prvm_edict_t *edict;
+       prvm_prog_t *prog_save = prog;
+
        // this function draws bounding boxes of server entities
        if (!sv.active)
                return;
+
        GL_CullFace(GL_NONE);
        R_SetupGenericShader(false);
+
+       prog = 0;
        SV_VM_Begin();
        for (i = 0;i < numsurfaces;i++)
        {
@@ -4036,6 +4063,7 @@ static void R_DrawEntityBBoxes_Callback(const entity_render_t *ent, const rtligh
                R_DrawBBoxMesh(edict->priv.server->areamins, edict->priv.server->areamaxs, color[0], color[1], color[2], color[3]);
        }
        SV_VM_End();
+       prog = prog_save;
 }
 
 static void R_DrawEntityBBoxes(void)
@@ -4043,19 +4071,29 @@ static void R_DrawEntityBBoxes(void)
        int i;
        prvm_edict_t *edict;
        vec3_t center;
+       prvm_prog_t *prog_save = prog;
+
        // this function draws bounding boxes of server entities
        if (!sv.active)
                return;
+
+       prog = 0;
        SV_VM_Begin();
        for (i = 0;i < prog->num_edicts;i++)
        {
                edict = PRVM_EDICT_NUM(i);
                if (edict->priv.server->free)
                        continue;
+               // exclude the following for now, as they don't live in world coordinate space and can't be solid:
+               if(PRVM_EDICTFIELDVALUE(edict, prog->fieldoffsets.tag_entity)->edict != 0)
+                       continue;
+               if(PRVM_EDICTFIELDVALUE(edict, prog->fieldoffsets.viewmodelforclient)->edict != 0)
+                       continue;
                VectorLerp(edict->priv.server->areamins, 0.5f, edict->priv.server->areamaxs, center);
                R_MeshQueue_AddTransparent(center, R_DrawEntityBBoxes_Callback, (entity_render_t *)NULL, i, (rtlight_t *)NULL);
        }
        SV_VM_End();
+       prog = prog_save;
 }
 
 unsigned short nomodelelements[24] =
@@ -4400,7 +4438,7 @@ static float R_EvaluateQ3WaveFunc(q3wavefunc_t func, const float *parms)
 void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
 {
        int i;
-       model_t *model = ent->model;
+       dp_model_t *model = ent->model;
        float f;
        float tcmat[12];
        q3shaderinfo_layer_tcmod_t *tcmod;
@@ -4717,7 +4755,7 @@ void R_Mesh_ResizeArrays(int newvertices)
 
 void RSurf_ActiveWorldEntity(void)
 {
-       model_t *model = r_refdef.scene.worldmodel;
+       dp_model_t *model = r_refdef.scene.worldmodel;
        if (rsurface.array_size < model->surfmesh.num_vertices)
                R_Mesh_ResizeArrays(model->surfmesh.num_vertices);
        rsurface.matrix = identitymatrix;
@@ -4786,7 +4824,7 @@ void RSurf_ActiveWorldEntity(void)
 
 void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, qboolean wanttangents)
 {
-       model_t *model = ent->model;
+       dp_model_t *model = ent->model;
        if (rsurface.array_size < model->surfmesh.num_vertices)
                R_Mesh_ResizeArrays(model->surfmesh.num_vertices);
        rsurface.matrix = ent->matrix;
@@ -6453,7 +6491,7 @@ void R_DrawDebugModel(entity_render_t *ent)
        const int *elements;
        q3mbrush_t *brush;
        msurface_t *surface;
-       model_t *model = ent->model;
+       dp_model_t *model = ent->model;
        vec3_t v;
 
        flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WALL;
@@ -6578,20 +6616,32 @@ void R_DrawDebugModel(entity_render_t *ent)
 }
 
 extern void R_BuildLightMap(const entity_render_t *ent, msurface_t *surface);
+int r_maxsurfacelist = 0;
+msurface_t **r_surfacelist = NULL;
 void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes, qboolean debug)
 {
        int i, j, endj, f, flagsmask;
-       msurface_t *surface;
        texture_t *t;
-       model_t *model = r_refdef.scene.worldmodel;
-       const int maxsurfacelist = 1024;
+       dp_model_t *model = r_refdef.scene.worldmodel;
+       msurface_t *surfaces;
+       unsigned char *update;
        int numsurfacelist = 0;
-       msurface_t *surfacelist[1024];
        if (model == NULL)
                return;
 
+       if (r_maxsurfacelist < model->num_surfaces)
+       {
+               r_maxsurfacelist = model->num_surfaces;
+               if (r_surfacelist)
+                       Mem_Free(r_surfacelist);
+               r_surfacelist = Mem_Alloc(r_main_mempool, r_maxsurfacelist * sizeof(*r_surfacelist));
+       }
+
        RSurf_ActiveWorldEntity();
 
+       surfaces = model->data_surfaces;
+       update = model->brushq1.lightmapupdateflags;
+
        // update light styles on this submodel
        if (!skysurfaces && !depthonly && !addwaterplanes && model->brushq1.num_lightstyles && r_refdef.lightmapintensity > 0)
        {
@@ -6600,11 +6650,10 @@ void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean dep
                {
                        if (style->value != r_refdef.scene.lightstylevalue[style->style])
                        {
-                               msurface_t *surfaces = model->data_surfaces;
                                int *list = style->surfacelist;
                                style->value = r_refdef.scene.lightstylevalue[style->style];
                                for (j = 0;j < style->numsurfaces;j++)
-                                       surfaces[list[j]].cached_dlight = true;
+                                       update[list[j]] = true;
                        }
                }
        }
@@ -6624,54 +6673,60 @@ void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean dep
        rsurface.texture = NULL;
        rsurface.rtlight = NULL;
        numsurfacelist = 0;
+       // add visible surfaces to draw list
        j = model->firstmodelsurface;
        endj = j + model->nummodelsurfaces;
-       while (j < endj)
+       if (update)
        {
-               // quickly skip over non-visible surfaces
-               for (;j < endj && !r_refdef.viewcache.world_surfacevisible[j];j++)
-                       ;
-               // quickly iterate over visible surfaces
-               for (;j < endj && r_refdef.viewcache.world_surfacevisible[j];j++)
+               for (;j < endj;j++)
                {
-                       // process this surface
-                       surface = model->data_surfaces + j;
-                       // if this surface fits the criteria, add it to the list
-                       if (surface->num_triangles)
+                       if (r_refdef.viewcache.world_surfacevisible[j])
                        {
-                               // if lightmap parameters changed, rebuild lightmap texture
-                               if (surface->cached_dlight)
-                                       R_BuildLightMap(r_refdef.scene.worldentity, surface);
-                               // add face to draw list
-                               surfacelist[numsurfacelist++] = surface;
-                               r_refdef.stats.world_triangles += surface->num_triangles;
-                               if (numsurfacelist >= maxsurfacelist)
-                               {
-                                       r_refdef.stats.world_surfaces += numsurfacelist;
-                                       R_QueueSurfaceList(r_refdef.scene.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
-                                       numsurfacelist = 0;
-                               }
+                               r_surfacelist[numsurfacelist++] = surfaces + j;
+                               // update lightmap if needed
+                               if (update[j])
+                                       R_BuildLightMap(r_refdef.scene.worldentity, surfaces + j);
                        }
                }
        }
-       r_refdef.stats.world_surfaces += numsurfacelist;
-       if (numsurfacelist)
-               R_QueueSurfaceList(r_refdef.scene.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
+       else
+               for (;j < endj;j++)
+                       if (r_refdef.viewcache.world_surfacevisible[j])
+                               r_surfacelist[numsurfacelist++] = surfaces + j;
+       // don't do anything if there were no surfaces
+       if (!numsurfacelist)
+               return;
+       R_QueueSurfaceList(r_refdef.scene.worldentity, numsurfacelist, r_surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
        GL_AlphaTest(false);
+
+       // add to stats if desired
+       if (r_speeds.integer && !skysurfaces && !depthonly && !addwaterplanes)
+       {
+               r_refdef.stats.world_surfaces += numsurfacelist;
+               for (j = 0;j < numsurfacelist;j++)
+                       r_refdef.stats.world_triangles += r_surfacelist[j]->num_triangles;
+       }
 }
 
 void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes, qboolean debug)
 {
-       int i, j, f, flagsmask;
-       msurface_t *surface, *endsurface;
+       int i, j, endj, f, flagsmask;
        texture_t *t;
-       model_t *model = ent->model;
-       const int maxsurfacelist = 1024;
+       dp_model_t *model = ent->model;
+       msurface_t *surfaces;
+       unsigned char *update;
        int numsurfacelist = 0;
-       msurface_t *surfacelist[1024];
        if (model == NULL)
                return;
 
+       if (r_maxsurfacelist < model->num_surfaces)
+       {
+               r_maxsurfacelist = model->num_surfaces;
+               if (r_surfacelist)
+                       Mem_Free(r_surfacelist);
+               r_surfacelist = Mem_Alloc(r_main_mempool, r_maxsurfacelist * sizeof(*r_surfacelist));
+       }
+
        // if the model is static it doesn't matter what value we give for
        // wantnormals and wanttangents, so this logic uses only rules applicable
        // to a model, knowing that they are meaningless otherwise
@@ -6682,6 +6737,9 @@ void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean wr
        else
                RSurf_ActiveModelEntity(ent, true, r_glsl.integer && gl_support_fragment_shader && !depthonly);
 
+       surfaces = model->data_surfaces;
+       update = model->brushq1.lightmapupdateflags;
+
        // update light styles
        if (!skysurfaces && !depthonly && !addwaterplanes && model->brushq1.num_lightstyles && r_refdef.lightmapintensity > 0)
        {
@@ -6690,11 +6748,10 @@ void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean wr
                {
                        if (style->value != r_refdef.scene.lightstylevalue[style->style])
                        {
-                               msurface_t *surfaces = model->data_surfaces;
                                int *list = style->surfacelist;
                                style->value = r_refdef.scene.lightstylevalue[style->style];
                                for (j = 0;j < style->numsurfaces;j++)
-                                       surfaces[list[j]].cached_dlight = true;
+                                       update[list[j]] = true;
                        }
                }
        }
@@ -6714,29 +6771,28 @@ void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean wr
        rsurface.texture = NULL;
        rsurface.rtlight = NULL;
        numsurfacelist = 0;
-       surface = model->data_surfaces + model->firstmodelsurface;
-       endsurface = surface + model->nummodelsurfaces;
-       for (;surface < endsurface;surface++)
+       // add visible surfaces to draw list
+       j = model->firstmodelsurface;
+       endj = j + model->nummodelsurfaces;
+       for (;j < endj;j++)
+               r_surfacelist[numsurfacelist++] = surfaces + j;
+       // don't do anything if there were no surfaces
+       if (!numsurfacelist)
+               return;
+       // update lightmaps if needed
+       if (update)
+               for (j = model->firstmodelsurface;j < endj;j++)
+                       if (update[j])
+                               R_BuildLightMap(ent, surfaces + j);
+       R_QueueSurfaceList(ent, numsurfacelist, r_surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
+       GL_AlphaTest(false);
+
+       // add to stats if desired
+       if (r_speeds.integer && !skysurfaces && !depthonly && !addwaterplanes)
        {
-               // if this surface fits the criteria, add it to the list
-               if (surface->num_triangles)
-               {
-                       // if lightmap parameters changed, rebuild lightmap texture
-                       if (surface->cached_dlight)
-                               R_BuildLightMap(ent, surface);
-                       // add face to draw list
-                       surfacelist[numsurfacelist++] = surface;
-                       r_refdef.stats.entities_triangles += surface->num_triangles;
-                       if (numsurfacelist >= maxsurfacelist)
-                       {
-                               r_refdef.stats.entities_surfaces += numsurfacelist;
-                               R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
-                               numsurfacelist = 0;
-                       }
-               }
+               r_refdef.stats.entities++;
+               r_refdef.stats.entities_surfaces += numsurfacelist;
+               for (j = 0;j < numsurfacelist;j++)
+                       r_refdef.stats.entities_triangles += r_surfacelist[j]->num_triangles;
        }
-       r_refdef.stats.entities_surfaces += numsurfacelist;
-       if (numsurfacelist)
-               R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
-       GL_AlphaTest(false);
 }