Skip to content

Commit

Permalink
WiP: Forward port HDR lightmaps to latest.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsvensson committed Dec 6, 2023
1 parent 3eb5cb7 commit 4c2493d
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 25 deletions.
16 changes: 16 additions & 0 deletions common/bspfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,22 @@ void bspdata_t::bspxentries::transfer(const char *xname, std::vector<uint8_t> &&
entries.insert_or_assign(xname, xdata);
}

// get rid of this, just stupid
void bspdata_t::bspxentries::transfer(const char *xname, std::vector<uint32_t> &xdata)
{
std::vector<uint8_t> output;
output.reserve(xdata.size() * 4);

for (uint32_t value : xdata) {
output.push_back(static_cast<uint8_t>(value & 0xFF));
output.push_back(static_cast<uint8_t>((value >> 8) & 0xFF));
output.push_back(static_cast<uint8_t>((value >> 16) & 0xFF));
output.push_back(static_cast<uint8_t>((value >> 24) & 0xFF));
}

entries.insert_or_assign(xname, std::move(output));
}

/*
* =============
* LoadBSPFile
Expand Down
3 changes: 3 additions & 0 deletions include/common/bspfile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ struct bspdata_t
// transfer ownership of the vector into a BSPX lump
void transfer(const char *xname, std::vector<uint8_t> &xdata);

// transfer ownership of the vector into a BSPX lump
void transfer(const char *xname, std::vector<uint32_t> &xdata);

// transfer ownership of the vector into a BSPX lump
void transfer(const char *xname, std::vector<uint8_t> &&xdata);
};
Expand Down
10 changes: 7 additions & 3 deletions include/light/light.hh
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ enum class lightfile
external = 1,
bspx = 2,
both = external | bspx,
lit2 = 4
lit2 = 4,
hdr = 8,
};

/* tracelist is a std::vector of pointers to modelinfo_t to use for LOS tests */
Expand Down Expand Up @@ -390,6 +391,8 @@ public:
setting_func lit;
setting_func lit2;
setting_func bspxlit;
setting_func hdr;
setting_func bspxhdr;
setting_func lux;
setting_func bspxlux;
setting_func bspxonly;
Expand Down Expand Up @@ -432,6 +435,7 @@ extern settings::light_settings light_options;

extern std::vector<uint8_t> filebase;
extern std::vector<uint8_t> lit_filebase;
extern std::vector<uint32_t> hdr_filebase;
extern std::vector<uint8_t> lux_filebase;

const std::unordered_map<int, std::vector<uint8_t>> &UncompressedVis();
Expand All @@ -446,8 +450,8 @@ extern std::vector<surfflags_t> extended_texinfo_flags;
// public functions

