2013-04-17 23:29:41 -04: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-04 23:01:20 -03:00
|
|
|
#include "VideoBackends/D3D/BoundingBox.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
2014-10-29 01:19:09 +01:00
|
|
|
#include "VideoBackends/D3D/D3DState.h"
|
2014-12-03 22:17:56 +01:00
|
|
|
#include "VideoBackends/D3D/GeometryShaderCache.h"
|
2014-02-17 05:18:15 -05: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-04 23:01:20 -03:00
|
|
|
#include "VideoCommon/BoundingBox.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#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-26 01:10:44 +02:00
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2014-02-17 05:18:15 -05: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 19:04:33 +02: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 19:04:33 +02: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 19:24:08 +02:00
|
|
|
|
2014-10-25 19:04:33 +02:00
|
|
|
for (int i = 0; i < MAX_BUFFER_COUNT; i++)
|
2012-10-20 10:22:15 -03:00
|
|
|
{
|
2014-10-25 19:04:33 +02: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 10:22:15 -03:00
|
|
|
}
|
2014-10-25 19:04:33 +02:00
|
|
|
|
|
|
|
m_currentBuffer = 0;
|
|
|
|
m_bufferCursor = MAX_BUFFER_SIZE;
|
2011-06-11 19:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexManager::DestroyDeviceObjects()
|
|
|
|
{
|
2014-10-25 19:04:33 +02:00
|
|
|
for (int i = 0; i < MAX_BUFFER_COUNT; i++)
|
2012-10-20 10:22:15 -03:00
|
|
|
{
|
2014-10-25 19:04:33 +02:00
|
|
|
SAFE_RELEASE(m_buffers[i]);
|
2012-10-20 10:22:15 -03:00
|
|
|
}
|
2013-10-29 01:23:17 -04:00
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VertexManager::VertexManager()
|
|
|
|
{
|
2014-01-23 15:27:18 +01:00
|
|
|
LocalVBuffer.resize(MAXVBUFFERSIZE);
|
2014-10-25 19:04:33 +02:00
|
|
|
|
2014-01-23 15:27:18 +01: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 19:04:33 +02:00
|
|
|
void VertexManager::PrepareDrawBuffers(u32 stride)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
|
|
|
|
2014-10-25 19:04:33 +02: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 19:04:33 +02:00
|
|
|
cursor += stride - padding;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
2012-10-20 10:22:15 -03:00
|
|
|
|
2014-10-25 19:04:33 +02: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 19:04:33 +02:00
|
|
|
m_currentBuffer = (m_currentBuffer + 1) % MAX_BUFFER_COUNT;
|
|
|
|
cursor = 0;
|
2012-10-20 10:22:15 -03:00
|
|
|
MapType = D3D11_MAP_WRITE_DISCARD;
|
2010-10-20 02:17:16 +00:00
|
|
|
}
|
2012-10-26 11:34:02 -03:00
|
|
|
|
2014-10-25 19:04:33 +02:00
|
|
|
m_vertexDrawOffset = cursor;
|
|
|
|
m_indexDrawOffset = cursor + vertexBufferSize;
|
2013-10-29 01:23:17 -04:00
|
|
|
|
2014-10-25 19:04:33 +02: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 19:04:33 +02:00
|
|
|
void VertexManager::Draw(u32 stride)
|
2010-10-20 02:17:16 +00:00
|
|
|
{
|
2014-10-25 19:04:33 +02:00
|
|
|
u32 indices = IndexGenerator::GetIndexLen();
|
|
|
|
|
2014-12-06 14:54:06 +01:00
|
|
|
D3D::stateman->SetVertexBuffer(m_buffers[m_currentBuffer], stride, 0);
|
|
|
|
D3D::stateman->SetIndexBuffer(m_buffers[m_currentBuffer]);
|
2014-10-25 19:10:41 +02:00
|
|
|
|
|
|
|
u32 baseVertex = m_vertexDrawOffset / stride;
|
|
|
|
u32 startIndex = m_indexDrawOffset / sizeof(u16);
|
2013-10-29 01:23:17 -04:00
|
|
|
|
2014-12-17 03:05:23 +01:00
|
|
|
switch (current_primitive_type)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2014-12-17 03:05:23 +01:00
|
|
|
case PRIMITIVE_POINTS:
|
|
|
|
D3D::stateman->SetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
|
|
|
|
((DX11::Renderer*)g_renderer)->ApplyCullDisable();
|
|
|
|
break;
|
|
|
|
case PRIMITIVE_LINES:
|
|
|
|
D3D::stateman->SetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
|
|
|
|
((DX11::Renderer*)g_renderer)->ApplyCullDisable();
|
|
|
|
break;
|
|
|
|
case PRIMITIVE_TRIANGLES:
|
|
|
|
D3D::stateman->SetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
|
|
|
|
break;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
2014-10-29 01:19:09 +01:00
|
|
|
|
2014-12-17 03:05:23 +01:00
|
|
|
D3D::stateman->Apply();
|
|
|
|
D3D::context->DrawIndexed(indices, startIndex, baseVertex);
|
2014-10-29 01:19:09 +01:00
|
|
|
|
2014-12-17 03:05:23 +01:00
|
|
|
INCSTAT(stats.thisFrame.numDrawCalls);
|
2011-03-14 09:38:29 +00:00
|
|
|
|
2014-12-17 03:05:23 +01:00
|
|
|
if (current_primitive_type != PRIMITIVE_TRIANGLES)
|
2014-12-16 01:49:29 +01:00
|
|
|
((DX11::Renderer*)g_renderer)->RestoreCull();
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2014-02-03 16:56:17 +01:00
|
|
|
void VertexManager::vFlush(bool useDstAlpha)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2014-07-26 01:10:44 +02:00
|
|
|
u32 components = VertexLoaderManager::GetCurrentVertexFormat()->m_components;
|
2014-10-25 19:04:33 +02:00
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
if (!PixelShaderCache::SetShader(
|
2014-07-26 01:10:44 +02: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 01:41:52 -06:00
|
|
|
return;
|
2010-12-05 14:15:36 +00:00
|
|
|
}
|
2014-10-25 19:04:33 +02:00
|
|
|
|
2014-07-26 01:10:44 +02: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 01:41:52 -06:00
|
|
|
return;
|
2010-12-05 14:15:36 +00:00
|
|
|
}
|
2014-11-02 23:40:52 +01:00
|
|
|
|
2014-12-14 19:41:16 +01:00
|
|
|
if (!GeometryShaderCache::SetShader(current_primitive_type))
|
2014-11-02 23:40:52 +01:00
|
|
|
{
|
2014-12-14 19:41:16 +01:00
|
|
|
GFX_DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR, true, { printf("Fail to set pixel shader\n"); });
|
|
|
|
return;
|
2014-11-02 23:40:52 +01:00
|
|
|
}
|
|
|
|
|
2014-12-04 23:01:20 -03: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 23:40:52 +01:00
|
|
|
|
2014-10-25 19:04:33 +02:00
|
|
|
u32 stride = VertexLoaderManager::GetCurrentVertexFormat()->GetVertexStride();
|
2014-12-25 00:34:22 -07:00
|
|
|
u32 indices = IndexGenerator::GetIndexLen();
|
2014-10-25 19:04:33 +02:00
|
|
|
|
|
|
|
PrepareDrawBuffers(stride);
|
|
|
|
|
2014-12-25 00:34:22 -07:00
|
|
|
if (!bpmem.genMode.zfreeze && indices >= 3)
|
|
|
|
{
|
|
|
|
float vtx[9];
|
|
|
|
float out[12];
|
|
|
|
|
|
|
|
// Lookup vertices of the last rendered triangle and software-transform them
|
|
|
|
// This allows us to determine the depth slope, which will be used if zfreeze
|
|
|
|
// is enabled in the following flush.
|
|
|
|
for (unsigned int i = 0; i < 3; ++i)
|
|
|
|
{
|
|
|
|
const int base_index = GetIndexBuffer()[indices - 3 + i];
|
|
|
|
u8* vtx_ptr = &((u8*)GetVertexBuffer())[base_index * stride];
|
|
|
|
vtx[0 + i * 3] = ((float*)vtx_ptr)[0];
|
|
|
|
vtx[1 + i * 3] = ((float*)vtx_ptr)[1];
|
|
|
|
vtx[2 + i * 3] = ((float*)vtx_ptr)[2];
|
|
|
|
|
|
|
|
VertexShaderManager::TransformToClipSpace(&vtx[i * 3], &out[i * 4]);
|
|
|
|
|
|
|
|
// viewport offset ignored because we only look at coordinate differences.
|
|
|
|
out[0 + i * 4] = out[0 + i * 4] / out[3 + i * 4] * xfmem.viewport.wd;
|
|
|
|
out[1 + i * 4] = out[1 + i * 4] / out[3 + i * 4] * xfmem.viewport.ht;
|
|
|
|
out[2 + i * 4] = out[2 + i * 4] / out[3 + i * 4] * xfmem.viewport.zRange + xfmem.viewport.farZ;
|
|
|
|
}
|
|
|
|
float dx31 = out[8] - out[0];
|
|
|
|
float dx12 = out[0] - out[4];
|
|
|
|
float dy12 = out[1] - out[5];
|
|
|
|
float dy31 = out[9] - out[1];
|
|
|
|
|
|
|
|
float DF31 = out[10] - out[2];
|
|
|
|
float DF21 = out[6] - out[2];
|
|
|
|
float a = DF31 * -dy12 - DF21 * dy31;
|
|
|
|
float b = dx31 * DF21 + dx12 * DF31;
|
|
|
|
float c = -dx12 * dy31 - dx31 * -dy12;
|
|
|
|
|
|
|
|
float slope_dfdx = -a / c;
|
|
|
|
float slope_dfdy = -b / c;
|
|
|
|
float slope_f0 = out[2];
|
|
|
|
|
|
|
|
PixelShaderManager::SetZSlopeChanged(slope_dfdx, slope_dfdy, slope_f0);
|
|
|
|
}
|
|
|
|
|
2014-07-26 01:10:44 +02:00
|
|
|
VertexLoaderManager::GetCurrentVertexFormat()->SetupVertexPointers();
|
2012-08-10 18:57:37 +02:00
|
|
|
g_renderer->ApplyState(useDstAlpha);
|
2013-03-01 01:07:34 +01:00
|
|
|
|
2010-10-20 03:11:22 +00:00
|
|
|
Draw(stride);
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2012-03-30 01:56:24 +02:00
|
|
|
g_renderer->RestoreState();
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
2010-10-03 00:41:06 +00:00
|
|
|
|
2014-01-23 15:27:18 +01:00
|
|
|
void VertexManager::ResetBuffer(u32 stride)
|
|
|
|
{
|
|
|
|
s_pCurBufferPointer = s_pBaseBufferPointer;
|
|
|
|
IndexGenerator::Start(GetIndexBuffer());
|
|
|
|
}
|
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
} // namespace
|