Skip to content

Commit

Permalink
fuck up d3dx9math to replace with directxmath
Browse files Browse the repository at this point in the history
  • Loading branch information
er2off committed May 25, 2023
1 parent 512a597 commit 9426857
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 619 deletions.
1 change: 1 addition & 0 deletions materialsystem/shaderapidx9/locald3dtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ typedef intp PixelShader_t;
#define D3DRS_NOTSUPPORTED D3DRS_FORCE_DWORD

#include "togl/rendermechanism.h"
#include <DirectXMath.h>

#if defined( _X360 )

Expand Down
171 changes: 104 additions & 67 deletions materialsystem/shaderapidx9/meshdx8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ class CTempMeshDX8 : public CBaseMeshDX8
private:
// Selection mode
void TestSelection( );
void ClipTriangle( D3DXVECTOR3 **ppVert, float zNear, D3DXMATRIX &proj );
void ClipTriangle( DirectX::XMVECTOR **ppVert, float zNear, DirectX::XMMATRIX &proj );

CDynamicMeshDX8 *GetDynamicMesh();

Expand Down Expand Up @@ -1942,31 +1942,31 @@ void CVertexBufferDx8::HandlePerFrameTextureStats( int nFrame )
// Helpers with meshdescs...
//-----------------------------------------------------------------------------
// FIXME: add compression-agnostic read-accessors (which decompress and return by value, checking desc.m_CompressionType)
inline D3DXVECTOR3 &Position( MeshDesc_t const &desc, int vert )
inline DirectX::XMVECTOR &Position( MeshDesc_t const &desc, int vert )
{
return *(D3DXVECTOR3*)((unsigned char*)desc.m_pPosition + vert * desc.m_VertexSize_Position );
return *(DirectX::XMVECTOR*)((unsigned char*)desc.m_pPosition + vert * desc.m_VertexSize_Position );
}

inline float Wrinkle( MeshDesc_t const &desc, int vert )
{
return *(float*)((unsigned char*)desc.m_pWrinkle + vert * desc.m_VertexSize_Wrinkle );
}

inline D3DXVECTOR3 &BoneWeight( MeshDesc_t const &desc, int vert )
inline DirectX::XMVECTOR &BoneWeight( MeshDesc_t const &desc, int vert )
{
Assert( desc.m_CompressionType == VERTEX_COMPRESSION_NONE );
return *(D3DXVECTOR3*)((unsigned char*)desc.m_pBoneWeight + vert * desc.m_VertexSize_BoneWeight );
return *(DirectX::XMVECTOR*)((unsigned char*)desc.m_pBoneWeight + vert * desc.m_VertexSize_BoneWeight );
}

inline unsigned char *BoneIndex( MeshDesc_t const &desc, int vert )
{
return desc.m_pBoneMatrixIndex + vert * desc.m_VertexSize_BoneMatrixIndex;
}

inline D3DXVECTOR3 &Normal( MeshDesc_t const &desc, int vert )
inline DirectX::XMVECTOR &Normal( MeshDesc_t const &desc, int vert )
{
Assert( desc.m_CompressionType == VERTEX_COMPRESSION_NONE );
return *(D3DXVECTOR3*)((unsigned char*)desc.m_pNormal + vert * desc.m_VertexSize_Normal );
return *(DirectX::XMVECTOR*)((unsigned char*)desc.m_pNormal + vert * desc.m_VertexSize_Normal );
}

inline unsigned char *Color( MeshDesc_t const &desc, int vert )
Expand All @@ -1979,14 +1979,14 @@ inline D3DXVECTOR2 &TexCoord( MeshDesc_t const &desc, int vert, int stage )
return *(D3DXVECTOR2*)((unsigned char*)desc.m_pTexCoord[stage] + vert * desc.m_VertexSize_TexCoord[stage] );
}

inline D3DXVECTOR3 &TangentS( MeshDesc_t const &desc, int vert )
inline DirectX::XMVECTOR &TangentS( MeshDesc_t const &desc, int vert )
{
return *(D3DXVECTOR3*)((unsigned char*)desc.m_pTangentS + vert * desc.m_VertexSize_TangentS );
return *(DirectX::XMVECTOR*)((unsigned char*)desc.m_pTangentS + vert * desc.m_VertexSize_TangentS );
}

