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
|
|
|
|
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/VertexManager.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"
|
|
|
|
#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
|
|
|
{
|
2014-10-25 17:04:33 +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;
|
2014-10-25 17:24:08 +00:00
|
|
|
|
2014-10-25 17:04:33 +00:00
|
|
|
for (int i = 0; i < MAX_BUFFER_COUNT; i++)
|
2012-10-20 13:22:15 +00:00
|
|
|
{
|
2014-10-25 17:04:33 +00:00
|
|
|
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");
|
2012-10-20 13:22:15 +00:00
|
|
|
}
|
2014-10-25 17:04:33 +00:00
|
|
|
|
|
|
|
m_currentBuffer = 0;
|
|
|
|
m_bufferCursor = MAX_BUFFER_SIZE;
|
2011-06-11 19:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexManager::DestroyDeviceObjects()
|
|
|
|
{
|
2014-10-25 17:04:33 +00:00
|
|
|
for (int i = 0; i < MAX_BUFFER_COUNT; i++)
|
2012-10-20 13:22:15 +00:00
|
|
|
{
|
2014-10-25 17:04:33 +00:00
|
|
|
SAFE_RELEASE(m_buffers[i]);
|
2012-10-20 13:22:15 +00:00
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VertexManager::VertexManager()
|
|
|
|
{
|
2014-01-23 14:27:18 +00:00
|
|
|
LocalVBuffer.resize(MAXVBUFFERSIZE);
|
2014-10-25 17:04:33 +00:00
|
|
|
|
2014-01-23 14:27:18 +00:00
|
|
|
s_pCurBufferPointer = s_pBaseBufferPointer = &LocalVBuffer[0];
|
|
|
|
s_pEndBufferPointer = s_pBaseBufferPointer + LocalVBuffer.size();
|
|
|
|
|
|
|
|
LocalIBuffer.resize(MAXIBUFFERSIZE);
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
CreateDeviceObjects();
|
|
|
|
}
|
|
|
|
|
|
|
|
VertexManager::~VertexManager()
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
|
|
|
|
2014-10-25 17:04:33 +00:00
|
|
|
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)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2014-10-25 17:04:33 +00:00
|
|
|
cursor += stride - padding;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
2012-10-20 13:22:15 +00:00
|
|
|
|
2014-10-25 17:04:33 +00:00
|
|
|
D3D11_MAP MapType = D3D11_MAP_WRITE_NO_OVERWRITE;
|
|
|
|
if (cursor + totalBufferSize >= MAX_BUFFER_SIZE)
|
2010-10-20 02:17:16 +00:00
|
|
|
{
|
|
|
|
// Wrap around
|
2014-10-25 17:04:33 +00:00
|
|
|
m_currentBuffer = (m_currentBuffer + 1) % MAX_BUFFER_COUNT;
|
|
|
|
cursor = 0;
|
2012-10-20 13:22:15 +00:00
|
|
|
MapType = D3D11_MAP_WRITE_DISCARD;
|
2010-10-20 02:17:16 +00:00
|
|
|
}
|
2012-10-26 14:34:02 +00:00
|
|
|
|
2014-10-25 17:04:33 +00:00
|
|
|
m_vertexDrawOffset = cursor;
|
|
|
|
m_indexDrawOffset = cursor + vertexBufferSize;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-10-25 17:04:33 +00:00
|
|
|
D3D::context->Map(m_buffers[m_currentBuffer], 0, MapType, 0, &map);
|
|
|
|
u8* mappedData = reinterpret_cast<u8*>(map.pData);
|
|
|
|
memcpy(mappedData + m_vertexDrawOffset, s_pBaseBufferPointer, vertexBufferSize);
|
|
|
|
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
|
|
|
{
|
2014-10-25 17:04:33 +00:00
|
|
|
u32 indices = IndexGenerator::GetIndexLen();
|
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
D3D::stateman->SetVertexBuffer(m_buffers[m_currentBuffer], stride, 0);
|
|
|
|
D3D::stateman->SetIndexBuffer(m_buffers[m_currentBuffer]);
|
2014-10-25 17:10:41 +00:00
|
|
|
|
|
|
|
u32 baseVertex = m_vertexDrawOffset / stride;
|
|
|
|
u32 startIndex = m_indexDrawOffset / sizeof(u16);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-12-17 02:05:23 +00:00
|
|
|
switch (current_primitive_type)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2014-12-17 02:05:23 +00:00
|
|
|
case PRIMITIVE_POINTS:
|
|
|
|
D3D::stateman->SetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
|
2015-12-21 02:49:49 +00:00
|
|
|
static_cast<Renderer*>(g_renderer.get())->ApplyCullDisable();
|
2014-12-17 02:05:23 +00:00
|
|
|
break;
|
|
|
|
case PRIMITIVE_LINES:
|
|
|
|
D3D::stateman->SetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
|
2015-12-21 02:49:49 +00:00
|
|
|
static_cast<Renderer*>(g_renderer.get())->ApplyCullDisable();
|
2014-12-17 02:05:23 +00:00
|
|
|
break;
|
|
|
|
case PRIMITIVE_TRIANGLES:
|
|
|
|
D3D::stateman->SetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
|
|
|
|
break;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
2014-10-29 00:19:09 +00:00
|
|
|
|
2014-12-17 02:05:23 +00:00
|
|
|
D3D::stateman->Apply();
|
|
|
|
D3D::context->DrawIndexed(indices, startIndex, baseVertex);
|
2014-10-29 00:19:09 +00:00
|
|
|
|
2014-12-17 02:05:23 +00:00
|
|
|
INCSTAT(stats.thisFrame.numDrawCalls);
|
2011-03-14 09:38:29 +00:00
|
|
|
|
2014-12-17 02:05:23 +00:00
|
|
|
if (current_primitive_type != PRIMITIVE_TRIANGLES)
|
2015-12-21 02:49:49 +00:00
|
|
|
static_cast<Renderer*>(g_renderer.get())->RestoreCull();
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2014-02-03 15:56:17 +00:00
|
|
|
void VertexManager::vFlush(bool useDstAlpha)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2011-06-11 19:37:21 +00:00
|
|
|
if (!PixelShaderCache::SetShader(
|
2015-11-01 21:39:31 +00:00
|
|
|
useDstAlpha ? DSTALPHA_DUAL_SOURCE_BLEND : DSTALPHA_NONE))
|
2010-12-05 14:15:36 +00:00
|
|
|
{
|
|
|
|
GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set pixel shader\n");});
|
2013-02-22 07:41:52 +00:00
|
|
|
return;
|
2010-12-05 14:15:36 +00:00
|
|
|
}
|
2014-10-25 17:04:33 +00:00
|
|
|
|
2015-11-01 21:39:31 +00:00
|
|
|
if (!VertexShaderCache::SetShader())
|
2010-12-05 14:15:36 +00:00
|
|
|
{
|
|
|
|
GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set pixel shader\n");});
|
2013-02-22 07:41:52 +00:00
|
|
|
return;
|
2010-12-05 14:15:36 +00:00
|
|
|
}
|
2014-11-02 22:40:52 +00:00
|
|
|
|
2014-12-14 18:41:16 +00:00
|
|
|
if (!GeometryShaderCache::SetShader(current_primitive_type))
|
2014-11-02 22:40:52 +00:00
|
|
|
{
|
2014-12-14 18:41:16 +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
|
|
|
}
|
|
|
|
|
2014-12-05 02:01:20 +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
|
|
|
|
2014-10-25 17:04:33 +00:00
|
|
|
u32 stride = VertexLoaderManager::GetCurrentVertexFormat()->GetVertexStride();
|
|
|
|
|
|
|
|
PrepareDrawBuffers(stride);
|
|
|
|
|
2014-07-25 23:10:44 +00:00
|
|
|
VertexLoaderManager::GetCurrentVertexFormat()->SetupVertexPointers();
|
2012-08-10 16:57:37 +00:00
|
|
|
g_renderer->ApplyState(useDstAlpha);
|
2013-03-01 00:07:34 +00:00
|
|
|
|
2010-10-20 03:11:22 +00:00
|
|
|
Draw(stride);
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2012-03-29 23:56:24 +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)
|
|
|
|
{
|
|
|
|
s_pCurBufferPointer = s_pBaseBufferPointer;
|
|
|
|
IndexGenerator::Start(GetIndexBuffer());
|
|
|
|
}
|
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
} // namespace
|