Skip to content

Commit

Permalink
Adding a VkLoadTest for suballocation callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchvoltage committed Sep 9, 2023
1 parent c4b3cef commit 01ab174
Show file tree
Hide file tree
Showing 4 changed files with 19,846 additions and 10 deletions.
152 changes: 142 additions & 10 deletions tests/loadtests/vkloadtests/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,112 @@
#include <assert.h>
#include <exception>
#include <vector>
#include <random>
#include <unordered_map>

#include "argparser.h"
#include "Texture.h"
#include "VulkanTextureTranscoder.hpp"
#include "ltexceptions.h"

#define VMA_IMPLEMENTATION
#define VMA_VULKAN_VERSION 1002000
#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 1
#include "vma/vk_mem_alloc.h"

#define VERTEX_BUFFER_BIND_ID 0
#define ENABLE_VALIDATION false

namespace VMA_CALLBACKS
{
VmaAllocator vmaAllocator;
std::mt19937_64 mt64;

void InitVMA(VkPhysicalDevice& physicalDevice, VkDevice& device, VkInstance& instance)
{
VmaVulkanFunctions vulkanFunctions = {};
vulkanFunctions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr;
vulkanFunctions.vkGetDeviceProcAddr = &vkGetDeviceProcAddr;

VmaAllocatorCreateInfo allocatorCreateInfo = {};
allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_2;
allocatorCreateInfo.physicalDevice = physicalDevice;
allocatorCreateInfo.device = device;
allocatorCreateInfo.instance = instance;
allocatorCreateInfo.pVulkanFunctions = &vulkanFunctions;

vmaCreateAllocator(&allocatorCreateInfo, &vmaAllocator);
}

void DestroyVMA()
{
vmaDestroyAllocator(vmaAllocator);
}

struct AllocationInfo
{
VmaAllocation allocation;
VkDeviceSize mapSize;
};
std::unordered_map<uint64_t, AllocationInfo> AllocMemCWrapperDirectory;
uint64_t AllocMemCWrapper(VkMemoryAllocateInfo* allocInfo, VkMemoryRequirements* memReq, uint64_t* numPages)
{
uint64_t allocId = mt64();
VmaAllocationCreateInfo pCreateInfo = {};
if (allocInfo->memoryTypeIndex == 8)
{
pCreateInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
pCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
}
else
{
pCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
}
pCreateInfo.memoryTypeBits = memReq->memoryTypeBits;

VmaAllocation allocation;
VkResult result = vmaAllocateMemory(vmaAllocator, memReq, &pCreateInfo, &allocation, VMA_NULL);
if (result != VK_SUCCESS)
{
return 0ull;
}

AllocMemCWrapperDirectory[allocId].allocation = allocation;
AllocMemCWrapperDirectory[allocId].mapSize = memReq->size;
*numPages = 1ull;

return allocId;
}

VkResult BindBufferMemoryCWrapper(VkBuffer buffer, uint64_t allocId)
{
return vmaBindBufferMemory(vmaAllocator, AllocMemCWrapperDirectory[allocId].allocation, buffer);
}

VkResult BindImageMemoryCWrapper(VkImage image, uint64_t allocId)
{
return vmaBindImageMemory(vmaAllocator, AllocMemCWrapperDirectory[allocId].allocation, image);
}

VkResult MapMemoryCWrapper(uint64_t allocId, uint64_t pageNumber, VkDeviceSize* mapLength, void** dataPtr)
{
*mapLength = AllocMemCWrapperDirectory[allocId].mapSize;
return vmaMapMemory(vmaAllocator, AllocMemCWrapperDirectory[allocId].allocation, dataPtr);
}

void UnmapMemoryCWrapper(uint64_t allocId, uint64_t pageNumber)
{
vmaUnmapMemory(vmaAllocator, AllocMemCWrapperDirectory[allocId].allocation);
}

void FreeMemCWrapper(uint64_t allocId)
{
vmaFreeMemory(vmaAllocator, AllocMemCWrapperDirectory[allocId].allocation);
AllocMemCWrapperDirectory.erase(allocId);
}
}

