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
|
|
|
|
|
|
|
#include "D3DBase.h"
|
2011-01-25 16:43:08 +00:00
|
|
|
#include "PixelShaderCache.h"
|
2010-06-13 19:50:06 +00:00
|
|
|
#include "VertexManager.h"
|
|
|
|
#include "VertexShaderCache.h"
|
|
|
|
|
2011-01-25 16:43:08 +00:00
|
|
|
#include "BPMemory.h"
|
2010-12-05 14:15:36 +00:00
|
|
|
#include "Debugger.h"
|
2011-01-25 16:43:08 +00:00
|
|
|
#include "IndexGenerator.h"
|
|
|
|
#include "MainBase.h"
|
|
|
|
#include "PixelShaderManager.h"
|
|
|
|
#include "RenderBase.h"
|
2011-03-15 03:51:31 +00:00
|
|
|
#include "Render.h"
|
2011-01-25 16:43:08 +00:00
|
|
|
#include "Statistics.h"
|
|
|
|
#include "TextureCacheBase.h"
|
|
|
|
#include "VertexShaderManager.h"
|
|
|
|
#include "VideoConfig.h"
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
// internal state for loading vertices
|
|
|
|
extern NativeVertexFormat *g_nativeVertexFmt;
|
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
|
2013-03-12 16:48:20 +00:00
|
|
|
const UINT IBUFFER_SIZE = VertexManager::MAXIBUFFERSIZE * sizeof(u16) * 8;
|
|
|
|
const UINT VBUFFER_SIZE = VertexManager::MAXVBUFFERSIZE;
|
2013-03-15 16:29:12 +00:00
|
|
|
const UINT MAX_VBUFFER_COUNT = 2;
|
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
|
|
|
{
|
2010-10-20 02:17:16 +00:00
|
|
|
D3D11_BUFFER_DESC bufdesc = CD3D11_BUFFER_DESC(IBUFFER_SIZE,
|
|
|
|
D3D11_BIND_INDEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2012-10-26 14:34:02 +00:00
|
|
|
m_vertex_draw_offset = 0;
|
2014-01-15 20:44:46 +00:00
|
|
|
m_index_draw_offset = 0;
|
2012-10-26 14:34:02 +00:00
|
|
|
m_index_buffers = new PID3D11Buffer[MAX_VBUFFER_COUNT];
|
2013-10-29 05:23:17 +00:00
|
|
|
m_vertex_buffers = new PID3D11Buffer[MAX_VBUFFER_COUNT];
|
2012-10-26 14:34:02 +00:00
|
|
|
for (m_current_index_buffer = 0; m_current_index_buffer < MAX_VBUFFER_COUNT; m_current_index_buffer++)
|
2012-10-20 13:22:15 +00:00
|
|
|
{
|
2012-10-26 14:34:02 +00:00
|
|
|
m_index_buffers[m_current_index_buffer] = NULL;
|
|
|
|
CHECK(SUCCEEDED(D3D::device->CreateBuffer(&bufdesc, NULL, &m_index_buffers[m_current_index_buffer])),
|
2012-10-20 13:22:15 +00:00
|
|
|
"Failed to create index buffer.");
|
2012-10-26 14:34:02 +00:00
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_index_buffers[m_current_index_buffer], "index buffer of VertexManager");
|
2012-10-20 13:22:15 +00:00
|
|
|
}
|
|
|
|
bufdesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
|
|
|
bufdesc.ByteWidth = VBUFFER_SIZE;
|
2012-10-26 14:34:02 +00:00
|
|
|
for (m_current_vertex_buffer = 0; m_current_vertex_buffer < MAX_VBUFFER_COUNT; m_current_vertex_buffer++)
|
2012-10-20 13:22:15 +00:00
|
|
|
{
|
2012-10-26 14:34:02 +00:00
|
|
|
m_vertex_buffers[m_current_vertex_buffer] = NULL;
|
|
|
|
CHECK(SUCCEEDED(D3D::device->CreateBuffer(&bufdesc, NULL, &m_vertex_buffers[m_current_vertex_buffer])),
|
2012-10-20 13:22:15 +00:00
|
|
|
"Failed to create vertex buffer.");
|
2012-10-26 14:34:02 +00:00
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_vertex_buffers[m_current_vertex_buffer], "Vertex buffer of VertexManager");
|
2012-10-20 13:22:15 +00:00
|
|
|
}
|
2012-10-26 14:34:02 +00:00
|
|
|
m_current_vertex_buffer = 0;
|
|
|
|
m_current_index_buffer = 0;
|
|
|
|
m_index_buffer_cursor = IBUFFER_SIZE;
|
|
|
|
m_vertex_buffer_cursor = VBUFFER_SIZE;
|
2011-06-11 19:37:21 +00:00
|
|
|
m_lineShader.Init();
|
|
|
|
m_pointShader.Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexManager::DestroyDeviceObjects()
|
|
|
|
{
|
|
|
|
m_pointShader.Shutdown();
|
|
|
|
m_lineShader.Shutdown();
|
2012-10-26 14:34:02 +00:00
|
|
|
for (m_current_vertex_buffer = 0; m_current_vertex_buffer < MAX_VBUFFER_COUNT; m_current_vertex_buffer++)
|
2012-10-20 13:22:15 +00:00
|
|
|
{
|
2012-10-26 14:34:02 +00:00
|
|
|
SAFE_RELEASE(m_vertex_buffers[m_current_vertex_buffer]);
|
|
|
|
SAFE_RELEASE(m_index_buffers[m_current_vertex_buffer]);
|
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);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-10-26 14:34:02 +00:00
|
|
|
void VertexManager::PrepareDrawBuffers()
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
|
|
|
|
2013-02-21 11:36:29 +00:00
|
|
|
UINT vSize = UINT(s_pCurBufferPointer - s_pBaseBufferPointer);
|
2012-10-20 13:22:15 +00:00
|
|
|
D3D11_MAP MapType = D3D11_MAP_WRITE_NO_OVERWRITE;
|
2012-10-26 14:34:02 +00:00
|
|
|
if (m_vertex_buffer_cursor + vSize >= VBUFFER_SIZE)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2010-10-20 02:17:16 +00:00
|
|
|
// Wrap around
|
2013-10-29 05:23:17 +00:00
|
|
|
m_current_vertex_buffer = (m_current_vertex_buffer + 1) % MAX_VBUFFER_COUNT;
|
2012-10-26 14:34:02 +00:00
|
|
|
m_vertex_buffer_cursor = 0;
|
2012-10-20 13:22:15 +00:00
|
|
|
MapType = D3D11_MAP_WRITE_DISCARD;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
2012-10-20 13:22:15 +00:00
|
|
|
|
2012-10-26 14:34:02 +00:00
|
|
|
D3D::context->Map(m_vertex_buffers[m_current_vertex_buffer], 0, MapType, 0, &map);
|
2012-10-20 13:22:15 +00:00
|
|
|
|
2013-03-06 14:59:29 +00:00
|
|
|
memcpy((u8*)map.pData + m_vertex_buffer_cursor, s_pBaseBufferPointer, vSize);
|
2012-10-26 14:34:02 +00:00
|
|
|
D3D::context->Unmap(m_vertex_buffers[m_current_vertex_buffer], 0);
|
|
|
|
m_vertex_draw_offset = m_vertex_buffer_cursor;
|
|
|
|
m_vertex_buffer_cursor += vSize;
|
2010-10-20 02:17:16 +00:00
|
|
|
|
2014-01-15 20:44:46 +00:00
|
|
|
UINT iCount = IndexGenerator::GetIndexLen();
|
2012-10-20 13:22:15 +00:00
|
|
|
MapType = D3D11_MAP_WRITE_NO_OVERWRITE;
|
2012-10-26 14:34:02 +00:00
|
|
|
if (m_index_buffer_cursor + iCount >= (IBUFFER_SIZE / sizeof(u16)))
|
2010-10-20 02:17:16 +00:00
|
|
|
{
|
|
|
|
// Wrap around
|
2013-10-29 05:23:17 +00:00
|
|
|
m_current_index_buffer = (m_current_index_buffer + 1) % MAX_VBUFFER_COUNT;
|
2012-10-26 14:34:02 +00:00
|
|
|
m_index_buffer_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
|
|
|
D3D::context->Map(m_index_buffers[m_current_index_buffer], 0, MapType, 0, &map);
|
|
|
|
|
2014-01-15 20:44:46 +00:00
|
|
|
memcpy((u16*)map.pData + m_index_buffer_cursor, GetIndexBuffer(), sizeof(u16) * IndexGenerator::GetIndexLen());
|
2012-10-26 14:34:02 +00:00
|
|
|
D3D::context->Unmap(m_index_buffers[m_current_index_buffer], 0);
|
2014-01-15 20:44:46 +00:00
|
|
|
m_index_draw_offset = m_index_buffer_cursor;
|
2012-10-26 14:34:02 +00:00
|
|
|
m_index_buffer_cursor += iCount;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-05-23 19:07:01 +00:00
|
|
|
ADDSTAT(stats.thisFrame.bytesVertexStreamed, vSize);
|
|
|
|
ADDSTAT(stats.thisFrame.bytesIndexStreamed, iCount*sizeof(u16));
|
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
|
|
|
|
};
|
|
|
|
|
2010-10-20 03:11:22 +00:00
|
|
|
void VertexManager::Draw(UINT stride)
|
2010-10-20 02:17:16 +00:00
|
|
|
{
|
2012-10-26 14:34:02 +00:00
|
|
|
D3D::context->IASetVertexBuffers(0, 1, &m_vertex_buffers[m_current_vertex_buffer], &stride, &m_vertex_draw_offset);
|
|
|
|
D3D::context->IASetIndexBuffer(m_index_buffers[m_current_index_buffer], DXGI_FORMAT_R16_UINT, 0);
|
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
|
|
|
{
|
2013-04-08 14:34:47 +00:00
|
|
|
D3D::context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
|
2014-01-15 20:44:46 +00:00
|
|
|
D3D::context->DrawIndexed(IndexGenerator::GetIndexLen(), m_index_draw_offset, 0);
|
2010-06-13 19:50:06 +00:00
|
|
|
INCSTAT(stats.thisFrame.numIndexedDrawCalls);
|
|
|
|
}
|
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];
|
2011-04-11 01:49:32 +00:00
|
|
|
float vpWidth = 2.0f * xfregs.viewport.wd;
|
|
|
|
float vpHeight = -2.0f * xfregs.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;
|
|
|
|
|
|
|
|
if (m_lineShader.SetShader(g_nativeVertexFmt->m_components, lineWidth,
|
|
|
|
texOffset, vpWidth, vpHeight, texOffsetEnable))
|
2011-03-14 09:38:29 +00:00
|
|
|
{
|
2014-01-15 20:44:46 +00:00
|
|
|
((DX11::Renderer*)g_renderer)->ApplyCullDisable(); // Disable culling for lines and points
|
2011-06-11 19:37:21 +00:00
|
|
|
D3D::context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
|
2014-01-15 20:44:46 +00:00
|
|
|
D3D::context->DrawIndexed(IndexGenerator::GetIndexLen(), m_index_draw_offset, 0);
|
2011-03-14 09:38:29 +00:00
|
|
|
INCSTAT(stats.thisFrame.numIndexedDrawCalls);
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
D3D::context->GSSetShader(NULL, NULL, 0);
|
2014-01-15 20:44:46 +00:00
|
|
|
((DX11::Renderer*)g_renderer)->RestoreCull();
|
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];
|
2011-04-11 01:49:32 +00:00
|
|
|
float vpWidth = 2.0f * xfregs.viewport.wd;
|
|
|
|
float vpHeight = -2.0f * xfregs.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;
|
|
|
|
|
|
|
|
if (m_pointShader.SetShader(g_nativeVertexFmt->m_components, pointSize,
|
|
|
|
texOffset, vpWidth, vpHeight, texOffsetEnable))
|
2011-03-14 09:38:29 +00:00
|
|
|
{
|
2014-01-15 20:44:46 +00:00
|
|
|
((DX11::Renderer*)g_renderer)->ApplyCullDisable(); // Disable culling for lines and points
|
2011-06-11 19:37:21 +00:00
|
|
|
D3D::context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
|
2014-01-15 20:44:46 +00:00
|
|
|
D3D::context->DrawIndexed(IndexGenerator::GetIndexLen(), m_index_draw_offset, 0);
|
2011-03-14 09:38:29 +00:00
|
|
|
INCSTAT(stats.thisFrame.numIndexedDrawCalls);
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
D3D::context->GSSetShader(NULL, NULL, 0);
|
2014-01-15 20:44:46 +00:00
|
|
|
((DX11::Renderer*)g_renderer)->RestoreCull();
|
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
|
|
|
{
|
2011-06-11 19:37:21 +00:00
|
|
|
if (!PixelShaderCache::SetShader(
|
2012-08-10 16:57:37 +00:00
|
|
|
useDstAlpha ? DSTALPHA_DUAL_SOURCE_BLEND : DSTALPHA_NONE,
|
2010-10-21 05:22:18 +00:00
|
|
|
g_nativeVertexFmt->m_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
|
|
|
}
|
2011-06-11 19:37:21 +00:00
|
|
|
if (!VertexShaderCache::SetShader(g_nativeVertexFmt->m_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
|
|
|
}
|
2012-10-26 14:34:02 +00:00
|
|
|
PrepareDrawBuffers();
|
2010-06-13 19:50:06 +00:00
|
|
|
unsigned int stride = g_nativeVertexFmt->GetVertexStride();
|
|
|
|
g_nativeVertexFmt->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
|