-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDebugCellShaders.h
255 lines (221 loc) · 7.4 KB
/
DebugCellShaders.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
#pragma once
#include "NiNodes.h"
enum DebugRenderModes {
DEBUG_NONE = 0,
DEBUG_LIGHT_COUNT = 1,
DEBUG_PASS_COUNT = 2,
DEBUG_TEXTURE_COUNT = 4,
};
#define LightingFlags *(UInt32*)0xF23C28
#define eDebugRenderMode *(DebugRenderModes*)0xF23C04
#define ShaderConstant_AmbientColor (*(NiColorAlpha*)0xF244E0)
#define ShaderConstant_AmbientColor30 (*(NiColorAlpha*)0xF2B988)
#define BSBatchRenderer_pCurrentPass (*(BSShaderProperty::RenderPass**)0xF23C30)
static const NiColorAlpha LightCountDebugColors[] = {
NiColorAlpha(0.0f, 1.0f, 0.0f, 1.0f),
NiColorAlpha(0.0f, 0.8f, 0.2f, 1.0f),
NiColorAlpha(0.0f, 0.6f, 0.4f, 1.0f),
NiColorAlpha(0.0f, 0.4f, 0.6f, 1.0f),
NiColorAlpha(0.0f, 0.2f, 0.8f, 1.0f),
NiColorAlpha(0.0f, 0.0f, 1.0f, 1.0f),
NiColorAlpha(0.2f, 0.0f, 0.8f, 1.0f),
NiColorAlpha(0.4f, 0.0f, 0.6f, 1.0f),
NiColorAlpha(0.6f, 0.0f, 0.4f, 1.0f),
NiColorAlpha(0.8f, 0.0f, 0.2f, 1.0f),
NiColorAlpha(1.0f, 0.0f, 0.0f, 1.0f),
};
static const NiColorAlpha PassCountDebugColors[] = {
NiColorAlpha(0.0f, 1.0f, 0.0f, 1.0f),
NiColorAlpha(0.0f, 0.875f, 0.125f, 1.0f),
NiColorAlpha(0.0f, 0.75f, 0.25f, 1.0f),
NiColorAlpha(0.0f, 0.625f, 0.375f, 1.0f),
NiColorAlpha(0.0f, 0.5f, 0.5f, 1.0f),
NiColorAlpha(0.0f, 0.375f, 0.625f, 1.0f),
NiColorAlpha(0.0f, 0.25f, 0.75f, 1.0f),
NiColorAlpha(0.0f, 0.125f, 0.875f, 1.0f),
NiColorAlpha(0.0f, 0.0f, 1.0f, 1.0f),
NiColorAlpha(0.125f, 0.0f, 0.875f, 1.0f),
NiColorAlpha(0.25f, 0.0f, 0.75f, 1.0f),
NiColorAlpha(0.375f, 0.0f, 0.625f, 1.0f),
NiColorAlpha(0.5f, 0.0f, 0.5f, 1.0f),
NiColorAlpha(0.625f, 0.0f, 0.375f, 1.0f),
NiColorAlpha(0.75f, 0.0f, 0.25f, 1.0f),
NiColorAlpha(0.875f, 0.0f, 0.125f, 1.0f),
NiColorAlpha(1.0f, 0.0f, 0.0f, 1.0f),
};
_declspec(naked) void DebugShaderHook925E80()
{
static const UInt32 debugAddr = 0x925E8D;
static const UInt32 retnAddr = 0x925ECE;
_asm
{
mov eax, [0xF23C04] // g_renderWindowShaderDebugFlags
cmp eax, 1
jl noDebugFlags
cmp eax, 6
jg noDebugFlags
jmp debugAddr
noDebugFlags :
mov ecx, [0xF23C30]
jmp retnAddr
}
}
_declspec(naked) void DebugShaderHook9261D0()
{
static const UInt32 debugAddr = 0x9261EB;
static const UInt32 retnAddr = 0x926229;
_asm
{
mov eax, [0xF23C04] // g_renderWindowShaderDebugFlags
cmp eax, 1
jl noDebugFlags
cmp eax, 6
jg noDebugFlags
jmp debugAddr
noDebugFlags :
mov esi, [edi + 0xF8]
jmp retnAddr
}
}
_declspec(naked) void DebugShaderHook952F60()
{
static const UInt32 debugAddr = 0x9538D5;
static const UInt32 retnAddr = 0x95374A;
_asm
{
je debugFlags
mov eax, [0xF23C04] // g_renderWindowShaderDebugFlags
test eax, eax
jne debugFlags
jmp retnAddr
debugFlags :
jmp debugAddr
}
}
_declspec(naked) void DebugShaderHook965B40()
{
static const UInt32 retnAddr = 0x965B78;
_asm
{
mov eax, [0xF23C04] // g_renderWindowShaderDebugFlags
cmp eax, 6
ja debugFlags
cmp edi, 0x1BD
jb noDebug
cmp edi, 0x1E1
jbe debugFlags
noDebug:
mov ecx, [0xF983E4]
mov edx, [ecx + 0x24]
mov eax, [edx]
test eax, eax
je jmp1
mov ecx, [0xF23F80]
mov[eax + 8], ecx
jmp1:
mov edx, [0xF983E4]
mov eax, [edx + 0x24]
mov eax, [eax + 4]
test eax, eax
je done
mov ecx, [0xF23F60]
mov[eax + 8], ecx
jmp done
debugFlags:
mov edx, [esi]
mov eax, [edx + 0x13C]
push ebx
push edi
call eax
done:
jmp retnAddr
}
}
// TODO - Wall
// Attach bAutoLightWarnings to some button or a hotkey
// Finish Lighting30Shader variant
static bool bRenderModeChanged = false;
static DebugRenderModes eLastRenderMode = DEBUG_NONE;
// Returns false if debug mode is active
bool SetDebugColors(NiColorAlpha* apAmbient, BSShaderProperty* apShaderProp, NiGeometry* apGeometry) {
UInt32 uiLightCount = ThisCall(0x925830, apShaderProp); // GetActiveLightCount
bool bAutoColor = config.bAutoLightWarnings && (uiLightCount > 6) && eDebugRenderMode != DEBUG_LIGHT_COUNT;
if (eLastRenderMode != eDebugRenderMode) {
bRenderModeChanged = true;
eLastRenderMode = eDebugRenderMode;
}
if (eDebugRenderMode > 0 || bAutoColor) {
if (eDebugRenderMode == DEBUG_LIGHT_COUNT || bAutoColor) {
uiLightCount = max(min(uiLightCount, 10), 0);
*apAmbient = LightCountDebugColors[uiLightCount];
if (bAutoColor) {
apAmbient->Scale(1.5f);
}
}
else if (eDebugRenderMode == DEBUG_PASS_COUNT) {
UInt16 usPasses = apShaderProp->GetNumberofPasses(apGeometry);
*apAmbient = PassCountDebugColors[min(16, max(0, usPasses))];
}
if (bRenderModeChanged) {
LightingFlags = 1;
bRenderModeChanged = false;
}
return false;
}
else {
return true;
}
}
void inline HandleDebugRender(DWORD* apShader, BSShaderProperty* apShaderProperty, DWORD* apRenderPass, NiGeometry* apGeometry, NiMaterialProperty* apMaterialProperty, bool abIs30Shader) {
static NiColorAlpha* pAmbientColor = abIs30Shader ? &ShaderConstant_AmbientColor30 : &ShaderConstant_AmbientColor;
if (SetDebugColors(pAmbientColor, apShaderProperty, apGeometry)) {
if (abIs30Shader) {
StdCall(0x9661C0, apMaterialProperty, apShaderProperty); // Lighting30Shader::UpdateAmbientColor
}
else {
StdCall(0x94DA90, apShaderProperty, apRenderPass, apGeometry, apMaterialProperty); // ShadowLightShader::UpdateAmbientColor
}
if (bRenderModeChanged) {
LightingFlags |= (1 | 2 | 8 | 20);
bRenderModeChanged = false;
}
}
}
void __stdcall ShadowLightShader_UpdateAmbientColorEx(BSShaderProperty* apShaderProperty, DWORD* apRenderPass, NiGeometry* apGeometry, NiMaterialProperty* apMaterialProperty) {
HandleDebugRender(nullptr, apShaderProperty, apRenderPass, apGeometry, apMaterialProperty, false);
}
void __stdcall Lighting30Shader_UpdateAmbientColor30Ex(NiMaterialProperty* apMaterialProperty, BSShaderProperty* apShaderProperty) {
HandleDebugRender(nullptr, apShaderProperty, nullptr, nullptr, apMaterialProperty, true);
}
void __fastcall BSShaderPPLightingProperty__AddPass_Opt(void* apThis, void*, void* apGeometry, void* apLight0, UInt16* apusPassCount, BOOL abAddPass, bool* abEnable, bool abSkinned, bool abGlowMap, bool abSpecular, bool bFacegen, bool abHasLights) {
if (abAddPass == 0) {
(*apusPassCount)++;
return;
}
ThisCall(0x97FDD0, apThis, apGeometry, apLight0, apusPassCount, abAddPass, abEnable, abSkinned, abGlowMap, abSpecular, bFacegen, abHasLights);
}
void __fastcall ShadowLightShader__SetAmbientColor(void* apThis) {
static NiColorAlpha* pAmbientColor = &ShaderConstant_AmbientColor;
NiGeometry* pGeometry = BSBatchRenderer_pCurrentPass->pGeometry;
BSShaderProperty* pShaderProperty = (BSShaderProperty*)pGeometry->m_kProperties.m_aspProps[NiPropertyState::SHADE];
if (SetDebugColors(pAmbientColor, pShaderProperty, pGeometry)) {
ThisCall(0x94E5A0, apThis);
}
}
void RestoreRenderWindowDebugShaders()
{
// Needs more decoding. They are for 3.0 shaders which are not that important for now - Wall
//WriteRelJump(0x925EC8, UInt32(DebugShaderHook925E80));
//WriteRelJump(0x926223, UInt32(DebugShaderHook9261D0));
//WriteRelJump(0x953744, UInt32(DebugShaderHook952F60));
//WriteRelJump(0x965B6C, UInt32(DebugShaderHook965B40));
WriteRelCall(0x94FBAB, UInt32(ShadowLightShader_UpdateAmbientColorEx));
//WriteRelCall(0x966625, UInt32(Lighting30Shader_UpdateAmbientColor30Ex));
WriteRelCall(0x94FD81, UInt32(ShadowLightShader__SetAmbientColor));
WriteRelCall(0x94FFC1, UInt32(ShadowLightShader__SetAmbientColor));
// Crash fix
WriteRelCall(0x98158C, UInt32(BSShaderPPLightingProperty__AddPass_Opt));
// Prevent shader package destruction (allows shader reloading)
SafeWrite8(0x9001F8, 0xEB);
SafeWrite8(0x9002AA, 0xEB);
}