Repository navigation
Expand file tree
/
Copy pathMeshMapBuildData.cpp
More file actions
226 lines (193 loc) · 7.5 KB
/
Copy pathMeshMapBuildData.cpp
File metadata and controls
226 lines (193 loc) · 7.5 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
class ULevel : public UObject, public IInterface_AssetUserData {
UMapBuildDataRegistry* MapBuildData;
}
class UMapBuildDataRegistry : public UObject {
TEnumAsByte<enum ELightingBuildQuality> LevelLightingQuality;
TMap<FGuid, FMeshMapBuildData> MeshBuildData;
TMap<FGuid, FLightComponentMapBuildData> LightBuildData;
TArray<FLightmapResourceCluster> LightmapResourceClusters;
}
/* class FLightmapClusterResourceInput {
const UTexture2D* LightMapTextures[2];
const UTexture2D* SkyOcclusionTexture;
const UTexture2D* AOMaterialMaskTexture;
const ULightMapVirtualTexture2D* LightMapVirtualTexture;
const UTexture2D* ShadowMapTexture;
} */
class FLightmapResourceCluster : public FRenderResource {
FLightmapClusterResourceInput Input;
FUniformBufferRHIRef UniformBuffer;
}
class FMeshMapBuildData {
FLightMapRef LightMap;
FShadowMapRef ShadowMap;
TArray<FGuid> IrrelevantLights;
TArray<FPerInstanceLightmapData> PerInstanceLightmapData;
const FLightmapResourceCluster* ResourceCluster = nullptr;
};
FMeshMapBuildData* UMapBuildDataRegistry::GetMeshBuildData(FGuid MeshId)
{
FMeshMapBuildData* FoundData = MeshBuildData.Find(MeshId);
if (FoundData && !FoundData->ResourceCluster)
{
return nullptr;
}
return FoundData;
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
class FStaticLightingMesh : public virtual FRefCountedObject {
const TArray<ULightComponent*> RelevantLights;/** The lights which affect the mesh's primitive. */
const UPrimitiveComponent* const Component;/** The primitive component this mesh was created by. */
FGuid Guid;/** Unique ID for tracking this lighting mesh during distributed lighting */
FGuid SourceMeshGuid;/** Cached guid for the source mesh */
}
class FStaticMeshStaticLightingMesh : public FStaticLightingMesh {}
class FStaticLightingMapping : public virtual FRefCountedObject {/** A mapping between world-space surfaces and a static lighting cache. */
class FStaticLightingMesh* Mesh;/** The mesh associated with the mapping. */
UObject* const Owner;/** The object which owns the mapping. */
uint32 bProcessMapping : 1;/** true if the mapping should be processed by Lightmass. */
}
class FStaticLightingTextureMapping : public FStaticLightingMapping {/** A mapping between world-space surfaces and static lighting cache textures. */
const int32 SizeX;/** The width of the static lighting textures used by the mapping. */
const int32 SizeY;/** The height of the static lighting textures used by the mapping. */
const int32 LightmapTextureCoordinateIndex;/** The lightmap texture coordinate index which is used for the mapping. */
const bool bBilinearFilter;/** Whether to apply a bilinear filter to the sample or not. */
};
class FStaticMeshStaticLightingTextureMapping : public FStaticLightingTextureMapping {/** Represents a static mesh primitive with texture mapped static lighting. */
TWeakObjectPtr<UStaticMeshComponent> Primitive;/** The primitive this mapping represents. */
const int32 LODIndex;/** The LOD this mapping represents. */
};
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
void FLightmassProcessor::ImportMappings(bool bProcessImmediately = false)
{
for(auto& Guid : CompletedMappingTasks.ExtractAll())
{
ImportMapping(Guid, bProcessImmediately);
}
}
void FLightmassProcessor::ImportMapping( const FGuid& MappingGuid, bool bProcessImmediately )
{
if (IsStaticLightingTextureMapping(MappingGuid))
{
ImportStaticLightingTextureMapping(MappingGuid, bProcessImmediately);
}
else
{
ULightComponent* Light = FindLight(MappingGuid);
if (Light)
{
ImportStaticShadowDepthMap(Light);
}
else
{
check(0);
}
}
}
class Lightmass::FStaticShadowDepthMapData {
FMatrix WorldToLight;
int32 ShadowMapSizeX;
int32 ShadowMapSizeY;
};
class FStaticShadowDepthMapData {
FMatrix WorldToLight;
int32 ShadowMapSizeX;
int32 ShadowMapSizeY;
TArray<FFloat16> DepthSamples;
};
class FLightComponentMapBuildData {
int32 ShadowMapChannel = -1;
FStaticShadowDepthMapData DepthMap;
};
/*
Add: UMapBuildDataRegistry::LightBuildData by (Light->LightGuid, FLightComponentMapBuildData())
Read: UMapBuildDataRegistry::LightBuildData[Light->LightGuid].DepthMap from Swam
*/
void FLightmassProcessor::ImportStaticShadowDepthMap(ULightComponent* Light)
{
BeginReleaseResource(&Light->StaticShadowDepthMap);
const FString ChannelName = Lightmass::CreateChannelName(Light->LightGuid, Lightmass::LM_DOMINANTSHADOW_VERSION, Lightmass::LM_DOMINANTSHADOW_EXTENSION);
const int32 Channel = Swarm.OpenChannel( *ChannelName, LM_DOMINANTSHADOW_CHANNEL_FLAGS );
UMapBuildDataRegistry* Registry = Light->GetOwner()->GetLevel()->GetOrCreateMapBuildData();
FLightComponentMapBuildData& CurrentLightData = Registry->FindOrAllocateLightBuildData(Light->LightGuid, true);
Lightmass::FStaticShadowDepthMapData ShadowMapData;
Swarm.ReadChannel(Channel, &ShadowMapData, sizeof(ShadowMapData));
CurrentLightData.DepthMap.WorldToLight = ShadowMapData.WorldToLight;
CurrentLightData.DepthMap.ShadowMapSizeX = ShadowMapData.ShadowMapSizeX;
CurrentLightData.DepthMap.ShadowMapSizeY = ShadowMapData.ShadowMapSizeY;
ReadArray(Channel, CurrentLightData.DepthMap.DepthSamples);
Swarm.CloseChannel(Channel);
}
class FStaticShadowDepthMap : public FTexture
{
const FStaticShadowDepthMapData* Data = nullptr;
virtual void InitRHI();
};
class ENGINE_API ULightComponent : public ULightComponentBase {
FStaticShadowDepthMap StaticShadowDepthMap;
}
class UMapBuildDataRegistry : public UObject {
TMap<FGuid, FLightComponentMapBuildData> LightBuildData;
FLightComponentMapBuildData* GetLightBuildData(FGuid LightId)
{
return LightBuildData.Find(LightId);
}
}
const FLightComponentMapBuildData* ULightComponent::GetLightComponentMapBuildData() const
{
ULevel* OwnerLevel = GetOwner()->GetLevel();
if (OwnerLevel && OwnerLevel->MapBuildData)
{
UMapBuildDataRegistry* MapBuildData = OwnerLevel->MapBuildData;
if (MapBuildData)
{
return MapBuildData->GetLightBuildData(LightGuid);
}
}
return NULL;
}
void ULightComponent::CreateRenderState_Concurrent(FRegisterComponentContext* Context)
{
Super::CreateRenderState_Concurrent(Context);
UWorld* World = GetWorld();
const bool bHidden = !ShouldComponentAddToScene() || !ShouldRender() || Intensity <= 0.f;
if (!bHidden)
{
InitializeStaticShadowDepthMap();
World->Scene->AddLight(this);
bAddedToSceneVisible = true;
}
// Add invisible stationary lights to the scene in the editor
// Even invisible stationary lights consume a shadowmap channel so they must be included in the stationary light overlap preview
else if (GIsEditor
&& !World->IsGameWorld()
&& CastShadows
&& CastStaticShadows
&& Mobility == EComponentMobility::Stationary)
{
InitializeStaticShadowDepthMap();
World->Scene->AddInvisibleLight(this);
}
}
void ULightComponent::InitializeStaticShadowDepthMap()
{
if (Mobility == EComponentMobility::Stationary)
{
const FStaticShadowDepthMapData* DepthMapData = NULL;
const FLightComponentMapBuildData* ShadowBuildData = GetLightComponentMapBuildData();
if (ShadowBuildData)
{
DepthMapData = &ShadowBuildData->DepthMap;
}
FStaticShadowDepthMap* DepthMap = &StaticShadowDepthMap;
ENQUEUE_RENDER_COMMAND(SetDepthMapData)(
[DepthMap, DepthMapData](FRHICommandList& RHICmdList)
{
DepthMap->Data = DepthMapData;
DepthMap->InitResource();
});
BeginInitResource(&StaticShadowDepthMap);
}
}