Ticket #2996: 0009-Add-externalTextureCompressTool-etct-parameter-to-ar.patch

File 0009-Add-externalTextureCompressTool-etct-parameter-to-ar.patch, 9.2 KB (added by BogDan, 9 years ago)
  • new file inaries/system/texturecompress.sh

    From 7c6bd305aba5a808c39db3d793b957f91c78e246 Mon Sep 17 00:00:00 2001
    From: BogDan Vatra <bogdan@kde.org>
    Date: Sun, 8 Feb 2015 20:11:29 +0200
    Subject: [PATCH 9/9] Add externalTextureCompressTool | etct parameter to
     archivebuild
    
    Now the user can compress the textures using any compression he wants.
    Default script uses Mali's etcpack to compress the textures using ETC2.
    
    Uses:
    $ ./binaries/system/pyrogenesis -archivebuild=binaries/data/mods/public -archivebuild-output=temp/public.zip -archivebuild-compress -etct=/path/to/texture/compress/script.sh
    If -etct value is missing pyrogenesis will use the default script (./binaries/system/texturecompress.sh)
    ---
     binaries/system/texturecompress.sh | 64 +++++++++++++++++++++++++
     source/graphics/TextureManager.cpp | 97 +++++++++++++++++++++++++++++++-------
     source/graphics/TextureManager.h   |  2 +-
     source/main.cpp                    | 13 ++++-
     source/ps/ArchiveBuilder.cpp       |  6 +--
     source/ps/ArchiveBuilder.h         |  3 +-
     6 files changed, 162 insertions(+), 23 deletions(-)
     create mode 100755 binaries/system/texturecompress.sh
    
    diff --git a/binaries/system/texturecompress.sh b/binaries/system/texturecompress.sh
    new file mode 100755
    index 0000000..4afa33b
    - +  
     1#!/bin/bash
     2
     3# MALI etcpack script
     4
     5set -e
     6
     7ETCPACKTOOL=`which etcpack`
     8
     9if [ $ETCPACKTOOL == '' ]; then
     10    echo "Can't find etcpack in your path";
     11    exit 1
     12fi
     13
     14INFILE=''
     15OUTFILE=''
     16FORMAT=''
     17MIPMAPS=''
     18while [ $# -gt 0 ]; do
     19    case $1 in
     20        -in )
     21                shift
     22                INFILE=`realpath "$1"`
     23                ;;
     24        -out )
     25                shift
     26                OUTFILE="$1"
     27                ;;
     28        -f )
     29                shift
     30                FORMAT="$1"
     31                ;;
     32        -mipmaps )
     33                MIPMAPS="$1"
     34                ;;
     35        * )
     36                exit 1
     37     esac
     38     shift
     39done
     40
     41if [ "$INFILE" = "" ]; then
     42    echo "Missing -in parameter"
     43    exit 1
     44fi
     45
     46if [ "$OUTFILE" = "" ]; then
     47    echo "Missing -out parameter"
     48    exit 1
     49fi
     50
     51if [ "$FORMAT" = "" ]; then
     52    echo "Missing -f parameter"
     53    exit 1
     54fi
     55
     56INFILEDIR=$(dirname "$INFILE")
     57KTXFILENAME=$(basename "$INFILE")
     58KTXFILENAME="$INFILEDIR/${KTXFILENAME%.*}.ktx"
     59
     60$ETCPACKTOOL "$INFILE" "$INFILEDIR" -c etc2 -ktx -f "$FORMAT" "$MIPMAPS"
     61
     62mv "$KTXFILENAME" "$OUTFILE"
     63
     64exit 0
  • source/graphics/TextureManager.cpp

    diff --git a/source/graphics/TextureManager.cpp b/source/graphics/TextureManager.cpp
    index bb209af..044b3e0 100644
    a b public:  
    307307        m_TextureConverter.ConvertTexture(texture, sourcePath, looseCachePath, settings);
    308308    }
    309309
    310     bool GenerateCachedTexture(const VfsPath& sourcePath, VfsPath& archiveCachePath)
     310    bool ExternalCompressTool(const VfsPath &in, const VfsPath &out, const CTextureConverter::Settings &settings, const Path &compressTool)
     311    {
     312        bool hasAlpha = false;
     313        {
     314            shared_ptr<u8> file;
     315            size_t fileSize;
     316            if (m_VFS->LoadFile(in, file, fileSize) < 0)
     317            {
     318                LOGERROR("Failed to load texture \"%s\"", in.string8());
     319                return false;
     320            }
     321
     322            Tex tex;
     323            if (tex.decode(file, fileSize) < 0)
     324            {
     325                LOGERROR("Failed to decode texture \"%s\"", in.string8());
     326                return false;
     327            }
     328            hasAlpha = tex.hasAlpha();
     329            if (hasAlpha && tex.m_Bpp == 32)
     330            {
     331                hasAlpha = false;
     332                u8* data = tex.get_data();
     333                for (size_t i = 0; i < tex.m_Width * tex.m_Height; ++i)
     334                {
     335                    if (data[i*4+3] != 0xFF)
     336                    {
     337                        hasAlpha = true;
     338                        break;
     339                    }
     340                }
     341            }
     342        }
     343
     344        Path inPath;
     345        Path outPath;
     346        {
     347            m_VFS->GetRealPath(in, inPath);
     348            shared_ptr<u8> dummy;
     349            m_VFS->CreateFile(out, dummy, 0);
     350            m_VFS->GetRealPath(out, outPath);
     351        }
     352        std::string command(compressTool.string8() + " -in \"");
     353        command += inPath.string8() + "\" -out \"" + outPath.string8() + "\"";
     354        if (settings.mipmap == CTextureConverter::MIP_TRUE)
     355            command += " -mipmaps";
     356        if (hasAlpha)
     357            command += " -f RGBA";
     358        else
     359            command += " -f RGB";
     360
     361        debug_printf(L"Executing %hs\n", command.c_str());
     362        int res = system(command.c_str());
     363        ENSURE(res == 0);
     364        return res == 0;
     365    }
     366
     367    bool GenerateCachedTexture(const VfsPath& sourcePath, VfsPath& archiveCachePath, const Path &compressTool)
    311368    {
    312369        archiveCachePath = m_CacheLoader.ArchiveCachePath(sourcePath);
    313370
    314371        CTextureProperties textureProps(sourcePath);
    315372        CTexturePtr texture = CreateTexture(textureProps);
    316373        CTextureConverter::Settings settings = GetConverterSettings(texture);
    317 
    318         if (!m_TextureConverter.ConvertTexture(texture, sourcePath, VfsPath("cache") / archiveCachePath, settings))
    319             return false;
    320 
    321         while (true)
    322         {
    323             CTexturePtr textureOut;
    324             VfsPath dest;
    325             bool ok;
    326             if (m_TextureConverter.Poll(textureOut, dest, ok))
    327                 return ok;
    328 
    329             // Spin-loop is dumb but it works okay for now
    330             SDL_Delay(0);
     374        if (compressTool.empty() ||
     375                settings.format == CTextureConverter::FMT_ALPHA ||
     376                !(settings.format & (CTextureConverter::FMT_DXT1 |
     377                                         CTextureConverter::FMT_DXT3 |
     378                                         CTextureConverter::FMT_DXT5))) {
     379            if (!m_TextureConverter.ConvertTexture(texture, sourcePath, VfsPath("cache") / archiveCachePath, settings))
     380                return false;
     381
     382            while (true)
     383            {
     384                CTexturePtr textureOut;
     385                VfsPath dest;
     386                bool ok;
     387                if (m_TextureConverter.Poll(textureOut, dest, ok))
     388                    return ok;
     389
     390                // Spin-loop is dumb but it works okay for now
     391                SDL_Delay(0);
     392            }
    331393        }
     394        return ExternalCompressTool(sourcePath, VfsPath("cache") / archiveCachePath, settings, compressTool);
    332395    }
    333396
    334397    bool MakeProgress()
    bool CTextureManager::MakeProgress()  
    652715    return m->MakeProgress();
    653716}
    654717
    655 bool CTextureManager::GenerateCachedTexture(const VfsPath& path, VfsPath& outputPath)
     718bool CTextureManager::GenerateCachedTexture(const VfsPath& path, VfsPath& outputPath, const Path &compressTool)
    656719{
    657     return m->GenerateCachedTexture(path, outputPath);
     720    return m->GenerateCachedTexture(path, outputPath, compressTool);
    658721}
    659722
    660723size_t CTextureManager::GetBytesUploaded() const
  • source/graphics/TextureManager.h

    diff --git a/source/graphics/TextureManager.h b/source/graphics/TextureManager.h
    index 296a3c8..a3914f2 100644
    a b public:  
    109109     * is intended for pre-caching textures in release archives.
    110110     * @return true on success
    111111     */
    112     bool GenerateCachedTexture(const VfsPath& path, VfsPath& outputPath);
     112    bool GenerateCachedTexture(const VfsPath& path, VfsPath& outputPath, const Path &compressTool);
    113113
    114114    /**
    115115     * Returns total number of bytes uploaded for all current texture.
  • source/main.cpp

    diff --git a/source/main.cpp b/source/main.cpp
    index f7053aa..ae7ed47 100644
    a b static void RunGameOrAtlas(int argc, const char* argv[])  
    476476        else
    477477            zip = mod.Filename().ChangeExtension(L".zip");
    478478
    479         CArchiveBuilder builder(mod, paths.Cache(), args.Has("etc2") ? CArchiveBuilder::ETC2 : CArchiveBuilder::S3TC);
     479        OsPath compressTool;
     480        if (args.Has("externalTextureCompressTool")) {
     481            compressTool = args.Get("externalTextureCompressTool");
     482            if (compressTool.empty())
     483                compressTool = args.GetArg0().Parent() / L"texturecompress.sh";
     484        }
     485        if (args.Has("etct")) {
     486            compressTool = args.Get("etct");
     487            if (compressTool.empty())
     488                compressTool = args.GetArg0().Parent() / L"texturecompress.sh";
     489        }
     490        CArchiveBuilder builder(mod, paths.Cache(), compressTool);
    480491
    481492        // Add mods provided on the command line
    482493        // NOTE: We do not handle mods in the user mod path here
  • source/ps/ArchiveBuilder.cpp

    diff --git a/source/ps/ArchiveBuilder.cpp b/source/ps/ArchiveBuilder.cpp
    index 4adb307..20848d2 100644
    a b  
    2828
    2929#include <boost/algorithm/string.hpp>
    3030
    31 CArchiveBuilder::CArchiveBuilder(const OsPath& mod, const OsPath& tempdir) :
    32     m_TempDir(tempdir), m_NumBaseMods(0)
     31CArchiveBuilder::CArchiveBuilder(const OsPath& mod, const OsPath& tempdir, const OsPath &compressTool) :
     32    m_TempDir(tempdir), m_NumBaseMods(0), m_CompressTextureTool(compressTool)
    3333{
    3434    m_VFS = CreateVfs(20*MiB);
    3535
    void CArchiveBuilder::Build(const OsPath& archive, bool compress)  
    9898        {
    9999            VfsPath cachedPath;
    100100            debug_printf(L"Converting texture %ls\n", realPath.string().c_str());
    101             bool ok = textureManager.GenerateCachedTexture(path, cachedPath);
     101            bool ok = textureManager.GenerateCachedTexture(path, cachedPath, m_CompressTextureTool );
    102102            ENSURE(ok);
    103103
    104104            OsPath cachedRealPath;
  • source/ps/ArchiveBuilder.h

    diff --git a/source/ps/ArchiveBuilder.h b/source/ps/ArchiveBuilder.h
    index 3e578e6..133d111 100644
    a b public:  
    3636     * @param mod path to data/mods/foo directory, containing files for conversion
    3737     * @param tempdir path to a writable directory for temporary files
    3838     */
    39     CArchiveBuilder(const OsPath& mod, const OsPath& tempdir);
     39    CArchiveBuilder(const OsPath& mod, const OsPath& tempdir, const OsPath& compressTool);
    4040
    4141    ~CArchiveBuilder();
    4242
    private:  
    6464    std::vector<VfsPath> m_Files;
    6565    OsPath m_TempDir;
    6666    size_t m_NumBaseMods;
     67    const OsPath& m_CompressTextureTool;
    6768};
    6869
    6970#endif // INCLUDED_ARCHIVEBUILDER