forked from shadeMe/shadeMe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShadowFigures.cpp
415 lines (349 loc) · 12.3 KB
/
ShadowFigures.cpp
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
#include "ShadowFigures.h"
#include "ShadowFacts.h"
#include "ShadowSundries.h"
#pragma warning(disable: 4005 4748)
#define STRINGIZE2(s) #s
#define STRINGIZE(s) STRINGIZE2(s)
#define DEF_SRC(name, ...) ShadowRenderConstant name(STRINGIZE2(name), ##__VA_ARGS__##)
namespace ShadowFigures
{
// these constants seem to have no discernible effect
// DEF_SRC(SRC_A3D8E8, true, 0.01, 0x007D4860 + 2);
// DEF_SRC(SRC_A91288, true, -0.01, 0x007D4877 + 2);
// DEF_SRC(SRC_B258D0, false, 1.0, 0x007D48B2 + 1);
// DEF_SRC(SRC_B258D4, false, 0, 0x007D48B7 + 2);
// DEF_SRC(SRC_B258D8, false, 0, 0x007D48BD + 2);
// DEF_SRC(SRC_A3F3E8, true, 10.0, 0x007D4BA6 + 2);
// DEF_SRC(SRC_B25AD0, false, 0.0, 0x007D4D3E + 1);
// DEF_SRC(SRC_B25AD4, false, 0.0, 0x007D4D43 + 2);
// DEF_SRC(SRC_B25AD8, false, 0.0, 0x007D4D49 + 2);
// DEF_SRC(SRC_B25ADC, false, 1.0, 0x007D4D56 + 1);
// DEF_SRC(SRC_A3D0C0, true, 2.0, 0x007D511A + 2); SRC_A3D0C0.AddPatchLocation(0x007D5161 + 2);
// DEF_SRC(SMRC_A2FC68, true, 0.0, 0x007D24E5 + 2);
// DEF_SRC(SMRC_A2FC70, true, 1000.0, 0x007D28D2 + 2);
// ====================================================
// Shadow Map Render Stage
// ====================================================
DEF_SRC(SRC_A30068, true, 0.05, 0x007D4740 + 2);
DEF_SRC(SRC_B258E8, false, 0, 0x007D4811 + 2);
DEF_SRC(SRC_B258EC, false, 0, 0x007D4823 + 2);
DEF_SRC(SRC_B258F0, false, 1.0, 0x007D4833 + 2);
DEF_SRC(SRC_A91278, true, 0.01745327934622765, 0x007D49F2 + 2); // bias?
DEF_SRC(SRC_A91280, true, 110.0, 0x007D49D8 + 2); // sampling scale?
DEF_SRC(SRC_A2FAA0, true, 0.5, 0x007D49EC + 2); // umbra related?
DEF_SRC(SRC_A6BEA0, true, 400.0, 0x007D4CF7 + 2);
// ====================================================
// Light Projection Stage
// ====================================================
DEF_SRC(SMRC_A31C70, true, 0.75, 0x007D2CB4 + 2); // distortion/extrude mul?
DEF_SRC(SMRC_A3B1B8, true, 256.0, 0x007D2CEC + 2); // some kinda resolution?
DEF_SRC(SMRC_A38618, true, 2.5, 0x007D2D01 + 2); // light source dist mul
DEF_SRC(SMRC_A3F3A0, true, 6.0, 0x007D2D94 + 2);
DEF_SRC(SMRC_A91270, true, 0.4, 0x007D2DB2 + 2);
DEF_SRC(SMRC_A91268, true, 0.8, 0x007D2DC8 + 2); // shadow darkness?
ShadowRenderConstant::ShadowRenderConstant( const char* Name, bool Wide, long double DefaultValue, UInt32 PrimaryPatchLocation ) :
Wide(Wide),
PatchLocations(),
Name(Name)
{
SME_ASSERT(Name);
Data.d = 0.0f;
Default.d = 0.0f;
if (Wide)
{
Data.d = DefaultValue;
Default.d = DefaultValue;
}
else
{
Data.f = DefaultValue;
Default.f = DefaultValue;
}
SME_ASSERT(PrimaryPatchLocation);
PatchLocations.push_back(PrimaryPatchLocation);
ShadowRenderConstantRegistry::GetSingleton()->Register(this);
}
ShadowRenderConstant::~ShadowRenderConstant()
{
PatchLocations.clear();
}
void ShadowRenderConstant::AddPatchLocation( UInt32 Location )
{
SME_ASSERT(Location);
PatchLocations.push_back(Location);
}
void ShadowRenderConstant::ApplyPatch( void ) const
{
for (PatchLocationListT::const_iterator Itr = PatchLocations.begin(); Itr != PatchLocations.end(); Itr++)
{
if (Wide)
SME::MemoryHandler::SafeWrite32(*Itr, (UInt32)&Data.d);
else
SME::MemoryHandler::SafeWrite32(*Itr, (UInt32)&Data.f);
}
}
void ShadowRenderConstant::SetValue( long double NewValue )
{
if (Wide)
Data.d = NewValue;
else
Data.f = NewValue;
}
void ShadowRenderConstant::ResetDefault( void )
{
if (Wide)
Data.d = Default.d;
else
Data.f = Default.f;
}
long double ShadowRenderConstant::GetValue( void ) const
{
if (Wide)
return Data.d;
else
return Data.f;
}
const char* ShadowRenderConstantRegistry::kINIPath = "Data\\OBSE\\Plugins\\ShadowRenderConstants.ini";
ShadowRenderConstantRegistry::ShadowRenderConstantRegistry() :
DataStore()
{
;//
}
ShadowRenderConstantRegistry::~ShadowRenderConstantRegistry()
{
DataStore.clear();
}
void ShadowRenderConstantRegistry::Save( void )
{
_MESSAGE("Saving shadow render constants to %s...", kINIPath);
char IntBuffer[0x200] = {0}, ExtBuffer[0x200] = {0};
for (ConstantValueTableT::const_iterator Itr = DataStore.begin(); Itr != DataStore.end(); Itr++)
{
FORMAT_STR(IntBuffer, "%f", (float)Itr->second.Interior);
FORMAT_STR(ExtBuffer, "%f", (float)Itr->second.Exterior);
WritePrivateProfileStringA("Interior", Itr->first->Name.c_str(), IntBuffer, kINIPath);
WritePrivateProfileStringA("Exterior", Itr->first->Name.c_str(), ExtBuffer, kINIPath);
}
}
ShadowRenderConstantRegistry* ShadowRenderConstantRegistry::GetSingleton( void )
{
static ShadowRenderConstantRegistry Singleton;
return &Singleton;
}
void ShadowRenderConstantRegistry::Initialize( void )
{
for (ConstantValueTableT::const_iterator Itr = DataStore.begin(); Itr != DataStore.end(); Itr++)
{
Itr->first->ApplyPatch();
}
std::fstream INIFile(kINIPath, std::fstream::in);
if (INIFile.fail())
{
// set the optimal values before dumping to INI
DataStore[&SRC_A6BEA0].Exterior = 16384;
DataStore[&SMRC_A38618].Exterior = 30;
DataStore[&SMRC_A38618].Interior = 30;
DataStore[&SMRC_A3F3A0].Exterior = 10;
Save();
}
}
void ShadowRenderConstantRegistry::Load( void )
{
_MESSAGE("Loading shadow render constants from %s...", kINIPath);
char IntBuffer[0x200] = {0}, ExtBuffer[0x200] = {0};
char Default[0x100] = {0};
for (ConstantValueTableT::iterator Itr = DataStore.begin(); Itr != DataStore.end(); Itr++)
{
FORMAT_STR(Default, "%f", (float)Itr->second.Interior);
GetPrivateProfileStringA("Interior", Itr->first->Name.c_str(), Default, IntBuffer, sizeof(IntBuffer), kINIPath);
Itr->second.Interior = atof(IntBuffer);
FORMAT_STR(Default, "%f", (float)Itr->second.Exterior);
GetPrivateProfileStringA("Exterior", Itr->first->Name.c_str(), Default, ExtBuffer, sizeof(ExtBuffer), kINIPath);
Itr->second.Exterior = atof(ExtBuffer);
}
}
void ShadowRenderConstantRegistry::UpdateConstants( void )
{
for (ConstantValueTableT::iterator Itr = DataStore.begin(); Itr != DataStore.end(); Itr++)
{
if (TES::GetSingleton()->currentInteriorCell)
Itr->first->SetValue(Itr->second.Interior);
else
Itr->first->SetValue(Itr->second.Exterior);
}
// special overrides
static const float DiffusePercent = 55.f;
float DiffuseMultiplier = DiffusePercent / 100.f * SMRC_A3F3A0.GetValue();
TESWeather* CurrentWeather = Sky::GetSingleton()->firstWeather;
if (CurrentWeather && TES::GetSingleton()->currentInteriorCell == NULL)
{
UInt8 WeatherType = Utilities::GetWeatherClassification(CurrentWeather);
if ((WeatherType == TESWeather::kType_Cloudy && Settings::kWeatherDiffuseCloudy().i) ||
(WeatherType == TESWeather::kType_Rainy && Settings::kWeatherDiffuseRainy().i) ||
(WeatherType == TESWeather::kType_Snow && Settings::kWeatherDiffuseSnow().i))
{
SMRC_A3F3A0.SetValue(DiffuseMultiplier);
}
}
}
void ShadowRenderConstantRegistry::Register( ShadowRenderConstant* Constant )
{
SME_ASSERT(Constant);
if (DataStore.count(Constant) == 0)
{
DataStore.insert(std::make_pair(Constant, ValuePair()));
DataStore[Constant].Interior = Constant->GetValue();
DataStore[Constant].Exterior = Constant->GetValue();
}
}
ShadowRenderConstantHotSwapper::Swapper::Swapper( ShadowRenderConstant* Constant ) :
Source(Constant),
OldValue(0.f),
Reset(false)
{
SME_ASSERT(Source);
OldValue = Source->GetValue();
}
ShadowRenderConstantHotSwapper::Swapper::~Swapper()
{
if (Reset)
Source->SetValue(OldValue);
}
void ShadowRenderConstantHotSwapper::Swapper::Swap( long double NewValue )
{
Source->SetValue(NewValue);
Reset = true;
}
void __stdcall ShadowRenderConstantHotSwapper::HandleLightProjectionStage( ShadowSceneLight* Source, void* AuxParam )
{
SME_ASSERT(Source);
// the SSL light projection stage method is an utter bastard with a really weird stack frame (bloody compiler optimizations!)
// this essentially limits us to calling the function under very specific conditions, i.e., this wrapper works for some reason
// so we'll use this bugger to force update unqueued caster SSLs that failed their aux checks
// why? because we need to keep up with the source ref's state changes (and also the active scene lights)
if (std::find(ShadowFacts::ShadowRenderTasks::LightProjectionUpdateQueue.begin(),
ShadowFacts::ShadowRenderTasks::LightProjectionUpdateQueue.end(),
Source) == ShadowFacts::ShadowRenderTasks::LightProjectionUpdateQueue.end())
{
ShadowFacts::ShadowRenderTasks::LightProjectionUpdateQueue.push_back(Source);
}
for (ShadowLightListT::iterator Itr = ShadowFacts::ShadowRenderTasks::LightProjectionUpdateQueue.begin();
Itr != ShadowFacts::ShadowRenderTasks::LightProjectionUpdateQueue.end();
Itr++)
{
Source = *Itr;
NiNode* Node = Source->sourceNode;
TESObjectREFR* Object = Utilities::GetNodeObjectRef(Node);
if (Node && Object)
{
Swapper ProjDist(&SMRC_A38618);
Swapper ExtendDist(&SMRC_A31C70);
float Bound = Node->m_kWorldBound.radius;
float NewProjDistMul = 0.f;
float BaseRadius = Settings::kObjectTier2BoundRadius().f;
float MaxRadius = Settings::kObjectTier3BoundRadius().f;
float PerPart = (MaxRadius - BaseRadius) / 3.f;
float Part1 = BaseRadius + PerPart;
float Part2 = BaseRadius + PerPart * 2;
float Part3 = BaseRadius + PerPart * 3;
if (Bound < BaseRadius)
NewProjDistMul = 2.5f;
else if (Bound > BaseRadius && Bound < Part1)
NewProjDistMul = 2.6f;
else if (Bound > Part1 && Bound < Part2)
NewProjDistMul = 2.7f;
else if (Bound > Part2 && Bound < Part3)
NewProjDistMul = 2.8f;
if (NewProjDistMul)
{
ProjDist.Swap(NewProjDistMul);
SHADOW_DEBUG(Object, "Changed Projection Distance Multiplier to %f", NewProjDistMul);
}
float NewExtendDistMul = 1.5f;
if (Bound < MaxRadius)
{
ExtendDist.Swap(NewExtendDistMul);
SHADOW_DEBUG(Object, "Changed Extend Distance Multiplier to %f", NewExtendDistMul);
}
thisCall<void>(0x007D2280, Source, AuxParam);
}
else
thisCall<void>(0x007D2280, Source, AuxParam);
}
// clear the queue as it's populated every single frame
ShadowFacts::ShadowRenderTasks::LightProjectionUpdateQueue.clear();
}
void __stdcall ShadowRenderConstantHotSwapper::HandleShadowMapRenderStage( ShadowSceneLight* Source, void* AuxParam )
{
SME_ASSERT(Source);
NiNode* Node = Source->sourceNode;
TESObjectREFR* Object = Utilities::GetNodeObjectRef(Node);
if (Node && Object)
{
Swapper SamplingScale(&SRC_A91280);
float Bound = Node->m_kWorldBound.radius;
float NewSampScale = 220.f;
float BaseRadius = Settings::kObjectTier2BoundRadius().f;
float MaxRadius = Settings::kObjectTier3BoundRadius().f;
if (Bound < MaxRadius)
{
SamplingScale.Swap(NewSampScale);
SHADOW_DEBUG(Object, "Changed Sampling Scale Multiplier to %f", NewSampScale);
}
thisCall<void>(0x007D46C0, Source, AuxParam);
}
else
thisCall<void>(0x007D46C0, Source, AuxParam);
}
_DefineHookHdlr(SwapLightProjectionStageConstants, 0x004078FA);
_DefineHookHdlr(SwapShadowMapRenderStageConstants, 0x007D59D2);
_DefinePatchHdlr(FixSSLLightSpaceProjectionStack, 0x007D2E85 + 1);
// mighty iffy about this one
// the method call being hooked is a weird one, mucking about with the stack
// would be safer to wrap the org call with a prolog/epilog handler
#define _hhName SwapLightProjectionStageConstants
_hhBegin()
{
_hhSetVar(Retn, 0x00407906);
__asm
{
mov [eax + 0x8], edi
mov ecx, esi
pushad
push 0
push ecx
call ShadowRenderConstantHotSwapper::HandleLightProjectionStage
popad
add esp, 0xC // restore the stack pointer
jmp _hhGetVar(Retn)
}
}
#define _hhName SwapShadowMapRenderStageConstants
_hhBegin()
{
_hhSetVar(Retn, 0x007D59D8);
__asm
{
pushad
push eax
push ecx
call ShadowRenderConstantHotSwapper::HandleShadowMapRenderStage
popad
jmp _hhGetVar(Retn)
}
}
void Patch( void )
{
_MemHdlr(SwapLightProjectionStageConstants).WriteJump();
_MemHdlr(FixSSLLightSpaceProjectionStack).WriteUInt16(0x4);
_MemHdlr(SwapShadowMapRenderStageConstants).WriteJump();
SRC_B258E8.AddPatchLocation(0x007D4BA0 + 2);
SRC_B258EC.AddPatchLocation(0x007D4BB4 + 2);
SRC_B258F0.AddPatchLocation(0x007D4BC0 + 2);
ShadowRenderConstantRegistry::GetSingleton()->Initialize();
}
void Initialize( void )
{
ShadowRenderConstantRegistry::GetSingleton()->Load();
}
}