2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:29:41 +00:00
|
|
|
// Refer to the license.txt file included.
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2017-02-01 04:29:29 +00:00
|
|
|
#include "VideoBackends/D3D/VertexManager.h"
|
|
|
|
|
|
|
|
#include <d3d11.h>
|
|
|
|
|
2016-01-02 20:01:12 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
|
2014-12-05 02:01:20 +00:00
|
|
|
#include "VideoBackends/D3D/BoundingBox.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
2014-10-29 00:19:09 +00:00
|
|
|
#include "VideoBackends/D3D/D3DState.h"
|
2014-12-03 21:17:56 +00:00
|
|
|
#include "VideoBackends/D3D/GeometryShaderCache.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/D3D/PixelShaderCache.h"
|
|
|
|
#include "VideoBackends/D3D/Render.h"
|
|
|
|
#include "VideoBackends/D3D/VertexShaderCache.h"
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2014-12-05 02:01:20 +00:00
|
|
|
#include "VideoCommon/BoundingBox.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/Debugger.h"
|
|
|
|
#include "VideoCommon/IndexGenerator.h"
|
2017-02-01 04:29:29 +00:00
|
|
|
#include "VideoCommon/NativeVertexFormat.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/RenderBase.h"
|
|
|
|
#include "VideoCommon/Statistics.h"
|
2014-07-25 23:10:44 +00:00
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2010-10-03 00:41:06 +00:00
|
|
|
namespace DX11
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2010-10-20 02:17:16 +00:00
|
|
|
// TODO: Find sensible values for these two
|
2014-10-25 17:04:33 +00:00
|
|
|
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;
|
2010-10-20 02:17:16 +00:00
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
void VertexManager::CreateDeviceObjects()
|
2010-10-03 00:41:06 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
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_vertexDrawOffset = 0;
|
|
|
|
m_indexDrawOffset = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_BUFFER_COUNT; i++)
|
|
|
|
{
|
|
|
|
m_buffers[i] = nullptr;
|
|
|
|
CHECK(SUCCEEDED(D3D::device->CreateBuffer(&bufdesc, nullptr, &m_buffers[i])),
|
|
|
|
"Failed to create buffer.");
|
2017-09-02 18:22:18 +00:00
|
|
|
D3D::SetDebugObjectName(m_buffers[i], "Buffer of VertexManager");
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_currentBuffer = 0;
|
|
|
|
m_bufferCursor = MAX_BUFFER_SIZE;
|
2011-06-11 19:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexManager::DestroyDeviceObjects()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
for (int i = 0; i < MAX_BUFFER_COUNT; i++)
|
|
|
|
{
|
|
|
|
SAFE_RELEASE(m_buffers[i]);
|
|
|
|
}
|
2011-06-11 19:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VertexManager::VertexManager()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
LocalVBuffer.resize(MAXVBUFFERSIZE);
|
2014-10-25 17:04:33 +00:00
|
|
|
|
2016-08-22 03:46:52 +00:00
|
|
|
m_cur_buffer_pointer = m_base_buffer_pointer = &LocalVBuffer[0];
|
|
|
|
m_end_buffer_pointer = m_base_buffer_pointer + LocalVBuffer.size();
|
2014-01-23 14:27:18 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
LocalIBuffer.resize(MAXIBUFFERSIZE);
|
2014-01-23 14:27:18 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
CreateDeviceObjects();
|
2011-06-11 19:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VertexManager::~VertexManager()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
DestroyDeviceObjects();
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2014-10-25 17:04:33 +00:00
|
|
|
void VertexManager::PrepareDrawBuffers(u32 stride)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
|
|
|
|
2016-08-22 03:46:52 +00:00
|
|
|
u32 vertexBufferSize = u32(m_cur_buffer_pointer - m_base_buffer_pointer);
|
2016-06-24 08:43:46 +00:00
|
|
|
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 (cursor + totalBufferSize >= MAX_BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
// Wrap around
|
|
|
|
m_currentBuffer = (m_currentBuffer + 1) % MAX_BUFFER_COUNT;
|
|
|
|
cursor = 0;
|
|
|
|
MapType = D3D11_MAP_WRITE_DISCARD;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_vertexDrawOffset = cursor;
|
|
|
|
m_indexDrawOffset = cursor + vertexBufferSize;
|
|
|
|
|
|
|
|
D3D::context->Map(m_buffers[m_currentBuffer], 0, MapType, 0, &map);
|
|
|
|
u8* mappedData = reinterpret_cast<u8*>(map.pData);
|
2016-08-22 03:46:52 +00:00
|
|
|
memcpy(mappedData + m_vertexDrawOffset, m_base_buffer_pointer, vertexBufferSize);
|
2016-06-24 08:43:46 +00:00
|
|
|
memcpy(mappedData + m_indexDrawOffset, GetIndexBuffer(), indexBufferSize);
|
|
|
|
D3D::context->Unmap(m_buffers[m_currentBuffer], 0);
|
|
|
|
|
|
|
|
m_bufferCursor = cursor + totalBufferSize;
|
|
|
|
|
|
|
|
ADDSTAT(stats.thisFrame.bytesVertexStreamed, vertexBufferSize);
|
|
|
|
ADDSTAT(stats.thisFrame.bytesIndexStreamed, indexBufferSize);
|
2010-10-20 02:17:16 +00:00
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2014-10-25 17:04:33 +00:00
|
|
|
void VertexManager::Draw(u32 stride)
|
2010-10-20 02:17:16 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
u32 indices = IndexGenerator::GetIndexLen();
|
|
|
|
|
|
|
|
D3D::stateman->SetVertexBuffer(m_buffers[m_currentBuffer], stride, 0);
|
|
|
|
D3D::stateman->SetIndexBuffer(m_buffers[m_currentBuffer]);
|
|
|
|
|
|
|
|
u32 baseVertex = m_vertexDrawOffset / stride;
|
|
|
|
u32 startIndex = m_indexDrawOffset / sizeof(u16);
|
|
|
|
|
|
|
|
D3D::stateman->Apply();
|
|
|
|
D3D::context->DrawIndexed(indices, startIndex, baseVertex);
|
|
|
|
|
|
|
|
INCSTAT(stats.thisFrame.numDrawCalls);
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 00:37:41 +00:00
|
|
|
void VertexManager::vFlush()
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2016-12-28 00:37:41 +00:00
|
|
|
if (!PixelShaderCache::SetShader())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR, true, { printf("Fail to set pixel shader\n"); });
|
|
|
|
return;
|
|
|
|
}
|
2014-10-25 17:04:33 +00:00
|
|
|
|
2017-07-20 05:25:31 +00:00
|
|
|
D3DVertexFormat* vertex_format =
|
|
|
|
static_cast<D3DVertexFormat*>(VertexLoaderManager::GetCurrentVertexFormat());
|
|
|
|
if (!VertexShaderCache::SetShader(vertex_format))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR, true, { printf("Fail to set pixel shader\n"); });
|
|
|
|
return;
|
|
|
|
}
|
2014-11-02 22:40:52 +00:00
|
|
|
|
2016-08-22 03:46:52 +00:00
|
|
|
if (!GeometryShaderCache::SetShader(m_current_primitive_type))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR, true, { printf("Fail to set pixel shader\n"); });
|
|
|
|
return;
|
|
|
|
}
|
2014-11-02 22:40:52 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsBBox && BoundingBox::active)
|
|
|
|
{
|
|
|
|
D3D::context->OMSetRenderTargetsAndUnorderedAccessViews(
|
|
|
|
D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL, nullptr, nullptr, 2, 1, &BBox::GetUAV(),
|
|
|
|
nullptr);
|
|
|
|
}
|
2014-11-02 22:40:52 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
u32 stride = VertexLoaderManager::GetCurrentVertexFormat()->GetVertexStride();
|
2014-10-25 17:04:33 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
PrepareDrawBuffers(stride);
|
2014-10-25 17:04:33 +00:00
|
|
|
|
2016-12-28 00:37:41 +00:00
|
|
|
g_renderer->ApplyState();
|
2013-03-01 00:07:34 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Draw(stride);
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
g_renderer->RestoreState();
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
2010-10-03 00:41:06 +00:00
|
|
|
|
2014-01-23 14:27:18 +00:00
|
|
|
void VertexManager::ResetBuffer(u32 stride)
|
|
|
|
{
|
2016-08-22 03:46:52 +00:00
|
|
|
m_cur_buffer_pointer = m_base_buffer_pointer;
|
2016-06-24 08:43:46 +00:00
|
|
|
IndexGenerator::Start(GetIndexBuffer());
|
2014-01-23 14:27:18 +00:00
|
|
|
}
|
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
} // namespace
|