forked from shadeMe/shadeMe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShadowFacts.h
415 lines (333 loc) · 14 KB
/
ShadowFacts.h
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#pragma once
#include "shadeMeInternals.h"
// the outer part of a shadow is called the penumbra!
namespace ShadowFacts
{
class ShadowCaster;
class ShadowCasterCountTable
{
enum
{
kMaxShadows_Actor = 0,
kMaxShadows_Book,
kMaxShadows_Flora,
kMaxShadows_Ingredient,
kMaxShadows_MiscItem,
kMaxShadows_AlchemyItem,
kMaxShadows_Equipment,
kMaxShadows__MAX
};
UInt32 Current[kMaxShadows__MAX];
UInt32 ValidatedShadowCount;
UInt32 MaxSceneShadowCount;
UInt32* GetCurrentCount(UInt8 Type);
SInt32 GetMaxCount(UInt8 Type) const;
public:
ShadowCasterCountTable(UInt32 MaxSceneShadows);
~ShadowCasterCountTable();
bool ValidateCount(ShadowCaster* Caster); // returns true if max count was not exceeded
void IncrementCount(ShadowCaster* Caster); // increments current count for the caster's type
bool GetSceneSaturated(void) const; // returns true if the total number of shadows in the scene has been reached
};
class ShadowCaster
{
friend class ShadowSceneProc;
NiNode* Node;
TESObjectREFR* Object;
float Distance; // from the player
float BoundRadius;
bool Actor;
bool UnderWater;
ShadowSceneLight* CreateShadowSceneLight(ShadowSceneNode* Root);
bool GetIsLargeObject(void) const;
bool GetIsMediumObject(void) const;
static bool SortComparatorDistance(ShadowCaster& LHS, ShadowCaster& RHS);
static bool SortComparatorBoundRadius(ShadowCaster& LHS, ShadowCaster& RHS);
static bool SortComparatorDistanceWeightedBoundRadius(ShadowCaster& LHS, ShadowCaster& RHS);
static bool SortComparatorBoundRadiusWeightedDistance(ShadowCaster & LHS, ShadowCaster & RHS);
public:
ShadowCaster(NiNode* Node, TESObjectREFR* Object);
~ShadowCaster();
TESObjectREFR* GetObject(void) const;
void GetDescription(std::string& Out) const;
// returns true if successful
bool Queue(ShadowSceneNode* Root, ShadowCasterCountTable* Count, ShadowSceneLight** OutSSL = NULL);
};
class ShadowSceneProc
{
typedef std::vector<ShadowCaster> CasterListT;
CasterListT Casters;
ShadowSceneNode* Root;
int ReducedGrids;
void DebugDump(void) const;
void CleanupSceneCasters(ShadowLightListT* ValidCasters) const;
void EnumerateSceneCasters(void);
void ProcessCell(TESObjectCELL* Cell);
public:
ShadowSceneProc(ShadowSceneNode* Root);
~ShadowSceneProc();
void Execute(UInt32 MaxShadowCount);
};
typedef Utilities::FilePathINIParamList PathSubstringListT;
typedef Utilities::IntegerINIParamList ObjectTypeListT;
class ShadowExclusionParameters
{
protected:
struct ParameterData
{
PathSubstringListT PathSubstrings;
ObjectTypeListT ObjectTypes;
INI::INISetting* PathsSource;
INI::INISetting* TypesSource;
ParameterData() :
PathSubstrings(), ObjectTypes(),
PathsSource(NULL), TypesSource(NULL)
{
;//
}
void Refresh(void)
{
SME_ASSERT(PathsSource && TypesSource);
PathSubstrings.Refresh(PathsSource);
ObjectTypes.Refresh(TypesSource);
}
};
enum
{
kParamType_Interior = 0,
kParamType_Exterior = 1,
kParamType__MAX,
};
ParameterData Parameters[kParamType__MAX];
void LoadParameters(UInt8 ParamType, SME::INI::INISetting* ExcludedTypes, SME::INI::INISetting* ExcludedPaths);
virtual void SetInteriorFlag(bool State, NiNode* Node, BSXFlags* xFlags) const = 0;
virtual void SetExteriorFlag(bool State, NiNode* Node, BSXFlags* xFlags) const = 0;
virtual bool GetAllowedInterior(NiNode* Node, BSXFlags* xFlags) const = 0;
virtual bool GetAllowedExterior(NiNode* Node, BSXFlags* xFlags) const = 0;
virtual const char* GetDescription(void) const = 0;
public:
virtual ~ShadowExclusionParameters();
virtual void Initialize(void) = 0;
void HandleModelLoad(NiNode* Node, BSXFlags* xFlags) const;
bool GetAllowed(NiNode* Node, TESObjectREFR* Object) const;
void RefreshParameters(void);
};
// hacky - stored directly in the NiAVObject to reduce render-time overhead
// AFAIK, these bits are unused
// only used for string comparisons
class NiAVObjectSpecialFlags
{
public:
enum
{
//===========================================================================
k__BEGININTERNAL = NiAVObject::kFlag_Unk06,
//===========================================================================
// primarily used for exclusion params paths
kDontReceiveInteriorShadow = 1 << 7,
kDontReceiveExteriorShadow = 1 << 8,
kDontCastInteriorSelfShadow = 1 << 9,
kDontCastExteriorSelfShadow = 1 << 10,
kDontCastInteriorShadow = 1 << 11,
kDontCastExteriorShadow = 1 << 12,
};
static bool GetFlag(NiAVObject* Node, UInt16 Flag);
static void SetFlag(NiAVObject* Node, UInt16 Flag, bool State);
};
// same as above but for BSXFlags
class BSXFlagsSpecialFlags
{
public:
enum
{
//=========================================================================
k__BEGININTERNAL = BSXFlags::kFlag_Unk05,
//=========================================================================
kCannotBeLargeObject = 1 << 6,
kRenderBackFacesToShadowMap = 1 << 7,
kDontPerformLOSCheck = 1 << 8,
kAllowInteriorHeuristics = 1 << 9,
kOnlySelfShadowInterior = 1 << 10,
kOnlySelfShadowExterior = 1 << 11,
//=========================================================================
k__BEGINEXTERNAL = 1 << 29,
//=========================================================================
// can be baked into the model
kDontReceiveShadow = 1 << 30,
kDontCastShadow = 1 << 31,
};
static bool GetFlag(BSXFlags* Store, UInt32 Flag);
static void SetFlag(BSXFlags* Store, UInt32 Flag, bool State);
static bool GetFlag(NiAVObject* Node, UInt32 Flag);
static void SetFlag(NiAVObject* Node, UInt32 Flag, bool State);
};
// the regular CastsShadows flag will be used on non-light refs to indicate the opposite
enum
{
kTESFormSpecialFlag_DoesntCastShadow = TESForm::kFormFlags_CastShadows,
};
class MainShadowExParams : public ShadowExclusionParameters
{
protected:
virtual void SetInteriorFlag(bool State, NiNode* Node, BSXFlags* xFlags) const;
virtual void SetExteriorFlag(bool State, NiNode* Node, BSXFlags* xFlags) const;
virtual bool GetAllowedInterior(NiNode* Node, BSXFlags* xFlags) const;
virtual bool GetAllowedExterior(NiNode* Node, BSXFlags* xFlags) const;
virtual const char* GetDescription(void) const;
public:
virtual ~MainShadowExParams();
virtual void Initialize(void);
static MainShadowExParams Instance;
};
class SelfShadowExParams : public ShadowExclusionParameters
{
protected:
virtual void SetInteriorFlag(bool State, NiNode* Node, BSXFlags* xFlags) const;
virtual void SetExteriorFlag(bool State, NiNode* Node, BSXFlags* xFlags) const;
virtual bool GetAllowedInterior(NiNode* Node, BSXFlags* xFlags) const;
virtual bool GetAllowedExterior(NiNode* Node, BSXFlags* xFlags) const;
virtual const char* GetDescription(void) const;
public:
virtual ~SelfShadowExParams();
virtual void Initialize(void);
static SelfShadowExParams Instance;
};
class ShadowReceiverExParams : public ShadowExclusionParameters
{
protected:
virtual void SetInteriorFlag(bool State, NiNode* Node, BSXFlags* xFlags) const;
virtual void SetExteriorFlag(bool State, NiNode* Node, BSXFlags* xFlags) const;
virtual bool GetAllowedInterior(NiNode* Node, BSXFlags* xFlags) const;
virtual bool GetAllowedExterior(NiNode* Node, BSXFlags* xFlags) const;
virtual const char* GetDescription(void) const;
public:
virtual ~ShadowReceiverExParams();
virtual void Initialize(void);
static ShadowReceiverExParams Instance;
};
class ShadowReceiverValidator : public Utilities::NiNodeChildVisitor
{
protected:
NiNodeListT* NonReceivers;
public:
ShadowReceiverValidator(NiNodeListT* OutList);
virtual ~ShadowReceiverValidator();
virtual bool AcceptBranch(NiNode* Node);
virtual void AcceptLeaf(NiAVObject* Object);
};
class FadeNodeShadowFlagUpdater: public Utilities::NiNodeChildVisitor
{
public:
virtual ~FadeNodeShadowFlagUpdater();
virtual bool AcceptBranch(NiNode* Node);
virtual void AcceptLeaf(NiAVObject* Object);
};
class ShadowMapTexturePool
{
private:
// highest resolution to lowest
enum
{
kPool_Tier1 = 0,
kPool_Tier2 = 1,
kPool_Tier3 = 2,
kPool__MAX
};
BSTextureManager* TexturePool[kPool__MAX];
UInt16 PoolResolution[kPool__MAX];
void Create(void);
void Reset(void);
void SetShadowMapResolution(UInt16 Resolution) const;
void ReserveShadowMaps(BSTextureManager* Manager, UInt32 Count) const;
BSTextureManager* GetPoolByResolution(UInt16 Resolution) const;
public:
ShadowMapTexturePool();
~ShadowMapTexturePool();
void Initialize(void);
void HandleShadowPass(NiDX9Renderer* Renderer, UInt32 MaxShadowCount) const;
BSRenderedTexture* GetShadowMapTexture(ShadowSceneLight* Light) const;
void DiscardShadowMapTexture(BSRenderedTexture* Texture) const;
static ShadowMapTexturePool Instance;
static bool GetEnabled(void);
};
class ShadowRenderTasks
{
public:
static ShadowLightListT LightProjectionUpdateQueue;
static const float ShadowDepthBias;
private:
static PathSubstringListT BackFaceIncludePaths;
static PathSubstringListT LargeObjectExcludePaths;
static PathSubstringListT LightLOSCheckExcludePaths;
static PathSubstringListT InteriorHeuristicsIncludePaths;
static PathSubstringListT InteriorHeuristicsExcludePaths;
static const float DirectionalLightCheckThresholdDistance;
static PathSubstringListT SelfExclusiveIncludePathsInterior;
static PathSubstringListT SelfExclusiveIncludePathsExterior;
static void ToggleBackFaceCulling(bool State);
static void PerformModelLoadTask(NiNode* Node, BSXFlags* xFlags);
static bool PerformExclusiveSelfShadowCheck(NiNode* Node, TESObjectREFR* Object);
static bool PerformShadowLightSourceCheck(ShadowSceneLight* Source, TESObjectREFR* Object);
static bool PerformLightLOSCheck(ShadowSceneLight* Source, TESObjectREFR* Object);
static bool __stdcall ReactsToSmallLights(ShadowSceneLight* Source);
static bool __stdcall CanHaveDirectionalShadow(ShadowSceneLight* Source);
enum
{
kSSLExtraFlag_NoActiveLights = 1 << 0, // no active scene lights within casting distance
kSSLExtraFlag_DisallowSmallLights = 1 << 1,
kSSLExtraFlag_DisallowDirectionalLight = 1 << 2,
kSSLExtraFlag_NoShadowLightSource = 1 << 3,
};
public:
static void Initialize(void);
static void RefreshMiscPathLists(void);
static void HandleMainProlog(void);
static void HandleMainEpilog(void);
static void __stdcall HandleSSLCreation(ShadowSceneLight* Light);
static void __stdcall HandleShadowMapRenderingProlog(NiNode* Node, ShadowSceneLight* Source);
static void __stdcall HandleShadowMapRenderingEpilog(NiNode* Node, ShadowSceneLight* Source);
static void __stdcall HandleShadowLightUpdateReceiver(ShadowSceneLight* Source, NiNode* SceneGraph);
static void __stdcall HandleLightProjectionProlog(ShadowSceneLight* Source);
static void __stdcall HandleLightProjectionEpilog(ShadowSceneLight* Source);
// check for active lights and if the occluder reacts to them
static bool __stdcall HandleLightProjectionStage1(ShadowSceneLight* Source, int ActiveLights);
// check if directional source is allowed
static bool __stdcall HandleLightProjectionStage2(ShadowSceneLight* Source);
static void __stdcall QueueShadowOccluders(UInt32 MaxShadowCount);
static bool __stdcall HandleSelfShadowing(ShadowSceneLight* Caster); // return true to allow
static void __stdcall HandleModelLoad(NiNode* Node, bool Allocation);
static void __stdcall HandleShadowReceiverLightingPropertyUpdate(ShadowSceneLight* Source, NiNode* Receiver);
static void __stdcall HandleTreeModelLoad(BSTreeNode* Node);
static bool CanBeLargeObject(NiNode* Node);
static bool IsLargeObject(NiNode* Node);
static bool IsMediumObject(NiNode* Node);
static bool __stdcall PerformAuxiliaryChecks(ShadowSceneLight* Source);
static bool HasPlayerLOS(TESObjectREFR* Object, NiNode* Node, float Distance);
static bool CanReceiveShadow(NiNode* Node);
static bool RunInteriorHeuristicGauntlet(TESObjectREFR* Caster, NiNode* Node, float BoundRadius); // return true to allow
};
_DeclareMemHdlr(EnumerateFadeNodes, "render unto Oblivion...");
_DeclareMemHdlr(RenderShadowsProlog, "");
_DeclareMemHdlr(RenderShadowsEpilog, "");
_DeclareMemHdlr(QueueModel3D, "");
_DeclareMemHdlr(UpdateGeometryLighting, "");
_DeclareMemHdlr(UpdateGeometryLightingSelf, "selective self-shadowing support");
_DeclareMemHdlr(RenderShadowMap, "");
_DeclareMemHdlr(CheckLargeObjectLightSource, "prevents large objects from being affected by small light sources (z.B magic projectiles, torches, etc)");
_DeclareMemHdlr(CheckShadowReceiver, "");
_DeclareMemHdlr(CheckDirectionalLightSource, "");
_DeclareMemHdlr(TextureManagerDiscardShadowMap, "");
_DeclareMemHdlr(TextureManagerReserveShadowMaps, "");
_DeclareMemHdlr(ShadowSceneLightGetShadowMap, "");
_DeclareMemHdlr(CreateWorldSceneGraph, "");
_DeclareMemHdlr(CullCellActorNodeA, "");
_DeclareMemHdlr(CullCellActorNodeB, "");
_DeclareMemHdlr(BlacklistTreeNode, "");
_DeclareMemHdlr(TrifleSupportPatch, "compatibility patch for Trifle's first person shadows");
_DeclareMemHdlr(ShadowSceneLightCtor, "");
_DeclareMemHdlr(CalculateProjectionProlog, "");
_DeclareMemHdlr(CalculateProjectionEpilog, "");
_DeclareMemHdlr(ShadowLightShaderDepthBias, "");
void Patch(void);
void Initialize(void);
}