2013-04-18 03:29:41 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2010-06-13 19:50:06 +00:00
|
|
|
|
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/BPMemory.h"
|
|
|
|
#include "VideoCommon/Debugger.h"
|
|
|
|
#include "VideoCommon/IndexGenerator.h"
|
|
|
|
#include "VideoCommon/MainBase.h"
|
|
|
|
#include "VideoCommon/PixelShaderManager.h"
|
|
|
|
#include "VideoCommon/RenderBase.h"
|
|
|
|
#include "VideoCommon/Statistics.h"
|
|
|
|
#include "VideoCommon/TextureCacheBase.h"
|
2014-07-25 23:10:44 +00:00
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VertexShaderManager.h"
|
|
|
|
#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
|
|
|
m_lineShader.Init();
|
|
|
|
m_pointShader.Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexManager::DestroyDeviceObjects()
|
|
|
|
{
|
|
|
|
m_pointShader.Shutdown();
|
|
|
|
m_lineShader.Shutdown();
|
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
|
|
|
|
2011-03-14 09:38:29 +00:00
|
|
|
static const float LINE_PT_TEX_OFFSETS[8] = {
|
|
|
|
0.f, 0.0625f, 0.125f, 0.25f, 0.5f, 1.f, 1.f, 1.f
|
|
|
|
};
|
|
|
|
|
2014-10-25 17:04:33 +00:00
|
|
|
void VertexManager::Draw(u32 stride)
|
2010-10-20 02:17:16 +00:00
|
|
|
{
|
2014-07-25 23:10:44 +00:00
|
|
|
u32 components = VertexLoaderManager::GetCurrentVertexFormat()->m_components;
|
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-01-15 20:44:46 +00:00
|
|
|
if (current_primitive_type == PRIMITIVE_TRIANGLES)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2014-12-06 13:54:06 +00:00
|
|
|
D3D::stateman->SetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
|
2014-10-29 00:19:09 +00:00
|
|
|
|
|
|
|
D3D::stateman->Apply();
|
2014-10-25 17:10:41 +00:00
|
|
|
D3D::context->DrawIndexed(indices, startIndex, baseVertex);
|
2014-10-29 00:19:09 +00:00
|
|
|
|
2014-06-02 17:36:09 +00:00
|
|
|
INCSTAT(stats.thisFrame.numDrawCalls);
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
2014-01-15 20:44:46 +00:00
|
|
|
else if (current_primitive_type == PRIMITIVE_LINES)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2011-03-14 09:38:29 +00:00
|
|
|
float lineWidth = float(bpmem.lineptwidth.linesize) / 6.f;
|
|
|
|
float texOffset = LINE_PT_TEX_OFFSETS[bpmem.lineptwidth.lineoff];
|
2014-04-27 18:59:04 +00:00
|
|
|
float vpWidth = 2.0f * xfmem.viewport.wd;
|
|
|
|
float vpHeight = -2.0f * xfmem.viewport.ht;
|
2011-03-14 09:38:29 +00:00
|
|
|
|
2011-04-15 22:34:54 +00:00
|
|
|
bool texOffsetEnable[8];
|
2011-06-11 19:37:21 +00:00
|
|
|
|
2011-04-15 22:34:54 +00:00
|
|
|
for (int i = 0; i < 8; ++i)
|
|
|
|
texOffsetEnable[i] = bpmem.texcoords[i].s.line_offset;
|
|
|
|
|
2014-07-25 23:10:44 +00:00
|
|
|
if (m_lineShader.SetShader(components, lineWidth,
|
2011-04-15 22:34:54 +00:00
|
|
|
texOffset, vpWidth, vpHeight, texOffsetEnable))
|
2011-03-14 09:38:29 +00:00
|
|
|
{
|
2014-12-06 13:54:06 +00:00
|
|
|
D3D::stateman->SetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
|
2014-10-29 00:19:09 +00:00
|
|
|
|
|
|
|
D3D::stateman->Apply();
|
2014-10-25 17:10:41 +00:00
|
|
|
D3D::context->DrawIndexed(indices, startIndex, baseVertex);
|
2014-10-29 00:19:09 +00:00
|
|
|
|
2014-06-02 17:36:09 +00:00
|
|
|
INCSTAT(stats.thisFrame.numDrawCalls);
|
2011-03-14 09:38:29 +00:00
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
D3D::stateman->SetGeometryShader(nullptr);
|
2011-03-14 09:38:29 +00:00
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
2014-01-15 20:44:46 +00:00
|
|
|
else //if (current_primitive_type == PRIMITIVE_POINTS)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2011-03-14 09:38:29 +00:00
|
|
|
float pointSize = float(bpmem.lineptwidth.pointsize) / 6.f;
|
|
|
|
float texOffset = LINE_PT_TEX_OFFSETS[bpmem.lineptwidth.pointoff];
|
2014-04-27 18:59:04 +00:00
|
|
|
float vpWidth = 2.0f * xfmem.viewport.wd;
|
|
|
|
float vpHeight = -2.0f * xfmem.viewport.ht;
|
2011-03-14 09:38:29 +00:00
|
|
|
|
2011-04-15 22:34:54 +00:00
|
|
|
bool texOffsetEnable[8];
|
2011-06-11 19:37:21 +00:00
|
|
|
|
2011-04-15 22:34:54 +00:00
|
|
|
for (int i = 0; i < 8; ++i)
|
|
|
|
texOffsetEnable[i] = bpmem.texcoords[i].s.point_offset;
|
|
|
|
|
2014-07-25 23:10:44 +00:00
|
|
|
if (m_pointShader.SetShader(components, pointSize,
|
2011-04-15 22:34:54 +00:00
|
|
|
texOffset, vpWidth, vpHeight, texOffsetEnable))
|
2011-03-14 09:38:29 +00:00
|
|
|
{
|
2014-12-06 13:54:06 +00:00
|
|
|
D3D::stateman->SetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
|
2014-10-29 00:19:09 +00:00
|
|
|
|
|
|
|
D3D::stateman->Apply();
|
2014-10-25 17:10:41 +00:00
|
|
|
D3D::context->DrawIndexed(indices, startIndex, baseVertex);
|
2014-10-29 00:19:09 +00:00
|
|
|
|
2014-06-02 17:36:09 +00:00
|
|
|
INCSTAT(stats.thisFrame.numDrawCalls);
|
2011-03-14 09:38:29 +00:00
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
D3D::stateman->SetGeometryShader(nullptr);
|
2011-03-14 09:38:29 +00:00
|
|
|
}
|
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
|
|
|
{
|
2014-07-25 23:10:44 +00:00
|
|
|
u32 components = VertexLoaderManager::GetCurrentVertexFormat()->m_components;
|
2014-10-25 17:04:33 +00:00
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
if (!PixelShaderCache::SetShader(
|
2014-07-25 23:10:44 +00:00
|
|
|
useDstAlpha ? DSTALPHA_DUAL_SOURCE_BLEND : DSTALPHA_NONE, components))
|
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
|
|
|
|
2014-07-25 23:10:44 +00:00
|
|
|
if (!VertexShaderCache::SetShader(components))
|
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-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-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
|