libktx Reference 4.3.2
Libraries and tools to create and read KTX image texture files.
Loading...
Searching...
No Matches
vkload.cpp

This shows how to create and load a Vulkan image using the Vulkan texture image loading functions.

// Copyright 2018-2020 The Khronos Group Inc.
// SPDX-License-Identifier: Apache-2.0
#include <vulkan/vulkan.h>
#include <ktxvulkan.h>
// Use c++ to keep the example short. Not required.
#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)
{
ktxTexture* kTexture;
KTX_error_code ktxresult;
createVulkanInstance();
findVulkanGpu(); // Find a suitable physical device
createVulkanSurface();
createVulkanDevice();
prepareVulkanSwapchain();
/*
.
.
.
*/
// This structure is used to pass the Vulkan device information to the loader
// with the expectation that app's will typically load many textures.
ktxVulkanDeviceInfo_Construct(&kvdi, gpu, device, queue, commandPool, nullptr);
ktxresult = ktxTexture_CreateFromNamedFile(
(getAssetPath() + ktxfile).c_str(),
KTX_TEXTURE_CREATE_NO_FLAGS,
&kTexture);
if (KTX_SUCCESS != ktxresult) {
std::stringstream message;
message << "Creation of ktxTexture from \"" << getAssetPath()
<< ktxfile << "\" failed: " << ktxErrorString(ktxresult);
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);
if (KTX_SUCCESS != ktxresult) {
std::stringstream message;
message << "ktxTexture_VkUpload failed: " << ktxErrorString(ktxresult);
throw std::runtime_error(message.str());
}
char* pValue;
uint32_t valueLen;
if (KTX_SUCCESS == ktxHashTable_FindValue(&kTexture->kvDataHead,
&valueLen, (void**)&pValue))
{
char s, t;
if (sscanf(pValue, KTX_ORIENTATION2_FMT, &s, &t) == 2) {
if (s == 'l') sign_s = -1;
if (t == 'u') sign_t = -1;
}
}
ktxTexture_Destroy(kTexture);
ktxVulkanDeviceInfo_destruct(&kvdi);
try {
prepareSamplerAndView(); // See below for implementation.
// Setup a layout with, e.g., a binding for a combined image-sampler.
setupDescriptorSetLayout();
// Create a descriptor set and update it with the sampler and image view handles.
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()
{
// Create sampler.
vk::SamplerCreateInfo samplerInfo;
// Set the non-default values
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);
// Create image view.
// Textures are not directly accessed by the shaders and are abstracted
// by image views containing additional information and sub resource
// ranges.
vk::ImageViewCreateInfo viewInfo;
// Set the non-default values.
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);
}
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */
#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