-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShadowMapManager.cpp
More file actions
156 lines (132 loc) · 4.17 KB
/
Copy pathShadowMapManager.cpp
File metadata and controls
156 lines (132 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "ShadowMapManager.h"
#include "RenderTexture.h"
#include "RenderData.h"
#include "Renderer.h"
#include "ShelfPacker.h"
#include "Formats.hpp"
#include "IRenderContext.h"
#include "Core/Name.h"
#include "Core/SObject.h"
#include "Core/GarbageCollection.h"
#include "Core/Logger.h"
#include <algorithm>
namespace sh::render
{
ShadowMapManager::ShadowMapManager() = default;
ShadowMapManager::~ShadowMapManager()
{
Clear();
}
SH_RENDER_API void ShadowMapManager::Init(IRenderContext& ctx, uint32_t atlasSize)
{
this->ctx = &ctx;
this->atlasSize = atlasSize;
packer = std::make_unique<ShelfPacker>(static_cast<int>(atlasSize), static_cast<int>(atlasSize), 0);
renderData.tag = core::Name{ "Depth" };
renderData.priority = 1000; // 다른 패스보다 먼저 실행되도록
}
SH_RENDER_API void ShadowMapManager::Clear()
{
if (atlas != nullptr)
{
core::GarbageCollection::GetInstance()->RemoveRootSet(atlas);
atlas = nullptr;
}
casters.clear();
casterSlots.clear();
casterLightSpace.clear();
renderData = RenderData{};
packer.reset();
ctx = nullptr;
}
void ShadowMapManager::EnsureAtlas()
{
if (atlas != nullptr)
return;
if (ctx == nullptr)
{
SH_ERROR("ShadowMapManager: context is not initialized");
return;
}
atlas = core::SObject::Create<RenderTexture>(TextureFormat::None, TextureFormat::D32, false);
atlas->SetSize(atlasSize, atlasSize);
atlas->Build(*ctx);
atlas->SetName("ShadowAtlas");
// 매니저는 SObject가 아니므로 GC가 회수하지 않도록 루트셋에 등록
core::GarbageCollection::GetInstance()->SetRootSet(atlas);
}
SH_RENDER_API void ShadowMapManager::Register(IShadowCaster& caster)
{
if (std::find(casters.begin(), casters.end(), &caster) != casters.end())
return;
casters.push_back(&caster);
}
SH_RENDER_API void ShadowMapManager::Unregister(IShadowCaster& caster)
{
casters.erase(std::remove(casters.begin(), casters.end(), &caster), casters.end());
casterSlots.erase(&caster);
casterLightSpace.erase(&caster);
}
SH_RENDER_API void ShadowMapManager::Submit(Renderer& renderer)
{
if (casters.empty())
return;
EnsureAtlas();
if (atlas == nullptr || packer == nullptr)
return;
packer->Reset();
casterSlots.clear();
casterLightSpace.clear();
renderData.renderViewers.clear();
renderData.SetRenderTarget(atlas);
renderData.renderViewers.reserve(casters.size());
for (const IShadowCaster* caster : casters)
{
if (caster == nullptr)
continue;
const uint32_t res = caster->GetShadowMapResolution();
if (res == 0)
continue;
int x = 0, y = 0;
if (!packer->Alloc(static_cast<int>(res), static_cast<int>(res), x, y))
{
SH_ERROR_FORMAT("ShadowMapManager: out of atlas space (atlas {}, requested {})",
atlasSize, res);
continue;
}
Slot slot{};
slot.uvOffset = glm::vec2{ static_cast<float>(x) / atlasSize, static_cast<float>(y) / atlasSize };
slot.uvSize = glm::vec2{ static_cast<float>(res) / atlasSize, static_cast<float>(res) / atlasSize };
slot.valid = true;
const glm::mat4 view = caster->GetShadowViewMatrix();
const glm::mat4 proj = caster->GetShadowProjMatrix();
casterSlots.emplace(caster, slot);
casterLightSpace.emplace(caster, proj * view);
RenderViewer viewer{};
viewer.viewMatrix = view;
viewer.projMatrix = proj;
viewer.pos = caster->GetShadowPos();
viewer.to = caster->GetShadowLookAt();
viewer.viewportRect = glm::uvec4{ atlasSize * slot.uvOffset.x, atlasSize * slot.uvOffset.y, atlasSize * slot.uvSize.x, atlasSize * slot.uvSize.y };
viewer.viewportScissor = viewer.viewportRect;
renderData.renderViewers.push_back(viewer);
}
if (renderData.renderViewers.empty())
return;
renderer.PushRenderData(renderData);
}
SH_RENDER_API auto ShadowMapManager::GetSlot(const IShadowCaster& caster) const -> Slot
{
auto it = casterSlots.find(&caster);
if (it == casterSlots.end())
return Slot{};
return it->second;
}
SH_RENDER_API auto ShadowMapManager::GetLightSpaceMatrix(const IShadowCaster& caster) const -> glm::mat4
{
auto it = casterLightSpace.find(&caster);
if (it == casterLightSpace.end())
return glm::mat4{ 1.f };
return it->second;
}
}//namespace