From 1c85bb882afad0e219d54808aa72ee99c4d5c0cd Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 25 Jun 2023 21:27:24 -0600 Subject: [PATCH] light: minimize includes --- common/CMakeLists.txt | 2 + include/common/aligned_allocator.hh | 141 ++++++++++++++++++++++++++++ include/common/cmdlib.hh | 119 ----------------------- include/light/bounce.hh | 14 +-- include/light/entities.hh | 19 ++-- include/light/light.hh | 34 +++---- include/light/lightgrid.hh | 2 +- include/light/litfile.hh | 8 +- include/light/ltface.hh | 30 +++--- include/light/phong.hh | 11 +-- include/light/surflight.hh | 12 +++ include/light/trace.hh | 31 ++---- include/light/trace_embree.hh | 30 ++++-- light/bounce.cc | 6 +- light/entities.cc | 7 +- light/light.cc | 5 +- light/lightgrid.cc | 3 +- light/litfile.cc | 3 +- light/ltface.cc | 23 +++-- light/phong.cc | 6 +- light/surflight.cc | 5 +- light/trace.cc | 2 + light/trace_embree.cc | 6 +- tests/test_light.cc | 1 + 24 files changed, 287 insertions(+), 233 deletions(-) create mode 100644 include/common/aligned_allocator.hh diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 7e66d13a..3bf2fa1a 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -20,6 +20,7 @@ add_library(common STATIC prtfile.cc debugger.natvis ../include/common/aabb.hh + ../include/common/aligned_allocator.hh ../include/common/bitflags.hh ../include/common/bspinfo.hh ../include/common/bspfile.hh @@ -34,6 +35,7 @@ add_library(common STATIC ../include/common/iterators.hh ../include/common/log.hh ../include/common/mathlib.hh + ../include/common/numeric_cast.hh ../include/common/parser.hh ../include/common/polylib.hh ../include/common/qvec.hh diff --git a/include/common/aligned_allocator.hh b/include/common/aligned_allocator.hh new file mode 100644 index 00000000..bec21635 --- /dev/null +++ b/include/common/aligned_allocator.hh @@ -0,0 +1,141 @@ +/* Copyright (C) 1996-1997 Id Software, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + See file, 'COPYING', for details. +*/ + +#pragma once + +#include + +void *q_aligned_malloc(size_t align, size_t size); +void q_aligned_free(void *ptr); + +/** + * Allocator for aligned data. + * + * Modified from the Mallocator from Stephan T. Lavavej. + * + */ +template +class aligned_allocator +{ +public: + // The following will be the same for virtually all allocators. + using pointer = T *; + using const_pointer = const T *; + using reference = T &; + using const_reference = const T &; + using value_type = T; + using size_type = std::size_t; + using difference_type = ptrdiff_t; + + T *address(T &r) const { return &r; } + + const T *address(const T &s) const { return &s; } + + std::size_t max_size() const + { + // The following has been carefully written to be independent of + // the definition of size_t and to avoid signed/unsigned warnings. + return (static_cast(0) - static_cast(1)) / sizeof(T); + } + + // The following must be the same for all allocators. + template + struct rebind + { + typedef aligned_allocator other; + }; + + bool operator!=(const aligned_allocator &other) const { return !(*this == other); } + + void construct(T *const p, const T &t) const + { + void *const pv = reinterpret_cast(p); + new (pv) T(t); + } + + void destroy(T *const p) const { p->~T(); } + + // Returns true if and only if storage allocated from *this + // can be deallocated from other, and vice versa. + // Always returns true for stateless allocators. + bool operator==(const aligned_allocator &other) const { return true; } + + // Default constructor, copy constructor, rebinding constructor, and destructor. + // Empty for stateless allocators. + aligned_allocator() { } + aligned_allocator(const aligned_allocator &) { } + template + aligned_allocator(const aligned_allocator &) + { + } + ~aligned_allocator() { } + + // The following will be different for each allocator. + T *allocate(const std::size_t n) const + { + // The return value of allocate(0) is unspecified. + // Mallocator returns NULL in order to avoid depending + // on malloc(0)'s implementation-defined behavior + // (the implementation can define malloc(0) to return NULL, + // in which case the bad_alloc check below would fire). + // All allocators can return NULL in this case. + if (n == 0) { + return nullptr; + } + + // All allocators should contain an integer overflow check. + // The Standardization Committee recommends that std::length_error + // be thrown in the case of integer overflow. + if (n > max_size()) { + throw std::length_error("aligned_allocator::allocate() - Integer overflow."); + } + + // Mallocator wraps malloc(). + void *const pv = q_aligned_malloc(Alignment, n * sizeof(T)); + + // Allocators should throw std::bad_alloc in the case of memory allocation failure. + if (pv == nullptr) { + throw std::bad_alloc(); + } + + return reinterpret_cast(pv); + } + + void deallocate(T *const p, const std::size_t n) const { q_aligned_free(p); } + + // The following will be the same for all allocators that ignore hints. + template + T *allocate(const std::size_t n, const U * /* const hint */) const + { + return allocate(n); + } + + // Allocators are not required to be assignable, so + // all allocators should have a private unimplemented + // assignment operator. Note that this will trigger the + // off-by-default (enabled under /Wall) warning C4626 + // "assignment operator could not be generated because a + // base class assignment operator is inaccessible" within + // the STL headers, but that warning is useless. +private: + aligned_allocator &operator=(const aligned_allocator &); +}; + +template +using aligned_vector = std::vector>; diff --git a/include/common/cmdlib.hh b/include/common/cmdlib.hh index 75569d5d..4424ffd6 100644 --- a/include/common/cmdlib.hh +++ b/include/common/cmdlib.hh @@ -569,122 +569,3 @@ struct omemsizestream : virtual omemsizebuf, std::ostream void CRC_Init(uint16_t &crcvalue); void CRC_ProcessByte(uint16_t &crcvalue, uint8_t data); uint16_t CRC_Block(const uint8_t *start, int count); - -void *q_aligned_malloc(size_t align, size_t size); -void q_aligned_free(void *ptr); - -/** - * Allocator for aligned data. - * - * Modified from the Mallocator from Stephan T. Lavavej. - * - */ -template -class aligned_allocator -{ -public: - // The following will be the same for virtually all allocators. - using pointer = T *; - using const_pointer = const T *; - using reference = T &; - using const_reference = const T &; - using value_type = T; - using size_type = std::size_t; - using difference_type = ptrdiff_t; - - T *address(T &r) const { return &r; } - - const T *address(const T &s) const { return &s; } - - std::size_t max_size() const - { - // The following has been carefully written to be independent of - // the definition of size_t and to avoid signed/unsigned warnings. - return (static_cast(0) - static_cast(1)) / sizeof(T); - } - - // The following must be the same for all allocators. - template - struct rebind - { - typedef aligned_allocator other; - }; - - bool operator!=(const aligned_allocator &other) const { return !(*this == other); } - - void construct(T *const p, const T &t) const - { - void *const pv = reinterpret_cast(p); - new (pv) T(t); - } - - void destroy(T *const p) const { p->~T(); } - - // Returns true if and only if storage allocated from *this - // can be deallocated from other, and vice versa. - // Always returns true for stateless allocators. - bool operator==(const aligned_allocator &other) const { return true; } - - // Default constructor, copy constructor, rebinding constructor, and destructor. - // Empty for stateless allocators. - aligned_allocator() { } - aligned_allocator(const aligned_allocator &) { } - template - aligned_allocator(const aligned_allocator &) - { - } - ~aligned_allocator() { } - - // The following will be different for each allocator. - T *allocate(const std::size_t n) const - { - // The return value of allocate(0) is unspecified. - // Mallocator returns NULL in order to avoid depending - // on malloc(0)'s implementation-defined behavior - // (the implementation can define malloc(0) to return NULL, - // in which case the bad_alloc check below would fire). - // All allocators can return NULL in this case. - if (n == 0) { - return nullptr; - } - - // All allocators should contain an integer overflow check. - // The Standardization Committee recommends that std::length_error - // be thrown in the case of integer overflow. - if (n > max_size()) { - throw std::length_error("aligned_allocator::allocate() - Integer overflow."); - } - - // Mallocator wraps malloc(). - void *const pv = q_aligned_malloc(Alignment, n * sizeof(T)); - - // Allocators should throw std::bad_alloc in the case of memory allocation failure. - if (pv == nullptr) { - throw std::bad_alloc(); - } - - return reinterpret_cast(pv); - } - - void deallocate(T *const p, const std::size_t n) const { q_aligned_free(p); } - - // The following will be the same for all allocators that ignore hints. - template - T *allocate(const std::size_t n, const U * /* const hint */) const - { - return allocate(n); - } - - // Allocators are not required to be assignable, so - // all allocators should have a private unimplemented - // assignment operator. Note that this will trigger the - // off-by-default (enabled under /Wall) warning C4626 - // "assignment operator could not be generated because a - // base class assignment operator is inaccessible" within - // the STL headers, but that warning is useless. -private: - aligned_allocator &operator=(const aligned_allocator &); -}; - -template -using aligned_vector = std::vector>; \ No newline at end of file diff --git a/include/light/bounce.hh b/include/light/bounce.hh index 6832b508..deba5c9d 100644 --- a/include/light/bounce.hh +++ b/include/light/bounce.hh @@ -20,15 +20,11 @@ #pragma once -#include -#include - -#include -#include - -#include -#include -#include "surflight.hh" +namespace settings +{ +class worldspawn_keys; +} +struct mbsp_t; // public functions diff --git a/include/light/entities.hh b/include/light/entities.hh index cbc65f69..1ef1ab9e 100644 --- a/include/light/entities.hh +++ b/include/light/entities.hh @@ -19,15 +19,22 @@ #pragma once -#include #include #include -#include -#include -#include -#include -#include +#include +#include +#include // for entdict_t +#include // for sun_t + +struct mbsp_t; +struct mface_t; +struct mleaf_t; + +namespace img +{ +struct texture; +} constexpr vec_t DEFAULTLIGHTLEVEL = 300.0; diff --git a/include/light/light.hh b/include/light/light.hh index 87f7af0f..f5f13905 100644 --- a/include/light/light.hh +++ b/include/light/light.hh @@ -19,29 +19,17 @@ #pragma once -#include -#include -#include -#include -#include -#include -#include #include -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include +#include // for faceextents_t #include -#include + +namespace img +{ +struct texture; +} +struct mbsp_t; +struct mface_t; constexpr vec_t LIGHT_ON_EPSILON = 0.1; constexpr vec_t LIGHT_ANGLE_EPSILON = 0.01; @@ -95,6 +83,8 @@ public: using lightmapdict_t = std::vector; struct surfacelight_t; +class raystream_occlusion_t; +class raystream_intersection_t; struct lightsurf_t { @@ -149,8 +139,8 @@ struct lightsurf_t int height; // ray batch stuff - raystream_occlusion_t occlusion_stream; - raystream_intersection_t intersection_stream; + std::unique_ptr occlusion_stream; + std::unique_ptr intersection_stream; lightmapdict_t lightmapsByStyle; diff --git a/include/light/lightgrid.hh b/include/light/lightgrid.hh index dbc92f01..6405f0e2 100644 --- a/include/light/lightgrid.hh +++ b/include/light/lightgrid.hh @@ -19,7 +19,7 @@ #pragma once -#include +#include // for lightgrid_samples_t struct bspdata_t; diff --git a/include/light/litfile.hh b/include/light/litfile.hh index e66408c7..5a104728 100644 --- a/include/light/litfile.hh +++ b/include/light/litfile.hh @@ -19,7 +19,13 @@ #pragma once -#include +#include +#include + +#include +#include + +struct mbsp_t; constexpr int32_t LIT_VERSION = 1; diff --git a/include/light/ltface.hh b/include/light/ltface.hh index 2623f235..1438e6d1 100644 --- a/include/light/ltface.hh +++ b/include/light/ltface.hh @@ -19,25 +19,23 @@ #pragma once -#include -#include -#include -#include -#include -#include #include -#include -#include -#include - -#include -#include -#include -#include -#include -#include #include +#include + +struct mface_t; +struct mbsp_t; + +namespace settings +{ +class worldspawn_keys; +} +struct lightsurf_t; +struct bspx_decoupled_lm_perface; +class faceextents_t; +class light_t; +struct facesup_t; extern std::atomic total_light_rays, total_light_ray_hits, total_samplepoints; extern std::atomic total_bounce_rays, total_bounce_ray_hits; diff --git a/include/light/phong.hh b/include/light/phong.hh index d2fbb674..76258ce9 100644 --- a/include/light/phong.hh +++ b/include/light/phong.hh @@ -20,20 +20,15 @@ #pragma once -#include -#include -#include -#include -#include - -#include - #include #include #include #include +struct mbsp_t; +struct mface_t; + struct neighbour_t { const mface_t *face; diff --git a/include/light/surflight.hh b/include/light/surflight.hh index 6599c400..d104ac5f 100644 --- a/include/light/surflight.hh +++ b/include/light/surflight.hh @@ -21,8 +21,20 @@ See file, 'COPYING', for details. #pragma once #include +#include #include +#include +#include + +struct mleaf_t; +struct mface_t; +struct mbsp_t; +namespace settings +{ +class worldspawn_keys; +} + struct surfacelight_t { qvec3d pos; diff --git a/include/light/trace.hh b/include/light/trace.hh index b7cd01ce..ae04a88c 100644 --- a/include/light/trace.hh +++ b/include/light/trace.hh @@ -19,35 +19,20 @@ #pragma once -#include -#include -#include -#include -#include -#include -#include +#include -#include -#include -#include -#include -#include -#include -#include // for std::pair - -enum class hittype_t : uint8_t +namespace img { - NONE = 0, - SOLID = 1, - SKY = 2 -}; +struct texture; +} +struct mtexinfo_t; +struct mface_t; +struct mbsp_t; uint32_t clamp_texcoord(vec_t in, uint32_t width); qvec4b SampleTexture(const mface_t *face, const mtexinfo_t *tex, const img::texture *texture, const mbsp_t *bsp, const qvec3d &point); // mxd. Palette index -> RGBA class modelinfo_t; - +struct mleaf_t; const mleaf_t *Light_PointInLeaf(const mbsp_t *bsp, const qvec3d &point); - -#include "trace_embree.hh" \ No newline at end of file diff --git a/include/light/trace_embree.hh b/include/light/trace_embree.hh index 65e557b7..69333d97 100644 --- a/include/light/trace_embree.hh +++ b/include/light/trace_embree.hh @@ -19,19 +19,18 @@ #pragma once -#include -#include -#include -#include -#include -#include +#include +#include +#include // for FError + #include +struct mbsp_t; +class modelinfo_t; + void ResetEmbree(); void Embree_TraceInit(const mbsp_t *bsp); -class modelinfo_t; - class raystream_embree_common_t { public: @@ -106,6 +105,14 @@ public: extern RTCScene scene; +class light_t; +struct mface_t; +struct mtexinfo_t; +namespace img +{ +struct texture; +} + inline RTCRayHit SetupRay(unsigned rayindex, const qvec3d &start, const qvec3d &dir, vec_t dist) { RTCRayHit ray; @@ -171,6 +178,13 @@ extern sceneinfo skygeom; // sky. always occludes. extern sceneinfo solidgeom; // solids. always occludes. extern sceneinfo filtergeom; // conditional occluders.. needs to run ray intersection filter +enum class hittype_t : uint8_t +{ + NONE = 0, + SOLID = 1, + SKY = 2 +}; + inline const sceneinfo &Embree_SceneinfoForGeomID(unsigned int geomID) { if (geomID == skygeom.geomID) { diff --git a/light/bounce.cc b/light/bounce.cc index cea587c7..6aecbc26 100644 --- a/light/bounce.cc +++ b/light/bounce.cc @@ -18,14 +18,16 @@ See file, 'COPYING', for details. */ +#include + #include -// #include #include #include -#include +#include // for EstimateVisibleBoundsAtPoint #include #include +#include // for Light_PointInLeaf #include #include diff --git a/light/entities.cc b/light/entities.cc index 8a764c7f..1a52504b 100644 --- a/light/entities.cc +++ b/light/entities.cc @@ -17,15 +17,20 @@ See file, 'COPYING', for details. */ +#include + #include #include #include +#include // for img::find #include #include #include +#include +#include +#include #include -#include #include #include diff --git a/light/light.cc b/light/light.cc index 7f506864..f53f510f 100644 --- a/light/light.cc +++ b/light/light.cc @@ -17,18 +17,21 @@ See file, 'COPYING', for details. */ +#include + #include #include #include #include -#include #include #include #include #include //mxd #include #include +#include // for facesup_t +#include #include #include diff --git a/light/lightgrid.cc b/light/lightgrid.cc index 3f715d94..d81d1adb 100644 --- a/light/lightgrid.cc +++ b/light/lightgrid.cc @@ -17,6 +17,8 @@ See file, 'COPYING', for details. */ +#include + #include #include #include @@ -28,7 +30,6 @@ #include #include -#include #include #include diff --git a/light/litfile.cc b/light/litfile.cc index 7a7a7d1e..5f080599 100644 --- a/light/litfile.cc +++ b/light/litfile.cc @@ -17,9 +17,10 @@ See file, 'COPYING', for details. */ +#include + #include -#include #include #include diff --git a/light/ltface.cc b/light/ltface.cc index 36b94aa8..1556b1de 100644 --- a/light/ltface.cc +++ b/light/ltface.cc @@ -17,15 +17,18 @@ See file, 'COPYING', for details. */ +#include + #include +#include #include -#include #include //mxd #include #include #include -#include +#include // for facesup_t +#include #include #include #include @@ -589,6 +592,8 @@ static std::unique_ptr Lightsurf_Init(const modelinfo_t *modelinfo, lightsurf->modelinfo = modelinfo; lightsurf->bsp = bsp; lightsurf->face = face; + lightsurf->occlusion_stream = std::make_unique(); + lightsurf->intersection_stream = std::make_unique(); if (Face_IsLightmapped(bsp, face)) { /* if liquid doesn't have the TEX_SPECIAL flag set, the map was qbsp'ed with @@ -730,8 +735,8 @@ static std::unique_ptr Lightsurf_Init(const modelinfo_t *modelinfo, lightsurf->extents.origin += modelinfo->offset; lightsurf->extents.bounds = lightsurf->extents.bounds.translate(modelinfo->offset); - lightsurf->intersection_stream.resize(lightsurf->samples.size()); - lightsurf->occlusion_stream.resize(lightsurf->samples.size()); + lightsurf->intersection_stream->resize(lightsurf->samples.size()); + lightsurf->occlusion_stream->resize(lightsurf->samples.size()); /* Setup vis data */ CalcPvs(bsp, lightsurf.get()); @@ -1225,7 +1230,7 @@ static void LightFace_Entity( /* * Check it for real */ - raystream_occlusion_t &rs = lightsurf->occlusion_stream; + raystream_occlusion_t &rs = *lightsurf->occlusion_stream; rs.clearPushedRays(); for (int i = 0; i < lightsurf->samples.size(); i++) { @@ -1389,7 +1394,7 @@ static void LightFace_Sky(const mbsp_t *bsp, const sun_t *sun, lightsurf_t *ligh } /* Check each point... */ - raystream_intersection_t &rs = lightsurf->intersection_stream; + raystream_intersection_t &rs = *lightsurf->intersection_stream; rs.clearPushedRays(); for (int i = 0; i < lightsurf->samples.size(); i++) { @@ -1710,7 +1715,7 @@ static void LightFace_LocalMin( continue; } - raystream_occlusion_t &rs = lightsurf->occlusion_stream; + raystream_occlusion_t &rs = *lightsurf->occlusion_stream; rs.clearPushedRays(); lightmap_t *lightmap = Lightmap_ForStyle(lightmaps, entity->style.value(), lightsurf); @@ -2001,7 +2006,7 @@ LightFace_SurfaceLight(const mbsp_t *bsp, lightsurf_t *lightsurf, lightmapdict_t else if (SurfaceLight_SphereCull(&vpl, lightsurf, vpl_setting, surflight_gate, hotspot_clamp)) continue; - raystream_occlusion_t &rs = lightsurf->occlusion_stream; + raystream_occlusion_t &rs = *lightsurf->occlusion_stream; for (int c = 0; c < vpl.points.size(); c++) { if (light_options.visapprox.value() == visapprox_t::VIS && @@ -2353,7 +2358,7 @@ static void LightFace_CalculateDirt(lightsurf_t *lightsurf) } for (int j = 0; j < numDirtVectors; j++) { - raystream_intersection_t &rs = lightsurf->intersection_stream; + raystream_intersection_t &rs = *lightsurf->intersection_stream; rs.clearPushedRays(); // fill in input buffers diff --git a/light/phong.cc b/light/phong.cc index 6da1dbe9..ba028bcc 100644 --- a/light/phong.cc +++ b/light/phong.cc @@ -18,11 +18,13 @@ See file, 'COPYING', for details. */ +#include + +#include // for extended_texinfo_flags + #include #include -#include - #include #include diff --git a/light/surflight.cc b/light/surflight.cc index 44d06ce2..65a19b64 100644 --- a/light/surflight.cc +++ b/light/surflight.cc @@ -18,10 +18,13 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA See file, 'COPYING', for details. */ +#include + #include +#include // for FixLightOnFace +#include // for Light_PointInLeaf #include -#include #include #include diff --git a/light/trace.cc b/light/trace.cc index c39d08e9..ff9c1437 100644 --- a/light/trace.cc +++ b/light/trace.cc @@ -18,6 +18,8 @@ */ #include + +#include #include /* diff --git a/light/trace_embree.cc b/light/trace_embree.cc index 1fe15d99..d0b10b5c 100644 --- a/light/trace_embree.cc +++ b/light/trace_embree.cc @@ -17,9 +17,11 @@ See file, 'COPYING', for details. */ -#include -#include #include + +#include +#include // for SampleTexture + #include #include #include diff --git a/tests/test_light.cc b/tests/test_light.cc index 685a36b5..b0d4a8db 100644 --- a/tests/test_light.cc +++ b/tests/test_light.cc @@ -1,6 +1,7 @@ #include #include +#include // for clamp_texcoord #include #include