From a5125583fa730d9faa8c1b87a8c92624dfa375e2 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Fri, 11 Nov 2022 16:52:07 -0700 Subject: [PATCH] light: clear global data so light can run multiple times per process --- include/light/bounce.hh | 1 + include/light/entities.hh | 1 + include/light/light.hh | 1 + include/light/ltface.hh | 1 + include/light/phong.hh | 1 + include/light/surflight.hh | 1 + include/light/trace_embree.hh | 1 + light/bounce.cc | 6 +++++ light/entities.cc | 28 +++++++++++++++------ light/light.cc | 47 +++++++++++++++++++++++++++++++++++ light/ltface.cc | 18 ++++++++++++++ light/phong.cc | 11 ++++++++ light/surflight.cc | 7 ++++++ light/trace_embree.cc | 29 +++++++++++++++++---- 14 files changed, 141 insertions(+), 12 deletions(-) diff --git a/include/light/bounce.hh b/include/light/bounce.hh index 8c306812..448fd877 100644 --- a/include/light/bounce.hh +++ b/include/light/bounce.hh @@ -32,5 +32,6 @@ // public functions +void ResetBounce(); const std::vector &BounceLights(); void MakeBounceLights(const settings::worldspawn_keys &cfg, const mbsp_t *bsp); diff --git a/include/light/entities.hh b/include/light/entities.hh index f038cf5e..c5de03f8 100644 --- a/include/light/entities.hh +++ b/include/light/entities.hh @@ -122,6 +122,7 @@ public: * Stores the RGB values to determine the light color */ +void ResetLightEntities(); std::string TargetnameForLightStyle(int style); std::vector> &GetLights(); std::vector &GetSuns(); diff --git a/include/light/light.hh b/include/light/light.hh index f117a8ad..d2f04e54 100644 --- a/include/light/light.hh +++ b/include/light/light.hh @@ -427,5 +427,6 @@ const modelinfo_t *ModelInfoForFace(const mbsp_t *bsp, int facenum); const img::texture *Face_Texture(const mbsp_t *bsp, const mface_t *face); const qvec3b &Face_LookupTextureColor(const mbsp_t *bsp, const mface_t *face); const qvec3d &Face_LookupTextureBounceColor(const mbsp_t *bsp, const mface_t *face); +void light_reset(); int light_main(int argc, const char **argv); int light_main(const std::vector &args); diff --git a/include/light/ltface.hh b/include/light/ltface.hh index 70d8adf3..0f090963 100644 --- a/include/light/ltface.hh +++ b/include/light/ltface.hh @@ -61,3 +61,4 @@ void FinishLightmapSurface(const mbsp_t *bsp, lightsurf_t *lightsurf); void SaveLightmapSurface(const mbsp_t *bsp, mface_t *face, facesup_t *facesup, bspx_decoupled_lm_perface *facesup_decoupled, lightsurf_t *lightsurf, const faceextents_t &extents, const faceextents_t &output_extents); +void ResetLtFace(); diff --git a/include/light/phong.hh b/include/light/phong.hh index 341eb889..d2fbb674 100644 --- a/include/light/phong.hh +++ b/include/light/phong.hh @@ -79,3 +79,4 @@ public: }; const face_cache_t &FaceCacheForFNum(int fnum); +void ResetPhong(); diff --git a/include/light/surflight.hh b/include/light/surflight.hh index 532eeeaf..c04104e6 100644 --- a/include/light/surflight.hh +++ b/include/light/surflight.hh @@ -49,6 +49,7 @@ struct surfacelight_t bool rescale; }; +void ResetSurflight(); std::vector &GetSurfaceLights(); std::optional> IsSurfaceLitFace(const mbsp_t *bsp, const mface_t *face); const std::vector &SurfaceLightsForFaceNum(int facenum); diff --git a/include/light/trace_embree.hh b/include/light/trace_embree.hh index 219a3eac..05f73115 100644 --- a/include/light/trace_embree.hh +++ b/include/light/trace_embree.hh @@ -27,6 +27,7 @@ #include #include +void ResetEmbree(); void Embree_TraceInit(const mbsp_t *bsp); hitresult_t Embree_TestSky(const qvec3d &start, const qvec3d &dirn, const modelinfo_t *self, const mface_t **face_out); hitresult_t Embree_TestLight(const qvec3d &start, const qvec3d &stop, const modelinfo_t *self); diff --git a/light/bounce.cc b/light/bounce.cc index c74ecce1..974f6723 100644 --- a/light/bounce.cc +++ b/light/bounce.cc @@ -44,6 +44,12 @@ mutex bouncelights_lock; static std::vector bouncelights; static std::atomic_size_t bouncelightpoints; +void ResetBounce() +{ + bouncelights.clear(); + bouncelightpoints = 0; +} + static bool Face_ShouldBounce(const mbsp_t *bsp, const mface_t *face) { // make bounce light, only if this face is shadow casting diff --git a/light/entities.cc b/light/entities.cc index d413fb0a..637680dd 100644 --- a/light/entities.cc +++ b/light/entities.cc @@ -33,6 +33,27 @@ std::vector> all_lights; std::vector all_suns; std::vector entdicts; std::vector radlights; +static std::vector> lightstyleForTargetname; +static std::vector> surfacelight_templates; +static std::ofstream surflights_dump_file; +static fs::path surflights_dump_filename; + +/** + * Resets global data in this file + */ +void ResetLightEntities() +{ + all_lights.clear(); + all_suns.clear(); + entdicts.clear(); + radlights.clear(); + + lightstyleForTargetname.clear(); + + surfacelight_templates.clear(); + surflights_dump_file = {}; + surflights_dump_filename.clear(); +} std::vector> &GetLights() { @@ -104,8 +125,6 @@ void light_t::expandAABB(const qvec3d &pt) { bounds += pt; } * ============================================================================ */ -static std::vector> lightstyleForTargetname; - entdict_t &WorldEnt() { if (entdicts.size() == 0 || entdicts.at(0).get("classname") != "worldspawn") { @@ -1152,16 +1171,11 @@ void WriteEntitiesToString(const settings::worldspawn_keys &cfg, mbsp_t *bsp) * ======================================================================= */ -static std::vector> surfacelight_templates; - const std::vector> &GetSurfaceLightTemplates() { return surfacelight_templates; } -static std::ofstream surflights_dump_file; -static fs::path surflights_dump_filename; - static void SurfLights_WriteEntityToFile(light_t *entity, const qvec3d &pos) { Q_assert(entity->epairs != nullptr); diff --git a/light/light.cc b/light/light.cc index 52be1f05..a96a9cd2 100644 --- a/light/light.cc +++ b/light/light.cc @@ -1465,6 +1465,51 @@ void load_textures(const mbsp_t *bsp) } } +/** + * Resets globals in this file + */ +static void ResetLight() +{ + dirt_in_use = false; + light_surfaces.clear(); + faces_sup.clear(); + facesup_decoupled_global.clear(); + + filebase.clear(); + file_p = 0; + file_end = 0; + + lit_filebase.clear(); + lit_file_p = 0; + lit_file_end = 0; + + lux_filebase.clear(); + lux_file_p = 0; + lux_file_end = 0; + + modelinfo.clear(); + tracelist.clear(); + selfshadowlist.clear(); + shadowworldonlylist.clear(); + switchableshadowlist.clear(); + + extended_texinfo_flags.clear(); + + dump_facenum = -1; + dump_vertnum = -1; +} + +void light_reset() +{ + ResetBounce(); + ResetLightEntities(); + ResetLight(); + ResetLtFace(); + ResetPhong(); + ResetSurflight(); + ResetEmbree(); +} + /* * ================== * main @@ -1473,6 +1518,8 @@ void load_textures(const mbsp_t *bsp) */ int light_main(int argc, const char **argv) { + light_reset(); + bspdata_t bspdata; light_options.preinitialize(argc, argv); diff --git a/light/ltface.cc b/light/ltface.cc index f4b11634..13cc6e1e 100644 --- a/light/ltface.cc +++ b/light/ltface.cc @@ -1953,6 +1953,7 @@ void SetupDirt(settings::worldspawn_keys &cfg) /* iterate angle */ float angle = 0.0f; + numDirtVectors = 0; for (int i = 0; i < DIRT_NUM_ANGLE_STEPS; i++, angle += angleStep) { /* iterate elevation */ float elevation = elevationStep * 0.5f; @@ -3037,3 +3038,20 @@ void IndirectLightFace(const mbsp_t *bsp, lightsurf_t &lightsurf, const settings } } } + +void ResetLtFace() +{ + total_light_rays = 0; + total_light_ray_hits = 0; + total_samplepoints = 0; + + total_bounce_rays = 0; + total_bounce_ray_hits = 0; + total_surflight_rays = 0; + total_surflight_ray_hits = 0; + + fully_transparent_lightmaps = 0; + + warned_about_light_map_overflow = false; + warned_about_light_style_overflow = false; +} diff --git a/light/phong.cc b/light/phong.cc index 24e57edb..e9d793ef 100644 --- a/light/phong.cc +++ b/light/phong.cc @@ -182,6 +182,17 @@ static map> planesToFaces; static edgeToFaceMap_t EdgeToFaceMap; static vector FaceCache; +void ResetPhong() +{ + s_builtPhongCaches = false; + vertex_normals = {}; + smoothFaces = {}; + vertsToFaces = {}; + planesToFaces = {}; + EdgeToFaceMap = {}; + FaceCache = {}; +} + vector FacesUsingVert(int vertnum) { const auto &vertsToFaces_const = vertsToFaces; diff --git a/light/surflight.cc b/light/surflight.cc index 904a62ed..09288f4a 100644 --- a/light/surflight.cc +++ b/light/surflight.cc @@ -42,6 +42,13 @@ static std::vector surfacelights; static std::map> surfacelightsByFacenum; static size_t total_surflight_points = 0; +void ResetSurflight() +{ + surfacelights = {}; + surfacelightsByFacenum = {}; + total_surflight_points = {}; +} + std::vector &GetSurfaceLights() { return surfacelights; diff --git a/light/trace_embree.cc b/light/trace_embree.cc index 9290171c..65766ead 100644 --- a/light/trace_embree.cc +++ b/light/trace_embree.cc @@ -32,6 +32,30 @@ sceneinfo skygeom; // sky. always occludes. sceneinfo solidgeom; // solids. always occludes. sceneinfo filtergeom; // conditional occluders.. needs to run ray intersection filter +static RTCDevice device; +RTCScene scene; + +static const mbsp_t *bsp_static; + +void ResetEmbree() +{ + skygeom = {}; + solidgeom = {}; + filtergeom = {}; + + if (scene) { + rtcReleaseScene(scene); + scene = nullptr; + } + + if (device) { + rtcReleaseDevice(device); + device = nullptr; + } + + bsp_static = nullptr; +} + /** * Returns 1.0 unless a custom alpha value is set. * The priority is: "_light_alpha" (read from extended_texinfo_flags), then "alpha", then Q2 surface flags @@ -229,11 +253,6 @@ static void CreateGeometryFromWindings(RTCDevice g_device, RTCScene scene, const rtcCommitGeometry(geom_1); } -RTCDevice device; -RTCScene scene; - -static const mbsp_t *bsp_static; - void ErrorCallback(void *userptr, const RTCError code, const char *str) { fmt::print("RTC Error {}: {}\n", code, str);