Skip to content

Commit 9426857

Browse files
committed
fuck up d3dx9math to replace with directxmath
1 parent 512a597 commit 9426857

File tree

5 files changed

+123
-619
lines changed

5 files changed

+123
-619
lines changed

materialsystem/shaderapidx9/locald3dtypes.h

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ typedef intp PixelShader_t;
128128
#define D3DRS_NOTSUPPORTED D3DRS_FORCE_DWORD
129129

130130
#include "togl/rendermechanism.h"
131+
#include <DirectXMath.h>
131132

132133
#if defined( _X360 )
133134

materialsystem/shaderapidx9/meshdx8.cpp

+104-67
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ class CTempMeshDX8 : public CBaseMeshDX8
622622
private:
623623
// Selection mode
624624
void TestSelection( );
625-
void ClipTriangle( D3DXVECTOR3 **ppVert, float zNear, D3DXMATRIX &proj );
625+
void ClipTriangle( DirectX::XMVECTOR **ppVert, float zNear, DirectX::XMMATRIX &proj );
626626

627627
CDynamicMeshDX8 *GetDynamicMesh();
628628

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

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

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

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

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

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

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

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

19921992

@@ -2176,6 +2176,7 @@ inline void CBaseMeshDX8::DrawMesh( )
21762176
void CBaseMeshDX8::Spew( int nVertexCount, int nIndexCount, const MeshDesc_t &spewDesc )
21772177
{
21782178
LOCK_SHADERAPI();
2179+
#if 0
21792180
// This has regressed.
21802181
int i;
21812182

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

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

22662267
if (fmt & VERTEX_TANGENT_S)
22672268
{
2268-
D3DXVECTOR3& tangentS = TangentS( desc, i );
2269+
DirectX::XMVECTOR& tangentS = TangentS( desc, i );
22692270
temp += sprintf(temp, "S %1.2f %1.2f %1.2f ",
22702271
tangentS[0], tangentS[1], tangentS[2]);
22712272
}
22722273

22732274
if (fmt & VERTEX_TANGENT_T)
22742275
{
2275-
D3DXVECTOR3& tangentT = TangentT( desc, i );
2276+
DirectX::XMVECTOR& tangentT = TangentT( desc, i );
22762277
temp += sprintf(temp, "T %1.2f %1.2f %1.2f ",
22772278
tangentT[0], tangentT[1], tangentT[2]);
22782279
}
@@ -2297,6 +2298,7 @@ void CBaseMeshDX8::Spew( int nVertexCount, int nIndexCount, const MeshDesc_t &sp
22972298
}
22982299
sprintf(temp,"\n");
22992300
Plat_DebugString( tempbuf );
2301+
#endif
23002302
}
23012303