inline D3DXVECTOR3 &TangentT( MeshDesc_t const &desc, int vert )
inline DirectX::XMVECTOR &TangentT( MeshDesc_t const &desc, int vert )
{
return *(D3DXVECTOR3*)((unsigned char*)desc.m_pTangentT + vert * desc.m_VertexSize_TangentT );
return *(DirectX::XMVECTOR*)((unsigned char*)desc.m_pTangentT + vert * desc.m_VertexSize_TangentT );
}


Expand Down Expand Up @@ -2176,6 +2176,7 @@ inline void CBaseMeshDX8::DrawMesh( )
void CBaseMeshDX8::Spew( int nVertexCount, int nIndexCount, const MeshDesc_t &spewDesc )
{
LOCK_SHADERAPI();
#if 0
// This has regressed.
int i;

Expand Down Expand Up @@ -2210,7 +2211,7 @@ void CBaseMeshDX8::Spew( int nVertexCount, int nIndexCount, const MeshDesc_t &sp
temp += sprintf( temp, "[%4d] ", i + desc.m_nFirstVertex );
if( fmt & VERTEX_POSITION )
{
D3DXVECTOR3& pos = Position( desc, i );
DirectX::XMVECTOR& pos = Position( desc, i );
temp += sprintf(temp, "P %8.2f %8.2f %8.2f ",
pos[0], pos[1], pos[2]);
}
Expand Down Expand Up @@ -2242,7 +2243,7 @@ void CBaseMeshDX8::Spew( int nVertexCount, int nIndexCount, const MeshDesc_t &sp

if ( fmt & VERTEX_NORMAL )
{
D3DXVECTOR3& normal = Normal( desc, i );
DirectX::XMVECTOR& normal = Normal( desc, i );
temp += sprintf(temp, "N %1.2f %1.2f %1.2f ",
normal[0], normal[1], normal[2]);
}
Expand All @@ -2265,14 +2266,14 @@ void CBaseMeshDX8::Spew( int nVertexCount, int nIndexCount, const MeshDesc_t &sp

if (fmt & VERTEX_TANGENT_S)
{
D3DXVECTOR3& tangentS = TangentS( desc, i );
DirectX::XMVECTOR& tangentS = TangentS( desc, i );
temp += sprintf(temp, "S %1.2f %1.2f %1.2f ",
tangentS[0], tangentS[1], tangentS[2]);
}

if (fmt & VERTEX_TANGENT_T)
{
D3DXVECTOR3& tangentT = TangentT( desc, i );
DirectX::XMVECTOR& tangentT = TangentT( desc, i );
temp += sprintf(temp, "T %1.2f %1.2f %1.2f ",
tangentT[0], tangentT[1], tangentT[2]);
}
Expand All @@ -2297,6 +2298,7 @@ void CBaseMeshDX8::Spew( int nVertexCount, int nIndexCount, const MeshDesc_t &sp
}
sprintf(temp,"\n");
Plat_DebugString( tempbuf );
#endif
}

void CBaseMeshDX8::ValidateData( int nVertexCount, int nIndexCount, const MeshDesc_t &spewDesc )
Expand All @@ -2320,7 +2322,7 @@ void CBaseMeshDX8::ValidateData( int nVertexCount, int nIndexCount, const MeshDe
{
if( fmt & VERTEX_POSITION )
{
D3DXVECTOR3& pos = Position( desc, i );
DirectX::XMVECTOR& pos = Position( desc, i );
Assert( IsFinite( pos[0] ) && IsFinite( pos[1] ) && IsFinite( pos[2] ) );
}
if( fmt & VERTEX_WRINKLE )
Expand All @@ -2346,7 +2348,7 @@ void CBaseMeshDX8::ValidateData( int nVertexCount, int nIndexCount, const MeshDe
}
if( fmt & VERTEX_NORMAL )
{
D3DXVECTOR3& normal = Normal( desc, i );
DirectX::XMVECTOR& normal = Normal( desc, i );
Assert( normal[0] >= -1.05f && normal[0] <= 1.05f );
Assert( normal[1] >= -1.05f && normal[1] <= 1.05f );
Assert( normal[2] >= -1.05f && normal[2] <= 1.05f );
Expand All @@ -2369,13 +2371,13 @@ void CBaseMeshDX8::ValidateData( int nVertexCount, int nIndexCount, const MeshDe

if (fmt & VERTEX_TANGENT_S)
{
D3DXVECTOR3& tangentS = TangentS( desc, i );
DirectX::XMVECTOR& tangentS = TangentS( desc, i );
Assert( IsFinite( tangentS[0] ) && IsFinite( tangentS[1] ) && IsFinite( tangentS[2] ) );
}

if (fmt & VERTEX_TANGENT_T)
{
D3DXVECTOR3& tangentT = TangentT( desc, i );
DirectX::XMVECTOR& tangentT = TangentT( desc, i );
Assert( IsFinite( tangentT[0] ) && IsFinite( tangentT[1] ) && IsFinite( tangentT[2] ) );
}
}
Expand Down Expand Up @@ -4142,13 +4144,13 @@ void CTempMeshDX8::CopyToMeshBuilder(
//-----------------------------------------------------------------------------
// Selection mode helper functions
//-----------------------------------------------------------------------------
static void ComputeModelToView( D3DXMATRIX& modelToView )
static void ComputeModelToView( DirectX::XMMATRIX& modelToView )
{
// Get the modelview matrix...
D3DXMATRIX world, view;
DirectX::XMMATRIX world, view;
ShaderAPI()->GetMatrix( MATERIAL_MODEL, (float*)&world );
ShaderAPI()->GetMatrix( MATERIAL_VIEW, (float*)&view );
D3DXMatrixMultiply( &modelToView, &world, &view );
modelToView = DirectX::XMMatrixMultiply( world, view );
}

static float ComputeCullFactor( )
Expand Down Expand Up @@ -4178,41 +4180,77 @@ static float ComputeCullFactor( )
// Clip to viewport
//-----------------------------------------------------------------------------
static int g_NumClipVerts;
static D3DXVECTOR3 g_ClipVerts[16];
static DirectX::XMVECTOR g_ClipVerts[16];

static float XMVec3Component( DirectX::XMVECTOR &flt3, int cmp )
{
switch (cmp)
{
case 0:
return DirectX::XMVectorGetX( flt3 );
break;
case 1:
return DirectX::XMVectorGetY( flt3 );
break;
case 2:
return DirectX::XMVectorGetZ( flt3 );
break;
default:
return DirectX::XMVectorGetX( flt3 );
}
}

static bool PointInsidePlane( D3DXVECTOR3* pVert, int normalInd, float val, bool nearClip )
static void XMVec3SetComponent( DirectX::XMVECTOR &flt3, int cmp, float val )
{
switch (cmp)
{
case 0:
flt3 = DirectX::XMVectorSetX( flt3, val );
break;
case 1:
flt3 = DirectX::XMVectorSetY( flt3, val );
break;
case 2:
flt3 = DirectX::XMVectorSetZ( flt3, val );
break;
default:
Assert( 0 );
}
}

static bool PointInsidePlane( DirectX::XMVECTOR* pVert, int normalInd, float val, bool nearClip )
{
if ((val > 0) || nearClip)
return (val - (*pVert)[normalInd] >= 0);
return (val - XMVec3Component( *pVert, normalInd ) >= 0);
else
return ((*pVert)[normalInd] - val >= 0);
return (XMVec3Component( *pVert, normalInd ) - val >= 0);
}

static void IntersectPlane( D3DXVECTOR3* pStart, D3DXVECTOR3* pEnd,
int normalInd, float val, D3DXVECTOR3* pOutVert )
static void IntersectPlane( DirectX::XMVECTOR* pStart, DirectX::XMVECTOR* pEnd,
int normalInd, float val, DirectX::XMVECTOR* pOutVert )
{
D3DXVECTOR3 dir;
D3DXVec3Subtract( &dir, pEnd, pStart );
Assert( dir[normalInd] != 0.0f );
float t = (val - (*pStart)[normalInd]) / dir[normalInd];
pOutVert->x = pStart->x + dir.x * t;
pOutVert->y = pStart->y + dir.y * t;
pOutVert->z = pStart->z + dir.z * t;
DirectX::XMVECTOR dir;
dir = DirectX::XMVectorSubtract( *pEnd, *pStart );
Assert( XMVec3Component( dir, normalInd ) != 0.0f );
float t = (val - XMVec3Component( *pStart, normalInd )) / XMVec3Component( dir, normalInd );
DirectX::XMVectorSetX( *pOutVert, XMVec3Component( *pStart, 0 ) + XMVec3Component( dir, 0 ) * t );
DirectX::XMVectorSetY( *pOutVert, XMVec3Component( *pStart, 1 ) + XMVec3Component( dir, 1 ) * t );
DirectX::XMVectorSetZ( *pOutVert, XMVec3Component( *pStart, 2 ) + XMVec3Component( dir, 2 ) * t );

// Avoid any precision problems.
(*pOutVert)[normalInd] = val;
XMVec3SetComponent( *pOutVert, normalInd, val );
}

static int ClipTriangleAgainstPlane( D3DXVECTOR3** ppVert, int nVertexCount,
D3DXVECTOR3** ppOutVert, int normalInd, float val, bool nearClip = false )
static int ClipTriangleAgainstPlane( DirectX::XMVECTOR** ppVert, int nVertexCount,
DirectX::XMVECTOR** ppOutVert, int normalInd, float val, bool nearClip = false )
{
// Ye Olde Sutherland-Hodgman clipping algorithm
int numOutVerts = 0;
D3DXVECTOR3* pStart = ppVert[nVertexCount-1];
DirectX::XMVECTOR* pStart = ppVert[nVertexCount-1];
bool startInside = PointInsidePlane( pStart, normalInd, val, nearClip );
for (int i = 0; i < nVertexCount; ++i)
{
D3DXVECTOR3* pEnd = ppVert[i];
DirectX::XMVECTOR* pEnd = ppVert[i];
bool endInside = PointInsidePlane( pEnd, normalInd, val, nearClip );
if (endInside)
{
Expand All @@ -4238,12 +4276,12 @@ static int ClipTriangleAgainstPlane( D3DXVECTOR3** ppVert, int nVertexCount,
return numOutVerts;
}

void CTempMeshDX8::ClipTriangle( D3DXVECTOR3** ppVert, float zNear, D3DXMATRIX& projection )
void CTempMeshDX8::ClipTriangle( DirectX::XMVECTOR** ppVert, float zNear, DirectX::XMMATRIX& projection )
{
int i;
int nVertexCount = 3;
D3DXVECTOR3* ppClipVert1[10];
D3DXVECTOR3* ppClipVert2[10];
DirectX::XMVECTOR* ppClipVert1[10];
DirectX::XMVECTOR* ppClipVert2[10];

g_NumClipVerts = 0;

Expand All @@ -4261,16 +4299,16 @@ void CTempMeshDX8::ClipTriangle( D3DXVECTOR3** ppVert, float zNear, D3DXMATRIX&
{
if (ppClipVert1[i] == &g_ClipVerts[0])
{
D3DXVec3TransformCoord( &g_ClipVerts[0], ppClipVert1[i], &projection );
g_ClipVerts[0] = DirectX::XMVector3TransformCoord( *ppClipVert1[i], projection );
}
else if (ppClipVert1[i] == &g_ClipVerts[1])
{
D3DXVec3TransformCoord( &g_ClipVerts[1], ppClipVert1[i], &projection );
g_ClipVerts[1] = DirectX::XMVector3TransformCoord( *ppClipVert1[i], projection );
}
else
{
D3DXVec3TransformCoord( &g_ClipVerts[g_NumClipVerts], ppClipVert1[i], &projection );
ppClipVert1[i] = &g_ClipVerts[g_NumClipVerts];
g_ClipVerts[g_NumClipVerts] = DirectX::XMVector3TransformCoord( *ppClipVert1[i], projection );
ppClipVert1[i] = &g_ClipVerts[g_NumClipVerts];
++g_NumClipVerts;
}
}
Expand Down Expand Up @@ -4334,14 +4372,14 @@ void CTempMeshDX8::ClipTriangle( D3DXVECTOR3** ppVert, float zNear, D3DXMATRIX&
#endif

// Compute closest and furthest verts
float minz = ppClipVert2[0]->z;
float maxz = ppClipVert2[0]->z;
float minz = XMVec3Component( *ppClipVert2[0], 2 );
float maxz = minz;
for ( i = 1; i < nVertexCount; ++i )
{
if (ppClipVert2[i]->z < minz)
minz = ppClipVert2[i]->z;
else if (ppClipVert2[i]->z > maxz)
maxz = ppClipVert2[i]->z;
if (XMVec3Component( *ppClipVert2[i], 2 ) < minz)
minz = XMVec3Component( *ppClipVert2[i], 2 );
else if (XMVec3Component( *ppClipVert2[i], 2 ) > maxz)
maxz = XMVec3Component( *ppClipVert2[i], 2 );
}

ShaderAPI()->RegisterSelectionHit( minz, maxz );
Expand All @@ -4360,14 +4398,15 @@ void CTempMeshDX8::TestSelection()
if ((m_Type != MATERIAL_TRIANGLES) && (m_Type != MATERIAL_TRIANGLE_STRIP))
return;

D3DXMATRIX modelToView, projection;
DirectX::XMMATRIX modelToView, projection;
ComputeModelToView( modelToView );
ShaderAPI()->GetMatrix( MATERIAL_PROJECTION, (float*)&projection );
float zNear = -projection.m[3][2] / projection.m[2][2];
// FIXME: Er2: XMMATRIX .m is in unnamed structure
float zNear = 0.1f; // -projection.m[3][2] / projection.m[2][2];

D3DXVECTOR3* pPos[3];
D3DXVECTOR3 edge[2];
D3DXVECTOR3 normal;
DirectX::XMVECTOR* pPos[3];
DirectX::XMVECTOR edge[2];
DirectX::XMVECTOR normal;

int numTriangles;
if (m_Type == MATERIAL_TRIANGLES)
Expand Down Expand Up @@ -4413,15 +4452,15 @@ void CTempMeshDX8::TestSelection()
for (int j = 0; j < 3; ++j)
{
int index = m_IndexData[indexPos];
D3DXVECTOR3* pPosition = (D3DXVECTOR3*)&m_VertexData[index * m_VertexSize];
DirectX::XMVECTOR* pPosition = (DirectX::XMVECTOR*)&m_VertexData[index * m_VertexSize];
if ((transformedVert[index >> 3] & (1 << (index & 0x7))) == 0)
{
D3DXVec3TransformCoord( pPosition, pPosition, &modelToView );
*pPosition = DirectX::XMVector3TransformCoord( *pPosition, modelToView );
transformedVert[index >> 3] |= (1 << (index & 0x7));
}

pPos[j] = pPosition;
if (pPos[j]->z < 0.0f)
if (XMVec3Component( *pPos[j], 2 ) < 0.0f)
inFrontIdx = j;
++indexPos;
}
Expand All @@ -4431,10 +4470,10 @@ void CTempMeshDX8::TestSelection()
continue;

// backface cull....
D3DXVec3Subtract( &edge[0], pPos[1], pPos[0] );
D3DXVec3Subtract( &edge[1], pPos[2], pPos[0] );
D3DXVec3Cross( &normal, &edge[0], &edge[1] );
float dot = D3DXVec3Dot( &normal, pPos[inFrontIdx] );
edge[0] = DirectX::XMVectorSubtract( *pPos[1], *pPos[0] );
edge[1] = DirectX::XMVectorSubtract( *pPos[2], *pPos[0] );
normal = DirectX::XMVector3Cross( edge[0], edge[1] );
float dot = DirectX::XMVectorGetX( DirectX::XMVector3Dot( normal, *pPos[inFrontIdx] ) );
if (dot * cullFactor > 0.0f)
continue;

Expand Down Expand Up @@ -5873,8 +5912,6 @@ void CMeshMgr::Draw( MaterialPrimitiveType_t primitiveType, int nFirstIndex, int

m_nFirstIndex = nFirstIndex;
m_nNumIndices = nIndexCount;

ShaderAPI()->DrawWithVertexAndIndexBuffers();
}

void CMeshMgr::RenderPassWithVertexAndIndexBuffers( void )
Expand Down
Loading

0 comments on commit 9426857

Please sign in to comment.