// Vertex layout for this example
struct Vertex {
std::array<float, 3> pos;
Expand All @@ -65,6 +162,7 @@ Texture::Texture(VulkanContext& vkctx,
zoom = -2.5f;
rotation = { 0.0f, 15.0f, 0.0f };
tiling = vk::ImageTiling::eOptimal;
useSubAlloc = vk::UseSuballocator::No;
rgbcolor upperLeftColor{ 0.7f, 0.1f, 0.2f };
rgbcolor lowerLeftColor{ 0.8f, 0.9f, 0.3f };
rgbcolor upperRightColor{ 0.4f, 1.0f, 0.5f };
Expand Down Expand Up @@ -118,10 +216,28 @@ Texture::Texture(VulkanContext& vkctx,
throw unsupported_ttype();
}

ktxresult = ktxTexture_VkUploadEx(kTexture, &vdi, &texture,
static_cast<VkImageTiling>(tiling),
VK_IMAGE_USAGE_SAMPLED_BIT,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
if (useSubAlloc == vk::UseSuballocator::Yes)
{
VkInstance vkInst = vkctx.instance;
VMA_CALLBACKS::InitVMA(vdi.physicalDevice, vdi.device, vkInst);
ktxVulkanTexture_subAllocatorCallbacks callbacks;
callbacks.allocMemFuncPtr = VMA_CALLBACKS::AllocMemCWrapper;
callbacks.bindBufferFuncPtr = VMA_CALLBACKS::BindBufferMemoryCWrapper;
callbacks.bindImageFuncPtr = VMA_CALLBACKS::BindImageMemoryCWrapper;
callbacks.memoryMapFuncPtr = VMA_CALLBACKS::MapMemoryCWrapper;
callbacks.memoryUnmapFuncPtr = VMA_CALLBACKS::UnmapMemoryCWrapper;
callbacks.freeMemFuncPtr = VMA_CALLBACKS::FreeMemCWrapper;

ktxresult = ktxTexture_VkUploadEx_WithSuballocator(kTexture, &vdi, &texture,
static_cast<VkImageTiling>(tiling),
VK_IMAGE_USAGE_SAMPLED_BIT,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, &callbacks);
}
else
ktxresult = ktxTexture_VkUploadEx(kTexture, &vdi, &texture,
static_cast<VkImageTiling>(tiling),
VK_IMAGE_USAGE_SAMPLED_BIT,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);

if (KTX_SUCCESS != ktxresult) {
std::stringstream message;
Expand Down Expand Up @@ -182,7 +298,7 @@ Texture::Texture(VulkanContext& vkctx,

ktxTexture_Destroy(kTexture);
ktxVulkanDeviceInfo_Destruct(&vdi);

try {
prepare();
} catch (std::exception& e) {
Expand Down Expand Up @@ -222,10 +338,11 @@ Texture::processArgs(std::string sArgs)
{
// Options descriptor
struct argparser::option longopts[] = {
{"external", argparser::option::no_argument, &externalFile, 1},
{"linear-tiling", argparser::option::no_argument, (int*)&tiling, (int)vk::ImageTiling::eLinear},
{"qcolor", argparser::option::required_argument, NULL, 1},
{NULL, argparser::option::no_argument, NULL, 0}
{"external", argparser::option::no_argument, &externalFile, 1},
{"linear-tiling", argparser::option::no_argument, (int*)&tiling, (int)vk::ImageTiling::eLinear},
{"use-vma", argparser::option::no_argument, (int*)&useSubAlloc, (int)vk::UseSuballocator::Yes},
{"qcolor", argparser::option::required_argument, NULL, 1},
{NULL, argparser::option::no_argument, NULL, 0}
};

argvector argv(sArgs);
Expand Down Expand Up @@ -279,7 +396,22 @@ Texture::cleanup()
if (imageView)
vkctx.device.destroyImageView(imageView);

ktxVulkanTexture_Destruct(&texture, vkctx.device, nullptr);

if (useSubAlloc == vk::UseSuballocator::Yes)
{
VkDevice vkDev = vkctx.device;
ktxVulkanTexture_subAllocatorCallbacks callbacks;
callbacks.allocMemFuncPtr = VMA_CALLBACKS::AllocMemCWrapper;
callbacks.bindBufferFuncPtr = VMA_CALLBACKS::BindBufferMemoryCWrapper;
callbacks.bindImageFuncPtr = VMA_CALLBACKS::BindImageMemoryCWrapper;
callbacks.memoryMapFuncPtr = VMA_CALLBACKS::MapMemoryCWrapper;
callbacks.memoryUnmapFuncPtr = VMA_CALLBACKS::UnmapMemoryCWrapper;
callbacks.freeMemFuncPtr = VMA_CALLBACKS::FreeMemCWrapper;
(void)ktxVulkanTexture_Destruct_WithSuballocator(&texture, vkDev, VK_NULL_HANDLE, &callbacks);
VMA_CALLBACKS::DestroyVMA();
}
else
ktxVulkanTexture_Destruct(&texture, vkctx.device, nullptr);

if (pipelines.solid)
vkctx.device.destroyPipeline(pipelines.solid);
Expand Down
1 change: 1 addition & 0 deletions tests/loadtests/vkloadtests/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Texture : public VulkanLoadTestSample
vk::Sampler sampler;
vk::ImageView imageView;
vk::ImageTiling tiling;
vk::UseSuballocator useSubAlloc;
vk::ComponentMapping swizzle;

struct {
Expand Down
4 changes: 4 additions & 0 deletions tests/loadtests/vkloadtests/VulkanLoadTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ const VulkanLoadTests::sampleInvocation siSamples[] = {
"uastc_Iron_Bars_001_normal.ktx2",
"Transcode of UASTC+zstd Compressed KTX2 XY normal map mipmapped"
},
{ Texture::create,
"--use-vma uastc_Iron_Bars_001_normal.ktx2",
"Transcode of UASTC+zstd Compressed KTX2 XY normal map mipmapped, using VMA"
},
{ Texture::create,
"ktx_document_uastc_rdo4_zstd5.ktx2",
"UASTC+rdo+zstd compressed KTX2 RGBA8 mipmapped"
Expand Down
Loading

0 comments on commit 01ab174

Please sign in to comment.