D3D: minor vs constant-setting optimization, remove a stupid memcpy that doesn't do anything, don't see much benefit though :/ At least the PIX logs will be cleaner.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@4301 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
hrydgard 2009-09-19 10:46:25 +00:00
parent 0d9543030b
commit dae1a68bfc
9 changed files with 81 additions and 30 deletions

View File

@ -100,9 +100,8 @@ void VertexShaderManager::SetConstants()
int startn = nNormalMatricesChanged[0] / 3; int startn = nNormalMatricesChanged[0] / 3;
int endn = (nNormalMatricesChanged[1] + 2) / 3; int endn = (nNormalMatricesChanged[1] + 2) / 3;
const float *pnstart = (const float*)&xfmem[XFMEM_NORMALMATRICES+3*startn]; const float *pnstart = (const float*)&xfmem[XFMEM_NORMALMATRICES+3*startn];
for (int i = startn; i < endn; ++i, pnstart += 3) SetMultiVSConstant3fv(C_NORMALMATRICES + startn, endn - startn, pnstart);
SetVSConstant4fv(C_NORMALMATRICES + i, pnstart); // looks like we're reading one too much.. nNormalMatricesChanged[0] = nNormalMatricesChanged[1] = -1;
nNormalMatricesChanged[0] = nNormalMatricesChanged[1] = -1;
} }
if (nPostTransformMatricesChanged[0] >= 0) if (nPostTransformMatricesChanged[0] >= 0)
@ -165,9 +164,7 @@ void VertexShaderManager::SetConstants()
const float *norm = (const float *)xfmem + XFMEM_NORMALMATRICES + 3 * (MatrixIndexA.PosNormalMtxIdx & 31); const float *norm = (const float *)xfmem + XFMEM_NORMALMATRICES + 3 * (MatrixIndexA.PosNormalMtxIdx & 31);
SetMultiVSConstant4fv(C_POSNORMALMATRIX, 3, pos); SetMultiVSConstant4fv(C_POSNORMALMATRIX, 3, pos);
SetVSConstant4fv(C_POSNORMALMATRIX+3, norm); SetMultiVSConstant3fv(C_POSNORMALMATRIX + 3, 3, norm);
SetVSConstant4fv(C_POSNORMALMATRIX+4, norm + 3);
SetVSConstant4fv(C_POSNORMALMATRIX+5, norm + 6);
} }
if (bTexMatricesChanged[0]) if (bTexMatricesChanged[0])

View File

@ -46,6 +46,7 @@ public:
void SetVSConstant4f(int const_number, float f1, float f2, float f3, float f4); void SetVSConstant4f(int const_number, float f1, float f2, float f3, float f4);
void SetVSConstant4fv(int const_number, const float *f); void SetVSConstant4fv(int const_number, const float *f);
void SetMultiVSConstant3fv(int const_number, int count, const float *f);
void SetMultiVSConstant4fv(int const_number, int count, const float *f); void SetMultiVSConstant4fv(int const_number, int count, const float *f);
#endif // _VERTEXSHADERMANAGER_H #endif // _VERTEXSHADERMANAGER_H

View File

