restructure allocation strategy for small allocations
continuous-integration/drone/push Build is passing Details

pull/51/head
cosmonaut 2024-03-02 00:56:46 -08:00
parent 0994ac152e
commit 2c77bf96d9
1 changed files with 108 additions and 84 deletions

View File

@ -74,9 +74,9 @@ typedef struct VulkanExtensions
/* Defines */
#define STARTING_ALLOCATION_SIZE 67108864 /* 64MiB */
#define MAX_ALLOCATION_SIZE 268435456 /* 256MiB */
#define ALLOCATION_INCREMENT 16777216 /* 16MiB */
#define SMALL_ALLOCATION_THRESHOLD 1048576 /* 1 MiB */
#define SMALL_ALLOCATION_SIZE 16777216 /* 16 MiB */
#define LARGE_ALLOCATION_INCREMENT 67108864 /* 64 MiB */
#define UBO_BUFFER_SIZE 16777216 /* 16 MiB */
#define MAX_UBO_SECTION_SIZE 4096 /* 4 KiB */
#define DESCRIPTOR_POOL_STARTING_SIZE 128
@ -424,7 +424,6 @@ typedef struct VulkanMemoryUsedRegion
typedef struct VulkanMemorySubAllocator
{
uint32_t memoryTypeIndex;
VkDeviceSize nextAllocationSize;
VulkanMemoryAllocation **allocations;
uint32_t allocationCount;
VulkanMemoryFreeRegion **sortedFreeRegions;
@ -2626,13 +2625,15 @@ static uint8_t VULKAN_INTERNAL_BindResourceMemory(
VulkanMemoryAllocation *allocation;
VulkanMemorySubAllocator *allocator;
VulkanMemoryFreeRegion *region;
VulkanMemoryFreeRegion *selectedRegion;
VulkanMemoryUsedRegion *usedRegion;
VkDeviceSize requiredSize, allocationSize;
VkDeviceSize alignedOffset;
uint32_t newRegionSize, newRegionOffset;
uint8_t shouldAllocDedicated = forceDedicated;
uint8_t isHostVisible, allocationResult;
uint8_t isHostVisible, smallAllocation, allocationResult;
int32_t i;
isHostVisible =
(renderer->memoryProperties.memoryTypes[memoryTypeIndex].propertyFlags &
@ -2640,6 +2641,7 @@ static uint8_t VULKAN_INTERNAL_BindResourceMemory(
allocator = &renderer->memoryAllocator->subAllocators[memoryTypeIndex];
requiredSize = memoryRequirements->memoryRequirements.size;
smallAllocation = requiredSize < SMALL_ALLOCATION_THRESHOLD;
if ( (buffer == VK_NULL_HANDLE && image == VK_NULL_HANDLE) ||
(buffer != VK_NULL_HANDLE && image != VK_NULL_HANDLE) )
@ -2650,11 +2652,25 @@ static uint8_t VULKAN_INTERNAL_BindResourceMemory(
SDL_LockMutex(renderer->allocatorLock);
/* find the largest free region and use it */
if (!shouldAllocDedicated && allocator->sortedFreeRegionCount > 0)
selectedRegion = NULL;
if (!shouldAllocDedicated)
{
region = allocator->sortedFreeRegions[0];
allocation = region->allocation;
for (i = allocator->sortedFreeRegionCount - 1; i >= 0; i -= 1)
{
region = allocator->sortedFreeRegions[i];
if (smallAllocation && region->allocation->size != SMALL_ALLOCATION_SIZE)
{
/* region is not in a small allocation */
continue;
}
if (!smallAllocation && region->allocation->size == SMALL_ALLOCATION_SIZE)
{
/* allocation is not small and current region is in a small allocation */
continue;
}
alignedOffset = VULKAN_INTERNAL_NextHighestAlignment(
region->offset,
@ -2663,6 +2679,17 @@ static uint8_t VULKAN_INTERNAL_BindResourceMemory(
if (alignedOffset + requiredSize <= region->offset + region->size)
{
selectedRegion = region;
break;
}
}
}
if (selectedRegion != NULL)
{
region = selectedRegion;
allocation = region->allocation;
usedRegion = VULKAN_INTERNAL_NewMemoryUsedRegion(
renderer,
allocation,
@ -2730,10 +2757,8 @@ static uint8_t VULKAN_INTERNAL_BindResourceMemory(
*pMemoryUsedRegion = usedRegion;
return 1;
}
}
/* No suitable free regions exist, allocate a new memory region */
if (
!shouldAllocDedicated &&
renderer->allocationsToDefragCount == 0 &&
@ -2747,15 +2772,15 @@ static uint8_t VULKAN_INTERNAL_BindResourceMemory(
{
allocationSize = requiredSize;
}
else if (requiredSize > allocator->nextAllocationSize)
else if (requiredSize > SMALL_ALLOCATION_THRESHOLD)
{
/* allocate a page of required size aligned to ALLOCATION_INCREMENT increments */
/* allocate a page of required size aligned to LARGE_ALLOCATION_INCREMENT increments */
allocationSize =
VULKAN_INTERNAL_NextHighestAlignment(requiredSize, ALLOCATION_INCREMENT);
VULKAN_INTERNAL_NextHighestAlignment(requiredSize, LARGE_ALLOCATION_INCREMENT);
}
else
{
allocationSize = allocator->nextAllocationSize;
allocationSize = SMALL_ALLOCATION_SIZE;
}
allocationResult = VULKAN_INTERNAL_AllocateMemory(
@ -11543,7 +11568,6 @@ static Refresh_Device* VULKAN_CreateDevice(
for (i = 0; i < VK_MAX_MEMORY_TYPES; i += 1)
{
renderer->memoryAllocator->subAllocators[i].memoryTypeIndex = i;
renderer->memoryAllocator->subAllocators[i].nextAllocationSize = STARTING_ALLOCATION_SIZE;
renderer->memoryAllocator->subAllocators[i].allocations = NULL;
renderer->memoryAllocator->subAllocators[i].allocationCount = 0;
renderer->memoryAllocator->subAllocators[i].sortedFreeRegions = SDL_malloc(