Skip to content

Commit

Permalink
vulkan object allocator init
Browse files Browse the repository at this point in the history
  • Loading branch information
K1ngst0m committed Oct 14, 2023
1 parent 0df88e3 commit 48e0eba
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 47 deletions.
4 changes: 2 additions & 2 deletions engine/api/vulkan/descriptorSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ DescriptorSetLayout::~DescriptorSetLayout()
// Destroy all created pools.
for(auto pool : m_pools)
{
vkDestroyDescriptorPool(getDevice()->getHandle(), pool, nullptr);
vkDestroyDescriptorPool(getDevice()->getHandle(), pool, vkAllocator());
}
}

Expand Down Expand Up @@ -75,7 +75,7 @@ DescriptorSet* DescriptorSetLayout::allocateSet()
createInfo.pNext = &descriptorPoolInlineUniformBlockCreateInfo;
}
VkDescriptorPool handle = VK_NULL_HANDLE;
auto result = vkCreateDescriptorPool(getDevice()->getHandle(), &createInfo, nullptr, &handle);
auto result = vkCreateDescriptorPool(getDevice()->getHandle(), &createInfo, vkAllocator(), &handle);
if(result != VK_SUCCESS)
return VK_NULL_HANDLE;

Expand Down
46 changes: 24 additions & 22 deletions engine/api/vulkan/device.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "device.h"
#include "api/gpuResource.h"

const VkAllocationCallbacks* gVkAllocator = aph::vk::vkAllocator();

namespace aph::vk
{

Expand Down Expand Up @@ -108,7 +110,7 @@ std::unique_ptr<Device> Device::Create(const DeviceCreateInfo& createInfo)
};

VkDevice handle = VK_NULL_HANDLE;
auto result = vkCreateDevice(physicalDevice->getHandle(), &deviceCreateInfo, nullptr, &handle);
auto result = vkCreateDevice(physicalDevice->getHandle(), &deviceCreateInfo, gVkAllocator, &handle);
if(result != VK_SUCCESS)
{
VK_LOG_ERR("Failed to create device: %s.", vk::utils::errorString(result));
Expand Down Expand Up @@ -151,7 +153,7 @@ void Device::Destroy(Device* pDevice)

if(pDevice->m_handle)
{
pDevice->m_table.vkDestroyDevice(pDevice->m_handle, nullptr);
pDevice->m_table.vkDestroyDevice(pDevice->m_handle, gVkAllocator);
}
}

Expand All @@ -168,7 +170,7 @@ VkResult Device::create(const CommandPoolCreateInfo& createInfo, VkCommandPool*
}

VkCommandPool cmdPool = VK_NULL_HANDLE;
_VR(m_table.vkCreateCommandPool(m_handle, &cmdPoolInfo, nullptr, &cmdPool));
_VR(m_table.vkCreateCommandPool(m_handle, &cmdPoolInfo, gVkAllocator, &cmdPool));
*ppPool = cmdPool;
return VK_SUCCESS;
}
Expand Down Expand Up @@ -199,7 +201,7 @@ VkResult Device::create(const ImageViewCreateInfo& createInfo, ImageView** ppIma
memcpy(&info.components, &createInfo.components, sizeof(VkComponentMapping));

VkImageView handle = VK_NULL_HANDLE;
_VR(m_table.vkCreateImageView(getHandle(), &info, nullptr, &handle));
_VR(m_table.vkCreateImageView(getHandle(), &info, gVkAllocator, &handle));

*ppImageView = new ImageView(createInfo, handle);

Expand All @@ -216,7 +218,7 @@ VkResult Device::create(const BufferCreateInfo& createInfo, Buffer** ppBuffer)
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
VkBuffer buffer;
_VR(vkCreateBuffer(getHandle(), &bufferInfo, nullptr, &buffer));
_VR(vkCreateBuffer(getHandle(), &bufferInfo, gVkAllocator, &buffer));

VkMemoryDedicatedRequirementsKHR dedicatedRequirements = {
VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR,
Expand Down Expand Up @@ -249,14 +251,14 @@ VkResult Device::create(const BufferCreateInfo& createInfo, Buffer** ppBuffer)
m_physicalDevice->findMemoryType(createInfo.domain, memRequirements.memoryRequirements.memoryTypeBits),
};

_VR(vkAllocateMemory(getHandle(), &memoryAllocateInfo, nullptr, &memory));
_VR(vkAllocateMemory(getHandle(), &memoryAllocateInfo, gVkAllocator, &memory));
}
else
{
VkMemoryAllocateInfo allocInfo = init::memoryAllocateInfo(
memRequirements.memoryRequirements.size,
m_physicalDevice->findMemoryType(createInfo.domain, memRequirements.memoryRequirements.memoryTypeBits));
_VR(vkAllocateMemory(m_handle, &allocInfo, nullptr, &memory));
_VR(vkAllocateMemory(m_handle, &allocInfo, gVkAllocator, &memory));
}

