Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Settings/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
visit(DdrawOverrideStencilFormat) \
visit(DdrawResolutionHack) \
visit(DdrawUseDirect3D9Ex) \
visit(DdrawConvertHomogeneousW) \
visit(DdrawUseNativeResolution) \
visit(DdrawEnableMouseHook) \
visit(DdrawHookSystem32) \
Expand Down Expand Up @@ -209,6 +210,7 @@ struct CONFIG
bool DdrawIntegerScalingClamp = false; // Scales the screen by an integer value to help preserve video quality
bool DdrawMaintainAspectRatio = false; // Keeps the current DirectDraw aspect ratio when overriding the game's resolution
bool DdrawUseDirect3D9Ex = false; // Use Direct3D9Ex extensions for Dd7to9
bool DdrawConvertHomogeneousW = false; // Convert primites using D3DFVF_XYZRHW to D3DFVF_XYZW.
bool DdrawUseNativeResolution = false; // Uses the current screen resolution for Dd7to9
DWORD DdrawClippedWidth = 0; // Used to scaled Direct3d9 to use this width when using Dd7to9
DWORD DdrawClippedHeight = 0; // Used to scaled Direct3d9 to use this height when using Dd7to9
Expand Down
64 changes: 64 additions & 0 deletions ddraw/IDirect3DDeviceX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,28 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat
break;
}

if (Config.DdrawConvertHomogeneousW)
{
if (dtstTransformStateType == D3DTS_VIEW)
{
D3DVIEWPORT9 Viewport9;
if (SUCCEEDED((*d3d9Device)->GetViewport(&Viewport9)))
{
ZeroMemory(lpD3DMatrix, sizeof(_D3DMATRIX));
lpD3DMatrix->_11 = 2.0f / (float)Viewport9.Width;
lpD3DMatrix->_22 = -2.0f / (float)Viewport9.Height;
lpD3DMatrix->_33 = 1.0f;
lpD3DMatrix->_41 = -1.0f; // translate X
lpD3DMatrix->_42 = 1.0f; // translate Y
lpD3DMatrix->_44 = 1.0f;
}
}
else
{
return D3D_OK;
}
}

HRESULT hr = (*d3d9Device)->SetTransform(dtstTransformStateType, lpD3DMatrix);

if (SUCCEEDED(hr))
Expand Down Expand Up @@ -1943,6 +1965,28 @@ HRESULT m_IDirect3DDeviceX::SetRenderState(D3DRENDERSTATETYPE dwRenderStateType,

if (Config.Dd7to9)
{
if(Config.DdrawConvertHomogeneousW)
{
// ReSharper disable once CppIncompleteSwitchStatement
switch(dwRenderStateType)
{
case D3DRS_CULLMODE:
//dwRenderState = D3DCULL_NONE;
break;

case D3DRS_LIGHTING:
dwRenderState = FALSE;
break;

case D3DRS_CLIPPLANEENABLE:
//dwRenderState = 0;
break;

default:
break;
}
}

// Check for device interface
if (FAILED(CheckInterface(__FUNCTION__, true)))
{
Expand Down Expand Up @@ -2352,6 +2396,26 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy
}
else
{
const UINT stride = GetVertexStride(dwVertexTypeDesc);

// Handle PositionT
if((dwVertexTypeDesc & D3DFVF_XYZRHW) != 0 && Config.DdrawConvertHomogeneousW)
{
UINT8 *vertex = (UINT8*)lpVertices;

for (UINT x = 0; x < dwVertexCount; x++)
{
float *pos = (float*) vertex;

pos[3] = 1.0f;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you lose information here if rhw is != 1?
It's not used for many things, but at least for perspective-correct texture mapping and some exotic stuff like fog.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation read that the use of W is undefined here, and not seen as part of the position vector. For me it made no difference if I had it or not. So it is probably better to just remove it then.

Copy link
Contributor

@Trass3r Trass3r May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah not sure if they use the W at all in this mode, also they also say it's "designed for, and can only be used with, the programmable vertex pipeline."
https://learn.microsoft.com/en-us/windows/win32/direct3d9/d3dfvf

So the input coordinates are in screen space and your view matrix transforms it back into clip space?

https://gamedev.net/forums/topic/582097-difference-between-d3dfvf_xyzw-and-d3dfvf_xyzrhw/4705397/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SetTransform function overrides the game matrices with its own, when the mode is active.

// Replace the matrix with one that handles D3DFVF_XYZRHW geometry
D3DVIEWPORT9 Viewport9;
if(SUCCEEDED((*d3d9Device)->GetViewport(&Viewport9)))
{
	ZeroMemory(lpD3DMatrix, sizeof(_D3DMATRIX));
	lpD3DMatrix->_11 = 2.0f / (float)Viewport9.Width;
	lpD3DMatrix->_22 = -2.0f / (float)Viewport9.Height;
	lpD3DMatrix->_33 = 1.0f;
	lpD3DMatrix->_41 = -1.0f;  // translate X
	lpD3DMatrix->_42 = 1.0f;   // translate Y
	lpD3DMatrix->_44 = 1.0f;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know.
I guess an identity viewport matrix would also suffice but not sure about this mode.
https://learn.microsoft.com/en-us/windows/win32/dxtecharts/the-direct3d-transformation-pipeline


vertex += stride;
}

// Update the FVF
dwVertexTypeDesc = (dwVertexTypeDesc & ~D3DFVF_XYZRHW) | D3DFVF_XYZW;
}

// Set fixed function vertex type
(*d3d9Device)->SetFVF(dwVertexTypeDesc);

Expand Down