light: clear global data so light can run multiple times per process

This commit is contained in:
Eric Wasylishen 2022-11-11 16:52:07 -07:00
parent b81b52da5a
commit a5125583fa
14 changed files with 141 additions and 12 deletions

View File

@ -32,5 +32,6 @@
// public functions
void ResetBounce();
const std::vector<surfacelight_t> &BounceLights();
void MakeBounceLights(const settings::worldspawn_keys &cfg, const mbsp_t *bsp);

View File

@ -122,6 +122,7 @@ public:
* Stores the RGB values to determine the light color
*/
void ResetLightEntities();
std::string TargetnameForLightStyle(int style);
std::vector<std::unique_ptr<light_t>> &GetLights();
std::vector<sun_t> &GetSuns();

View File

@ -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<std::string> &args);

View File

@ -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();

View File

@ -79,3 +79,4 @@ public:
};
const face_cache_t &FaceCacheForFNum(int fnum);
void ResetPhong();

View File

@ -49,6 +49,7 @@ struct surfacelight_t
bool rescale;
};
void ResetSurflight();
std::vector<surfacelight_t> &GetSurfaceLights();
std::optional<std::tuple<int32_t, int32_t>> IsSurfaceLitFace(const mbsp_t *bsp, const mface_t *face);
const std::vector<int> &SurfaceLightsForFaceNum(int facenum);

View File

@ -27,6 +27,7 @@
#include <common/polylib.hh>
#include <vector>
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);

View File

@ -44,6 +44,12 @@ mutex bouncelights_lock;
static std::vector<surfacelight_t> 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

View File

@ -33,6 +33,27 @@ std::vector<std::unique_ptr<light_t>> all_lights;
std::vector<sun_t> all_suns;
std::vector<entdict_t> entdicts;
std::vector<entdict_t> radlights;
static std::vector<std::pair<std::string, int>> lightstyleForTargetname;
static std::vector<std::unique_ptr<light_t>> 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<std::unique_ptr<light_t>> &GetLights()
{
@ -104,8 +125,6 @@ void light_t::expandAABB(const qvec3d &pt) { bounds += pt; }
* ============================================================================
*/
static std::vector<std::pair<std::string, int>> 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<std::unique_ptr<light_t>> surfacelight_templates;
const std::vector<std::unique_ptr<light_t>> &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);

View File

@ -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);

View File

@ -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;
}

View File

@ -182,6 +182,17 @@ static map<int, vector<const mface_t *>> planesToFaces;
static edgeToFaceMap_t EdgeToFaceMap;
static vector<face_cache_t> FaceCache;
void ResetPhong()
{
s_builtPhongCaches = false;
vertex_normals = {};
smoothFaces = {};
vertsToFaces = {};
planesToFaces = {};
EdgeToFaceMap = {};
FaceCache = {};
}
vector<const mface_t *> FacesUsingVert(int vertnum)
{
const auto &vertsToFaces_const = vertsToFaces;

View File

@ -42,6 +42,13 @@ static std::vector<surfacelight_t> surfacelights;
static std::map<int, std::vector<int>> surfacelightsByFacenum;
static size_t total_surflight_points = 0;
void ResetSurflight()
{
surfacelights = {};
surfacelightsByFacenum = {};
total_surflight_points = {};
}
std::vector<surfacelight_t> &GetSurfaceLights()
{
return surfacelights;

View File

@ -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);