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"
|
2019-02-15 01:59:50 +00:00
|
|
|
#include "Common/Assert.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"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/D3D/Render.h"
|
2019-03-09 13:31:36 +00:00
|
|
|
#include "VideoBackends/D3DCommon/Common.h"
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2014-12-05 02:01:20 +00:00
|
|
|
#include "VideoCommon/BoundingBox.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/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
|
|
|
{
|
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);
|
2019-03-09 13:31:36 +00:00
|
|
|
CHECK(SUCCEEDED(hr), "shader constant buffer (size=%u)", cbsize);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
D3DCommon::SetDebugObjectName(cbuf, "constant buffer");
|
2018-11-27 07:16:53 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
static ID3D11ShaderResourceView*
|
|
|
|
CreateTexelBufferView(ID3D11Buffer* buffer, TexelBufferFormat format, DXGI_FORMAT srv_format)
|
2010-10-03 00:41:06 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
ID3D11ShaderResourceView* srv;
|
|
|
|
CD3D11_SHADER_RESOURCE_VIEW_DESC srv_desc(buffer, srv_format, 0,
|
|
|
|
VertexManager::TEXEL_STREAM_BUFFER_SIZE /
|
|
|
|
VertexManager::GetTexelBufferElementSize(format));
|
|
|
|
CHECK(SUCCEEDED(D3D::device->CreateShaderResourceView(buffer, &srv_desc, &srv)),
|
|
|
|
"Create SRV for texel buffer");
|
|
|
|
return srv;
|
|
|
|
}
|
|
|
|
|
|
|
|
VertexManager::VertexManager() = default;
|
|
|
|
|
|
|
|
VertexManager::~VertexManager()
|
|
|
|
{
|
|
|
|
for (auto& srv_ptr : m_texel_buffer_views)
|
|
|
|
SAFE_RELEASE(srv_ptr);
|
|
|
|
SAFE_RELEASE(m_texel_buffer);
|
|
|
|
SAFE_RELEASE(m_pixel_constant_buffer);
|
|
|
|
SAFE_RELEASE(m_geometry_constant_buffer);
|
|
|
|
SAFE_RELEASE(m_vertex_constant_buffer);
|
|
|
|
for (auto& buffer : m_buffers)
|
|
|
|
SAFE_RELEASE(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VertexManager::Initialize()
|
|
|
|
{
|
|
|
|
if (!VertexManagerBase::Initialize())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CD3D11_BUFFER_DESC bufdesc((VERTEX_STREAM_BUFFER_SIZE + INDEX_STREAM_BUFFER_SIZE) / BUFFER_COUNT,
|
|
|
|
D3D11_BIND_INDEX_BUFFER | D3D11_BIND_VERTEX_BUFFER,
|
|
|
|
D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
for (int i = 0; i < BUFFER_COUNT; i++)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
CHECK(SUCCEEDED(D3D::device->CreateBuffer(&bufdesc, nullptr, &m_buffers[i])),
|
|
|
|
"Failed to create buffer.");
|
2019-03-09 13:31:36 +00:00
|
|
|
if (m_buffers[i])
|
|
|
|
D3DCommon::SetDebugObjectName(m_buffers[i], "Buffer of VertexManager");
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
m_vertex_constant_buffer = AllocateConstantBuffer(sizeof(VertexShaderConstants));
|
|
|
|
m_geometry_constant_buffer = AllocateConstantBuffer(sizeof(GeometryShaderConstants));
|
|
|
|
m_pixel_constant_buffer = AllocateConstantBuffer(sizeof(PixelShaderConstants));
|
2019-02-15 01:59:50 +00:00
|
|
|
if (!m_vertex_constant_buffer || !m_geometry_constant_buffer || !m_pixel_constant_buffer)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CD3D11_BUFFER_DESC texel_buf_desc(TEXEL_STREAM_BUFFER_SIZE, D3D11_BIND_SHADER_RESOURCE,
|
|
|
|
D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
|
|
|
|
CHECK(SUCCEEDED(D3D::device->CreateBuffer(&texel_buf_desc, nullptr, &m_texel_buffer)),
|
|
|
|
"Creating texel buffer failed");
|
|
|
|
if (!m_texel_buffer)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
static constexpr std::array<std::pair<TexelBufferFormat, DXGI_FORMAT>, NUM_TEXEL_BUFFER_FORMATS>
|
|
|
|
format_mapping = {{
|
|
|
|
{TEXEL_BUFFER_FORMAT_R8_UINT, DXGI_FORMAT_R8_UINT},
|
|
|
|
{TEXEL_BUFFER_FORMAT_R16_UINT, DXGI_FORMAT_R16_UINT},
|
2019-02-28 10:35:17 +00:00
|
|
|
{TEXEL_BUFFER_FORMAT_RGBA8_UINT, DXGI_FORMAT_R8G8B8A8_UINT},
|
2019-02-15 01:59:50 +00:00
|
|
|
{TEXEL_BUFFER_FORMAT_R32G32_UINT, DXGI_FORMAT_R32G32_UINT},
|
|
|
|
}};
|
|
|
|
for (const auto& it : format_mapping)
|
|
|
|
{
|
|
|
|
m_texel_buffer_views[it.first] = CreateTexelBufferView(m_texel_buffer, it.first, it.second);
|
|
|
|
if (!m_texel_buffer_views[it.first])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2011-06-11 19:37:21 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
void VertexManager::UploadUtilityUniforms(const void* uniforms, u32 uniforms_size)
|
2011-06-11 19:37:21 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
// Just use the one buffer for all three.
|
|
|
|
InvalidateConstants();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VertexManager::MapTexelBuffer(u32 required_size, D3D11_MAPPED_SUBRESOURCE& sr)
|
|
|
|
{
|
|
|
|
if ((m_texel_buffer_offset + required_size) > TEXEL_STREAM_BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
// Restart buffer.
|
|
|
|
HRESULT hr = D3D::context->Map(m_texel_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &sr);
|
|
|
|
CHECK(SUCCEEDED(hr), "Map texel buffer");
|
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_texel_buffer_offset = 0;
|
|
|
|
}
|
|
|
|
else
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
// Don't overwrite the earlier-used space.
|
|
|
|
HRESULT hr = D3D::context->Map(m_texel_buffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &sr);
|
|
|
|
CHECK(SUCCEEDED(hr), "Map texel buffer");
|
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2019-02-15 01:59:50 +00:00
|
|
|
|
|
|
|
return true;
|
2011-06-11 19:37:21 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
bool VertexManager::UploadTexelBuffer(const void* data, u32 data_size, TexelBufferFormat format,
|
|
|
|
u32* out_offset)
|
2011-06-11 19:37:21 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
if (data_size > TEXEL_STREAM_BUFFER_SIZE)
|
|
|
|
return false;
|
2014-10-25 17:04:33 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
const u32 elem_size = GetTexelBufferElementSize(format);
|
|
|
|
m_texel_buffer_offset = Common::AlignUp(m_texel_buffer_offset, elem_size);
|
2014-01-23 14:27:18 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
D3D11_MAPPED_SUBRESOURCE sr;
|
|
|
|
if (!MapTexelBuffer(data_size, sr))
|
|
|
|
return false;
|
2014-01-23 14:27:18 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
*out_offset = m_texel_buffer_offset / elem_size;
|
|
|
|
std::memcpy(static_cast<u8*>(sr.pData) + m_texel_buffer_offset, data, data_size);
|
|
|
|
ADDSTAT(stats.thisFrame.bytesUniformStreamed, data_size);
|
|
|
|
m_texel_buffer_offset += data_size;
|
2011-06-11 19:37:21 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
D3D::context->Unmap(m_texel_buffer, 0);
|
|
|
|
D3D::stateman->SetTexture(0, m_texel_buffer_views[static_cast<size_t>(format)]);
|
|
|
|
return true;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
bool VertexManager::UploadTexelBuffer(const void* data, u32 data_size, TexelBufferFormat format,
|
|
|
|
u32* out_offset, const void* palette_data, u32 palette_size,
|
|
|
|
TexelBufferFormat palette_format, u32* out_palette_offset)
|
2018-11-27 07:16:53 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
const u32 elem_size = GetTexelBufferElementSize(format);
|
|
|
|
const u32 palette_elem_size = GetTexelBufferElementSize(palette_format);
|
|
|
|
const u32 reserve_size = data_size + palette_size + palette_elem_size;
|
|
|
|
if (reserve_size > TEXEL_STREAM_BUFFER_SIZE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_texel_buffer_offset = Common::AlignUp(m_texel_buffer_offset, elem_size);
|
|
|
|
|
|
|
|
D3D11_MAPPED_SUBRESOURCE sr;
|
|
|
|
if (!MapTexelBuffer(reserve_size, sr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const u32 palette_byte_offset = Common::AlignUp(data_size, palette_elem_size);
|
|
|
|
std::memcpy(static_cast<u8*>(sr.pData) + m_texel_buffer_offset, data, data_size);
|
|
|
|
std::memcpy(static_cast<u8*>(sr.pData) + m_texel_buffer_offset + palette_byte_offset,
|
|
|
|
palette_data, palette_size);
|
|
|
|
ADDSTAT(stats.thisFrame.bytesUniformStreamed, palette_byte_offset + palette_size);
|
|
|
|
*out_offset = m_texel_buffer_offset / elem_size;
|
|
|
|
*out_palette_offset = (m_texel_buffer_offset + palette_byte_offset) / palette_elem_size;
|
|
|
|
m_texel_buffer_offset += palette_byte_offset + palette_size;
|
|
|
|
|
|
|
|
D3D::context->Unmap(m_texel_buffer, 0);
|
|
|
|
D3D::stateman->SetTexture(0, m_texel_buffer_views[static_cast<size_t>(format)]);
|
|
|
|
D3D::stateman->SetTexture(1, m_texel_buffer_views[static_cast<size_t>(palette_format)]);
|
|
|
|
return true;
|
2018-11-27 07:16:53 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
void VertexManager::ResetBuffer(u32 vertex_stride)
|
2018-11-27 07:16:53 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
m_base_buffer_pointer = m_cpu_vertex_buffer.data();
|
2018-11-27 07:16:53 +00:00
|
|
|
m_cur_buffer_pointer = m_base_buffer_pointer;
|
2019-02-15 01:59:50 +00:00
|
|
|
m_end_buffer_pointer = m_base_buffer_pointer + m_cpu_vertex_buffer.size();
|
|
|
|
IndexGenerator::Start(m_cpu_index_buffer.data());
|
2018-11-27 07:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-02-15 01:59:50 +00:00
|
|
|
if (cursor + totalBufferSize >= BUFFER_SIZE)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
// Wrap around
|
2019-02-15 01:59:50 +00:00
|
|
|
m_current_buffer = (m_current_buffer + 1) % 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)
|
2019-02-15 01:59:50 +00:00
|
|
|
std::memcpy(mappedData + cursor + vertexBufferSize, m_cpu_index_buffer.data(), indexBufferSize);
|
2018-11-27 07:16:53 +00:00
|
|
|
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
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
void VertexManager::UploadUniforms()
|
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
|
|
|
} // namespace DX11
|