void FixupGlobalSettings(void);
void GetFileSpace(uint8_t **lightdata, uint8_t **colordata, uint8_t **deluxdata, int size);
void GetFileSpace_PreserveOffsetInBsp(uint8_t **lightdata, uint8_t **colordata, uint8_t **deluxdata, int lightofs);
void GetFileSpace(uint8_t **lightdata, uint8_t **colordata, uint32_t **hdrdata, uint8_t **deluxdata, int size);
void GetFileSpace_PreserveOffsetInBsp(uint8_t **lightdata, uint8_t **colordata, uint32_t **hdrdata, uint8_t **deluxdata, int lightofs);
const modelinfo_t *ModelInfoForModel(const mbsp_t *bsp, int modelnum);
/**
* returns nullptr for "skip" faces
Expand Down
45 changes: 43 additions & 2 deletions light/light.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ static int lit_file_p;
/// offset of end of space for litfile data
static int lit_file_end;

/// start of litfile data
std::vector<uint32_t> hdr_filebase;
/// offset of start of free space after litfile data (should be kept a multiple of 12)
static int hdr_file_p;
/// offset of end of space for litfile data
static int hdr_file_end;

/// start of luxfile data
std::vector<uint8_t> lux_filebase;
/// offset of start of free space after luxfile data (should be kept a multiple of 12)
Expand Down Expand Up @@ -338,6 +345,9 @@ light_settings::light_settings()
lit2{this, "lit2", [&](source) { write_litfile = lightfile::lit2; }, &experimental_group, "write .lit2 file"},
bspxlit{this, "bspxlit", [&](source) { write_litfile |= lightfile::bspx; }, &experimental_group,
"writes rgb data into the bsp itself"},
hdr{this, "hdr", [&](source) { write_litfile |= lightfile::external; write_litfile |= lightfile::hdr; }, &experimental_group, "write .lit file as e5bgr9"},
bspxhdr{this, "bspxhdr", [&](source) { write_litfile |= lightfile::bspx; write_litfile |= lightfile::hdr; }, &experimental_group,
"writes rgb data into the bsp itself as e5bgr9"},
lux{this, "lux", [&](source) { write_luxfile |= lightfile::external; }, &experimental_group, "write .lux file"},
bspxlux{this, "bspxlux", [&](source) { write_luxfile |= lightfile::bspx; }, &experimental_group,
"writes lux data into the bsp itself"},
Expand Down Expand Up @@ -484,8 +494,12 @@ void light_settings::postinitialize(int argc, const char **argv)
} else {
if (write_litfile & lightfile::external)
logging::print(".lit colored light output requested on command line.\n");
if (write_litfile & lightfile::external && write_litfile & lightfile::hdr)
logging::print(".lit colored E5BGR9 light output requested on command line.\n");
if (write_litfile & lightfile::bspx)
logging::print("BSPX colored light output requested on command line.\n");
if (write_litfile & lightfile::bspx && write_litfile & lightfile::hdr)
logging::print("BSPX colored E5BGR9 light output requested on command line.\n");
if (write_luxfile & lightfile::external)
logging::print(".lux light directions output requested on command line.\n");
if (write_luxfile & lightfile::bspx)
Expand Down Expand Up @@ -563,18 +577,22 @@ static std::mutex light_mutex;
* size is the number of greyscale pixels = number of bytes to allocate
* and return in *lightdata
*/
void GetFileSpace(uint8_t **lightdata, uint8_t **colordata, uint8_t **deluxdata, int size)
void GetFileSpace(uint8_t **lightdata, uint8_t **colordata, uint32_t **hdrdata, uint8_t **deluxdata, int size)
{
light_mutex.lock();

*lightdata = *colordata = *deluxdata = nullptr;
*hdrdata = nullptr;

if (!filebase.empty()) {
*lightdata = filebase.data() + file_p;
}
if (!lit_filebase.empty()) {
*colordata = lit_filebase.data() + lit_file_p;
}
if (!hdr_filebase.empty()) {
*hdrdata = hdr_filebase.data() + hdr_file_p;
}
if (!lux_filebase.empty()) {
*deluxdata = lux_filebase.data() + lux_file_p;
}
Expand All @@ -592,6 +610,9 @@ void GetFileSpace(uint8_t **lightdata, uint8_t **colordata, uint8_t **deluxdata,
if (!lit_filebase.empty()) {
lit_file_p += 3 * size;
}
if (!hdr_filebase.empty()) {
hdr_file_p += size;
}
if (!lux_filebase.empty()) {
lux_file_p += 3 * size;
}
Expand All @@ -603,13 +624,16 @@ void GetFileSpace(uint8_t **lightdata, uint8_t **colordata, uint8_t **deluxdata,

if (lit_file_p > lit_file_end)
FError("overrun");

if (hdr_file_p > hdr_file_end)
FError("overrun");
}

/**
* Special version of GetFileSpace for when we're relighting a .bsp and can't modify it.
* In this case the offsets are already known.
*/
void GetFileSpace_PreserveOffsetInBsp(uint8_t **lightdata, uint8_t **colordata, uint8_t **deluxdata, int lightofs)
void GetFileSpace_PreserveOffsetInBsp(uint8_t **lightdata, uint8_t **colordata, uint32_t **hdrdata, uint8_t **deluxdata, int lightofs)
{
Q_assert(lightofs >= 0);

Expand All @@ -623,6 +647,10 @@ void GetFileSpace_PreserveOffsetInBsp(uint8_t **lightdata, uint8_t **colordata,
*colordata = lit_filebase.data() + (lightofs * 3);
}

if (hdrdata && !hdr_filebase.empty()) {
*hdrdata = hdr_filebase.data() + lightofs;
}

if (deluxdata && !lux_filebase.empty()) {
*deluxdata = lux_filebase.data() + (lightofs * 3);
}
Expand Down Expand Up @@ -871,6 +899,7 @@ static void LightWorld(bspdata_t *bspdata, bool forcedscale)
light_surfaces.clear();
filebase.clear();
lit_filebase.clear();
hdr_filebase.clear();
lux_filebase.clear();

if (!bsp.loadversion->game->has_rgb_lightmap) {
Expand All @@ -887,6 +916,13 @@ static void LightWorld(bspdata_t *bspdata, bool forcedscale)
lit_file_end = (MAX_MAP_LIGHTING * 3);
}

if (bsp.loadversion->game->has_rgb_lightmap || light_options.write_litfile) {
/* hdr data stored in a separate buffer */
hdr_filebase.resize(MAX_MAP_LIGHTING);
hdr_file_p = 0;
hdr_file_end = MAX_MAP_LIGHTING;
}

if (light_options.write_luxfile) {
/* lux data stored in a separate buffer */
lux_filebase.resize(MAX_MAP_LIGHTING * 3);
Expand Down Expand Up @@ -1643,6 +1679,7 @@ int light_main(int argc, const char **argv)

/*invalidate any bspx lighting info early*/
bspdata.bspx.entries.erase("RGBLIGHTING");
bspdata.bspx.entries.erase("LIGHTING_E5BGR9");
bspdata.bspx.entries.erase("LIGHTINGDIR");

if (light_options.write_litfile == lightfile::lit2) {
Expand All @@ -1658,6 +1695,10 @@ int light_main(int argc, const char **argv)
lit_filebase.resize(bsp.dlightdata.size() * 3);
bspdata.bspx.transfer("RGBLIGHTING", lit_filebase);
}
if (light_options.write_litfile & lightfile::bspx && light_options.write_litfile & lightfile::hdr) {
hdr_filebase.resize(bsp.dlightdata.size());
bspdata.bspx.transfer("LIGHTING_E5BGR9", hdr_filebase);
}
if (light_options.write_luxfile & lightfile::external) {
WriteLuxFile(&bsp, source, LIT_VERSION);
}
Expand Down
8 changes: 6 additions & 2 deletions light/litfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ void WriteLitFile(const mbsp_t *bsp, const std::vector<facesup_t> &facesup, cons
}
litfile.write((const char *)lit_filebase.data(), bsp->dlightdata.size() * 3);
litfile.write((const char *)lux_filebase.data(), bsp->dlightdata.size() * 3);
} else
litfile.write((const char *)lit_filebase.data(), bsp->dlightdata.size() * 3);
} else {
if ((version & 0xffff0000)==0x00010000)
litfile.write((const char *)hdr_filebase.data(), bsp->dlightdata.size() * 4);
else
litfile.write((const char *)lit_filebase.data(), bsp->dlightdata.size() * 3);
}
}

void WriteLuxFile(const mbsp_t *bsp, const fs::path &filename, int version)
Expand Down
Loading

0 comments on commit 4c2493d

Please sign in to comment.