*ppBuffer = new Buffer(createInfo, buffer, memory);
Expand Down Expand Up @@ -288,7 +290,7 @@ VkResult Device::create(const ImageCreateInfo& createInfo, Image** ppImage)
imageCreateInfo.extent.depth = createInfo.extent.depth;

VkImage image;
_VR(m_table.vkCreateImage(m_handle, &imageCreateInfo, nullptr, &image));
_VR(m_table.vkCreateImage(m_handle, &imageCreateInfo, gVkAllocator, &image));

VkMemoryDedicatedRequirementsKHR dedicatedRequirements = {
VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR,
Expand Down Expand Up @@ -320,7 +322,7 @@ VkResult Device::create(const ImageCreateInfo& createInfo, Image** ppImage)
m_physicalDevice->findMemoryType(createInfo.domain, memRequirements.memoryRequirements.memoryTypeBits),
};

_VR(vkAllocateMemory(getHandle(), &memoryAllocateInfo, nullptr, &memory));
_VR(vkAllocateMemory(getHandle(), &memoryAllocateInfo, gVkAllocator, &memory));
}
else
{
Expand All @@ -331,7 +333,7 @@ VkResult Device::create(const ImageCreateInfo& createInfo, Image** ppImage)
m_physicalDevice->findMemoryType(createInfo.domain, memRequirements.memoryRequirements.memoryTypeBits),
};

_VR(vkAllocateMemory(m_handle, &allocInfo, nullptr, &memory));
_VR(vkAllocateMemory(m_handle, &allocInfo, gVkAllocator, &memory));
}

*ppImage = new Image(this, createInfo, image, memory);
Expand All @@ -348,9 +350,9 @@ void Device::destroy(Buffer* pBuffer)
{
if(pBuffer->getMemory() != VK_NULL_HANDLE)
{
vkFreeMemory(m_handle, pBuffer->getMemory(), nullptr);
vkFreeMemory(m_handle, pBuffer->getMemory(), gVkAllocator);
}
vkDestroyBuffer(m_handle, pBuffer->getHandle(), nullptr);
vkDestroyBuffer(m_handle, pBuffer->getHandle(), gVkAllocator);
delete pBuffer;
pBuffer = nullptr;
}
Expand All @@ -359,16 +361,16 @@ void Device::destroy(Image* pImage)
{
if(pImage->getMemory() != VK_NULL_HANDLE)
{
vkFreeMemory(m_handle, pImage->getMemory(), nullptr);
vkFreeMemory(m_handle, pImage->getMemory(), gVkAllocator);
}
vkDestroyImage(m_handle, pImage->getHandle(), nullptr);
vkDestroyImage(m_handle, pImage->getHandle(), gVkAllocator);
delete pImage;
pImage = nullptr;
}