@ -58,6 +58,7 @@ static DWORD m_RenderStates[MaxRenderStates];
static DWORD m_TextureStageStates[MaxTextureStages][MaxTextureTypes]; static DWORD m_TextureStageStates[MaxTextureStages][MaxTextureTypes];
static DWORD m_SamplerStates[MaxSamplerSize][MaxSamplerTypes]; static DWORD m_SamplerStates[MaxSamplerSize][MaxSamplerTypes];
LPDIRECT3DBASETEXTURE9 m_Textures[16]; LPDIRECT3DBASETEXTURE9 m_Textures[16];
LPDIRECT3DVERTEXDECLARATION9 m_VtxDecl;
void Enumerate(); void Enumerate();
@ -256,25 +257,18 @@ HRESULT Create(int adapter, HWND wnd, bool _fullscreen, int _resolution, int aa_
adapter, adapter,
D3DDEVTYPE_HAL, D3DDEVTYPE_HAL,
wnd, wnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, D3DCREATE_HARDWARE_VERTEXPROCESSING, // | D3DCREATE_PUREDEVICE, doesn't seem to make a difference
&d3dpp, &dev))) &d3dpp, &dev)))
{ {
MessageBox(wnd,
_T("Sorry, but it looks like your 3D accelerator is too old,\n")
_T("or doesn't support features that Dolphin requires.\n")
_T("Falling back to software vertex processing.\n"),
_T("Dolphin Direct3D plugin"), MB_OK | MB_ICONERROR);
if (FAILED(D3D->CreateDevice( if (FAILED(D3D->CreateDevice(
adapter, adapter,
D3DDEVTYPE_HAL, D3DDEVTYPE_HAL,
wnd, wnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
// |D3DCREATE_MULTITHREADED /* | D3DCREATE_PUREDEVICE*/,
//D3DCREATE_SOFTWARE_VERTEXPROCESSING ,
&d3dpp, &dev))) &d3dpp, &dev)))
{ {
MessageBox(wnd, MessageBox(wnd,
_T("Software VP failed too. Upgrade your graphics card."), _T("Failed to initialize Direct3D."),
_T("Dolphin Direct3D plugin"), MB_OK | MB_ICONERROR); _T("Dolphin Direct3D plugin"), MB_OK | MB_ICONERROR);
return E_FAIL; return E_FAIL;
} }
@ -442,6 +436,7 @@ void ApplyCachedState()
// so no stale state is around. // so no stale state is around.
memset(m_Textures, 0, sizeof(m_Textures)); memset(m_Textures, 0, sizeof(m_Textures));
memset(m_TextureStageStates, 0xFF, sizeof(m_TextureStageStates)); memset(m_TextureStageStates, 0xFF, sizeof(m_TextureStageStates));
m_VtxDecl = NULL;
} }
void SetTexture(DWORD Stage, LPDIRECT3DBASETEXTURE9 pTexture) void SetTexture(DWORD Stage, LPDIRECT3DBASETEXTURE9 pTexture)
@ -481,4 +476,17 @@ void SetSamplerState(DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value)
} }
} }
void SetVertexDeclaration(LPDIRECT3DVERTEXDECLARATION9 decl)
{
if (!decl) {
m_VtxDecl = NULL;
return;
}
if (decl != m_VtxDecl)
{
D3D::dev->SetVertexDeclaration(decl);
m_VtxDecl = decl;
}
}
} // namespace } // namespace

View File

@ -73,6 +73,7 @@ void SetTexture(DWORD Stage, IDirect3DBaseTexture9 *pTexture);
void SetRenderState(D3DRENDERSTATETYPE State, DWORD Value); void SetRenderState(D3DRENDERSTATETYPE State, DWORD Value);
void SetTextureStageState(DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value); void SetTextureStageState(DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value);
void SetSamplerState(DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value); void SetSamplerState(DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value);
void SetVertexDeclaration(LPDIRECT3DVERTEXDECLARATION9 decl);
void ApplyCachedState(); void ApplyCachedState();
// Utility functions for vendor specific hacks. So far, just the one. // Utility functions for vendor specific hacks. So far, just the one.

View File

@ -227,6 +227,7 @@ void CD3DFont::SetRenderStates()
dev->SetPixelShader(0); dev->SetPixelShader(0);
dev->SetVertexShader(0); dev->SetVertexShader(0);
D3D::SetVertexDeclaration(NULL); // throw away cached vtx decl
dev->SetFVF(D3DFVF_FONT2DVERTEX); dev->SetFVF(D3DFVF_FONT2DVERTEX);
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
@ -366,6 +367,7 @@ void quad2d(float x1, float y1, float x2, float y2, u32 color, float u1, float v
dev->SetVertexShader(0); dev->SetVertexShader(0);
dev->SetVertexDeclaration(0); dev->SetVertexDeclaration(0);
D3D::SetVertexDeclaration(NULL); // throw away cached vtx decl
dev->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1); dev->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
dev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, coords, sizeof(Q2DVertex)); dev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, coords, sizeof(Q2DVertex));

View File

