Skip to content

Commit

Permalink
Zephyr: GPU: second refactoring pass
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed Nov 21, 2023
1 parent dd313e0 commit 0c26d68
Show file tree
Hide file tree
Showing 17 changed files with 533 additions and 518 deletions.
45 changes: 24 additions & 21 deletions zephyr/gpu/src/vulkan/bind_group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,30 @@ namespace zephyr {
VkDevice device,
VkDescriptorPool descriptor_pool,
const std::shared_ptr<BindGroupLayout>& layout
) : device(device), descriptor_pool(descriptor_pool), m_layout{layout} {
) : m_device{device}
, m_descriptor_pool{descriptor_pool}
, m_layout{layout} {
const void* layout_handle = layout->Handle();

auto info = VkDescriptorSetAllocateInfo{
const VkDescriptorSetAllocateInfo info{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.pNext = nullptr,
.descriptorPool = descriptor_pool,
.descriptorSetCount = 1,
.pSetLayouts = (const VkDescriptorSetLayout*)&layout_handle
};

if (vkAllocateDescriptorSets(device, &info, &descriptor_set) != VK_SUCCESS) {
if (vkAllocateDescriptorSets(device, &info, &m_descriptor_set) != VK_SUCCESS) {
ZEPHYR_PANIC("VulkanBindGroup: failed to allocate descriptor set");
}
}

~VulkanBindGroup() override {
vkFreeDescriptorSets(device, descriptor_pool, 1, &descriptor_set);
vkFreeDescriptorSets(m_device, m_descriptor_pool, 1, &m_descriptor_set);
}

void* Handle() override {
return (void*)descriptor_set;
return (void*)m_descriptor_set;
}

void Bind(u32 binding, Buffer* buffer, BindingType type) override {
Expand All @@ -46,7 +48,7 @@ namespace zephyr {
const VkWriteDescriptorSet write_descriptor_set{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = descriptor_set,
.dstSet = m_descriptor_set,
.dstBinding = binding,
.dstArrayElement = 0,
.descriptorCount = 1,
Expand All @@ -56,7 +58,7 @@ namespace zephyr {
.pTexelBufferView = nullptr
};

vkUpdateDescriptorSets(device, 1, &write_descriptor_set, 0, nullptr);
vkUpdateDescriptorSets(m_device, 1, &write_descriptor_set, 0, nullptr);
}

void Bind(
Expand All @@ -74,7 +76,7 @@ namespace zephyr {
const VkWriteDescriptorSet write_descriptor_set{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = descriptor_set,
.dstSet = m_descriptor_set,
.dstBinding = binding,
.dstArrayElement = 0,
.descriptorCount = 1,
Expand All @@ -84,7 +86,7 @@ namespace zephyr {
.pTexelBufferView = nullptr
};

vkUpdateDescriptorSets(device, 1, &write_descriptor_set, 0, nullptr);
vkUpdateDescriptorSets(m_device, 1, &write_descriptor_set, 0, nullptr);
}

void Bind(
Expand All @@ -102,7 +104,7 @@ namespace zephyr {
const VkWriteDescriptorSet write_descriptor_set{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = descriptor_set,
.dstSet = m_descriptor_set,
.dstBinding = binding,
.dstArrayElement = 0,
.descriptorCount = 1,
Expand All @@ -112,13 +114,13 @@ namespace zephyr {
.pTexelBufferView = nullptr
};

vkUpdateDescriptorSets(device, 1, &write_descriptor_set, 0, nullptr);
vkUpdateDescriptorSets(m_device, 1, &write_descriptor_set, 0, nullptr);
}

private:
VkDevice device;
VkDescriptorPool descriptor_pool;
VkDescriptorSet descriptor_set;
VkDevice m_device;
VkDescriptorPool m_descriptor_pool;
VkDescriptorSet m_descriptor_set{};
std::shared_ptr<BindGroupLayout> m_layout;
};

Expand All @@ -128,7 +130,8 @@ namespace zephyr {
VkDevice device,
VkDescriptorPool descriptor_pool,
std::span<BindGroupLayout::Entry const> entries
) : device(device), descriptor_pool(descriptor_pool) {
) : m_device{device}
, m_descriptor_pool{descriptor_pool} {
std::vector<VkDescriptorSetLayoutBinding> bindings{};

for (const auto& entry : entries) {
Expand All @@ -149,23 +152,23 @@ namespace zephyr {
.pBindings = bindings.data()
};

if (vkCreateDescriptorSetLayout(device, &info, nullptr, &layout) != VK_SUCCESS) {
if (vkCreateDescriptorSetLayout(device, &info, nullptr, &m_layout) != VK_SUCCESS) {
ZEPHYR_PANIC("VulkanBindGroupLayout: failed to create descriptor set layout");
}
}

~VulkanBindGroupLayout() override {
vkDestroyDescriptorSetLayout(device, layout, nullptr);
vkDestroyDescriptorSetLayout(m_device, m_layout, nullptr);
}

void* Handle() override {
return (void*)layout;
return (void*)m_layout;
}

private:
VkDevice device;
VkDescriptorPool descriptor_pool;
VkDescriptorSetLayout layout;
VkDevice m_device;
VkDescriptorPool m_descriptor_pool;
VkDescriptorSetLayout m_layout;
};

} // namespace zephyr
49 changes: 25 additions & 24 deletions zephyr/gpu/src/vulkan/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class VulkanBuffer final : public Buffer {
Buffer::Usage usage,
Buffer::Flags flags,
size_t size
) : allocator(allocator), size(size), host_visible(false) {
) : m_allocator{allocator}
, m_size{size} {
const VkBufferCreateInfo buffer_info{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
Expand All @@ -33,78 +34,78 @@ class VulkanBuffer final : public Buffer {
.usage = VMA_MEMORY_USAGE_AUTO
};

host_visible = flags & Buffer::Flags::HostVisible;
m_host_visible = flags & Buffer::Flags::HostVisible;

if(host_visible) {
if(m_host_visible) {
alloc_info.flags = flags & Buffer::Flags::HostAccessRandom ?
VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT :
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
}

if(vmaCreateBuffer(allocator, &buffer_info, &alloc_info, &buffer, &allocation, nullptr) != VK_SUCCESS) {
if(vmaCreateBuffer(allocator, &buffer_info, &alloc_info, &m_buffer, &m_allocation, nullptr) != VK_SUCCESS) {
ZEPHYR_PANIC("VulkanBuffer: failed to create buffer");
}
}

~VulkanBuffer() override {
Unmap();
vmaDestroyBuffer(allocator, buffer, allocation);
vmaDestroyBuffer(m_allocator, m_buffer, m_allocation);
}

void* Handle() override {
return (void*)buffer;
return (void*)m_buffer;
}

void Map() override {
if(host_data == nullptr) {
if(!host_visible) {
if(m_host_data == nullptr) {
if(!m_host_visible) {
ZEPHYR_PANIC("VulkanBuffer: attempted to map buffer which is not host visible");
}

if(vmaMapMemory(allocator, allocation, &host_data) != VK_SUCCESS) {
ZEPHYR_PANIC("VulkanBuffer: failed to map buffer to host memory, size={}", size);
if(vmaMapMemory(m_allocator, m_allocation, &m_host_data) != VK_SUCCESS) {
ZEPHYR_PANIC("VulkanBuffer: failed to map buffer to host memory, size={}", m_size);
}
}
}

void Unmap() override {
if(host_data != nullptr) {
vmaUnmapMemory(allocator, allocation);
host_data = nullptr;
if(m_host_data != nullptr) {
vmaUnmapMemory(m_allocator, m_allocation);
m_host_data = nullptr;
}
}

void* Data() override {
return host_data;
return m_host_data;
}

size_t Size() const override {
return size;
return m_size;
}

void Flush() override {
Flush(0, size);
Flush(0, m_size);
}

void Flush(size_t offset, size_t size) override {
auto range_end = offset + size;

if(range_end > this->size) {
if(range_end > this->m_size) {
ZEPHYR_PANIC("VulkanBuffer: out-of-bounds flush request, offset={}, size={}", offset, size);
}

if(vmaFlushAllocation(allocator, allocation, offset, size) != VK_SUCCESS) {
if(vmaFlushAllocation(m_allocator, m_allocation, offset, size) != VK_SUCCESS) {
ZEPHYR_PANIC("VulkanBuffer: failed to flush range");
}
}

private:
VkBuffer buffer;
VmaAllocator allocator;
VmaAllocation allocation;
size_t size;
bool host_visible;
void* host_data = nullptr;
VkBuffer m_buffer;
VmaAllocator m_allocator;
VmaAllocation m_allocation;
size_t m_size;
bool m_host_visible{false};
void* m_host_data{};
};

} // namespace zephyr
Loading

0 comments on commit 0c26d68

Please sign in to comment.