From 70331a37fdef386a1ff46dee1f3042d64210db53 Mon Sep 17 00:00:00 2001 From: castano Date: Tue, 6 May 2008 23:55:19 +0000 Subject: [PATCH] Refactor compression functions, group them into class methods. --- src/nvtt/CompressDXT.cpp | 137 ++++++++++++++++++++++----------------- src/nvtt/CompressDXT.h | 56 +++++++++++----- src/nvtt/Compressor.cpp | 34 ++++++---- 3 files changed, 135 insertions(+), 92 deletions(-) diff --git a/src/nvtt/CompressDXT.cpp b/src/nvtt/CompressDXT.cpp index 52de101..ef9f656 100644 --- a/src/nvtt/CompressDXT.cpp +++ b/src/nvtt/CompressDXT.cpp @@ -57,17 +57,31 @@ using namespace nv; using namespace nvtt; -void nv::fastCompressDXT1(const Image * image, const OutputOptions::Private & outputOptions) +nv::FastCompressor::FastCompressor() : m_image(NULL), m_alphaMode(AlphaMode_None) +{ +} + +nv::FastCompressor::~FastCompressor() +{ +} + +void nv::FastCompressor::setImage(const Image * image, nvtt::AlphaMode alphaMode) { - const uint w = image->width(); - const uint h = image->height(); + m_image = image; + m_alphaMode = alphaMode; +} + +void nv::FastCompressor::compressDXT1(const OutputOptions::Private & outputOptions) +{ + const uint w = m_image->width(); + const uint h = m_image->height(); ColorBlock rgba; BlockDXT1 block; for (uint y = 0; y < h; y += 4) { for (uint x = 0; x < w; x += 4) { - rgba.init(image, x, y); + rgba.init(m_image, x, y); QuickCompress::compressDXT1(rgba, &block); @@ -79,17 +93,17 @@ void nv::fastCompressDXT1(const Image * image, const OutputOptions::Private & ou } -void nv::fastCompressDXT1a(const Image * image, const OutputOptions::Private & outputOptions) +void nv::FastCompressor::compressDXT1a(const OutputOptions::Private & outputOptions) { - const uint w = image->width(); - const uint h = image->height(); + const uint w = m_image->width(); + const uint h = m_image->height(); ColorBlock rgba; BlockDXT1 block; for (uint y = 0; y < h; y += 4) { for (uint x = 0; x < w; x += 4) { - rgba.init(image, x, y); + rgba.init(m_image, x, y); QuickCompress::compressDXT1a(rgba, &block); @@ -101,17 +115,17 @@ void nv::fastCompressDXT1a(const Image * image, const OutputOptions::Private & o } -void nv::fastCompressDXT3(const Image * image, const nvtt::OutputOptions::Private & outputOptions) +void nv::FastCompressor::compressDXT3(const nvtt::OutputOptions::Private & outputOptions) { - const uint w = image->width(); - const uint h = image->height(); + const uint w = m_image->width(); + const uint h = m_image->height(); ColorBlock rgba; BlockDXT3 block; for (uint y = 0; y < h; y += 4) { for (uint x = 0; x < w; x += 4) { - rgba.init(image, x, y); + rgba.init(m_image, x, y); QuickCompress::compressDXT3(rgba, &block); @@ -123,17 +137,17 @@ void nv::fastCompressDXT3(const Image * image, const nvtt::OutputOptions::Privat } -void nv::fastCompressDXT5(const Image * image, const nvtt::OutputOptions::Private & outputOptions) +void nv::FastCompressor::compressDXT5(const nvtt::OutputOptions::Private & outputOptions) { - const uint w = image->width(); - const uint h = image->height(); + const uint w = m_image->width(); + const uint h = m_image->height(); ColorBlock rgba; BlockDXT5 block; for (uint y = 0; y < h; y += 4) { for (uint x = 0; x < w; x += 4) { - rgba.init(image, x, y); + rgba.init(m_image, x, y); QuickCompress::compressDXT5(rgba, &block, 0); @@ -145,17 +159,17 @@ void nv::fastCompressDXT5(const Image * image, const nvtt::OutputOptions::Privat } -void nv::fastCompressDXT5n(const Image * image, const nvtt::OutputOptions::Private & outputOptions) +void nv::FastCompressor::compressDXT5n(const nvtt::OutputOptions::Private & outputOptions) { - const uint w = image->width(); - const uint h = image->height(); + const uint w = m_image->width(); + const uint h = m_image->height(); ColorBlock rgba; BlockDXT5 block; for (uint y = 0; y < h; y += 4) { for (uint x = 0; x < w; x += 4) { - rgba.init(image, x, y); + rgba.init(m_image, x, y); // copy X coordinate to alpha channel and Y coordinate to green channel. rgba.swizzleDXT5n(); @@ -170,24 +184,24 @@ void nv::fastCompressDXT5n(const Image * image, const nvtt::OutputOptions::Priva } -void nv::fastCompressBC4(const Image * image, const nvtt::OutputOptions::Private & outputOptions) +nv::SlowCompressor::SlowCompressor() : m_image(NULL), m_alphaMode(AlphaMode_None) +{ +} + +nv::SlowCompressor::~SlowCompressor() +{ +} + +void nv::SlowCompressor::setImage(const Image * image, nvtt::AlphaMode alphaMode) { - // @@ TODO - // compress red channel (X) + m_image = image; + m_alphaMode = alphaMode; } - -void nv::fastCompressBC5(const Image * image, const nvtt::OutputOptions::Private & outputOptions) +void nv::SlowCompressor::compressDXT1(const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) { - // @@ TODO - // compress red, green channels (X,Y) -} - - -void nv::compressDXT1(const Image * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions) -{ - const uint w = image->width(); - const uint h = image->height(); + const uint w = m_image->width(); + const uint h = m_image->height(); ColorBlock rgba; BlockDXT1 block; @@ -200,7 +214,7 @@ void nv::compressDXT1(const Image * image, const OutputOptions::Private & output for (uint y = 0; y < h; y += 4) { for (uint x = 0; x < w; x += 4) { - rgba.init(image, x, y); + rgba.init(m_image, x, y); if (rgba.isSingleColor()) { @@ -221,10 +235,10 @@ void nv::compressDXT1(const Image * image, const OutputOptions::Private & output } -void nv::compressDXT1a(const Image * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions) +void nv::SlowCompressor::compressDXT1a(const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) { - const uint w = image->width(); - const uint h = image->height(); + const uint w = m_image->width(); + const uint h = m_image->height(); ColorBlock rgba; BlockDXT1 block; @@ -235,7 +249,7 @@ void nv::compressDXT1a(const Image * image, const OutputOptions::Private & outpu for (uint y = 0; y < h; y += 4) { for (uint x = 0; x < w; x += 4) { - rgba.init(image, x, y); + rgba.init(m_image, x, y); bool anyAlpha = false; bool allAlpha = true; @@ -265,21 +279,22 @@ void nv::compressDXT1a(const Image * image, const OutputOptions::Private & outpu } -void nv::compressDXT3(const Image * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions) +void nv::SlowCompressor::compressDXT3(const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) { - const uint w = image->width(); - const uint h = image->height(); + const uint w = m_image->width(); + const uint h = m_image->height(); ColorBlock rgba; BlockDXT3 block; squish::WeightedClusterFit fit; + //squish::FastClusterFit fit; fit.SetMetric(compressionOptions.colorWeight.x(), compressionOptions.colorWeight.y(), compressionOptions.colorWeight.z()); for (uint y = 0; y < h; y += 4) { for (uint x = 0; x < w; x += 4) { - rgba.init(image, x, y); + rgba.init(m_image, x, y); // Compress explicit alpha. OptimalCompress::compressDXT3A(rgba, &block.alpha); @@ -303,10 +318,10 @@ void nv::compressDXT3(const Image * image, const OutputOptions::Private & output } } -void nv::compressDXT5(const Image * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions) +void nv::SlowCompressor::compressDXT5(const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) { - const uint w = image->width(); - const uint h = image->height(); + const uint w = m_image->width(); + const uint h = m_image->height(); ColorBlock rgba; BlockDXT5 block; @@ -317,7 +332,7 @@ void nv::compressDXT5(const Image * image, const OutputOptions::Private & output for (uint y = 0; y < h; y += 4) { for (uint x = 0; x < w; x += 4) { - rgba.init(image, x, y); + rgba.init(m_image, x, y); // Compress alpha. if (compressionOptions.quality == Quality_Highest) @@ -328,7 +343,7 @@ void nv::compressDXT5(const Image * image, const OutputOptions::Private & output { QuickCompress::compressDXT5A(rgba, &block.alpha); } - + // Compress color. if (rgba.isSingleColor()) { @@ -349,10 +364,10 @@ void nv::compressDXT5(const Image * image, const OutputOptions::Private & output } -void nv::compressDXT5n(const Image * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions) +void nv::SlowCompressor::compressDXT5n(const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) { - const uint w = image->width(); - const uint h = image->height(); + const uint w = m_image->width(); + const uint h = m_image->height(); ColorBlock rgba; BlockDXT5 block; @@ -360,7 +375,7 @@ void nv::compressDXT5n(const Image * image, const OutputOptions::Private & outpu for (uint y = 0; y < h; y += 4) { for (uint x = 0; x < w; x += 4) { - rgba.init(image, x, y); + rgba.init(m_image, x, y); // copy X coordinate to green channel and Y coordinate to alpha channel. rgba.swizzleDXT5n(); @@ -386,10 +401,10 @@ void nv::compressDXT5n(const Image * image, const OutputOptions::Private & outpu } -void nv::compressBC4(const Image * image, const nvtt::OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions) +void nv::SlowCompressor::compressBC4(const CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions) { - const uint w = image->width(); - const uint h = image->height(); + const uint w = m_image->width(); + const uint h = m_image->height(); ColorBlock rgba; AlphaBlockDXT5 block; @@ -397,7 +412,7 @@ void nv::compressBC4(const Image * image, const nvtt::OutputOptions::Private & o for (uint y = 0; y < h; y += 4) { for (uint x = 0; x < w; x += 4) { - rgba.init(image, x, y); + rgba.init(m_image, x, y); if (compressionOptions.quality == Quality_Highest) { @@ -416,10 +431,10 @@ void nv::compressBC4(const Image * image, const nvtt::OutputOptions::Private & o } -void nv::compressBC5(const Image * image, const nvtt::OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions) +void nv::SlowCompressor::compressBC5(const CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions) { - const uint w = image->width(); - const uint h = image->height(); + const uint w = m_image->width(); + const uint h = m_image->height(); ColorBlock xcolor; ColorBlock ycolor; @@ -429,10 +444,10 @@ void nv::compressBC5(const Image * image, const nvtt::OutputOptions::Private & o for (uint y = 0; y < h; y += 4) { for (uint x = 0; x < w; x += 4) { - xcolor.init(image, x, y); + xcolor.init(m_image, x, y); xcolor.splatX(); - ycolor.init(image, x, y); + ycolor.init(m_image, x, y); ycolor.splatY(); if (compressionOptions.quality == Quality_Highest) diff --git a/src/nvtt/CompressDXT.h b/src/nvtt/CompressDXT.h index bc614ce..3b7d343 100644 --- a/src/nvtt/CompressDXT.h +++ b/src/nvtt/CompressDXT.h @@ -32,24 +32,46 @@ namespace nv class Image; class FloatImage; - // Fast compressors. - void fastCompressDXT1(const Image * image, const nvtt::OutputOptions::Private & outputOptions); - void fastCompressDXT1a(const Image * image, const nvtt::OutputOptions::Private & outputOptions); - void fastCompressDXT3(const Image * image, const nvtt::OutputOptions::Private & outputOptions); - void fastCompressDXT5(const Image * image, const nvtt::OutputOptions::Private & outputOptions); - void fastCompressDXT5n(const Image * image, const nvtt::OutputOptions::Private & outputOptions); - void fastCompressBC4(const Image * image, const nvtt::OutputOptions::Private & outputOptions); - void fastCompressBC5(const Image * image, const nvtt::OutputOptions::Private & outputOptions); + class FastCompressor + { + public: + FastCompressor(); + ~FastCompressor(); + + void setImage(const Image * image, nvtt::AlphaMode alphaMode); + + void compressDXT1(const nvtt::OutputOptions::Private & outputOptions); + void compressDXT1a(const nvtt::OutputOptions::Private & outputOptions); + void compressDXT3(const nvtt::OutputOptions::Private & outputOptions); + void compressDXT5(const nvtt::OutputOptions::Private & outputOptions); + void compressDXT5n(const nvtt::OutputOptions::Private & outputOptions); + + private: + const Image * m_image; + nvtt::AlphaMode m_alphaMode; + }; + + class SlowCompressor + { + public: + SlowCompressor(); + ~SlowCompressor(); + + void setImage(const Image * image, nvtt::AlphaMode alphaMode); + + void compressDXT1(const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions); + void compressDXT1a(const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions); + void compressDXT3(const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions); + void compressDXT5(const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions); + void compressDXT5n(const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions); + void compressBC4(const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions); + void compressBC5(const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions); + + private: + const Image * m_image; + nvtt::AlphaMode m_alphaMode; + }; - // Normal compressors. - void compressDXT1(const Image * image, const nvtt::OutputOptions::Private & outputOptions, const nvtt::CompressionOptions::Private & compressionOptions); - void compressDXT1a(const Image * image, const nvtt::OutputOptions::Private & outputOptions, const nvtt::CompressionOptions::Private & compressionOptions); - void compressDXT3(const Image * image, const nvtt::OutputOptions::Private & outputOptions, const nvtt::CompressionOptions::Private & compressionOptions); - void compressDXT5(const Image * image, const nvtt::OutputOptions::Private & outputOptions, const nvtt::CompressionOptions::Private & compressionOptions); - void compressDXT5n(const Image * image, const nvtt::OutputOptions::Private & outputOptions, const nvtt::CompressionOptions::Private & compressionOptions); - void compressBC4(const Image * image, const nvtt::OutputOptions::Private & outputOptions, const nvtt::CompressionOptions::Private & compressionOptions); - void compressBC5(const Image * image, const nvtt::OutputOptions::Private & outputOptions, const nvtt::CompressionOptions::Private & compressionOptions); - // External compressors. #if defined(HAVE_S3QUANT) void s3CompressDXT1(const Image * image, const nvtt::OutputOptions::Private & outputOptions); diff --git a/src/nvtt/Compressor.cpp b/src/nvtt/Compressor.cpp index 48fa985..6d4be87 100644 --- a/src/nvtt/Compressor.cpp +++ b/src/nvtt/Compressor.cpp @@ -712,9 +712,15 @@ void Compressor::Private::quantizeMipmap(Mipmap & mipmap, const CompressionOptio bool Compressor::Private::compressMipmap(const Mipmap & mipmap, const InputOptions::Private & inputOptions, const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) const { const Image * image = mipmap.asFixedImage(); - nvDebugCheck(image != NULL); + FastCompressor fast; + fast.setImage(image, inputOptions.alphaMode); + + SlowCompressor slow; + slow.setImage(image, inputOptions.alphaMode); + + if (compressionOptions.format == Format_RGBA || compressionOptions.format == Format_RGB) { compressRGB(image, outputOptions, compressionOptions); @@ -738,7 +744,7 @@ bool Compressor::Private::compressMipmap(const Mipmap & mipmap, const InputOptio #endif if (compressionOptions.quality == Quality_Fastest) { - fastCompressDXT1(image, outputOptions); + fast.compressDXT1(outputOptions); } else { @@ -750,7 +756,7 @@ bool Compressor::Private::compressMipmap(const Mipmap & mipmap, const InputOptio } else { - compressDXT1(image, outputOptions, compressionOptions); + slow.compressDXT1(compressionOptions, outputOptions); } } } @@ -758,18 +764,18 @@ bool Compressor::Private::compressMipmap(const Mipmap & mipmap, const InputOptio { if (compressionOptions.quality == Quality_Fastest) { - fastCompressDXT1a(image, outputOptions); + fast.compressDXT1a(outputOptions); } else { if (cudaEnabled) { nvDebugCheck(cudaSupported); - /*cuda*/compressDXT1a(image, outputOptions, compressionOptions); + /*cuda*/slow.compressDXT1a(compressionOptions, outputOptions); } else { - compressDXT1a(image, outputOptions, compressionOptions); + slow.compressDXT1a(compressionOptions, outputOptions); } } } @@ -790,7 +796,7 @@ bool Compressor::Private::compressMipmap(const Mipmap & mipmap, const InputOptio { if (compressionOptions.quality == Quality_Fastest) { - fastCompressDXT3(image, outputOptions); + fast.compressDXT3(outputOptions); } else { @@ -802,7 +808,7 @@ bool Compressor::Private::compressMipmap(const Mipmap & mipmap, const InputOptio } else { - compressDXT3(image, outputOptions, compressionOptions); + slow.compressDXT3(compressionOptions, outputOptions); } } } @@ -810,7 +816,7 @@ bool Compressor::Private::compressMipmap(const Mipmap & mipmap, const InputOptio { if (compressionOptions.quality == Quality_Fastest) { - fastCompressDXT5(image, outputOptions); + fast.compressDXT5(outputOptions); } else { @@ -822,7 +828,7 @@ bool Compressor::Private::compressMipmap(const Mipmap & mipmap, const InputOptio } else { - compressDXT5(image, outputOptions, compressionOptions); + slow.compressDXT5(compressionOptions, outputOptions); } } } @@ -830,20 +836,20 @@ bool Compressor::Private::compressMipmap(const Mipmap & mipmap, const InputOptio { if (compressionOptions.quality == Quality_Fastest) { - fastCompressDXT5n(image, outputOptions); + fast.compressDXT5n(outputOptions); } else { - compressDXT5n(image, outputOptions, compressionOptions); + slow.compressDXT5n(compressionOptions, outputOptions); } } else if (compressionOptions.format == Format_BC4) { - compressBC4(image, outputOptions, compressionOptions); + slow.compressBC4(compressionOptions, outputOptions); } else if (compressionOptions.format == Format_BC5) { - compressBC5(image, outputOptions, compressionOptions); + slow.compressBC5(compressionOptions, outputOptions); } else if (compressionOptions.format == Format_CTX1) {