Merge pull request #3645 from lioncash/dxmem

D3D12: Get rid of most explicit delete and new usages
This commit is contained in:
Mathew Maidment 2016-02-16 22:54:04 -05:00
commit 05e431d5b5
11 changed files with 117 additions and 113 deletions

View File

@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm> #include <algorithm>
#include <memory>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/MsgHandler.h" #include "Common/MsgHandler.h"
@ -46,7 +47,7 @@ namespace D3D
ID3D12Device* device12 = nullptr; ID3D12Device* device12 = nullptr;
ID3D12CommandQueue* command_queue = nullptr; ID3D12CommandQueue* command_queue = nullptr;
D3DCommandListManager* command_list_mgr = nullptr; std::unique_ptr<D3DCommandListManager> command_list_mgr;
ID3D12GraphicsCommandList* current_command_list = nullptr; ID3D12GraphicsCommandList* current_command_list = nullptr;
ID3D12RootSignature* default_root_signature = nullptr; ID3D12RootSignature* default_root_signature = nullptr;
@ -55,10 +56,10 @@ D3D12_CPU_DESCRIPTOR_HANDLE null_srv_cpu_shadow = {};
unsigned int resource_descriptor_size = 0; unsigned int resource_descriptor_size = 0;
unsigned int sampler_descriptor_size = 0; unsigned int sampler_descriptor_size = 0;
D3DDescriptorHeapManager* gpu_descriptor_heap_mgr = nullptr; std::unique_ptr<D3DDescriptorHeapManager> gpu_descriptor_heap_mgr;
D3DDescriptorHeapManager* sampler_descriptor_heap_mgr = nullptr; std::unique_ptr<D3DDescriptorHeapManager> sampler_descriptor_heap_mgr;
D3DDescriptorHeapManager* dsv_descriptor_heap_mgr = nullptr; std::unique_ptr<D3DDescriptorHeapManager> dsv_descriptor_heap_mgr;
D3DDescriptorHeapManager* rtv_descriptor_heap_mgr = nullptr; std::unique_ptr<D3DDescriptorHeapManager> rtv_descriptor_heap_mgr;
std::array<ID3D12DescriptorHeap*, 2> gpu_descriptor_heaps; std::array<ID3D12DescriptorHeap*, 2> gpu_descriptor_heaps;
HWND hWnd; HWND hWnd;
@ -558,7 +559,7 @@ HRESULT Create(HWND wnd)
CreateDescriptorHeaps(); CreateDescriptorHeaps();
CreateRootSignatures(); CreateRootSignatures();
command_list_mgr = new D3DCommandListManager( command_list_mgr = std::make_unique<D3DCommandListManager>(
D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_LIST_TYPE_DIRECT,
device12, device12,
command_queue command_queue
@ -605,7 +606,7 @@ void CreateDescriptorHeaps()
gpu_descriptor_heap_desc.NumDescriptors = 500000; gpu_descriptor_heap_desc.NumDescriptors = 500000;
gpu_descriptor_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; gpu_descriptor_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
gpu_descriptor_heap_mgr = new D3DDescriptorHeapManager(&gpu_descriptor_heap_desc, device12, 50000); gpu_descriptor_heap_mgr = std::make_unique<D3DDescriptorHeapManager>(&gpu_descriptor_heap_desc, device12, 50000);
gpu_descriptor_heaps[0] = gpu_descriptor_heap_mgr->GetDescriptorHeap(); gpu_descriptor_heaps[0] = gpu_descriptor_heap_mgr->GetDescriptorHeap();
@ -641,7 +642,7 @@ void CreateDescriptorHeaps()
sampler_descriptor_heap_desc.NumDescriptors = 2000; sampler_descriptor_heap_desc.NumDescriptors = 2000;
sampler_descriptor_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER; sampler_descriptor_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER;
sampler_descriptor_heap_mgr = new D3DDescriptorHeapManager(&sampler_descriptor_heap_desc, device12); sampler_descriptor_heap_mgr = std::make_unique<D3DDescriptorHeapManager>(&sampler_descriptor_heap_desc, device12);
gpu_descriptor_heaps[1] = sampler_descriptor_heap_mgr->GetDescriptorHeap(); gpu_descriptor_heaps[1] = sampler_descriptor_heap_mgr->GetDescriptorHeap();
} }
@ -652,7 +653,7 @@ void CreateDescriptorHeaps()
dsv_descriptor_heap_desc.NumDescriptors = 2000; dsv_descriptor_heap_desc.NumDescriptors = 2000;
dsv_descriptor_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV; dsv_descriptor_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
dsv_descriptor_heap_mgr = new D3DDescriptorHeapManager(&dsv_descriptor_heap_desc, device12); dsv_descriptor_heap_mgr = std::make_unique<D3DDescriptorHeapManager>(&dsv_descriptor_heap_desc, device12);
} }
{ {
@ -662,7 +663,7 @@ void CreateDescriptorHeaps()
rtv_descriptor_heap_desc.NumDescriptors = 1000000; rtv_descriptor_heap_desc.NumDescriptors = 1000000;
rtv_descriptor_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; rtv_descriptor_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
rtv_descriptor_heap_mgr = new D3DDescriptorHeapManager(&rtv_descriptor_heap_desc, device12); rtv_descriptor_heap_mgr = std::make_unique<D3DDescriptorHeapManager>(&rtv_descriptor_heap_desc, device12);
} }
} }
@ -757,15 +758,15 @@ void Close()
SAFE_RELEASE(s_swap_chain); SAFE_RELEASE(s_swap_chain);
SAFE_DELETE(command_list_mgr); command_list_mgr.reset();
command_queue->Release(); command_queue->Release();
default_root_signature->Release(); default_root_signature->Release();
SAFE_DELETE(gpu_descriptor_heap_mgr); gpu_descriptor_heap_mgr.reset();
SAFE_DELETE(sampler_descriptor_heap_mgr); sampler_descriptor_heap_mgr.reset();
SAFE_DELETE(rtv_descriptor_heap_mgr); rtv_descriptor_heap_mgr.reset();
SAFE_DELETE(dsv_descriptor_heap_mgr); dsv_descriptor_heap_mgr.reset();
ULONG remaining_references = device12->Release(); ULONG remaining_references = device12->Release();
if ((!s_debug_device12 && remaining_references) || (s_debug_device12 && remaining_references > 1)) if ((!s_debug_device12 && remaining_references) || (s_debug_device12 && remaining_references > 1))

