Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
ComputeSystemVKWithAllocator.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2025 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#ifdef JPH_USE_VK
8
11
13
16class JPH_EXPORT ComputeSystemVKWithAllocator : public ComputeSystemVK
17{
18public:
19 JPH_DECLARE_RTTI_VIRTUAL(JPH_EXPORT, ComputeSystemVKWithAllocator)
20
21 // Initialize the compute system
22 bool Initialize(VkInstance inInstance, VkPhysicalDevice inPhysicalDevice, PFN_vkGetInstanceProcAddr inGetInstanceProcAddr, PFN_vkGetDeviceProcAddr inVkGetDeviceProcAddr, VkDevice inDevice, uint32 inComputeQueueIndex, ComputeSystemResult &outResult);
23
25 virtual bool CreateBuffer(VkDeviceSize inSize, VkBufferUsageFlags inUsage, VkMemoryPropertyFlags inProperties, BufferVK &outBuffer) override;
26 virtual void FreeBuffer(BufferVK &ioBuffer) override;
27 virtual void * MapBuffer(BufferVK &ioBuffer) override;
28 virtual void UnmapBuffer(BufferVK &ioBuffer) override;
29
30protected:
31 virtual bool InitializeMemory() override;
32 virtual void ShutdownMemory() override;
33
34 uint32 FindMemoryType(uint32 inTypeFilter, VkMemoryPropertyFlags inProperties) const;
35 void AllocateMemory(VkDeviceSize inSize, uint32 inMemoryTypeBits, VkMemoryPropertyFlags inProperties, MemoryVK &ioMemory);
36 void FreeMemory(MemoryVK &ioMemory);
37
38 VkPhysicalDeviceMemoryProperties mMemoryProperties;
39
40 PFN_vkGetPhysicalDeviceMemoryProperties mVkGetPhysicalDeviceMemoryProperties = nullptr;
41
42private:
43 // Smaller allocations (from cMinAllocSize to cMaxAllocSize) will be done in blocks of cBlockSize bytes.
44 // We do this because there is a limit to the number of allocations that we can make in Vulkan.
45 static constexpr VkDeviceSize cMinAllocSize = 512;
46 static constexpr VkDeviceSize cMaxAllocSize = 65536;
47 static constexpr VkDeviceSize cBlockSize = 524288;
48
49 struct MemoryKey
50 {
51 bool operator == (const MemoryKey &inRHS) const
52 {
53 return mSize == inRHS.mSize && mProperties == inRHS.mProperties;
54 }
55
56 VkDeviceSize mSize;
57 VkMemoryPropertyFlags mProperties;
58 };
59
60 JPH_MAKE_HASH_STRUCT(MemoryKey, MemoryKeyHasher, t.mProperties, t.mSize)
61
62 struct Memory
63 {
64 Ref<MemoryVK> mMemory;
65 VkDeviceSize mOffset;
66 };
67
68 using MemoryCache = UnorderedMap<MemoryKey, Array<Memory>, MemoryKeyHasher>;
69
70 MemoryCache mMemoryCache;
71};
72
74
75#endif // JPH_USE_VK
#define JPH_EXPORT
Definition Core.h:278
#define JPH_NAMESPACE_END
Definition Core.h:428
std::uint32_t uint32
Definition Core.h:508
#define JPH_NAMESPACE_BEGIN
Definition Core.h:422
#define JPH_MAKE_HASH_STRUCT(type, name,...)
Definition HashCombine.h:198
#define JPH_DECLARE_RTTI_VIRTUAL(linkage, class_name)
Definition RTTI.h:245
Simple wrapper class to manage a Vulkan buffer.
Definition BufferVK.h:34
Simple wrapper class to manage a Vulkan memory block.
Definition BufferVK.h:15
Definition Reference.h:110
Helper class that either contains a valid result or an error.
Definition Result.h:12
Definition UnorderedMap.h:31