Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ To clean-up, simply call `Release()` once the is GPU is finished.
```cpp
#include <gpgmm_vk.h>

gpgmm::vk::GpCreateAllocatorInfo allocatorInfo = {};
gpgmm::vk::GpAllocatorCreateInfo allocatorInfo = {};

gpgmm::vk::GpResourceAllocator resourceAllocator;
gpgmm::vk::gpCreateResourceAllocator(allocatorInfo, &resourceAllocator)
Expand Down
7 changes: 4 additions & 3 deletions src/gpgmm/common/MemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace gpgmm {
}

uint64_t MemoryAllocator::GetMemoryAlignment() const {
return kInvalidOffset;
return kNoRequiredAlignment;
}

MemoryAllocatorInfo MemoryAllocator::GetInfo() const {
Expand Down Expand Up @@ -143,8 +143,9 @@ namespace gpgmm {
}

// Check request size has compatible alignment with |this| memory allocator.
if (GetMemoryAlignment() != kInvalidSize &&
!IsAligned(GetMemoryAlignment(), request.Alignment)) {
// Alignment value of 1 means no alignment required.
if (GetMemoryAlignment() == 0 ||
(GetMemoryAlignment() > 1 && !IsAligned(GetMemoryAlignment(), request.Alignment))) {
DebugEvent(GetTypename(), EventMessageId::AlignmentMismatch)
<< "Requested alignment exceeds memory alignment (" +
std::to_string(request.Alignment) + " vs " +
Expand Down
1 change: 1 addition & 0 deletions src/gpgmm/utils/Limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace gpgmm {

static constexpr uint64_t kNoRequiredAlignment = 1u;
static constexpr uint64_t kInvalidOffset = std::numeric_limits<uint64_t>::max();
static constexpr uint64_t kInvalidSize = std::numeric_limits<uint64_t>::max();
static constexpr uint64_t kInvalidIndex = std::numeric_limits<uint64_t>::max();
Expand Down
12 changes: 2 additions & 10 deletions src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@
namespace gpgmm::vk {

DeviceMemoryAllocator::DeviceMemoryAllocator(GpResourceAllocator resourceAllocator,
uint32_t memoryTypeIndex,
VkDeviceSize memorySize)
: mResourceAllocator(resourceAllocator),
mMemoryTypeIndex(memoryTypeIndex),
mMemorySize(memorySize) {
uint32_t memoryTypeIndex)
: mResourceAllocator(resourceAllocator), mMemoryTypeIndex(memoryTypeIndex) {
}

std::unique_ptr<MemoryAllocation> DeviceMemoryAllocator::TryAllocateMemory(
Expand Down Expand Up @@ -86,9 +83,4 @@ namespace gpgmm::vk {

SafeRelease(allocation);
}

uint64_t DeviceMemoryAllocator::GetMemorySize() const {
return mMemorySize;
}

} // namespace gpgmm::vk
7 changes: 1 addition & 6 deletions src/gpgmm/vk/DeviceMemoryAllocatorVk.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,17 @@ namespace gpgmm::vk {

class DeviceMemoryAllocator final : public MemoryAllocator {
public:
DeviceMemoryAllocator(GpResourceAllocator resourceAllocator,
uint32_t memoryTypeIndex,
VkDeviceSize memorySize);
DeviceMemoryAllocator(GpResourceAllocator resourceAllocator, uint32_t memoryTypeIndex);
~DeviceMemoryAllocator() override = default;

// MemoryAllocator interface
std::unique_ptr<MemoryAllocation> TryAllocateMemory(
const MemoryAllocationRequest& request) override;
void DeallocateMemory(std::unique_ptr<MemoryAllocation> allocation) override;

uint64_t GetMemorySize() const override;

private:
GpResourceAllocator mResourceAllocator;
uint32_t mMemoryTypeIndex;
VkDeviceSize mMemorySize;
};

} // namespace gpgmm::vk
Expand Down
24 changes: 12 additions & 12 deletions src/gpgmm/vk/FunctionsVk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@ namespace gpgmm::vk {
DestroyImage = vkFunctions->DestroyImage;
}

void VulkanFunctions::AssertVulkanFunctionsAreValid() {
ASSERT(GetPhysicalDeviceMemoryProperties != nullptr);
ASSERT(GetPhysicalDeviceProperties != nullptr);
ASSERT(AllocateMemory != nullptr);
ASSERT(FreeMemory != nullptr);
ASSERT(BindBufferMemory != nullptr);
ASSERT(GetBufferMemoryRequirements != nullptr);
ASSERT(GetImageMemoryRequirements != nullptr);
ASSERT(CreateBuffer != nullptr);
ASSERT(DestroyBuffer != nullptr);
ASSERT(CreateImage != nullptr);
ASSERT(DestroyImage != nullptr);
void AssertVulkanFunctionsExist(const VulkanFunctions& vkFunctions) {
ASSERT(vkFunctions.GetPhysicalDeviceMemoryProperties != nullptr);
ASSERT(vkFunctions.GetPhysicalDeviceProperties != nullptr);
ASSERT(vkFunctions.AllocateMemory != nullptr);
ASSERT(vkFunctions.FreeMemory != nullptr);
ASSERT(vkFunctions.BindBufferMemory != nullptr);
ASSERT(vkFunctions.GetBufferMemoryRequirements != nullptr);
ASSERT(vkFunctions.GetImageMemoryRequirements != nullptr);
ASSERT(vkFunctions.CreateBuffer != nullptr);
ASSERT(vkFunctions.DestroyBuffer != nullptr);
ASSERT(vkFunctions.CreateImage != nullptr);
ASSERT(vkFunctions.DestroyImage != nullptr);
}

} // namespace gpgmm::vk
8 changes: 4 additions & 4 deletions src/gpgmm/vk/FunctionsVk.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ namespace gpgmm::vk {
// Used to statically set functions from a static library (Vulkan loader).
void ImportDeviceFunctions();

// Used to import functions pre-specified by user.
// Used to import pre-loaded functions set by the user.
void ImportDeviceFunctions(const VulkanFunctions* vkFunctions);

// ASSERTs if any Vulkan function is left unset.
void AssertVulkanFunctionsAreValid();

// Order is important: instance must be loaded before device.
PFN_vkGetInstanceProcAddr GetInstanceProcAddr = nullptr;

Expand All @@ -50,4 +47,7 @@ namespace gpgmm::vk {
PFN_vkDestroyImage DestroyImage = nullptr;
};

// ASSERTs if any Vulkan function is left unset.
void AssertVulkanFunctionsExist(const VulkanFunctions& vkFunctions);

} // namespace gpgmm::vk
49 changes: 40 additions & 9 deletions src/gpgmm/vk/ResourceAllocatorVk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "gpgmm/vk/ResourceAllocatorVk.h"

#include "gpgmm/common/EventMessage.h"
#include "gpgmm/common/PooledMemoryAllocator.h"
#include "gpgmm/common/SegmentedMemoryAllocator.h"
#include "gpgmm/vk/BackendVk.h"
#include "gpgmm/vk/CapsVk.h"
#include "gpgmm/vk/DeviceMemoryAllocatorVk.h"
Expand All @@ -23,7 +25,7 @@

namespace gpgmm::vk {

VkResult gpCreateResourceAllocator(const GpCreateAllocatorInfo& info,
VkResult gpCreateResourceAllocator(const GpAllocatorCreateInfo& info,
GpResourceAllocator* allocatorOut) {
return GpResourceAllocator_T::CreateAllocator(info, allocatorOut);
}
Expand Down Expand Up @@ -109,7 +111,7 @@ namespace gpgmm::vk {
// GpResourceAllocator_T

// static
VkResult GpResourceAllocator_T::CreateAllocator(const GpCreateAllocatorInfo& info,
VkResult GpResourceAllocator_T::CreateAllocator(const GpAllocatorCreateInfo& info,
GpResourceAllocator* allocatorOut) {
VulkanFunctions vulkanFunctions = {};
{
Expand All @@ -125,7 +127,7 @@ namespace gpgmm::vk {
}

#ifndef NDEBUG
vulkanFunctions.AssertVulkanFunctionsAreValid();
AssertVulkanFunctionsExist(vulkanFunctions);
#endif
}

Expand All @@ -144,7 +146,7 @@ namespace gpgmm::vk {
return VK_SUCCESS;
}

GpResourceAllocator_T::GpResourceAllocator_T(const GpCreateAllocatorInfo& info,
GpResourceAllocator_T::GpResourceAllocator_T(const GpAllocatorCreateInfo& info,
const VulkanFunctions& vulkanFunctions,
std::unique_ptr<Caps> caps)
: mDevice(info.device), mVulkanFunctions(vulkanFunctions), mCaps(std::move(caps)) {
Expand All @@ -160,9 +162,8 @@ namespace gpgmm::vk {

for (uint32_t memoryTypeIndex = 0; memoryTypeIndex < mMemoryTypes.size();
memoryTypeIndex++) {
mDeviceAllocatorsPerType.emplace_back(std::make_unique<DeviceMemoryAllocator>(
this, memoryTypeIndex,
memoryHeaps[mMemoryTypes[memoryTypeIndex].heapIndex].size));
mDeviceAllocatorsPerType.emplace_back(
CreateDeviceMemoryAllocator(info, memoryTypeIndex, kNoRequiredAlignment));
}
}
}
Expand Down Expand Up @@ -225,9 +226,10 @@ namespace gpgmm::vk {
MemoryAllocationRequest request = {};
request.SizeInBytes = requirements.size;
request.Alignment = requirements.alignment;
request.NeverAllocate = (allocationInfo.flags & GP_ALLOCATION_FLAG_NEVER_ALLOCATE_MEMORY);
request.NeverAllocate = (allocationInfo.flags & GP_ALLOCATION_CREATE_NEVER_ALLOCATE_MEMORY);
request.AlwaysCacheSize = false;
request.AlwaysPrefetch = (allocationInfo.flags & GP_ALLOCATION_FLAG_ALWAYS_PREFETCH_MEMORY);
request.AlwaysPrefetch =
(allocationInfo.flags & GP_ALLOCATION_CREATE_ALWAYS_PREFETCH_MEMORY);

std::unique_ptr<MemoryAllocation> memoryAllocation = allocator->TryAllocateMemory(request);
if (memoryAllocation == nullptr) {
Expand Down Expand Up @@ -261,4 +263,33 @@ namespace gpgmm::vk {
Caps* GpResourceAllocator_T::GetCaps() const {
return mCaps.get();
}

std::unique_ptr<MemoryAllocator> GpResourceAllocator_T::CreateDeviceMemoryAllocator(
const GpAllocatorCreateInfo& info,
uint64_t memoryTypeIndex,
uint64_t memoryAlignment) {
std::unique_ptr<MemoryAllocator> deviceMemoryAllocator =
std::make_unique<DeviceMemoryAllocator>(this, memoryTypeIndex);

if (!(info.flags & GP_ALLOCATOR_CREATE_ALWAYS_ON_DEMAND)) {
switch (info.poolAlgorithm) {
case GP_ALLOCATOR_ALGORITHM_FIXED_POOL: {
return std::make_unique<PooledMemoryAllocator>(
info.preferredDeviceMemorySize, memoryAlignment,
std::move(deviceMemoryAllocator));
}
case GP_ALLOCATOR_ALGORITHM_SEGMENTED_POOL: {
return std::make_unique<SegmentedMemoryAllocator>(
std::move(deviceMemoryAllocator), memoryAlignment);
}
default: {
UNREACHABLE();
return {};
}
}
}

return deviceMemoryAllocator;
}

} // namespace gpgmm::vk
Loading