@ -162,7 +162,7 @@ void D3DVertexFormat::Initialize(const PortableVertexDeclaration &_vtx_decl)
void D3DVertexFormat::SetupVertexPointers() const void D3DVertexFormat::SetupVertexPointers() const
{ {
if (d3d_decl) if (d3d_decl)
D3D::dev->SetVertexDeclaration(d3d_decl); D3D::SetVertexDeclaration(d3d_decl);
else else
ERROR_LOG(VIDEO, "invalid d3d decl"); ERROR_LOG(VIDEO, "invalid d3d decl");
} }

View File

@ -99,6 +99,7 @@ void Shutdown()
void CreateDeviceObjects() void CreateDeviceObjects()
{ {
} }
void DestroyDeviceObjects() void DestroyDeviceObjects()
@ -140,12 +141,8 @@ void AddVertices(int _primitive, int _numVertices)
if (collection != type) if (collection != type)
{ {
//We are NOT collecting the right type. // We are NOT collecting the right type.
Flush(); Flush();
// Copy the extra verts that we lost.
memcpy(s_pCurBufferPointer, fakeVBuffer, _numVertices * g_nativeVertexFmt->GetVertexStride());
collection = type; collection = type;
u16 *ptr = 0; u16 *ptr = 0;
if (type != C_POINTS) if (type != C_POINTS)

View File

@ -43,11 +43,11 @@ void SetVSConstant4f(int const_number, float f1, float f2, float f3, float f4)
lastVSconstants[const_number][3] != f4) lastVSconstants[const_number][3] != f4)
{ {
const float f[4] = {f1, f2, f3, f4}; const float f[4] = {f1, f2, f3, f4};
D3D::dev->SetVertexShaderConstantF(const_number, f, 1);
lastVSconstants[const_number][0] = f1; lastVSconstants[const_number][0] = f1;
lastVSconstants[const_number][1] = f2; lastVSconstants[const_number][1] = f2;
lastVSconstants[const_number][2] = f3; lastVSconstants[const_number][2] = f3;
lastVSconstants[const_number][3] = f4; lastVSconstants[const_number][3] = f4;
D3D::dev->SetVertexShaderConstantF(const_number, lastVSconstants[const_number], 1);
} }
} }
@ -58,11 +58,37 @@ void SetVSConstant4fv(int const_number, const float *f)
lastVSconstants[const_number][2] != f[2] || lastVSconstants[const_number][2] != f[2] ||
lastVSconstants[const_number][3] != f[3]) lastVSconstants[const_number][3] != f[3])
{ {
D3D::dev->SetVertexShaderConstantF(const_number, f, 1);
lastVSconstants[const_number][0] = f[0]; lastVSconstants[const_number][0] = f[0];
lastVSconstants[const_number][1] = f[1]; lastVSconstants[const_number][1] = f[1];
lastVSconstants[const_number][2] = f[2]; lastVSconstants[const_number][2] = f[2];
lastVSconstants[const_number][3] = f[3]; lastVSconstants[const_number][3] = f[3];
D3D::dev->SetVertexShaderConstantF(const_number, lastVSconstants[const_number], 1);
}
}
void SetMultiVSConstant3fv(int const_number, int count, const float *f)
{
bool change = false;
for (int i = 0; i < count; i++)
{
if (lastVSconstants[const_number + i][0] != f[0 + i*3] ||
lastVSconstants[const_number + i][1] != f[1 + i*3] ||
lastVSconstants[const_number + i][2] != f[2 + i*3])
{
change = true;
break;
}
}
if (change)
{
for (int i = 0; i < count; i++)
{
lastVSconstants[const_number + i][0] = f[0 + i*3];
lastVSconstants[const_number + i][1] = f[1 + i*3];
lastVSconstants[const_number + i][2] = f[2 + i*3];
lastVSconstants[const_number + i][3] = 0.0f;
}
D3D::dev->SetVertexShaderConstantF(const_number, lastVSconstants[const_number], count);
} }
} }
@ -82,13 +108,14 @@ void SetMultiVSConstant4fv(int const_number, int count, const float *f)
} }
if (change) if (change)
{ {
D3D::dev->SetVertexShaderConstantF(const_number, f, count); for (int i = 0; i < count; i++)
for (int i = 0; i < count; i++) { {
lastVSconstants[const_number + i][0] = f[0 + i*4]; lastVSconstants[const_number + i][0] = f[0 + i*4];
lastVSconstants[const_number + i][1] = f[1 + i*4]; lastVSconstants[const_number + i][1] = f[1 + i*4];
lastVSconstants[const_number + i][2] = f[2 + i*4]; lastVSconstants[const_number + i][2] = f[2 + i*4];
lastVSconstants[const_number + i][3] = f[3 + i*4]; lastVSconstants[const_number + i][3] = f[3 + i*4];
} }
D3D::dev->SetVertexShaderConstantF(const_number, lastVSconstants[const_number], count);
} }
} }

