You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nvidia-texture-tools/src/nvtt/tests/testsuite.cpp

723 lines
21 KiB
C++

// Copyright (c) 2009-2011 Ignacio Castano <castano@gmail.com>
// Copyright (c) 2007-2009 NVIDIA Corporation -- Ignacio Castano <icastano@nvidia.com>
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#include <nvtt/nvtt.h>
#include <nvimage/Image.h>
#include <nvimage/ImageIO.h>
#include <nvimage/BlockDXT.h>
#include <nvimage/ColorBlock.h>
#include <nvcore/Ptr.h>
#include <nvcore/Debug.h>
#include <nvcore/StrLib.h>
#include <nvcore/StdStream.h>
#include <nvcore/TextWriter.h>
#include <nvcore/FileSystem.h>
#include <nvcore/Timer.h>
#include <stdlib.h> // free
#include <string.h> // memcpy
#include "../tools/cmdline.h"
using namespace nv;
#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
// Kodak image set
static const char * s_kodakImageSet[] = {
14 years ago
"kodim01.png",
"kodim02.png",
"kodim03.png",
"kodim04.png",
"kodim05.png",
"kodim06.png",
"kodim07.png",
"kodim08.png",
"kodim09.png",
"kodim10.png",
"kodim11.png",
"kodim12.png",
"kodim13.png",
"kodim14.png",
"kodim15.png",
"kodim16.png",
"kodim17.png",
"kodim18.png",
"kodim19.png",
"kodim20.png",
"kodim21.png",
"kodim22.png",
"kodim23.png",
"kodim24.png",
};
// Waterloo image set
static const char * s_waterlooImageSet[] = {
14 years ago
"clegg.png",
"frymire.png",
"lena.png",
"monarch.png",
"peppers.png",
"sail.png",
"serrano.png",
"tulips.png",
};
// Epic image set
static const char * s_epicImageSet[] = {
14 years ago
"Bradley1.png",
"Gradient.png",
"MoreRocks.png",
"Wall.png",
"Rainbow.png",
"Text.png",
};
// Farbrausch
static const char * s_farbrauschImageSet[] = {
"t.2d.pn02.png",
"t.aircondition.01.png",
"t.bricks.02.png",
"t.bricks.05.png",
"t.concrete.cracked.01.png",
"t.envi.colored02.png",
"t.envi.colored03.png",
"t.font.01.png",
"t.sewers.01.png",
"t.train.03.png",
"t.yello.01.png",
};
// Lugaru
static const char * s_lugaruImageSet[] = {
14 years ago
"lugaru-blood.png",
"lugaru-bush.png",
"lugaru-cursor.png",
"lugaru-hawk.png",
};
// Quake3
static const char * s_quake3ImageSet[] = {
14 years ago
"q3-blocks15cgeomtrn.tga",
"q3-blocks17bloody.tga",
"q3-dark_tin2.tga",
"q3-fan_grate.tga",
"q3-fan.tga",
"q3-metal2_2.tga",
"q3-panel_glo.tga",
"q3-proto_fence.tga",
"q3-wires02.tga",
};
static const char * s_witnessImageSet[] = {
"applebark.tga",
"grass-01.tga",
"brownRock.tga",
"rock-01.tga",
"rock-02.tga",
"Lao-picture.tga",
"laser-base.tga",
"skydome.tga",
"speaker.tga",
"specRuin-base.tga",
"vault.tga",
"specRuin-puzzle.tga"
};
static const char * s_witnessLmapImageSet[] = {
"specruin.dds",
};
enum Mode {
Mode_BC1,
Mode_BC1_Alpha,
Mode_BC2_Alpha,
Mode_BC3_Alpha,
Mode_BC3_YCoCg,
Mode_BC3_RGBM,
Mode_BC3_LUVW,
Mode_BC1_Normal,
Mode_BC3_Normal,
Mode_BC5_Normal,
Mode_BC3_Lightmap_1,
Mode_BC3_Lightmap_2,
};
static const char * s_modeNames[] = {
"BC1",
"BC1-Alpha",
"BC2-Alpha",
"BC3-Alpha",
"BC3-YCoCg",
"BC3-RGBM",
"BC3-LUVW",
"BC1-Normal",
"BC3-Normal",
"BC5-Normal",
"BC3-RGBM",
"BC3-LUVW",
};
struct Test {
const char * name;
int count;
Mode modes[4];
};
static Test s_imageTests[] = {
{"DXT Color", 1, {Mode_BC1, Mode_BC3_YCoCg, Mode_BC3_RGBM, Mode_BC3_LUVW}},
{"DXT Alpha", 3, {Mode_BC1_Alpha, Mode_BC2_Alpha, Mode_BC3_Alpha}},
{"DXT Normal", 3, {Mode_BC1_Normal, Mode_BC3_Normal, Mode_BC5_Normal}},
{"DXT Lightmap", 2, {Mode_BC3_Lightmap_1, Mode_BC3_Lightmap_2}},
};
const int s_imageTestCount = ARRAY_SIZE(s_imageTests);
struct ImageSet
{
const char * name;
const char * basePath;
14 years ago
const char ** fileNames;
int fileCount;
};
static ImageSet s_imageSets[] = {
{"Kodak", "kodak", s_kodakImageSet, ARRAY_SIZE(s_kodakImageSet)}, // 0
{"Waterloo", "waterloo", s_waterlooImageSet, ARRAY_SIZE(s_waterlooImageSet)}, // 1
{"Epic", "epic", s_epicImageSet, ARRAY_SIZE(s_epicImageSet)}, // 2
{"Farbraush", "farbrausch", s_farbrauschImageSet, ARRAY_SIZE(s_farbrauschImageSet)}, // 3
{"Lugaru", "lugaru", s_lugaruImageSet, ARRAY_SIZE(s_lugaruImageSet)}, // 4
{"Quake3", "quake3", s_quake3ImageSet, ARRAY_SIZE(s_quake3ImageSet)}, // 5
{"Witness", "witness", s_witnessImageSet, ARRAY_SIZE(s_witnessImageSet)}, // 6
{"Lightmap", "lightmap", s_witnessLmapImageSet, ARRAY_SIZE(s_witnessLmapImageSet)}, // 7
};
const int s_imageSetCount = sizeof(s_imageSets)/sizeof(s_imageSets[0]);
struct MyOutputHandler : public nvtt::OutputHandler
{
14 years ago
MyOutputHandler() : m_data(NULL), m_ptr(NULL) {}
~MyOutputHandler()
{
free(m_data);
}
virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel)
{
m_size = size;
m_width = width;
m_height = height;
free(m_data);
m_data = (unsigned char *)malloc(size);
m_ptr = m_data;
}
virtual bool writeData(const void * data, int size)
{
memcpy(m_ptr, data, size);
m_ptr += size;
return true;
}
nvtt::TexImage decompress(Mode mode, nvtt::Decoder decoder)
14 years ago
{
nvtt::Format format;
if (mode == Mode_BC1 || mode == Mode_BC1_Alpha || mode == Mode_BC1_Normal) format = nvtt::Format_BC1;
else if (mode == Mode_BC2_Alpha) format = nvtt::Format_BC2;
else if (mode == Mode_BC5_Normal) format = nvtt::Format_BC5;
else format = nvtt::Format_BC3;
14 years ago
nvtt::TexImage img;
img.setImage2D(format, decoder, m_width, m_height, m_data);
14 years ago
return img;
14 years ago
}
int m_size;
int m_width;
int m_height;
unsigned char * m_data;
unsigned char * m_ptr;
};
int main(int argc, char *argv[])
{
MyAssertHandler assertHandler;
MyMessageHandler messageHandler;
14 years ago
const uint version = nvtt::version();
const uint major = version / 100 / 100;
const uint minor = (version / 100) % 100;
const uint rev = version % 100;
14 years ago
printf("NVIDIA Texture Tools %u.%u.%u - Copyright NVIDIA Corporation 2007\n\n", major, minor, rev);
14 years ago
int setIndex = 0;
int testIndex = 0;
14 years ago
bool fast = false;
bool nocuda = false;
bool showHelp = false;
nvtt::Decoder decoder = nvtt::Decoder_Reference;
Path basePath = "";
14 years ago
const char * outPath = "output";
const char * regressPath = NULL;
// Parse arguments.
for (int i = 1; i < argc; i++)
{
if (strcmp("-set", argv[i]) == 0)
{
if (i+1 < argc && argv[i+1][0] != '-') {
setIndex = atoi(argv[i+1]);
for (int j = 0; j < s_imageSetCount; j++) {
if (strCaseCmp(s_imageSets[j].name, argv[i+1]) == 0) {
setIndex = j;
break;
}
}
14 years ago
i++;
}
}
else if (strcmp("-test", argv[i]) == 0)
{
if (i+1 < argc && argv[i+1][0] != '-') {
testIndex = atoi(argv[i+1]);
i++;
}
}
14 years ago
else if (strcmp("-dec", argv[i]) == 0)
{
if (i+1 < argc && argv[i+1][0] != '-') {
decoder = (nvtt::Decoder)atoi(argv[i+1]);
14 years ago
i++;
}
}
else if (strcmp("-fast", argv[i]) == 0)
{
fast = true;
}
else if (strcmp("-nocuda", argv[i]) == 0)
{
nocuda = true;
}
else if (strcmp("-help", argv[i]) == 0)
{
showHelp = true;
}
else if (strcmp("-path", argv[i]) == 0)
{
if (i+1 < argc && argv[i+1][0] != '-') {
basePath = argv[i+1];
i++;
}
}
else if (strcmp("-out", argv[i]) == 0)
{
if (i+1 < argc && argv[i+1][0] != '-') {
outPath = argv[i+1];
i++;
}
}
else if (strcmp("-regress", argv[i]) == 0)
{
if (i+1 < argc && argv[i+1][0] != '-') {
regressPath = argv[i+1];
i++;
}
}
}
// Validate inputs.
if (testIndex >= s_imageTestCount) {
printf("Invalid test %d\n", testIndex);
return 0;
}
if (setIndex >= s_imageSetCount) {
printf("Invalid image set %d\n", setIndex);
return 0;
}
14 years ago
if (showHelp)
{
printf("usage: nvtestsuite [options]\n\n");
printf("Input options:\n");
printf(" -path <path> \tInput image path.\n");
printf(" -regress <path>\tRegression directory.\n");
printf(" -set [0:%d] \tImage set.\n", s_imageSetCount-1);
for (int i = 0; i < s_imageSetCount; i++) {
printf(" %i: \t%s.\n", i, s_imageSets[i].name);
}
printf(" -test [0:%d] \tCompression tests to run.", s_imageTestCount);
for (int i = 0; i < s_imageTestCount; i++) {
printf(" %i: \t%s.\n", i, s_imageTests[i].name);
}
14 years ago
printf(" -dec x \tDecompressor.\n");
printf(" 0: \tReference.\n");
printf(" 1: \tNVIDIA.\n");
14 years ago
printf("Compression options:\n");
printf(" -fast \tFast compression.\n");
printf(" -nocuda \tDo not use cuda compressor.\n");
14 years ago
printf("Output options:\n");
printf(" -out <path> \tOutput directory.\n");
14 years ago
return 1;
}
14 years ago
nvtt::CompressionOptions compressionOptions;
compressionOptions.setFormat(nvtt::Format_BC1);
if (fast)
{
compressionOptions.setQuality(nvtt::Quality_Fastest);
}
else
{
compressionOptions.setQuality(nvtt::Quality_Production);
}
//compressionOptions.setExternalCompressor("ati");
//compressionOptions.setExternalCompressor("squish");
//compressionOptions.setExternalCompressor("d3dx");
//compressionOptions.setExternalCompressor("stb");
const ImageSet & set = s_imageSets[setIndex];
const Test & test = s_imageTests[testIndex];
14 years ago
nvtt::OutputOptions outputOptions;
outputOptions.setOutputHeader(false);
MyOutputHandler outputHandler;
outputOptions.setOutputHandler(&outputHandler);
nvtt::Context context;
context.enableCudaAcceleration(!nocuda);
basePath.append(set.basePath);
FileSystem::changeDirectory(basePath.str());
14 years ago
FileSystem::createDirectory(outPath);
//Path csvFileName;
//csvFileName.format("%s/result-%d.csv", outPath, setIndex);
//StdOutputStream csvStream(csvFileName.str());
//TextWriter csvWriter(&csvStream);
14 years ago
Path graphFileName;
graphFileName.format("%s/chart.txt", outPath/*, test.name*/);
StdOutputStream graphStream(graphFileName.str());
TextWriter graphWriter(&graphStream);
14 years ago
graphWriter << "http://chart.apis.google.com/chart?";
14 years ago
// Graph size.
graphWriter << "chs=480x240";
14 years ago
// Graph type: line
graphWriter << "&cht=lc";
// Margins.
graphWriter << "&chma=30,10,10|0,40";
// Grid lines.
graphWriter << "&chxt=x,y&chxtc=0,-1000|1,-1000";
// Labels.
graphWriter << "&chxr=0,1," << set.fileCount << ",1|1,0,0.05,0.01";
graphWriter << "&chdlp=b"; // Labels at the bottom.
// Line colors.
graphWriter << "&chco=";
for (int t = 0; t < test.count; t++)
{
const char * colors[] = {
"3D7930", "952826", "3D1FC1",
"FF9900", "999999", "999999", // pick other colors...
};
graphWriter << colors[t];
if (t != test.count-1) graphWriter << ",";
}
// Line width.
graphWriter << "&chls=";
for (int t = 0; t < test.count; t++)
{
graphWriter << "2";
if (t != test.count-1) graphWriter << "|";
}
// Data ranges.
graphWriter << "&chds=";
for (int t = 0; t < test.count; t++)
{
graphWriter << "0,0.05";
if (t != test.count-1) graphWriter << ",";
}
// Leyends.
graphWriter << "&chdl=";
for (int t = 0; t < test.count; t++)
14 years ago
{
graphWriter << s_modeNames[test.modes[t]];
if (t != test.count-1) graphWriter << "|";
}
14 years ago
// Title
graphWriter << "&chtt=" << set.name << " - " << test.name;
float totalTime = 0;
float totalRMSE = 0;
//int failedTests = 0;
float totalDiff = 0;
14 years ago
Timer timer;
14 years ago
nvtt::TexImage img;
14 years ago
printf("Running Test: %s\n", set.name);
14 years ago
graphWriter << "&chd=t:";
14 years ago
for (int t = 0; t < test.count; t++)
{
Mode mode = test.modes[t];
if (mode == Mode_BC1 || mode == Mode_BC1_Alpha || mode == Mode_BC1_Normal) {
compressionOptions.setFormat(nvtt::Format_BC1);
}
else if (mode == Mode_BC3_Alpha || mode == Mode_BC3_YCoCg || mode == Mode_BC3_RGBM || mode == Mode_BC3_LUVW || mode == Mode_BC3_Lightmap_1 || mode == Mode_BC3_Lightmap_2) {
compressionOptions.setFormat(nvtt::Format_BC3);
}
else if (mode == Mode_BC3_Normal) {
compressionOptions.setFormat(nvtt::Format_BC3n);
}
else if (mode == Mode_BC5_Normal) {
compressionOptions.setFormat(nvtt::Format_BC5);
}
if (mode == Mode_BC3_Alpha || mode == Mode_BC3_Lightmap_1 || mode == Mode_BC3_Lightmap_2) { // Lightmap's alpha channel is coverage.
img.setAlphaMode(nvtt::AlphaMode_Transparency);
}
if (mode == Mode_BC1_Normal || mode == Mode_BC3_Normal || mode == Mode_BC5_Normal) {
img.setNormalMap(true);
}
14 years ago
printf("Processing Set: %s\n", set.name);
for (int i = 0; i < set.fileCount; i++)
14 years ago
{
if (!img.load(set.fileNames[i]))
{
printf("Input image '%s' not found.\n", set.fileNames[i]);
return EXIT_FAILURE;
}
14 years ago
if (img.isNormalMap()) {
img.normalizeNormalMap();
}
14 years ago
nvtt::TexImage tmp = img;
if (mode == Mode_BC3_YCoCg) {
tmp.toYCoCg();
tmp.blockScaleCoCg();
tmp.scaleBias(0, 0.5, 0.5);
tmp.scaleBias(1, 0.5, 0.5);
}
else if (mode == Mode_BC3_RGBM) {
tmp.toRGBM();
}
else if (mode == Mode_BC3_LUVW) {
tmp.toLUVW();
}
else if (mode == Mode_BC3_Lightmap_1) {
tmp.toRGBM(4);
/*float rmin, rmax;
tmp.range(0, &rmin, &rmax);
float gmin, gmax;
tmp.range(1, &gmin, &gmax);
float bmin, bmax;
tmp.range(2, &bmin, &bmax);
float lmin, lmax;
tmp.range(3, &lmin, &lmax);
printf("rmin: %.3f rmax: %.3f\n", rmin, rmax);
printf("gmin: %.3f gmax: %.3f\n", gmin, gmax);
printf("bmin: %.3f bmax: %.3f\n", bmin, bmax);
printf("lmin: %.3f lmax: %.3f\n", lmin, lmax);
const int N = 32;
int chistogram[N];
int lhistogram[N];
memset(chistogram, 0, sizeof(chistogram));
memset(lhistogram, 0, sizeof(lhistogram));
tmp.histogram(0, 0, 1, N, chistogram);
tmp.histogram(1, 0, 1, N, chistogram);
tmp.histogram(2, 0, 1, N, chistogram);
tmp.histogram(3, 0, 1, N, lhistogram);
printf("Color histogram:\n");
for (int i = 0; i < N; i++) {
printf("%d, ", chistogram[i]);
}
printf("\n");
printf("Luminance histogram:\n");
for (int i = 0; i < N; i++) {
printf("%d, ", lhistogram[i]);
}
printf("\n");*/
}
else if (mode == Mode_BC3_Lightmap_2) {
tmp.toLUVW(4);
}
14 years ago
printf("Compressing: \t'%s'\n", set.fileNames[i]);
14 years ago
timer.start();
context.compress(tmp, 0, 0, compressionOptions, outputOptions);
timer.stop();
printf(" Time: \t%.3f sec\n", timer.elapsed());
totalTime += timer.elapsed();
nvtt::TexImage img_out = outputHandler.decompress(mode, decoder);
img_out.setAlphaMode(img.alphaMode());
img_out.setNormalMap(img.isNormalMap());
14 years ago
if (mode == Mode_BC3_YCoCg) {
img_out.scaleBias(0, 1.0, -0.5);
img_out.scaleBias(1, 1.0, -0.5);
img_out.fromYCoCg();
}
else if (mode == Mode_BC3_RGBM) {
img_out.fromRGBM();
}
else if (mode == Mode_BC3_LUVW) {
img_out.fromLUVW();
}
else if (mode == Mode_BC3_Lightmap_1) {
img_out.fromRGBM(4);
}
else if (mode == Mode_BC3_Lightmap_2) {
img_out.fromLUVW(4);
}
Path outputFilePath;
outputFilePath.format("%s/%s", outPath, s_modeNames[test.modes[t]]);
FileSystem::createDirectory(outputFilePath.str());
Path outputFileName;
outputFileName.format("%s/%s", outputFilePath.str(), set.fileNames[i]);
outputFileName.stripExtension();
if (mode == Mode_BC3_Lightmap_1 || mode == Mode_BC3_Lightmap_2) {
outputFileName.append(".dds");
}
else {
outputFileName.append(".png");
}
if (!img_out.save(outputFileName.str()))
14 years ago
{
printf("Error saving file '%s'.\n", outputFileName.str());
14 years ago
}
float rmse = nvtt::rmsError(img, img_out);
totalRMSE += rmse;
14 years ago
printf(" RMSE: \t%.4f\n", rmse);
14 years ago
graphWriter << rmse;
if (i != set.fileCount-1) graphWriter << ",";
14 years ago
outputFileName.stripExtension();
outputFileName.append("_diff.png");
nvtt::diff(img, img_out, 4.0f).save(outputFileName.str());
// Output csv file
//csvWriter << "\"" << fileNames[i] << "\"," << rmse << "\n";
/*if (regressPath != NULL)
{
Path regressFileName;
regressFileName.format("%s/%s", regressPath, fileNames[i]);
regressFileName.stripExtension();
regressFileName.append(".png");
nvtt::TexImage img_reg;
if (!img_reg.load(regressFileName.str()))
{
printf("Regression image '%s' not found.\n", regressFileName.str());
return EXIT_FAILURE;
}
float rmse_reg = rmsError(img, img_reg);
float diff = rmse_reg - rmse;
totalDiff += diff;
const char * text = "PASSED";
if (equal(diff, 0)) text = "PASSED";
else if (diff < 0) {
text = "FAILED";
failedTests++;
}
printf(" Diff: \t%.4f (%s)\n", diff, text);
}*/
fflush(stdout);
14 years ago
}
totalRMSE /= set.fileCount;
totalDiff /= set.fileCount;
14 years ago
printf("Total Results:\n");
printf(" Total Time: \t%.3f sec\n", totalTime);
printf(" Average RMSE:\t%.4f\n", totalRMSE);
14 years ago
if (t != test.count-1) graphWriter << "|";
}
14 years ago
/*if (regressPath != NULL)
14 years ago
{
printf("Regression Results:\n");
printf(" Diff: %.4f\n", totalDiff);
printf(" %d/%d tests failed.\n", failedTests, fileCount);
}*/
14 years ago
return EXIT_SUCCESS;
}