From 1fa81f24d3b24f8b47dfa58040f57bbeb7407c30 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 18 Feb 2017 03:14:30 -0500 Subject: [PATCH] VertexManagerBase: Make CreateNativeVertexFormat return a unique_ptr Much safer as opposed to just returning raw allocated memory. --- Source/Core/VideoBackends/D3D/NativeVertexFormat.cpp | 4 ++-- Source/Core/VideoBackends/D3D/VertexManager.h | 5 ++++- Source/Core/VideoBackends/D3D12/D3DState.cpp | 2 +- Source/Core/VideoBackends/D3D12/NativeVertexFormat.cpp | 4 ++-- Source/Core/VideoBackends/D3D12/VertexManager.h | 4 +++- Source/Core/VideoBackends/Null/VertexManager.cpp | 4 ++-- Source/Core/VideoBackends/Null/VertexManager.h | 5 ++++- Source/Core/VideoBackends/OGL/NativeVertexFormat.cpp | 4 ++-- Source/Core/VideoBackends/OGL/VertexManager.h | 6 +++++- Source/Core/VideoBackends/Software/SWVertexLoader.cpp | 4 ++-- Source/Core/VideoBackends/Software/SWVertexLoader.h | 4 +++- Source/Core/VideoBackends/Vulkan/VertexManager.cpp | 4 ++-- Source/Core/VideoBackends/Vulkan/VertexManager.h | 3 ++- Source/Core/VideoCommon/VertexLoaderManager.cpp | 2 +- Source/Core/VideoCommon/VertexManagerBase.h | 2 +- 15 files changed, 36 insertions(+), 21 deletions(-) diff --git a/Source/Core/VideoBackends/D3D/NativeVertexFormat.cpp b/Source/Core/VideoBackends/D3D/NativeVertexFormat.cpp index 4aa52bd4bf..01658b66e6 100644 --- a/Source/Core/VideoBackends/D3D/NativeVertexFormat.cpp +++ b/Source/Core/VideoBackends/D3D/NativeVertexFormat.cpp @@ -27,10 +27,10 @@ private: ID3D11InputLayout* m_layout = nullptr; }; -NativeVertexFormat* +std::unique_ptr VertexManager::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) { - return new D3DVertexFormat(vtx_decl); + return std::make_unique(vtx_decl); } static const DXGI_FORMAT d3d_format_lookup[5 * 4 * 2] = { diff --git a/Source/Core/VideoBackends/D3D/VertexManager.h b/Source/Core/VideoBackends/D3D/VertexManager.h index 67c2962522..3bb444a3b4 100644 --- a/Source/Core/VideoBackends/D3D/VertexManager.h +++ b/Source/Core/VideoBackends/D3D/VertexManager.h @@ -4,6 +4,7 @@ #pragma once +#include #include "VideoCommon/VertexManagerBase.h" struct ID3D11Buffer; @@ -16,7 +17,9 @@ public: VertexManager(); ~VertexManager(); - NativeVertexFormat* CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) override; + std::unique_ptr + CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) override; + void CreateDeviceObjects() override; void DestroyDeviceObjects() override; diff --git a/Source/Core/VideoBackends/D3D12/D3DState.cpp b/Source/Core/VideoBackends/D3D12/D3DState.cpp index bf0497ba51..6b0f0dd807 100644 --- a/Source/Core/VideoBackends/D3D12/D3DState.cpp +++ b/Source/Core/VideoBackends/D3D12/D3DState.cpp @@ -81,7 +81,7 @@ public: if (!native) { - native.reset(g_vertex_manager->CreateNativeVertexFormat(native_vtx_decl)); + native = g_vertex_manager->CreateNativeVertexFormat(native_vtx_decl); } desc.InputLayout = reinterpret_cast(native.get())->GetActiveInputLayout12(); diff --git a/Source/Core/VideoBackends/D3D12/NativeVertexFormat.cpp b/Source/Core/VideoBackends/D3D12/NativeVertexFormat.cpp index 30b071833a..f7f1d58074 100644 --- a/Source/Core/VideoBackends/D3D12/NativeVertexFormat.cpp +++ b/Source/Core/VideoBackends/D3D12/NativeVertexFormat.cpp @@ -13,10 +13,10 @@ namespace DX12 { -NativeVertexFormat* +std::unique_ptr VertexManager::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) { - return new D3DVertexFormat(vtx_decl); + return std::make_unique(vtx_decl); } static const constexpr DXGI_FORMAT d3d_format_lookup[5 * 4 * 2] = { diff --git a/Source/Core/VideoBackends/D3D12/VertexManager.h b/Source/Core/VideoBackends/D3D12/VertexManager.h index c180542ccc..9de05bfb4b 100644 --- a/Source/Core/VideoBackends/D3D12/VertexManager.h +++ b/Source/Core/VideoBackends/D3D12/VertexManager.h @@ -17,7 +17,9 @@ public: VertexManager(); ~VertexManager(); - NativeVertexFormat* CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) override; + std::unique_ptr + CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) override; + void CreateDeviceObjects() override; void DestroyDeviceObjects() override; diff --git a/Source/Core/VideoBackends/Null/VertexManager.cpp b/Source/Core/VideoBackends/Null/VertexManager.cpp index c615bc468b..be2a8f0af5 100644 --- a/Source/Core/VideoBackends/Null/VertexManager.cpp +++ b/Source/Core/VideoBackends/Null/VertexManager.cpp @@ -19,10 +19,10 @@ public: void SetupVertexPointers() override {} }; -NativeVertexFormat* +std::unique_ptr VertexManager::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) { - return new NullNativeVertexFormat; + return std::make_unique(); } VertexManager::VertexManager() : m_local_v_buffer(MAXVBUFFERSIZE), m_local_i_buffer(MAXIBUFFERSIZE) diff --git a/Source/Core/VideoBackends/Null/VertexManager.h b/Source/Core/VideoBackends/Null/VertexManager.h index 3fd5d347d7..863090cab6 100644 --- a/Source/Core/VideoBackends/Null/VertexManager.h +++ b/Source/Core/VideoBackends/Null/VertexManager.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include "VideoCommon/VertexManagerBase.h" @@ -15,7 +16,9 @@ class VertexManager : public VertexManagerBase public: VertexManager(); ~VertexManager(); - NativeVertexFormat* CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) override; + + std::unique_ptr + CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) override; protected: void ResetBuffer(u32 stride) override; diff --git a/Source/Core/VideoBackends/OGL/NativeVertexFormat.cpp b/Source/Core/VideoBackends/OGL/NativeVertexFormat.cpp index c20e21666d..69a6229741 100644 --- a/Source/Core/VideoBackends/OGL/NativeVertexFormat.cpp +++ b/Source/Core/VideoBackends/OGL/NativeVertexFormat.cpp @@ -17,10 +17,10 @@ namespace OGL { -NativeVertexFormat* +std::unique_ptr VertexManager::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) { - return new GLVertexFormat(vtx_decl); + return std::make_unique(vtx_decl); } static inline GLuint VarToGL(VarType t) diff --git a/Source/Core/VideoBackends/OGL/VertexManager.h b/Source/Core/VideoBackends/OGL/VertexManager.h index a376c29e0e..7ba18b6ecc 100644 --- a/Source/Core/VideoBackends/OGL/VertexManager.h +++ b/Source/Core/VideoBackends/OGL/VertexManager.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include "Common/CommonTypes.h" @@ -31,7 +32,10 @@ class VertexManager : public VertexManagerBase public: VertexManager(); ~VertexManager(); - NativeVertexFormat* CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) override; + + std::unique_ptr + CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) override; + void CreateDeviceObjects() override; void DestroyDeviceObjects() override; diff --git a/Source/Core/VideoBackends/Software/SWVertexLoader.cpp b/Source/Core/VideoBackends/Software/SWVertexLoader.cpp index 4115aa7479..d81cb4da4f 100644 --- a/Source/Core/VideoBackends/Software/SWVertexLoader.cpp +++ b/Source/Core/VideoBackends/Software/SWVertexLoader.cpp @@ -33,10 +33,10 @@ public: void SetupVertexPointers() override {} }; -NativeVertexFormat* +std::unique_ptr SWVertexLoader::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) { - return new NullNativeVertexFormat(vtx_decl); + return std::make_unique(vtx_decl); } SWVertexLoader::SWVertexLoader() : LocalVBuffer(MAXVBUFFERSIZE), LocalIBuffer(MAXIBUFFERSIZE) diff --git a/Source/Core/VideoBackends/Software/SWVertexLoader.h b/Source/Core/VideoBackends/Software/SWVertexLoader.h index 6ad4a1069a..abb1280352 100644 --- a/Source/Core/VideoBackends/Software/SWVertexLoader.h +++ b/Source/Core/VideoBackends/Software/SWVertexLoader.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include "Common/CommonTypes.h" @@ -19,7 +20,8 @@ public: SWVertexLoader(); ~SWVertexLoader(); - NativeVertexFormat* CreateNativeVertexFormat(const PortableVertexDeclaration& vdec) override; + std::unique_ptr + CreateNativeVertexFormat(const PortableVertexDeclaration& vdec) override; protected: void ResetBuffer(u32 stride) override; diff --git a/Source/Core/VideoBackends/Vulkan/VertexManager.cpp b/Source/Core/VideoBackends/Vulkan/VertexManager.cpp index 63857a8c42..9a5e821e19 100644 --- a/Source/Core/VideoBackends/Vulkan/VertexManager.cpp +++ b/Source/Core/VideoBackends/Vulkan/VertexManager.cpp @@ -63,10 +63,10 @@ bool VertexManager::Initialize() return true; } -NativeVertexFormat* +std::unique_ptr VertexManager::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) { - return new VertexFormat(vtx_decl); + return std::make_unique(vtx_decl); } void VertexManager::PrepareDrawBuffers(u32 stride) diff --git a/Source/Core/VideoBackends/Vulkan/VertexManager.h b/Source/Core/VideoBackends/Vulkan/VertexManager.h index 8d122179ce..1f892e4151 100644 --- a/Source/Core/VideoBackends/Vulkan/VertexManager.h +++ b/Source/Core/VideoBackends/Vulkan/VertexManager.h @@ -24,7 +24,8 @@ public: bool Initialize(); - NativeVertexFormat* CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) override; + std::unique_ptr + CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) override; protected: void PrepareDrawBuffers(u32 stride); diff --git a/Source/Core/VideoCommon/VertexLoaderManager.cpp b/Source/Core/VideoCommon/VertexLoaderManager.cpp index 7f8c79c77d..c7a2f46bab 100644 --- a/Source/Core/VideoCommon/VertexLoaderManager.cpp +++ b/Source/Core/VideoCommon/VertexLoaderManager.cpp @@ -161,7 +161,7 @@ static VertexLoaderBase* RefreshLoader(int vtx_attr_group, bool preprocess = fal std::unique_ptr& native = s_native_vertex_map[format]; if (!native) { - native.reset(g_vertex_manager->CreateNativeVertexFormat(format)); + native = g_vertex_manager->CreateNativeVertexFormat(format); } loader->m_native_vertex_format = native.get(); } diff --git a/Source/Core/VideoCommon/VertexManagerBase.h b/Source/Core/VideoCommon/VertexManagerBase.h index 9ebda79a60..20ebd9307d 100644 --- a/Source/Core/VideoCommon/VertexManagerBase.h +++ b/Source/Core/VideoCommon/VertexManagerBase.h @@ -56,7 +56,7 @@ public: void Flush(); - virtual NativeVertexFormat* + virtual std::unique_ptr CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) = 0; void DoState(PointerWrap& p);