View File

@ -50,12 +50,11 @@ void SetVSConstant4f(int const_number, float f1, float f2, float f3, float f4)
lastVSconstants[const_number][2] != f3 || lastVSconstants[const_number][2] != f3 ||
lastVSconstants[const_number][3] != f4) lastVSconstants[const_number][3] != f4)
{ {
const float f[4] = {f1, f2, f3, f4};
glProgramEnvParameter4fARB(GL_VERTEX_PROGRAM_ARB, const_number, f1, f2, f3, f4);
lastVSconstants[const_number][0] = f1; lastVSconstants[const_number][0] = f1;
lastVSconstants[const_number][1] = f2; lastVSconstants[const_number][1] = f2;
lastVSconstants[const_number][2] = f3; lastVSconstants[const_number][2] = f3;
lastVSconstants[const_number][3] = f4; lastVSconstants[const_number][3] = f4;
glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number, lastVSconstants[const_number]);
} }
} }
@ -66,11 +65,11 @@ void SetVSConstant4fv(int const_number, const float *f)
lastVSconstants[const_number][2] != f[2] || lastVSconstants[const_number][2] != f[2] ||
lastVSconstants[const_number][3] != f[3]) lastVSconstants[const_number][3] != f[3])
{ {
glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number, f);
lastVSconstants[const_number][0] = f[0]; lastVSconstants[const_number][0] = f[0];
lastVSconstants[const_number][1] = f[1]; lastVSconstants[const_number][1] = f[1];
lastVSconstants[const_number][2] = f[2]; lastVSconstants[const_number][2] = f[2];
lastVSconstants[const_number][3] = f[3]; lastVSconstants[const_number][3] = f[3];
glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number, lastVSconstants[const_number]);
} }
} }
@ -83,15 +82,34 @@ void SetMultiVSConstant4fv(int const_number, int count, const float *f)
lastVSconstants[const_number + i][2] != f[2 + i*4] || lastVSconstants[const_number + i][2] != f[2 + i*4] ||
lastVSconstants[const_number + i][3] != f[3 + i*4]) lastVSconstants[const_number + i][3] != f[3 + i*4])
{ {
glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number + i, f + i * 4);
lastVSconstants[const_number + i][0] = f[0 + i*4]; lastVSconstants[const_number + i][0] = f[0 + i*4];
lastVSconstants[const_number + i][1] = f[1 + i*4]; lastVSconstants[const_number + i][1] = f[1 + i*4];
lastVSconstants[const_number + i][2] = f[2 + i*4]; lastVSconstants[const_number + i][2] = f[2 + i*4];
lastVSconstants[const_number + i][3] = f[3 + i*4]; lastVSconstants[const_number + i][3] = f[3 + i*4];
glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number + i, lastVSconstants[const_number + i]);
} }
} }
} }
void SetMultiVSConstant3fv(int const_number, int count, const float *f)
{
for (int i = 0; i < count; i++)
{
if (lastVSconstants[const_number + i][0] != f[0 + i*3] ||
lastVSconstants[const_number + i][1] != f[1 + i*3] ||
lastVSconstants[const_number + i][2] != f[2 + i*3] ||
lastVSconstants[const_number + i][3] != 0.0f)
{
lastVSconstants[const_number + i][0] = f[0 + i*3];
lastVSconstants[const_number + i][1] = f[1 + i*3];
lastVSconstants[const_number + i][2] = f[2 + i*3];
lastVSconstants[const_number + i][3] = 0.0f;
glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number + i, lastVSconstants[const_number + i]);
}
}
}
void VertexShaderCache::Init() void VertexShaderCache::Init()
{ {
for( int i=0;i<(C_FOGPARAMS+8)*4;i++) for( int i=0;i<(C_FOGPARAMS+8)*4;i++)