TextureLoader now uses VulkanDevice, refactoring, default parameters, documentation

This commit is contained in:
saschawillems 2016-07-23 12:26:49 +02:00
parent 6a9bd4fc55
commit 2889bea4e9
2 changed files with 148 additions and 135 deletions

View file

@ -11,13 +11,17 @@
#include <vulkan/vulkan.h>
#include <gli/gli.hpp>
#include "vulkandevice.hpp"
#if defined(__ANDROID__)
#include <android/asset_manager.h>
#endif
namespace vkTools
{
/**
* @brief Encapsulates a Vulkan texture object (including view, sampler, descriptor, etc.)
*/
struct VulkanTexture
{
VkSampler sampler;
@ -31,53 +35,67 @@ namespace vkTools
VkDescriptorImageInfo descriptor;
};
/**
* @brief A simple Vulkan texture uploader for getting images into GPU memory
*/
class VulkanTextureLoader
{
private:
VkPhysicalDevice physicalDevice;
VkDevice device;
vk::VulkanDevice *vulkanDevice;
VkQueue queue;
VkCommandBuffer cmdBuffer;
VkCommandPool cmdPool;
VkPhysicalDeviceMemoryProperties deviceMemoryProperties;
// Get appropriate memory type index for a memory allocation
uint32_t getMemoryType(uint32_t typeBits, VkFlags properties)
{
for (uint32_t i = 0; i < 32; i++)
{
if ((typeBits & 1) == 1)
{
if ((deviceMemoryProperties.memoryTypes[i].propertyFlags & properties) == properties)
{
return i;
}
}
typeBits >>= 1;
}
// todo : throw error
return 0;
}
public:
#if defined(__ANDROID__)
AAssetManager* assetManager = nullptr;
#endif
// Load a 2D texture
void loadTexture(std::string filename, VkFormat format, VulkanTexture *texture)
/**
* Default constructor
*
* @param vulkanDevice Pointer to a valid VulkanDevice
* @param queue Queue for the copy commands when using staging (queue must support transfers)
* @param cmdPool Commandpool used to get command buffers for copies and layout transitions
*/
VulkanTextureLoader(vk::VulkanDevice *vulkanDevice, VkQueue queue, VkCommandPool cmdPool)
{
loadTexture(filename, format, texture, false);
this->vulkanDevice = vulkanDevice;
this->queue = queue;
this->cmdPool = cmdPool;
// Create command buffer for submitting image barriers
// and converting tilings
VkCommandBufferAllocateInfo cmdBufInfo = {};
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
cmdBufInfo.commandPool = cmdPool;
cmdBufInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cmdBufInfo.commandBufferCount = 1;
VK_CHECK_RESULT(vkAllocateCommandBuffers(vulkanDevice->logicalDevice, &cmdBufInfo, &cmdBuffer));
}
// Load a 2D texture
void loadTexture(std::string filename, VkFormat format, VulkanTexture *texture, bool forceLinear)
/**
* Default destructor
*
* @note Does not free texture resources
*/
~VulkanTextureLoader()
{
loadTexture(filename, format, texture, forceLinear, VK_IMAGE_USAGE_SAMPLED_BIT);
vkFreeCommandBuffers(vulkanDevice->logicalDevice, cmdPool, 1, &cmdBuffer);
}
// Load a 2D texture
void loadTexture(std::string filename, VkFormat format, VulkanTexture *texture, bool forceLinear, VkImageUsageFlags imageUsageFlags)
/**
* Load a 2D texture including all mip levels
*
* @param filename File to load
* @param format Vulkan format of the image data stored in the file
* @param texture Pointer to the texture object to load the image into
* @param (Optional) forceLinear Force linear tiling (not advised, defaults to false)
* @param (Optional) imageUsageFlags Usage flags for the texture's image (defaults to VK_IMAGE_USAGE_SAMPLED_BIT)
*
* @note Only supports .ktx and .dds
*/
void loadTexture(std::string filename, VkFormat format, VulkanTexture *texture, bool forceLinear = false, VkImageUsageFlags imageUsageFlags = VK_IMAGE_USAGE_SAMPLED_BIT)
{
#if defined(__ANDROID__)
assert(assetManager != nullptr);
@ -107,7 +125,7 @@ namespace vkTools
// Get device properites for the requested texture format
VkFormatProperties formatProperties;
vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProperties);
vkGetPhysicalDeviceFormatProperties(vulkanDevice->physicalDevice, format, &formatProperties);
// Only use linear tiling if requested (and supported by the device)
// Support for linear tiling is mostly limited, so prefer to use
@ -135,23 +153,23 @@ namespace vkTools
bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
VK_CHECK_RESULT(vkCreateBuffer(device, &bufferCreateInfo, nullptr, &stagingBuffer));
VK_CHECK_RESULT(vkCreateBuffer(vulkanDevice->logicalDevice, &bufferCreateInfo, nullptr, &stagingBuffer));
// Get memory requirements for the staging buffer (alignment, memory type bits)
vkGetBufferMemoryRequirements(device, stagingBuffer, &memReqs);
vkGetBufferMemoryRequirements(vulkanDevice->logicalDevice, stagingBuffer, &memReqs);
memAllocInfo.allocationSize = memReqs.size;
// Get memory type index for a host visible buffer
memAllocInfo.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &stagingMemory));
VK_CHECK_RESULT(vkBindBufferMemory(device, stagingBuffer, stagingMemory, 0));
VK_CHECK_RESULT(vkAllocateMemory(vulkanDevice->logicalDevice, &memAllocInfo, nullptr, &stagingMemory));
VK_CHECK_RESULT(vkBindBufferMemory(vulkanDevice->logicalDevice, stagingBuffer, stagingMemory, 0));
// Copy texture data into staging buffer
uint8_t *data;
VK_CHECK_RESULT(vkMapMemory(device, stagingMemory, 0, memReqs.size, 0, (void **)&data));
VK_CHECK_RESULT(vkMapMemory(vulkanDevice->logicalDevice, stagingMemory, 0, memReqs.size, 0, (void **)&data));
memcpy(data, tex2D.data(), tex2D.size());
vkUnmapMemory(device, stagingMemory);
vkUnmapMemory(vulkanDevice->logicalDevice, stagingMemory);
// Setup buffer copy regions for each mip level
std::vector<VkBufferImageCopy> bufferCopyRegions;
@ -188,15 +206,15 @@ namespace vkTools
imageCreateInfo.extent = { texture->width, texture->height, 1 };
imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
VK_CHECK_RESULT(vkCreateImage(device, &imageCreateInfo, nullptr, &texture->image));
VK_CHECK_RESULT(vkCreateImage(vulkanDevice->logicalDevice, &imageCreateInfo, nullptr, &texture->image));
vkGetImageMemoryRequirements(device, texture->image, &memReqs);
vkGetImageMemoryRequirements(vulkanDevice->logicalDevice, texture->image, &memReqs);
memAllocInfo.allocationSize = memReqs.size;
memAllocInfo.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &texture->deviceMemory));
VK_CHECK_RESULT(vkBindImageMemory(device, texture->image, texture->deviceMemory, 0));
memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VK_CHECK_RESULT(vkAllocateMemory(vulkanDevice->logicalDevice, &memAllocInfo, nullptr, &texture->deviceMemory));
VK_CHECK_RESULT(vkBindImageMemory(vulkanDevice->logicalDevice, texture->image, texture->deviceMemory, 0));
VkImageSubresourceRange subresourceRange = {};
subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
@ -240,7 +258,7 @@ namespace vkTools
// Create a fence to make sure that the copies have finished before continuing
VkFence copyFence;
VkFenceCreateInfo fenceCreateInfo = vkTools::initializers::fenceCreateInfo(VK_FLAGS_NONE);
VK_CHECK_RESULT(vkCreateFence(device, &fenceCreateInfo, nullptr, &copyFence));
VK_CHECK_RESULT(vkCreateFence(vulkanDevice->logicalDevice, &fenceCreateInfo, nullptr, &copyFence));
VkSubmitInfo submitInfo = vkTools::initializers::submitInfo();
submitInfo.commandBufferCount = 1;
@ -248,13 +266,13 @@ namespace vkTools
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, copyFence));
VK_CHECK_RESULT(vkWaitForFences(device, 1, &copyFence, VK_TRUE, DEFAULT_FENCE_TIMEOUT));
VK_CHECK_RESULT(vkWaitForFences(vulkanDevice->logicalDevice, 1, &copyFence, VK_TRUE, DEFAULT_FENCE_TIMEOUT));
vkDestroyFence(device, copyFence, nullptr);
vkDestroyFence(vulkanDevice->logicalDevice, copyFence, nullptr);
// Clean up staging resources
vkFreeMemory(device, stagingMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vkFreeMemory(vulkanDevice->logicalDevice, stagingMemory, nullptr);
vkDestroyBuffer(vulkanDevice->logicalDevice, stagingBuffer, nullptr);
}
else
{
@ -281,22 +299,22 @@ namespace vkTools
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
// Load mip map level 0 to linear tiling image
VK_CHECK_RESULT(vkCreateImage(device, &imageCreateInfo, nullptr, &mappableImage));
VK_CHECK_RESULT(vkCreateImage(vulkanDevice->logicalDevice, &imageCreateInfo, nullptr, &mappableImage));
// Get memory requirements for this image
// like size and alignment
vkGetImageMemoryRequirements(device, mappableImage, &memReqs);
vkGetImageMemoryRequirements(vulkanDevice->logicalDevice, mappableImage, &memReqs);
// Set memory allocation size to required memory size
memAllocInfo.allocationSize = memReqs.size;
// Get memory type that can be mapped to host memory
memAllocInfo.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
// Allocate host memory
VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &mappableMemory));
VK_CHECK_RESULT(vkAllocateMemory(vulkanDevice->logicalDevice, &memAllocInfo, nullptr, &mappableMemory));
// Bind allocated image for use
VK_CHECK_RESULT(vkBindImageMemory(device, mappableImage, mappableMemory, 0));
VK_CHECK_RESULT(vkBindImageMemory(vulkanDevice->logicalDevice, mappableImage, mappableMemory, 0));
// Get sub resource layout
// Mip map count, array layer, etc.
@ -309,15 +327,15 @@ namespace vkTools
// Get sub resources layout
// Includes row pitch, size offsets, etc.
vkGetImageSubresourceLayout(device, mappableImage, &subRes, &subResLayout);
vkGetImageSubresourceLayout(vulkanDevice->logicalDevice, mappableImage, &subRes, &subResLayout);
// Map image memory
VK_CHECK_RESULT(vkMapMemory(device, mappableMemory, 0, memReqs.size, 0, &data));
VK_CHECK_RESULT(vkMapMemory(vulkanDevice->logicalDevice, mappableMemory, 0, memReqs.size, 0, &data));
// Copy image data into memory
memcpy(data, tex2D[subRes.mipLevel].data(), tex2D[subRes.mipLevel].size());
vkUnmapMemory(device, mappableMemory);
vkUnmapMemory(vulkanDevice->logicalDevice, mappableMemory);
// Linear tiled images don't need to be staged
// and can be directly used as textures
@ -365,7 +383,7 @@ namespace vkTools
sampler.maxAnisotropy = 8;
sampler.anisotropyEnable = VK_TRUE;
sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &texture->sampler));
VK_CHECK_RESULT(vkCreateSampler(vulkanDevice->logicalDevice, &sampler, nullptr, &texture->sampler));
// Create image view
// Textures are not directly accessed by the shaders and
@ -383,7 +401,7 @@ namespace vkTools
// Only set mip map count if optimal tiling is used
view.subresourceRange.levelCount = (useStaging) ? texture->mipLevels : 1;
view.image = texture->image;
VK_CHECK_RESULT(vkCreateImageView(device, &view, nullptr, &texture->view));
VK_CHECK_RESULT(vkCreateImageView(vulkanDevice->logicalDevice, &view, nullptr, &texture->view));
// Fill descriptor image info that can be used for setting up descriptor sets
texture->descriptor.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
@ -391,40 +409,15 @@ namespace vkTools
texture->descriptor.sampler = texture->sampler;
}
// Clean up vulkan resources used by a texture object
void destroyTexture(VulkanTexture texture)
{
vkDestroyImageView(device, texture.view, nullptr);
vkDestroyImage(device, texture.image, nullptr);
vkDestroySampler(device, texture.sampler, nullptr);
vkFreeMemory(device, texture.deviceMemory, nullptr);
}
VulkanTextureLoader(VkPhysicalDevice physicalDevice, VkDevice device, VkQueue queue, VkCommandPool cmdPool)
{
this->physicalDevice = physicalDevice;
this->device = device;
this->queue = queue;
this->cmdPool = cmdPool;
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &deviceMemoryProperties);
// Create command buffer for submitting image barriers
// and converting tilings
VkCommandBufferAllocateInfo cmdBufInfo = {};
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
cmdBufInfo.commandPool = cmdPool;
cmdBufInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cmdBufInfo.commandBufferCount = 1;
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufInfo, &cmdBuffer));
}
~VulkanTextureLoader()
{
vkFreeCommandBuffers(device, cmdPool, 1, &cmdBuffer);
}
// Load a cubemap texture (single file)
/**
* Load a cubemap texture including all mip levels from a single file
*
* @param filename File to load
* @param format Vulkan format of the image data stored in the file
* @param texture Pointer to the texture object to load the image into
*
* @note Only supports .ktx and .dds
*/
void loadCubemap(std::string filename, VkFormat format, VulkanTexture *texture)
{
#if defined(__ANDROID__)
@ -466,23 +459,23 @@ namespace vkTools
bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
VK_CHECK_RESULT(vkCreateBuffer(device, &bufferCreateInfo, nullptr, &stagingBuffer));
VK_CHECK_RESULT(vkCreateBuffer(vulkanDevice->logicalDevice, &bufferCreateInfo, nullptr, &stagingBuffer));
// Get memory requirements for the staging buffer (alignment, memory type bits)
vkGetBufferMemoryRequirements(device, stagingBuffer, &memReqs);
vkGetBufferMemoryRequirements(vulkanDevice->logicalDevice, stagingBuffer, &memReqs);
memAllocInfo.allocationSize = memReqs.size;
// Get memory type index for a host visible buffer
memAllocInfo.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &stagingMemory));
VK_CHECK_RESULT(vkBindBufferMemory(device, stagingBuffer, stagingMemory, 0));
VK_CHECK_RESULT(vkAllocateMemory(vulkanDevice->logicalDevice, &memAllocInfo, nullptr, &stagingMemory));
VK_CHECK_RESULT(vkBindBufferMemory(vulkanDevice->logicalDevice, stagingBuffer, stagingMemory, 0));
// Copy texture data into staging buffer
uint8_t *data;
VK_CHECK_RESULT(vkMapMemory(device, stagingMemory, 0, memReqs.size, 0, (void **)&data));
VK_CHECK_RESULT(vkMapMemory(vulkanDevice->logicalDevice, stagingMemory, 0, memReqs.size, 0, (void **)&data));
memcpy(data, texCube.data(), texCube.size());
vkUnmapMemory(device, stagingMemory);
vkUnmapMemory(vulkanDevice->logicalDevice, stagingMemory);
// Setup buffer copy regions for each face including all of it's miplevels
std::vector<VkBufferImageCopy> bufferCopyRegions;
@ -526,15 +519,15 @@ namespace vkTools
// This flag is required for cube map images
imageCreateInfo.flags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
VK_CHECK_RESULT(vkCreateImage(device, &imageCreateInfo, nullptr, &texture->image));
VK_CHECK_RESULT(vkCreateImage(vulkanDevice->logicalDevice, &imageCreateInfo, nullptr, &texture->image));
vkGetImageMemoryRequirements(device, texture->image, &memReqs);
vkGetImageMemoryRequirements(vulkanDevice->logicalDevice, texture->image, &memReqs);
memAllocInfo.allocationSize = memReqs.size;
memAllocInfo.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &texture->deviceMemory));
VK_CHECK_RESULT(vkBindImageMemory(device, texture->image, texture->deviceMemory, 0));
VK_CHECK_RESULT(vkAllocateMemory(vulkanDevice->logicalDevice, &memAllocInfo, nullptr, &texture->deviceMemory));
VK_CHECK_RESULT(vkBindImageMemory(vulkanDevice->logicalDevice, texture->image, texture->deviceMemory, 0));
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
VK_CHECK_RESULT(vkBeginCommandBuffer(cmdBuffer, &cmdBufInfo));
@ -579,7 +572,7 @@ namespace vkTools
// Create a fence to make sure that the copies have finished before continuing
VkFence copyFence;
VkFenceCreateInfo fenceCreateInfo = vkTools::initializers::fenceCreateInfo(VK_FLAGS_NONE);
VK_CHECK_RESULT(vkCreateFence(device, &fenceCreateInfo, nullptr, &copyFence));
VK_CHECK_RESULT(vkCreateFence(vulkanDevice->logicalDevice, &fenceCreateInfo, nullptr, &copyFence));
VkSubmitInfo submitInfo = vkTools::initializers::submitInfo();
submitInfo.commandBufferCount = 1;
@ -587,9 +580,9 @@ namespace vkTools
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, copyFence));
VK_CHECK_RESULT(vkWaitForFences(device, 1, &copyFence, VK_TRUE, DEFAULT_FENCE_TIMEOUT));
VK_CHECK_RESULT(vkWaitForFences(vulkanDevice->logicalDevice, 1, &copyFence, VK_TRUE, DEFAULT_FENCE_TIMEOUT));
vkDestroyFence(device, copyFence, nullptr);
vkDestroyFence(vulkanDevice->logicalDevice, copyFence, nullptr);
// Create sampler
VkSamplerCreateInfo sampler = vkTools::initializers::samplerCreateInfo();
@ -605,7 +598,7 @@ namespace vkTools
sampler.minLod = 0.0f;
sampler.maxLod = (float)texture->mipLevels;
sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &texture->sampler));
VK_CHECK_RESULT(vkCreateSampler(vulkanDevice->logicalDevice, &sampler, nullptr, &texture->sampler));
// Create image view
VkImageViewCreateInfo view = vkTools::initializers::imageViewCreateInfo();
@ -617,11 +610,11 @@ namespace vkTools
view.subresourceRange.layerCount = 6;
view.subresourceRange.levelCount = texture->mipLevels;
view.image = texture->image;
VK_CHECK_RESULT(vkCreateImageView(device, &view, nullptr, &texture->view));
VK_CHECK_RESULT(vkCreateImageView(vulkanDevice->logicalDevice, &view, nullptr, &texture->view));
// Clean up staging resources
vkFreeMemory(device, stagingMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vkFreeMemory(vulkanDevice->logicalDevice, stagingMemory, nullptr);
vkDestroyBuffer(vulkanDevice->logicalDevice, stagingBuffer, nullptr);
// Fill descriptor image info that can be used for setting up descriptor sets
texture->descriptor.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
@ -629,7 +622,15 @@ namespace vkTools
texture->descriptor.sampler = texture->sampler;
}
// Load an array texture (single file)
/**
* Load a texture array including all mip levels from a single file
*
* @param filename File to load
* @param format Vulkan format of the image data stored in the file
* @param texture Pointer to the texture object to load the image into
*
* @note Only supports .ktx and .dds
*/
void loadTextureArray(std::string filename, VkFormat format, VulkanTexture *texture)
{
#if defined(__ANDROID__)
@ -673,23 +674,23 @@ namespace vkTools
bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
VK_CHECK_RESULT(vkCreateBuffer(device, &bufferCreateInfo, nullptr, &stagingBuffer));
VK_CHECK_RESULT(vkCreateBuffer(vulkanDevice->logicalDevice, &bufferCreateInfo, nullptr, &stagingBuffer));
// Get memory requirements for the staging buffer (alignment, memory type bits)
vkGetBufferMemoryRequirements(device, stagingBuffer, &memReqs);
vkGetBufferMemoryRequirements(vulkanDevice->logicalDevice, stagingBuffer, &memReqs);
memAllocInfo.allocationSize = memReqs.size;
// Get memory type index for a host visible buffer
memAllocInfo.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &stagingMemory));
VK_CHECK_RESULT(vkBindBufferMemory(device, stagingBuffer, stagingMemory, 0));
VK_CHECK_RESULT(vkAllocateMemory(vulkanDevice->logicalDevice, &memAllocInfo, nullptr, &stagingMemory));
VK_CHECK_RESULT(vkBindBufferMemory(vulkanDevice->logicalDevice, stagingBuffer, stagingMemory, 0));
// Copy texture data into staging buffer
uint8_t *data;
VK_CHECK_RESULT(vkMapMemory(device, stagingMemory, 0, memReqs.size, 0, (void **)&data));
VK_CHECK_RESULT(vkMapMemory(vulkanDevice->logicalDevice, stagingMemory, 0, memReqs.size, 0, (void **)&data));
memcpy(data, tex2DArray.data(), static_cast<size_t>(tex2DArray.size()));
vkUnmapMemory(device, stagingMemory);
vkUnmapMemory(vulkanDevice->logicalDevice, stagingMemory);
// Setup buffer copy regions for each layer including all of it's miplevels
std::vector<VkBufferImageCopy> bufferCopyRegions;
@ -730,15 +731,15 @@ namespace vkTools
imageCreateInfo.arrayLayers = texture->layerCount;
imageCreateInfo.mipLevels = texture->mipLevels;
VK_CHECK_RESULT(vkCreateImage(device, &imageCreateInfo, nullptr, &texture->image));
VK_CHECK_RESULT(vkCreateImage(vulkanDevice->logicalDevice, &imageCreateInfo, nullptr, &texture->image));
vkGetImageMemoryRequirements(device, texture->image, &memReqs);
vkGetImageMemoryRequirements(vulkanDevice->logicalDevice, texture->image, &memReqs);
memAllocInfo.allocationSize = memReqs.size;
memAllocInfo.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &texture->deviceMemory));
VK_CHECK_RESULT(vkBindImageMemory(device, texture->image, texture->deviceMemory, 0));
VK_CHECK_RESULT(vkAllocateMemory(vulkanDevice->logicalDevice, &memAllocInfo, nullptr, &texture->deviceMemory));
VK_CHECK_RESULT(vkBindImageMemory(vulkanDevice->logicalDevice, texture->image, texture->deviceMemory, 0));
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
VK_CHECK_RESULT(vkBeginCommandBuffer(cmdBuffer, &cmdBufInfo));
@ -783,7 +784,7 @@ namespace vkTools
// Create a fence to make sure that the copies have finished before continuing
VkFence copyFence;
VkFenceCreateInfo fenceCreateInfo = vkTools::initializers::fenceCreateInfo(VK_FLAGS_NONE);
VK_CHECK_RESULT(vkCreateFence(device, &fenceCreateInfo, nullptr, &copyFence));
VK_CHECK_RESULT(vkCreateFence(vulkanDevice->logicalDevice, &fenceCreateInfo, nullptr, &copyFence));
VkSubmitInfo submitInfo = vkTools::initializers::submitInfo();
submitInfo.commandBufferCount = 1;
@ -791,9 +792,9 @@ namespace vkTools
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, copyFence));
VK_CHECK_RESULT(vkWaitForFences(device, 1, &copyFence, VK_TRUE, DEFAULT_FENCE_TIMEOUT));
VK_CHECK_RESULT(vkWaitForFences(vulkanDevice->logicalDevice, 1, &copyFence, VK_TRUE, DEFAULT_FENCE_TIMEOUT));
vkDestroyFence(device, copyFence, nullptr);
vkDestroyFence(vulkanDevice->logicalDevice, copyFence, nullptr);
// Create sampler
VkSamplerCreateInfo sampler = vkTools::initializers::samplerCreateInfo();
@ -809,7 +810,7 @@ namespace vkTools
sampler.minLod = 0.0f;
sampler.maxLod = (float)texture->mipLevels;
sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &texture->sampler));
VK_CHECK_RESULT(vkCreateSampler(vulkanDevice->logicalDevice, &sampler, nullptr, &texture->sampler));
// Create image view
VkImageViewCreateInfo view = vkTools::initializers::imageViewCreateInfo();
@ -821,17 +822,29 @@ namespace vkTools
view.subresourceRange.layerCount = texture->layerCount;
view.subresourceRange.levelCount = texture->mipLevels;
view.image = texture->image;
VK_CHECK_RESULT(vkCreateImageView(device, &view, nullptr, &texture->view));
VK_CHECK_RESULT(vkCreateImageView(vulkanDevice->logicalDevice, &view, nullptr, &texture->view));
// Clean up staging resources
vkFreeMemory(device, stagingMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vkFreeMemory(vulkanDevice->logicalDevice, stagingMemory, nullptr);
vkDestroyBuffer(vulkanDevice->logicalDevice, stagingBuffer, nullptr);
// Fill descriptor image info that can be used for setting up descriptor sets
texture->descriptor.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
texture->descriptor.imageView = texture->view;
texture->descriptor.sampler = texture->sampler;
}
};
/**
* Free all Vulkan resources used by a texture object
*
* @param texture Texture object whose resources are to be freed
*/
void destroyTexture(VulkanTexture texture)
{
vkDestroyImageView(vulkanDevice->logicalDevice, texture.view, nullptr);
vkDestroyImage(vulkanDevice->logicalDevice, texture.image, nullptr);
vkDestroySampler(vulkanDevice->logicalDevice, texture.sampler, nullptr);
vkFreeMemory(vulkanDevice->logicalDevice, texture.deviceMemory, nullptr);
}
};
};

View file

@ -225,7 +225,7 @@ void VulkanExampleBase::prepare()
// Recreate setup command buffer for derived class
createSetupCommandBuffer();
// Create a simple texture loader class
textureLoader = new vkTools::VulkanTextureLoader(physicalDevice, device, queue, cmdPool);
textureLoader = new vkTools::VulkanTextureLoader(vulkanDevice, queue, cmdPool);
#if defined(__ANDROID__)
textureLoader->assetManager = androidApp->activity->assetManager;
#endif