Skip to content

Commit

Permalink
Vulkan: Don't bother with extra userdata, comments.
Browse files Browse the repository at this point in the history
Was a failed attempt to avoid new/delete.
  • Loading branch information
unknownbrackets committed Mar 27, 2016
1 parent bd7c431 commit 9921fd2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
13 changes: 6 additions & 7 deletions Common/Vulkan/VulkanContext.h
Expand Up @@ -57,13 +57,12 @@ struct VulkanPhysicalDeviceInfo {
// This is a bit repetitive...
class VulkanDeleteList {
struct Callback {
explicit Callback(void (*f)(void *userdata1, void *userdata2), void *u1, void *u2)
: func(f), userdata1(u1), userdata2(u2) {
explicit Callback(void (*f)(void *userdata), void *u)
: func(f), userdata(u) {
}

void (*func)(void *userdata1, void *userdata2);
void *userdata1;
void *userdata2;
void (*func)(void *userdata);
void *userdata;
};

public:
Expand All @@ -78,7 +77,7 @@ class VulkanDeleteList {
void QueueDeletePipelineCache(VkPipelineCache pipelineCache) { pipelineCaches_.push_back(pipelineCache); }
void QueueDeleteRenderPass(VkRenderPass renderPass) { renderPasses_.push_back(renderPass); }
void QueueDeleteFramebuffer(VkFramebuffer framebuffer) { framebuffers_.push_back(framebuffer); }
void QueueCallback(void (*func)(void *userdata1, void *userdata2), void *userdata1, void *userdata2) { callbacks_.push_back(Callback(func, userdata1, userdata2)); }
void QueueCallback(void (*func)(void *userdata), void *userdata) { callbacks_.push_back(Callback(func, userdata)); }

void Take(VulkanDeleteList &del) {
assert(descPools_.size() == 0);
Expand Down Expand Up @@ -153,7 +152,7 @@ class VulkanDeleteList {
}
framebuffers_.clear();
for (auto &callback : callbacks_) {
callback.func(callback.userdata1, callback.userdata2);
callback.func(callback.userdata);
}
callbacks_.clear();
}
Expand Down
4 changes: 2 additions & 2 deletions Common/Vulkan/VulkanMemory.cpp
Expand Up @@ -228,8 +228,8 @@ void VulkanDeviceAllocator::Free(VkDeviceMemory deviceMemory, size_t offset) {
}

// Okay, now enqueue. It's valid.
FreeInfo *info = new FreeInfo(deviceMemory, offset);
vulkan_->Delete().QueueCallback(&DispatchFree, this, info);
FreeInfo *info = new FreeInfo(this, deviceMemory, offset);
vulkan_->Delete().QueueCallback(&DispatchFree, info);
}

void VulkanDeviceAllocator::ExecuteFree(FreeInfo *userdata) {
Expand Down
20 changes: 15 additions & 5 deletions Common/Vulkan/VulkanMemory.h
Expand Up @@ -128,11 +128,18 @@ class VulkanPushBuffer {
uint8_t *writePtr_;
};

// VulkanDeviceAllocator
//
// Implements a slab based allocator that manages suballocations inside the slabs.
// Bitmaps are used to handle allocation state, with a 1KB grain.
class VulkanDeviceAllocator {
public:
// Slab sizes start at minSlabSize and double until maxSlabSize.
// Total slab count is unlimited, as long as there's free memory.
VulkanDeviceAllocator(VulkanContext *vulkan, size_t minSlabSize, size_t maxSlabSize);
~VulkanDeviceAllocator();

// Requires all memory be free beforehand (including all pending deletes.)
void Destroy();

void Begin() {
Expand All @@ -142,7 +149,10 @@ class VulkanDeviceAllocator {
void End() {
}

// May return ALLOCATE_FAILED if the allocation fails.
size_t Allocate(const VkMemoryRequirements &reqs, VkDeviceMemory *deviceMemory);

// Crashes on a double or misfree.
void Free(VkDeviceMemory deviceMemory, size_t offset);

static const size_t ALLOCATE_FAILED = -1;
Expand All @@ -164,18 +174,18 @@ class VulkanDeviceAllocator {
};

struct FreeInfo {
explicit FreeInfo(VkDeviceMemory d, size_t o)
: deviceMemory(d), offset(o) {
explicit FreeInfo(VulkanDeviceAllocator *a, VkDeviceMemory d, size_t o)
: allocator(a), deviceMemory(d), offset(o) {
}

VulkanDeviceAllocator *allocator;
VkDeviceMemory deviceMemory;
size_t offset;
};

static void DispatchFree(void *thiz, void *userdata) {
auto allocator = static_cast<VulkanDeviceAllocator *>(thiz);
static void DispatchFree(void *userdata) {
auto freeInfo = static_cast<FreeInfo *>(userdata);
allocator->ExecuteFree(freeInfo);
freeInfo->allocator->ExecuteFree(freeInfo);
}

bool AllocateSlab(size_t minBytes);
Expand Down

0 comments on commit 9921fd2

Please sign in to comment.