dolphin/Source/Core/VideoBackends/D3D12/D3D12VertexManager.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

382 lines
16 KiB
C++
Raw Permalink Normal View History

2019-03-28 11:35:46 +01:00
// Copyright 2019 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2019-03-28 11:35:46 +01:00
#include "VideoBackends/D3D12/D3D12VertexManager.h"
2019-03-28 11:35:46 +01:00
#include "Common/Align.h"
#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
#include "Common/MsgHandler.h"
#include "Core/System.h"
2023-01-29 11:58:54 +01:00
#include "VideoBackends/D3D12/D3D12Gfx.h"
#include "VideoBackends/D3D12/D3D12StreamBuffer.h"
#include "VideoBackends/D3D12/DX12Context.h"
2019-03-28 11:35:46 +01:00
#include "VideoCommon/GeometryShaderManager.h"
#include "VideoCommon/IndexGenerator.h"
#include "VideoCommon/PixelShaderManager.h"
#include "VideoCommon/Statistics.h"
#include "VideoCommon/VertexLoaderManager.h"
#include "VideoCommon/VertexShaderManager.h"
#include "VideoCommon/VideoConfig.h"
namespace DX12
{
VertexManager::VertexManager() = default;
VertexManager::~VertexManager() = default;
bool VertexManager::Initialize()
{
if (!VertexManagerBase::Initialize())
return false;
2019-03-28 11:35:46 +01:00
if (!m_vertex_stream_buffer.AllocateBuffer(VERTEX_STREAM_BUFFER_SIZE) ||
!m_index_stream_buffer.AllocateBuffer(INDEX_STREAM_BUFFER_SIZE) ||
!m_uniform_stream_buffer.AllocateBuffer(UNIFORM_STREAM_BUFFER_SIZE) ||
!m_texel_stream_buffer.AllocateBuffer(TEXEL_STREAM_BUFFER_SIZE))
{
PanicAlertFmt("Failed to allocate streaming buffers");
2019-03-28 11:35:46 +01:00
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},
{TEXEL_BUFFER_FORMAT_RGBA8_UINT, DXGI_FORMAT_R8G8B8A8_UINT},
{TEXEL_BUFFER_FORMAT_R32G32_UINT, DXGI_FORMAT_R32G32_UINT},
}};
for (const auto& it : format_mapping)
{
DescriptorHandle& dh = m_texel_buffer_views[it.first];
if (!g_dx_context->GetDescriptorHeapManager().Allocate(&dh))
{
PanicAlertFmt("Failed to allocate descriptor for texel buffer");
2019-03-28 11:35:46 +01:00
return false;
}
D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {it.second, D3D12_SRV_DIMENSION_BUFFER,
D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING};
srv_desc.Buffer.NumElements =
m_texel_stream_buffer.GetSize() / GetTexelBufferElementSize(it.first);
g_dx_context->GetDevice()->CreateShaderResourceView(m_texel_stream_buffer.GetBuffer(),
&srv_desc, dh.cpu_handle);
}
if (!g_dx_context->GetDescriptorHeapManager().Allocate(&m_vertex_srv))
{
PanicAlertFmt("Failed to allocate descriptor for vertex srv");
return false;
}
D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {DXGI_FORMAT_R32_UINT, D3D12_SRV_DIMENSION_BUFFER,
D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING};
srv_desc.Buffer.NumElements = m_vertex_stream_buffer.GetSize() / sizeof(u32);
g_dx_context->GetDevice()->CreateShaderResourceView(m_vertex_stream_buffer.GetBuffer(), &srv_desc,
m_vertex_srv.cpu_handle);
2019-03-28 11:35:46 +01:00
UploadAllConstants();
return true;
}
void VertexManager::ResetBuffer(u32 vertex_stride)
{
// Attempt to allocate from buffers
bool has_vbuffer_allocation = m_vertex_stream_buffer.ReserveMemory(MAXVBUFFERSIZE, vertex_stride);
bool has_ibuffer_allocation =
m_index_stream_buffer.ReserveMemory(MAXIBUFFERSIZE * sizeof(u16), sizeof(u16));
if (!has_vbuffer_allocation || !has_ibuffer_allocation)
{
// Flush any pending commands first, so that we can wait on the fences
WARN_LOG_FMT(VIDEO, "Executing command list while waiting for space in vertex/index buffer");
2023-01-29 11:58:54 +01:00
Gfx::GetInstance()->ExecuteCommandList(false);
2019-03-28 11:35:46 +01:00
// Attempt to allocate again, this may cause a fence wait
if (!has_vbuffer_allocation)
has_vbuffer_allocation = m_vertex_stream_buffer.ReserveMemory(MAXVBUFFERSIZE, vertex_stride);
if (!has_ibuffer_allocation)
has_ibuffer_allocation =
m_index_stream_buffer.ReserveMemory(MAXIBUFFERSIZE * sizeof(u16), sizeof(u16));
// If we still failed, that means the allocation was too large and will never succeed, so panic
if (!has_vbuffer_allocation || !has_ibuffer_allocation)
PanicAlertFmt("Failed to allocate space in streaming buffers for pending draw");
2019-03-28 11:35:46 +01:00
}
// Update pointers
m_base_buffer_pointer = m_vertex_stream_buffer.GetHostPointer();
m_end_buffer_pointer = m_vertex_stream_buffer.GetCurrentHostPointer() + MAXVBUFFERSIZE;
m_cur_buffer_pointer = m_vertex_stream_buffer.GetCurrentHostPointer();
m_index_generator.Start(reinterpret_cast<u16*>(m_index_stream_buffer.GetCurrentHostPointer()));
2019-03-28 11:35:46 +01:00
}
void VertexManager::CommitBuffer(u32 num_vertices, u32 vertex_stride, u32 num_indices,
u32* out_base_vertex, u32* out_base_index)
{
const u32 vertex_data_size = num_vertices * vertex_stride;
const u32 index_data_size = num_indices * sizeof(u16);
*out_base_vertex =
vertex_stride > 0 ? (m_vertex_stream_buffer.GetCurrentOffset() / vertex_stride) : 0;
*out_base_index = m_index_stream_buffer.GetCurrentOffset() / sizeof(u16);
m_vertex_stream_buffer.CommitMemory(vertex_data_size);
m_index_stream_buffer.CommitMemory(index_data_size);
ADDSTAT(g_stats.this_frame.bytes_vertex_streamed, static_cast<int>(vertex_data_size));
ADDSTAT(g_stats.this_frame.bytes_index_streamed, static_cast<int>(index_data_size));
2019-03-28 11:35:46 +01:00
2023-01-29 11:58:54 +01:00
Gfx::GetInstance()->SetVertexBuffer(m_vertex_stream_buffer.GetGPUPointer(),
2023-01-31 05:29:16 +01:00
m_vertex_srv.cpu_handle, vertex_stride,
m_vertex_stream_buffer.GetSize());
2023-01-29 11:58:54 +01:00
Gfx::GetInstance()->SetIndexBuffer(m_index_stream_buffer.GetGPUPointer(),
2023-01-31 05:29:16 +01:00
m_index_stream_buffer.GetSize(), DXGI_FORMAT_R16_UINT);
2019-03-28 11:35:46 +01:00
}
void VertexManager::UploadUniforms()
{
UpdateVertexShaderConstants();
UpdateGeometryShaderConstants();
UpdatePixelShaderConstants();
}
void VertexManager::UpdateVertexShaderConstants()
{
auto& system = Core::System::GetInstance();
auto& vertex_shader_manager = system.GetVertexShaderManager();
if (!vertex_shader_manager.dirty || !ReserveConstantStorage())
2019-03-28 11:35:46 +01:00
return;
2023-01-29 11:58:54 +01:00
Gfx::GetInstance()->SetConstantBuffer(1, m_uniform_stream_buffer.GetCurrentGPUPointer());
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer(), &vertex_shader_manager.constants,
2019-03-28 11:35:46 +01:00
sizeof(VertexShaderConstants));
m_uniform_stream_buffer.CommitMemory(sizeof(VertexShaderConstants));
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, sizeof(VertexShaderConstants));
vertex_shader_manager.dirty = false;
2019-03-28 11:35:46 +01:00
}
void VertexManager::UpdateGeometryShaderConstants()
{
auto& system = Core::System::GetInstance();
auto& geometry_shader_manager = system.GetGeometryShaderManager();
if (!geometry_shader_manager.dirty || !ReserveConstantStorage())
2019-03-28 11:35:46 +01:00
return;
Gfx::GetInstance()->SetConstantBuffer(3, m_uniform_stream_buffer.GetCurrentGPUPointer());
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer(), &geometry_shader_manager.constants,
2019-03-28 11:35:46 +01:00
sizeof(GeometryShaderConstants));
m_uniform_stream_buffer.CommitMemory(sizeof(GeometryShaderConstants));
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, sizeof(GeometryShaderConstants));
geometry_shader_manager.dirty = false;
2019-03-28 11:35:46 +01:00
}
void VertexManager::UpdatePixelShaderConstants()
{
auto& system = Core::System::GetInstance();
auto& pixel_shader_manager = system.GetPixelShaderManager();
if (!ReserveConstantStorage())
2019-03-28 11:35:46 +01:00
return;
if (pixel_shader_manager.dirty)
{
Gfx::GetInstance()->SetConstantBuffer(0, m_uniform_stream_buffer.GetCurrentGPUPointer());
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer(), &pixel_shader_manager.constants,
sizeof(PixelShaderConstants));
m_uniform_stream_buffer.CommitMemory(sizeof(PixelShaderConstants));
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, sizeof(PixelShaderConstants));
pixel_shader_manager.dirty = false;
}
if (pixel_shader_manager.custom_constants_dirty)
{
Gfx::GetInstance()->SetConstantBuffer(2, m_uniform_stream_buffer.GetCurrentGPUPointer());
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer(),
pixel_shader_manager.custom_constants.data(),
pixel_shader_manager.custom_constants.size());
m_uniform_stream_buffer.CommitMemory(
static_cast<u32>(pixel_shader_manager.custom_constants.size()));
pixel_shader_manager.custom_constants_dirty = false;
}
2019-03-28 11:35:46 +01:00
}
bool VertexManager::ReserveConstantStorage()
{
auto& system = Core::System::GetInstance();
auto& pixel_shader_manager = system.GetPixelShaderManager();
2019-03-28 11:35:46 +01:00
static constexpr u32 reserve_size =
static_cast<u32>(std::max({sizeof(PixelShaderConstants), sizeof(VertexShaderConstants),
sizeof(GeometryShaderConstants)}));
const u32 custom_constants_size = static_cast<u32>(pixel_shader_manager.custom_constants.size());
if (m_uniform_stream_buffer.ReserveMemory(reserve_size + custom_constants_size,
2019-03-28 11:35:46 +01:00
D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT))
{
return true;
}
// The only places that call constant updates are safe to have state restored.
WARN_LOG_FMT(VIDEO, "Executing command list while waiting for space in uniform buffer");
2023-01-29 11:58:54 +01:00
Gfx::GetInstance()->ExecuteCommandList(false);
2019-03-28 11:35:46 +01:00
// Since we are on a new command buffer, all constants have been invalidated, and we need
// to reupload them. We may as well do this now, since we're issuing a draw anyway.
UploadAllConstants();
return false;
}
void VertexManager::UploadAllConstants()
{
auto& system = Core::System::GetInstance();
auto& pixel_shader_manager = system.GetPixelShaderManager();
2019-03-28 11:35:46 +01:00
// We are free to re-use parts of the buffer now since we're uploading all constants.
const u32 pixel_constants_offset = 0;
constexpr u32 vertex_constants_offset =
2019-03-28 11:35:46 +01:00
Common::AlignUp(pixel_constants_offset + sizeof(PixelShaderConstants),
D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT);
constexpr u32 geometry_constants_offset =
2019-03-28 11:35:46 +01:00
Common::AlignUp(vertex_constants_offset + sizeof(VertexShaderConstants),
D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT);
constexpr u32 custom_pixel_constants_offset =
Common::AlignUp(geometry_constants_offset + sizeof(GeometryShaderConstants),
D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT);
const u32 allocation_size = custom_pixel_constants_offset +
static_cast<u32>(pixel_shader_manager.custom_constants.size());
2019-03-28 11:35:46 +01:00
// Allocate everything at once.
// We should only be here if the buffer was full and a command buffer was submitted anyway.
if (!m_uniform_stream_buffer.ReserveMemory(allocation_size,
D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT))
{
PanicAlertFmt("Failed to allocate space for constants in streaming buffer");
2019-03-28 11:35:46 +01:00
return;
}
// Update bindings
2023-01-29 11:58:54 +01:00
Gfx::GetInstance()->SetConstantBuffer(0, m_uniform_stream_buffer.GetCurrentGPUPointer() +
2023-01-31 05:29:16 +01:00
pixel_constants_offset);
2023-01-29 11:58:54 +01:00
Gfx::GetInstance()->SetConstantBuffer(1, m_uniform_stream_buffer.GetCurrentGPUPointer() +
2023-01-31 05:29:16 +01:00
vertex_constants_offset);
2023-01-29 11:58:54 +01:00
Gfx::GetInstance()->SetConstantBuffer(2, m_uniform_stream_buffer.GetCurrentGPUPointer() +
custom_pixel_constants_offset);
Gfx::GetInstance()->SetConstantBuffer(3, m_uniform_stream_buffer.GetCurrentGPUPointer() +
2023-01-31 05:29:16 +01:00
geometry_constants_offset);
2019-03-28 11:35:46 +01:00
auto& vertex_shader_manager = system.GetVertexShaderManager();
auto& geometry_shader_manager = system.GetGeometryShaderManager();
2019-03-28 11:35:46 +01:00
// Copy the actual data in
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer() + pixel_constants_offset,
&pixel_shader_manager.constants, sizeof(PixelShaderConstants));
2019-03-28 11:35:46 +01:00
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer() + vertex_constants_offset,
&vertex_shader_manager.constants, sizeof(VertexShaderConstants));
2019-03-28 11:35:46 +01:00
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer() + geometry_constants_offset,
&geometry_shader_manager.constants, sizeof(GeometryShaderConstants));
if (!pixel_shader_manager.custom_constants.empty())
{
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer() + custom_pixel_constants_offset,
pixel_shader_manager.custom_constants.data(),
pixel_shader_manager.custom_constants.size());
}
2019-03-28 11:35:46 +01:00
// Finally, flush buffer memory after copying
m_uniform_stream_buffer.CommitMemory(allocation_size);
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, allocation_size);
2019-03-28 11:35:46 +01:00
// Clear dirty flags
vertex_shader_manager.dirty = false;
geometry_shader_manager.dirty = false;
pixel_shader_manager.dirty = false;
2019-03-28 11:35:46 +01:00
}
void VertexManager::UploadUtilityUniforms(const void* data, u32 data_size)
{
InvalidateConstants();
if (!m_uniform_stream_buffer.ReserveMemory(data_size,
D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT))
{
WARN_LOG_FMT(VIDEO, "Executing command buffer while waiting for ext space in uniform buffer");
2023-01-29 11:58:54 +01:00
Gfx::GetInstance()->ExecuteCommandList(false);
2019-03-28 11:35:46 +01:00
}
2023-01-29 11:58:54 +01:00
Gfx::GetInstance()->SetConstantBuffer(0, m_uniform_stream_buffer.GetCurrentGPUPointer());
Gfx::GetInstance()->SetConstantBuffer(1, m_uniform_stream_buffer.GetCurrentGPUPointer());
Gfx::GetInstance()->SetConstantBuffer(2, m_uniform_stream_buffer.GetCurrentGPUPointer());
2019-03-28 11:35:46 +01:00
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer(), data, data_size);
m_uniform_stream_buffer.CommitMemory(data_size);
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, data_size);
2019-03-28 11:35:46 +01:00
}
bool VertexManager::UploadTexelBuffer(const void* data, u32 data_size, TexelBufferFormat format,
u32* out_offset)
{
if (data_size > m_texel_stream_buffer.GetSize())
return false;
const u32 elem_size = GetTexelBufferElementSize(format);
if (!m_texel_stream_buffer.ReserveMemory(data_size, elem_size))
{
// Try submitting cmdbuffer.
WARN_LOG_FMT(VIDEO, "Submitting command buffer while waiting for space in texel buffer");
2023-01-29 11:58:54 +01:00
Gfx::GetInstance()->ExecuteCommandList(false);
2019-03-28 11:35:46 +01:00
if (!m_texel_stream_buffer.ReserveMemory(data_size, elem_size))
{
PanicAlertFmt("Failed to allocate {} bytes from texel buffer", data_size);
2019-03-28 11:35:46 +01:00
return false;
}
}
std::memcpy(m_texel_stream_buffer.GetCurrentHostPointer(), data, data_size);
*out_offset = static_cast<u32>(m_texel_stream_buffer.GetCurrentOffset()) / elem_size;
m_texel_stream_buffer.CommitMemory(data_size);
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, data_size);
2023-01-29 11:58:54 +01:00
Gfx::GetInstance()->SetTextureDescriptor(0, m_texel_buffer_views[format].cpu_handle);
2019-03-28 11:35:46 +01:00
return true;
}
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)
{
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 > m_texel_stream_buffer.GetSize())
return false;
if (!m_texel_stream_buffer.ReserveMemory(reserve_size, elem_size))
{
// Try submitting cmdbuffer.
WARN_LOG_FMT(VIDEO, "Submitting command buffer while waiting for space in texel buffer");
2023-01-29 11:58:54 +01:00
Gfx::GetInstance()->ExecuteCommandList(false);
2019-03-28 11:35:46 +01:00
if (!m_texel_stream_buffer.ReserveMemory(reserve_size, elem_size))
{
PanicAlertFmt("Failed to allocate {} bytes from texel buffer", reserve_size);
2019-03-28 11:35:46 +01:00
return false;
}
}
const u32 palette_byte_offset = Common::AlignUp(data_size, palette_elem_size);
std::memcpy(m_texel_stream_buffer.GetCurrentHostPointer(), data, data_size);
std::memcpy(m_texel_stream_buffer.GetCurrentHostPointer() + palette_byte_offset, palette_data,
palette_size);
*out_offset = static_cast<u32>(m_texel_stream_buffer.GetCurrentOffset()) / elem_size;
*out_palette_offset =
(static_cast<u32>(m_texel_stream_buffer.GetCurrentOffset()) + palette_byte_offset) /
palette_elem_size;
m_texel_stream_buffer.CommitMemory(palette_byte_offset + palette_size);
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, palette_byte_offset + palette_size);
2023-01-29 11:58:54 +01:00
Gfx::GetInstance()->SetTextureDescriptor(0, m_texel_buffer_views[format].cpu_handle);
Gfx::GetInstance()->SetTextureDescriptor(1, m_texel_buffer_views[palette_format].cpu_handle);
2019-03-28 11:35:46 +01:00
return true;
}
} // namespace DX12