Merge pull request #1414 from kayru/d3d_optimization

D3D: Couple of small optimizations
This commit is contained in:
Ryan Houdek 2014-10-29 13:26:50 -06:00
commit 52e6a940cf
3 changed files with 72 additions and 78 deletions

View File

@ -1027,9 +1027,6 @@ void Renderer::ApplyState(bool bUseDstAlpha)
void Renderer::RestoreState() void Renderer::RestoreState()
{ {
ID3D11ShaderResourceView* shader_resources[8] = { nullptr };
D3D::context->PSSetShaderResources(0, 8, shader_resources);
D3D::stateman->PopBlendState(); D3D::stateman->PopBlendState();
D3D::stateman->PopDepthState(); D3D::stateman->PopDepthState();
D3D::stateman->PopRasterizerState(); D3D::stateman->PopRasterizerState();

View File

@ -24,39 +24,28 @@ namespace DX11
{ {
// TODO: Find sensible values for these two // TODO: Find sensible values for these two
const UINT IBUFFER_SIZE = VertexManager::MAXIBUFFERSIZE * sizeof(u16) * 8; const u32 MAX_IBUFFER_SIZE = VertexManager::MAXIBUFFERSIZE * sizeof(u16) * 8;
const UINT VBUFFER_SIZE = VertexManager::MAXVBUFFERSIZE; const u32 MAX_VBUFFER_SIZE = VertexManager::MAXVBUFFERSIZE;
const UINT MAX_VBUFFER_COUNT = 2; const u32 MAX_BUFFER_SIZE = MAX_IBUFFER_SIZE + MAX_VBUFFER_SIZE;
void VertexManager::CreateDeviceObjects() void VertexManager::CreateDeviceObjects()
{ {
D3D11_BUFFER_DESC bufdesc = CD3D11_BUFFER_DESC(IBUFFER_SIZE, D3D11_BUFFER_DESC bufdesc = CD3D11_BUFFER_DESC(MAX_BUFFER_SIZE,
D3D11_BIND_INDEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); D3D11_BIND_INDEX_BUFFER | D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
m_vertex_draw_offset = 0; m_vertexDrawOffset = 0;
m_index_draw_offset = 0; m_indexDrawOffset = 0;
m_index_buffers = new PID3D11Buffer[MAX_VBUFFER_COUNT];
m_vertex_buffers = new PID3D11Buffer[MAX_VBUFFER_COUNT]; for (int i = 0; i < MAX_BUFFER_COUNT; i++)
for (m_current_index_buffer = 0; m_current_index_buffer < MAX_VBUFFER_COUNT; m_current_index_buffer++)
{ {
m_index_buffers[m_current_index_buffer] = nullptr; m_buffers[i] = nullptr;
CHECK(SUCCEEDED(D3D::device->CreateBuffer(&bufdesc, nullptr, &m_index_buffers[m_current_index_buffer])), CHECK(SUCCEEDED(D3D::device->CreateBuffer(&bufdesc, nullptr, &m_buffers[i])), "Failed to create buffer.");
"Failed to create index buffer."); D3D::SetDebugObjectName((ID3D11DeviceChild*)m_buffers[i], "Buffer of VertexManager");
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_index_buffers[m_current_index_buffer], "index buffer of VertexManager");
} }
bufdesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufdesc.ByteWidth = VBUFFER_SIZE; m_currentBuffer = 0;
for (m_current_vertex_buffer = 0; m_current_vertex_buffer < MAX_VBUFFER_COUNT; m_current_vertex_buffer++) m_bufferCursor = MAX_BUFFER_SIZE;
{
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_lineShader.Init(); m_lineShader.Init();
m_pointShader.Init(); m_pointShader.Init();
} }
@ -65,10 +54,10 @@ void VertexManager::DestroyDeviceObjects()
{ {
m_pointShader.Shutdown(); m_pointShader.Shutdown();
m_lineShader.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_buffers[i]);
SAFE_RELEASE(m_index_buffers[m_current_vertex_buffer]);
} }
} }
@ -76,6 +65,7 @@ void VertexManager::DestroyDeviceObjects()
VertexManager::VertexManager() VertexManager::VertexManager()
{ {
LocalVBuffer.resize(MAXVBUFFERSIZE); LocalVBuffer.resize(MAXVBUFFERSIZE);
s_pCurBufferPointer = s_pBaseBufferPointer = &LocalVBuffer[0]; s_pCurBufferPointer = s_pBaseBufferPointer = &LocalVBuffer[0];
s_pEndBufferPointer = s_pBaseBufferPointer + LocalVBuffer.size(); s_pEndBufferPointer = s_pBaseBufferPointer + LocalVBuffer.size();
@ -89,61 +79,65 @@ VertexManager::~VertexManager()
DestroyDeviceObjects(); DestroyDeviceObjects();
} }
void VertexManager::PrepareDrawBuffers() void VertexManager::PrepareDrawBuffers(u32 stride)
{ {
D3D11_MAPPED_SUBRESOURCE map; 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; D3D11_MAP MapType = D3D11_MAP_WRITE_NO_OVERWRITE;
if (m_vertex_buffer_cursor + vSize >= VBUFFER_SIZE) if (cursor + totalBufferSize >= MAX_BUFFER_SIZE)
{ {
// Wrap around // Wrap around
m_current_vertex_buffer = (m_current_vertex_buffer + 1) % MAX_VBUFFER_COUNT; m_currentBuffer = (m_currentBuffer + 1) % MAX_BUFFER_COUNT;
m_vertex_buffer_cursor = 0; cursor = 0;
MapType = D3D11_MAP_WRITE_DISCARD; 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->Map(m_buffers[m_currentBuffer], 0, MapType, 0, &map);
D3D::context->Unmap(m_vertex_buffers[m_current_vertex_buffer], 0); u8* mappedData = reinterpret_cast<u8*>(map.pData);
m_vertex_draw_offset = m_vertex_buffer_cursor; memcpy(mappedData + m_vertexDrawOffset, s_pBaseBufferPointer, vertexBufferSize);
m_vertex_buffer_cursor += vSize; memcpy(mappedData + m_indexDrawOffset, GetIndexBuffer(), indexBufferSize);
D3D::context->Unmap(m_buffers[m_currentBuffer], 0);
UINT iCount = IndexGenerator::GetIndexLen(); m_bufferCursor = cursor + totalBufferSize;
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);
memcpy((u16*)map.pData + m_index_buffer_cursor, GetIndexBuffer(), sizeof(u16) * IndexGenerator::GetIndexLen()); ADDSTAT(stats.thisFrame.bytesVertexStreamed, vertexBufferSize);
D3D::context->Unmap(m_index_buffers[m_current_index_buffer], 0); ADDSTAT(stats.thisFrame.bytesIndexStreamed, indexBufferSize);
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));
} }
static const float LINE_PT_TEX_OFFSETS[8] = { static const float LINE_PT_TEX_OFFSETS[8] = {
0.f, 0.0625f, 0.125f, 0.25f, 0.5f, 1.f, 1.f, 1.f 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; u32 components = VertexLoaderManager::GetCurrentVertexFormat()->m_components;
D3D::context->IASetVertexBuffers(0, 1, &m_vertex_buffers[m_current_vertex_buffer], &stride, &m_vertex_draw_offset); u32 indices = IndexGenerator::GetIndexLen();
D3D::context->IASetIndexBuffer(m_index_buffers[m_current_index_buffer], DXGI_FORMAT_R16_UINT, 0);
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) if (current_primitive_type == PRIMITIVE_TRIANGLES)
{ {
D3D::context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); 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); INCSTAT(stats.thisFrame.numDrawCalls);
} }
else if (current_primitive_type == PRIMITIVE_LINES) 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 ((DX11::Renderer*)g_renderer)->ApplyCullDisable(); // Disable culling for lines and points
D3D::context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST); 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); INCSTAT(stats.thisFrame.numDrawCalls);
D3D::context->GSSetShader(nullptr, nullptr, 0); 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 ((DX11::Renderer*)g_renderer)->ApplyCullDisable(); // Disable culling for lines and points
D3D::context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST); 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); INCSTAT(stats.thisFrame.numDrawCalls);
D3D::context->GSSetShader(nullptr, nullptr, 0); D3D::context->GSSetShader(nullptr, nullptr, 0);
@ -199,19 +193,24 @@ void VertexManager::Draw(UINT stride)
void VertexManager::vFlush(bool useDstAlpha) void VertexManager::vFlush(bool useDstAlpha)
{ {
u32 components = VertexLoaderManager::GetCurrentVertexFormat()->m_components; u32 components = VertexLoaderManager::GetCurrentVertexFormat()->m_components;
if (!PixelShaderCache::SetShader( if (!PixelShaderCache::SetShader(
useDstAlpha ? DSTALPHA_DUAL_SOURCE_BLEND : DSTALPHA_NONE, components)) useDstAlpha ? DSTALPHA_DUAL_SOURCE_BLEND : DSTALPHA_NONE, components))
{ {
GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set pixel shader\n");}); GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set pixel shader\n");});
return; return;
} }
if (!VertexShaderCache::SetShader(components)) if (!VertexShaderCache::SetShader(components))
{ {
GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set pixel shader\n");}); GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set pixel shader\n");});
return; return;
} }
PrepareDrawBuffers();
unsigned int stride = VertexLoaderManager::GetCurrentVertexFormat()->GetVertexStride(); u32 stride = VertexLoaderManager::GetCurrentVertexFormat()->GetVertexStride();
PrepareDrawBuffers(stride);
VertexLoaderManager::GetCurrentVertexFormat()->SetupVertexPointers(); VertexLoaderManager::GetCurrentVertexFormat()->SetupVertexPointers();
g_renderer->ApplyState(useDstAlpha); g_renderer->ApplyState(useDstAlpha);

View File

@ -27,20 +27,18 @@ protected:
private: private:
void PrepareDrawBuffers(); void PrepareDrawBuffers(u32 stride);
void Draw(u32 stride); void Draw(u32 stride);
// temp // temp
void vFlush(bool useDstAlpha) override; void vFlush(bool useDstAlpha) override;
u32 m_vertex_buffer_cursor; u32 m_vertexDrawOffset;
u32 m_vertex_draw_offset; u32 m_indexDrawOffset;
u32 m_index_buffer_cursor; u32 m_currentBuffer;
u32 m_index_draw_offset; u32 m_bufferCursor;
u32 m_current_vertex_buffer;
u32 m_current_index_buffer; enum { MAX_BUFFER_COUNT = 2 };
typedef ID3D11Buffer* PID3D11Buffer; ID3D11Buffer* m_buffers[MAX_BUFFER_COUNT];
PID3D11Buffer* m_index_buffers;
PID3D11Buffer* m_vertex_buffers;
LineGeometryShader m_lineShader; LineGeometryShader m_lineShader;
PointGeometryShader m_pointShader; PointGeometryShader m_pointShader;