Skip to content

Commit

Permalink
Update draw states
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed May 17, 2024
1 parent 62978ef commit 3650a9e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Dllmain/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 7017
#define BUILD_NUMBER 7018
85 changes: 66 additions & 19 deletions ddraw/IDirect3DDeviceX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ HRESULT m_IDirect3DDeviceX::GetTexture(DWORD dwStage, LPDIRECT3DTEXTURE2* lplpTe

if (ProxyDirectXVersion > 3)
{
if (!lplpTexture || dwStage >= MaxTextureBlendStages)
if (!lplpTexture || dwStage >= MaxTextureStages)
{
return DDERR_INVALIDPARAMS;
}
Expand Down Expand Up @@ -783,7 +783,7 @@ HRESULT m_IDirect3DDeviceX::GetTexture(DWORD dwStage, LPDIRECTDRAWSURFACE7* lplp

if (Config.Dd7to9)
{
if (!lplpTexture || dwStage >= MaxTextureBlendStages)
if (!lplpTexture || dwStage >= MaxTextureStages)
{
return DDERR_INVALIDPARAMS;
}
Expand Down Expand Up @@ -858,7 +858,7 @@ HRESULT m_IDirect3DDeviceX::SetTexture(DWORD dwStage, LPDIRECT3DTEXTURE2 lpTextu

if (ProxyDirectXVersion > 3)
{
if (dwStage >= MaxTextureBlendStages)
if (dwStage >= MaxTextureStages)
{
return DDERR_INVALIDPARAMS;
}
Expand Down Expand Up @@ -900,7 +900,7 @@ HRESULT m_IDirect3DDeviceX::SetTexture(DWORD dwStage, LPDIRECTDRAWSURFACE7 lpSur

if (Config.Dd7to9)
{
if (dwStage >= MaxTextureBlendStages)
if (dwStage >= MaxTextureStages)
{
return DDERR_INVALIDPARAMS;
}
Expand Down Expand Up @@ -1181,7 +1181,7 @@ HRESULT m_IDirect3DDeviceX::SetTextureStageState(DWORD dwStage, D3DTEXTURESTAGES

if (Config.Dd7to9)
{
if (dwStage >= MaxTextureBlendStages)
if (dwStage >= MaxTextureStages)
{
return DDERR_INVALIDPARAMS;
}
Expand Down Expand Up @@ -4174,26 +4174,52 @@ inline void m_IDirect3DDeviceX::SetDrawStates(DWORD dwVertexTypeDesc, DWORD& dwF
}
if (dwFlags & D3DDP_DXW_DRAW2DSURFACE)
{
// Set by DirectDrawSurface
// Get states set by DirectDrawSurface
(*d3d9Device)->GetRenderState(D3DRS_LIGHTING, &DrawStates.rsLighting);
(*d3d9Device)->GetSamplerState(0, D3DSAMP_MAGFILTER, &DrawStates.ssMagFilter[0]);

// Other states
// Get texture states
(*d3d9Device)->GetTextureStageState(0, D3DTSS_COLOROP, &DrawStates.tsColorOP);
(*d3d9Device)->GetTextureStageState(0, D3DTSS_COLORARG1, &DrawStates.tsColorArg1);
(*d3d9Device)->GetTextureStageState(0, D3DTSS_COLORARG2, &DrawStates.tsColorArg2);
(*d3d9Device)->GetTextureStageState(0, D3DTSS_ALPHAOP, &DrawStates.tsAlphaOP);

// Get render states
(*d3d9Device)->GetRenderState(D3DRS_ALPHATESTENABLE, &DrawStates.rsAlphaTestEnable);
(*d3d9Device)->GetRenderState(D3DRS_ALPHABLENDENABLE, &DrawStates.rsAlphaBlendEnable);
(*d3d9Device)->GetRenderState(D3DRS_FOGENABLE, &DrawStates.rsFogEnable);
(*d3d9Device)->GetRenderState(D3DRS_ZENABLE, &DrawStates.rsZEnable);
(*d3d9Device)->GetRenderState(D3DRS_ZWRITEENABLE, &DrawStates.rsZWriteEnable);
(*d3d9Device)->GetRenderState(D3DRS_STENCILENABLE, &DrawStates.rsStencilEnable);

// Set texture states
(*d3d9Device)->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
(*d3d9Device)->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
(*d3d9Device)->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT);
(*d3d9Device)->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

// Set render states
(*d3d9Device)->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
(*d3d9Device)->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
(*d3d9Device)->SetRenderState(D3DRS_FOGENABLE, FALSE);
(*d3d9Device)->SetRenderState(D3DRS_ZENABLE, FALSE);
(*d3d9Device)->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
(*d3d9Device)->SetRenderState(D3DRS_STENCILENABLE, FALSE);

// Set textures
for (int x = 1; x < MaxTextureStages; x++)
{
if (AttachedTexture[x])
{
(*d3d9Device)->SetTexture(x, nullptr);
}
}
}
else if (Config.Dd7to9)
{
if (Config.DdrawFixByteAlignment > 1)
{
for (UINT x = 0; x < MaxTextureBlendStages; x++)
for (UINT x = 0; x < MaxTextureStages; x++)
{
if (CurrentTextureSurfaceX[x] && CurrentTextureSurfaceX[x]->GetWasBitAlignLocked())
{
Expand All @@ -4211,17 +4237,22 @@ inline void m_IDirect3DDeviceX::SetDrawStates(DWORD dwVertexTypeDesc, DWORD& dwF
(*d3d9Device)->GetRenderState(D3DRS_ALPHAFUNC, &DrawStates.rsAlphaFunc);
(*d3d9Device)->GetRenderState(D3DRS_ALPHAREF, &DrawStates.rsAlphaRef);

// Check for color key texture
for (UINT x = 0; x < MaxTextureBlendStages; x++)
// Check for color key alpha texture
bool AlphaSurfaceSet = false;
for (UINT x = 0; x < MaxTextureStages; x++)
{
if (CurrentTextureSurfaceX[x] && CurrentTextureSurfaceX[x]->IsColorKeyTexture() && CurrentTextureSurfaceX[x]->Get3DDrawTexture())
{
(*d3d9Device)->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
(*d3d9Device)->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
(*d3d9Device)->SetRenderState(D3DRS_ALPHAREF, (DWORD)0x01);
AlphaSurfaceSet = true;
(*d3d9Device)->SetTexture(x, CurrentTextureSurfaceX[x]->Get3DDrawTexture());
}
}
if (AlphaSurfaceSet)
{
(*d3d9Device)->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
(*d3d9Device)->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
(*d3d9Device)->SetRenderState(D3DRS_ALPHAREF, (DWORD)0x01);
}
}
}
if (dwFlags & D3DDP_DXW_COLORKEYENABLE)
Expand Down Expand Up @@ -4259,24 +4290,40 @@ inline void m_IDirect3DDeviceX::RestoreDrawStates(DWORD dwVertexTypeDesc, DWORD
}
if (dwFlags & D3DDP_DXW_DRAW2DSURFACE)
{
// Other states
// Restore states set by DirectDrawSurface
(*d3d9Device)->SetRenderState(D3DRS_LIGHTING, DrawStates.rsLighting);
(*d3d9Device)->SetSamplerState(0, D3DSAMP_MAGFILTER, DrawStates.ssMagFilter[0]);

// Restore texture states
(*d3d9Device)->SetTextureStageState(0, D3DTSS_COLOROP, DrawStates.tsColorOP);
(*d3d9Device)->SetTextureStageState(0, D3DTSS_COLORARG1, DrawStates.tsColorArg1);
(*d3d9Device)->SetTextureStageState(0, D3DTSS_COLORARG2, DrawStates.tsColorArg2);
(*d3d9Device)->SetTextureStageState(0, D3DTSS_ALPHAOP, DrawStates.tsAlphaOP);

// Restore render states
(*d3d9Device)->SetRenderState(D3DRS_ALPHATESTENABLE, DrawStates.rsAlphaTestEnable);
(*d3d9Device)->SetRenderState(D3DRS_ALPHABLENDENABLE, DrawStates.rsAlphaBlendEnable);
(*d3d9Device)->SetRenderState(D3DRS_FOGENABLE, DrawStates.rsFogEnable);
(*d3d9Device)->SetRenderState(D3DRS_ZENABLE, DrawStates.rsZEnable);
(*d3d9Device)->SetRenderState(D3DRS_ZWRITEENABLE, DrawStates.rsZWriteEnable);
(*d3d9Device)->SetRenderState(D3DRS_STENCILENABLE, DrawStates.rsStencilEnable);

// Set by DirectDrawSurface
(*d3d9Device)->SetRenderState(D3DRS_LIGHTING, DrawStates.rsLighting);
(*d3d9Device)->SetSamplerState(0, D3DSAMP_MAGFILTER, DrawStates.ssMagFilter[0]);

// Restore textures
SetTexture(0, AttachedTexture[0]);
SetTexture(1, AttachedTexture[1]);
for (int x = 2; x < MaxTextureStages; x++)
{
if (AttachedTexture[x])
{
SetTexture(x, AttachedTexture[x]);
}
}
}
else if (Config.Dd7to9)
{
if (Config.DdrawFixByteAlignment > 1)
{
for (UINT x = 0; x < MaxTextureBlendStages; x++)
for (UINT x = 0; x < MaxTextureStages; x++)
{
if (CurrentTextureSurfaceX[x] && CurrentTextureSurfaceX[x]->GetWasBitAlignLocked())
{
Expand Down
18 changes: 12 additions & 6 deletions ddraw/IDirect3DDeviceX.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "IDirectDrawX.h"
#include <unordered_map>

constexpr UINT MaxTextureBlendStages = 8; // Devices can have up to eight set textures.
constexpr UINT MaxTextureStages = 8; // Devices can have up to eight set textures.

class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject
{
Expand Down Expand Up @@ -34,13 +34,19 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject
DWORD rsLighting = 0;
DWORD rsExtents = 0;
DWORD tsColorOP = 0;
DWORD tsColorArg1 = 0;
DWORD tsColorArg2 = 0;
DWORD tsAlphaOP = 0;
DWORD rsAlphaBlendEnable = 0;
DWORD rsAlphaTestEnable = 0;
DWORD rsAlphaFunc = 0;
DWORD rsAlphaRef = 0;
DWORD rsFogEnable = 0;
DWORD ssMinFilter[MaxTextureBlendStages] = {};
DWORD ssMagFilter[MaxTextureBlendStages] = {};
DWORD rsZEnable = 0;
DWORD rsZWriteEnable = 0;
DWORD rsStencilEnable = 0;
DWORD ssMinFilter[MaxTextureStages] = {};
DWORD ssMagFilter[MaxTextureStages] = {};
float lowColorKey[4] = {};
float highColorKey[4] = {};
} DrawStates;
Expand All @@ -67,12 +73,12 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject
DWORD rsSrcBlend;
DWORD rsDestBlend;
DWORD rsColorKeyEnabled;
DWORD ssMipFilter[MaxTextureBlendStages] = {};
DWORD ssMipFilter[MaxTextureStages] = {};

// SetTexture array
LPDIRECTDRAWSURFACE7 CurrentRenderTarget = nullptr;
m_IDirectDrawSurfaceX* CurrentTextureSurfaceX[MaxTextureBlendStages] = {};
LPDIRECTDRAWSURFACE7 AttachedTexture[MaxTextureBlendStages] = {};
m_IDirectDrawSurfaceX* CurrentTextureSurfaceX[MaxTextureStages] = {};
LPDIRECTDRAWSURFACE7 AttachedTexture[MaxTextureStages] = {};

// Texture handle map
std::unordered_map<DWORD, m_IDirect3DTextureX*> TextureHandleMap;
Expand Down

0 comments on commit 3650a9e

Please sign in to comment.