Skip to content

Commit

Permalink
Fixes & Improvements
Browse files Browse the repository at this point in the history
-Patch dx7 functions that do window transitions
-Add additional check for waterfall foam in G1 to avoid false positives
-Rewrite cinema scope and screen fade effects into native GD3D11 functions
  • Loading branch information
SaiyansKing committed Nov 29, 2021
1 parent bd1be6c commit 0294c99
Show file tree
Hide file tree
Showing 15 changed files with 331 additions and 26 deletions.
3 changes: 3 additions & 0 deletions D3D11Engine/BaseGraphicsEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ class BaseGraphicsEngine {
/** Returns the graphics-device this is running on */
virtual std::string GetGraphicsDeviceName() = 0;

/** Draws a screen fade effects */
virtual XRESULT DrawScreenFade( void* camera ) { return XR_SUCCESS; };

/** Draws a vertexarray, used for rendering gothics UI */
virtual XRESULT DrawVertexArray( ExVertexStruct* vertices, unsigned int numVertices, unsigned int startVertex = 0, unsigned int stride = sizeof( ExVertexStruct ) ) = 0;
virtual XRESULT DrawVertexArrayMM( ExVertexStruct* vertices, unsigned int numVertices, unsigned int startVertex = 0, unsigned int stride = sizeof( ExVertexStruct ) ) = 0;
Expand Down
142 changes: 131 additions & 11 deletions D3D11Engine/D3D11GraphicsEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,23 +451,25 @@ XRESULT D3D11GraphicsEngine::Init() {

/** Called when the game created its window */
XRESULT D3D11GraphicsEngine::SetWindow( HWND hWnd ) {
LogInfo() << "Creating swapchain";
OutputWindow = hWnd;
if ( !OutputWindow ) {
LogInfo() << "Creating swapchain";
OutputWindow = hWnd;

const INT2 res = Resolution;
const INT2 res = Resolution;

#ifdef BUILD_SPACER
RECT r;
GetClientRect( hWnd, &r );
RECT r;
GetClientRect( hWnd, &r );

res.x = r.right;
res.y = r.bottom;
res.x = r.right;
res.y = r.bottom;
#endif
if ( res.x != 0 && res.y != 0 ) OnResize( res );
if ( res.x != 0 && res.y != 0 ) OnResize( res );

// We need to update clip cursor here because we hook the window too late to receive proper window message
m_isWindowActive = (GetForegroundWindow() == hWnd);
UpdateClipCursor( hWnd );
// We need to update clip cursor here because we hook the window too late to receive proper window message
m_isWindowActive = (GetForegroundWindow() == hWnd);
UpdateClipCursor( hWnd );
}

return XR_SUCCESS;
}
Expand Down Expand Up @@ -1429,6 +1431,124 @@ XRESULT D3D11GraphicsEngine::BindViewportInformation( const std::string& shader,
return XR_SUCCESS;
}

/** Draws a screen fade effects */
XRESULT D3D11GraphicsEngine::DrawScreenFade( void* c ) {
zCCamera* camera = reinterpret_cast<zCCamera*>(c);

bool ResetStates = false;
if ( camera->HasCinemaScopeEnabled() ) {
camera->ResetCinemaScopeEnabled();
ResetStates = true;

zColor cinemaScopeColor = camera->GetCinemaScopeColor();

// Default states
SetDefaultStates();
Engine::GAPI->GetRendererState().BlendState.SetAlphaBlending();
Engine::GAPI->GetRendererState().BlendState.SetDirty();
Engine::GAPI->GetRendererState().DepthState.DepthBufferCompareFunc = GothicDepthBufferStateInfo::CF_COMPARISON_ALWAYS;
Engine::GAPI->GetRendererState().DepthState.DepthWriteEnabled = false;
Engine::GAPI->GetRendererState().DepthState.SetDirty();

SetActivePixelShader( "PS_PFX_CinemaScope" );
ActivePS->Apply();

SetActiveVertexShader( "VS_CinemaScope" );
ActiveVS->Apply();

GhostAlphaConstantBuffer colorBuffer;
colorBuffer.GA_Alpha = cinemaScopeColor.bgra.alpha / 255.f;
colorBuffer.GA_Pad.x = cinemaScopeColor.bgra.r / 255.f;
colorBuffer.GA_Pad.y = cinemaScopeColor.bgra.g / 255.f;
colorBuffer.GA_Pad.z = cinemaScopeColor.bgra.b / 255.f;
ActivePS->GetConstantBuffer()[0]->UpdateBuffer( &colorBuffer );
ActivePS->GetConstantBuffer()[0]->BindToPixelShader( 0 );

UpdateRenderStates();
GetContext()->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
GetContext()->Draw( 12, 0 );
}

if ( camera->HasScreenFadeEnabled() ) {
camera->ResetScreenFadeEnabled();
ResetStates = true;

bool haveTexture = true;
zCMaterial* material = reinterpret_cast<zCMaterial*>(camera->GetPolyMaterial());
if ( zCTexture* texture = material->GetAniTexture() ) {
if ( texture->CacheIn( 0.6f ) == zRES_CACHED_IN )
texture->Bind( 0 );
else
goto Continue_ResetState;
}
else
haveTexture = false;

zColor screenFadeColor = camera->GetScreenFadeColor();

// Default states
SetDefaultStates();
switch ( camera->GetScreenFadeBlendFunc() ) {
case zRND_ALPHA_FUNC_BLEND:
case zRND_ALPHA_FUNC_BLEND_TEST:
case zRND_ALPHA_FUNC_SUB: {
Engine::GAPI->GetRendererState().BlendState.SetAlphaBlending();
Engine::GAPI->GetRendererState().BlendState.SetDirty();
break;
}
case zRND_ALPHA_FUNC_ADD: {
Engine::GAPI->GetRendererState().BlendState.SetAdditiveBlending();
Engine::GAPI->GetRendererState().BlendState.SetDirty();
break;
}
case zRND_ALPHA_FUNC_MUL: {
Engine::GAPI->GetRendererState().BlendState.SetModulateBlending();
Engine::GAPI->GetRendererState().BlendState.SetDirty();
break;
}
case zRND_ALPHA_FUNC_MUL2: {
Engine::GAPI->GetRendererState().BlendState.SetModulate2Blending();
Engine::GAPI->GetRendererState().BlendState.SetDirty();
break;
}
}
Engine::GAPI->GetRendererState().DepthState.DepthBufferCompareFunc = GothicDepthBufferStateInfo::CF_COMPARISON_ALWAYS;
Engine::GAPI->GetRendererState().DepthState.DepthWriteEnabled = false;
Engine::GAPI->GetRendererState().DepthState.SetDirty();

if ( haveTexture )
SetActivePixelShader( "PS_PFX_Alpha_Blend" );
else
SetActivePixelShader( "PS_PFX_CinemaScope" );

ActivePS->Apply();

SetActiveVertexShader( "VS_PFX" );
ActiveVS->Apply();

GhostAlphaConstantBuffer colorBuffer;
colorBuffer.GA_Alpha = screenFadeColor.bgra.alpha / 255.f;
colorBuffer.GA_Pad.x = screenFadeColor.bgra.r / 255.f;
colorBuffer.GA_Pad.y = screenFadeColor.bgra.g / 255.f;
colorBuffer.GA_Pad.z = screenFadeColor.bgra.b / 255.f;
ActivePS->GetConstantBuffer()[0]->UpdateBuffer( &colorBuffer );
ActivePS->GetConstantBuffer()[0]->BindToPixelShader( 0 );

PfxRenderer->DrawFullScreenQuad();
}

Continue_ResetState:
if ( ResetStates ) {
// Enable blending, in case some modifications need it
SetDefaultStates();
Engine::GAPI->GetRendererState().BlendState.SetDefault();
Engine::GAPI->GetRendererState().BlendState.BlendEnabled = true;
Engine::GAPI->GetRendererState().BlendState.SetDirty();
UpdateRenderStates();
}
return XR_SUCCESS;
}

/** Draws a vertexarray, non-indexed (HUD, 2D)*/
XRESULT D3D11GraphicsEngine::DrawVertexArray( ExVertexStruct* vertices,
unsigned int numVertices,
Expand Down
3 changes: 3 additions & 0 deletions D3D11Engine/D3D11GraphicsEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ class D3D11GraphicsEngine : public D3D11GraphicsEngineBase {
/** Draws a skeletal mesh */
virtual XRESULT DrawSkeletalMesh( SkeletalVobInfo* vi, const std::vector<DirectX::XMFLOAT4X4>& transforms, float4 color, float fatness = 1.0f ) override;

/** Draws a screen fade effects */
virtual XRESULT DrawScreenFade( void* camera ) override;

/** Draws a vertexarray, non-indexed */
virtual XRESULT DrawVertexArray( ExVertexStruct* vertices, unsigned int numVertices, unsigned int startVertex = 0, unsigned int stride = sizeof( ExVertexStruct ) ) override;
virtual XRESULT DrawVertexArrayMM( ExVertexStruct* vertices, unsigned int numVertices, unsigned int startVertex = 0, unsigned int stride = sizeof( ExVertexStruct ) ) override;
Expand Down
10 changes: 9 additions & 1 deletion D3D11Engine/D3D11ShaderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ XRESULT D3D11ShaderManager::Init() {
Shaders.push_back( ShaderInfo( "VS_PFX", "VS_PFX.hlsl", "v" ) );
Shaders.back().cBufferSizes.push_back( sizeof( PFXVS_ConstantBuffer ) );

Shaders.push_back( ShaderInfo( "VS_CinemaScope", "VS_CinemaScope.hlsl", "v" ) );
Shaders.back().cBufferSizes.push_back( sizeof( PFXVS_ConstantBuffer ) );

Shaders.push_back( ShaderInfo( "PS_PFX_Simple", "PS_PFX_Simple.hlsl", "p" ) );


Expand All @@ -198,7 +201,12 @@ XRESULT D3D11ShaderManager::Init() {
Shaders.back().cBufferSizes.push_back( sizeof( CloudConstantBuffer ) );
Shaders.back().cBufferSizes.push_back( sizeof( AtmosphereConstantBuffer ) );

Shaders.push_back( ShaderInfo( "PS_PFX_Copy_NoAlpha", "PS_PFX_Copy_NoAlpha.hlsl", "p" ) );
Shaders.push_back( ShaderInfo( "PS_PFX_Alpha_Blend", "PS_PFX_Alpha_Blend.hlsl", "p" ) );
Shaders.back().cBufferSizes.push_back( sizeof( GhostAlphaConstantBuffer ) );

Shaders.push_back( ShaderInfo( "PS_PFX_CinemaScope", "PS_PFX_CinemaScope.hlsl", "p" ) );
Shaders.back().cBufferSizes.push_back( sizeof( GhostAlphaConstantBuffer ) );

Shaders.push_back( ShaderInfo( "PS_PFX_Blend", "PS_PFX_Blend.hlsl", "p" ) );
Shaders.push_back( ShaderInfo( "PS_PFX_DistanceBlur", "PS_PFX_DistanceBlur.hlsl", "p" ) );
Shaders.push_back( ShaderInfo( "PS_PFX_LumConvert", "PS_PFX_LumConvert.hlsl", "p" ) );
Expand Down
12 changes: 10 additions & 2 deletions D3D11Engine/GothicMemoryLocations1_08k.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ struct GothicMemoryLocations {
static const unsigned int CacheInSurface = 0x0071A3E0;
static const unsigned int CacheOutSurface = 0x0071A7F0;

static const unsigned int RenderScreenFade = 0x005376F0;
static const unsigned int RenderCinemaScope = 0x005377B0;

static const unsigned int Offset_RenderState = 0x34;
static const unsigned int Offset_BoundTexture = 0x80E38;
};
Expand Down Expand Up @@ -143,8 +146,13 @@ struct GothicMemoryLocations {
static const unsigned int SetFOV = 0x00536720;
static const unsigned int GetFOV_f2 = 0x005366B0;

static const unsigned int Offset_FarPlane = 2272;
static const unsigned int Offset_NearPlane = 2276;
static const unsigned int Offset_FarPlane = 0x8E0;
static const unsigned int Offset_NearPlane = 0x8E4;
static const unsigned int Offset_ScreenFadeEnabled = 0x8C0;
static const unsigned int Offset_ScreenFadeColor = 0x8C4;
static const unsigned int Offset_CinemaScopeEnabled = 0x8C8;
static const unsigned int Offset_CinemaScopeColor = 0x8CC;
static const unsigned int Offset_PolyMaterial = 0x8BC;
};

struct zCVob {
Expand Down
12 changes: 10 additions & 2 deletions D3D11Engine/GothicMemoryLocations1_12f.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ struct GothicMemoryLocations {
static const unsigned int CacheInSurface = 0x007565D0;
static const unsigned int CacheOutSurface = 0x00756A60;

static const unsigned int RenderScreenFade = 0x0054E810;
static const unsigned int RenderCinemaScope = 0x0054E8E0;

static const unsigned int Offset_RenderState = 0x34;
static const unsigned int Offset_BoundTexture = 0x80E38;
};
Expand Down Expand Up @@ -130,8 +133,13 @@ struct GothicMemoryLocations {
static const unsigned int SetFOV = 0x0054D700;
static const unsigned int GetFOV_f2 = 0x0054D690;

static const unsigned int Offset_FarPlane = 2272;
static const unsigned int Offset_NearPlane = 2276;
static const unsigned int Offset_FarPlane = 0x8E0;
static const unsigned int Offset_NearPlane = 0x8E4;
static const unsigned int Offset_ScreenFadeEnabled = 0x8C0;
static const unsigned int Offset_ScreenFadeColor = 0x8C4;
static const unsigned int Offset_CinemaScopeEnabled = 0x8C8;
static const unsigned int Offset_CinemaScopeColor = 0x8CC;
static const unsigned int Offset_PolyMaterial = 0x8BC;
};

struct zCVob {
Expand Down
9 changes: 9 additions & 0 deletions D3D11Engine/GothicMemoryLocations2_6_fix.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ struct GothicMemoryLocations {
static const unsigned int CacheOutSurface = 0x00652F40;
static const unsigned int Vid_GetGammaCorrection = 0x00659610;

static const unsigned int RenderScreenFade = 0x0054BC40;
static const unsigned int RenderCinemaScope = 0x0054BD30;

static const unsigned int Offset_RenderState = 0x38;
static const unsigned int Offset_BoundTexture = 0x82E50;
};
Expand Down Expand Up @@ -320,6 +323,12 @@ struct GothicMemoryLocations {
static const unsigned int Activate = 0x0054A700;
static const unsigned int Offset_NearPlane = 0x900;
static const unsigned int Offset_FarPlane = 0x8FC;
static const unsigned int Offset_ScreenFadeEnabled = 0x8C0;
static const unsigned int Offset_ScreenFadeColor = 0x8C4;
static const unsigned int Offset_ScreenFadeBlendFunc = 0x8E0;
static const unsigned int Offset_CinemaScopeEnabled = 0x8E4;
static const unsigned int Offset_CinemaScopeColor = 0x8E8;
static const unsigned int Offset_PolyMaterial = 0x8BC;
static const unsigned int SetFarPlane = 0x0054B200;
static const unsigned int BBox3DInFrustum = 0x0054B410;
static const unsigned int Var_FreeLook = 0x008CE42C;
Expand Down
17 changes: 12 additions & 5 deletions D3D11Engine/HookedFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ void HookedFunctionInfo::InitHooks() {

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

LogInfo() << "Patching: Disable dx7 window transitions";
PatchAddr( 0x0075CA7B, "\x90\x90" );
PatchAddr( 0x0074DAD0, "\x90\x90" );
#else
LogInfo() << "Patching: BroadCast fix";
{
Expand Down Expand Up @@ -137,6 +141,10 @@ void HookedFunctionInfo::InitHooks() {
LogInfo() << "Patching: Fix using settings in freelook mode";
PatchAddr( 0x00478FE2, "\x0F\x84\x9A\x00\x00\x00" );

LogInfo() << "Patching: Disable dx7 window transitions";
PatchAddr( 0x0072018B, "\x90\x90" );
PatchAddr( 0x00711F70, "\x90\x90" );

LogInfo() << "Patching: Show correct tris on toggle frame";
{
char* trisHndl[5];
Expand Down Expand Up @@ -182,11 +190,6 @@ void HookedFunctionInfo::InitHooks() {
PatchAddr( 0x0044C72F, zSPYwnd );
}

// Workaround to fix disappearing ui elements under certain circumstances
// e.g. praying at Beliar statue, screen blend causing dialog boxes to disappear.
LogInfo() << "Patching: Overriding zCVobScreenFX::OnTick() if (blend.visible) -> if (false)";
PatchAddr( 0x0061808F, "\x90\xE9" );

LogInfo() << "Patching: Interupt gamestart sound";
PatchAddr( 0x004DB89F, "\x00" );

Expand Down Expand Up @@ -238,6 +241,10 @@ void HookedFunctionInfo::InitHooks() {
PatchAddr( 0x004806C2, "\x0F\x84\x9A\x00\x00\x00" );
#endif

LogInfo() << "Patching: Disable dx7 window transitions";
PatchAddr( 0x00658BCB, "\x90\x90" );
PatchAddr( 0x006483A2, "\x90\x90" );

LogInfo() << "Patching: Show correct tris on toggle frame";
{
char* trisHndl[5];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//--------------------------------------------------------------------------------------
// World/VOB-Pixelshader for G2D3D11 by Degenerated
// Alpha Blend Buffer
//--------------------------------------------------------------------------------------
cbuffer AlphaBlendInfo : register( b0 )
{
float AB_Alpha;
float3 AB_Color;
};

//--------------------------------------------------------------------------------------
// Textures and Samplers
Expand All @@ -25,7 +30,7 @@ struct PS_INPUT
float4 PSMain( PS_INPUT Input ) : SV_TARGET
{
float4 color = TX_Texture0.Sample(SS_Linear, Input.vTexcoord);

return float4(color.rgb, 0);
color *= float4(1.0f, 1.0f, 1.0f, AB_Alpha);
return color;
}

26 changes: 26 additions & 0 deletions D3D11Engine/Shaders/PS_PFX_CinemaScope.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//--------------------------------------------------------------------------------------
// Cinema Scope Buffer
//--------------------------------------------------------------------------------------
cbuffer AlphaBlendInfo : register( b0 )
{
float AB_Alpha;
float3 AB_Color;
};

//--------------------------------------------------------------------------------------
// Input / Output structures
//--------------------------------------------------------------------------------------
struct PS_INPUT
{
float2 vTexcoord : TEXCOORD0;
float3 vEyeRay : TEXCOORD1;
float4 vPosition : SV_POSITION;
};

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PSMain( PS_INPUT Input ) : SV_TARGET
{
return float4(AB_Color, AB_Alpha);
}
Loading

0 comments on commit 0294c99

Please sign in to comment.