File size: 5,820 Bytes
6b0231d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | /* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */
/*
* Copyright 2017-2020 Mark Callow.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @internal
* @class VulkanLoadTestSample
* @~English
*
* @brief Base class for Vulkan texture loading test samples.
*
* @author Mark Callow, github.com/MarkCallow.
*/
#include "VulkanLoadTestSample.h"
#include <random>
#include <unordered_map>
#define VMA_IMPLEMENTATION
#define VMA_VULKAN_VERSION 1000000
#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 1
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4100)
#pragma warning(disable: 4189)
#pragma warning(disable: 4324)
#endif
#include "vma/vk_mem_alloc.h"
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
namespace VMA_CALLBACKS
{
VmaAllocator vmaAllocator;
std::mt19937_64 mt64;
VkPhysicalDeviceMemoryProperties cachedDevMemProps;
void InitVMA(VkPhysicalDevice& physicalDevice, VkDevice& device, VkInstance& instance,
VkPhysicalDeviceMemoryProperties& devMemProps)
{
cachedDevMemProps = devMemProps;
VmaVulkanFunctions vulkanFunctions = {};
vulkanFunctions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr;
vulkanFunctions.vkGetDeviceProcAddr = &vkGetDeviceProcAddr;
VmaAllocatorCreateInfo allocatorCreateInfo = {};
allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_0;
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 ((cachedDevMemProps.memoryTypes[allocInfo->memoryTypeIndex].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) ||
(cachedDevMemProps.memoryTypes[allocInfo->memoryTypeIndex].propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
{
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, VkDeviceSize* mapLength, void** dataPtr)
{
*mapLength = AllocMemCWrapperDirectory[allocId].mapSize;
return vmaMapMemory(vmaAllocator, AllocMemCWrapperDirectory[allocId].allocation, dataPtr);
}
void UnmapMemoryCWrapper(uint64_t allocId, uint64_t)
{
vmaUnmapMemory(vmaAllocator, AllocMemCWrapperDirectory[allocId].allocation);
}
void FreeMemCWrapper(uint64_t allocId)
{
vmaFreeMemory(vmaAllocator, AllocMemCWrapperDirectory[allocId].allocation);
AllocMemCWrapperDirectory.erase(allocId);
}
}
VulkanLoadTestSample::~VulkanLoadTestSample()
{
for (auto& shaderModule : shaderModules)
{
vkctx.device.destroyShaderModule(shaderModule, nullptr);
}
}
void
VulkanLoadTestSample::loadMesh(std::string filename,
vkMeshLoader::MeshBuffer* meshBuffer,
std::vector<vkMeshLoader::VertexLayout> vertexLayout,
float scale)
{
VulkanMeshLoader *mesh = new VulkanMeshLoader();
if (!mesh->LoadMesh(filename)) {
std::stringstream message;
message << "Error reading or parsing mesh file \"" << filename << "\"";
throw std::runtime_error(message.str());
}
assert(mesh->m_Entries.size() > 0);
vk::CommandBuffer copyCmd =
vkctx.createCommandBuffer(vk::CommandBufferLevel::ePrimary, false);
mesh->createBuffers(
vkctx.device,
static_cast<VkPhysicalDeviceMemoryProperties>(vkctx.memoryProperties),
meshBuffer,
vertexLayout,
scale,
true,
copyCmd,
vkctx.queue);
vkctx.device.freeCommandBuffers(vkctx.commandPool, 1, ©Cmd);
meshBuffer->dim = mesh->dim.size;
delete(mesh);
}
vk::PipelineShaderStageCreateInfo
VulkanLoadTestSample::loadShader(std::string filename,
vk::ShaderStageFlagBits stage,
const char* modname)
{
vk::PipelineShaderStageCreateInfo shaderStage = vkctx.loadShader(filename, stage, modname);
assert(shaderStage.module);
shaderModules.push_back(shaderStage.module);
return shaderStage;
}
|