Replaced gli with libktx for mip generation sample

This commit is contained in:
Sascha Willems 2019-08-03 10:20:36 +02:00
parent 0eef7f2ee1
commit 803ccd0cf4

View file

@ -1,13 +1,11 @@
/* /*
* Vulkan Example - Runtime mip map generation * Vulkan Example - Runtime mip map generation
* *
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de * Copyright (C) by Sascha Willems - www.saschawillems.de
* *
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/ */
// todo: Fallback for sampler selection on devices that don't support shaderSampledImageArrayDynamicIndexing
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -19,13 +17,14 @@
#define GLM_FORCE_DEPTH_ZERO_TO_ONE #define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <gli/gli.hpp>
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include "vulkanexamplebase.h" #include "vulkanexamplebase.h"
#include "VulkanDevice.hpp" #include "VulkanDevice.hpp"
#include "VulkanBuffer.hpp" #include "VulkanBuffer.hpp"
#include "VulkanModel.hpp" #include "VulkanModel.hpp"
#include <ktx.h>
#include <ktxvulkan.h>
#define VERTEX_BUFFER_BIND_ID 0 #define VERTEX_BUFFER_BIND_ID 0
#define ENABLE_VALIDATION false #define ENABLE_VALIDATION false
@ -110,13 +109,18 @@ public:
models.tunnel.destroy(); models.tunnel.destroy();
} }
void loadTexture(std::string fileName, VkFormat format, bool forceLinearTiling) void loadTexture(std::string filename, VkFormat format, bool forceLinearTiling)
{ {
ktxResult result;
ktxTexture* ktxTexture;
#if defined(__ANDROID__) #if defined(__ANDROID__)
// Textures are stored inside the apk on Android (compressed) // Textures are stored inside the apk on Android (compressed)
// So they need to be loaded via the asset manager // So they need to be loaded via the asset manager
AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, fileName.c_str(), AASSET_MODE_STREAMING); AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING);
assert(asset); if (!asset) {
vks::tools::exitFatal("Could not load texture from " + filename + "\n\nThe file may be part of the additional asset pack.\n\nRun \"download_assets.py\" in the repository root to download the latest version.", -1);
}
size_t size = AAsset_getLength(asset); size_t size = AAsset_getLength(asset);
assert(size > 0); assert(size > 0);
@ -124,17 +128,23 @@ public:
AAsset_read(asset, textureData, size); AAsset_read(asset, textureData, size);
AAsset_close(asset); AAsset_close(asset);
gli::texture2d tex2D(gli::load((const char*)textureData, size)); result = ktxTexture_CreateFromMemory(textureData, size, KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, target);
#else
gli::texture2d tex2D(gli::load(fileName));
#endif
assert(!tex2D.empty()); free(textureData);
#else
if (!vks::tools::fileExists(filename)) {
vks::tools::exitFatal("Could not load texture from " + filename + "\n\nThe file may be part of the additional asset pack.\n\nRun \"download_assets.py\" in the repository root to download the latest version.", -1);
}
result = ktxTexture_CreateFromNamedFile(filename.c_str(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &ktxTexture);
#endif
assert(result == KTX_SUCCESS);
VkFormatProperties formatProperties; VkFormatProperties formatProperties;
texture.width = static_cast<uint32_t>(tex2D[0].extent().x); texture.width = ktxTexture->baseWidth;
texture.height = static_cast<uint32_t>(tex2D[0].extent().y); texture.height = ktxTexture->baseHeight;
ktx_uint8_t *ktxTextureData = ktxTexture_GetData(ktxTexture);
ktx_size_t ktxTextureSize = ktxTexture_GetImageSize(ktxTexture, 0);
// calculate num of mip maps // calculate num of mip maps
// numLevels = 1 + floor(log2(max(w, h, d))) // numLevels = 1 + floor(log2(max(w, h, d)))
@ -156,7 +166,7 @@ public:
VkDeviceMemory stagingMemory; VkDeviceMemory stagingMemory;
VkBufferCreateInfo bufferCreateInfo = vks::initializers::bufferCreateInfo(); VkBufferCreateInfo bufferCreateInfo = vks::initializers::bufferCreateInfo();
bufferCreateInfo.size = tex2D.size(); bufferCreateInfo.size = ktxTextureSize;
// This buffer is used as a transfer source for the buffer copy // This buffer is used as a transfer source for the buffer copy
bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
@ -170,7 +180,7 @@ public:
// Copy texture data into staging buffer // Copy texture data into staging buffer
uint8_t *data; uint8_t *data;
VK_CHECK_RESULT(vkMapMemory(device, stagingMemory, 0, memReqs.size, 0, (void **)&data)); VK_CHECK_RESULT(vkMapMemory(device, stagingMemory, 0, memReqs.size, 0, (void **)&data));
memcpy(data, tex2D.data(), tex2D.size()); memcpy(data, ktxTextureData, ktxTextureSize);
vkUnmapMemory(device, stagingMemory); vkUnmapMemory(device, stagingMemory);
// Create optimal tiled target image // Create optimal tiled target image
@ -233,6 +243,7 @@ public:
// Clean up staging resources // Clean up staging resources
vkFreeMemory(device, stagingMemory, nullptr); vkFreeMemory(device, stagingMemory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr); vkDestroyBuffer(device, stagingBuffer, nullptr);
ktxTexture_Destroy(ktxTexture);
// Generate the mip chain // Generate the mip chain
// --------------------------------------------------------------- // ---------------------------------------------------------------