Skip to content

Commit

Permalink
refactor(rendering): move shadows code to a single folder
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas7770 committed Nov 25, 2024
1 parent d6b2085 commit 098d1a1
Show file tree
Hide file tree
Showing 35 changed files with 553 additions and 498 deletions.
5 changes: 5 additions & 0 deletions core/include/cubos/core/geom/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ namespace cubos::core::geom
/// @param corners Output vector where the corners will be stored.
CUBOS_CORE_API void getCameraFrustumCorners(const glm::mat4& view, const glm::mat4& proj, float zNear, float zFar,
std::vector<glm::vec4>& corners);

/// @brief Gets view matrices for rendering a cubemap.
/// @param inverseView Matrix that transforms the camera's view space to world space.
/// @param cubeViewMatrices Output vector where the view matrices will be stored.
CUBOS_CORE_API void getCubeViewMatrices(const glm::mat4& inverseView, std::vector<glm::mat4>& cubeViewMatrices);
} // namespace cubos::core::geom
14 changes: 14 additions & 0 deletions core/src/geom/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <limits>

#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/mat3x3.hpp>

#include <cubos/core/geom/utils.hpp>
Expand Down Expand Up @@ -191,3 +192,16 @@ void cubos::core::geom::getCameraFrustumCorners(const glm::mat4& view, const glm
}
}
}

void cubos::core::geom::getCubeViewMatrices(const glm::mat4& inverseView, std::vector<glm::mat4>& cubeViewMatrices)

Check warning on line 196 in core/src/geom/utils.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/geom/utils.cpp#L196

Added line #L196 was not covered by tests
{
static const float ViewAngle[6] = {0.0F, 180.0F, 90.0F, -90.0F, 90.0F, -90.0F};
static const glm::vec3 ViewAxis[6] = {glm::vec3(0.0F, 1.0F, 0.0F), glm::vec3(0.0F, 1.0F, 0.0F),
glm::vec3(0.0F, 1.0F, 0.0F), glm::vec3(0.0F, 1.0F, 0.0F),
glm::vec3(1.0F, 0.0F, 0.0F), glm::vec3(1.0F, 0.0, 0.0F)};

for (int i = 0; i < 6; i++)
{
cubeViewMatrices.push_back(glm::inverse(glm::rotate(inverseView, glm::radians(ViewAngle[i]), ViewAxis[i])));

Check warning on line 205 in core/src/geom/utils.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/geom/utils.cpp#L205

Added line #L205 was not covered by tests
}
}

Check warning on line 207 in core/src/geom/utils.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/geom/utils.cpp#L207

