From dae1a68bfc1d2f4e2b08cecb09afd94894e85262 Mon Sep 17 00:00:00 2001 From: hrydgard Date: Sat, 19 Sep 2009 10:46:25 +0000 Subject: [PATCH] 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 --- .../VideoCommon/Src/VertexShaderManager.cpp | 9 ++--- .../VideoCommon/Src/VertexShaderManager.h | 1 + .../Plugins/Plugin_VideoDX9/Src/D3DBase.cpp | 28 +++++++++------ Source/Plugins/Plugin_VideoDX9/Src/D3DBase.h | 1 + .../Plugins/Plugin_VideoDX9/Src/D3DUtil.cpp | 2 ++ .../Src/NativeVertexFormat.cpp | 2 +- .../Plugin_VideoDX9/Src/VertexManager.cpp | 7 ++-- .../Plugin_VideoDX9/Src/VertexShaderCache.cpp | 35 ++++++++++++++++--- .../Plugin_VideoOGL/Src/VertexShaderCache.cpp | 26 +++++++++++--- 9 files changed, 81 insertions(+), 30 deletions(-) diff --git a/Source/Core/VideoCommon/Src/VertexShaderManager.cpp b/Source/Core/VideoCommon/Src/VertexShaderManager.cpp index c90e8531f9..3a5936e3fe 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderManager.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderManager.cpp @@ -100,9 +100,8 @@ void VertexShaderManager::SetConstants() int startn = nNormalMatricesChanged[0] / 3; int endn = (nNormalMatricesChanged[1] + 2) / 3; const float *pnstart = (const float*)&xfmem[XFMEM_NORMALMATRICES+3*startn]; - for (int i = startn; i < endn; ++i, pnstart += 3) - SetVSConstant4fv(C_NORMALMATRICES + i, pnstart); // looks like we're reading one too much.. - nNormalMatricesChanged[0] = nNormalMatricesChanged[1] = -1; + SetMultiVSConstant3fv(C_NORMALMATRICES + startn, endn - startn, pnstart); + nNormalMatricesChanged[0] = nNormalMatricesChanged[1] = -1; } if (nPostTransformMatricesChanged[0] >= 0) @@ -165,9 +164,7 @@ void VertexShaderManager::SetConstants() const float *norm = (const float *)xfmem + XFMEM_NORMALMATRICES + 3 * (MatrixIndexA.PosNormalMtxIdx & 31); SetMultiVSConstant4fv(C_POSNORMALMATRIX, 3, pos); - SetVSConstant4fv(C_POSNORMALMATRIX+3, norm); - SetVSConstant4fv(C_POSNORMALMATRIX+4, norm + 3); - SetVSConstant4fv(C_POSNORMALMATRIX+5, norm + 6); + SetMultiVSConstant3fv(C_POSNORMALMATRIX + 3, 3, norm); } if (bTexMatricesChanged[0]) diff --git a/Source/Core/VideoCommon/Src/VertexShaderManager.h b/Source/Core/VideoCommon/Src/VertexShaderManager.h index 619c3af5de..be7a506716 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderManager.h +++ b/Source/Core/VideoCommon/Src/VertexShaderManager.h @@ -46,6 +46,7 @@ public: void SetVSConstant4f(int const_number, float f1, float f2, float f3, float f4); 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); #endif // _VERTEXSHADERMANAGER_H diff --git a/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.cpp b/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.cpp index 896eebb3a2..b37a4e4c29 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.cpp @@ -58,6 +58,7 @@ static DWORD m_RenderStates[MaxRenderStates]; static DWORD m_TextureStageStates[MaxTextureStages][MaxTextureTypes]; static DWORD m_SamplerStates[MaxSamplerSize][MaxSamplerTypes]; LPDIRECT3DBASETEXTURE9 m_Textures[16]; +LPDIRECT3DVERTEXDECLARATION9 m_VtxDecl; void Enumerate(); @@ -256,25 +257,18 @@ HRESULT Create(int adapter, HWND wnd, bool _fullscreen, int _resolution, int aa_ adapter, D3DDEVTYPE_HAL, wnd, - D3DCREATE_HARDWARE_VERTEXPROCESSING, + D3DCREATE_HARDWARE_VERTEXPROCESSING, // | D3DCREATE_PUREDEVICE, doesn't seem to make a difference &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( adapter, D3DDEVTYPE_HAL, wnd, - D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, - // |D3DCREATE_MULTITHREADED /* | D3DCREATE_PUREDEVICE*/, - //D3DCREATE_SOFTWARE_VERTEXPROCESSING , + D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &dev))) { MessageBox(wnd, - _T("Software VP failed too. Upgrade your graphics card."), + _T("Failed to initialize Direct3D."), _T("Dolphin Direct3D plugin"), MB_OK | MB_ICONERROR); return E_FAIL; } @@ -442,6 +436,7 @@ void ApplyCachedState() // so no stale state is around. memset(m_Textures, 0, sizeof(m_Textures)); memset(m_TextureStageStates, 0xFF, sizeof(m_TextureStageStates)); + m_VtxDecl = NULL; } 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 diff --git a/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.h b/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.h index 6fcbe675bf..557eef1d9e 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.h +++ b/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.h @@ -73,6 +73,7 @@ void SetTexture(DWORD Stage, IDirect3DBaseTexture9 *pTexture); void SetRenderState(D3DRENDERSTATETYPE State, DWORD Value); void SetTextureStageState(DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value); void SetSamplerState(DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value); +void SetVertexDeclaration(LPDIRECT3DVERTEXDECLARATION9 decl); void ApplyCachedState(); // Utility functions for vendor specific hacks. So far, just the one. diff --git a/Source/Plugins/Plugin_VideoDX9/Src/D3DUtil.cpp b/Source/Plugins/Plugin_VideoDX9/Src/D3DUtil.cpp index 600c470fc9..a1b30a3dc2 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/D3DUtil.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/D3DUtil.cpp @@ -227,6 +227,7 @@ void CD3DFont::SetRenderStates() dev->SetPixelShader(0); dev->SetVertexShader(0); + D3D::SetVertexDeclaration(NULL); // throw away cached vtx decl dev->SetFVF(D3DFVF_FONT2DVERTEX); 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->SetVertexDeclaration(0); + D3D::SetVertexDeclaration(NULL); // throw away cached vtx decl dev->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1); dev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, coords, sizeof(Q2DVertex)); diff --git a/Source/Plugins/Plugin_VideoDX9/Src/NativeVertexFormat.cpp b/Source/Plugins/Plugin_VideoDX9/Src/NativeVertexFormat.cpp index f4891bdcd5..1717861db2 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/NativeVertexFormat.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/NativeVertexFormat.cpp @@ -162,7 +162,7 @@ void D3DVertexFormat::Initialize(const PortableVertexDeclaration &_vtx_decl) void D3DVertexFormat::SetupVertexPointers() const { if (d3d_decl) - D3D::dev->SetVertexDeclaration(d3d_decl); + D3D::SetVertexDeclaration(d3d_decl); else ERROR_LOG(VIDEO, "invalid d3d decl"); } \ No newline at end of file diff --git a/Source/Plugins/Plugin_VideoDX9/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoDX9/Src/VertexManager.cpp index e89cfc56aa..516bcb0be5 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/VertexManager.cpp @@ -99,6 +99,7 @@ void Shutdown() void CreateDeviceObjects() { + } void DestroyDeviceObjects() @@ -140,12 +141,8 @@ void AddVertices(int _primitive, int _numVertices) if (collection != type) { - //We are NOT collecting the right type. + // We are NOT collecting the right type. Flush(); - - // Copy the extra verts that we lost. - memcpy(s_pCurBufferPointer, fakeVBuffer, _numVertices * g_nativeVertexFmt->GetVertexStride()); - collection = type; u16 *ptr = 0; if (type != C_POINTS) diff --git a/Source/Plugins/Plugin_VideoDX9/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoDX9/Src/VertexShaderCache.cpp index 5beace957b..5409c217b3 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/VertexShaderCache.cpp @@ -43,11 +43,11 @@ void SetVSConstant4f(int const_number, float f1, float f2, float f3, float f4) lastVSconstants[const_number][3] != f4) { const float f[4] = {f1, f2, f3, f4}; - D3D::dev->SetVertexShaderConstantF(const_number, f, 1); lastVSconstants[const_number][0] = f1; lastVSconstants[const_number][1] = f2; lastVSconstants[const_number][2] = f3; 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][3] != f[3]) { - D3D::dev->SetVertexShaderConstantF(const_number, f, 1); lastVSconstants[const_number][0] = f[0]; lastVSconstants[const_number][1] = f[1]; lastVSconstants[const_number][2] = f[2]; 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) { - 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][1] = f[1 + i*4]; lastVSconstants[const_number + i][2] = f[2 + i*4]; lastVSconstants[const_number + i][3] = f[3 + i*4]; } + D3D::dev->SetVertexShaderConstantF(const_number, lastVSconstants[const_number], count); } } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp index defa2ca440..2d6cb30ddf 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderCache.cpp @@ -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][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][1] = f2; lastVSconstants[const_number][2] = f3; 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][3] != f[3]) { - glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, const_number, f); lastVSconstants[const_number][0] = f[0]; lastVSconstants[const_number][1] = f[1]; lastVSconstants[const_number][2] = f[2]; 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][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][1] = f[1 + i*4]; lastVSconstants[const_number + i][2] = f[2 + 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() { for( int i=0;i<(C_FOGPARAMS+8)*4;i++)