View File

@ -13,6 +13,7 @@
#include <d3d12.h> #include <d3d12.h>
#include <d3dcompiler.h> #include <d3dcompiler.h>
#include <dxgi1_4.h> #include <dxgi1_4.h>
#include <memory>
#include <vector> #include <vector>
#include "../../Externals/d3dx12/d3dx12.h" #include "../../Externals/d3dx12/d3dx12.h"
@ -25,8 +26,6 @@ namespace DX12
{ {
#define SAFE_RELEASE(x) { if (x) (x)->Release(); (x) = nullptr; } #define SAFE_RELEASE(x) { if (x) (x)->Release(); (x) = nullptr; }
#define SAFE_DELETE(x) { delete (x); (x) = nullptr; }
#define SAFE_DELETE_ARRAY(x) { delete[] (x); (x) = nullptr; }
#define CHECK(cond, Message, ...) if (!(cond)) { __debugbreak(); PanicAlert(__FUNCTION__ " failed in %s at line %d: " Message, __FILE__, __LINE__, __VA_ARGS__); } #define CHECK(cond, Message, ...) if (!(cond)) { __debugbreak(); PanicAlert(__FUNCTION__ " failed in %s at line %d: " Message, __FILE__, __LINE__, __VA_ARGS__); }
// DEBUGCHECK is for high-frequency functions that we only want to check on debug builds. // DEBUGCHECK is for high-frequency functions that we only want to check on debug builds.
@ -80,17 +79,17 @@ extern ID3D12Device* device12;
extern unsigned int resource_descriptor_size; extern unsigned int resource_descriptor_size;
extern unsigned int sampler_descriptor_size; extern unsigned int sampler_descriptor_size;
extern D3DDescriptorHeapManager* gpu_descriptor_heap_mgr; extern std::unique_ptr<D3DDescriptorHeapManager> gpu_descriptor_heap_mgr;
extern D3DDescriptorHeapManager* sampler_descriptor_heap_mgr; extern std::unique_ptr<D3DDescriptorHeapManager> sampler_descriptor_heap_mgr;
extern D3DDescriptorHeapManager* dsv_descriptor_heap_mgr; extern std::unique_ptr<D3DDescriptorHeapManager> dsv_descriptor_heap_mgr;
extern D3DDescriptorHeapManager* rtv_descriptor_heap_mgr; extern std::unique_ptr<D3DDescriptorHeapManager> rtv_descriptor_heap_mgr;
extern std::array<ID3D12DescriptorHeap*, 2> gpu_descriptor_heaps; extern std::array<ID3D12DescriptorHeap*, 2> gpu_descriptor_heaps;
extern D3D12_CPU_DESCRIPTOR_HANDLE null_srv_cpu; extern D3D12_CPU_DESCRIPTOR_HANDLE null_srv_cpu;
extern D3D12_CPU_DESCRIPTOR_HANDLE null_srv_cpu_shadow; extern D3D12_CPU_DESCRIPTOR_HANDLE null_srv_cpu_shadow;
extern D3DCommandListManager* command_list_mgr; extern std::unique_ptr<D3DCommandListManager> command_list_mgr;
extern ID3D12GraphicsCommandList* current_command_list; extern ID3D12GraphicsCommandList* current_command_list;
extern ID3D12RootSignature* default_root_signature; extern ID3D12RootSignature* default_root_signature;

View File

@ -12,7 +12,7 @@
namespace DX12 namespace DX12
{ {
D3DStreamBuffer::D3DStreamBuffer(unsigned int initial_size, unsigned int max_size, bool* buffer_reallocation_notification) : D3DStreamBuffer::D3DStreamBuffer(size_t initial_size, size_t max_size, bool* buffer_reallocation_notification) :
m_buffer_size(initial_size), m_buffer_size(initial_size),
m_buffer_max_size(max_size), m_buffer_max_size(max_size),
m_buffer_reallocation_notification(buffer_reallocation_notification) m_buffer_reallocation_notification(buffer_reallocation_notification)
@ -39,13 +39,13 @@ D3DStreamBuffer::~D3DStreamBuffer()
// Obviously this is non-performant, so the buffer max_size should be large enough to // Obviously this is non-performant, so the buffer max_size should be large enough to
// ensure this never happens. // ensure this never happens.
bool D3DStreamBuffer::AllocateSpaceInBuffer(unsigned int allocation_size, unsigned int alignment) bool D3DStreamBuffer::AllocateSpaceInBuffer(size_t allocation_size, size_t alignment)
{ {
CHECK(allocation_size <= m_buffer_max_size, "Error: Requested allocation size in D3DStreamBuffer is greater than max allowed size of backing buffer."); CHECK(allocation_size <= m_buffer_max_size, "Error: Requested allocation size in D3DStreamBuffer is greater than max allowed size of backing buffer.");
if (alignment) if (alignment)
{ {
unsigned int padding = m_buffer_offset % alignment; size_t padding = m_buffer_offset % alignment;
// Check for case when adding alignment causes CPU offset to equal GPU offset, // Check for case when adding alignment causes CPU offset to equal GPU offset,
// which would imply entire buffer is available (if not corrected). // which would imply entire buffer is available (if not corrected).
@ -84,12 +84,12 @@ bool D3DStreamBuffer::AllocateSpaceInBuffer(unsigned int allocation_size, unsign
// we call AllocateSpaceInBuffer. We have to conservatively allocate 16MB (!). // we call AllocateSpaceInBuffer. We have to conservatively allocate 16MB (!).
// After the vertex data is written, we can choose to specify the 'real' allocation // After the vertex data is written, we can choose to specify the 'real' allocation
// size to avoid wasting space. // size to avoid wasting space.
void D3DStreamBuffer::OverrideSizeOfPreviousAllocation(unsigned int override_allocation_size) void D3DStreamBuffer::OverrideSizeOfPreviousAllocation(size_t override_allocation_size)
{ {
m_buffer_offset = m_buffer_current_allocation_offset + override_allocation_size; m_buffer_offset = m_buffer_current_allocation_offset + override_allocation_size;
} }
void D3DStreamBuffer::AllocateBuffer(unsigned int size) void D3DStreamBuffer::AllocateBuffer(size_t size)
{ {
// First, put existing buffer (if it exists) in deferred destruction list. // First, put existing buffer (if it exists) in deferred destruction list.
if (m_buffer) if (m_buffer)
@ -120,7 +120,7 @@ void D3DStreamBuffer::AllocateBuffer(unsigned int size)
// Function returns true if current command list executed as a result of current command list // Function returns true if current command list executed as a result of current command list
// referencing all of buffer's contents, AND we are already at max_size. No alternative but to // referencing all of buffer's contents, AND we are already at max_size. No alternative but to
// flush. See comments above AllocateSpaceInBuffer for more details. // flush. See comments above AllocateSpaceInBuffer for more details.
bool D3DStreamBuffer::AttemptBufferResizeOrElseStall(unsigned int allocation_size) bool D3DStreamBuffer::AttemptBufferResizeOrElseStall(size_t allocation_size)
{ {
// This function will attempt to increase the size of the buffer, in response // This function will attempt to increase the size of the buffer, in response
// to running out of room. If the buffer is already at its maximum size specified // to running out of room. If the buffer is already at its maximum size specified
@ -148,7 +148,7 @@ bool D3DStreamBuffer::AttemptBufferResizeOrElseStall(unsigned int allocation_siz
} }
// 2) Next, prefer increasing buffer size instead of stalling. // 2) Next, prefer increasing buffer size instead of stalling.
unsigned int new_size = std::min(static_cast<unsigned int>(m_buffer_size * 1.5f), m_buffer_max_size); size_t new_size = std::min(static_cast<size_t>(m_buffer_size * 1.5f), m_buffer_max_size);
new_size = std::max(new_size, allocation_size); new_size = std::max(new_size, allocation_size);
// Can we grow buffer further? // Can we grow buffer further?
@ -193,7 +193,7 @@ bool D3DStreamBuffer::AttemptBufferResizeOrElseStall(unsigned int allocation_siz
} }
// Return true if space is found. // Return true if space is found.
bool D3DStreamBuffer::AttemptToAllocateOutOfExistingUnusedSpaceInBuffer(unsigned int allocation_size) bool D3DStreamBuffer::AttemptToAllocateOutOfExistingUnusedSpaceInBuffer(size_t allocation_size)
{ {
// First, check if there is room at end of buffer. Fast path. // First, check if there is room at end of buffer. Fast path.
if (m_buffer_offset >= m_buffer_gpu_completion_offset) if (m_buffer_offset >= m_buffer_gpu_completion_offset)
@ -225,12 +225,11 @@ bool D3DStreamBuffer::AttemptToAllocateOutOfExistingUnusedSpaceInBuffer(unsigned
} }
// Returns true if fence was found and waited on. // Returns true if fence was found and waited on.
bool D3DStreamBuffer::AttemptToFindExistingFenceToStallOn(unsigned int allocation_size) bool D3DStreamBuffer::AttemptToFindExistingFenceToStallOn(size_t allocation_size)
{ {
// Let's find the first fence that will free up enough space in our buffer. // Let's find the first fence that will free up enough space in our buffer.
UINT64 fence_value_required = 0; UINT64 fence_value_required = 0;
unsigned int new_buffer_offset = 0;
while (m_queued_fences.size() > 0) while (m_queued_fences.size() > 0)
{ {
@ -335,12 +334,12 @@ void* D3DStreamBuffer::GetCPUAddressOfCurrentAllocation() const
return static_cast<u8*>(m_buffer_cpu_address) + m_buffer_current_allocation_offset; return static_cast<u8*>(m_buffer_cpu_address) + m_buffer_current_allocation_offset;
} }
unsigned int D3DStreamBuffer::GetOffsetOfCurrentAllocation() const size_t D3DStreamBuffer::GetOffsetOfCurrentAllocation() const
{ {
return m_buffer_current_allocation_offset; return m_buffer_current_allocation_offset;
} }
unsigned int D3DStreamBuffer::GetSize() const size_t D3DStreamBuffer::GetSize() const
{ {
return m_buffer_size; return m_buffer_size;
} }

View File

@ -14,29 +14,29 @@ namespace DX12
class D3DStreamBuffer class D3DStreamBuffer
{ {
public: public:
D3DStreamBuffer(unsigned int initial_size, unsigned int max_size, bool* buffer_reallocation_notification); D3DStreamBuffer(size_t initial_size, size_t max_size, bool* buffer_reallocation_notification);
~D3DStreamBuffer(); ~D3DStreamBuffer();
bool AllocateSpaceInBuffer(unsigned int allocation_size, unsigned int alignment); bool AllocateSpaceInBuffer(size_t allocation_size, size_t alignment);
void OverrideSizeOfPreviousAllocation(unsigned int override_allocation_size); void OverrideSizeOfPreviousAllocation(size_t override_allocation_size);
void* GetBaseCPUAddress() const; void* GetBaseCPUAddress() const;
D3D12_GPU_VIRTUAL_ADDRESS GetBaseGPUAddress() const; D3D12_GPU_VIRTUAL_ADDRESS GetBaseGPUAddress() const;
ID3D12Resource* GetBuffer() const; ID3D12Resource* GetBuffer() const;
void* GetCPUAddressOfCurrentAllocation() const; void* GetCPUAddressOfCurrentAllocation() const;
D3D12_GPU_VIRTUAL_ADDRESS GetGPUAddressOfCurrentAllocation() const; D3D12_GPU_VIRTUAL_ADDRESS GetGPUAddressOfCurrentAllocation() const;
unsigned int GetOffsetOfCurrentAllocation() const; size_t GetOffsetOfCurrentAllocation() const;
unsigned int GetSize() const; size_t GetSize() const;
static void QueueFenceCallback(void* owning_object, UINT64 fence_value); static void QueueFenceCallback(void* owning_object, UINT64 fence_value);
private: private:
void AllocateBuffer(unsigned int size); void AllocateBuffer(size_t size);
bool AttemptBufferResizeOrElseStall(unsigned int new_size); bool AttemptBufferResizeOrElseStall(size_t new_size);
bool AttemptToAllocateOutOfExistingUnusedSpaceInBuffer(unsigned int allocation_size); bool AttemptToAllocateOutOfExistingUnusedSpaceInBuffer(size_t allocation_size);
bool AttemptToFindExistingFenceToStallOn(unsigned int allocation_size); bool AttemptToFindExistingFenceToStallOn(size_t allocation_size);
void UpdateGPUProgress(); void UpdateGPUProgress();
void QueueFence(UINT64 fence_value); void QueueFence(UINT64 fence_value);
@ -44,7 +44,7 @@ private:
struct FenceTrackingInformation struct FenceTrackingInformation
{ {
UINT64 fence_value; UINT64 fence_value;
unsigned int buffer_offset; size_t buffer_offset;
}; };
std::queue<FenceTrackingInformation> m_queued_fences; std::queue<FenceTrackingInformation> m_queued_fences;
@ -56,13 +56,13 @@ private:
void* m_buffer_cpu_address = nullptr; void* m_buffer_cpu_address = nullptr;
D3D12_GPU_VIRTUAL_ADDRESS m_buffer_gpu_address = {}; D3D12_GPU_VIRTUAL_ADDRESS m_buffer_gpu_address = {};
unsigned int m_buffer_current_allocation_offset = 0; size_t m_buffer_current_allocation_offset = 0;
unsigned int m_buffer_offset = 0; size_t m_buffer_offset = 0;
unsigned int m_buffer_size = 0; size_t m_buffer_size = 0;
const unsigned int m_buffer_max_size = 0; const size_t m_buffer_max_size = 0;
unsigned int m_buffer_gpu_completion_offset = 0; size_t m_buffer_gpu_completion_offset = 0;
bool* m_buffer_reallocation_notification = nullptr; bool* m_buffer_reallocation_notification = nullptr;
}; };

View File

@ -2,6 +2,8 @@
// Licensed under GPLv2+ // Licensed under GPLv2+
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <memory>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/MsgHandler.h" #include "Common/MsgHandler.h"
#include "VideoBackends/D3D12/D3DBase.h" #include "VideoBackends/D3D12/D3DBase.h"
@ -19,11 +21,11 @@ namespace DX12
namespace D3D namespace D3D
{ {
static D3DStreamBuffer* s_texture_upload_stream_buffer = nullptr; static std::unique_ptr<D3DStreamBuffer> s_texture_upload_stream_buffer;
void CleanupPersistentD3DTextureResources() void CleanupPersistentD3DTextureResources()
{ {
SAFE_DELETE(s_texture_upload_stream_buffer); s_texture_upload_stream_buffer.reset();
} }
void ReplaceRGBATexture2D(ID3D12Resource* texture12, const u8* buffer, unsigned int width, unsigned int height, unsigned int src_pitch, unsigned int level, D3D12_RESOURCE_STATES current_resource_state) void ReplaceRGBATexture2D(ID3D12Resource* texture12, const u8* buffer, unsigned int width, unsigned int height, unsigned int src_pitch, unsigned int level, D3D12_RESOURCE_STATES current_resource_state)
@ -32,7 +34,7 @@ void ReplaceRGBATexture2D(ID3D12Resource* texture12, const u8* buffer, unsigned
if (!s_texture_upload_stream_buffer) if (!s_texture_upload_stream_buffer)
{ {
s_texture_upload_stream_buffer = new D3DStreamBuffer(4 * 1024 * 1024, 64 * 1024 * 1024, nullptr); s_texture_upload_stream_buffer = std::make_unique<D3DStreamBuffer>(4 * 1024 * 1024, 64 * 1024 * 1024, nullptr);
} }
bool current_command_list_executed = s_texture_upload_stream_buffer->AllocateSpaceInBuffer(upload_size, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT); bool current_command_list_executed = s_texture_upload_stream_buffer->AllocateSpaceInBuffer(upload_size, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);

View File

@ -4,6 +4,7 @@
#include <cctype> #include <cctype>
#include <list> #include <list>
#include <memory>
#include <string> #include <string>
#include "VideoBackends/D3D12/D3DBase.h" #include "VideoBackends/D3D12/D3DBase.h"
@ -56,19 +57,19 @@ void ResourceBarrier(ID3D12GraphicsCommandList* command_list, ID3D12Resource* re
class UtilVertexBuffer class UtilVertexBuffer
{ {
public: public:
explicit UtilVertexBuffer(int size) explicit UtilVertexBuffer(size_t size)
{ {
m_stream_buffer = new D3DStreamBuffer(size, size * 4, nullptr); m_stream_buffer = std::make_unique<D3DStreamBuffer>(size, size * 4, nullptr);
} }
~UtilVertexBuffer() ~UtilVertexBuffer()
{ {
SAFE_DELETE(m_stream_buffer);
} }
unsigned int GetSize() const { return m_stream_buffer->GetSize(); } size_t GetSize() const { return m_stream_buffer->GetSize(); }
// returns vertex offset to the new data // returns vertex offset to the new data
int AppendData(void* data, int size, int vertex_size) size_t AppendData(const void* data, size_t size, size_t vertex_size)
{ {
m_stream_buffer->AllocateSpaceInBuffer(size, vertex_size); m_stream_buffer->AllocateSpaceInBuffer(size, vertex_size);
@ -77,7 +78,7 @@ public:
return m_stream_buffer->GetOffsetOfCurrentAllocation() / vertex_size; return m_stream_buffer->GetOffsetOfCurrentAllocation() / vertex_size;
} }
int BeginAppendData(void** write_ptr, int size, int vertex_size) size_t BeginAppendData(void** write_ptr, size_t size, size_t vertex_size)
{ {
m_stream_buffer->AllocateSpaceInBuffer(size, vertex_size); m_stream_buffer->AllocateSpaceInBuffer(size, vertex_size);
@ -97,14 +98,14 @@ public:
} }
private: private:
D3DStreamBuffer* m_stream_buffer = nullptr; std::unique_ptr<D3DStreamBuffer> m_stream_buffer;
}; };
CD3DFont font; CD3DFont font;
UtilVertexBuffer* util_vbuf_stq = nullptr; static std::unique_ptr<UtilVertexBuffer> util_vbuf_stq;
UtilVertexBuffer* util_vbuf_cq = nullptr; static std::unique_ptr<UtilVertexBuffer> util_vbuf_cq;
UtilVertexBuffer* util_vbuf_clearq = nullptr; static std::unique_ptr<UtilVertexBuffer> util_vbuf_clearq;
UtilVertexBuffer* util_vbuf_efbpokequads = nullptr; static std::unique_ptr<UtilVertexBuffer> util_vbuf_efbpokequads;
static const unsigned int s_max_num_vertices = 8000 * 6; static const unsigned int s_max_num_vertices = 8000 * 6;
@ -535,16 +536,16 @@ struct
} clear_quad_data; } clear_quad_data;
// ring buffer offsets // ring buffer offsets
int stq_offset; static size_t stq_offset;
int cq_offset; static size_t cq_offset;
int clearq_offset; static size_t clearq_offset;
void InitUtils() void InitUtils()
{ {
util_vbuf_stq = new UtilVertexBuffer(0x10000); util_vbuf_stq = std::make_unique<UtilVertexBuffer>(0x10000);
util_vbuf_cq = new UtilVertexBuffer(0x10000); util_vbuf_cq = std::make_unique<UtilVertexBuffer>(0x10000);
util_vbuf_clearq = new UtilVertexBuffer(0x10000); util_vbuf_clearq = std::make_unique<UtilVertexBuffer>(0x10000);
util_vbuf_efbpokequads = new UtilVertexBuffer(0x100000); util_vbuf_efbpokequads = std::make_unique<UtilVertexBuffer>(0x100000);
D3D12_SAMPLER_DESC point_sampler_desc = { D3D12_SAMPLER_DESC point_sampler_desc = {
D3D12_FILTER_MIN_MAG_MIP_POINT, D3D12_FILTER_MIN_MAG_MIP_POINT,
@ -590,10 +591,10 @@ void ShutdownUtils()
{ {
font.Shutdown(); font.Shutdown();
SAFE_DELETE(util_vbuf_stq); util_vbuf_stq.reset();
SAFE_DELETE(util_vbuf_cq); util_vbuf_cq.reset();
SAFE_DELETE(util_vbuf_clearq); util_vbuf_clearq.reset();
SAFE_DELETE(util_vbuf_efbpokequads); util_vbuf_efbpokequads.reset();
} }
void SetPointCopySampler() void SetPointCopySampler()
@ -660,7 +661,7 @@ void DrawShadedTexQuad(D3DTexture2D* texture,
D3D12_VERTEX_BUFFER_VIEW vb_view = { D3D12_VERTEX_BUFFER_VIEW vb_view = {
util_vbuf_stq->GetBuffer12()->GetGPUVirtualAddress(), // D3D12_GPU_VIRTUAL_ADDRESS BufferLocation; util_vbuf_stq->GetBuffer12()->GetGPUVirtualAddress(), // D3D12_GPU_VIRTUAL_ADDRESS BufferLocation;
util_vbuf_stq->GetSize(), // UINT SizeInBytes; This is the size of the entire buffer, not just the size of the vertex data for one draw call, since the offsetting is done in the draw call itself. static_cast<UINT>(util_vbuf_stq->GetSize()), // UINT SizeInBytes; This is the size of the entire buffer, not just the size of the vertex data for one draw call, since the offsetting is done in the draw call itself.
sizeof(STQVertex) // UINT StrideInBytes; sizeof(STQVertex) // UINT StrideInBytes;
}; };
@ -714,7 +715,7 @@ void DrawShadedTexQuad(D3DTexture2D* texture,
// 2 ^ D3D12_MAX_TEXTURE_DIMENSION_2_TO_EXP = 131072 // 2 ^ D3D12_MAX_TEXTURE_DIMENSION_2_TO_EXP = 131072
D3D::current_command_list->RSSetScissorRects(1, &CD3DX12_RECT(0, 0, 131072, 131072)); D3D::current_command_list->RSSetScissorRects(1, &CD3DX12_RECT(0, 0, 131072, 131072));
D3D::current_command_list->DrawInstanced(4, 1, stq_offset, 0); D3D::current_command_list->DrawInstanced(4, 1, static_cast<UINT>(stq_offset), 0);
g_renderer->RestoreAPIState(); g_renderer->RestoreAPIState();
} }
@ -749,7 +750,7 @@ void DrawColorQuad(u32 Color, float z, float x1, float y1, float x2, float y2, D
D3D12_VERTEX_BUFFER_VIEW vb_view = { D3D12_VERTEX_BUFFER_VIEW vb_view = {
util_vbuf_cq->GetBuffer12()->GetGPUVirtualAddress(), // D3D12_GPU_VIRTUAL_ADDRESS BufferLocation; util_vbuf_cq->GetBuffer12()->GetGPUVirtualAddress(), // D3D12_GPU_VIRTUAL_ADDRESS BufferLocation;
util_vbuf_cq->GetSize(), // UINT SizeInBytes; This is the size of the entire buffer, not just the size of the vertex data for one draw call, since the offsetting is done in the draw call itself. static_cast<UINT>(util_vbuf_cq->GetSize()), // UINT SizeInBytes; This is the size of the entire buffer, not just the size of the vertex data for one draw call, since the offsetting is done in the draw call itself.
sizeof(ColVertex) // UINT StrideInBytes; sizeof(ColVertex) // UINT StrideInBytes;
}; };
@ -795,7 +796,7 @@ void DrawColorQuad(u32 Color, float z, float x1, float y1, float x2, float y2, D
// 2 ^ D3D12_MAX_TEXTURE_DIMENSION_2_TO_EXP = 131072 // 2 ^ D3D12_MAX_TEXTURE_DIMENSION_2_TO_EXP = 131072
D3D::current_command_list->RSSetScissorRects(1, &CD3DX12_RECT(0, 0, 131072, 131072)); D3D::current_command_list->RSSetScissorRects(1, &CD3DX12_RECT(0, 0, 131072, 131072));
D3D::current_command_list->DrawInstanced(4, 1, cq_offset, 0); D3D::current_command_list->DrawInstanced(4, 1, static_cast<UINT>(cq_offset), 0);
g_renderer->RestoreAPIState(); g_renderer->RestoreAPIState();
} }
@ -822,7 +823,7 @@ void DrawClearQuad(u32 Color, float z, D3D12_BLEND_DESC* blend_desc, D3D12_DEPTH
D3D12_VERTEX_BUFFER_VIEW vb_view = { D3D12_VERTEX_BUFFER_VIEW vb_view = {
util_vbuf_clearq->GetBuffer12()->GetGPUVirtualAddress(), // D3D12_GPU_VIRTUAL_ADDRESS BufferLocation; util_vbuf_clearq->GetBuffer12()->GetGPUVirtualAddress(), // D3D12_GPU_VIRTUAL_ADDRESS BufferLocation;
util_vbuf_clearq->GetSize(), // UINT SizeInBytes; This is the size of the entire buffer, not just the size of the vertex data for one draw call, since the offsetting is done in the draw call itself. static_cast<UINT>(util_vbuf_clearq->GetSize()), // UINT SizeInBytes; This is the size of the entire buffer, not just the size of the vertex data for one draw call, since the offsetting is done in the draw call itself.
sizeof(ClearVertex) // UINT StrideInBytes; sizeof(ClearVertex) // UINT StrideInBytes;
}; };
@ -870,7 +871,7 @@ void DrawClearQuad(u32 Color, float z, D3D12_BLEND_DESC* blend_desc, D3D12_DEPTH
// 2 ^ D3D12_MAX_TEXTURE_DIMENSION_2_TO_EXP = 131072 // 2 ^ D3D12_MAX_TEXTURE_DIMENSION_2_TO_EXP = 131072
D3D::current_command_list->RSSetScissorRects(1, &CD3DX12_RECT(0, 0, 131072, 131072)); D3D::current_command_list->RSSetScissorRects(1, &CD3DX12_RECT(0, 0, 131072, 131072));
D3D::current_command_list->DrawInstanced(4, 1, clearq_offset, 0); D3D::current_command_list->DrawInstanced(4, 1, static_cast<UINT>(clearq_offset), 0);
g_renderer->RestoreAPIState(); g_renderer->RestoreAPIState();
} }
@ -942,7 +943,7 @@ void DrawEFBPokeQuads(EFBAccessType type,
size_t required_bytes = COL_QUAD_SIZE * points_to_draw; size_t required_bytes = COL_QUAD_SIZE * points_to_draw;
void* buffer_ptr = nullptr; void* buffer_ptr = nullptr;
int base_vertex_index = util_vbuf_efbpokequads->BeginAppendData(&buffer_ptr, static_cast<int>(required_bytes), sizeof(ColVertex)); size_t base_vertex_index = util_vbuf_efbpokequads->BeginAppendData(&buffer_ptr, required_bytes, sizeof(ColVertex));
CHECK(base_vertex_index * 16 + required_bytes <= util_vbuf_efbpokequads->GetSize(), "Uh oh"); CHECK(base_vertex_index * 16 + required_bytes <= util_vbuf_efbpokequads->GetSize(), "Uh oh");
@ -953,7 +954,7 @@ void DrawEFBPokeQuads(EFBAccessType type,
D3D12_VERTEX_BUFFER_VIEW vb_view = { D3D12_VERTEX_BUFFER_VIEW vb_view = {
util_vbuf_efbpokequads->GetBuffer12()->GetGPUVirtualAddress(), // D3D12_GPU_VIRTUAL_ADDRESS BufferLocation; util_vbuf_efbpokequads->GetBuffer12()->GetGPUVirtualAddress(), // D3D12_GPU_VIRTUAL_ADDRESS BufferLocation;
util_vbuf_efbpokequads->GetSize(), // UINT SizeInBytes; This is the size of the entire buffer, not just the size of the vertex data for one draw call, since the offsetting is done in the draw call itself. static_cast<UINT>(util_vbuf_efbpokequads->GetSize()), // UINT SizeInBytes; This is the size of the entire buffer, not just the size of the vertex data for one draw call, since the offsetting is done in the draw call itself.
sizeof(ColVertex) // UINT StrideInBytes; sizeof(ColVertex) // UINT StrideInBytes;
}; };
@ -991,7 +992,7 @@ void DrawEFBPokeQuads(EFBAccessType type,
} }
// Issue the draw // Issue the draw
D3D::current_command_list->DrawInstanced(6 * static_cast<unsigned int>(points_to_draw), 1, base_vertex_index, 0); D3D::current_command_list->DrawInstanced(6 * static_cast<UINT>(points_to_draw), 1, static_cast<UINT>(base_vertex_index), 0);
} }
} }

View File

@ -2,11 +2,12 @@
// Licensed under GPLv2+ // Licensed under GPLv2+
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include "D3DBase.h" #include <memory>
#include "D3DCommandListManager.h"
#include "D3DStreamBuffer.h"
#include "ShaderConstantsManager.h" #include "VideoBackends/D3D12/D3DBase.h"
#include "VideoBackends/D3D12/D3DCommandListManager.h"
#include "VideoBackends/D3D12/D3DStreamBuffer.h"
#include "VideoBackends/D3D12/ShaderConstantsManager.h"
#include "VideoCommon/GeometryShaderManager.h" #include "VideoCommon/GeometryShaderManager.h"
#include "VideoCommon/PixelShaderManager.h" #include "VideoCommon/PixelShaderManager.h"
@ -25,7 +26,7 @@ enum SHADER_STAGE
SHADER_STAGE_COUNT = 3 SHADER_STAGE_COUNT = 3
}; };
static std::array<D3DStreamBuffer*, SHADER_STAGE_COUNT> s_shader_constant_stream_buffers = {}; static std::array<std::unique_ptr<D3DStreamBuffer>, SHADER_STAGE_COUNT> s_shader_constant_stream_buffers;
static const unsigned int s_shader_constant_buffer_padded_sizes[SHADER_STAGE_COUNT] = { static const unsigned int s_shader_constant_buffer_padded_sizes[SHADER_STAGE_COUNT] = {
(sizeof(GeometryShaderConstants) + 0xff) & ~0xff, (sizeof(GeometryShaderConstants) + 0xff) & ~0xff,
@ -37,14 +38,14 @@ void ShaderConstantsManager::Init()
{ {
// Allow a large maximum size, as we want to minimize stalls here // Allow a large maximum size, as we want to minimize stalls here
std::generate(std::begin(s_shader_constant_stream_buffers), std::end(s_shader_constant_stream_buffers), []() { std::generate(std::begin(s_shader_constant_stream_buffers), std::end(s_shader_constant_stream_buffers), []() {
return new D3DStreamBuffer(2 * 1024 * 1024, 64 * 1024 * 1024, nullptr); return std::make_unique<D3DStreamBuffer>(2 * 1024 * 1024, 64 * 1024 * 1024, nullptr);
}); });
} }
void ShaderConstantsManager::Shutdown() void ShaderConstantsManager::Shutdown()
{ {
for (auto& it : s_shader_constant_stream_buffers) for (auto& buffer : s_shader_constant_stream_buffers)
SAFE_DELETE(it); buffer.reset();
} }
bool ShaderConstantsManager::LoadAndSetGeometryShaderConstants() bool ShaderConstantsManager::LoadAndSetGeometryShaderConstants()

View File

@ -563,7 +563,7 @@ TextureCache::TextureCache()
m_palette_pixel_shaders[GX_TL_RGB565] = GetConvertShader12(std::string("RGB565")); m_palette_pixel_shaders[GX_TL_RGB565] = GetConvertShader12(std::string("RGB565"));
m_palette_pixel_shaders[GX_TL_RGB5A3] = GetConvertShader12(std::string("RGB5A3")); m_palette_pixel_shaders[GX_TL_RGB5A3] = GetConvertShader12(std::string("RGB5A3"));
m_palette_stream_buffer = new D3DStreamBuffer(sizeof(u16) * 256 * 1024, sizeof(u16) * 256 * 1024 * 16, nullptr); m_palette_stream_buffer = std::make_unique<D3DStreamBuffer>(sizeof(u16) * 256 * 1024, sizeof(u16) * 256 * 1024 * 16, nullptr);
// Right now, there are only two variants of palette_uniform data. So, we'll just create an upload heap to permanently store both of these. // Right now, there are only two variants of palette_uniform data. So, we'll just create an upload heap to permanently store both of these.
CheckHR( CheckHR(
@ -604,8 +604,6 @@ TextureCache::~TextureCache()
s_efb_copy_stream_buffer.reset(); s_efb_copy_stream_buffer.reset();
SAFE_DELETE(m_palette_stream_buffer);
if (s_texture_cache_entry_readback_buffer) if (s_texture_cache_entry_readback_buffer)
{ {
D3D::command_list_mgr->DestroyResourceAfterCurrentCommandListExecuted(s_texture_cache_entry_readback_buffer); D3D::command_list_mgr->DestroyResourceAfterCurrentCommandListExecuted(s_texture_cache_entry_readback_buffer);

View File

@ -4,6 +4,8 @@
#pragma once #pragma once
#include <memory>
#include "VideoBackends/D3D12/D3DTexture.h" #include "VideoBackends/D3D12/D3DTexture.h"
#include "VideoCommon/TextureCacheBase.h" #include "VideoCommon/TextureCacheBase.h"
@ -59,7 +61,7 @@ private:
void CompileShaders() override { } void CompileShaders() override { }
void DeleteShaders() override { } void DeleteShaders() override { }
D3DStreamBuffer* m_palette_stream_buffer = nullptr; std::unique_ptr<D3DStreamBuffer> m_palette_stream_buffer;
ID3D12Resource* m_palette_uniform_buffer = nullptr; ID3D12Resource* m_palette_uniform_buffer = nullptr;
D3D12_SHADER_BYTECODE m_palette_pixel_shaders[3] = {}; D3D12_SHADER_BYTECODE m_palette_pixel_shaders[3] = {};

View File

@ -31,9 +31,9 @@ static constexpr unsigned int MAX_VBUFFER_SIZE = VertexManager::MAXVBUFFERSIZE *
void VertexManager::SetIndexBuffer() void VertexManager::SetIndexBuffer()
{ {
D3D12_INDEX_BUFFER_VIEW ib_view = { D3D12_INDEX_BUFFER_VIEW ib_view = {
m_index_stream_buffer->GetBaseGPUAddress(), // D3D12_GPU_VIRTUAL_ADDRESS BufferLocation; m_index_stream_buffer->GetBaseGPUAddress(), // D3D12_GPU_VIRTUAL_ADDRESS BufferLocation;
m_index_stream_buffer->GetSize(), // UINT SizeInBytes; static_cast<UINT>(m_index_stream_buffer->GetSize()), // UINT SizeInBytes;
DXGI_FORMAT_R16_UINT // DXGI_FORMAT Format; DXGI_FORMAT_R16_UINT // DXGI_FORMAT Format;
}; };
D3D::current_command_list->IASetIndexBuffer(&ib_view); D3D::current_command_list->IASetIndexBuffer(&ib_view);
@ -44,8 +44,8 @@ void VertexManager::CreateDeviceObjects()
m_vertex_draw_offset = 0; m_vertex_draw_offset = 0;
m_index_draw_offset = 0; m_index_draw_offset = 0;
m_vertex_stream_buffer = new D3DStreamBuffer(VertexManager::MAXVBUFFERSIZE * 2, MAX_VBUFFER_SIZE, &m_vertex_stream_buffer_reallocated); m_vertex_stream_buffer = std::make_unique<D3DStreamBuffer>(MAXVBUFFERSIZE * 2, MAX_VBUFFER_SIZE, &m_vertex_stream_buffer_reallocated);
m_index_stream_buffer = new D3DStreamBuffer(VertexManager::MAXIBUFFERSIZE * sizeof(u16) * 2, VertexManager::MAXIBUFFERSIZE * sizeof(u16) * 16, &m_index_stream_buffer_reallocated); m_index_stream_buffer = std::make_unique<D3DStreamBuffer>(MAXIBUFFERSIZE * sizeof(u16) * 2, MAXIBUFFERSIZE * sizeof(u16) * 16, &m_index_stream_buffer_reallocated);
SetIndexBuffer(); SetIndexBuffer();
@ -57,8 +57,8 @@ void VertexManager::CreateDeviceObjects()
void VertexManager::DestroyDeviceObjects() void VertexManager::DestroyDeviceObjects()
{ {
SAFE_DELETE(m_vertex_stream_buffer); m_vertex_stream_buffer.reset();
SAFE_DELETE(m_index_stream_buffer); m_index_stream_buffer.reset();
m_vertex_cpu_buffer.clear(); m_vertex_cpu_buffer.clear();
m_index_cpu_buffer.clear(); m_index_cpu_buffer.clear();
@ -95,9 +95,9 @@ void VertexManager::Draw(u32 stride)
if (D3D::command_list_mgr->GetCommandListDirtyState(COMMAND_LIST_STATE_VERTEX_BUFFER) || s_previous_stride != stride) if (D3D::command_list_mgr->GetCommandListDirtyState(COMMAND_LIST_STATE_VERTEX_BUFFER) || s_previous_stride != stride)
{ {
D3D12_VERTEX_BUFFER_VIEW vb_view = { D3D12_VERTEX_BUFFER_VIEW vb_view = {
m_vertex_stream_buffer->GetBaseGPUAddress(), // D3D12_GPU_VIRTUAL_ADDRESS BufferLocation; m_vertex_stream_buffer->GetBaseGPUAddress(), // D3D12_GPU_VIRTUAL_ADDRESS BufferLocation;
m_vertex_stream_buffer->GetSize(), // UINT SizeInBytes; static_cast<UINT>(m_vertex_stream_buffer->GetSize()), // UINT SizeInBytes;
stride // UINT StrideInBytes; stride // UINT StrideInBytes;
}; };
D3D::current_command_list->IASetVertexBuffers(0, 1, &vb_view); D3D::current_command_list->IASetVertexBuffers(0, 1, &vb_view);
@ -194,11 +194,11 @@ void VertexManager::ResetBuffer(u32 stride)
} }
s_pBaseBufferPointer = static_cast<u8*>(m_vertex_stream_buffer->GetBaseCPUAddress()); s_pBaseBufferPointer = static_cast<u8*>(m_vertex_stream_buffer->GetBaseCPUAddress());
s_pEndBufferPointer = s_pBaseBufferPointer + m_vertex_stream_buffer->GetSize(); s_pEndBufferPointer = s_pBaseBufferPointer + m_vertex_stream_buffer->GetSize();
s_pCurBufferPointer = static_cast<u8*>(m_vertex_stream_buffer->GetCPUAddressOfCurrentAllocation()); s_pCurBufferPointer = static_cast<u8*>(m_vertex_stream_buffer->GetCPUAddressOfCurrentAllocation());
m_vertex_draw_offset = m_vertex_stream_buffer->GetOffsetOfCurrentAllocation(); m_vertex_draw_offset = static_cast<u32>(m_vertex_stream_buffer->GetOffsetOfCurrentAllocation());
command_list_executed |= m_index_stream_buffer->AllocateSpaceInBuffer(MAXIBUFFERSIZE * sizeof(u16), sizeof(u16)); command_list_executed |= m_index_stream_buffer->AllocateSpaceInBuffer(MAXIBUFFERSIZE * sizeof(u16), sizeof(u16));
if (command_list_executed) if (command_list_executed)
{ {
g_renderer->SetViewport(); g_renderer->SetViewport();
@ -211,8 +211,8 @@ void VertexManager::ResetBuffer(u32 stride)
m_index_stream_buffer_reallocated = false; m_index_stream_buffer_reallocated = false;
} }
m_index_draw_offset = m_index_stream_buffer->GetOffsetOfCurrentAllocation(); m_index_draw_offset = static_cast<u32>(m_index_stream_buffer->GetOffsetOfCurrentAllocation());
IndexGenerator::Start(reinterpret_cast<u16*>(m_index_stream_buffer->GetCPUAddressOfCurrentAllocation())); IndexGenerator::Start(static_cast<u16*>(m_index_stream_buffer->GetCPUAddressOfCurrentAllocation()));
} }
} // namespace } // namespace

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <memory>
#include "VideoCommon/VertexManagerBase.h" #include "VideoCommon/VertexManagerBase.h"
namespace DX12 namespace DX12
@ -34,8 +35,8 @@ private:
u32 m_vertex_draw_offset; u32 m_vertex_draw_offset;
u32 m_index_draw_offset; u32 m_index_draw_offset;
D3DStreamBuffer* m_vertex_stream_buffer = nullptr; std::unique_ptr<D3DStreamBuffer> m_vertex_stream_buffer;
D3DStreamBuffer* m_index_stream_buffer = nullptr; std::unique_ptr<D3DStreamBuffer> m_index_stream_buffer;
bool m_vertex_stream_buffer_reallocated = false; bool m_vertex_stream_buffer_reallocated = false;
bool m_index_stream_buffer_reallocated = false; bool m_index_stream_buffer_reallocated = false;