From c6b19fe6daaf7f58323b30d5b44e8bfa7bdef149 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Mon, 14 Nov 2022 00:30:37 -0700 Subject: [PATCH] light: implement -novanilla with -world_units_per_luxel currently it's required because the non-novanilla path isn't yet implemented --- light/ltface.cc | 10 ++++++---- tests/test_ltface.cc | 44 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/light/ltface.cc b/light/ltface.cc index 13cc6e1e..708055c9 100644 --- a/light/ltface.cc +++ b/light/ltface.cc @@ -2848,13 +2848,15 @@ void SaveLightmapSurface(const mbsp_t *bsp, mface_t *face, facesup_t *facesup, lightofs = out - filebase.data(); } - if (facesup) { + if (facesup_decoupled && light_options.novanilla.value()) { + facesup_decoupled->offset = lightofs; + face->lightofs = -1; + } else if (facesup_decoupled && !light_options.novanilla.value()) { + FError("-world_units_per_luxel currently requires -novanilla"); + } else if (facesup) { facesup->lightofs = lightofs; } else { face->lightofs = lightofs; - if (facesup_decoupled) { - facesup_decoupled->offset = lightofs; - } } // sanity check that we don't save a lightmap for a non-lightmapped face diff --git a/tests/test_ltface.cc b/tests/test_ltface.cc index fee69fd4..8309fccd 100644 --- a/tests/test_ltface.cc +++ b/tests/test_ltface.cc @@ -64,7 +64,7 @@ static testresults_t LoadTestmap(const std::filesystem::path &name, std::vector< } TEST_CASE("TestLight") { - LoadTestmap("q2_lightmap_custom_scale.map", {"-threads", "1", "-world_units_per_luxel", "8"}); + LoadTestmap("q2_lightmap_custom_scale.map", {"-threads", "1", "-world_units_per_luxel", "8", "-novanilla"}); } TEST_CASE("emissive cube artifacts") { @@ -80,7 +80,7 @@ TEST_CASE("emissive cube artifacts") { // // "_surflight_rescale" "0" // - auto [bsp, bspx] = LoadTestmap("light_q2_emissive_cube.map", {"-threads", "1", "-world_units_per_luxel", "4"}); + auto [bsp, bspx] = LoadTestmap("light_q2_emissive_cube.map", {"-threads", "1", "-world_units_per_luxel", "4", "-novanilla"}); const auto start = qvec3d{1044, -1244, 880}; const auto end = qvec3d{1044, -1272, 880}; @@ -107,3 +107,43 @@ TEST_CASE("emissive cube artifacts") { previous_sample = sample; } } + +TEST_CASE("-novanilla + -world_units_per_luxel") +{ + auto [bsp, bspx] = LoadTestmap("q2_lightmap_custom_scale.map", {"-novanilla", "-world_units_per_luxel", "8"}); + + for (auto &face : bsp.dfaces) { + CHECK(face.lightofs == -1); + } + + // make sure no other bspx lumps are written + CHECK(bspx.size() == 1); + CHECK(bspx.find("DECOUPLED_LM") != bspx.end()); + + // make sure all dlightdata bytes are accounted for by the DECOUPLED_LM lump + // and no extra was written. + size_t expected_dlightdata_bytes = 0; + for (auto &face : bsp.dfaces) { + // count used styles + size_t face_used_styles = 0; + for (auto style : face.styles) { + if (style != 255) { + ++face_used_styles; + } + } + + // count used pixels per style + auto lm_info = BSPX_DecoupledLM(bspx, Face_GetNum(&bsp, &face)); + const faceextents_t extents(face, bsp, lm_info.lmwidth, lm_info.lmheight, lm_info.world_to_lm_space); + int samples_per_face = extents.numsamples() * face_used_styles; + + // round up to multiple of 4 + if (samples_per_face % 4) { + samples_per_face += (4 - (samples_per_face % 4)); + } + + int bytes_per_face = 3 * samples_per_face; + expected_dlightdata_bytes += bytes_per_face; + } + CHECK(bsp.dlightdata.size() == expected_dlightdata_bytes); +}