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
|
|
|
|
2017-02-01 04:29:29 +00:00
|
|
|
#include "VideoBackends/D3D/VertexManager.h"
|
|
|
|
|
|
|
|
#include <d3d11.h>
|
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
#include "Common/Align.h"
|
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"
|
2018-02-24 15:15:35 +00:00
|
|
|
#include "VideoBackends/D3D/FramebufferManager.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/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"
|
2018-11-27 07:16:53 +00:00
|
|
|
#include "VideoCommon/GeometryShaderManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/IndexGenerator.h"
|
2017-02-01 04:29:29 +00:00
|
|
|
#include "VideoCommon/NativeVertexFormat.h"
|
2018-11-27 07:16:53 +00:00
|
|
|
#include "VideoCommon/PixelShaderManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/RenderBase.h"
|
|
|
|
#include "VideoCommon/Statistics.h"
|
2014-07-25 23:10:44 +00:00
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2018-11-27 07:16:53 +00:00
|
|
|
#include "VideoCommon/VertexShaderManager.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
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
static ID3D11Buffer* AllocateConstantBuffer(u32 size)
|
|
|
|
{
|
|
|
|
const u32 cbsize = Common::AlignUp(size, 16u); // must be a multiple of 16
|
|
|
|
const CD3D11_BUFFER_DESC cbdesc(cbsize, D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC,
|
|
|
|
D3D11_CPU_ACCESS_WRITE);
|
|
|
|
ID3D11Buffer* cbuf;
|
|
|
|
const HRESULT hr = D3D::device->CreateBuffer(&cbdesc, nullptr, &cbuf);
|
|
|
|
CHECK(hr == S_OK, "shader constant buffer (size=%u)", cbsize);
|
|
|
|
D3D::SetDebugObjectName(cbuf, "constant buffer used to emulate the GX pipeline");
|
|
|
|
return cbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdateConstantBuffer(ID3D11Buffer* const buffer, const void* data, u32 data_size)
|
|
|
|
{
|
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
|
|
|
D3D::context->Map(buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
|
|
|
|
memcpy(map.pData, data, data_size);
|
|
|
|
D3D::context->Unmap(buffer, 0);
|
|
|
|
|
|
|
|
ADDSTAT(stats.thisFrame.bytesUniformStreamed, data_size);
|
|
|
|
}
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
void VertexManager::CreateDeviceObjects()
|
2010-10-03 00:41:06 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +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);
|
|
|
|
|
|
|
|
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 18:22:18 +00:00
|
|
|
D3D::SetDebugObjectName(m_buffers[i], "Buffer of VertexManager");
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
m_buffer_cursor = MAX_BUFFER_SIZE;
|
|
|
|
|
|
|
|
m_vertex_constant_buffer = AllocateConstantBuffer(sizeof(VertexShaderConstants));
|
|
|
|
m_geometry_constant_buffer = AllocateConstantBuffer(sizeof(GeometryShaderConstants));
|
|
|
|
m_pixel_constant_buffer = AllocateConstantBuffer(sizeof(PixelShaderConstants));
|
2011-06-11 19:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexManager::DestroyDeviceObjects()
|
|
|
|
{
|
2018-11-27 07:16:53 +00:00
|
|
|
SAFE_RELEASE(m_pixel_constant_buffer);
|
|
|
|
SAFE_RELEASE(m_geometry_constant_buffer);
|
|
|
|
SAFE_RELEASE(m_vertex_constant_buffer);
|
2016-06-24 08:43:46 +00:00
|
|
|
for (int i = 0; i < MAX_BUFFER_COUNT; i++)
|
|
|
|
{
|
|
|
|
SAFE_RELEASE(m_buffers[i]);
|
|
|
|
}
|
2011-06-11 19:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VertexManager::VertexManager()
|
|
|
|
{
|
2018-11-27 07:16:53 +00:00
|
|
|
m_staging_vertex_buffer.resize(MAXVBUFFERSIZE);
|
2014-10-25 17:04:33 +00:00
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
m_cur_buffer_pointer = m_base_buffer_pointer = &m_staging_vertex_buffer[0];
|
|
|
|
m_end_buffer_pointer = m_base_buffer_pointer + m_staging_vertex_buffer.size();
|
2014-01-23 14:27:18 +00:00
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
m_staging_index_buffer.resize(MAXIBUFFERSIZE);
|
2014-01-23 14:27:18 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
CreateDeviceObjects();
|
2011-06-11 19:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VertexManager::~VertexManager()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
DestroyDeviceObjects();
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
void VertexManager::UploadUtilityUniforms(const void* uniforms, u32 uniforms_size)
|
|
|
|
{
|
|
|
|
// Just use the one buffer for all three.
|
|
|
|
UpdateConstantBuffer(m_vertex_constant_buffer, uniforms, uniforms_size);
|
|
|
|
D3D::stateman->SetVertexConstants(m_vertex_constant_buffer);
|
|
|
|
D3D::stateman->SetGeometryConstants(m_vertex_constant_buffer);
|
|
|
|
D3D::stateman->SetPixelConstants(m_vertex_constant_buffer);
|
|
|
|
VertexShaderManager::dirty = true;
|
|
|
|
GeometryShaderManager::dirty = true;
|
|
|
|
PixelShaderManager::dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexManager::ResetBuffer(u32 vertex_stride, bool cull_all)
|
|
|
|
{
|
|
|
|
m_cur_buffer_pointer = m_base_buffer_pointer;
|
|
|
|
IndexGenerator::Start(m_staging_index_buffer.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexManager::CommitBuffer(u32 num_vertices, u32 vertex_stride, u32 num_indices,
|
|
|
|
u32* out_base_vertex, u32* out_base_index)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
u32 vertexBufferSize = Common::AlignUp(num_vertices * vertex_stride, sizeof(u16));
|
|
|
|
u32 indexBufferSize = num_indices * sizeof(u16);
|
2016-06-24 08:43:46 +00:00
|
|
|
u32 totalBufferSize = vertexBufferSize + indexBufferSize;
|
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
u32 cursor = m_buffer_cursor;
|
|
|
|
u32 padding = vertex_stride > 0 ? (m_buffer_cursor % vertex_stride) : 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
if (padding)
|
|
|
|
{
|
2018-11-27 07:16:53 +00:00
|
|
|
cursor += vertex_stride - padding;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
D3D11_MAP MapType = D3D11_MAP_WRITE_NO_OVERWRITE;
|
|
|
|
if (cursor + totalBufferSize >= MAX_BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
// Wrap around
|
2018-11-27 07:16:53 +00:00
|
|
|
m_current_buffer = (m_current_buffer + 1) % MAX_BUFFER_COUNT;
|
2016-06-24 08:43:46 +00:00
|
|
|
cursor = 0;
|
|
|
|
MapType = D3D11_MAP_WRITE_DISCARD;
|
|
|
|
}
|
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
*out_base_vertex = vertex_stride > 0 ? (cursor / vertex_stride) : 0;
|
|
|
|
*out_base_index = (cursor + vertexBufferSize) / sizeof(u16);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
D3D::context->Map(m_buffers[m_current_buffer], 0, MapType, 0, &map);
|
2016-06-24 08:43:46 +00:00
|
|
|
u8* mappedData = reinterpret_cast<u8*>(map.pData);
|
2018-11-27 07:16:53 +00:00
|
|
|
if (vertexBufferSize > 0)
|
|
|
|
std::memcpy(mappedData + cursor, m_base_buffer_pointer, vertexBufferSize);
|
|
|
|
if (indexBufferSize > 0)
|
|
|
|
std::memcpy(mappedData + cursor + vertexBufferSize, m_staging_index_buffer.data(),
|
|
|
|
indexBufferSize);
|
|
|
|
D3D::context->Unmap(m_buffers[m_current_buffer], 0);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
m_buffer_cursor = cursor + totalBufferSize;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
ADDSTAT(stats.thisFrame.bytesVertexStreamed, vertexBufferSize);
|
|
|
|
ADDSTAT(stats.thisFrame.bytesIndexStreamed, indexBufferSize);
|
2018-11-27 07:16:53 +00:00
|
|
|
|
|
|
|
D3D::stateman->SetVertexBuffer(m_buffers[m_current_buffer], vertex_stride, 0);
|
|
|
|
D3D::stateman->SetIndexBuffer(m_buffers[m_current_buffer]);
|
2010-10-20 02:17:16 +00:00
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
void VertexManager::UploadConstants()
|
2010-10-20 02:17:16 +00:00
|
|
|
{
|
2018-11-27 07:16:53 +00:00
|
|
|
if (VertexShaderManager::dirty)
|
|
|
|
{
|
|
|
|
UpdateConstantBuffer(m_vertex_constant_buffer, &VertexShaderManager::constants,
|
|
|
|
sizeof(VertexShaderConstants));
|
|
|
|
VertexShaderManager::dirty = false;
|
|
|
|
}
|
|
|
|
if (GeometryShaderManager::dirty)
|
|
|
|
{
|
|
|
|
UpdateConstantBuffer(m_geometry_constant_buffer, &GeometryShaderManager::constants,
|
|
|
|
sizeof(GeometryShaderConstants));
|
|
|
|
GeometryShaderManager::dirty = false;
|
|
|
|
}
|
|
|
|
if (PixelShaderManager::dirty)
|
|
|
|
{
|
|
|
|
UpdateConstantBuffer(m_pixel_constant_buffer, &PixelShaderManager::constants,
|
|
|
|
sizeof(PixelShaderConstants));
|
|
|
|
PixelShaderManager::dirty = false;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
D3D::stateman->SetPixelConstants(m_pixel_constant_buffer, g_ActiveConfig.bEnablePixelLighting ?
|
|
|
|
m_vertex_constant_buffer :
|
|
|
|
nullptr);
|
|
|
|
D3D::stateman->SetVertexConstants(m_vertex_constant_buffer);
|
|
|
|
D3D::stateman->SetGeometryConstants(m_geometry_constant_buffer);
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
void VertexManager::DrawCurrentBatch(u32 base_index, u32 num_indices, u32 base_vertex)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2018-02-24 15:15:35 +00:00
|
|
|
FramebufferManager::SetIntegerEFBRenderTarget(
|
|
|
|
m_current_pipeline_config.blending_state.logicopenable);
|
2018-11-27 07:16:53 +00:00
|
|
|
|
2018-03-16 14:20:43 +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);
|
|
|
|
}
|
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
D3D::stateman->Apply();
|
|
|
|
D3D::context->DrawIndexed(num_indices, base_index, base_vertex);
|
2014-01-23 14:27:18 +00:00
|
|
|
}
|
2018-11-27 07:16:53 +00:00
|
|
|
} // namespace DX11
|