|
| 1 | +#include "ge_compressor_astc_4x4.hpp" |
| 2 | + |
| 3 | +#include "ge_main.hpp" |
| 4 | +#include "ge_vulkan_command_loader.hpp" |
| 5 | +#include "ge_vulkan_features.hpp" |
| 6 | + |
| 7 | +#include <algorithm> |
| 8 | +#include <cassert> |
| 9 | +#include <cstdio> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +#ifdef ENABLE_LIBASTCENC |
| 13 | +#include <astcenc.h> |
| 14 | +#include <SDL_cpuinfo.h> |
| 15 | +#endif |
| 16 | + |
| 17 | +namespace GE |
| 18 | +{ |
| 19 | +// ============================================================================ |
| 20 | +#ifdef ENABLE_LIBASTCENC |
| 21 | +namespace GEVulkanFeatures |
| 22 | +{ |
| 23 | +extern bool g_supports_astc_4x4; |
| 24 | +} |
| 25 | + |
| 26 | +std::vector<astcenc_context*> g_astc_contexts; |
| 27 | +#endif |
| 28 | +// ============================================================================ |
| 29 | +void GECompressorASTC4x4::init() |
| 30 | +{ |
| 31 | +#ifdef ENABLE_LIBASTCENC |
| 32 | + if (!GEVulkanFeatures::g_supports_astc_4x4) |
| 33 | + return; |
| 34 | + |
| 35 | + // Check for neon existence because libastcenc doesn't do that |
| 36 | + // x86 will exit in astcenc_context_alloc if sse2 / sse4.1 is not supported |
| 37 | +#if defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined (_M_ARM64) |
| 38 | + if (SDL_HasNEON() == SDL_FALSE) |
| 39 | + return; |
| 40 | +#endif |
| 41 | + |
| 42 | + astcenc_config cfg = {}; |
| 43 | + float quality = ASTCENC_PRE_FASTEST; |
| 44 | + if (astcenc_config_init(ASTCENC_PRF_LDR, 4, 4, 1, quality, 0, &cfg) != |
| 45 | + ASTCENC_SUCCESS) |
| 46 | + return; |
| 47 | + |
| 48 | + for (unsigned i = 0; i < GEVulkanCommandLoader::getLoaderCount(); i++) |
| 49 | + { |
| 50 | + astcenc_context* context = NULL; |
| 51 | + if (astcenc_context_alloc(&cfg, 1, &context) != ASTCENC_SUCCESS) |
| 52 | + { |
| 53 | + destroy(); |
| 54 | + return; |
| 55 | + } |
| 56 | + g_astc_contexts.push_back(context); |
| 57 | + } |
| 58 | +#endif |
| 59 | +} // init |
| 60 | + |
| 61 | +// ============================================================================ |
| 62 | +void GECompressorASTC4x4::destroy() |
| 63 | +{ |
| 64 | +#ifdef ENABLE_LIBASTCENC |
| 65 | + for (astcenc_context* context : g_astc_contexts) |
| 66 | + astcenc_context_free(context); |
| 67 | + g_astc_contexts.clear(); |
| 68 | +#endif |
| 69 | +} // destroy |
| 70 | + |
| 71 | +// ============================================================================ |
| 72 | +bool GECompressorASTC4x4::loaded() |
| 73 | +{ |
| 74 | +#ifdef ENABLE_LIBASTCENC |
| 75 | + return !g_astc_contexts.empty(); |
| 76 | +#else |
| 77 | + return false; |
| 78 | +#endif |
| 79 | +} // loaded |
| 80 | + |
| 81 | +// ---------------------------------------------------------------------------- |
| 82 | +GECompressorASTC4x4::GECompressorASTC4x4(uint8_t* texture, unsigned channels, |
| 83 | + const irr::core::dimension2d<irr::u32>& size, |
| 84 | + bool normal_map) |
| 85 | + : GEMipmapGenerator(texture, channels, size, normal_map) |
| 86 | +{ |
| 87 | +#ifdef ENABLE_LIBASTCENC |
| 88 | + assert(channels == 4); |
| 89 | + size_t total_size = 0; |
| 90 | + m_mipmap_sizes = 0; |
| 91 | + for (unsigned i = 0; i < m_levels.size(); i++) |
| 92 | + { |
| 93 | + GEImageLevel& level = m_levels[i]; |
| 94 | + unsigned cur_size = get4x4CompressedTextureSize(level.m_dim.Width, |
| 95 | + level.m_dim.Height); |
| 96 | + total_size += cur_size; |
| 97 | + if (i > 0) |
| 98 | + m_mipmap_sizes += cur_size; |
| 99 | + } |
| 100 | + |
| 101 | + std::vector<GEImageLevel> compressed_levels; |
| 102 | + m_compressed_data = new uint8_t[total_size]; |
| 103 | + uint8_t* cur_offset = m_compressed_data; |
| 104 | + |
| 105 | + for (GEImageLevel& level : m_levels) |
| 106 | + { |
| 107 | + astcenc_image img; |
| 108 | + img.dim_x = level.m_dim.Width; |
| 109 | + img.dim_y = level.m_dim.Height; |
| 110 | + img.dim_z = 1; |
| 111 | + img.data_type = ASTCENC_TYPE_U8; |
| 112 | + img.data = &level.m_data; |
| 113 | + |
| 114 | + astcenc_swizzle swizzle; |
| 115 | + swizzle.r = ASTCENC_SWZ_R; |
| 116 | + swizzle.g = ASTCENC_SWZ_G; |
| 117 | + swizzle.b = ASTCENC_SWZ_B; |
| 118 | + swizzle.a = ASTCENC_SWZ_A; |
| 119 | + |
| 120 | + unsigned cur_size = get4x4CompressedTextureSize(level.m_dim.Width, |
| 121 | + level.m_dim.Height); |
| 122 | + if (astcenc_compress_image( |
| 123 | + g_astc_contexts[GEVulkanCommandLoader::getLoaderId()], &img, |
| 124 | + &swizzle, cur_offset, cur_size, 0) != ASTCENC_SUCCESS) |
| 125 | + printf("astcenc_compress_image failed!\n"); |
| 126 | + compressed_levels.push_back({ level.m_dim, cur_size, cur_offset }); |
| 127 | + cur_offset += cur_size; |
| 128 | + } |
| 129 | + freeMipmapCascade(); |
| 130 | + std::swap(compressed_levels, m_levels); |
| 131 | +#endif |
| 132 | +} // GECompressorASTC4x4 |
| 133 | + |
| 134 | +} |
0 commit comments