Added line #L207 was not covered by tests
23 changes: 12 additions & 11 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,18 @@ set(CUBOS_ENGINE_SOURCE
"src/render/split_screen/split_screen.cpp"
"src/render/bloom/plugin.cpp"
"src/render/bloom/bloom.cpp"
"src/render/shadows/plugin.cpp"
"src/render/shadows/caster.cpp"
"src/render/shadows/spot_caster.cpp"
"src/render/shadows/directional_caster.cpp"
"src/render/shadows/point_caster.cpp"
"src/render/shadow_atlas/plugin.cpp"
"src/render/shadow_atlas/shadow_atlas.cpp"
"src/render/shadow_atlas_rasterizer/plugin.cpp"
"src/render/shadow_atlas_rasterizer/shadow_atlas_rasterizer.cpp"
"src/render/cascaded_shadow_maps/plugin.cpp"
"src/render/cascaded_shadow_maps_rasterizer/plugin.cpp"
"src/render/shadows/casters/plugin.cpp"
"src/render/shadows/casters/caster.cpp"
"src/render/shadows/casters/spot_caster.cpp"
"src/render/shadows/casters/directional_caster.cpp"
"src/render/shadows/casters/point_caster.cpp"
"src/render/shadows/atlas/plugin.cpp"
"src/render/shadows/atlas/spot_atlas.cpp"
"src/render/shadows/atlas/point_atlas.cpp"
"src/render/shadows/atlas_rasterizer/plugin.cpp"
"src/render/shadows/atlas_rasterizer/atlas_rasterizer.cpp"
"src/render/shadows/cascaded/plugin.cpp"
"src/render/shadows/cascaded_rasterizer/plugin.cpp"

"src/tools/settings_inspector/plugin.cpp"
"src/tools/selection/plugin.cpp"
Expand Down
97 changes: 0 additions & 97 deletions engine/include/cubos/engine/render/shadow_atlas/shadow_atlas.hpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@
namespace cubos::engine
{
/// @defgroup render-shadow-atlas-plugin Shadow atlas
/// @ingroup render-plugins
/// @brief Creates and manages a shadow map atlas.
/// @ingroup render-shadows-plugins
/// @brief Creates and manages shadow map atlases.
///
/// ## Dependencies
/// - @ref render-shadows-plugin
/// - @ref render-shadow-casters-plugin
/// - @ref window-plugin

/// @brief Creates the shadow atlas.
/// @brief Creates the shadow atlases.
/// @ingroup render-shadow-atlas-plugin
CUBOS_ENGINE_API extern Tag createShadowAtlasTag;

/// @brief Reserves space for shadow casters.
/// @ingroup render-shadow-atlas-plugin
CUBOS_ENGINE_API extern Tag reserveShadowCastersTag;

/// @brief Systems which draw to the shadow atlas texture should be tagged with this.
/// @brief Systems which draw to the shadow atlas textures should be tagged with this.
/// @ingroup render-shadow-atlas-plugin
CUBOS_ENGINE_API extern Tag drawToShadowAtlasTag;

Expand Down
60 changes: 60 additions & 0 deletions engine/include/cubos/engine/render/shadows/atlas/point_atlas.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/// @file
/// @brief Resource @ref cubos::engine::PointShadowAtlas.
/// @ingroup render-shadow-atlas-plugin

#pragma once

#include <map>

#include <glm/vec2.hpp>

#include <cubos/core/gl/render_device.hpp>
#include <cubos/core/reflection/reflect.hpp>

#include <cubos/engine/api.hpp>
#include <cubos/engine/render/shadows/atlas/slot.hpp>

namespace cubos::engine
{
/// @brief Resource which stores the shadow map atlas for point lights,
/// a large texture that holds the shadow maps for each shadow caster
/// in a quadtree structure, reducing texture switching.
/// @ingroup render-shadow-atlas-plugin
class CUBOS_ENGINE_API PointShadowAtlas
{
public:
CUBOS_REFLECT;

/// @brief Gets the size of the shadow atlas texture.
/// @return Size of the shadow atlas texture, in pixels.
glm::uvec2 getSize() const;

/// @brief Recreates the shadow atlas texture.
/// @param rd Render device used to create the texture.
void resize(cubos::core::gl::RenderDevice& rd);

/// @brief Configured size of the shadow atlas texture, in pixels.
/// Use this to change the resolution of the atlas. Note that the
/// texture isn't immediately resized; use @ref getSize() to get the
/// actual texture size.
glm::uvec2 configSize = {1024, 1024};

/// @brief Whether the shadow atlas texture has already been cleared this frame.
bool cleared = false;

/// @brief Stores shadow maps for each point shadow caster component.
/// Each texture of the array corresponds to a face of a cubemap.
core::gl::Texture2DArray atlas{nullptr};

/// @brief Stores the sizes, offsets, and caster ids of the shadow maps
/// in the atlas.
std::vector<std::shared_ptr<cubos::engine::ShadowMapSlot>> slots;

/// @brief Maps shadow caster ids to their corresponding slots.
std::map<int, std::shared_ptr<cubos::engine::ShadowMapSlot>> slotsMap;

private:
/// @brief Size of the shadow atlas texture, in pixels.
glm::uvec2 mSize = {0, 0};
};
} // namespace cubos::engine
29 changes: 29 additions & 0 deletions engine/include/cubos/engine/render/shadows/atlas/slot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// @file
/// @brief Resource @ref cubos::engine::ShadowMapSlot.
/// @ingroup render-shadow-atlas-plugin

#pragma once

#include <glm/vec2.hpp>

#include <cubos/engine/api.hpp>

namespace cubos::engine
{
/// @brief Slot for a shadow map in the shadow atlas.
struct ShadowMapSlot
{
glm::vec2 size; ///< Shadow map size, in normalized coordinates.
glm::vec2 offset; ///< Shadow map offset, in normalized coordinates.
int casterId; ///< Id of the shadow caster (-1 if none).

/// @brief Constructs.
/// @param size Shadow map size, in normalized coordinates.
/// @param offset Shadow map offset, in normalized coordinates.
/// @param casterId Id of the shadow caster (-1 if none).
ShadowMapSlot(glm::vec2 size, glm::vec2 offset, int casterId)
: size(size)
, offset(offset)
, casterId(casterId) {};

Check warning on line 27 in engine/include/cubos/engine/render/shadows/atlas/slot.hpp

View check run for this annotation

Codecov / codecov/patch

engine/include/cubos/engine/render/shadows/atlas/slot.hpp#L24-L27

Added lines #L24 - L27 were not covered by tests
};
} // namespace cubos::engine
59 changes: 59 additions & 0 deletions engine/include/cubos/engine/render/shadows/atlas/spot_atlas.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/// @file
/// @brief Resource @ref cubos::engine::SpotShadowAtlas.
/// @ingroup render-shadow-atlas-plugin

#pragma once

#include <map>

#include <glm/vec2.hpp>

#include <cubos/core/gl/render_device.hpp>
#include <cubos/core/reflection/reflect.hpp>

#include <cubos/engine/api.hpp>
#include <cubos/engine/render/shadows/atlas/slot.hpp>

namespace cubos::engine
{
/// @brief Resource which stores the shadow map atlas for spot lights,
/// a large texture that holds the shadow maps for each shadow caster
/// in a quadtree structure, reducing texture switching.
/// @ingroup render-shadow-atlas-plugin
class CUBOS_ENGINE_API SpotShadowAtlas
{
public:
CUBOS_REFLECT;

/// @brief Gets the size of the shadow atlas texture.
/// @return Size of the shadow atlas texture, in pixels.
glm::uvec2 getSize() const;

/// @brief Recreates the shadow atlas texture.
/// @param rd Render device used to create the texture.
void resize(cubos::core::gl::RenderDevice& rd);

/// @brief Configured size of the shadow atlas texture, in pixels.
/// Use this to change the resolution of the atlas. Note that the
/// texture isn't immediately resized; use @ref getSize() to get the
/// actual texture size.
glm::uvec2 configSize = {4096, 4096};

/// @brief Whether the shadow atlas texture has already been cleared this frame.
bool cleared = false;

/// @brief Stores shadow maps for each spot shadow caster component.
core::gl::Texture2D atlas{nullptr};

/// @brief Stores the sizes, offsets, and caster ids of the shadow maps
/// in the atlas.
std::vector<std::shared_ptr<cubos::engine::ShadowMapSlot>> slots;

/// @brief Maps shadow caster ids to their corresponding slots.
std::map<int, std::shared_ptr<cubos::engine::ShadowMapSlot>> slotsMap;

private:
/// @brief Size of the shadow atlas texture, in pixels.
glm::uvec2 mSize = {0, 0};
};
} // namespace cubos::engine
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ namespace cubos::engine
/// @brief Framebuffer used by the rasterizer to render to the point shadow atlas.
core::gl::Framebuffer pointAtlasFramebuffer{nullptr};

/// @brief Atlas texture stored to check if the framebuffers need to be recreated.
core::gl::Texture2D atlas{nullptr};
/// @brief Atlas texture stored to check if the spot atlas framebuffer needs to be recreated.
core::gl::Texture2D spotAtlas{nullptr};

/// @brief Atlas texture stored to check if the point atlas framebuffer needs to be recreated.
core::gl::Texture2DArray pointAtlas{nullptr};
};
} // namespace cubos::engine
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace cubos::engine
{
/// @defgroup render-shadow-atlas-rasterizer-plugin Shadow atlas rasterizer
/// @ingroup render-plugins
/// @ingroup render-shadows-plugins
/// @brief Draws all render meshes for each light to the shadow map atlas.
///
/// ## Dependencies
Expand All @@ -22,7 +22,7 @@ namespace cubos::engine
/// - @ref transform-plugin
/// - @ref lights-plugin
/// - @ref render-shadow-atlas-plugin
/// - @ref render-shadows-plugin
/// - @ref render-shadow-casters-plugin
/// - @ref render-mesh-plugin
/// - @ref render-voxels-plugin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
namespace cubos::engine
{
/// @defgroup render-cascaded-shadow-maps-plugin Cascaded shadow maps
/// @ingroup render-plugins
/// @ingroup render-shadows-plugins
/// @brief Creates and manages shadow maps for directional shadow casters.
///
/// ## Dependencies
/// - @ref render-camera-plugin
/// - @ref render-shadows-plugin
/// - @ref render-shadow-casters-plugin
/// - @ref window-plugin

/// @brief Creates the shadow maps.
Expand Down
Loading

0 comments on commit 098d1a1

Please sign in to comment.