diff --git a/Source/Core/VideoBackends/D3D/Render.cpp b/Source/Core/VideoBackends/D3D/Render.cpp index 8803cbb4f2..0441e63d7b 100644 --- a/Source/Core/VideoBackends/D3D/Render.cpp +++ b/Source/Core/VideoBackends/D3D/Render.cpp @@ -1027,9 +1027,6 @@ void Renderer::ApplyState(bool bUseDstAlpha) void Renderer::RestoreState() { - ID3D11ShaderResourceView* shader_resources[8] = { nullptr }; - D3D::context->PSSetShaderResources(0, 8, shader_resources); - D3D::stateman->PopBlendState(); D3D::stateman->PopDepthState(); D3D::stateman->PopRasterizerState(); diff --git a/Source/Core/VideoBackends/D3D/VertexManager.cpp b/Source/Core/VideoBackends/D3D/VertexManager.cpp index f6d7685a52..a59ea63dea 100644 --- a/Source/Core/VideoBackends/D3D/VertexManager.cpp +++ b/Source/Core/VideoBackends/D3D/VertexManager.cpp @@ -24,39 +24,28 @@ namespace DX11 { // TODO: Find sensible values for these two -const UINT IBUFFER_SIZE = VertexManager::MAXIBUFFERSIZE * sizeof(u16) * 8; -const UINT VBUFFER_SIZE = VertexManager::MAXVBUFFERSIZE; -const UINT MAX_VBUFFER_COUNT = 2; +const u32 MAX_IBUFFER_SIZE = VertexManager::MAXIBUFFERSIZE * sizeof(u16) * 8; +const u32 MAX_VBUFFER_SIZE = VertexManager::MAXVBUFFERSIZE; +const u32 MAX_BUFFER_SIZE = MAX_IBUFFER_SIZE + MAX_VBUFFER_SIZE; void VertexManager::CreateDeviceObjects() { - D3D11_BUFFER_DESC bufdesc = CD3D11_BUFFER_DESC(IBUFFER_SIZE, - D3D11_BIND_INDEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); + D3D11_BUFFER_DESC bufdesc = CD3D11_BUFFER_DESC(MAX_BUFFER_SIZE, + D3D11_BIND_INDEX_BUFFER | D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); - m_vertex_draw_offset = 0; - m_index_draw_offset = 0; - m_index_buffers = new PID3D11Buffer[MAX_VBUFFER_COUNT]; - m_vertex_buffers = new PID3D11Buffer[MAX_VBUFFER_COUNT]; - for (m_current_index_buffer = 0; m_current_index_buffer < MAX_VBUFFER_COUNT; m_current_index_buffer++) + m_vertexDrawOffset = 0; + m_indexDrawOffset = 0; + + for (int i = 0; i < MAX_BUFFER_COUNT; i++) { - m_index_buffers[m_current_index_buffer] = nullptr; - CHECK(SUCCEEDED(D3D::device->CreateBuffer(&bufdesc, nullptr, &m_index_buffers[m_current_index_buffer])), - "Failed to create index buffer."); - D3D::SetDebugObjectName((ID3D11DeviceChild*)m_index_buffers[m_current_index_buffer], "index buffer of VertexManager"); + m_buffers[i] = nullptr; + CHECK(SUCCEEDED(D3D::device->CreateBuffer(&bufdesc, nullptr, &m_buffers[i])), "Failed to create buffer."); + D3D::SetDebugObjectName((ID3D11DeviceChild*)m_buffers[i], "Buffer of VertexManager"); } - bufdesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; - bufdesc.ByteWidth = VBUFFER_SIZE; - for (m_current_vertex_buffer = 0; m_current_vertex_buffer < MAX_VBUFFER_COUNT; m_current_vertex_buffer++) - { - m_vertex_buffers[m_current_vertex_buffer] = nullptr; - CHECK(SUCCEEDED(D3D::device->CreateBuffer(&bufdesc, nullptr, &m_vertex_buffers[m_current_vertex_buffer])), - "Failed to create vertex buffer."); - D3D::SetDebugObjectName((ID3D11DeviceChild*)m_vertex_buffers[m_current_vertex_buffer], "Vertex buffer of VertexManager"); - } - m_current_vertex_buffer = 0; - m_current_index_buffer = 0; - m_index_buffer_cursor = IBUFFER_SIZE; - m_vertex_buffer_cursor = VBUFFER_SIZE; + + m_currentBuffer = 0; + m_bufferCursor = MAX_BUFFER_SIZE; + m_lineShader.Init(); m_pointShader.Init(); } @@ -65,10 +54,10 @@ void VertexManager::DestroyDeviceObjects() { m_pointShader.Shutdown(); m_lineShader.Shutdown(); - for (m_current_vertex_buffer = 0; m_current_vertex_buffer < MAX_VBUFFER_COUNT; m_current_vertex_buffer++) + + for (int i = 0; i < MAX_BUFFER_COUNT; i++) { - SAFE_RELEASE(m_vertex_buffers[m_current_vertex_buffer]); - SAFE_RELEASE(m_index_buffers[m_current_vertex_buffer]); + SAFE_RELEASE(m_buffers[i]); } } @@ -76,6 +65,7 @@ void VertexManager::DestroyDeviceObjects() VertexManager::VertexManager() { LocalVBuffer.resize(MAXVBUFFERSIZE); + s_pCurBufferPointer = s_pBaseBufferPointer = &LocalVBuffer[0]; s_pEndBufferPointer = s_pBaseBufferPointer + LocalVBuffer.size(); @@ -89,61 +79,65 @@ VertexManager::~VertexManager() DestroyDeviceObjects(); } -void VertexManager::PrepareDrawBuffers() +void VertexManager::PrepareDrawBuffers(u32 stride) { D3D11_MAPPED_SUBRESOURCE map; - UINT vSize = UINT(s_pCurBufferPointer - s_pBaseBufferPointer); + u32 vertexBufferSize = u32(s_pCurBufferPointer - s_pBaseBufferPointer); + u32 indexBufferSize = IndexGenerator::GetIndexLen() * sizeof(u16); + u32 totalBufferSize = vertexBufferSize + indexBufferSize; + + u32 cursor = m_bufferCursor; + u32 padding = m_bufferCursor % stride; + if (padding) + { + cursor += stride - padding; + } + D3D11_MAP MapType = D3D11_MAP_WRITE_NO_OVERWRITE; - if (m_vertex_buffer_cursor + vSize >= VBUFFER_SIZE) + if (cursor + totalBufferSize >= MAX_BUFFER_SIZE) { // Wrap around - m_current_vertex_buffer = (m_current_vertex_buffer + 1) % MAX_VBUFFER_COUNT; - m_vertex_buffer_cursor = 0; + m_currentBuffer = (m_currentBuffer + 1) % MAX_BUFFER_COUNT; + cursor = 0; MapType = D3D11_MAP_WRITE_DISCARD; } - D3D::context->Map(m_vertex_buffers[m_current_vertex_buffer], 0, MapType, 0, &map); + m_vertexDrawOffset = cursor; + m_indexDrawOffset = cursor + vertexBufferSize; - memcpy((u8*)map.pData + m_vertex_buffer_cursor, s_pBaseBufferPointer, vSize); - D3D::context->Unmap(m_vertex_buffers[m_current_vertex_buffer], 0); - m_vertex_draw_offset = m_vertex_buffer_cursor; - m_vertex_buffer_cursor += vSize; + D3D::context->Map(m_buffers[m_currentBuffer], 0, MapType, 0, &map); + u8* mappedData = reinterpret_cast(map.pData); + memcpy(mappedData + m_vertexDrawOffset, s_pBaseBufferPointer, vertexBufferSize); + memcpy(mappedData + m_indexDrawOffset, GetIndexBuffer(), indexBufferSize); + D3D::context->Unmap(m_buffers[m_currentBuffer], 0); - UINT iCount = IndexGenerator::GetIndexLen(); - MapType = D3D11_MAP_WRITE_NO_OVERWRITE; - if (m_index_buffer_cursor + iCount >= (IBUFFER_SIZE / sizeof(u16))) - { - // Wrap around - m_current_index_buffer = (m_current_index_buffer + 1) % MAX_VBUFFER_COUNT; - m_index_buffer_cursor = 0; - MapType = D3D11_MAP_WRITE_DISCARD; - } - D3D::context->Map(m_index_buffers[m_current_index_buffer], 0, MapType, 0, &map); + m_bufferCursor = cursor + totalBufferSize; - memcpy((u16*)map.pData + m_index_buffer_cursor, GetIndexBuffer(), sizeof(u16) * IndexGenerator::GetIndexLen()); - D3D::context->Unmap(m_index_buffers[m_current_index_buffer], 0); - m_index_draw_offset = m_index_buffer_cursor; - m_index_buffer_cursor += iCount; - - ADDSTAT(stats.thisFrame.bytesVertexStreamed, vSize); - ADDSTAT(stats.thisFrame.bytesIndexStreamed, iCount*sizeof(u16)); + ADDSTAT(stats.thisFrame.bytesVertexStreamed, vertexBufferSize); + ADDSTAT(stats.thisFrame.bytesIndexStreamed, indexBufferSize); } static const float LINE_PT_TEX_OFFSETS[8] = { 0.f, 0.0625f, 0.125f, 0.25f, 0.5f, 1.f, 1.f, 1.f }; -void VertexManager::Draw(UINT stride) +void VertexManager::Draw(u32 stride) { u32 components = VertexLoaderManager::GetCurrentVertexFormat()->m_components; - D3D::context->IASetVertexBuffers(0, 1, &m_vertex_buffers[m_current_vertex_buffer], &stride, &m_vertex_draw_offset); - D3D::context->IASetIndexBuffer(m_index_buffers[m_current_index_buffer], DXGI_FORMAT_R16_UINT, 0); + u32 indices = IndexGenerator::GetIndexLen(); + + u32 zero = 0; + D3D::context->IASetVertexBuffers(0, 1, &m_buffers[m_currentBuffer], &stride, &zero); + D3D::context->IASetIndexBuffer(m_buffers[m_currentBuffer], DXGI_FORMAT_R16_UINT, 0); + + u32 baseVertex = m_vertexDrawOffset / stride; + u32 startIndex = m_indexDrawOffset / sizeof(u16); if (current_primitive_type == PRIMITIVE_TRIANGLES) { D3D::context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); - D3D::context->DrawIndexed(IndexGenerator::GetIndexLen(), m_index_draw_offset, 0); + D3D::context->DrawIndexed(indices, startIndex, baseVertex); INCSTAT(stats.thisFrame.numDrawCalls); } else if (current_primitive_type == PRIMITIVE_LINES) @@ -163,7 +157,7 @@ void VertexManager::Draw(UINT stride) { ((DX11::Renderer*)g_renderer)->ApplyCullDisable(); // Disable culling for lines and points D3D::context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST); - D3D::context->DrawIndexed(IndexGenerator::GetIndexLen(), m_index_draw_offset, 0); + D3D::context->DrawIndexed(indices, startIndex, baseVertex); INCSTAT(stats.thisFrame.numDrawCalls); D3D::context->GSSetShader(nullptr, nullptr, 0); @@ -187,7 +181,7 @@ void VertexManager::Draw(UINT stride) { ((DX11::Renderer*)g_renderer)->ApplyCullDisable(); // Disable culling for lines and points D3D::context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST); - D3D::context->DrawIndexed(IndexGenerator::GetIndexLen(), m_index_draw_offset, 0); + D3D::context->DrawIndexed(indices, startIndex, baseVertex); INCSTAT(stats.thisFrame.numDrawCalls); D3D::context->GSSetShader(nullptr, nullptr, 0); @@ -199,19 +193,24 @@ void VertexManager::Draw(UINT stride) void VertexManager::vFlush(bool useDstAlpha) { u32 components = VertexLoaderManager::GetCurrentVertexFormat()->m_components; + if (!PixelShaderCache::SetShader( useDstAlpha ? DSTALPHA_DUAL_SOURCE_BLEND : DSTALPHA_NONE, components)) { GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set pixel shader\n");}); return; } + if (!VertexShaderCache::SetShader(components)) { GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set pixel shader\n");}); return; } - PrepareDrawBuffers(); - unsigned int stride = VertexLoaderManager::GetCurrentVertexFormat()->GetVertexStride(); + + u32 stride = VertexLoaderManager::GetCurrentVertexFormat()->GetVertexStride(); + + PrepareDrawBuffers(stride); + VertexLoaderManager::GetCurrentVertexFormat()->SetupVertexPointers(); g_renderer->ApplyState(useDstAlpha); diff --git a/Source/Core/VideoBackends/D3D/VertexManager.h b/Source/Core/VideoBackends/D3D/VertexManager.h index 183f65575f..bd84128ff1 100644 --- a/Source/Core/VideoBackends/D3D/VertexManager.h +++ b/Source/Core/VideoBackends/D3D/VertexManager.h @@ -27,20 +27,18 @@ protected: private: - void PrepareDrawBuffers(); + void PrepareDrawBuffers(u32 stride); void Draw(u32 stride); // temp void vFlush(bool useDstAlpha) override; - u32 m_vertex_buffer_cursor; - u32 m_vertex_draw_offset; - u32 m_index_buffer_cursor; - u32 m_index_draw_offset; - u32 m_current_vertex_buffer; - u32 m_current_index_buffer; - typedef ID3D11Buffer* PID3D11Buffer; - PID3D11Buffer* m_index_buffers; - PID3D11Buffer* m_vertex_buffers; + u32 m_vertexDrawOffset; + u32 m_indexDrawOffset; + u32 m_currentBuffer; + u32 m_bufferCursor; + + enum { MAX_BUFFER_COUNT = 2 }; + ID3D11Buffer* m_buffers[MAX_BUFFER_COUNT]; LineGeometryShader m_lineShader; PointGeometryShader m_pointShader;