From c50c0a2c1dafab1dd1e0dd4aa47293a5d976f826 Mon Sep 17 00:00:00 2001 From: Silent Date: Thu, 23 Apr 2015 18:41:20 +0200 Subject: [PATCH] Optimized NVC shader --- SilentPatch/nvc.fx | 10 +++-- SilentPatchSA/SilentPatchSA.cpp | 61 ++++++++++++++++++++--------- SilentPatchSA/SilentPatchSA.vcxproj | 2 +- 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/SilentPatch/nvc.fx b/SilentPatch/nvc.fx index 3ea8b4f..713d6e0 100644 --- a/SilentPatch/nvc.fx +++ b/SilentPatch/nvc.fx @@ -15,15 +15,17 @@ struct VS_OUTPUT float2 fEnvVars : register(c0); float4 AmbientLight : register(c1); -float4x4 world : register(c2); -float4x4 view : register(c6); -float4x4 proj : register(c10); +float4x4 worldViewProj : register(c2); +//float4x4 world : register(c2); +//float4x4 view : register(c6); +//float4x4 proj : register(c10); VS_OUTPUT NVC_vertex_shader( in VS_INPUT In ) { VS_OUTPUT Out; - Out.Position = mul(proj, mul(view, mul(world, In.Position))); + //Out.Position = mul(proj, mul(view, mul(world, In.Position))); + Out.Position = mul(worldViewProj, In.Position); Out.Texture = In.Texture; Out.Color.rgb = lerp(In.DayColor, In.NightColor, fEnvVars[0]).rgb + AmbientLight.rgb; diff --git a/SilentPatchSA/SilentPatchSA.cpp b/SilentPatchSA/SilentPatchSA.cpp index 3a3338e..aa1cf2f 100644 --- a/SilentPatchSA/SilentPatchSA.cpp +++ b/SilentPatchSA/SilentPatchSA.cpp @@ -686,9 +686,11 @@ char* GetMyDocumentsPath() return cUserFilesPath; } +#include + static void* pNVCShader = nullptr; static bool bRenderNVC = false; -static RpAtomic* pRenderedAtomic; +static bool bXMSupported; bool ShaderAttach() { @@ -704,6 +706,8 @@ bool ShaderAttach() RwD3D9CreateVertexShader(shader, reinterpret_cast(&pNVCShader)); FreeResource(shader); + + bXMSupported = XMVerifyCPUSupport(); return true; } return false; @@ -746,13 +750,46 @@ void SetShader(RxD3D9InstanceData* pInstData) //_rwD3D9VSSetActiveWorldMatrix(RwFrameGetMatrix(RpAtomicGetFrame(pRenderedAtomic))); //_rwD3D9VSGetComposedTransformMatrix(&outMat); - D3DMATRIX worldMat, viewMat, projMat; + XMMATRIX worldMat, viewMat, projMat; + XMMATRIX worldViewProjMat; _RwD3D9GetTransform(D3DTS_WORLD, &worldMat); _RwD3D9GetTransform(D3DTS_VIEW, &viewMat); _RwD3D9GetTransform(D3DTS_PROJECTION, &projMat); - RwD3D9SetVertexShaderConstant(2, &worldMat, 4); - RwD3D9SetVertexShaderConstant(6, &viewMat, 4); - RwD3D9SetVertexShaderConstant(10, &projMat, 4); + + if ( bXMSupported ) + { + worldViewProjMat = XMMatrixMultiply(XMMatrixMultiply(worldMat, viewMat), projMat); + } + else + { + XMMATRIX tempMat; + ZeroMemory(&worldViewProjMat, sizeof(worldViewProjMat)); + ZeroMemory(&tempMat, sizeof(tempMat)); + + for( int i = 0; i < 4; i++ ) + { + for( int j = 0; j < 4; j++ ) + { + for(int x = 0; x < 4; x++) + tempMat.m[i][j] += worldMat.m[i][x] * viewMat.m[x][j]; + } + } + + for( int i = 0; i < 4; i++ ) + { + for( int j = 0; j < 4; j++ ) + { + for(int x = 0; x < 4; x++) + worldViewProjMat.m[i][j] += tempMat.m[i][x] * projMat.m[x][j]; + } + } + + } + + //RwD3D9SetVertexShaderConstant(2, &worldMat, 4); + //RwD3D9SetVertexShaderConstant(6, &viewMat, 4); + //RwD3D9SetVertexShaderConstant(10, &projMat, 4); + RwD3D9SetVertexShaderConstant(2, &worldViewProjMat, 4); RwD3D9SetVertexShaderConstant(0, fEnvVars, 1); RwD3D9SetVertexShaderConstant(1, AmbientLight, 1); @@ -761,17 +798,6 @@ void SetShader(RxD3D9InstanceData* pInstData) RwD3D9SetVertexShader(pInstData->vertexShader); } -static void* HijackAtomic_JumpBack = AddressByVersion(0x5D6480, 0x5D6C60, 0x5F2C80); -void __declspec(naked) HijackAtomic() -{ - _asm - { - mov eax, [esp+8] - mov pRenderedAtomic, eax - jmp HijackAtomic_JumpBack - } -} - void __declspec(naked) SetShader2() { _asm @@ -1726,7 +1752,6 @@ BOOL InjectDelayedPatches_10() InjectHook(0x5D637B, HijackEsi, PATCH_JUMP); InjectHook(0x5BF3A1, ShaderAttach); InjectHook(0x53D910, ShaderDetach); - Patch(0x5D67F4, HijackAtomic); Patch(0x5D7200, 0xC3); Patch(0x5D67BB, 0x6890); Patch(0x5D67D7, 0x6890); @@ -1935,7 +1960,6 @@ BOOL InjectDelayedPatches_11() InjectHook(0x5D6B5B, HijackEsi, PATCH_JUMP); //InjectHook(0x5BF3A1, ShaderAttach); InjectHook(0x53DDB0, ShaderDetach); - Patch(0x5D6FD4, HijackAtomic); Patch(0x5D79E0, 0xC3); Patch(0x5D6F9B, 0x6890); Patch(0x5D6FB7, 0x6890); @@ -2150,7 +2174,6 @@ BOOL InjectDelayedPatches_Steam() InjectHook(0x5F2B7A, HijackEsi, PATCH_JUMP); InjectHook(0x5DE5A1, ShaderAttach); InjectHook(0x550070, ShaderDetach); - Patch(0x5F3004, HijackAtomic); Patch(0x5F3760, 0xC3); Patch(0x5F2FCB, 0x6890); Patch(0x5F2FE7, 0x6890); diff --git a/SilentPatchSA/SilentPatchSA.vcxproj b/SilentPatchSA/SilentPatchSA.vcxproj index fc4509a..84f7318 100644 --- a/SilentPatchSA/SilentPatchSA.vcxproj +++ b/SilentPatchSA/SilentPatchSA.vcxproj @@ -81,7 +81,7 @@ copy /y "$(TargetPath)" "D:\gry\GTA San Andreas clean\scripts\newsteam_r2_lowvio SILENTPATCH_SA_VER;NDEBUG;%(PreprocessorDefinitions) true MultiThreaded - D:\RWSDK\Graphics\rwsdk\include\d3d9;..\SilentPatch + D:\RWSDK\Graphics\rwsdk\include\d3d9;$(DXSDK_DIR)\Include;..\SilentPatch Use StdAfxSA.h