diff --git a/Source/Plugins/Plugin_VideoDX11/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoDX11/Src/VertexManager.cpp index e2a8f4a547..7026522cf6 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/VertexManager.cpp @@ -39,31 +39,24 @@ namespace DX11 { // TODO: Find sensible values for these two -const UINT IBUFFER_SIZE = VertexManager::MAXIBUFFERSIZE * 12; -const UINT VBUFFER_SIZE = VertexManager::MAXVBUFFERSIZE * 8; -const UINT MAXVBUFFER_COUNT = 4; +const UINT IBUFFER_SIZE = VertexManager::MAXIBUFFERSIZE * 16 * sizeof(u16); +const UINT VBUFFER_SIZE = VertexManager::MAXVBUFFERSIZE * 16; +const UINT MAXVBUFFER_COUNT = 2; void VertexManager::CreateDeviceObjects() { D3D11_BUFFER_DESC bufdesc = CD3D11_BUFFER_DESC(IBUFFER_SIZE, D3D11_BIND_INDEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); - m_indexBufferCursor = 0; - m_vertexBufferCursor = 0; m_vertexDrawOffset = 0; m_triangleDrawIndex = 0; m_lineDrawIndex = 0; m_pointDrawIndex = 0; m_indexBuffers = new PID3D11Buffer[MAXVBUFFER_COUNT]; - m_vertexBuffers = new PID3D11Buffer[MAXVBUFFER_COUNT]; - bool Fail = false; - for (m_activeVertexBuffer = 0; m_activeVertexBuffer < MAXVBUFFER_COUNT; m_activeVertexBuffer++) - { - m_indexBuffers[m_activeVertexBuffer] = NULL; - m_vertexBuffers[m_activeVertexBuffer] = NULL; - } + m_vertexBuffers = new PID3D11Buffer[MAXVBUFFER_COUNT]; for (m_activeIndexBuffer = 0; m_activeIndexBuffer < MAXVBUFFER_COUNT; m_activeIndexBuffer++) { + m_indexBuffers[m_activeIndexBuffer] = NULL; CHECK(SUCCEEDED(D3D::device->CreateBuffer(&bufdesc, NULL, &m_indexBuffers[m_activeIndexBuffer])), "Failed to create index buffer."); D3D::SetDebugObjectName((ID3D11DeviceChild*)m_indexBuffers[m_activeIndexBuffer], "index buffer of VertexManager"); @@ -72,15 +65,15 @@ void VertexManager::CreateDeviceObjects() bufdesc.ByteWidth = VBUFFER_SIZE; for (m_activeVertexBuffer = 0; m_activeVertexBuffer < MAXVBUFFER_COUNT; m_activeVertexBuffer++) { + m_vertexBuffers[m_activeVertexBuffer] = NULL; CHECK(SUCCEEDED(D3D::device->CreateBuffer(&bufdesc, NULL, &m_vertexBuffers[m_activeVertexBuffer])), "Failed to create vertex buffer."); D3D::SetDebugObjectName((ID3D11DeviceChild*)m_vertexBuffers[m_activeVertexBuffer], "Vertex buffer of VertexManager"); } m_activeVertexBuffer = 0; m_activeIndexBuffer = 0; - m_LastVertexBuffer = MAXVBUFFER_COUNT; - m_LastIndexBuffer = MAXVBUFFER_COUNT; - + m_indexBufferCursor = IBUFFER_SIZE; + m_vertexBufferCursor = VBUFFER_SIZE; m_lineShader.Init(); m_pointShader.Init(); } @@ -113,7 +106,7 @@ void VertexManager::LoadBuffers() UINT vSize = UINT(s_pCurBufferPointer - LocalVBuffer); D3D11_MAP MapType = D3D11_MAP_WRITE_NO_OVERWRITE; - if (m_vertexBufferCursor + vSize >= VBUFFER_SIZE || m_activeVertexBuffer != m_LastVertexBuffer) + if (m_vertexBufferCursor + vSize >= VBUFFER_SIZE) { // Wrap around m_activeVertexBuffer = (m_activeVertexBuffer + 1) % MAXVBUFFER_COUNT; @@ -131,7 +124,7 @@ void VertexManager::LoadBuffers() UINT iCount = IndexGenerator::GetTriangleindexLen() + IndexGenerator::GetLineindexLen() + IndexGenerator::GetPointindexLen(); MapType = D3D11_MAP_WRITE_NO_OVERWRITE; - if (m_indexBufferCursor + iCount >= IBUFFER_SIZE/2 || m_activeIndexBuffer != m_LastIndexBuffer) + if (m_indexBufferCursor + iCount >= (IBUFFER_SIZE / sizeof(u16))) { // Wrap around m_activeIndexBuffer = (m_activeIndexBuffer + 1) % MAXVBUFFER_COUNT; @@ -143,9 +136,9 @@ void VertexManager::LoadBuffers() m_triangleDrawIndex = m_indexBufferCursor; m_lineDrawIndex = m_triangleDrawIndex + IndexGenerator::GetTriangleindexLen(); m_pointDrawIndex = m_lineDrawIndex + IndexGenerator::GetLineindexLen(); - memcpy((u16*)map.pData + m_triangleDrawIndex, TIBuffer, 2*IndexGenerator::GetTriangleindexLen()); - memcpy((u16*)map.pData + m_lineDrawIndex, LIBuffer, 2*IndexGenerator::GetLineindexLen()); - memcpy((u16*)map.pData + m_pointDrawIndex, PIBuffer, 2*IndexGenerator::GetPointindexLen()); + memcpy((u16*)map.pData + m_triangleDrawIndex, TIBuffer, sizeof(u16) * IndexGenerator::GetTriangleindexLen()); + memcpy((u16*)map.pData + m_lineDrawIndex, LIBuffer, sizeof(u16) * IndexGenerator::GetLineindexLen()); + memcpy((u16*)map.pData + m_pointDrawIndex, PIBuffer, sizeof(u16) * IndexGenerator::GetPointindexLen()); D3D::context->Unmap(m_indexBuffers[m_activeIndexBuffer], 0); m_indexBufferCursor += iCount; } @@ -158,8 +151,6 @@ void VertexManager::Draw(UINT stride) { D3D::context->IASetVertexBuffers(0, 1, &m_vertexBuffers[m_activeVertexBuffer], &stride, &m_vertexDrawOffset); D3D::context->IASetIndexBuffer(m_indexBuffers[m_activeIndexBuffer], DXGI_FORMAT_R16_UINT, 0); - m_LastIndexBuffer = m_activeIndexBuffer; - m_LastVertexBuffer = m_activeVertexBuffer; if (IndexGenerator::GetNumTriangles() > 0) { diff --git a/Source/Plugins/Plugin_VideoDX11/Src/VertexManager.h b/Source/Plugins/Plugin_VideoDX11/Src/VertexManager.h index 2e8cfaa388..9e6b7f2dca 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/VertexManager.h +++ b/Source/Plugins/Plugin_VideoDX11/Src/VertexManager.h @@ -50,8 +50,6 @@ private: UINT m_pointDrawIndex; UINT m_activeVertexBuffer; UINT m_activeIndexBuffer; - UINT m_LastVertexBuffer; - UINT m_LastIndexBuffer; typedef ID3D11Buffer* PID3D11Buffer; PID3D11Buffer* m_indexBuffers; PID3D11Buffer* m_vertexBuffers; diff --git a/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp b/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp index 653e387192..a59adb4793 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp @@ -1202,7 +1202,9 @@ void Renderer::ApplyState(bool bUseDstAlpha) D3D::ChangeRenderState(D3DRS_ALPHABLENDENABLE, false); if(bpmem.zmode.testenable && bpmem.zmode.updateenable) { - D3D::ChangeRenderState(D3DRS_ZENABLE, TRUE); + //This is needed to draw to the correct pixels in multi-pass algorithms + //this avoid z-figthing and grants that you write to the same pixels + //affected by the last pass D3D::ChangeRenderState(D3DRS_ZWRITEENABLE, false); D3D::ChangeRenderState(D3DRS_ZFUNC, D3DCMP_EQUAL); } @@ -1215,7 +1217,6 @@ void Renderer::RestoreState() D3D::RefreshRenderState(D3DRS_ALPHABLENDENABLE); if(bpmem.zmode.testenable && bpmem.zmode.updateenable) { - D3D::RefreshRenderState(D3DRS_ZENABLE); D3D::RefreshRenderState(D3DRS_ZWRITEENABLE); D3D::RefreshRenderState(D3DRS_ZFUNC); } diff --git a/Source/Plugins/Plugin_VideoDX9/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoDX9/Src/VertexManager.cpp index e8e425cc96..17a7aa1498 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/VertexManager.cpp @@ -41,6 +41,10 @@ extern NativeVertexFormat *g_nativeVertexFmt; namespace DX9 { +//This are the initially requeted size for the buffers expresed in elements +const u32 IBUFFER_SIZE = VertexManager::MAXIBUFFERSIZE * 16; +const u32 VBUFFER_SIZE = VertexManager::MAXVBUFFERSIZE * 16; +const u32 MAXVBUFFER_COUNT = 2; inline void DumpBadShaders() { @@ -64,37 +68,26 @@ inline void DumpBadShaders() void VertexManager::CreateDeviceObjects() { NumVBuffers = 0; - CurrentIBufferIndex = 0; - CurrentVBufferIndex = 0; - CurrentVBufferSize = 8 * MAXVBUFFERSIZE; - CurrentIBufferSize = 12 * MAXIBUFFERSIZE; + VBuffers = NULL; + IBuffers = NULL; D3DCAPS9 DeviceCaps = D3D::GetCaps(); - int maxdevicevbuffersize = DeviceCaps.MaxPrimitiveCount * 3 * DeviceCaps.MaxStreamStride; - if (CurrentVBufferSize > maxdevicevbuffersize) - { - CurrentVBufferSize = maxdevicevbuffersize; - } - if (CurrentIBufferSize > DeviceCaps.MaxVertexIndex) - { - CurrentIBufferSize = DeviceCaps.MaxVertexIndex; - } - if (CurrentIBufferSize < MAXIBUFFERSIZE) - { - return; - } - if (CurrentVBufferSize < MAXVBUFFERSIZE) - { - return; - } - VBuffers = new LPDIRECT3DVERTEXBUFFER9[MAX_VBufferCount]; - IBuffers = new LPDIRECT3DINDEXBUFFER9[MAX_VBufferCount]; + u32 devicevMaxBufferSize = DeviceCaps.MaxPrimitiveCount * 3 * DeviceCaps.MaxStreamStride; + //Calculate Device Dependant size + CurrentVBufferSize = (VBUFFER_SIZE > devicevMaxBufferSize) ? devicevMaxBufferSize : VBUFFER_SIZE; + CurrentIBufferSize = (IBUFFER_SIZE > DeviceCaps.MaxVertexIndex) ? DeviceCaps.MaxVertexIndex : IBUFFER_SIZE; + //if device caps are not enough for Vbuffer fall back to vertex arrays + if (CurrentIBufferSize < MAXIBUFFERSIZE || CurrentVBufferSize < MAXVBUFFERSIZE) return; + + VBuffers = new LPDIRECT3DVERTEXBUFFER9[MAXVBUFFER_COUNT]; + IBuffers = new LPDIRECT3DINDEXBUFFER9[MAXVBUFFER_COUNT]; + bool Fail = false; - for (CurrentVBuffer = 0; CurrentVBuffer < MAX_VBufferCount; CurrentVBuffer++) + for (CurrentVBuffer = 0; CurrentVBuffer < MAXVBUFFER_COUNT; CurrentVBuffer++) { VBuffers[CurrentVBuffer] = NULL; IBuffers[CurrentVBuffer] = NULL; } - for (CurrentVBuffer = 0; CurrentVBuffer < MAX_VBufferCount; CurrentVBuffer++) + for (CurrentVBuffer = 0; CurrentVBuffer < MAXVBUFFER_COUNT; CurrentVBuffer++) { if(FAILED( D3D::dev->CreateVertexBuffer( CurrentVBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &VBuffers[CurrentVBuffer], NULL ) ) ) { @@ -110,24 +103,25 @@ void VertexManager::CreateDeviceObjects() NumVBuffers = CurrentVBuffer; CurrentVBuffer = 0; CurrentIBuffer = 0; - LastVBuffer = NumVBuffers; - LastIBuffer = NumVBuffers; + CurrentIBufferIndex = CurrentIBufferSize; + CurrentVBufferIndex = CurrentVBufferSize; + if (Fail) { NumVBuffers--; if (NumVBuffers < 2) { - NumVBuffers = MAX_VBufferCount; + //Error creating Vertex buffers. clean and fall to Vertex arrays + NumVBuffers = MAXVBUFFER_COUNT; DestroyDeviceObjects(); } - } - + } } void VertexManager::DestroyDeviceObjects() { D3D::dev->SetStreamSource( 0, NULL, 0, 0); D3D::dev->SetIndices(NULL); - for (int i = 0; i < MAX_VBufferCount; i++) + for (int i = 0; i < MAXVBUFFER_COUNT; i++) { if(VBuffers) { @@ -152,16 +146,6 @@ void VertexManager::DestroyDeviceObjects() IBuffers = NULL; } -VertexManager::VertexManager() -{ - //CreateDeviceObjects(); -} - -VertexManager::~VertexManager() -{ - //DestroyDeviceObjects(); -} - void VertexManager::PrepareVBuffers(int stride) { if (!NumVBuffers) @@ -177,7 +161,7 @@ void VertexManager::PrepareVBuffers(int stride) int IndexDataSize = TdataSize + LDataSize + PDataSize; DWORD LockMode = D3DLOCK_NOOVERWRITE; - if (CurrentVBufferIndex > CurrentVBufferSize - datasize || LastVBuffer != CurrentVBuffer) + if (CurrentVBufferIndex > CurrentVBufferSize - datasize) { LockMode = D3DLOCK_DISCARD; CurrentVBufferIndex = 0; @@ -194,7 +178,7 @@ void VertexManager::PrepareVBuffers(int stride) LockMode = D3DLOCK_NOOVERWRITE; - if (CurrentIBufferIndex > CurrentIBufferSize - IndexDataSize || LastIBuffer != CurrentIBuffer) + if (CurrentIBufferIndex > CurrentIBufferSize - IndexDataSize) { LockMode = D3DLOCK_DISCARD; CurrentIBufferIndex = 0; @@ -221,100 +205,104 @@ void VertexManager::PrepareVBuffers(int stride) memcpy(pIndices, PIBuffer, PDataSize * sizeof(u16)); } IBuffers[CurrentIBuffer]->Unlock(); + D3D::dev->SetStreamSource( 0, VBuffers[CurrentVBuffer], CurrentVBufferIndex, stride); + if(CurrentIBufferIndex == 0) + { + D3D::dev->SetIndices(IBuffers[CurrentIBuffer]); + } } -void VertexManager::Draw(int stride) +void VertexManager::DrawVB(int stride) { - if(NumVBuffers) + if (IndexGenerator::GetNumTriangles() > 0) { - if (IndexGenerator::GetNumTriangles() > 0) + if (FAILED(D3D::dev->DrawIndexedPrimitive( + D3DPT_TRIANGLELIST, + 0, + 0, + IndexGenerator::GetNumVerts(), + CurrentIBufferIndex, + IndexGenerator::GetNumTriangles()))) { - if (FAILED(D3D::dev->DrawIndexedPrimitive( - D3DPT_TRIANGLELIST, - 0, - 0, - IndexGenerator::GetNumVerts(), - CurrentIBufferIndex, - IndexGenerator::GetNumTriangles()))) - { - DumpBadShaders(); - } - INCSTAT(stats.thisFrame.numIndexedDrawCalls); - } - if (IndexGenerator::GetNumLines() > 0) - { - if (FAILED(D3D::dev->DrawIndexedPrimitive( - D3DPT_LINELIST, - 0, - 0, - IndexGenerator::GetNumVerts(), - CurrentIBufferIndex + IndexGenerator::GetTriangleindexLen(), - IndexGenerator::GetNumLines()))) - { - DumpBadShaders(); - } - INCSTAT(stats.thisFrame.numIndexedDrawCalls); - } - if (IndexGenerator::GetNumPoints() > 0) - { - if (FAILED(D3D::dev->DrawIndexedPrimitive( - D3DPT_POINTLIST, - 0, - 0, - IndexGenerator::GetNumVerts(), - CurrentIBufferIndex + IndexGenerator::GetTriangleindexLen() + IndexGenerator::GetLineindexLen(), - IndexGenerator::GetNumPoints()))) - { - DumpBadShaders(); - } - INCSTAT(stats.thisFrame.numIndexedDrawCalls); + DumpBadShaders(); } + INCSTAT(stats.thisFrame.numIndexedDrawCalls); } - else + if (IndexGenerator::GetNumLines() > 0) { - if (IndexGenerator::GetNumTriangles() > 0) + if (FAILED(D3D::dev->DrawIndexedPrimitive( + D3DPT_LINELIST, + 0, + 0, + IndexGenerator::GetNumVerts(), + CurrentIBufferIndex + IndexGenerator::GetTriangleindexLen(), + IndexGenerator::GetNumLines()))) { - if (FAILED(D3D::dev->DrawIndexedPrimitiveUP( - D3DPT_TRIANGLELIST, - 0, IndexGenerator::GetNumVerts(), IndexGenerator::GetNumTriangles(), - TIBuffer, - D3DFMT_INDEX16, - LocalVBuffer, - stride))) - { - DumpBadShaders(); - } - INCSTAT(stats.thisFrame.numIndexedDrawCalls); - } - if (IndexGenerator::GetNumLines() > 0) - { - if (FAILED(D3D::dev->DrawIndexedPrimitiveUP( - D3DPT_LINELIST, - 0, IndexGenerator::GetNumVerts(), IndexGenerator::GetNumLines(), - LIBuffer, - D3DFMT_INDEX16, - LocalVBuffer, - stride))) - { - DumpBadShaders(); - } - INCSTAT(stats.thisFrame.numIndexedDrawCalls); - } - if (IndexGenerator::GetNumPoints() > 0) - { - if (FAILED(D3D::dev->DrawIndexedPrimitiveUP( - D3DPT_POINTLIST, - 0, IndexGenerator::GetNumVerts(), IndexGenerator::GetNumPoints(), - PIBuffer, - D3DFMT_INDEX16, - LocalVBuffer, - stride))) - { - DumpBadShaders(); - } - INCSTAT(stats.thisFrame.numIndexedDrawCalls); + DumpBadShaders(); } + INCSTAT(stats.thisFrame.numIndexedDrawCalls); } + if (IndexGenerator::GetNumPoints() > 0) + { + if (FAILED(D3D::dev->DrawIndexedPrimitive( + D3DPT_POINTLIST, + 0, + 0, + IndexGenerator::GetNumVerts(), + CurrentIBufferIndex + IndexGenerator::GetTriangleindexLen() + IndexGenerator::GetLineindexLen(), + IndexGenerator::GetNumPoints()))) + { + DumpBadShaders(); + } + INCSTAT(stats.thisFrame.numIndexedDrawCalls); + } + +} + +void VertexManager::DrawVA(int stride) +{ + if (IndexGenerator::GetNumTriangles() > 0) + { + if (FAILED(D3D::dev->DrawIndexedPrimitiveUP( + D3DPT_TRIANGLELIST, + 0, IndexGenerator::GetNumVerts(), IndexGenerator::GetNumTriangles(), + TIBuffer, + D3DFMT_INDEX16, + LocalVBuffer, + stride))) + { + DumpBadShaders(); + } + INCSTAT(stats.thisFrame.numIndexedDrawCalls); + } + if (IndexGenerator::GetNumLines() > 0) + { + if (FAILED(D3D::dev->DrawIndexedPrimitiveUP( + D3DPT_LINELIST, + 0, IndexGenerator::GetNumVerts(), IndexGenerator::GetNumLines(), + LIBuffer, + D3DFMT_INDEX16, + LocalVBuffer, + stride))) + { + DumpBadShaders(); + } + INCSTAT(stats.thisFrame.numIndexedDrawCalls); + } + if (IndexGenerator::GetNumPoints() > 0) + { + if (FAILED(D3D::dev->DrawIndexedPrimitiveUP( + D3DPT_POINTLIST, + 0, IndexGenerator::GetNumVerts(), IndexGenerator::GetNumPoints(), + PIBuffer, + D3DFMT_INDEX16, + LocalVBuffer, + stride))) + { + DumpBadShaders(); + } + INCSTAT(stats.thisFrame.numIndexedDrawCalls); + } } void VertexManager::vFlush() @@ -375,24 +363,13 @@ void VertexManager::vFlush() } PrepareVBuffers(stride); - if (NumVBuffers) - { - D3D::dev->SetStreamSource( 0, VBuffers[CurrentVBuffer], CurrentVBufferIndex, stride); - if(LastIBuffer != CurrentIBuffer) - { - LastIBuffer = CurrentIBuffer; - D3D::dev->SetIndices(IBuffers[CurrentIBuffer]); - } - LastVBuffer = CurrentVBuffer; - } g_nativeVertexFmt->SetupVertexPointers(); - Draw(stride); + if(NumVBuffers){ DrawVB(stride);} else { DrawVA(stride);} bool useDstAlpha = !g_ActiveConfig.bDstAlphaPass && bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate && bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24; if (useDstAlpha) { - DWORD write = 0; if (!PixelShaderCache::SetShader(DSTALPHA_ALPHA_PASS, g_nativeVertexFmt->m_components)) { GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set pixel shader\n");}); @@ -400,7 +377,7 @@ void VertexManager::vFlush() } // update alpha only g_renderer->ApplyState(true); - Draw(stride); + if(NumVBuffers){ DrawVB(stride);} else { DrawVA(stride);} g_renderer->RestoreState(); } GFX_DEBUGGER_PAUSE_AT(NEXT_FLUSH, true); diff --git a/Source/Plugins/Plugin_VideoDX9/Src/VertexManager.h b/Source/Plugins/Plugin_VideoDX9/Src/VertexManager.h index a80f12276d..aa4e9ed57b 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/VertexManager.h +++ b/Source/Plugins/Plugin_VideoDX9/Src/VertexManager.h @@ -23,7 +23,6 @@ #include "VertexManagerBase.h" -#define MAX_VBufferCount 4 namespace DX9 { @@ -34,23 +33,19 @@ public: void GetElements(NativeVertexFormat* format, D3DVERTEXELEMENT9** elems, int* num); void CreateDeviceObjects(); void DestroyDeviceObjects(); - VertexManager(); - ~VertexManager(); - private: u32 CurrentVBufferIndex; u32 CurrentVBufferSize; u32 CurrentIBufferIndex; - u32 CurrentIBufferSize; + u32 CurrentIBufferSize; u32 NumVBuffers; u32 CurrentVBuffer; u32 CurrentIBuffer; - u32 LastVBuffer; - u32 LastIBuffer; LPDIRECT3DVERTEXBUFFER9 *VBuffers; LPDIRECT3DINDEXBUFFER9 *IBuffers; void PrepareVBuffers(int stride); - void Draw(int stride); + void DrawVB(int stride); + void DrawVA(int stride); // temp void vFlush(); };