Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
LightPool.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_LIGHT_POOL_H_
34#define DVD_LIGHT_POOL_H_
35
36#include "config.h"
37
38#include "Light.h"
39
43
44namespace Divide {
45
46namespace Time {
47 class ProfileTimer;
48};
49
50class Frustum;
51class ShaderProgram;
52class SceneGraphNode;
53class SceneRenderState;
54
55FWD_DECLARE_MANAGED_CLASS(ShaderBuffer);
56
57class LightPool final : public FrameListener,
58 public SceneComponent,
60 protected:
67 vec4<F32> _position = { 0.0f, 0.0f, 0.0f, 0.0f };
70 vec4<F32> _direction = { 0.0f, 0.0f, 0.0f, 45.0f };
75 vec4<I32> _options = { 3, -1, 0, 0 };
76 };
77
78#pragma pack(push, 1)
80 {
83 };
85 {
89 };
91 {
95 };
97 std::array<PointShadowProperties, Config::Lighting::MAX_SHADOW_CASTING_POINT_LIGHTS> _pointLights{};
98 std::array<SpotShadowProperties, Config::Lighting::MAX_SHADOW_CASTING_SPOT_LIGHTS> _spotLights{};
99 std::array<CSMShadowProperties, Config::Lighting::MAX_SHADOW_CASTING_DIRECTIONAL_LIGHTS> _dirLights{};
100
101 [[nodiscard]] bufferPtr data() const noexcept { return (bufferPtr)_pointLights.data(); }
102 };
103#pragma pack(pop)
104
105 public:
107 {
108 std::array<Light*, Config::Lighting::MAX_SHADOW_CASTING_LIGHTS> _entries{};
110 };
113 bool _staticSource = false;
114 };
115 using LightList = eastl::fixed_vector<Light*, 32u, true>;
116
117 static void InitStaticData( PlatformContext& context );
118 static void DestroyStaticData();
119
121 ~LightPool() override;
122
124 [[nodiscard]] bool addLight(Light& light);
126 [[nodiscard]] bool removeLight(const Light& light);
128 void toggleLightType(const LightType type) noexcept { toggleLightType(type, !lightTypeEnabled(type)); }
129 void toggleLightType(const LightType type, const bool state) noexcept { _lightTypeState[to_U32(type)] = state; }
130
131 [[nodiscard]] bool lightTypeEnabled(const LightType type) const noexcept { return _lightTypeState[to_U32(type)]; }
133 [[nodiscard]] U32 getActiveLightCount(const RenderStage stage, const LightType type) const noexcept { return _activeLightCount[to_base(stage)][to_U32(type)]; }
134
135 bool clear() noexcept;
136 [[nodiscard]] LightList& getLights(const LightType type) {
138 return _lights[to_U32(type)];
139 }
140
141 [[nodiscard]] Light* getLight(I64 lightGUID, LightType type) const;
142
143 void sortLightData(RenderStage stage, const CameraSnapshot& cameraSnapshot);
144 void uploadLightData(RenderStage stage, const CameraSnapshot& cameraSnapshot, GFX::MemoryBarrierCommand& memCmdInOut);
145
146 void drawLightImpostors(GFX::CommandBuffer& bufferInOut) const;
147
148 void preRenderAllPasses(const Camera* playerCamera);
149
150 void onVolumeMoved(const BoundingSphere& volume, bool staticSource);
151
153 void debugLight(Light* light);
154
156 [[nodiscard]] static U8 GetShadowBindSlotOffset(const ShadowType type) noexcept {
157 return s_shadowLocation[to_U32(type)];
158 }
159
160 PROPERTY_RW(bool, lightImpostorsEnabled, false);
162
163 protected:
164 [[nodiscard]] bool frameStarted(const FrameEvent& evt) override;
165 [[nodiscard]] bool frameEnded(const FrameEvent& evt) override;
166
167 protected:
168 using LightShadowProperties = std::array<Light::ShadowProperties, Config::Lighting::MAX_SHADOW_CASTING_LIGHTS>;
169
170 friend class RenderPass;
171 void generateShadowMaps(const Camera& playerCamera, GFX::CommandBuffer& bufferInOut, GFX::MemoryBarrierCommand& memCmdInOut);
172
173 friend class ProjectManager;
174 [[nodiscard]] LightList::const_iterator findLight(const I64 GUID, const LightType type) const {
176 return findLightLocked(GUID, type);
177 }
178
179 [[nodiscard]] LightList::const_iterator findLightLocked(const I64 GUID, const LightType type) const {
180 return eastl::find_if(cbegin(_lights[to_U32(type)]), cend(_lights[to_U32(type)]),
181 [GUID](Light* const light) noexcept {
182 return light && light->getGUID() == GUID;
183 });
184 }
185
186 [[nodiscard]] bool isShadowCacheInvalidated(const vec3<F32>& cameraPosition, Light* light);
187
188
189 [[nodiscard]] static bool IsLightInViewFrustum(const Frustum& frustum, const Light* light) noexcept;
190
191
192 friend class ShadowMap;
193 static ShaderBuffer* ShadowBuffer() { return s_shadowBuffer.get(); }
194
195 friend class Renderer;
196 [[nodiscard]] static ShaderBuffer* LightBuffer() { return s_lightBuffer.get(); }
197 [[nodiscard]] static ShaderBuffer* SceneBuffer() { return s_sceneBuffer.get(); }
198
199 [[nodiscard]] U32 sortedLightCount(const RenderStage stage) const { return _sortedLightPropertiesCount[to_base(stage)]; }
200
201 private:
203 const LightList& lights,
204 const mat4<F32>& viewMatrix);
205
206 private:
207 struct SceneData {
208 // x = directional light count, y = point light count, z = spot light count, w = reserved
209 vec4<U32> _globalData = { 0, 0, 0, 0 };
210 // a = reserved
213 };
214
215 using LightData = std::array<LightProperties, Config::Lighting::MAX_ACTIVE_LIGHTS_PER_FRAME>;
216
218
224
228
230 eastl::fixed_vector<MovingVolume, Config::MAX_VISIBLE_NODES, true> _movedSceneVolumes;
231
235 bool _shadowBufferDirty = false;
236
240 static ShaderBuffer_uptr s_lightBuffer;
241 static ShaderBuffer_uptr s_sceneBuffer;
242 static ShaderBuffer_uptr s_shadowBuffer;
243};
244
246
247}; // namespace Divide
248
249#endif //DVD_LIGHT_POOL_H_
#define FWD_DECLARE_MANAGED_CLASS(T)
FORCE_INLINE I64 getGUID() const noexcept
Definition: GUIDWrapper.h:51
A light object placed in the scene at a certain position.
Definition: Light.h:58
void debugLight(Light *light)
nullptr = disabled
Definition: LightPool.cpp:355
std::array< LightData, to_base(RenderStage::COUNT)> _sortedLightProperties
Definition: LightPool.h:221
SharedMutex _movedSceneVolumesLock
Definition: LightPool.h:229
U32 uploadLightList(RenderStage stage, const LightList &lights, const mat4< F32 > &viewMatrix)
Definition: LightPool.cpp:375
void drawLightImpostors(GFX::CommandBuffer &bufferInOut) const
Definition: LightPool.cpp:574
Light * getLight(I64 lightGUID, LightType type) const
Definition: LightPool.cpp:361
static ShaderBuffer * LightBuffer()
Definition: LightPool.h:196
LightList & getLights(const LightType type)
Definition: LightPool.h:136
void uploadLightData(RenderStage stage, const CameraSnapshot &cameraSnapshot, GFX::MemoryBarrierCommand &memCmdInOut)
Definition: LightPool.cpp:465
void preRenderAllPasses(const Camera *playerCamera)
Definition: LightPool.cpp:537
static Handle< ShaderProgram > s_lightImpostorShader
Definition: LightPool.h:239
std::array< U32, to_base(LightType::COUNT)> LightCountPerType
Definition: LightPool.h:217
void generateShadowMaps(const Camera &playerCamera, GFX::CommandBuffer &bufferInOut, GFX::MemoryBarrierCommand &memCmdInOut)
Definition: LightPool.cpp:229
static ShaderBuffer_uptr s_lightBuffer
Definition: LightPool.h:240
void toggleLightType(const LightType type) noexcept
disable or enable a specific light type
Definition: LightPool.h:128
static ShaderBuffer * SceneBuffer()
Definition: LightPool.h:197
std::array< LightProperties, Config::Lighting::MAX_ACTIVE_LIGHTS_PER_FRAME > LightData
Definition: LightPool.h:215
POINTER_R(Light, debugLight, nullptr)
static bool IsLightInViewFrustum(const Frustum &frustum, const Light *light) noexcept
Definition: LightPool.cpp:49
void sortLightData(RenderStage stage, const CameraSnapshot &cameraSnapshot)
Definition: LightPool.cpp:423
std::array< SceneData, to_base(RenderStage::COUNT)> _sortedSceneProperties
Definition: LightPool.h:223
std::array< Light::ShadowProperties, Config::Lighting::MAX_SHADOW_CASTING_LIGHTS > LightShadowProperties
Definition: LightPool.h:168
LightList::const_iterator findLight(const I64 GUID, const LightType type) const
Definition: LightPool.h:174
ShadowProperties _shadowBufferData
Definition: LightPool.h:227
void onVolumeMoved(const BoundingSphere &volume, bool staticSource)
Definition: LightPool.cpp:220
std::array< LightList, to_base(LightType::COUNT)> _lights
Definition: LightPool.h:225
bool frameEnded(const FrameEvent &evt) override
frameEnded is called after the buffers have been swapped
Definition: LightPool.cpp:173
Time::ProfileTimer & _shadowPassTimer
Definition: LightPool.h:233
bool clear() noexcept
Definition: LightPool.cpp:161
bool removeLight(const Light &light)
remove a light from the manager
Definition: LightPool.cpp:203
bool isShadowCacheInvalidated(const vec3< F32 > &cameraPosition, Light *light)
Definition: LightPool.cpp:502
U32 sortedLightCount(const RenderStage stage) const
Definition: LightPool.h:199
bool _shadowBufferDirty
Definition: LightPool.h:235
static ShaderBuffer_uptr s_shadowBuffer
Definition: LightPool.h:242
static ShaderBuffer_uptr s_sceneBuffer
Definition: LightPool.h:241
eastl::fixed_vector< Light *, 32u, true > LightList
Definition: LightPool.h:115
static void DestroyStaticData()
Definition: LightPool.cpp:121
static U8 GetShadowBindSlotOffset(const ShadowType type) noexcept
Get the appropriate shadow bind slot for every light's shadow.
Definition: LightPool.h:156
std::array< bool, to_base(LightType::COUNT)> _lightTypeState
Definition: LightPool.h:226
static ShaderBuffer * ShadowBuffer()
Definition: LightPool.h:193
static void InitStaticData(PlatformContext &context)
Definition: LightPool.cpp:55
static std::array< U8, to_base(ShadowType::COUNT)> s_shadowLocation
Definition: LightPool.h:237
std::array< LightList, to_base(RenderStage::COUNT)> _sortedLights
Definition: LightPool.h:220
bool addLight(Light &light)
Add a new light to the manager.
Definition: LightPool.cpp:182
static Handle< Texture > s_lightIconsTexture
Definition: LightPool.h:238
std::array< U32, to_base(RenderStage::COUNT)> _sortedLightPropertiesCount
Definition: LightPool.h:222
U32 getActiveLightCount(const RenderStage stage, const LightType type) const noexcept
Retrieve the number of active lights in the scene;.
Definition: LightPool.h:133
~LightPool() override
Definition: LightPool.cpp:146
std::array< LightCountPerType, to_base(RenderStage::COUNT)> _activeLightCount
Definition: LightPool.h:219
PROPERTY_RW(bool, lightImpostorsEnabled, false)
SharedMutex _lightLock
Definition: LightPool.h:232
bool frameStarted(const FrameEvent &evt) override
Definition: LightPool.cpp:166
LightList::const_iterator findLightLocked(const I64 GUID, const LightType type) const
Definition: LightPool.h:179
bool lightTypeEnabled(const LightType type) const noexcept
Definition: LightPool.h:131
void toggleLightType(const LightType type, const bool state) noexcept
Definition: LightPool.h:129
eastl::fixed_vector< MovingVolume, Config::MAX_VISIBLE_NODES, true > _movedSceneVolumes
Definition: LightPool.h:230
PlatformContext & context() noexcept
TiledForwardShading.
Definition: Renderer.h:50
Scene & parentScene() noexcept
All the information needed for a single light's shadowmap.
Definition: ShadowMap.h:88
vec3< T > rgb
Definition: MathVectors.h:1378
constexpr U8 MAX_CSM_SPLITS_PER_LIGHT
Used for CSM or PSSM to determine the maximum number of frustum splits.
Definition: config.h:159
FColour4 WHITE
Random stuff added for convenience.
Definition: Colours.cpp:8
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr U32 to_U32(const T value)
uint8_t U8
std::shared_mutex SharedMutex
Definition: SharedMutex.h:43
std::shared_lock< mutex > SharedLock
Definition: SharedMutex.h:49
uint16_t U16
LightType
The different types of lights supported.
ShadowType
Definition: ShadowMap.h:40
int64_t I64
uint32_t U32
void * bufferPtr
constexpr auto to_base(const Type value) -> Type
std::array< vec4< F32 >, Config::Lighting::MAX_CSM_SPLITS_PER_LIGHT > _position
Definition: LightPool.h:93
std::array< mat4< F32 >, Config::Lighting::MAX_CSM_SPLITS_PER_LIGHT > _vpMatrix
Definition: LightPool.h:94
vec4< F32 > _padding1[2]
Definition: LightPool.h:212
mat4< F32 > _padding0[3]
Definition: LightPool.h:212
std::array< Light *, Config::Lighting::MAX_SHADOW_CASTING_LIGHTS > _entries
Definition: LightPool.h:108
std::array< PointShadowProperties, Config::Lighting::MAX_SHADOW_CASTING_POINT_LIGHTS > _pointLights
Definition: LightPool.h:97
std::array< SpotShadowProperties, Config::Lighting::MAX_SHADOW_CASTING_SPOT_LIGHTS > _spotLights
Definition: LightPool.h:98
std::array< CSMShadowProperties, Config::Lighting::MAX_SHADOW_CASTING_DIRECTIONAL_LIGHTS > _dirLights
Definition: LightPool.h:99
bufferPtr data() const noexcept
Definition: LightPool.h:101