Skip to content

Commit

Permalink
Improvements & Fixes
Browse files Browse the repository at this point in the history
-Fix rendered decals alignment towards camera
-Correctly fix duplicating rendered vobs in G1
-Optimize a little bit OnVobMoved function
-Change the way to acquire textures for animated meshes
-Skip loading unneeded lightmap textures
-Fix problem where we can have locked 20 FPS
-Add gothic executable detector to make it possible to pack GD3D11 in monolithic way
  • Loading branch information
SaiyansKing committed Oct 27, 2021
1 parent f4c9997 commit 2c8e253
Show file tree
Hide file tree
Showing 20 changed files with 650 additions and 201 deletions.
52 changes: 26 additions & 26 deletions D3D11Engine/D3D11GraphicsEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,12 @@ XRESULT D3D11GraphicsEngine::OnResize( INT2 newSize ) {

/** Called when the game wants to render a new frame */
XRESULT D3D11GraphicsEngine::OnBeginFrame() {
if ( !m_isWindowActive && GetForegroundWindow() == OutputWindow ) {
// Just in case to check if somehow we didn't get informed the window got activated
m_isWindowActive = true;
UpdateClipCursor( OutputWindow );
}

Engine::GAPI->GetRendererState().RendererInfo.Timing.StartTotal();
if ( !m_isWindowActive && Engine::GAPI->GetRendererState().RendererSettings.EnableInactiveFpsLock ) {
m_FrameLimiter->SetLimit( 20 );
Expand Down Expand Up @@ -1400,8 +1406,8 @@ XRESULT D3D11GraphicsEngine::DrawSkeletalMesh( SkeletalVobInfo* vi,
for ( auto const& itm : dynamic_cast<SkeletalMeshVisualInfo*>(vi->VisualInfo)->SkeletalMeshes ) {
for ( auto& mesh : itm.second ) {
if ( zCMaterial* mat = itm.first ) {
if ( ActivePS && mat->GetTexture() ) {
zCTexture* tex = mat->GetAniTexture();
zCTexture* tex;
if ( ActivePS && (tex = mat->GetAniTexture()) != nullptr ) {
if ( tex->CacheIn( 0.6f ) == zRES_CACHED_IN )
tex->Bind( 0 );
else
Expand Down Expand Up @@ -2183,7 +2189,7 @@ XRESULT D3D11GraphicsEngine::DrawWorldMesh( bool noTextures ) {

for ( auto const& mesh : meshList ) {
zCTexture* texture;
if ( ( texture = mesh.first.Material->GetAniTexture() ) == nullptr ) continue;
if ( ( texture = mesh.first.Texture ) == nullptr ) continue;

if ( texture->HasAlphaChannel() )
continue; // Don't pre-render stuff with alpha channel
Expand Down Expand Up @@ -3697,9 +3703,8 @@ XRESULT D3D11GraphicsEngine::DrawPolyStrips( bool noTextures ) {
ActivePS->GetConstantBuffer()[2]->BindToPixelShader( 2 );

for ( auto it = polyStripInfos.begin(); it != polyStripInfos.end(); it++ ) {

zCMaterial* mat = it->second.material;
zCTexture* tx = mat->GetAniTexture();
zCTexture* tx = it->first;

const std::vector<ExVertexStruct>& vertices = it->second.vertices;

Expand All @@ -3720,7 +3725,7 @@ XRESULT D3D11GraphicsEngine::DrawPolyStrips( bool noTextures ) {
MyDirectDrawSurface7* surface = tx->GetSurface();
ID3D11ShaderResourceView* srv[3];

BindShaderForTexture( mat->GetAniTexture(), false, mat->GetAlphaFunc() );
BindShaderForTexture( tx, false, mat->GetAlphaFunc() );

// Get diffuse and normalmap
srv[0] = surface->GetEngineTexture()->GetShaderResourceView().Get();
Expand Down Expand Up @@ -5090,29 +5095,24 @@ void D3D11GraphicsEngine::DrawDecalList( const std::vector<zCVob*>& decals,
-d->GetDecalSettings()->DecalSize.y * 2, 1 ) );

if ( alignment == zVISUAL_CAM_ALIGN_YAW ) {
// Rotate the FX towards the camera
// TODO: some candle flames (and other pfx?) are invisible / very thin when done this way.

XMFLOAT3 decalPos = decals[i]->GetPositionWorld();
float angle = atan2( decalPos.x - camPos.x, decalPos.z - camPos.z ) * 180 * XM_1DIVPI;
world *= XMMatrixTranspose( XMMatrixRotationY( XMConvertToRadians( angle ) ) );
}

XMMATRIX mat = view * world;

// E.g battle focus borders
if ( alignment == zVISUAL_CAM_ALIGN_FULL ) {
for ( int x = 0; x < 3; x++ ) {
for ( int y = 0; y < 3; y++ ) {
if ( x == y )
mat.r[x].m128_f32[y] = 1.0f;
else
mat.r[x].m128_f32[y] = 0.0f;
}
}
float angle = atan2( decalPos.x - camPos.x, decalPos.z - camPos.z );
XMMATRIX rotationVector = XMMatrixTranspose( XMMatrixRotationY( angle ) );
//world *= rotationVector;

// We only need to change rotation vectors - maintain old W-coordinates
XMStoreFloat3( reinterpret_cast<XMFLOAT3*>(&world.r[0]), rotationVector.r[0] );
XMStoreFloat3( reinterpret_cast<XMFLOAT3*>(&world.r[1]), rotationVector.r[1] );
XMStoreFloat3( reinterpret_cast<XMFLOAT3*>(&world.r[2]), rotationVector.r[2] );
} else if ( alignment == zVISUAL_CAM_ALIGN_FULL ) {
XMFLOAT3 decalPos = decals[i]->GetPositionWorld();
world = XMMatrixIdentity();
reinterpret_cast<XMFLOAT4*>(&world.r[0])->w = decalPos.x;
reinterpret_cast<XMFLOAT4*>(&world.r[1])->w = decalPos.y;
reinterpret_cast<XMFLOAT4*>(&world.r[2])->w = decalPos.z;
}

mat = mat * offset * scale;
XMMATRIX mat = view * world * offset * scale;

ParticleInstanceInfo ii;
ii.scale = float2( 50, 50 );
Expand Down
3 changes: 1 addition & 2 deletions D3D11Engine/DLLMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ extern "C" {
}

static HINSTANCE hLThis = 0;
static HINSTANCE hDDRAW = 0;

static HRESULT( WINAPI* DirectDrawCreateEx_t )(GUID FAR* lpGuid, LPVOID* lplpDD, REFIID iid, IUnknown FAR* pUnkOuter);
typedef void (WINAPI* DirectDrawSimple)();
Expand Down Expand Up @@ -288,7 +287,7 @@ BOOL WINAPI DllMain( HINSTANCE hInst, DWORD reason, LPVOID ) {
Engine::OnShutDown();

CoUninitialize();
FreeLibrary( hDDRAW );
FreeLibrary( ddraw.dll );

LogInfo() << "DDRAW Proxy DLL signing off.\n";
}
Expand Down
30 changes: 10 additions & 20 deletions D3D11Engine/GothicAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1132,13 +1132,10 @@ void GothicAPI::OnVobMoved( zCVob* vob ) {
auto it = VobMap.find( vob );
if ( it != VobMap.end() ) {
VobInfo* vi = it->second;
#ifdef BUILD_GOTHIC_1_08k
// Check if the transform changed, since G1 calls this function over and over again
if ( checkMatrix( vob->GetWorldMatrixXM(), XMLoadFloat4x4( &vi->WorldMatrix ) ) ) {
// No actual change
return;
}
#endif

if ( !vi->ParentBSPNodes.empty() ) {
// Move vob into the dynamic list, if not already done
Expand Down Expand Up @@ -1280,13 +1277,11 @@ void GothicAPI::OnVisualDeleted( zCVisual* visual ) {
void GothicAPI::DrawMeshInfo( zCMaterial* mat, MeshInfo* msh ) {
// Check for material and bind the texture if it exists
if ( mat ) {
if ( mat->GetTexture() ) {
// Setup alphatest //TODO: This has to be done earlier!
if ( mat->GetAlphaFunc() == zRND_ALPHA_FUNC_TEST )
RendererState.GraphicsState.FF_GSwitches |= GSWITCH_ALPHAREF;
else
RendererState.GraphicsState.FF_GSwitches &= ~GSWITCH_ALPHAREF;
}
// Setup alphatest //TODO: This has to be done earlier!
if ( mat->GetAlphaFunc() == zRND_ALPHA_FUNC_TEST )
RendererState.GraphicsState.FF_GSwitches |= GSWITCH_ALPHAREF;
else
RendererState.GraphicsState.FF_GSwitches &= ~GSWITCH_ALPHAREF;
}

if ( !msh->MeshIndexBuffer ) {
Expand Down Expand Up @@ -1732,9 +1727,11 @@ void GothicAPI::DrawSkeletalMeshVob( SkeletalVobInfo* vi, float distance, bool u
std::vector<XMFLOAT4X4> transforms;
model->GetBoneTransforms( &transforms, vi->Vob );

// Update attachments
model->UpdateAttachedVobs();
model->UpdateMeshLibTexAniState();
if ( updateState ) {
// Update attachments
model->UpdateAttachedVobs();
model->UpdateMeshLibTexAniState();
}

if ( !((SkeletalMeshVisualInfo*)vi->VisualInfo)->SkeletalMeshes.empty() ) {
Engine::GraphicsEngine->DrawSkeletalMesh( vi, transforms, fatness );
Expand Down Expand Up @@ -2761,13 +2758,6 @@ void GothicAPI::CollectVisibleVobs( std::vector<VobInfo*>& vobs, std::vector<Vob
if ( Engine::GAPI->GetRendererState().RendererSettings.DrawVOBs ) {
float dist;
for ( VobInfo* it : DynamicallyAddedVobs ) {
#ifdef BUILD_GOTHIC_1_08k
// TODO: This should not be needed
if ( it->Vob->GetHomeWorld() != oCGame::GetGame()->_zCSession_world ) {
removeList.push_back( it );
continue;
}
#endif
// Get distance to this vob
XMStoreFloat( &dist, DirectX::XMVector3Length( camPos - it->Vob->GetPositionWorldXM() ) );
// Draw, if in range
Expand Down
10 changes: 6 additions & 4 deletions D3D11Engine/GothicMemoryLocations1_08k.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ struct GothicMemoryLocations {
static const unsigned int DoFrameActivity = 0x006DCC80;
static const unsigned int GetBBoxLocal = 0x005EDCF0;
static const unsigned int Offset_HomeWorld = 0x0A8;
static const unsigned int Offset_Type = 0x0A0;

static const unsigned int Offset_Flags = 0xE4;
static const unsigned int MASK_ShowVisual = 0x1;
static const unsigned int Offset_CameraAlignment = 0x110;
static const unsigned int Offset_CameraAlignment = 0xF0;
static const unsigned int SHIFTLR_CameraAlignment = 0x1E;

static const unsigned int Destructor = 0x005D32A0;
Expand Down Expand Up @@ -262,6 +263,7 @@ struct GothicMemoryLocations {
static const unsigned int Constructor = 0x0054CFC0;
static const unsigned int Destructor = 0x0054D230;
static const unsigned int GetAniTexture = 0x007154C0;
static const unsigned int AdvanceAni = 0x0054E9C0;

};

Expand All @@ -274,9 +276,10 @@ struct GothicMemoryLocations {
static const unsigned int zCTex_D3DInsertTexture = 0x0071D7D0;
static const unsigned int LoadResourceData = 0x005CA5E0;
static const unsigned int Offset_CacheState = 0x4C;
static const unsigned int Offset_NextFrame = 0x58;
static const unsigned int Offset_ActAniFrame = 0x70;
static const unsigned int Offset_AniFrames = 0x7C;
static const unsigned int Mask_CacheState = 3;
//static const unsigned int GetName = 0x0059AF90;
//static const unsigned int GetAniTexture = 0x005DA9B0;

static const unsigned int Offset_Flags = 0x88;
static const unsigned int Mask_FlagHasAlpha = 0x1;
Expand Down Expand Up @@ -336,7 +339,6 @@ struct GothicMemoryLocations {
};

struct oCWorld {
static const unsigned int InsertVobInWorld = 0x006D7120;
static const unsigned int EnableVob = 0x006D7130;
static const unsigned int DisableVob = 0x006D7250;
static const unsigned int RemoveFromLists = 0x006D7750;
Expand Down
8 changes: 6 additions & 2 deletions D3D11Engine/GothicMemoryLocations1_12f.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,11 @@ struct GothicMemoryLocations {
static const unsigned int DoFrameActivity = 0x005F6BE0;
static const unsigned int GetBBoxLocal = 0x0060E470;
static const unsigned int Offset_HomeWorld = 0x0A8;
static const unsigned int Offset_Type = 0x0A0;

static const unsigned int Offset_Flags = 0xE4;
static const unsigned int MASK_ShowVisual = 0x1;
static const unsigned int Offset_CameraAlignment = 0x110;
static const unsigned int Offset_CameraAlignment = 0xF0;
static const unsigned int SHIFTLR_CameraAlignment = 0x1E;

static const unsigned int Destructor = 0x005F2490;
Expand Down Expand Up @@ -248,6 +249,7 @@ struct GothicMemoryLocations {
static const unsigned int Constructor = 0x00565370;
static const unsigned int Destructor = 0x005655E0;
static const unsigned int GetAniTexture = 0x00751320;
static const unsigned int AdvanceAni = 0x00566ED0;

};

Expand All @@ -259,6 +261,9 @@ struct GothicMemoryLocations {
static const unsigned int zCTex_D3DInsertTexture = 0x00759E90;
static const unsigned int LoadResourceData = 0x005E9020;
static const unsigned int Offset_CacheState = 0x4C;
static const unsigned int Offset_NextFrame = 0x58;
static const unsigned int Offset_ActAniFrame = 0x70;
static const unsigned int Offset_AniFrames = 0x7C;
static const unsigned int Mask_CacheState = 3;

static const unsigned int Offset_Flags = 0x88;
Expand Down Expand Up @@ -317,7 +322,6 @@ struct GothicMemoryLocations {
};

struct oCWorld {
static const unsigned int InsertVobInWorld = 0x0070EC40;
static const unsigned int EnableVob = 0x0070EC50;
static const unsigned int DisableVob = 0x0070ED90;
static const unsigned int RemoveFromLists = 0x0070F2B0;
Expand Down
7 changes: 5 additions & 2 deletions D3D11Engine/GothicMemoryLocations2_6_fix.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ struct GothicMemoryLocations {
static const unsigned int GetBBoxLocal = 0x0061B1F0;
static const unsigned int Offset_HomeWorld = 0x0B8;
static const unsigned int Offset_GroundPoly = 0x0BC;
static const unsigned int Offset_Type = 0xB0;
static const unsigned int Offset_Flags = 0x104;
static const unsigned int MASK_ShowVisual = 0x1;
static const unsigned int Offset_CameraAlignment = 0x110;
Expand Down Expand Up @@ -438,6 +439,7 @@ struct GothicMemoryLocations {
static const unsigned int Constructor = 0x00563E00;
static const unsigned int Destructor = 0x00564070;
static const unsigned int GetAniTexture = 0x0064BA20;
static const unsigned int AdvanceAni = 0x00565D50;

};

Expand All @@ -446,6 +448,9 @@ struct GothicMemoryLocations {
static const unsigned int LoadResourceData = 0x005F54D0;
static const unsigned int GetName = 0x005A9CD0;
static const unsigned int Offset_CacheState = 0x4C;
static const unsigned int Offset_NextFrame = 0x58;
static const unsigned int Offset_ActAniFrame = 0x70;
static const unsigned int Offset_AniFrames = 0x7C;
static const unsigned int Mask_CacheState = 3;

static const unsigned int Offset_Flags = 0x88;
Expand Down Expand Up @@ -473,7 +478,6 @@ struct GothicMemoryLocations {
};

struct oCWorld {
//static const unsigned int InsertVobInWorld = 0x006D7120;
static const unsigned int EnableVob = 0x00780340;
static const unsigned int DisableVob = 0x00780460;
static const unsigned int RemoveFromLists = 0x00780990;
Expand All @@ -482,7 +486,6 @@ struct GothicMemoryLocations {

struct zCWorld {
static const unsigned int Render = 0x00621700;
static const unsigned int InsertVobInWorld = 0x00780330;
static const unsigned int VobAddedToWorld = 0x00624830;
static const unsigned int Call_Render_zCBspTreeRender = 0x00621830;
static const unsigned int Offset_GlobalVobTree = 0x24;
Expand Down
2 changes: 0 additions & 2 deletions D3D11Engine/GothicMemoryLocations2_6_fix_Spacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,15 +442,13 @@ struct GothicMemoryLocations {
};

struct oCWorld {
//static const unsigned int InsertVobInWorld = 0x006D7120;
static const unsigned int EnableVob = 0x005D9920;
static const unsigned int DisableVob = 0x005D9A40;
static const unsigned int RemoveFromLists = 0x005D9F70;
};

struct zCWorld {
static const unsigned int Render = 0x007AC5F0;
static const unsigned int InsertVobInWorld = 0x005D9910;
static const unsigned int VobAddedToWorld = 0x007AF6C0;
//static const unsigned int Call_Render_zCBspTreeRender = 0x00621830;
static const unsigned int Offset_GlobalVobTree = 0x24;
Expand Down
44 changes: 43 additions & 1 deletion D3D11Engine/HookedFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void HookedFunctionInfo::InitHooks() {

LogInfo() << "Patching: Marking texture as cached-in after cache-out - fix";
PatchAddr( 0x005CA683, "\x90\x90" );

LogInfo() << "Patching: Improve loading times by disabling some unnecessary features";
PatchAddr( 0x005A4FE0, "\xC3\x90\x90" );
PatchAddr( 0x0055848A, "\xE9\xE2\x01\x00\x00\x90" );
Expand All @@ -124,6 +124,16 @@ void HookedFunctionInfo::InitHooks() {
PatchAddr( 0x0051E425, "\x90\x90" );
PatchAddr( 0x0051E5B5, "\xEB\x22" );
PatchAddr( 0x0051E62A, "\x8D\x24\x24\x8B\x4A\x30\x8B\x04\xA9\x8B\x48\x40\x83\xC0\x38\x85\xC9\x74\x28\x33\xF6\x85\xC9\x7E\x22\x8B\x18\x8B\xFB\x8D\x1B\x39\x17\x74\x0A\x46\x83\xC7\x04\x3B\xF1\x7C\xF4\xEB\x0E\x49\x3B\xF1\x89\x48\x08\x74\x06\x8B\x04\x8B\x89\x04\xB3\x8B\x42\x38\x45\x3B\xE8\x7C\xC0\xEB\x65\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" );

// Skip loading lightmaps and make general purpose lightmap to be able to detect indoor vobs
{
PatchAddr( 0x00525C56, "\xEB\xCF\x90" );
PatchAddr( 0x0055787E, "\xE9\x5E\x0B\x00\x00" );
PatchAddr( 0x00557A11, "\xE9\xCB\x09\x00\x00" );
PatchAddr( 0x005582C9, "\x8B\xCB\xE8\xA6\xEF\xFF\xFF\xEB\x2E\x90\x90" );
PatchAddr( 0x00557276, "\xE9\x00\x00\x00\x00" );
XHook( 0x00557276, HookedFunctionInfo::hooked_SetLightmap );
}

LogInfo() << "Patching: Show correct tris on toggle frame";
{
Expand Down Expand Up @@ -209,6 +219,16 @@ void HookedFunctionInfo::InitHooks() {
PatchAddr( 0x005396C9, "\xEB" );
PatchAddr( 0x00530F05, "\xEB\x22" );
PatchAddr( 0x00530F7A, "\x8D\xA4\x24\x00\x00\x00\x00\x8B\x4A\x30\x8B\x04\xA9\x8B\x48\x40\x83\xC0\x38\x85\xC0\x74\x28\x33\xF6\x85\xC9\x7E\x22\x8B\x18\x8B\xFB\x8D\x1B\x39\x17\x74\x0A\x46\x83\xC7\x04\x3B\xF1\x7C\xF4\xEB\x0E\x49\x3B\xF1\x89\x48\x08\x74\x06\x8B\x04\x8B\x89\x04\xB3\x8B\x42\x38\x45\x3B\xE8\x7C\xC0\xEB\x61\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" );

// Skip loading lightmaps and make general purpose lightmap to be able to detect indoor vobs
{
PatchAddr( 0x00539757, "\xEB\xCF\x90" );
PatchAddr( 0x00570746, "\xE9\x62\x0A\x00\x00" );
PatchAddr( 0x005708AC, "\xE9\xFC\x08\x00\x00" );
PatchAddr( 0x00571098, "\x8B\xCB\xE8\x13\x58\xFF\xFF\xEB\x2E\x90\x90" );
PatchAddr( 0x005668B2, "\xE9\x00\x00\x00\x00" );
XHook( 0x005668B2, HookedFunctionInfo::hooked_SetLightmap );
}
#endif

LogInfo() << "Patching: Show correct tris on toggle frame";
Expand Down Expand Up @@ -313,3 +333,25 @@ int __cdecl HookedFunctionInfo::hooked_GetNumDevices()
Engine::GraphicsEngine->OnUIEvent( BaseGraphicsEngine::EUIEvent::UI_OpenSettings );
return 1;
}

void __fastcall HookedFunctionInfo::hooked_SetLightmap( void* polygonPtr )
{
static zCLightmap* lightmap = nullptr;
if ( !lightmap ) {
#ifdef BUILD_GOTHIC_1_08k
DWORD alloc_lightmap = reinterpret_cast<DWORD( __cdecl* )( DWORD )>( 0x54EBE0 )( 0x4C ); // malloc memory
reinterpret_cast<void( __cdecl* )( DWORD, DWORD )>( 0x75A250 )( alloc_lightmap, 0x8CF070 ); // inform zengine object created
reinterpret_cast<void( __fastcall* )( DWORD )>( 0x5CDD70 )( alloc_lightmap ); // call the constructor
#else
DWORD alloc_lightmap = reinterpret_cast<DWORD( __cdecl* )( DWORD )>( 0x565F50 )( 0x4C ); // malloc memory
reinterpret_cast<void( __cdecl* )( DWORD, DWORD )>( 0x5AAEB0 )( alloc_lightmap, 0x99B250 ); // inform zengine object created
reinterpret_cast<void( __fastcall* )( DWORD )>( 0x5F8EA0 )( alloc_lightmap ); // call the constructor
#endif

lightmap = reinterpret_cast<zCLightmap*>(alloc_lightmap);
}

zCPolygon* polygon = reinterpret_cast<zCPolygon*>(polygonPtr);
polygon->SetLightmap( lightmap );
zCObject_AddRef( lightmap ); // Make sure it won't get deleted
}
1 change: 1 addition & 0 deletions D3D11Engine/HookedFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ struct HookedFunctionInfo {
static void __fastcall hooked_zCActiveSndAutoCalcObstruction( void* thisptr, void* unknwn, int i );

static int __cdecl hooked_GetNumDevices();
static void __fastcall hooked_SetLightmap( void* polygonPtr );
};

namespace HookedFunctions {
Expand Down
Loading

0 comments on commit 2c8e253

Please sign in to comment.