Track batch fences with the batches.

This commit is contained in:
Dr. Chat
2016-11-08 18:11:12 -06:00
parent 391b6e9bb7
commit 48f6ba747c
11 changed files with 255 additions and 108 deletions

View File

@@ -49,7 +49,7 @@ class BaseFencedPool {
void Scavenge() {
while (pending_batch_list_head_) {
auto batch = pending_batch_list_head_;
if (vkGetFenceStatus(device_, *batch->fence) == VK_SUCCESS) {
if (vkGetFenceStatus(device_, batch->fence) == VK_SUCCESS) {
// Batch has completed. Reclaim.
pending_batch_list_head_ = batch->next;
if (batch == pending_batch_list_tail_) {
@@ -72,7 +72,7 @@ class BaseFencedPool {
// Begins a new batch.
// All entries acquired within this batch will be marked as in-use until
// the fence specified in EndBatch is signalled.
void BeginBatch() {
VkFence BeginBatch() {
assert_null(open_batch_);
Batch* batch = nullptr;
if (free_batch_list_head_) {
@@ -80,15 +80,26 @@ class BaseFencedPool {
batch = free_batch_list_head_;
free_batch_list_head_ = batch->next;
batch->next = nullptr;
vkResetFences(device_, 1, &batch->fence);
} else {
// Allocate new batch.
batch = new Batch();
batch->next = nullptr;
VkFenceCreateInfo info;
info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
info.pNext = nullptr;
info.flags = 0;
VkResult res = vkCreateFence(device_, &info, nullptr, &batch->fence);
if (res != VK_SUCCESS) {
assert_always();
}
}
batch->entry_list_head = nullptr;
batch->entry_list_tail = nullptr;
batch->fence = nullptr;
open_batch_ = batch;
return batch->fence;
}
// Cancels an open batch, and releases all entries acquired within.
@@ -109,33 +120,8 @@ class BaseFencedPool {
batch->entry_list_tail = nullptr;
}
// Attempts to acquire an entry from the pool in the current batch.
// If none are available a new one will be allocated.
HANDLE AcquireEntry() {
Entry* entry = nullptr;
if (free_entry_list_head_) {
// Slice off an entry from the free list.
entry = free_entry_list_head_;
free_entry_list_head_ = entry->next;
} else {
// No entry available; allocate new.
entry = new Entry();
entry->handle = static_cast<T*>(this)->AllocateEntry();
}
entry->next = nullptr;
if (!open_batch_->entry_list_head) {
open_batch_->entry_list_head = entry;
}
if (open_batch_->entry_list_tail) {
open_batch_->entry_list_tail->next = entry;
}
open_batch_->entry_list_tail = entry;
return entry->handle;
}
// Ends the current batch using the given fence to indicate when the batch
// has completed execution on the GPU.
void EndBatch(std::shared_ptr<Fence> fence) {
// Ends the current batch.
void EndBatch() {
assert_not_null(open_batch_);
// Close and see if we have anything.
@@ -148,9 +134,6 @@ class BaseFencedPool {
return;
}
// Track the fence.
batch->fence = fence;
// Append to the end of the batch list.
batch->next = nullptr;
if (!pending_batch_list_head_) {
@@ -165,9 +148,52 @@ class BaseFencedPool {
}
protected:
void PushEntry(HANDLE handle) {
// Attempts to acquire an entry from the pool in the current batch.
// If none are available a new one will be allocated.
HANDLE AcquireEntry(void* data) {
Entry* entry = nullptr;
if (free_entry_list_head_) {
// Slice off an entry from the free list.
Entry* prev = nullptr;
Entry* cur = free_entry_list_head_;
while (cur != nullptr) {
if (cur->data == data) {
if (prev) {
prev->next = cur->next;
} else {
free_entry_list_head_ = cur->next;
}
entry = cur;
break;
}
prev = cur;
cur = cur->next;
}
}
if (!entry) {
// No entry available; allocate new.
entry = new Entry();
entry->data = data;
entry->handle = static_cast<T*>(this)->AllocateEntry(data);
}
entry->next = nullptr;
if (!open_batch_->entry_list_head) {
open_batch_->entry_list_head = entry;
}
if (open_batch_->entry_list_tail) {
open_batch_->entry_list_tail->next = entry;
}
open_batch_->entry_list_tail = entry;
return entry->handle;
}
void PushEntry(HANDLE handle, void* data) {
auto entry = new Entry();
entry->next = free_entry_list_head_;
entry->data = data;
entry->handle = handle;
free_entry_list_head_ = entry;
}
@@ -192,13 +218,14 @@ class BaseFencedPool {
private:
struct Entry {
Entry* next;
void* data;
HANDLE handle;
};
struct Batch {
Batch* next;
Entry* entry_list_head;
Entry* entry_list_tail;
std::shared_ptr<Fence> fence;
VkFence fence;
};
Batch* free_batch_list_head_ = nullptr;
@@ -211,19 +238,39 @@ class BaseFencedPool {
class CommandBufferPool
: public BaseFencedPool<CommandBufferPool, VkCommandBuffer> {
public:
typedef BaseFencedPool<CommandBufferPool, VkCommandBuffer> Base;
CommandBufferPool(VkDevice device, uint32_t queue_family_index,
VkCommandBufferLevel level);
~CommandBufferPool() override;
VkCommandBuffer AcquireEntry() { return Base::AcquireEntry(nullptr); }
protected:
friend class BaseFencedPool<CommandBufferPool, VkCommandBuffer>;
VkCommandBuffer AllocateEntry();
VkCommandBuffer AllocateEntry(void* data);
void FreeEntry(VkCommandBuffer handle);
VkCommandPool command_pool_ = nullptr;
VkCommandBufferLevel level_ = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
};
class DescriptorPool : public BaseFencedPool<DescriptorPool, VkDescriptorSet> {
public:
DescriptorPool(VkDevice device, uint32_t max_count,
std::vector<VkDescriptorPoolSize> pool_sizes);
~DescriptorPool() override;
VkDescriptorSet AcquireEntry(VkDescriptorSetLayout layout) { return nullptr; }
protected:
friend class BaseFencedPool<DescriptorPool, VkCommandBuffer>;
VkDescriptorSet AllocateEntry(void* data);
void FreeEntry(VkDescriptorSet handle);
VkDescriptorPool descriptor_pool_ = nullptr;
};
} // namespace vulkan
} // namespace ui
} // namespace xe