vulkan-renderer
vulkan-renderer copied to clipboard
Use initializer methods for Vulkan structs.
@yeetari already demonstrated:
template <>
VkApplicationInfo make_info() {
VkApplicationInfo ret{};
ret.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
return ret;
}
So we can fill out the missing fields:
auto app_info = make_info<VkApplicationInfo>();
app_info.pApplicationName = application_name.c_str();
app_info.applicationVersion = application_version;
app_info.pEngineName = engine_name.c_str();
app_info.engineVersion = engine_version;
app_info.apiVersion = vulkan_api_version;
We should use this for every Vulkan struct.
Sascha Willems example repository uses this as well:
/** @brief Initialize a buffer memory barrier with no image transfer ownership */
inline VkBufferMemoryBarrier bufferMemoryBarrier()
{
VkBufferMemoryBarrier bufferMemoryBarrier {};
bufferMemoryBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
bufferMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
bufferMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
return bufferMemoryBarrier;
}
Which is used like this:
VkBufferMemoryBarrier bufferBarrier = vks::initializers::bufferMemoryBarrier();
bufferBarrier.srcAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
bufferBarrier.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
bufferBarrier.srcQueueFamilyIndex = vulkanDevice->queueFamilyIndices.graphics;
bufferBarrier.dstQueueFamilyIndex = vulkanDevice->queueFamilyIndices.compute;
bufferBarrier.size = VK_WHOLE_SIZE;
Btw reading through Sascha Willem's code: Should we inline our code as well ?
Would it be possible to change the templates so they take the values of the structures as arguments?
So instead of
auto descriptor_set_ai = wrapper::make_info<VkDescriptorSetAllocateInfo>();
descriptor_set_ai.descriptorPool = m_descriptor_pool;
descriptor_set_ai.descriptorSetCount = static_cast<std::uint32_t>(swapchain_image_count);
descriptor_set_ai.pSetLayouts = descriptor_set_layouts.data();
we would use
auto descriptor_set_ai = wrapper::make_info<VkDescriptorSetAllocateInfo>(m_descriptor_pool,
static_cast<std::uint32_t>(swapchain_image_count),
descriptor_set_layouts.data());
This would have the advantage that we can't ignore or forget an argument in the structure because we have to specify it directly when calling make_info. So mistakes like this can't happen:
auto descriptor_set_ai = wrapper::make_info<VkDescriptorSetAllocateInfo>();
// descriptor_set_ai.descriptorPool = m_descriptor_pool; Oops we forgot to specify this value!
descriptor_set_ai.descriptorSetCount = static_cast<std::uint32_t>(swapchain_image_count);
descriptor_set_ai.pSetLayouts = descriptor_set_layouts.data();
I will close this issue because we are using this in the codebase already together with designated initializers. See pull request 487 for example.