diff --git a/examples/texturemipmapgen/texturemipmapgen.cpp b/examples/texturemipmapgen/texturemipmapgen.cpp index 471b1914..1eb40eef 100644 --- a/examples/texturemipmapgen/texturemipmapgen.cpp +++ b/examples/texturemipmapgen/texturemipmapgen.cpp @@ -1,13 +1,11 @@ /* * 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) */ -// todo: Fallback for sampler selection on devices that don't support shaderSampledImageArrayDynamicIndexing - #include #include #include @@ -19,13 +17,14 @@ #define GLM_FORCE_DEPTH_ZERO_TO_ONE #include #include -#include #include #include "vulkanexamplebase.h" #include "VulkanDevice.hpp" #include "VulkanBuffer.hpp" #include "VulkanModel.hpp" +#include +#include #define VERTEX_BUFFER_BIND_ID 0 #define ENABLE_VALIDATION false @@ -110,13 +109,18 @@ public: 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__) // Textures are stored inside the apk on Android (compressed) // So they need to be loaded via the asset manager - AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, fileName.c_str(), AASSET_MODE_STREAMING); - assert(asset); + AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING); + 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); assert(size > 0); @@ -124,17 +128,23 @@ public: AAsset_read(asset, textureData, size); AAsset_close(asset); - gli::texture2d tex2D(gli::load((const char*)textureData, size)); -#else - gli::texture2d tex2D(gli::load(fileName)); -#endif + result = ktxTexture_CreateFromMemory(textureData, size, KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, target); - 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; - texture.width = static_cast(tex2D[0].extent().x); - texture.height = static_cast(tex2D[0].extent().y); + texture.width = ktxTexture->baseWidth; + texture.height = ktxTexture->baseHeight; + ktx_uint8_t *ktxTextureData = ktxTexture_GetData(ktxTexture); + ktx_size_t ktxTextureSize = ktxTexture_GetImageSize(ktxTexture, 0); // calculate num of mip maps // numLevels = 1 + floor(log2(max(w, h, d))) @@ -156,7 +166,7 @@ public: VkDeviceMemory stagingMemory; VkBufferCreateInfo bufferCreateInfo = vks::initializers::bufferCreateInfo(); - bufferCreateInfo.size = tex2D.size(); + bufferCreateInfo.size = ktxTextureSize; // This buffer is used as a transfer source for the buffer copy bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; @@ -170,7 +180,7 @@ public: // Copy texture data into staging buffer uint8_t *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); // Create optimal tiled target image @@ -233,6 +243,7 @@ public: // Clean up staging resources vkFreeMemory(device, stagingMemory, nullptr); vkDestroyBuffer(device, stagingBuffer, nullptr); + ktxTexture_Destroy(ktxTexture); // Generate the mip chain // ---------------------------------------------------------------