void Device::destroy(ImageView* pImageView)
{
vkDestroyImageView(m_handle, pImageView->getHandle(), nullptr);
vkDestroyImageView(m_handle, pImageView->getHandle(), gVkAllocator);
delete pImageView;
pImageView = nullptr;
}
Expand All @@ -381,7 +383,7 @@ VkResult Device::create(const SwapChainCreateInfo& createInfo, SwapChain** ppSwa

void Device::destroy(SwapChain* pSwapchain)
{
vkDestroySwapchainKHR(getHandle(), pSwapchain->getHandle(), nullptr);
vkDestroySwapchainKHR(getHandle(), pSwapchain->getHandle(), gVkAllocator);
delete pSwapchain;
pSwapchain = nullptr;
}
Expand Down Expand Up @@ -419,7 +421,7 @@ VkCommandPool Device::getCommandPoolWithQueue(Queue* queue)

void Device::destroy(VkCommandPool pPool)
{
vkDestroyCommandPool(getHandle(), pPool, nullptr);
vkDestroyCommandPool(getHandle(), pPool, gVkAllocator);
pPool = nullptr;
}

Expand Down Expand Up @@ -585,7 +587,7 @@ void Device::destroy(Pipeline* pipeline)
{
auto program = pipeline->getProgram();
delete program;
m_table.vkDestroyPipeline(getHandle(), pipeline->getHandle(), nullptr);
m_table.vkDestroyPipeline(getHandle(), pipeline->getHandle(), gVkAllocator);
delete pipeline;
pipeline = nullptr;
}
Expand All @@ -598,7 +600,7 @@ VkResult Device::create(const ComputePipelineCreateInfo& createInfo, Pipeline**
ci.stage = init::pipelineShaderStageCreateInfo(VK_SHADER_STAGE_COMPUTE_BIT,
program->getShader(ShaderStage::CS)->getHandle());
VkPipeline handle = VK_NULL_HANDLE;
_VR(m_table.vkCreateComputePipelines(this->getHandle(), VK_NULL_HANDLE, 1, &ci, nullptr, &handle));
_VR(m_table.vkCreateComputePipelines(this->getHandle(), VK_NULL_HANDLE, 1, &ci, gVkAllocator, &handle));
*ppPipeline = new Pipeline(this, createInfo, handle, program);
return VK_SUCCESS;
}
Expand Down Expand Up @@ -801,7 +803,7 @@ VkResult Device::create(const SamplerCreateInfo& createInfo, Sampler** ppSampler
.forceExplicitReconstruction = convertInfo.forceExplicitReconstruction ? VK_TRUE : VK_FALSE,
};

_VR(vkCreateSamplerYcbcrConversion(getHandle(), &vkConvertInfo, nullptr, &ycbcr.conversion));
_VR(vkCreateSamplerYcbcrConversion(getHandle(), &vkConvertInfo, gVkAllocator, &ycbcr.conversion));

ycbcr.info.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO;
ycbcr.info.pNext = nullptr;
Expand All @@ -810,14 +812,14 @@ VkResult Device::create(const SamplerCreateInfo& createInfo, Sampler** ppSampler
ci.pNext = &ycbcr.info;
}

_VR(m_table.vkCreateSampler(getHandle(), &ci, nullptr, &sampler));
_VR(m_table.vkCreateSampler(getHandle(), &ci, gVkAllocator, &sampler));
*ppSampler = new Sampler(this, createInfo, sampler);
return VK_SUCCESS;
}

