diff --git a/Source/Core/VideoBackends/D3D12/D3DUtil.cpp b/Source/Core/VideoBackends/D3D12/D3DUtil.cpp index 606c4a466f..74aafe20f2 100644 --- a/Source/Core/VideoBackends/D3D12/D3DUtil.cpp +++ b/Source/Core/VideoBackends/D3D12/D3DUtil.cpp @@ -359,24 +359,7 @@ int CD3DFont::Init() const unsigned int text_vb_size = s_max_num_vertices * sizeof(FONT2DVERTEX); - CheckHR( - device12->CreateCommittedResource( - &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD), - D3D12_HEAP_FLAG_NONE, - &CD3DX12_RESOURCE_DESC::Buffer(text_vb_size), - D3D12_RESOURCE_STATE_GENERIC_READ, - nullptr, - IID_PPV_ARGS(&m_vb12) - ) - ); - - SetDebugObjectName12(m_vb12, "vertex buffer of a CD3DFont object"); - - m_vb12_view.BufferLocation = m_vb12->GetGPUVirtualAddress(); - m_vb12_view.SizeInBytes = text_vb_size; - m_vb12_view.StrideInBytes = sizeof(FONT2DVERTEX); - - CheckHR(m_vb12->Map(0, nullptr, &m_vb12_data)); + m_vertex_buffer = std::make_unique(text_vb_size * 2, text_vb_size * 16, nullptr); D3D12_GRAPHICS_PIPELINE_STATE_DESC text_pso_desc = { default_root_signature, // ID3D12RootSignature *pRootSignature; @@ -409,7 +392,7 @@ int CD3DFont::Init() int CD3DFont::Shutdown() { - D3D::command_list_mgr->DestroyResourceAfterCurrentCommandListExecuted(m_vb12); + m_vertex_buffer.reset(); D3D::command_list_mgr->DestroyResourceAfterCurrentCommandListExecuted(m_texture12); return S_OK; @@ -417,7 +400,7 @@ int CD3DFont::Shutdown() int CD3DFont::DrawTextScaled(float x, float y, float size, float spacing, u32 dwColor, const std::string& text) { - if (!m_vb12) + if (!m_vertex_buffer) return 0; float scale_x = 1 / static_cast(D3D::GetBackBufferWidth()) * 2.f; @@ -428,10 +411,6 @@ int CD3DFont::DrawTextScaled(float x, float y, float size, float spacing, u32 dw float sx = x * scale_x - 1.f; float sy = 1.f - y * scale_y; - // Fill vertex buffer - FONT2DVERTEX* vertices12 = static_cast(m_vb12_data) + m_vb12_offset / sizeof(FONT2DVERTEX); - int num_triangles = 0L; - // set general pipeline state D3D::current_command_list->SetPipelineState(m_pso); D3D::command_list_mgr->SetCommandListDirtyState(COMMAND_LIST_STATE_PSO, true); @@ -441,13 +420,11 @@ int CD3DFont::DrawTextScaled(float x, float y, float size, float spacing, u32 dw D3D::current_command_list->SetGraphicsRootDescriptorTable(DESCRIPTOR_TABLE_PS_SRV, m_texture12_gpu); - // If we are close to running off edge of vertex buffer, jump back to beginning. - if (m_vb12_offset + text.length() * 6 * sizeof(FONT2DVERTEX) >= s_max_num_vertices * sizeof(FONT2DVERTEX)) - { - m_vb12_offset = 0; - vertices12 = static_cast(m_vb12_data); - } + // upper bound is nchars * 6, assuming no spaces + m_vertex_buffer->AllocateSpaceInBuffer(static_cast(text.length()) * 6 * sizeof(FONT2DVERTEX), sizeof(FONT2DVERTEX)); + FONT2DVERTEX* vertices12 = reinterpret_cast(m_vertex_buffer->GetCPUAddressOfCurrentAllocation()); + int num_triangles = 0; float start_x = sx; for (char c : text) { @@ -487,13 +464,14 @@ int CD3DFont::DrawTextScaled(float x, float y, float size, float spacing, u32 dw // Render the vertex buffer if (num_triangles > 0) { - D3D::current_command_list->IASetVertexBuffers(0, 1, &m_vb12_view); + u32 written_size = num_triangles * 3 * sizeof(FONT2DVERTEX); + m_vertex_buffer->OverrideSizeOfPreviousAllocation(written_size); - D3D::current_command_list->DrawInstanced(3 * num_triangles, 1, m_vb12_offset / sizeof(FONT2DVERTEX), 0); + D3D12_VERTEX_BUFFER_VIEW vb_view = { m_vertex_buffer->GetGPUAddressOfCurrentAllocation(), written_size, sizeof(FONT2DVERTEX) }; + D3D::current_command_list->IASetVertexBuffers(0, 1, &vb_view); + D3D::current_command_list->DrawInstanced(3 * num_triangles, 1, 0, 0); } - m_vb12_offset += 3 * num_triangles * sizeof(FONT2DVERTEX); - return S_OK; } diff --git a/Source/Core/VideoBackends/D3D12/D3DUtil.h b/Source/Core/VideoBackends/D3D12/D3DUtil.h index c40784425c..1e72979b33 100644 --- a/Source/Core/VideoBackends/D3D12/D3DUtil.h +++ b/Source/Core/VideoBackends/D3D12/D3DUtil.h @@ -5,10 +5,12 @@ #pragma once #include +#include #include #include "Common/MathUtil.h" #include "VideoBackends/D3D12/D3DState.h" +#include "VideoBackends/D3D12/D3DStreamBuffer.h" #include "VideoCommon/RenderBase.h" @@ -48,10 +50,7 @@ private: D3D12_CPU_DESCRIPTOR_HANDLE m_texture12_cpu = {}; D3D12_GPU_DESCRIPTOR_HANDLE m_texture12_gpu = {}; - ID3D12Resource* m_vb12 = nullptr; - D3D12_VERTEX_BUFFER_VIEW m_vb12_view = {}; - void* m_vb12_data = nullptr; - unsigned int m_vb12_offset = 0; + std::unique_ptr m_vertex_buffer; D3D12_INPUT_LAYOUT_DESC m_input_layout12 = {}; D3D12_SHADER_BYTECODE m_pshader12 = {};