Skip to content

Commit

Permalink
tools: Add initial basic texture analysis script
Browse files Browse the repository at this point in the history
In order to calibrate the texture compression settings better, we need a
way to analyze a few representative files and plot the dependency
between quality/size and compression parameters. This version focuses on
UASTC and UASTC RDO lambda specifically as it's the most sensitive
parameter.
  • Loading branch information
zeux committed Jun 19, 2024
1 parent 5158851 commit 79640b8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Empty file modified tools/bitmask.py
100755 → 100644
Empty file.
61 changes: 61 additions & 0 deletions tools/gltfbasis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/python3

import argparse
import matplotlib.pyplot as plt
import os
import os.path
import re
import subprocess

argp = argparse.ArgumentParser()
argp.add_argument('--basisu', type=str, required=True)
argp.add_argument('files', type=str, nargs='+')
args = argp.parse_args()

def compress(path, flags):
temp_path = "/dev/null" if os.name == "posix" else "NUL"
output = subprocess.check_output([args.basisu, "-file", path, "-output_file", temp_path, "-stats", "-ktx2"] + flags)
for line in output.splitlines():
if m := re.match(r".*source image.*?(\d+)x(\d+)", line.decode()):
pixels = int(m.group(1)) * int(m.group(2))
elif m := re.match(r".*Compression succeeded.*size (\d+) bytes", line.decode()):
bytes = int(m.group(1))
elif m := re.match(r"\.basis RGB Avg:.*RMS: (\d+\.\d+) PSNR: (\d+\.\d+)", line.decode()):
rms = float(m.group(1))
psnr = float(m.group(2))

return {'path': path, 'bpp': bytes * 8 / pixels, 'rms': rms, 'psnr': psnr}

def uastc_stats(path):
results = []
for rdo_l in range(0, 20):
flags = ["-uastc", "-uastc_level", "1", "-uastc_rdo_l", str(rdo_l / 5), "-uastc_rdo_d", "1024"]
res = compress(path, flags)
res['rdo_l'] = rdo_l / 5
results.append(res)
return results

plt.figure(figsize=(15, 5))

for path in args.files:
print('Processing', path)
results = uastc_stats(path)
name = os.path.basename(path)
plt.subplot(1, 3, 1)
plt.plot([r['rdo_l'] for r in results], [r['bpp'] for r in results], label=name)
plt.subplot(1, 3, 2)
plt.plot([r['rdo_l'] for r in results], [r['rms'] for r in results], label=name)
plt.subplot(1, 3, 3)
plt.plot([r['rdo_l'] for r in results], [r['psnr'] for r in results], label=name)

plt.subplot(1, 3, 1)
plt.title('bpp')
plt.legend()
plt.subplot(1, 3, 2)
plt.title('rms')
plt.legend()
plt.subplot(1, 3, 3)
plt.title('psnr')
plt.legend()

plt.savefig('basisu.png')

0 comments on commit 79640b8

Please sign in to comment.