light: minimize includes

This commit is contained in:
Eric Wasylishen 2023-06-25 21:27:24 -06:00
parent 11d73b71d8
commit 1c85bb882a
24 changed files with 287 additions and 233 deletions

View File

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

View File

@ -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 <vector>
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.
* <http://blogs.msdn.com/b/vcblog/archive/2008/08/28/the-mallocator.aspx>
*/
template<typename T, std::size_t Alignment>
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<std::size_t>(0) - static_cast<std::size_t>(1)) / sizeof(T);
}
// The following must be the same for all allocators.
template<typename U>
struct rebind
{
typedef aligned_allocator<U, Alignment> 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<void *>(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<typename U>
aligned_allocator(const aligned_allocator<U, Alignment> &)
{
}
~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<T>::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<T *>(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<typename U>
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<typename T>
using aligned_vector = std::vector<T, aligned_allocator<T, alignof(T)>>;

View File

@ -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.
* <http://blogs.msdn.com/b/vcblog/archive/2008/08/28/the-mallocator.aspx>
*/
template<typename T, std::size_t Alignment>
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<std::size_t>(0) - static_cast<std::size_t>(1)) / sizeof(T);
}
// The following must be the same for all allocators.
template<typename U>
struct rebind
{
typedef aligned_allocator<U, Alignment> 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<void *>(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<typename U>
aligned_allocator(const aligned_allocator<U, Alignment> &)
{
}
~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<T>::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<T *>(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<typename U>
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<typename T>
using aligned_vector = std::vector<T, aligned_allocator<T, alignof(T)>>;

View File

@ -20,15 +20,11 @@
#pragma once
#include <common/cmdlib.hh>
#include <common/mathlib.hh>
#include <vector>
#include <map>
#include <common/qvec.hh>
#include <forward_list>
#include "surflight.hh"
namespace settings
{
class worldspawn_keys;
}
struct mbsp_t;
// public functions

View File

@ -19,15 +19,22 @@
#pragma once
#include <map>
#include <string>
#include <vector>
#include <common/entdata.h>
#include <common/mathlib.hh>
#include <common/bspfile.hh>
#include <common/imglib.hh>
#include <light/light.hh>
#include <common/aabb.hh>
#include <common/settings.hh>
#include <common/entdata.h> // for entdict_t
#include <light/light.hh> // for sun_t
struct mbsp_t;
struct mface_t;
struct mleaf_t;
namespace img
{
struct texture;
}
constexpr vec_t DEFAULTLIGHTLEVEL = 300.0;

View File

@ -19,29 +19,17 @@
#pragma once
#include <common/cmdlib.hh>
#include <common/mathlib.hh>
#include <common/bspfile.hh>
#include <common/log.hh>
#include <common/threads.hh>
#include <common/polylib.hh>
#include <common/imglib.hh>
#include <common/settings.hh>
#include <common/bitflags.hh>
#include <light/litfile.hh>
#include <light/trace.hh>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cassert>
#include <limits>
#include <sstream>
#include <common/bsputils.hh> // for faceextents_t
#include <common/qvec.hh>
#include <common/bsputils.hh>
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<lightmap_t>;
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<raystream_occlusion_t> occlusion_stream;
std::unique_ptr<raystream_intersection_t> intersection_stream;
lightmapdict_t lightmapsByStyle;

View File

@ -19,7 +19,7 @@
#pragma once
#include <light/ltface.hh>
#include <light/ltface.hh> // for lightgrid_samples_t
struct bspdata_t;

View File

@ -19,7 +19,13 @@
#pragma once
#include <common/bspfile.hh>
#include <array>
#include <vector>
#include <common/qvec.hh>
#include <common/fs.hh>
struct mbsp_t;
constexpr int32_t LIT_VERSION = 1;

View File

@ -19,25 +19,23 @@
#pragma once
#include <common/cmdlib.hh>
#include <common/mathlib.hh>
#include <common/bspfile.hh>
#include <common/log.hh>
#include <common/threads.hh>
#include <common/polylib.hh>
#include <common/qvec.hh>
#include <light/litfile.hh>
#include <light/trace.hh>
#include <light/entities.hh>
#include <vector>
#include <map>
#include <string>
#include <cassert>
#include <limits>
#include <sstream>
#include <atomic>
#include <memory>
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<uint32_t> total_light_rays, total_light_ray_hits, total_samplepoints;
extern std::atomic<uint32_t> total_bounce_rays, total_bounce_ray_hits;

View File

@ -20,20 +20,15 @@
#pragma once
#include <common/cmdlib.hh>
#include <common/mathlib.hh>
#include <common/bsputils.hh>
#include <common/bspfile.hh>
#include <common/log.hh>
#include <light/light.hh>
#include <set>
#include <map>
#include <vector>
#include <common/qvec.hh>
struct mbsp_t;
struct mface_t;
struct neighbour_t
{
const mface_t *face;

View File

@ -21,8 +21,20 @@ See file, 'COPYING', for details.
#pragma once
#include <vector>
#include <optional>
#include <tuple>
#include <common/qvec.hh>
#include <common/aabb.hh>
struct mleaf_t;
struct mface_t;
struct mbsp_t;
namespace settings
{
class worldspawn_keys;
}
struct surfacelight_t
{
qvec3d pos;

View File

@ -19,35 +19,20 @@
#pragma once
#include <common/cmdlib.hh>
#include <common/mathlib.hh>
#include <common/bspfile.hh>
#include <common/log.hh>
#include <common/threads.hh>
#include <common/polylib.hh>
#include <common/imglib.hh>
#include <common/qvec.hh>
#include <vector>
#include <map>
#include <string>
#include <cassert>
#include <limits>
#include <sstream>
#include <utility> // 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"

View File

@ -19,19 +19,18 @@
#pragma once
#include <common/cmdlib.hh>
#include <common/mathlib.hh>
#include <common/bspfile.hh>
#include <common/log.hh>
#include <common/threads.hh>
#include <common/polylib.hh>
#include <common/aligned_allocator.hh>
#include <common/qvec.hh>
#include <common/log.hh> // for FError
#include <vector>
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) {

View File

@ -18,14 +18,16 @@
See file, 'COPYING', for details.
*/
#include <light/bounce.hh>
#include <cstdint>
// #include <cstdio>
#include <atomic>
#include <light/light.hh>
#include <light/bounce.hh>
#include <light/entities.hh> // for EstimateVisibleBoundsAtPoint
#include <light/ltface.hh>
#include <light/surflight.hh>
#include <light/trace.hh> // for Light_PointInLeaf
#include <common/polylib.hh>
#include <common/bsputils.hh>

View File

@ -17,15 +17,20 @@
See file, 'COPYING', for details.
*/
#include <light/entities.hh>
#include <algorithm>
#include <cstring>
#include <fstream>
#include <common/imglib.hh> // for img::find
#include <common/log.hh>
#include <common/cmdlib.hh>
#include <common/parser.hh>
#include <light/litfile.hh>
#include <light/trace.hh>
#include <light/trace_embree.hh>
#include <light/light.hh>
#include <light/entities.hh>
#include <common/bsputils.hh>
#include <common/parallel.hh>

View File

@ -17,18 +17,21 @@
See file, 'COPYING', for details.
*/
#include <light/light.hh>
#include <cstdint>
#include <iostream>
#include <fmt/ostream.h>
#include <fmt/chrono.h>
#include <light/light.hh>
#include <light/lightgrid.hh>
#include <light/phong.hh>
#include <light/bounce.hh>
#include <light/surflight.hh> //mxd
#include <light/entities.hh>
#include <light/ltface.hh>
#include <light/litfile.hh> // for facesup_t
#include <light/trace_embree.hh>
#include <common/log.hh>
#include <common/bsputils.hh>

View File

@ -17,6 +17,8 @@
See file, 'COPYING', for details.
*/
#include <light/lightgrid.hh>
#include <cstdint>
#include <iostream>
#include <fstream>
@ -28,7 +30,6 @@
#include <fmt/chrono.h>
#include <light/light.hh>
#include <light/bounce.hh>
#include <light/entities.hh>
#include <light/ltface.hh>

View File

@ -17,9 +17,10 @@
See file, 'COPYING', for details.
*/
#include <light/litfile.hh>
#include <fstream>
#include <light/litfile.hh>
#include <light/light.hh>
#include <common/bspfile.hh>

View File

@ -17,15 +17,18 @@
See file, 'COPYING', for details.
*/
#include <light/ltface.hh>
#include <light/light.hh>
#include <light/trace_embree.hh>
#include <light/phong.hh>
#include <light/bounce.hh>
#include <light/surflight.hh> //mxd
#include <light/entities.hh>
#include <light/lightgrid.hh>
#include <light/trace.hh>
#include <light/ltface.hh>
#include <light/litfile.hh> // for facesup_t
#include <common/imglib.hh>
#include <common/log.hh>
#include <common/bsputils.hh>
#include <common/qvec.hh>
@ -589,6 +592,8 @@ static std::unique_ptr<lightsurf_t> Lightsurf_Init(const modelinfo_t *modelinfo,
lightsurf->modelinfo = modelinfo;
lightsurf->bsp = bsp;
lightsurf->face = face;
lightsurf->occlusion_stream = std::make_unique<raystream_occlusion_t>();
lightsurf->intersection_stream = std::make_unique<raystream_intersection_t>();
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_t> 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

View File

@ -18,11 +18,13 @@
See file, 'COPYING', for details.
*/
#include <light/phong.hh>
#include <light/light.hh> // for extended_texinfo_flags
#include <cstdint>
#include <iostream>
#include <light/phong.hh>
#include <common/polylib.hh>
#include <common/bsputils.hh>

View File

@ -18,10 +18,13 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
See file, 'COPYING', for details.
*/
#include <light/surflight.hh>
#include <cassert>
#include <light/entities.hh> // for FixLightOnFace
#include <light/trace.hh> // for Light_PointInLeaf
#include <light/light.hh>
#include <light/surflight.hh>
#include <light/ltface.hh>
#include <common/polylib.hh>

View File

@ -18,6 +18,8 @@
*/
#include <light/trace.hh>
#include <common/imglib.hh>
#include <common/bsputils.hh>
/*

View File

@ -17,9 +17,11 @@
See file, 'COPYING', for details.
*/
#include <light/light.hh>
#include <light/bounce.hh>
#include <light/trace_embree.hh>
#include <light/light.hh>
#include <light/trace.hh> // for SampleTexture
#include <common/bsputils.hh>
#include <common/polylib.hh>
#include <vector>

View File

@ -1,6 +1,7 @@
#include <doctest/doctest.h>
#include <light/light.hh>
#include <light/trace.hh> // for clamp_texcoord
#include <light/entities.hh>
#include <random>