23022304
void CBaseMeshDX8::ValidateData( int nVertexCount, int nIndexCount, const MeshDesc_t &spewDesc )
@@ -2320,7 +2322,7 @@ void CBaseMeshDX8::ValidateData( int nVertexCount, int nIndexCount, const MeshDe
23202322
{
23212323
if( fmt & VERTEX_POSITION )
23222324
{
2323-
D3DXVECTOR3& pos = Position( desc, i );
2325+
DirectX::XMVECTOR& pos = Position( desc, i );
23242326
Assert( IsFinite( pos[0] ) && IsFinite( pos[1] ) && IsFinite( pos[2] ) );
23252327
}
23262328
if( fmt & VERTEX_WRINKLE )
@@ -2346,7 +2348,7 @@ void CBaseMeshDX8::ValidateData( int nVertexCount, int nIndexCount, const MeshDe
23462348
}
23472349
if( fmt & VERTEX_NORMAL )
23482350
{
2349-
D3DXVECTOR3& normal = Normal( desc, i );
2351+
DirectX::XMVECTOR& normal = Normal( desc, i );
23502352
Assert( normal[0] >= -1.05f && normal[0] <= 1.05f );
23512353
Assert( normal[1] >= -1.05f && normal[1] <= 1.05f );
23522354
Assert( normal[2] >= -1.05f && normal[2] <= 1.05f );
@@ -2369,13 +2371,13 @@ void CBaseMeshDX8::ValidateData( int nVertexCount, int nIndexCount, const MeshDe
23692371

23702372
if (fmt & VERTEX_TANGENT_S)
23712373
{
2372-
D3DXVECTOR3& tangentS = TangentS( desc, i );
2374+
DirectX::XMVECTOR& tangentS = TangentS( desc, i );
23732375
Assert( IsFinite( tangentS[0] ) && IsFinite( tangentS[1] ) && IsFinite( tangentS[2] ) );
23742376
}
23752377

23762378
if (fmt & VERTEX_TANGENT_T)
23772379
{
2378-
D3DXVECTOR3& tangentT = TangentT( desc, i );
2380+
DirectX::XMVECTOR& tangentT = TangentT( desc, i );
23792381
Assert( IsFinite( tangentT[0] ) && IsFinite( tangentT[1] ) && IsFinite( tangentT[2] ) );
23802382
}
23812383
}
@@ -4142,13 +4144,13 @@ void CTempMeshDX8::CopyToMeshBuilder(
41424144
//-----------------------------------------------------------------------------
41434145
// Selection mode helper functions
41444146
//-----------------------------------------------------------------------------
4145-
static void ComputeModelToView( D3DXMATRIX& modelToView )
4147+
static void ComputeModelToView( DirectX::XMMATRIX& modelToView )
41464148
{
41474149
// Get the modelview matrix...
4148-
D3DXMATRIX world, view;
4150+
DirectX::XMMATRIX world, view;
41494151
ShaderAPI()->GetMatrix( MATERIAL_MODEL, (float*)&world );
41504152
ShaderAPI()->GetMatrix( MATERIAL_VIEW, (float*)&view );
4151-
D3DXMatrixMultiply( &modelToView, &world, &view );
4153+
modelToView = DirectX::XMMatrixMultiply( world, view );
41524154
}
41534155

41544156
static float ComputeCullFactor( )
@@ -4178,41 +4180,77 @@ static float ComputeCullFactor( )
41784180
// Clip to viewport
41794181
//-----------------------------------------------------------------------------
41804182
static int g_NumClipVerts;
4181-
static D3DXVECTOR3 g_ClipVerts[16];
4183+
static DirectX::XMVECTOR g_ClipVerts[16];
4184+
4185+
static float XMVec3Component( DirectX::XMVECTOR &flt3, int cmp )
4186+
{
4187+
switch (cmp)
4188+
{
4189+
case 0:
4190+
return DirectX::XMVectorGetX( flt3 );
4191+
break;
4192+
case 1:
4193+
return DirectX::XMVectorGetY( flt3 );
4194+
break;
4195+
case 2:
4196+
return DirectX::XMVectorGetZ( flt3 );
4197+
break;
4198+
default:
4199+
return DirectX::XMVectorGetX( flt3 );
4200+
}
4201+
}
41824202

4183-
static bool PointInsidePlane( D3DXVECTOR3* pVert, int normalInd, float val, bool nearClip )
4203+
static void XMVec3SetComponent( DirectX::XMVECTOR &flt3, int cmp, float val )
4204+
{
4205+
switch (cmp)
4206+
{
4207+
case 0:
4208+
flt3 = DirectX::XMVectorSetX( flt3, val );
4209+
break;
4210+
case 1:
4211+
flt3 = DirectX::XMVectorSetY( flt3, val );
4212+
break;
4213+
case 2:
4214+
flt3 = DirectX::XMVectorSetZ( flt3, val );
4215+
break;
4216+
default:
4217+
Assert( 0 );
4218+
}
4219+
}
4220+
4221+
static bool PointInsidePlane( DirectX::XMVECTOR* pVert, int normalInd, float val, bool nearClip )
41844222
{
41854223
if ((val > 0) || nearClip)
4186-
return (val - (*pVert)[normalInd] >= 0);
4224+
return (val - XMVec3Component( *pVert, normalInd ) >= 0);
41874225
else
4188-
return ((*pVert)[normalInd] - val >= 0);
4226+
return (XMVec3Component( *pVert, normalInd ) - val >= 0);
41894227
}
41904228

4191-
static void IntersectPlane( D3DXVECTOR3* pStart, D3DXVECTOR3* pEnd,
4192-
int normalInd, float val, D3DXVECTOR3* pOutVert )
4229+
static void IntersectPlane( DirectX::XMVECTOR* pStart, DirectX::XMVECTOR* pEnd,
4230+
int normalInd, float val, DirectX::XMVECTOR* pOutVert )
41934231
{
4194-
D3DXVECTOR3 dir;
4195-
D3DXVec3Subtract( &dir, pEnd, pStart );
4196-
Assert( dir[normalInd] != 0.0f );
4197-
float t = (val - (*pStart)[normalInd]) / dir[normalInd];
4198-
pOutVert->x = pStart->x + dir.x * t;
4199-
pOutVert->y = pStart->y + dir.y * t;
4200-
pOutVert->z = pStart->z + dir.z * t;
4232+
DirectX::XMVECTOR dir;
4233+
dir = DirectX::XMVectorSubtract( *pEnd, *pStart );
4234+
Assert( XMVec3Component( dir, normalInd ) != 0.0f );
4235+
float t = (val - XMVec3Component( *pStart, normalInd )) / XMVec3Component( dir, normalInd );
4236+
DirectX::XMVectorSetX( *pOutVert, XMVec3Component( *pStart, 0 ) + XMVec3Component( dir, 0 ) * t );
4237+
DirectX::XMVectorSetY( *pOutVert, XMVec3Component( *pStart, 1 ) + XMVec3Component( dir, 1 ) * t );
4238+
DirectX::XMVectorSetZ( *pOutVert, XMVec3Component( *pStart, 2 ) + XMVec3Component( dir, 2 ) * t );
42014239

42024240
// Avoid any precision problems.
4203-
(*pOutVert)[normalInd] = val;
4241+
XMVec3SetComponent( *pOutVert, normalInd, val );
42044242
}
42054243

4206-
static int ClipTriangleAgainstPlane( D3DXVECTOR3** ppVert, int nVertexCount,
4207-
D3DXVECTOR3** ppOutVert, int normalInd, float val, bool nearClip = false )
4244+
static int ClipTriangleAgainstPlane( DirectX::XMVECTOR** ppVert, int nVertexCount,
4245+
DirectX::XMVECTOR** ppOutVert, int normalInd, float val, bool nearClip = false )
42084246
{
42094247
// Ye Olde Sutherland-Hodgman clipping algorithm
42104248
int numOutVerts = 0;
4211-
D3DXVECTOR3* pStart = ppVert[nVertexCount-1];
4249+
DirectX::XMVECTOR* pStart = ppVert[nVertexCount-1];
42124250
bool startInside = PointInsidePlane( pStart, normalInd, val, nearClip );
42134251
for (int i = 0; i < nVertexCount; ++i)
42144252
{
4215-
D3DXVECTOR3* pEnd = ppVert[i];
4253+
DirectX::XMVECTOR* pEnd = ppVert[i];
42164254
bool endInside = PointInsidePlane( pEnd, normalInd, val, nearClip );
42174255
if (endInside)
42184256
{
@@ -4238,12 +4276,12 @@ static int ClipTriangleAgainstPlane( D3DXVECTOR3** ppVert, int nVertexCount,
42384276
return numOutVerts;
42394277
}
42404278

4241-
void CTempMeshDX8::ClipTriangle( D3DXVECTOR3** ppVert, float zNear, D3DXMATRIX& projection )
4279+
void CTempMeshDX8::ClipTriangle( DirectX::XMVECTOR** ppVert, float zNear, DirectX::XMMATRIX& projection )
42424280
{
42434281
int i;
42444282
int nVertexCount = 3;
4245-
D3DXVECTOR3* ppClipVert1[10];
4246-
D3DXVECTOR3* ppClipVert2[10];
4283+
DirectX::XMVECTOR* ppClipVert1[10];
4284+
DirectX::XMVECTOR* ppClipVert2[10];
42474285

42484286
g_NumClipVerts = 0;
42494287

@@ -4261,16 +4299,16 @@ void CTempMeshDX8::ClipTriangle( D3DXVECTOR3** ppVert, float zNear, D3DXMATRIX&
42614299
{
42624300
if (ppClipVert1[i] == &g_ClipVerts[0])
42634301
{
4264-
D3DXVec3TransformCoord( &g_ClipVerts[0], ppClipVert1[i], &projection );
4302+
g_ClipVerts[0] = DirectX::XMVector3TransformCoord( *ppClipVert1[i], projection );
42654303
}
42664304
else if (ppClipVert1[i] == &g_ClipVerts[1])
42674305
{
4268-
D3DXVec3TransformCoord( &g_ClipVerts[1], ppClipVert1[i], &projection );
4306+
g_ClipVerts[1] = DirectX::XMVector3TransformCoord( *ppClipVert1[i], projection );
42694307
}
42704308
else
42714309
{
4272-
D3DXVec3TransformCoord( &g_ClipVerts[g_NumClipVerts], ppClipVert1[i], &projection );
4273-
ppClipVert1[i] = &g_ClipVerts[g_NumClipVerts];
4310+
g_ClipVerts[g_NumClipVerts] = DirectX::XMVector3TransformCoord( *ppClipVert1[i], projection );
4311+
ppClipVert1[i] = &g_ClipVerts[g_NumClipVerts];
42744312
++g_NumClipVerts;
42754313
}
42764314
}
@@ -4334,14 +4372,14 @@ void CTempMeshDX8::ClipTriangle( D3DXVECTOR3** ppVert, float zNear, D3DXMATRIX&
43344372
#endif
43354373

43364374
// Compute closest and furthest verts
4337-
float minz = ppClipVert2[0]->z;
4338-
float maxz = ppClipVert2[0]->z;
4375+
float minz = XMVec3Component( *ppClipVert2[0], 2 );
4376+
float maxz = minz;
43394377
for ( i = 1; i < nVertexCount; ++i )
43404378
{
4341-
if (ppClipVert2[i]->z < minz)
4342-
minz = ppClipVert2[i]->z;
4343-
else if (ppClipVert2[i]->z > maxz)
4344-
maxz = ppClipVert2[i]->z;
4379+
if (XMVec3Component( *ppClipVert2[i], 2 ) < minz)
4380+
minz = XMVec3Component( *ppClipVert2[i], 2 );
4381+
else if (XMVec3Component( *ppClipVert2[i], 2 ) > maxz)
4382+
maxz = XMVec3Component( *ppClipVert2[i], 2 );
43454383
}
43464384

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

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

4368-
D3DXVECTOR3* pPos[3];
4369-
D3DXVECTOR3 edge[2];
4370-
D3DXVECTOR3 normal;
4407+
DirectX::XMVECTOR* pPos[3];
4408+
DirectX::XMVECTOR edge[2];
4409+
DirectX::XMVECTOR normal;
43714410

43724411
int numTriangles;
43734412
if (m_Type == MATERIAL_TRIANGLES)
@@ -4413,15 +4452,15 @@ void CTempMeshDX8::TestSelection()
44134452
for (int j = 0; j < 3; ++j)
44144453
{
44154454
int index = m_IndexData[indexPos];
4416-
D3DXVECTOR3* pPosition = (D3DXVECTOR3*)&m_VertexData[index * m_VertexSize];
4455+
DirectX::XMVECTOR* pPosition = (DirectX::XMVECTOR*)&m_VertexData[index * m_VertexSize];
44174456
if ((transformedVert[index >> 3] & (1 << (index & 0x7))) == 0)
44184457
{
4419-
D3DXVec3TransformCoord( pPosition, pPosition, &modelToView );
4458+
*pPosition = DirectX::XMVector3TransformCoord( *pPosition, modelToView );
44204459
transformedVert[index >> 3] |= (1 << (index & 0x7));
44214460
}
44224461

44234462
pPos[j] = pPosition;
4424-
if (pPos[j]->z < 0.0f)
4463+
if (XMVec3Component( *pPos[j], 2 ) < 0.0f)
44254464
inFrontIdx = j;
44264465
++indexPos;
44274466
}
@@ -4431,10 +4470,10 @@ void CTempMeshDX8::TestSelection()
44314470
continue;
44324471

44334472
// backface cull....
4434-
D3DXVec3Subtract( &edge[0], pPos[1], pPos[0] );
4435-
D3DXVec3Subtract( &edge[1], pPos[2], pPos[0] );
4436-
D3DXVec3Cross( &normal, &edge[0], &edge[1] );
4437-
float dot = D3DXVec3Dot( &normal, pPos[inFrontIdx] );
4473+
edge[0] = DirectX::XMVectorSubtract( *pPos[1], *pPos[0] );
4474+
edge[1] = DirectX::XMVectorSubtract( *pPos[2], *pPos[0] );
4475+
normal = DirectX::XMVector3Cross( edge[0], edge[1] );
4476+
float dot = DirectX::XMVectorGetX( DirectX::XMVector3Dot( normal, *pPos[inFrontIdx] ) );
44384477
if (dot * cullFactor > 0.0f)
44394478
continue;
44404479

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

58745913
m_nFirstIndex = nFirstIndex;
58755914
m_nNumIndices = nIndexCount;
5876-
5877-
ShaderAPI()->DrawWithVertexAndIndexBuffers();
58785915
}
58795916

58805917
void CMeshMgr::RenderPassWithVertexAndIndexBuffers( void )

0 commit comments

Comments
 (0)