vulkan/include/vulkan_helper.h

65 lines
3.3 KiB
C

#pragma once
#include <assert.h>
inline static constexpr VkDeviceSize roundDownAlignment(VkDeviceSize offset, VkDeviceSize alignment)
{
// must be a power of two
assert(alignment && ((alignment & (alignment - 1)) == 0));
return offset & ~(alignment - 1);
}
inline static constexpr VkDeviceSize roundAlignment(VkDeviceSize offset, VkDeviceSize alignment)
{
// must be a power of two
assert(alignment && ((alignment & (alignment - 1)) == 0));
return (offset + (alignment - 1)) & (-alignment);
}
inline static constexpr void alignMappedMemoryRanges(uint32_t nonCoherentAtomSize,
VkDeviceSize memorySize,
uint32_t count,
VkMappedMemoryRange * ranges)
{
for (uint32_t i = 0; i < count; i++) {
VkDeviceSize alignedOffset{ roundDownAlignment(ranges[i].offset, nonCoherentAtomSize) };
VkDeviceSize alignedSize{ roundAlignment(ranges[i].size + (ranges[i].offset - alignedOffset), nonCoherentAtomSize) };
if (alignedOffset + alignedSize > memorySize) {
alignedSize = VK_WHOLE_SIZE;
}
ranges[i].offset = alignedOffset;
ranges[i].size = alignedSize;
}
}
VkDeviceSize allocateFromMemoryRequirements(VkDevice device,
VkDeviceSize nonCoherentAtomSize,
VkPhysicalDeviceMemoryProperties const & physicalDeviceMemoryProperties,
VkMemoryRequirements const & memoryRequirements,
VkMemoryPropertyFlags memoryPropertyFlags,
VkMemoryAllocateFlags memoryAllocateFlags,
uint32_t count,
VkDeviceMemory * memory,
VkDeviceSize * stride);
VkDeviceSize allocateFromMemoryRequirements2(VkDevice device,
VkDeviceSize nonCoherentAtomSize,
VkPhysicalDeviceMemoryProperties const & physicalDeviceMemoryProperties,
VkMemoryPropertyFlags memoryPropertyFlags,
VkMemoryAllocateFlags memoryAllocateFlags,
uint32_t memoryRequirementsCount,
VkMemoryRequirements const * memoryRequirements,
VkDeviceMemory * memory,
VkDeviceSize * offsets);
void createImageFromFilenameDDS(VkDevice device,
VkQueue queue,
VkCommandBuffer commandBuffer,
VkFence fence,
VkDeviceSize nonCoherentAtomSize,
VkPhysicalDeviceMemoryProperties const & physicalDeviceMemoryProperties,
char const * const filename,
VkImage * outImage,
VkDeviceMemory * outMemory,
VkImageView * outImageView);