void Device::destroy(Sampler* pSampler)
{
m_table.vkDestroySampler(getHandle(), pSampler->getHandle(), nullptr);
m_table.vkDestroySampler(getHandle(), pSampler->getHandle(), gVkAllocator);
delete pSampler;
pSampler = nullptr;
}
Expand Down
8 changes: 4 additions & 4 deletions engine/api/vulkan/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ VkResult Instance::Create(const InstanceCreateInfo& createInfo, Instance** ppIns
#endif

VkInstance handle = VK_NULL_HANDLE;
VK_CHECK_RESULT(vkCreateInstance(&instanceCreateInfo, nullptr, &handle));
VK_CHECK_RESULT(vkCreateInstance(&instanceCreateInfo, vkAllocator(), &handle));

volkLoadInstance(handle);

Expand Down Expand Up @@ -165,7 +165,7 @@ VkResult Instance::Create(const InstanceCreateInfo& createInfo, Instance** ppIns
#if defined(APH_DEBUG)
{
VK_CHECK_RESULT(
createDebugUtilsMessengerEXT(handle, &createInfo.debugCreateInfo, nullptr, &instance->m_debugMessenger));
createDebugUtilsMessengerEXT(handle, &createInfo.debugCreateInfo, vkAllocator(), &instance->m_debugMessenger));
}
#endif
// Return success.
Expand All @@ -175,8 +175,8 @@ VkResult Instance::Create(const InstanceCreateInfo& createInfo, Instance** ppIns
void Instance::Destroy(Instance* pInstance)
{
#ifdef APH_DEBUG
destroyDebugUtilsMessengerEXT(pInstance->getHandle(), pInstance->m_debugMessenger, nullptr);
destroyDebugUtilsMessengerEXT(pInstance->getHandle(), pInstance->m_debugMessenger, vkAllocator());
#endif
vkDestroyInstance(pInstance->getHandle(), nullptr);
vkDestroyInstance(pInstance->getHandle(), vkAllocator());
}
} // namespace aph::vk
2 changes: 1 addition & 1 deletion engine/api/vulkan/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ VkResult VulkanPipelineBuilder::build(VkDevice device, VkPipelineCache pipelineC
.basePipelineIndex = -1,
};

const auto result = vkCreateGraphicsPipelines(device, pipelineCache, 1, &ci, nullptr, outPipeline);
const auto result = vkCreateGraphicsPipelines(device, pipelineCache, 1, &ci, vkAllocator(), outPipeline);

if(result != VK_SUCCESS)
{
Expand Down
10 changes: 5 additions & 5 deletions engine/api/vulkan/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ static DescriptorSetLayout* createDescriptorSetLayout(Device* m_pDevice, const S
DescriptorSetLayout* setLayout{};
{
VkDescriptorSetLayout vkSetLayout;
VK_CHECK_RESULT(m_pDevice->getDeviceTable()->vkCreateDescriptorSetLayout(m_pDevice->getHandle(), &info, nullptr,
VK_CHECK_RESULT(m_pDevice->getDeviceTable()->vkCreateDescriptorSetLayout(m_pDevice->getHandle(), &info, vkAllocator(),
&vkSetLayout));
setLayout = new DescriptorSetLayout(m_pDevice, info, vkSetLayout);
}
Expand All @@ -207,7 +207,7 @@ std::unique_ptr<Shader> Shader::Create(Device* pDevice, const std::filesystem::p
};

VkShaderModule handle;
VK_CHECK_RESULT(vkCreateShaderModule(pDevice->getHandle(), &createInfo, nullptr, &handle));
VK_CHECK_RESULT(vkCreateShaderModule(pDevice->getHandle(), &createInfo, vkAllocator(), &handle));

auto instance = std::unique_ptr<Shader>(new Shader(std::move(spvCode), handle, entrypoint));
return instance;
Expand Down Expand Up @@ -579,18 +579,18 @@ void ShaderProgram::createPipelineLayout(const ImmutableSamplerBank* samplerBank
#endif

auto table = m_pDevice->getDeviceTable();
if(table->vkCreatePipelineLayout(m_pDevice->getHandle(), &info, nullptr, &m_pipeLayout) != VK_SUCCESS)
if(table->vkCreatePipelineLayout(m_pDevice->getHandle(), &info, vkAllocator(), &m_pipeLayout) != VK_SUCCESS)
VK_LOG_ERR("Failed to create pipeline layout.");
}

ShaderProgram::~ShaderProgram()
{
for(auto* setLayout : m_pSetLayouts)
{
vkDestroyDescriptorSetLayout(m_pDevice->getHandle(), setLayout->getHandle(), nullptr);
vkDestroyDescriptorSetLayout(m_pDevice->getHandle(), setLayout->getHandle(), vkAllocator());
delete setLayout;
}
m_pDevice->getDeviceTable()->vkDestroyPipelineLayout(m_pDevice->getHandle(), m_pipeLayout, nullptr);
m_pDevice->getDeviceTable()->vkDestroyPipelineLayout(m_pDevice->getHandle(), m_pipeLayout, vkAllocator());
}
VkShaderStageFlags ShaderProgram::getConstantShaderStage(uint32_t offset, uint32_t size) const
{
Expand Down
2 changes: 1 addition & 1 deletion engine/api/vulkan/swapChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void SwapChain::reCreate()
.oldSwapchain = VK_NULL_HANDLE,
};

VK_CHECK_RESULT(vkCreateSwapchainKHR(m_pDevice->getHandle(), &swapChainCreateInfo, nullptr, &getHandle()));
VK_CHECK_RESULT(vkCreateSwapchainKHR(m_pDevice->getHandle(), &swapChainCreateInfo, vk::vkAllocator(), &getHandle()));

m_surfaceFormat = swapChainSupport.preferedSurfaceFormat;
m_extent = swapChainSupport.preferedExtent;
Expand Down
12 changes: 6 additions & 6 deletions engine/api/vulkan/syncPrimitivesPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ SyncPrimitivesPool::~SyncPrimitivesPool()
{
// Destroy all created fences.
for(auto* fence : m_allFences)
vkDestroyFence(m_device->getHandle(), fence, nullptr);
vkDestroyFence(m_device->getHandle(), fence, vk::vkAllocator());

// Destroy all created semaphores.
for(auto* semaphore : m_allSemaphores)
vkDestroySemaphore(m_device->getHandle(), semaphore, nullptr);
vkDestroySemaphore(m_device->getHandle(), semaphore, vk::vkAllocator());

for(auto* semaphore : m_allTimelineSemahpores)
vkDestroySemaphore(m_device->getHandle(), semaphore, nullptr);
vkDestroySemaphore(m_device->getHandle(), semaphore, vk::vkAllocator());
}

VkResult SyncPrimitivesPool::acquireFence(VkFence& fence, bool isSignaled)
Expand All @@ -41,7 +41,7 @@ VkResult SyncPrimitivesPool::acquireFence(VkFence& fence, bool isSignaled)
{
createInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
}
result = vkCreateFence(m_device->getHandle(), &createInfo, nullptr, &fence);
result = vkCreateFence(m_device->getHandle(), &createInfo, vk::vkAllocator(), &fence);
if(result == VK_SUCCESS)
m_allFences.emplace(fence);
}
Expand Down Expand Up @@ -95,7 +95,7 @@ VkResult SyncPrimitivesPool::acquireSemaphore(uint32_t semaphoreCount, VkSemapho
{
VkSemaphoreCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
result = vkCreateSemaphore(m_device->getHandle(), &createInfo, nullptr, &pSemaphores[i]);
result = vkCreateSemaphore(m_device->getHandle(), &createInfo, vk::vkAllocator(), &pSemaphores[i]);
if(result != VK_SUCCESS)
break;

Expand Down Expand Up @@ -158,7 +158,7 @@ VkResult SyncPrimitivesPool::acquireTimelineSemaphore(uint32_t semaphoreCount, V
.pNext = &timelineCreateInfo,
.flags = 0,
};
result = vkCreateSemaphore(m_device->getHandle(), &createInfo, nullptr, &pSemaphores[i]);
result = vkCreateSemaphore(m_device->getHandle(), &createInfo, vk::vkAllocator(), &pSemaphores[i]);
if(result != VK_SUCCESS)
{
break;
Expand Down
Loading

0 comments on commit 48e0eba

Please sign in to comment.