Skip to content

Commit aa44937

Browse files
authored
Fix cemetery water rendering (#1183)
1 parent d97a4b1 commit aa44937

File tree

3 files changed

+110
-17
lines changed

3 files changed

+110
-17
lines changed

Common/Settings.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
visit(DisableSafeMode, true) \
3434
visit(DisableScreenSaver, true) \
3535
visit(DisplayModeOption, true) \
36+
visit(DogRoomEnhanced, true) \
3637
visit(DynamicResolution, true) \
3738
visit(EnableDebugOverlay, true) \
3839
visit(EnableEnhancedMouse, true) \
@@ -203,7 +204,9 @@
203204
visit(water_spec_mult_strange_area, 0.017f) \
204205
visit(water_spec_mult_labyrinth, 0.017f) \
205206
visit(water_spec_mult_hotel, 0.05f) \
206-
visit(water_spec_uv_mult_hotel, 0.45f)
207+
visit(water_spec_uv_mult_hotel, 0.45f) \
208+
visit(water_spec_mult_cemetery, 0.015f) \
209+
visit(water_spec_uv_mult_cemetery, 1.0f)
207210

208211
#define VISIT_STR_SETTINGS(visit) \
209212
visit(CustomModFolder, "") \
@@ -237,6 +240,7 @@
237240
visit(DisableLoadingPressReturnMessages) \
238241
visit(DisableLogging) \
239242
visit(DisableRedCross) \
243+
visit(DogRoomEnhanced) \
240244
visit(EnableDebugOverlay) \
241245
visit(EnableInfoOverlay) \
242246
visit(EnableScreenshots) \
@@ -283,6 +287,8 @@
283287
visit(water_spec_mult_labyrinth) \
284288
visit(water_spec_mult_hotel) \
285289
visit(water_spec_uv_mult_hotel) \
290+
visit(water_spec_mult_cemetery) \
291+
visit(water_spec_uv_mult_cemetery) \
286292
visit(WrapperType)
287293

288294
typedef enum _SCREENMODE {
@@ -390,3 +396,5 @@ void __stdcall ParseCallback(char* lpName, char* lpValue, void*);
390396
void LogSettings();
391397
void UpdateConfigDefaults();
392398
void UpdateScaleResolution();
399+
400+

Patches/WaterEnhancement.cpp

Lines changed: 93 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ static DWORD* g_vsHandles = nullptr;
3333
#define WATER_TEXTURE_SLOT_DUDV 2
3434
#define WATER_TEXTURE_SLOT_CAUSTICS 3
3535

36-
#define WATER_DUDV_SCALE_PS_CB_SLOT 5
37-
#define WATER_DUDV_SPEC_SCALE_PS_CB_SLOT 6
38-
#define WATER_SPEC_MULT_PS_CB_SLOT 7
36+
#define WATER_DUDV_SCALE_PS_CB_SLOT 4
37+
#define WATER_DUDV_SPEC_SCALE_PS_CB_SLOT 5
38+
#define WATER_SPEC_MULT_PS_CB_SLOT 6
39+
#define WATER_FOG_COLOUR_PS_CB_SLOT 7
3940

4041
#define WATER_UVADD_VS_CB_SLOT 90
4142
#define WATER_UVMUL_VS_CB_SLOT 91
4243
#define WATER_WORLD_VS_CB_SLOT 92
44+
#define WATER_COLOUR_VS_CB_SLOT 93
4345

4446
/*
4547
vs.1.1
@@ -84,8 +86,9 @@ add oT1, r1, c90
8486
mov oT2, r0
8587
// scale caustics UVs
8688
mul oT3, r1, c91
87-
// pass-through vertex colour
89+
// pass-through vertex colour (contains fog)
8890
mov oD0, v5
91+
8992
*/
9093
DWORD g_WaterPondVSBytecode[] = {
9194
0xfffe0101, 0x0009fffe, 0x58443344, 0x68532038,
@@ -113,7 +116,7 @@ texcrd r4.xyz, t0
113116
texcrd r5.xyz, t3
114117
// sample dudv map, restore and rescale
115118
texld r2, t1
116-
mul r1, c5, r2_bx2
119+
mul r1, c4, r2_bx2
117120
// fill the void
118121
mov r3.zw, c0
119122
mov r4.zw, c0
@@ -123,7 +126,7 @@ mad r3, r3, c1, c0
123126
// disturb projected uv
124127
add r3, r3, r1
125128
// disturb caustics uv
126-
mad r5, r2_bx2, c6, r5
129+
mad r5, r2_bx2, c5, r5
127130
// pass along the disturbed base texture coords
128131
add r2, r4, r1
129132
@@ -140,7 +143,7 @@ mul r0, r0, v0
140143
// blend them
141144
lrp_sat r0, r0.w, r0, r1
142145
// add modulated caustics
143-
mad_sat r0, r3, c7, r0
146+
mad_sat r0, r3, c6, r0
144147
// fill alpha
145148
mov r0.w, c0
146149
*/
@@ -154,26 +157,95 @@ DWORD g_WaterPSBytecode[] = {
154157
0x80030003, 0xbaf40002, 0x00000040, 0x80070004,
155158
0xb0e40000, 0x00000040, 0x80070005, 0xb0e40003,
156159
0x00000042, 0x800f0002, 0xb0e40001, 0x00000005,
157-
0x800f0001, 0xa0e40005, 0x84e40002, 0x00000001,
160+
0x800f0001, 0xa0e40004, 0x84e40002, 0x00000001,
158161
0x800c0003, 0xa0e40000, 0x00000001, 0x800c0004,
159162
0xa0e40000, 0x00000001, 0x800c0005, 0xa0e40000,
160163
0x00000004, 0x800f0003, 0x80e40003, 0xa0e40001,
161164
0xa0e40000, 0x00000002, 0x800f0003, 0x80e40003,
162165
0x80e40001, 0x00000004, 0x800f0005, 0x84e40002,
163-
0xa0e40006, 0x80e40005, 0x00000002, 0x800f0002,
166+
0xa0e40005, 0x80e40005, 0x00000002, 0x800f0002,
164167
0x80e40004, 0x80e40001, 0x0000fffd, 0x00000042,
165168
0x800f0000, 0x80e40002, 0x00000042, 0x800f0001,
166169
0x80e40003, 0x00000042, 0x800f0003, 0x80e40005,
167170
0x00000005, 0x800f0000, 0x80e40000, 0x90e40000,
168171
0x00000012, 0x801f0000, 0x80ff0000, 0x80e40000,
169172
0x80e40001, 0x00000004, 0x801f0000, 0x80e40003,
170-
0xa0e40007, 0x80e40000, 0x00000001, 0x80080000,
173+
0xa0e40006, 0x80e40000, 0x00000001, 0x80080000,
174+
0xa0e40000, 0x0000ffff
175+
};
176+
177+
/*
178+
ps.1.4
179+
def c0, 0.5, 0.5, 1, 1
180+
def c1, 0.5, -0.5, 1, 1
181+
182+
// calc projected uv
183+
texcrd r3.xy, t2_dw.xyw
184+
// save aside base texture coords
185+
texcrd r4.xyz, t0
186+
// save aside caustics texture coords
187+
texcrd r5.xyz, t3
188+
// sample dudv map, restore and rescale
189+
texld r2, t1
190+
mul r1, c4, r2_bx2
191+
// fill the void
192+
mov r3.zw, c0
193+
mov r4.zw, c0
194+
mov r5.zw, c0
195+
// [-1;1] -> [0;1]
196+
mad r3, r3, c1, c0
197+
// disturb projected uv
198+
add r3, r3, r1
199+
// disturb caustics uv
200+
mad r5, r2_bx2, c5, r5
201+
// pass along the disturbed base texture coords
202+
add r2, r4, r1
203+
204+
phase
205+
206+
// sample water texture
207+
texld r0, r2
208+
// sample refraction texture
209+
texld r1, r3
210+
// sample caustics texture
211+
texld r3, r5
212+
// blend water colour and refraction
213+
lrp r0, v0.w, v0, r1
214+
// add modulated caustics
215+
mad_sat r0.xyz, r3, c6, r0
216+
// fill alpha
217+
+ mov r0.w, c0
218+
*/
219+
DWORD g_WaterPondPSBytecode[] = {
220+
0xffff0104, 0x0009fffe, 0x58443344, 0x68532038,
221+
0x72656461, 0x73734120, 0x6c626d65, 0x56207265,
222+
0x69737265, 0x30206e6f, 0x0031392e, 0x00000051,
223+
0xa00f0000, 0x3f000000, 0x3f000000, 0x3f800000,
224+
0x3f800000, 0x00000051, 0xa00f0001, 0x3f000000,
225+
0xbf000000, 0x3f800000, 0x3f800000, 0x00000040,
226+
0x80030003, 0xbaf40002, 0x00000040, 0x80070004,
227+
0xb0e40000, 0x00000040, 0x80070005, 0xb0e40003,
228+
0x00000042, 0x800f0002, 0xb0e40001, 0x00000005,
229+
0x800f0001, 0xa0e40004, 0x84e40002, 0x00000001,
230+
0x800c0003, 0xa0e40000, 0x00000001, 0x800c0004,
231+
0xa0e40000, 0x00000001, 0x800c0005, 0xa0e40000,
232+
0x00000004, 0x800f0003, 0x80e40003, 0xa0e40001,
233+
0xa0e40000, 0x00000002, 0x800f0003, 0x80e40003,
234+
0x80e40001, 0x00000004, 0x800f0005, 0x84e40002,
235+
0xa0e40005, 0x80e40005, 0x00000002, 0x800f0002,
236+
0x80e40004, 0x80e40001, 0x0000fffd, 0x00000042,
237+
0x800f0000, 0x80e40002, 0x00000042, 0x800f0001,
238+
0x80e40003, 0x00000042, 0x800f0003, 0x80e40005,
239+
0x00000012, 0x800f0000, 0x90ff0000, 0x90e40000,
240+
0x80e40001, 0x00000004, 0x80170000, 0x80e40003,
241+
0xa0e40006, 0x80e40000, 0x40000001, 0x80080000,
171242
0xa0e40000, 0x0000ffff
172243
};
173244

174245
DWORD g_WaterVSHandle = 0;
175246
DWORD g_WaterPondVSHandle = 0;
176247
DWORD g_WaterPSHandle = 0;
248+
DWORD g_WaterPondPSHandle = 0;
177249

178250
IDirect3DTexture8* g_ScreenCopyTexture = nullptr;
179251
IDirect3DSurface8* g_ScreenCopySurface = nullptr;
@@ -336,6 +408,8 @@ static void GetWaterConstantsByRoom(D3DXVECTOR4& specMult, D3DXVECTOR4& specUvMu
336408
// Pond
337409
case R_FOREST_CEMETERY:
338410
dudvScale = { 0.005f, 0.005f, 0.005f, 0.005f };
411+
specMult = { water_spec_mult_cemetery, water_spec_mult_cemetery, water_spec_mult_cemetery, 0.0f };
412+
specUvMult = { water_spec_uv_mult_cemetery, water_spec_uv_mult_cemetery, water_spec_uv_mult_cemetery, water_spec_uv_mult_cemetery };
339413
break;
340414
// Pyramidhead submerge
341415
case R_APT_W_STAIRCASE_N:
@@ -411,7 +485,7 @@ HRESULT DrawWaterEnhanced(bool needToGrabScreenForWater, LPDIRECT3DDEVICE8 Devic
411485
Device->GetTexture(WATER_TEXTURE_SLOT_CAUSTICS, &tex2);
412486

413487
Device->SetVertexShader((roomID == R_FOREST_CEMETERY) ? g_WaterPondVSHandle : g_WaterVSHandle);
414-
Device->SetPixelShader(g_WaterPSHandle);
488+
Device->SetPixelShader((roomID == R_FOREST_CEMETERY) ? g_WaterPondPSHandle : g_WaterPSHandle);
415489

416490
Device->SetTexture(WATER_TEXTURE_SLOT_REFRACTION, g_ScreenCopyTexture);
417491
Device->SetTexture(WATER_TEXTURE_SLOT_DUDV, g_DuDvTexture);
@@ -454,6 +528,14 @@ HRESULT DrawWaterEnhanced(bool needToGrabScreenForWater, LPDIRECT3DDEVICE8 Devic
454528
const float worldScale = 1.0f / 3000.0f;
455529
D3DXVECTOR4 worldDiv = { worldScale, worldScale, worldScale, worldScale };
456530
Device->SetVertexShaderConstant(WATER_WORLD_VS_CB_SLOT, &worldDiv, 1);
531+
532+
D3DXVECTOR4 waterColour = { 0.32f, 0.32f, 0.32f, 0.35f };
533+
Device->SetVertexShaderConstant(WATER_COLOUR_VS_CB_SLOT, &waterColour, 1);
534+
535+
DWORD dwFogColour = 0u;
536+
Device->GetRenderState(D3DRS_FOGCOLOR, &dwFogColour);
537+
D3DXVECTOR4 fogColour(float((dwFogColour >> 16) & 0xFF) / 255.0f, float((dwFogColour >> 8) & 0xFF) / 255.0f, float(dwFogColour & 0xFF) / 255.0f, 1.0f);
538+
Device->SetPixelShaderConstant(WATER_FOG_COLOUR_PS_CB_SLOT, &fogColour, 1u);
457539
}
458540

459541
Device->SetPixelShaderConstant(WATER_DUDV_SCALE_PS_CB_SLOT, &dudvScale, 1u);

Wrappers/d3d8/IDirect3DDevice8.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ extern char* ResetTexNameAddr;
3939
extern DWORD g_WaterVSHandle;
4040
extern DWORD g_WaterPondVSHandle;
4141
extern DWORD g_WaterPSHandle;
42+
extern DWORD g_WaterPondPSHandle;
4243
extern DWORD g_WaterVSBytecode[];
4344
extern DWORD g_WaterPondVSBytecode[];
4445
extern DWORD g_WaterPSBytecode[];
46+
extern DWORD g_WaterPondPSBytecode[];
4547
extern DWORD vsDeclWater[];
4648
extern void WaterEnhancedReleaseScreenCopy();
4749
extern HRESULT DrawWaterEnhanced(bool needToGrabScreenForWater, LPDIRECT3DDEVICE8 ProxyInterface, LPDIRECT3DSURFACE8 pRenderTarget, D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, const void* pVertexStreamZeroData, UINT VertexStreamZeroStride);
@@ -1282,6 +1284,10 @@ HRESULT m_IDirect3DDevice8::CreatePixelShader(THIS_ CONST DWORD* pFunction, DWOR
12821284
{
12831285
ProxyInterface->CreatePixelShader(g_WaterPSBytecode, &g_WaterPSHandle);
12841286
}
1287+
if (!g_WaterPondPSHandle)
1288+
{
1289+
ProxyInterface->CreatePixelShader(g_WaterPondPSBytecode, &g_WaterPondPSHandle);
1290+
}
12851291

12861292
if (!g_GammaPSHandle)
12871293
{
@@ -1529,13 +1535,10 @@ HRESULT m_IDirect3DDevice8::DrawShadersAndScaledSurface()
15291535
}
15301536

15311537
ProxyInterface->SetPixelShader(0);
1532-
ProxyInterface->SetVertexShader(0);
1538+
ProxyInterface->SetVertexShader(D3DFVF_XYZRHW | D3DFVF_TEX1);
15331539
}
15341540

1535-
// Set vertex declaration
1536-
ProxyInterface->SetVertexShader(D3DFVF_XYZRHW | D3DFVF_TEX1);
1537-
1538-
if (IsScaledResolutionsEnabled())
1541+
if (IsScaledResolutionsEnabled() && !RestoreBrightnessSelector)
15391542
{
15401543
// Set stream source
15411544
ProxyInterface->SetStreamSource(0, ScaleVertexBuffer, sizeof(CUSTOMVERTEX_TEX1));

0 commit comments

Comments
 (0)