2019-03-28 10:35:46 +00:00
|
|
|
// Copyright 2019 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-03-28 10:35:46 +00:00
|
|
|
|
2020-09-15 13:01:29 +00:00
|
|
|
#include "VideoBackends/D3D12/D3D12VertexManager.h"
|
2019-03-28 10:35:46 +00:00
|
|
|
|
|
|
|
#include "Common/Align.h"
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
|
|
|
|
2022-12-27 16:42:02 +00:00
|
|
|
#include "Core/System.h"
|
|
|
|
|
2023-01-29 10:58:54 +00:00
|
|
|
#include "VideoBackends/D3D12/D3D12Gfx.h"
|
2020-09-15 13:01:29 +00:00
|
|
|
#include "VideoBackends/D3D12/D3D12StreamBuffer.h"
|
|
|
|
#include "VideoBackends/D3D12/DX12Context.h"
|
2019-03-28 10:35:46 +00: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()
|
|
|
|
{
|
2019-12-09 09:16:04 +00:00
|
|
|
if (!VertexManagerBase::Initialize())
|
|
|
|
return false;
|
|
|
|
|
2019-03-28 10:35:46 +00: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))
|
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to allocate streaming buffers");
|
2019-03-28 10:35:46 +00: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))
|
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to allocate descriptor for texel buffer");
|
2019-03-28 10:35:46 +00: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);
|
|
|
|
}
|
|
|
|
|
2022-06-20 07:55:49 +00:00
|
|
|
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 10:35:46 +00: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
|
2020-11-09 07:59:48 +00:00
|
|
|
WARN_LOG_FMT(VIDEO, "Executing command list while waiting for space in vertex/index buffer");
|
2023-01-29 10:58:54 +00:00
|
|
|
Gfx::GetInstance()->ExecuteCommandList(false);
|
2019-03-28 10:35:46 +00: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)
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to allocate space in streaming buffers for pending draw");
|
2019-03-28 10:35:46 +00: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();
|
2019-12-05 15:01:33 +00:00
|
|
|
m_index_generator.Start(reinterpret_cast<u16*>(m_index_stream_buffer.GetCurrentHostPointer()));
|
2019-03-28 10:35:46 +00: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);
|
|
|
|
|
2019-07-11 03:34:50 +00:00
|
|
|
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 10:35:46 +00:00
|
|
|
|
2023-01-29 10:58:54 +00:00
|
|
|
Gfx::GetInstance()->SetVertexBuffer(m_vertex_stream_buffer.GetGPUPointer(),
|
2023-01-31 04:29:16 +00:00
|
|
|
m_vertex_srv.cpu_handle, vertex_stride,
|
|
|
|
m_vertex_stream_buffer.GetSize());
|
2023-01-29 10:58:54 +00:00
|
|
|
Gfx::GetInstance()->SetIndexBuffer(m_index_stream_buffer.GetGPUPointer(),
|
2023-01-31 04:29:16 +00:00
|
|
|
m_index_stream_buffer.GetSize(), DXGI_FORMAT_R16_UINT);
|
2019-03-28 10:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexManager::UploadUniforms()
|
|
|
|
{
|
|
|
|
UpdateVertexShaderConstants();
|
|
|
|
UpdateGeometryShaderConstants();
|
|
|
|
UpdatePixelShaderConstants();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexManager::UpdateVertexShaderConstants()
|
|
|
|
{
|
2022-12-28 14:38:46 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
auto& vertex_shader_manager = system.GetVertexShaderManager();
|
|
|
|
|
|
|
|
if (!vertex_shader_manager.dirty || !ReserveConstantStorage())
|
2019-03-28 10:35:46 +00:00
|
|
|
return;
|
|
|
|
|
2023-01-29 10:58:54 +00:00
|
|
|
Gfx::GetInstance()->SetConstantBuffer(1, m_uniform_stream_buffer.GetCurrentGPUPointer());
|
2022-12-28 14:38:46 +00:00
|
|
|
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer(), &vertex_shader_manager.constants,
|
2019-03-28 10:35:46 +00:00
|
|
|
sizeof(VertexShaderConstants));
|
|
|
|
m_uniform_stream_buffer.CommitMemory(sizeof(VertexShaderConstants));
|
2019-07-11 03:34:50 +00:00
|
|
|
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, sizeof(VertexShaderConstants));
|
2022-12-28 14:38:46 +00:00
|
|
|
vertex_shader_manager.dirty = false;
|
2019-03-28 10:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexManager::UpdateGeometryShaderConstants()
|
|
|
|
{
|
2022-12-29 14:27:48 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
auto& geometry_shader_manager = system.GetGeometryShaderManager();
|
|
|
|
|
|
|
|
if (!geometry_shader_manager.dirty || !ReserveConstantStorage())
|
2019-03-28 10:35:46 +00:00
|
|
|
return;
|
|
|
|
|
2023-09-20 00:29:38 +00:00
|
|
|
Gfx::GetInstance()->SetConstantBuffer(3, m_uniform_stream_buffer.GetCurrentGPUPointer());
|
2022-12-29 14:27:48 +00:00
|
|
|
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer(), &geometry_shader_manager.constants,
|
2019-03-28 10:35:46 +00:00
|
|
|
sizeof(GeometryShaderConstants));
|
|
|
|
m_uniform_stream_buffer.CommitMemory(sizeof(GeometryShaderConstants));
|
2019-07-11 03:34:50 +00:00
|
|
|
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, sizeof(GeometryShaderConstants));
|
2022-12-29 14:27:48 +00:00
|
|
|
geometry_shader_manager.dirty = false;
|
2019-03-28 10:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexManager::UpdatePixelShaderConstants()
|
|
|
|
{
|
2022-12-27 16:42:02 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
auto& pixel_shader_manager = system.GetPixelShaderManager();
|
|
|
|
|
2023-09-20 00:29:38 +00:00
|
|
|
if (!ReserveConstantStorage())
|
2019-03-28 10:35:46 +00:00
|
|
|
return;
|
|
|
|
|
2023-09-20 00:29:38 +00:00
|
|
|
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 10:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VertexManager::ReserveConstantStorage()
|
|
|
|
{
|
2023-09-20 00:29:38 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
auto& pixel_shader_manager = system.GetPixelShaderManager();
|
|
|
|
|
2019-03-28 10:35:46 +00:00
|
|
|
static constexpr u32 reserve_size =
|
|
|
|
static_cast<u32>(std::max({sizeof(PixelShaderConstants), sizeof(VertexShaderConstants),
|
|
|
|
sizeof(GeometryShaderConstants)}));
|
2023-09-20 00:29:38 +00:00
|
|
|
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 10:35:46 +00:00
|
|
|
D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The only places that call constant updates are safe to have state restored.
|
2020-11-09 07:59:48 +00:00
|
|
|
WARN_LOG_FMT(VIDEO, "Executing command list while waiting for space in uniform buffer");
|
2023-01-29 10:58:54 +00:00
|
|
|
Gfx::GetInstance()->ExecuteCommandList(false);
|
2019-03-28 10:35:46 +00: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()
|
|
|
|
{
|
2023-09-20 00:29:38 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
auto& pixel_shader_manager = system.GetPixelShaderManager();
|
|
|
|
|
2019-03-28 10:35:46 +00:00
|
|
|
// We are free to re-use parts of the buffer now since we're uploading all constants.
|
|
|
|
const u32 pixel_constants_offset = 0;
|
2020-08-18 00:30:49 +00:00
|
|
|
constexpr u32 vertex_constants_offset =
|
2019-03-28 10:35:46 +00:00
|
|
|
Common::AlignUp(pixel_constants_offset + sizeof(PixelShaderConstants),
|
|
|
|
D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT);
|
2020-08-18 00:30:49 +00:00
|
|
|
constexpr u32 geometry_constants_offset =
|
2019-03-28 10:35:46 +00:00
|
|
|
Common::AlignUp(vertex_constants_offset + sizeof(VertexShaderConstants),
|
|
|
|
D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT);
|
2023-09-20 00:29:38 +00:00
|
|
|
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 10:35:46 +00: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))
|
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to allocate space for constants in streaming buffer");
|
2019-03-28 10:35:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update bindings
|
2023-01-29 10:58:54 +00:00
|
|
|
Gfx::GetInstance()->SetConstantBuffer(0, m_uniform_stream_buffer.GetCurrentGPUPointer() +
|
2023-01-31 04:29:16 +00:00
|
|
|
pixel_constants_offset);
|
2023-01-29 10:58:54 +00:00
|
|
|
Gfx::GetInstance()->SetConstantBuffer(1, m_uniform_stream_buffer.GetCurrentGPUPointer() +
|
2023-01-31 04:29:16 +00:00
|
|
|
vertex_constants_offset);
|
2023-01-29 10:58:54 +00:00
|
|
|
Gfx::GetInstance()->SetConstantBuffer(2, m_uniform_stream_buffer.GetCurrentGPUPointer() +
|
2023-09-20 00:29:38 +00:00
|
|
|
custom_pixel_constants_offset);
|
|
|
|
Gfx::GetInstance()->SetConstantBuffer(3, m_uniform_stream_buffer.GetCurrentGPUPointer() +
|
2023-01-31 04:29:16 +00:00
|
|
|
geometry_constants_offset);
|
2019-03-28 10:35:46 +00:00
|
|
|
|
2022-12-28 14:38:46 +00:00
|
|
|
auto& vertex_shader_manager = system.GetVertexShaderManager();
|
2022-12-29 14:27:48 +00:00
|
|
|
auto& geometry_shader_manager = system.GetGeometryShaderManager();
|
2022-12-27 16:42:02 +00:00
|
|
|
|
2019-03-28 10:35:46 +00:00
|
|
|
// Copy the actual data in
|
|
|
|
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer() + pixel_constants_offset,
|
2022-12-27 16:42:02 +00:00
|
|
|
&pixel_shader_manager.constants, sizeof(PixelShaderConstants));
|
2019-03-28 10:35:46 +00:00
|
|
|
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer() + vertex_constants_offset,
|
2022-12-28 14:38:46 +00:00
|
|
|
&vertex_shader_manager.constants, sizeof(VertexShaderConstants));
|
2019-03-28 10:35:46 +00:00
|
|
|
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer() + geometry_constants_offset,
|
2022-12-29 14:27:48 +00:00
|
|
|
&geometry_shader_manager.constants, sizeof(GeometryShaderConstants));
|
2023-09-20 00:29:38 +00:00
|
|
|
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 10:35:46 +00:00
|
|
|
|
|
|
|
// Finally, flush buffer memory after copying
|
|
|
|
m_uniform_stream_buffer.CommitMemory(allocation_size);
|
2019-07-11 03:34:50 +00:00
|
|
|
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, allocation_size);
|
2019-03-28 10:35:46 +00:00
|
|
|
|
|
|
|
// Clear dirty flags
|
2022-12-28 14:38:46 +00:00
|
|
|
vertex_shader_manager.dirty = false;
|
2022-12-29 14:27:48 +00:00
|
|
|
geometry_shader_manager.dirty = false;
|
2022-12-27 16:42:02 +00:00
|
|
|
pixel_shader_manager.dirty = false;
|
2019-03-28 10:35:46 +00: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))
|
|
|
|
{
|
2020-11-09 07:59:48 +00:00
|
|
|
WARN_LOG_FMT(VIDEO, "Executing command buffer while waiting for ext space in uniform buffer");
|
2023-01-29 10:58:54 +00:00
|
|
|
Gfx::GetInstance()->ExecuteCommandList(false);
|
2019-03-28 10:35:46 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 10:58:54 +00: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 10:35:46 +00:00
|
|
|
std::memcpy(m_uniform_stream_buffer.GetCurrentHostPointer(), data, data_size);
|
|
|
|
m_uniform_stream_buffer.CommitMemory(data_size);
|
2019-07-11 03:34:50 +00:00
|
|
|
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, data_size);
|
2019-03-28 10:35:46 +00: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.
|
2020-11-09 07:59:48 +00:00
|
|
|
WARN_LOG_FMT(VIDEO, "Submitting command buffer while waiting for space in texel buffer");
|
2023-01-29 10:58:54 +00:00
|
|
|
Gfx::GetInstance()->ExecuteCommandList(false);
|
2019-03-28 10:35:46 +00:00
|
|
|
if (!m_texel_stream_buffer.ReserveMemory(data_size, elem_size))
|
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to allocate {} bytes from texel buffer", data_size);
|
2019-03-28 10:35:46 +00: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);
|
2019-07-11 03:34:50 +00:00
|
|
|
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, data_size);
|
2023-01-29 10:58:54 +00:00
|
|
|
Gfx::GetInstance()->SetTextureDescriptor(0, m_texel_buffer_views[format].cpu_handle);
|
2019-03-28 10:35:46 +00: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.
|
2020-11-09 07:59:48 +00:00
|
|
|
WARN_LOG_FMT(VIDEO, "Submitting command buffer while waiting for space in texel buffer");
|
2023-01-29 10:58:54 +00:00
|
|
|
Gfx::GetInstance()->ExecuteCommandList(false);
|
2019-03-28 10:35:46 +00:00
|
|
|
if (!m_texel_stream_buffer.ReserveMemory(reserve_size, elem_size))
|
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to allocate {} bytes from texel buffer", reserve_size);
|
2019-03-28 10:35:46 +00: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);
|
2019-07-11 03:34:50 +00:00
|
|
|
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, palette_byte_offset + palette_size);
|
2023-01-29 10:58:54 +00: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 10:35:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace DX12
|