2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:29:41 -06:00
|
|
|
// Refer to the license.txt file included.
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2017-01-31 21:29:29 -07:00
|
|
|
#include "VideoBackends/D3D/VertexManager.h"
|
|
|
|
|
|
|
|
#include <d3d11.h>
|
|
|
|
|
2016-01-02 13:01:12 -07:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
|
2014-12-04 19:01:20 -07:00
|
|
|
#include "VideoBackends/D3D/BoundingBox.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
2014-10-28 18:19:09 -06:00
|
|
|
#include "VideoBackends/D3D/D3DState.h"
|
2014-12-03 14:17:56 -07:00
|
|
|
#include "VideoBackends/D3D/GeometryShaderCache.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoBackends/D3D/PixelShaderCache.h"
|
|
|
|
#include "VideoBackends/D3D/Render.h"
|
|
|
|
#include "VideoBackends/D3D/VertexShaderCache.h"
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2014-12-04 19:01:20 -07:00
|
|
|
#include "VideoCommon/BoundingBox.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoCommon/Debugger.h"
|
|
|
|
#include "VideoCommon/IndexGenerator.h"
|
2017-01-31 21:29:29 -07:00
|
|
|
#include "VideoCommon/NativeVertexFormat.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoCommon/RenderBase.h"
|
|
|
|
#include "VideoCommon/Statistics.h"
|
2014-07-25 17:10:44 -06:00
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2010-10-02 18:41:06 -06:00
|
|
|
namespace DX11
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2010-10-19 20:17:16 -06:00
|
|
|
// TODO: Find sensible values for these two
|
2014-10-25 11:04:33 -06: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-19 20:17:16 -06:00
|
|
|
|
2011-06-11 13:37:21 -06:00
|
|
|
void VertexManager::CreateDeviceObjects()
|
2010-10-02 18:41:06 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06: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 12:22:18 -06:00
|
|
|
D3D::SetDebugObjectName(m_buffers[i], "Buffer of VertexManager");
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
m_currentBuffer = 0;
|
|
|
|
m_bufferCursor = MAX_BUFFER_SIZE;
|
2011-06-11 13:37:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexManager::DestroyDeviceObjects()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
for (int i = 0; i < MAX_BUFFER_COUNT; i++)
|
|
|
|
{
|
|
|
|
SAFE_RELEASE(m_buffers[i]);
|
|
|
|
}
|
2011-06-11 13:37:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
VertexManager::VertexManager()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
LocalVBuffer.resize(MAXVBUFFERSIZE);
|
2014-10-25 11:04:33 -06:00
|
|
|
|
2016-08-21 21:46:52 -06:00
|
|
|
m_cur_buffer_pointer = m_base_buffer_pointer = &LocalVBuffer[0];
|
|
|
|
m_end_buffer_pointer = m_base_buffer_pointer + LocalVBuffer.size();
|
2014-01-23 07:27:18 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
LocalIBuffer.resize(MAXIBUFFERSIZE);
|
2014-01-23 07:27:18 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
CreateDeviceObjects();
|
2011-06-11 13:37:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
VertexManager::~VertexManager()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
DestroyDeviceObjects();
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2014-10-25 11:04:33 -06:00
|
|
|
void VertexManager::PrepareDrawBuffers(u32 stride)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
|
|
|
|
2016-08-21 21:46:52 -06:00
|
|
|
u32 vertexBufferSize = u32(m_cur_buffer_pointer - m_base_buffer_pointer);
|
2016-06-24 02:43:46 -06: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-21 21:46:52 -06:00
|
|
|
memcpy(mappedData + m_vertexDrawOffset, m_base_buffer_pointer, vertexBufferSize);
|
2016-06-24 02:43:46 -06: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-19 20:17:16 -06:00
|
|
|
}
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2014-10-25 11:04:33 -06:00
|
|
|
void VertexManager::Draw(u32 stride)
|
2010-10-19 20:17:16 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06: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 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2016-12-27 17:37:41 -07:00
|
|
|
void VertexManager::vFlush()
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2016-12-27 17:37:41 -07:00
|
|
|
if (!PixelShaderCache::SetShader())
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR, true, { printf("Fail to set pixel shader\n"); });
|
|
|
|
return;
|
|
|
|
}
|
2014-10-25 11:04:33 -06:00
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
D3DVertexFormat* vertex_format =
|
|
|
|
static_cast<D3DVertexFormat*>(VertexLoaderManager::GetCurrentVertexFormat());
|
|
|
|
if (!VertexShaderCache::SetShader(vertex_format))
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR, true, { printf("Fail to set pixel shader\n"); });
|
|
|
|
return;
|
|
|
|
}
|
2014-11-02 15:40:52 -07:00
|
|
|
|
2016-08-21 21:46:52 -06:00
|
|
|
if (!GeometryShaderCache::SetShader(m_current_primitive_type))
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR, true, { printf("Fail to set pixel shader\n"); });
|
|
|
|
return;
|
|
|
|
}
|
2014-11-02 15:40:52 -07:00
|
|
|
|
2016-06-24 02:43:46 -06: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 15:40:52 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
u32 stride = VertexLoaderManager::GetCurrentVertexFormat()->GetVertexStride();
|
2014-10-25 11:04:33 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
PrepareDrawBuffers(stride);
|
2014-10-25 11:04:33 -06:00
|
|
|
|
2016-12-27 17:37:41 -07:00
|
|
|
g_renderer->ApplyState();
|
2013-02-28 17:07:34 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
Draw(stride);
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
g_renderer->RestoreState();
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
2010-10-02 18:41:06 -06:00
|
|
|
|
2014-01-23 07:27:18 -07:00
|
|
|
void VertexManager::ResetBuffer(u32 stride)
|
|
|
|
{
|
2016-08-21 21:46:52 -06:00
|
|
|
m_cur_buffer_pointer = m_base_buffer_pointer;
|
2016-06-24 02:43:46 -06:00
|
|
|
IndexGenerator::Start(GetIndexBuffer());
|
2014-01-23 07:27:18 -07:00
|
|
|
}
|
|
|
|
|
2010-06-13 13:50:06 -06:00
|
|
|
} // namespace
|