This shows how to create and load a Vulkan image using the Vulkan texture image loading functions.
#include <vulkan/vulkan.h>
#include <ktxvulkan.h>
#include <vulkan/vulkan.hpp>
class Texture {
public:
Texture(const std::string ktxfile);
~Texture();
protected:
vk::PhysicalDevice gpu;
vk::Device device;
vk::Queue queue;
vk::CommandPool commandPool;
cleanup();
prepareSamplerAndView();
};
Texture::Texture(const std::string ktxfile)
{
createVulkanInstance();
findVulkanGpu();
createVulkanSurface();
createVulkanDevice();
prepareVulkanSwapchain();
ktxVulkanDeviceInfo_Construct(&kvdi, gpu, device, queue, commandPool, nullptr);
ktxresult = ktxTexture_CreateFromNamedFile(
(getAssetPath() + ktxfile).c_str(),
KTX_TEXTURE_CREATE_NO_FLAGS,
&kTexture);
std::stringstream message;
message << "Creation of ktxTexture from \"" << getAssetPath()
throw std::runtime_error(message.str());
}
ktxresult = ktxTexture_VkUploadEx(kTexture, &kvdi, &texture,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_SAMPLED_BIT,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
std::stringstream message;
message <<
"ktxTexture_VkUpload failed: " <<
ktxErrorString(ktxresult);
throw std::runtime_error(message.str());
}
char* pValue;
uint32_t valueLen;
&valueLen, (void**)&pValue))
{
char s, t;
if (s == 'l') sign_s = -1;
if (t == 'u') sign_t = -1;
}
}
ktxVulkanDeviceInfo_destruct(&kvdi);
try {
prepareSamplerAndView();
setupDescriptorSetLayout();
setupDescriptorSet();
preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
buildCommandBuffers();
} catch (std::exception&) {
cleanup();
throw;
}
}
Texture::~Texture()
{
cleanup();
}
Texture::cleanup()
{
destroyCommandBuffers();
destroySampler();
destroyImageview();
ktxVulkanTexture_destruct(&texture, device, nullptr);
}
void
Texture::prepareSamplerAndView()
{
vk::SamplerCreateInfo samplerInfo;
samplerInfo.magFilter = vk::Filter::eLinear;
samplerInfo.minFilter = vk::Filter::eLinear;
samplerInfo.mipmapMode = vk::SamplerMipmapMode::eLinear;
samplerInfo.maxLod = texture.levelCount;
samplerInfo.anisotropyEnable = false;
samplerInfo.maxAnisotropy = 1.0;
samplerInfo.borderColor = vk::BorderColor::eFloatOpaqueWhite;
sampler = vkctx.device.createSampler(samplerInfo);
vk::ImageViewCreateInfo viewInfo;
viewInfo.image = texture.image;
viewInfo.format = static_cast<vk::Format>(texture.imageFormat);
viewInfo.viewType = static_cast<vk::ImageViewType>(texture.viewType);
viewInfo.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
viewInfo.subresourceRange.layerCount = texture.layerCount;
viewInfo.subresourceRange.levelCount = texture.levelCount;
imageView = vkctx.device.createImageView(viewInfo);
}
#define ktxTexture_Destroy(This)
Helper for calling the Destroy virtual method of a ktxTexture.
Definition: ktx.h:530
@ KTX_SUCCESS
Definition: ktx.h:170
#define KTX_ORIENTATION_KEY
Key string for standard orientation metadata.
Definition: ktx.h:124
KTX_API const char *KTX_APIENTRY ktxErrorString(KTX_error_code error)
Return a string corresponding to a KTX error code.
Definition: strings.c:59
#define KTX_ORIENTATION2_FMT
Standard KTX 1 format for 2D orientation value.
Definition: ktx.h:149
#define KTX_error_code
For backward compatibility.
Definition: ktx.h:198
Base class representing a texture.
Definition: ktx.h:287
ktxHashList kvDataHead
Head of the hash list of metadata.
Definition: ktx.h:288
Struct for passing information about the Vulkan device on which to create images to the texture image...
Definition: ktxvulkan.h:188
Struct for returning information about the Vulkan texture image created by the ktxTexture_VkUpload* f...
Definition: ktxvulkan.h:108