Skip to content

Commit

Permalink
Merge pull request zeux#264 from zeux/gltf-cmd
Browse files Browse the repository at this point in the history
gltfpack: Fix various issues with texture compression
  • Loading branch information
zeux authored Apr 7, 2021
2 parents af8ace2 + e76a227 commit 0b75d7f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
13 changes: 5 additions & 8 deletions gltf/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ var interface = {
fs.writeFileSync(path, data);
},
execute: function (command) {
var arg = command.split(' ');
var exe = arg.shift();

// perform substitution of command executable with environment-specific paths
var pk = Object.keys(paths);
for (var pi = 0; pi < pk.length; ++pi) {
if (command.startsWith(pk[pi] + " ")) {
command = paths[pk[pi]] + command.substr(pk[pi].length);
break;
}
}
exe = paths[exe] || exe;

var ret = cp.spawnSync(command, [], {shell:true});
var ret = cp.spawnSync(exe, arg);
return ret.status == null ? 256 : ret.status;
},
unlink: function (path) {
Expand Down
1 change: 1 addition & 0 deletions gltf/gltfpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ int gltfpack(const char* input, const char* output, const char* report, Settings
else if (!checkBasis(settings.verbose > 1))
{
fprintf(stderr, "Error: toktx is not present in PATH or TOKTX_PATH is not set\n");
fprintf(stderr, "Note: toktx must be installed manually from https://github.com/KhronosGroup/KTX-Software/releases\n");
return 3;
}

Expand Down
33 changes: 19 additions & 14 deletions gltf/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ static int execute(const char* cmd, bool ignore_stdout, bool ignore_stderr)
return system(cmd);
}

static const char* readenv(const char* name)
static std::string getExecutable(const char* name, const char* env)
{
return NULL;
return name;
}
#else
static int execute(const char* cmd_, bool ignore_stdout, bool ignore_stderr)
Expand All @@ -81,20 +81,28 @@ static int execute(const char* cmd_, bool ignore_stdout, bool ignore_stderr)
return system(cmd.c_str());
}

static const char* readenv(const char* name)
static std::string getExecutable(const char* name, const char* env)
{
return getenv(name);
const char* path = getenv(env);
path = path ? path : name;

#ifdef _WIN32
// when the executable path contains a space, we need to quote the path ourselves
if (path[0] != '"' && strchr(path, ' '))
return '"' + std::string(path) + '"';
#endif

return path;
}
#endif

bool checkBasis(bool verbose)
{
const char* basisu_path = readenv("BASISU_PATH");
std::string cmd = basisu_path ? basisu_path : "basisu";
std::string cmd = getExecutable("basisu", "BASISU_PATH");

cmd += " -version";

int rc = execute(cmd.c_str(), /* ignore_stdout= */ true, /* ignore_stderr= */ true);
int rc = execute(cmd.c_str(), /* ignore_stdout= */ !verbose, /* ignore_stderr= */ !verbose);
if (verbose)
printf("%s => %d\n", cmd.c_str(), rc);

Expand All @@ -112,8 +120,7 @@ bool encodeBasis(const std::string& data, const char* mime_type, std::string& re
if (!writeFile(temp_input.path.c_str(), data))
return false;

const char* basisu_path = readenv("BASISU_PATH");
std::string cmd = basisu_path ? basisu_path : "basisu";
std::string cmd = getExecutable("basisu", "BASISU_PATH");

const BasisSettings& bs = kBasisSettings[quality <= 0 ? 0 : quality > 9 ? 9 : quality - 1];

Expand Down Expand Up @@ -160,12 +167,11 @@ bool encodeBasis(const std::string& data, const char* mime_type, std::string& re

bool checkKtx(bool verbose)
{
const char* toktx_path = readenv("TOKTX_PATH");
std::string cmd = toktx_path ? toktx_path : "toktx";
std::string cmd = getExecutable("toktx", "TOKTX_PATH");

cmd += " --version";

int rc = execute(cmd.c_str(), /* ignore_stdout= */ true, /* ignore_stderr= */ true);
int rc = execute(cmd.c_str(), /* ignore_stdout= */ !verbose, /* ignore_stderr= */ !verbose);
if (verbose)
printf("%s => %d\n", cmd.c_str(), rc);

Expand Down Expand Up @@ -285,8 +291,7 @@ bool encodeKtx(const std::string& data, const char* mime_type, std::string& resu
if (!writeFile(temp_input.path.c_str(), data))
return false;

const char* toktx_path = readenv("TOKTX_PATH");
std::string cmd = toktx_path ? toktx_path : "toktx";
std::string cmd = getExecutable("toktx", "TOKTX_PATH");

const BasisSettings& bs = kBasisSettings[quality <= 0 ? 0 : quality > 9 ? 9 : quality - 1];

Expand Down

0 comments on commit 0b75d7f

Please sign in to comment.