diff --git a/include/RED4ext/Rendering/RenderObject.hpp b/include/RED4ext/Rendering/RenderObject.hpp new file mode 100644 index 000000000..2676d892e --- /dev/null +++ b/include/RED4ext/Rendering/RenderObject.hpp @@ -0,0 +1,132 @@ +#pragma once + +#include + +namespace RED4ext +{ +class IRenderObject +{ +public: + template T> + friend class TRenderPtr; + + using AllocatorType = Memory::RenderDataAllocator; + + virtual Memory::IAllocator* GetAllocator() + { + return AllocatorType::Get(); + } + + virtual void Destroy() + { + if (this) + { + Memory::Delete(this); + } + } + + virtual ~IRenderObject() = default; + +protected: + void Release() + { + if (InterlockedDecrement(reinterpret_cast(&m_refCount)) == 0) + { + Destroy(); + } + } + + void AddRef() + { + InterlockedIncrement(reinterpret_cast(&m_refCount)); + } + +private: + uint32_t m_refCount{1}; +}; +RED4EXT_ASSERT_SIZE(IRenderObject, 0x10); + +template T = IRenderObject> +class TRenderPtr +{ +public: + TRenderPtr() = default; + + TRenderPtr(std::nullptr_t) noexcept + { + } + + explicit TRenderPtr(T* aPointer) noexcept + : m_instance(aPointer) + { + } + + TRenderPtr(const TRenderPtr& aOther) noexcept + : m_instance(aOther.m_instance) + { + if (m_instance) + { + m_instance->AddRef(); + } + } + + TRenderPtr(TRenderPtr&& aOther) noexcept + { + Swap(aOther); + } + + ~TRenderPtr() + { + Release(); + } + + explicit operator bool() const noexcept + { + return m_instance != nullptr; + } + + TRenderPtr& operator=(const TRenderPtr& aRhs) noexcept + { + TRenderPtr(aRhs).Swap(*this); + return *this; + } + + TRenderPtr& operator=(TRenderPtr&& aRhs) noexcept + { + Swap(aRhs); + return *this; + } + + T* operator->() const noexcept + { + return m_instance; + } + + T& operator*() const noexcept + { + return *m_instance; + } + + void Swap(TRenderPtr& aOther) noexcept + { + std::swap(m_instance, aOther.m_instance); + } + + T* GetPtr() const noexcept + { + return m_instance; + } + +private: + void Release() + { + if (m_instance) + { + m_instance->Release(); + } + } + + T* m_instance{nullptr}; +}; +RED4EXT_ASSERT_SIZE(TRenderPtr<>, 0x8); +} // namespace RED4ext diff --git a/include/RED4ext/RenderProxy-inl.hpp b/include/RED4ext/Rendering/RenderProxy-inl.hpp similarity index 98% rename from include/RED4ext/RenderProxy-inl.hpp rename to include/RED4ext/Rendering/RenderProxy-inl.hpp index cb765bdb0..da6b5e75e 100644 --- a/include/RED4ext/RenderProxy-inl.hpp +++ b/include/RED4ext/Rendering/RenderProxy-inl.hpp @@ -1,7 +1,7 @@ #pragma once #ifdef RED4EXT_STATIC_LIB -#include +#include #endif #include diff --git a/include/RED4ext/RenderProxy.hpp b/include/RED4ext/Rendering/RenderProxy.hpp similarity index 95% rename from include/RED4ext/RenderProxy.hpp rename to include/RED4ext/Rendering/RenderProxy.hpp index 1557169cf..4cef4d7ac 100644 --- a/include/RED4ext/RenderProxy.hpp +++ b/include/RED4ext/Rendering/RenderProxy.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include @@ -83,3 +83,7 @@ struct CRenderProxyHandle RED4EXT_ASSERT_SIZE(CRenderProxyHandle, 0x28); RED4EXT_ASSERT_OFFSET(CRenderProxyHandle, renderProxy, 0x10); } // namespace RED4ext + +#ifdef RED4EXT_HEADER_ONLY +#include +#endif diff --git a/include/RED4ext/RenderResource.hpp b/include/RED4ext/Rendering/RenderResource.hpp similarity index 70% rename from include/RED4ext/RenderResource.hpp rename to include/RED4ext/Rendering/RenderResource.hpp index f37ba8f00..c4478172d 100644 --- a/include/RED4ext/RenderResource.hpp +++ b/include/RED4ext/Rendering/RenderResource.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -8,9 +9,24 @@ namespace RED4ext { -struct CRenderMesh +struct IRenderResource : IRenderObject +{ + using AllocatorType = Memory::RenderResourcesAllocator; + + virtual Memory::IAllocator* GetAllocator() override + { + return AllocatorType::Get(); + } + + virtual ~IRenderResource() = default; + virtual CName GetResourceName() = 0; + virtual char** sub_20() = 0; + virtual int32_t sub_28() = 0; +}; +RED4EXT_ASSERT_SIZE(IRenderResource, 0x10); + +struct CRenderMesh : IRenderResource { - uint8_t unk00[0x10 - 0x00]; // 00 Vector4 quantizationScale; // 10 Vector4 quantizationBias; // 20 uint32_t vertexBufferID; // 30 - GpuApi buffer ID diff --git a/include/RED4ext/Scripting/Natives/CMesh.hpp b/include/RED4ext/Scripting/Natives/CMesh.hpp index d8fec7ba0..3a225dead 100644 --- a/include/RED4ext/Scripting/Natives/CMesh.hpp +++ b/include/RED4ext/Scripting/Natives/CMesh.hpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/include/RED4ext/Scripting/Natives/entMorphTargetSkinnedMeshComponent.hpp b/include/RED4ext/Scripting/Natives/entMorphTargetSkinnedMeshComponent.hpp index 7a5adeb5d..23be078e7 100644 --- a/include/RED4ext/Scripting/Natives/entMorphTargetSkinnedMeshComponent.hpp +++ b/include/RED4ext/Scripting/Natives/entMorphTargetSkinnedMeshComponent.hpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/include/RED4ext/Scripting/Natives/entSkinnedMeshComponent.hpp b/include/RED4ext/Scripting/Natives/entSkinnedMeshComponent.hpp index 50030530f..69a562808 100644 --- a/include/RED4ext/Scripting/Natives/entSkinnedMeshComponent.hpp +++ b/include/RED4ext/Scripting/Natives/entSkinnedMeshComponent.hpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/RenderProxy.cpp b/src/Rendering/RenderProxy.cpp similarity index 66% rename from src/RenderProxy.cpp rename to src/Rendering/RenderProxy.cpp index 39ed20746..4bfa893af 100644 --- a/src/RenderProxy.cpp +++ b/src/Rendering/RenderProxy.cpp @@ -2,4 +2,4 @@ #error Please define 'RED4EXT_STATIC_LIB' to compile this file. #endif -#include +#include