@ -71,204 +71,165 @@ typedef ULONG_PTR DWORD_PTR;
# include "stb/stb_dxt.h"
# endif
# pragma message(NV_FILE_LINE "FIXME: Define HAVE_OPENMP from cmake.")
# define HAVE_OPENMP
# include <omp.h>
using namespace nv ;
using namespace nvtt ;
nv : : FastCompressor : : FastCompressor ( ) : m_image ( NULL ) , m_alphaMode ( AlphaMode_None )
void FixedBlockCompressor : : compress ( nvtt : : InputFormat inputFormat , nvtt : : AlphaMode alphaMode , uint w , uint h , void * data , const nvtt : : CompressionOptions : : Private & compressionOptions , const nvtt : : OutputOptions : : Private & outputOptions )
{
}
const uint bs = blockSize ( ) ;
const uint bw = ( w + 3 ) / 4 ;
const uint bh = ( h + 3 ) / 4 ;
const uint size = bs * bw * bh ;
nv : : FastCompressor : : ~ FastCompressor ( )
{
}
# if defined(HAVE_OPENMP)
bool singleThreaded = false ;
# else
bool singleThreaded = true ;
# endif
void nv : : FastCompressor : : setImage ( const Image * image , nvtt : : AlphaMode alphaMode )
{
m_image = image ;
m_alphaMode = alphaMode ;
}
// Use a single thread to compress small textures.
if ( bw * bh < 16 ) singleThreaded = true ;
void nv : : FastCompressor : : compressDXT1 ( const OutputOptions : : Private & outputOptions )
if ( singleThreaded )
{
const uint w = m_image - > width ( ) ;
const uint h = m_image - > height ( ) ;
ColorBlock rgba ;
BlockDXT1 block ;
nvDebugCheck ( bs < = 16 ) ;
uint8 mem [ 16 ] ;
for ( u int y = 0 ; y < h ; y + = 4 ) {
for ( int y = 0 ; y < int ( h ) ; y + = 4 ) {
for ( uint x = 0 ; x < w ; x + = 4 ) {
rgba . init ( m_image , x , y ) ;
QuickCompress : : compressDXT1 ( rgba , & block ) ;
if ( outputOptions . outputHandler ! = NULL ) {
outputOptions . outputHandler - > writeData ( & block , sizeof ( block ) ) ;
}
}
ColorBlock rgba ;
if ( inputFormat = = nvtt : : InputFormat_BGRA_8UB ) {
rgba . init ( w , h , ( uint * ) data , x , y ) ;
}
else {
nvDebugCheck ( inputFormat = = nvtt : : InputFormat_RGBA_32F ) ;
rgba . init ( w , h , ( float * ) data , x , y ) ;
}
void nv : : FastCompressor : : compressDXT1a ( 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 ( m_image , x , y ) ;
QuickCompress : : compressDXT1a ( rgba , & block ) ;
compressBlock ( rgba , alphaMode , compressionOptions , mem ) ;
if ( outputOptions . outputHandler ! = NULL ) {
outputOptions . outputHandler - > writeData ( & block , sizeof ( block ) ) ;
outputOptions . outputHandler - > writeData ( mem , bs ) ;
}
}
}
}
# if defined(HAVE_OPENMP)
else
{
uint8 * mem = new uint8 [ size ] ;
void nv : : FastCompressor : : compressDXT3 ( const nvtt : : OutputOptions : : Private & outputOptions )
# pragma omp parallel
{
# pragma omp for
for ( int i = 0 ; i < int ( bw * bh ) ; i + + )
{
const uint w = m_image - > width ( ) ;
const uint h = m_image - > height ( ) ;
const uint x = i % bw ;
const uint y = i / bw ;
ColorBlock rgba ;
BlockDXT3 block ;
for ( uint y = 0 ; y < h ; y + = 4 ) {
for ( uint x = 0 ; x < w ; x + = 4 ) {
rgba . init ( m_image , x , y ) ;
QuickCompress : : compressDXT3 ( rgba , & block ) ;
if ( outputOptions . outputHandler ! = NULL ) {
outputOptions . outputHandler - > writeData ( & block , sizeof ( block ) ) ;
}
}
if ( inputFormat = = nvtt : : InputFormat_BGRA_8UB ) {
rgba . init ( w , h , ( uint * ) data , 4 * x , 4 * y ) ;
}
else {
nvDebugCheck ( inputFormat = = nvtt : : InputFormat_RGBA_32F ) ;
rgba . init ( w , h , ( float * ) data , 4 * x , 4 * y ) ;
}
void nv : : FastCompressor : : compressDXT5 ( const nvtt : : OutputOptions : : Private & outputOptions )
{
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 ( m_image , x , y ) ;
QuickCompress : : compressDXT5 ( rgba , & block , 0 ) ;
uint8 * ptr = mem + ( y * bw + x ) * bs ;
compressBlock ( rgba , alphaMode , compressionOptions , ptr ) ;
} // omp for
} // omp parallel
if ( outputOptions . outputHandler ! = NULL ) {
outputOptions . outputHandler - > writeData ( & block , sizeof ( block ) ) ;
}
outputOptions . outputHandler - > writeData ( mem , size ) ;
}
delete [ ] mem ;
}
# endif
}
void nv: : FastCompressor : : compressDXT5n ( const nvtt : : OutputOptions : : Private & outputOptions )
void FastCompressorDXT1: : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
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 ( m_image , x , y ) ;
rgba . swizzleDXT5n ( ) ;
QuickCompress : : compressDXT5 ( rgba , & block , 0 ) ;
if ( outputOptions . outputHandler ! = NULL ) {
outputOptions . outputHandler - > writeData ( & block , sizeof ( block ) ) ;
}
}
BlockDXT1 * block = new ( output ) BlockDXT1 ;
QuickCompress : : compressDXT1 ( rgba , block ) ;
}
void FastCompressorDXT1a : : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
BlockDXT1 * block = new ( output ) BlockDXT1 ;
QuickCompress : : compressDXT1a ( rgba , block ) ;
}
void FastCompressorDXT3 : : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
BlockDXT3 * block = new ( output ) BlockDXT3 ;
QuickCompress : : compressDXT3 ( rgba , block ) ;
}
nv : : SlowCompressor : : SlowCompressor ( ) : m_image ( NULL ) , m_alphaMode ( AlphaMode_None )
void FastCompressorDXT5 : : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
BlockDXT5 * block = new ( output ) BlockDXT5 ;
QuickCompress : : compressDXT5 ( rgba , block ) ;
}
nv : : SlowCompressor : : ~ SlowCompressor ( )
void FastCompressorDXT5n : : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
rgba . swizzle ( 4 , 1 , 5 , 0 ) ; // 0xFF, G, 0, R
BlockDXT5 * block = new ( output ) BlockDXT5 ;
QuickCompress : : compressDXT5 ( rgba , block ) ;
}
void nv : : SlowCompressor : : setImage ( const Image * image , nvtt : : AlphaMode alphaMode )
void FastCompressorBC4: : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
m_image = image ;
m_alphaMode = alphaMode ;
BlockATI1 * block = new ( output ) BlockATI1 ;
rgba . swizzle ( 0 , 1 , 2 , 0 ) ; // Copy red to alpha
QuickCompress : : compressDXT5A ( rgba , & block - > alpha ) ;
}
void nv: : SlowCompressor : : compressDXT1 ( const CompressionOptions : : Private & compressionOptions , const OutputOptions : : Private & outputOptions )
void FastCompressorBC5: : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
const uint w = m_image - > width ( ) ;
const uint h = m_image - > height ( ) ;
BlockATI2 * block = new ( output ) BlockATI2 ;
ColorBlock rgba ;
BlockDXT1 block ;
rgba. swizzle ( 0 , 1 , 2 , 0 ) ; // Copy red to alpha
QuickCompress: : compressDXT5A ( rgba , & block - > x ) ;
nvsquish : : WeightedClusterFit fit ;
//nvsquish::ClusterFit fit;
//nvsquish::FastClusterFit fit;
fit . SetMetric ( compressionOptions . colorWeight . x ( ) , compressionOptions . colorWeight . y ( ) , compressionOptions . colorWeight . z ( ) ) ;
rgba . swizzle ( 0 , 1 , 2 , 1 ) ; // Copy green to alpha
QuickCompress : : compressDXT5A ( rgba , & block - > y ) ;
}
for ( uint y = 0 ; y < h ; y + = 4 ) {
for ( uint x = 0 ; x < w ; x + = 4 ) {
rgba . init ( m_image , x , y ) ;
void NormalCompressorDXT1 : : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
nvsquish : : WeightedClusterFit fit ;
fit . SetMetric ( compressionOptions . colorWeight . x ( ) , compressionOptions . colorWeight . y ( ) , compressionOptions . colorWeight . z ( ) ) ;
if ( rgba . isSingleColor ( ) )
{
OptimalCompress : : compressDXT1 ( rgba . color ( 0 ) , & block ) ;
BlockDXT1 * block = new ( output ) BlockDXT1 ;
OptimalCompress : : compressDXT1 ( rgba . color ( 0 ) , block ) ;
}
else
{
nvsquish : : ColourSet colours ( ( uint8 * ) rgba . colors ( ) , 0 , true ) ;
nvsquish : : ColourSet colours ( ( uint8 * ) rgba . colors ( ) , 0 ) ;
fit . SetColourSet ( & colours , nvsquish : : kDxt1 ) ;
fit . Compress ( & block ) ;
}
if ( outputOptions . outputHandler ! = NULL ) {
outputOptions . outputHandler - > writeData ( & block , sizeof ( block ) ) ;
}
}
fit . Compress ( output ) ;
}
}
void nv: : SlowCompressor : : compressDXT1a ( const CompressionOptions : : Private & compressionOptions , const OutputOptions : : Private & outputOptions )
void NormalCompressorDXT1a : : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
const uint w = m_image - > width ( ) ;
const uint h = m_image - > height ( ) ;
ColorBlock rgba ;
BlockDXT1 block ;
nvsquish : : WeightedClusterFit 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 ( m_image , x , y ) ;
bool anyAlpha = false ;
bool allAlpha = true ;
@ -278,246 +239,158 @@ void nv::SlowCompressor::compressDXT1a(const CompressionOptions::Private & compr
else allAlpha = false ;
}
if ( ( ! anyAlpha & & rgba . isSingleColor ( ) | | allAlpha ) )
const bool isSingleColor = rgba . isSingleColor ( ) ;
if ( ( ! anyAlpha & & isSingleColor | | allAlpha ) )
{
OptimalCompress : : compressDXT1a ( rgba . color ( 0 ) , & block ) ;
BlockDXT1 * block = new ( output ) BlockDXT1 ;
OptimalCompress : : compressDXT1a ( rgba . color ( 0 ) , block ) ;
}
else
{
nvsquish : : ColourSet colours ( ( uint8 * ) rgba . colors ( ) , nvsquish : : kDxt1 | nvsquish : : kWeightColourByAlpha ) ;
nvsquish : : WeightedClusterFit fit ;
fit . SetMetric ( compressionOptions . colorWeight . x ( ) , compressionOptions . colorWeight . y ( ) , compressionOptions . colorWeight . z ( ) ) ;
int flags = nvsquish : : kDxt1 ;
if ( alphaMode = = nvtt : : AlphaMode_Transparency ) flags | = nvsquish : : kWeightColourByAlpha ;
nvsquish : : ColourSet colours ( ( uint8 * ) rgba . colors ( ) , flags ) ;
fit . SetColourSet ( & colours , nvsquish : : kDxt1 ) ;
fit . Compress ( & block ) ;
}
if ( outputOptions . outputHandler ! = NULL ) {
outputOptions . outputHandler - > writeData ( & block , sizeof ( block ) ) ;
}
}
fit . Compress ( output ) ;
}
}
void nv: : SlowCompressor : : compressDXT3 ( const CompressionOptions : : Private & compressionOptions , const OutputOptions : : Private & outputOptions )
void NormalCompressorDXT3: : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
const uint w = m_image - > width ( ) ;
const uint h = m_image - > height ( ) ;
ColorBlock rgba ;
BlockDXT3 block ;
nvsquish : : WeightedClusterFit fit ;
fit . SetMetric ( compressionOptions . colorWeight . x ( ) , compressionOptions . colorWeight . y ( ) , compressionOptions . colorWeight . z ( ) ) ;
int flags = 0 ;
if ( m_alphaMode = = AlphaMode_Transparency )
{
flags = nvsquish : : kWeightColourByAlpha ;
}
for ( uint y = 0 ; y < h ; y + = 4 ) {
for ( uint x = 0 ; x < w ; x + = 4 ) {
rgba . init ( m_image , x , y ) ;
BlockDXT3 * block = new ( output ) BlockDXT3 ;
// Compress explicit alpha.
OptimalCompress : : compressDXT3A ( rgba , & block . alpha ) ;
OptimalCompress : : compressDXT3A ( rgba , & block - > alpha ) ;
// Compress color.
if ( rgba . isSingleColor ( ) )
{
OptimalCompress : : compressDXT1 ( rgba . color ( 0 ) , & block . color ) ;
OptimalCompress : : compressDXT1 ( rgba . color ( 0 ) , & block - > color ) ;
}
else
{
nvsquish : : ColourSet colours ( ( uint8 * ) rgba . colors ( ) , flags ) ;
fit . SetColourSet ( & colours , 0 ) ;
fit . Compress ( & block . color ) ;
}
if ( outputOptions . outputHandler ! = NULL ) {
outputOptions . outputHandler - > writeData ( & block , sizeof ( block ) ) ;
}
}
}
}
void nv : : SlowCompressor : : compressDXT5 ( const CompressionOptions : : Private & compressionOptions , const OutputOptions : : Private & outputOptions )
{
const uint w = m_image - > width ( ) ;
const uint h = m_image - > height ( ) ;
ColorBlock rgba ;
BlockDXT5 block ;
nvsquish : : WeightedClusterFit fit ;
fit . SetMetric ( compressionOptions . colorWeight . x ( ) , compressionOptions . colorWeight . y ( ) , compressionOptions . colorWeight . z ( ) ) ;
int flags = 0 ;
if ( m_alphaMode = = AlphaMode_Transparency )
{
flags = nvsquish : : kWeightColourByAlpha ;
if ( alphaMode = = nvtt : : AlphaMode_Transparency ) flags | = nvsquish : : kWeightColourByAlpha ;
nvsquish : : ColourSet colours ( ( uint8 * ) rgba . colors ( ) , flags ) ;
fit . SetColourSet ( & colours , 0 ) ;
fit . Compress ( & block - > color ) ;
}
}
for ( uint y = 0 ; y < h ; y + = 4 ) {
for ( uint x = 0 ; x < w ; x + = 4 ) {
rgba . init ( m_image , x , y ) ;
void NormalCompressorDXT5 : : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
BlockDXT5 * block = new ( output ) BlockDXT5 ;
// Compress alpha.
if ( compressionOptions . quality = = Quality_Highest )
{
OptimalCompress : : compressDXT5A ( rgba , & block . alpha ) ;
OptimalCompress : : compressDXT5A ( rgba , & block - > alpha ) ;
}
else
{
QuickCompress : : compressDXT5A ( rgba , & block . alpha ) ;
QuickCompress : : compressDXT5A ( rgba , & block - > alpha ) ;
}
// Compress color.
if ( rgba . isSingleColor ( ) )
{
OptimalCompress : : compressDXT1 ( rgba . color ( 0 ) , & block . color ) ;
OptimalCompress : : compressDXT1 ( rgba . color ( 0 ) , & block - > color ) ;
}
else
{
nvsquish : : WeightedClusterFit fit ;
fit . SetMetric ( compressionOptions . colorWeight . x ( ) , compressionOptions . colorWeight . y ( ) , compressionOptions . colorWeight . z ( ) ) ;
int flags = 0 ;
if ( alphaMode = = nvtt : : AlphaMode_Transparency ) flags | = nvsquish : : kWeightColourByAlpha ;
nvsquish : : ColourSet colours ( ( uint8 * ) rgba . colors ( ) , flags ) ;
fit . SetColourSet ( & colours , 0 ) ;
fit . Compress ( & block . color ) ;
}
if ( outputOptions . outputHandler ! = NULL ) {
outputOptions . outputHandler - > writeData ( & block , sizeof ( block ) ) ;
}
}
fit . Compress ( & block - > color ) ;
}
}
void nv: : SlowCompressor : : compressDXT5n ( const CompressionOptions : : Private & compressionOptions , const OutputOptions : : Private & outputOptions )
void NormalCompressorDXT5n : : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
const uint w = m_image - > width ( ) ;
const uint h = m_image - > height ( ) ;
rgba . swizzle ( 4 , 1 , 5 , 0 ) ; // 0xFF, G, 0, R
ColorBlock rgba ;
BlockDXT5 block ;
nvsquish : : WeightedClusterFit fit ;
fit . SetMetric ( 0 , 1 , 0 ) ;
for ( uint y = 0 ; y < h ; y + = 4 ) {
for ( uint x = 0 ; x < w ; x + = 4 ) {
rgba . init ( m_image , x , y ) ;
rgba . swizzleDXT5n ( ) ;
BlockDXT5 * block = new ( output ) BlockDXT5 ;
// Compress X.
if ( compressionOptions . quality = = Quality_Highest )
{
OptimalCompress : : compressDXT5A ( rgba , & block . alpha ) ;
OptimalCompress : : compressDXT5A ( rgba , & block - > alpha ) ;
}
else
{
QuickCompress : : compressDXT5A ( rgba , & block . alpha ) ;
QuickCompress : : compressDXT5A ( rgba , & block - > alpha ) ;
}
// Compress Y.
//OptimalCompress::compressDXT1G(rgba, &block.color);
/*if (rgba.isSingleColor())
{
OptimalCompress : : compressDXT1G ( rgba . color ( 0 ) , & block . color ) ;
}
else */
if ( compressionOptions . quality = = Quality_Highest )
{
nvsquish : : ColourSet colours ( ( uint8 * ) rgba . colors ( ) , 0 ) ;
fit . SetColourSet ( & colours , 0 ) ;
fit . Compress ( & block . color ) ;
}
if ( outputOptions . outputHandler ! = NULL ) {
outputOptions . outputHandler - > writeData ( & block , sizeof ( block ) ) ;
OptimalCompress : : compressDXT1G ( rgba , & block - > color ) ;
}
}
}
}
void nv : : SlowCompressor : : compressBC4 ( const CompressionOptions : : Private & compressionOptions , const nvtt : : OutputOptions : : Private & outputOptions )
else
{
const uint w = m_image - > width ( ) ;
const uint h = m_image - > height ( ) ;
ColorBlock rgba ;
AlphaBlockDXT5 block ;
for ( uint y = 0 ; y < h ; y + = 4 ) {
for ( uint x = 0 ; x < w ; x + = 4 ) {
rgba . init ( m_image , x , y ) ;
if ( compressionOptions . quality = = Quality_Highest )
if ( rgba . isSingleColor ( ) )
{
OptimalCompress : : compressDXT 5A( rgba , & block ) ;
OptimalCompress : : compressDXT1G ( rgba . color ( 0 ) , & block - > color ) ;
}
else
{
QuickCompress : : compressDXT5A ( rgba , & block ) ;
}
nvsquish : : WeightedClusterFit fit ;
fit . SetMetric ( 0 , 1 , 0 ) ;
if ( outputOptions . outputHandler ! = NULL ) {
outputOptions . outputHandler - > writeData ( & block , sizeof ( block ) ) ;
}
int flags = 0 ;
if ( alphaMode = = nvtt : : AlphaMode_Transparency ) flags | = nvsquish : : kWeightColourByAlpha ;
nvsquish : : ColourSet colours ( ( uint8 * ) rgba . colors ( ) , flags ) ;
fit . SetColourSet ( & colours , 0 ) ;
fit . Compress ( & block - > color ) ;
}
}
}
void nv: : SlowCompressor : : compressBC5 ( const CompressionOptions : : Private & compressionOptions , const nvtt : : OutputOptions : : Private & outputOptions )
void ProductionCompressorBC4: : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
const uint w = m_image - > width ( ) ;
const uint h = m_image - > height ( ) ;
ColorBlock xcolor ;
ColorBlock ycolor ;
BlockATI2 block ;
BlockATI1 * block = new ( output ) BlockATI1 ;
for ( uint y = 0 ; y < h ; y + = 4 ) {
for ( uint x = 0 ; x < w ; x + = 4 ) {
rgba . swizzle ( 0 , 1 , 2 , 0 ) ; // Copy red to alpha
OptimalCompress : : compressDXT5A ( rgba , & block - > alpha ) ;
}
xcolor . init ( m_image , x , y ) ;
xcolor . splatX ( ) ;
void ProductionCompressorBC5 : : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
BlockATI2 * block = new ( output ) BlockATI2 ;
ycolor . init ( m_image , x , y ) ;
ycolor . splatY ( ) ;
rgba . swizzle ( 0 , 1 , 2 , 0 ) ; // Copy red to alpha
OptimalCompress : : compressDXT5A ( rgba , & block - > x ) ;
if ( compressionOptions . quality = = Quality_Highest )
{
OptimalCompress : : compressDXT5A ( xcolor , & block . x ) ;
OptimalCompress : : compressDXT5A ( ycolor , & block . y ) ;
}
else
{
QuickCompress : : compressDXT5A ( xcolor , & block . x ) ;
QuickCompress : : compressDXT5A ( ycolor , & block . y ) ;
rgba . swizzle ( 0 , 1 , 2 , 1 ) ; // Copy green to alpha
OptimalCompress : : compressDXT5A ( rgba , & block - > y ) ;
}
if ( outputOptions . outputHandler ! = NULL ) {
outputOptions . outputHandler - > writeData ( & block , sizeof ( block ) ) ;
}
}
}
}
# if defined(HAVE_S3QUANT)
void nv: : s3CompressDXT1 ( const Image * image , const OutputOptions : : Private & outputOptions )
void S3CompressorDXT1 : : compress ( nvtt : : InputFormat inputFormat , nvtt : : AlphaMode alphaMode , uint w , uint h , void * data , const nvtt : : CompressionOptions : : Private & compressionOptions , const nvtt : : OutputOptions : : Private & outputOptions )
{
const uint w = image - > width ( ) ;
const uint h = image - > height ( ) ;
float error = 0.0f ;
BlockDXT1 dxtBlock3 ;
@ -526,7 +399,7 @@ void nv::s3CompressDXT1(const Image * image, const OutputOptions::Private & outp
for ( uint y = 0 ; y < h ; y + = 4 ) {
for ( uint x = 0 ; x < w ; x + = 4 ) {
block . init ( i mage , x , y ) ;
block . init ( i nputFormat, w , h , data , x , y ) ;
// Init rgb block.
RGBBlock rgbBlock ;
@ -606,30 +479,47 @@ void nv::s3CompressDXT1(const Image * image, const OutputOptions::Private & outp
# if defined(HAVE_ATITC)
void nv: : atiCompressDXT1 ( const Image * image , const OutputOptions : : Private & outputOptions )
void AtiCompressorDXT1: : compress ( nvtt : : InputFormat inputFormat , nvtt : : AlphaMode alphaMode , uint w , uint h , void * data , const nvtt : : CompressionOptions : : Private & compressionOptions , const nvtt : : OutputOptions : : Private & outputOptions )
{
// Init source texture
ATI_TC_Texture srcTexture ;
srcTexture . dwSize = sizeof ( srcTexture ) ;
srcTexture . dwWidth = image - > width ( ) ;
srcTexture . dwHeight = image - > height ( ) ;
srcTexture . dwPitch = image - > width ( ) * 4 ;
srcTexture . dwWidth = w ;
srcTexture . dwHeight = h ;
if ( inputFormat = = nvtt : : InputFormat_BGRA_8UB )
{
srcTexture . dwPitch = w * 4 ;
srcTexture . format = ATI_TC_FORMAT_ARGB_8888 ;
}
else
{
srcTexture . dwPitch = w * 16 ;
srcTexture . format = ATI_TC_FORMAT_ARGB_32F ;
}
srcTexture . dwDataSize = ATI_TC_CalculateBufferSize ( & srcTexture ) ;
srcTexture . pData = ( ATI_TC_BYTE * ) image - > pixels ( ) ;
srcTexture . pData = ( ATI_TC_BYTE * ) data ;
// Init dest texture
ATI_TC_Texture destTexture ;
destTexture . dwSize = sizeof ( destTexture ) ;
destTexture . dwWidth = image- > width( ) ;
destTexture . dwHeight = image- > height( ) ;
destTexture . dwWidth = w;
destTexture . dwHeight = h;
destTexture . dwPitch = 0 ;
destTexture . format = ATI_TC_FORMAT_DXT1 ;
destTexture . dwDataSize = ATI_TC_CalculateBufferSize ( & destTexture ) ;
destTexture . pData = ( ATI_TC_BYTE * ) mem : : malloc ( destTexture . dwDataSize ) ;
ATI_TC_CompressOptions options ;
options . dwSize = sizeof ( options ) ;
options . bUseChannelWeighting = false ;
options . bUseAdaptiveWeighting = false ;
options . bDXT1UseAlpha = false ;
options . nCompressionSpeed = ATI_TC_Speed_Normal ;
options . bDisableMultiThreading = false ;
//options.bDisableMultiThreading = true;
// Compress
ATI_TC_ConvertTexture ( & srcTexture , & destTexture , NULL , NULL , NULL , NULL ) ;
ATI_TC_ConvertTexture ( & srcTexture , & destTexture , & options , NULL , NULL , NULL ) ;
if ( outputOptions . outputHandler ! = NULL ) {
outputOptions . outputHandler - > writeData ( destTexture . pData , destTexture . dwDataSize ) ;
@ -638,23 +528,31 @@ void nv::atiCompressDXT1(const Image * image, const OutputOptions::Private & out
mem : : free ( destTexture . pData ) ;
}
void nv: : atiCompressDXT5 ( const Image * image , const OutputOptions : : Private & outputOptions )
void AtiCompressorDXT5: : compress ( nvtt : : InputFormat inputFormat , nvtt : : AlphaMode alphaMode , uint w , uint h , void * data , const nvtt : : CompressionOptions : : Private & compressionOptions , const nvtt : : OutputOptions : : Private & outputOptions )
{
// Init source texture
ATI_TC_Texture srcTexture ;
srcTexture . dwSize = sizeof ( srcTexture ) ;
srcTexture . dwWidth = image - > width ( ) ;
srcTexture . dwHeight = image - > height ( ) ;
srcTexture . dwPitch = image - > width ( ) * 4 ;
srcTexture . dwWidth = w ;
srcTexture . dwHeight = h ;
if ( inputFormat = = nvtt : : InputFormat_BGRA_8UB )
{
srcTexture . dwPitch = w * 4 ;
srcTexture . format = ATI_TC_FORMAT_ARGB_8888 ;
}
else
{
srcTexture . dwPitch = w * 16 ;
srcTexture . format = ATI_TC_FORMAT_ARGB_32F ;
}
srcTexture . dwDataSize = ATI_TC_CalculateBufferSize ( & srcTexture ) ;
srcTexture . pData = ( ATI_TC_BYTE * ) image - > pixels ( ) ;
srcTexture . pData = ( ATI_TC_BYTE * ) data ;
// Init dest texture
ATI_TC_Texture destTexture ;
destTexture . dwSize = sizeof ( destTexture ) ;
destTexture . dwWidth = image- > width( ) ;
destTexture . dwHeight = image- > height( ) ;
destTexture . dwWidth = w;
destTexture . dwHeight = h;
destTexture . dwPitch = 0 ;
destTexture . format = ATI_TC_FORMAT_DXT5 ;
destTexture . dwDataSize = ATI_TC_CalculateBufferSize ( & destTexture ) ;
@ -674,8 +572,10 @@ void nv::atiCompressDXT5(const Image * image, const OutputOptions::Private & out
# if defined(HAVE_SQUISH)
void nv: : squishCompressDXT1 ( const Image * image , const OutputOptions : : Private & outputOptions )
void SquishCompressorDXT1: : compress ( nvtt : : InputFormat inputFormat , nvtt : : AlphaMode alphaMode , uint w , uint h , void * data , const nvtt : : CompressionOptions : : Private & compressionOptions , const nvtt : : OutputOptions : : Private & outputOptions )
{
# pragma message(NV_FILE_LINE "TODO: Convert input to fixed point ABGR format instead of ARGB")
/*
Image img ( * image ) ;
int count = img . width ( ) * img . height ( ) ;
for ( int i = 0 ; i < count ; i + + )
@ -694,6 +594,7 @@ void nv::squishCompressDXT1(const Image * image, const OutputOptions::Private &
}
mem : : free ( blocks ) ;
*/
}
# endif // defined(HAVE_SQUISH)
@ -701,7 +602,7 @@ void nv::squishCompressDXT1(const Image * image, const OutputOptions::Private &
# if defined(HAVE_D3DX)
void nv: : d3dxCompressDXT1 ( const Image * image , const OutputOptions : : Private & outputOptions )
void D3DXCompressorDXT1: : compress ( nvtt : : InputFormat inputFormat , nvtt : : AlphaMode alphaMode , uint w , uint h , void * data , const nvtt : : CompressionOptions : : Private & compressionOptions , const nvtt : : OutputOptions : : Private & outputOptions )
{
IDirect3D9 * d3d = Direct3DCreate9 ( D3D_SDK_VERSION ) ;
@ -719,7 +620,7 @@ void nv::d3dxCompressDXT1(const Image * image, const OutputOptions::Private & ou
err = d3d - > CreateDevice ( D3DADAPTER_DEFAULT , D3DDEVTYPE_REF , GetDesktopWindow ( ) , D3DCREATE_SOFTWARE_VERTEXPROCESSING , & presentParams , & device ) ;
IDirect3DTexture9 * texture = NULL ;
err = D3DXCreateTexture ( device , image- > width( ) , image- > height( ) , 1 , 0 , D3DFMT_DXT1 , D3DPOOL_SYSTEMMEM , & texture ) ;
err = D3DXCreateTexture ( device , w, h, 1 , 0 , D3DFMT_DXT1 , D3DPOOL_SYSTEMMEM , & texture ) ;
IDirect3DSurface9 * surface = NULL ;
err = texture - > GetSurfaceLevel ( 0 , & surface ) ;
@ -727,10 +628,17 @@ void nv::d3dxCompressDXT1(const Image * image, const OutputOptions::Private & ou
RECT rect ;
rect . left = 0 ;
rect . top = 0 ;
rect . bottom = image- > height( ) ;
rect . right = image- > width( ) ;
rect . bottom = h;
rect . right = w;
err = D3DXLoadSurfaceFromMemory ( surface , NULL , NULL , image - > pixels ( ) , D3DFMT_A8R8G8B8 , image - > width ( ) * sizeof ( Color32 ) , NULL , & rect , D3DX_DEFAULT , 0 ) ;
if ( inputFormat = = nvtt : : InputFormat_BGRA_8UB )
{
err = D3DXLoadSurfaceFromMemory ( surface , NULL , NULL , data , D3DFMT_A8R8G8B8 , w * 4 , NULL , & rect , D3DX_DEFAULT , 0 ) ;
}
else
{
err = D3DXLoadSurfaceFromMemory ( surface , NULL , NULL , data , D3DFMT_A32B32G32R32F , w * 16 , NULL , & rect , D3DX_DEFAULT , 0 ) ;
}
if ( err ! = D3DERR_INVALIDCALL & & err ! = D3DXERR_INVALIDDATA )
{
@ -740,7 +648,7 @@ void nv::d3dxCompressDXT1(const Image * image, const OutputOptions::Private & ou
err = surface - > LockRect ( & rect , NULL , D3DLOCK_READONLY ) ;
if ( outputOptions . outputHandler ! = NULL ) {
int size = rect . Pitch * ( ( image- > height( ) + 3 ) / 4 ) ;
int size = rect . Pitch * ( ( h + 3 ) / 4 ) ;
outputOptions . outputHandler - > writeData ( rect . pBits , size ) ;
}
@ -757,28 +665,11 @@ void nv::d3dxCompressDXT1(const Image * image, const OutputOptions::Private & ou
# if defined(HAVE_STB)
void nv: : stbCompressDXT1 ( const Image * image , const OutputOptions : : Private & outputOptions )
void StbCompressorDXT1: : compressBlock ( ColorBlock & rgba , nvtt : : AlphaMode alphaMode , const nvtt : : CompressionOptions : : Private & compressionOptions , void * output )
{
const uint w = image - > width ( ) ;
const uint h = image - > height ( ) ;
float error = 0.0f ;
BlockDXT1 dxtBlock ;
ColorBlock block ;
for ( uint y = 0 ; y < h ; y + = 4 ) {
for ( uint x = 0 ; x < w ; x + = 4 ) {
block . init ( image , x , y ) ;
block . swizzleSTB ( ) ;
stb_compress_dxt_block ( ( unsigned char * ) & dxtBlock , ( unsigned char * ) block . colors ( ) , 0 , 0 ) ;
if ( outputOptions . outputHandler ! = NULL ) {
outputOptions . outputHandler - > writeData ( & dxtBlock , sizeof ( dxtBlock ) ) ;
}
}
}
rgba . swizzle ( 2 , 1 , 0 , 3 ) ; // Swap R and B
stb_compress_dxt_block ( ( unsigned char * ) output , ( unsigned char * ) rgba . colors ( ) , 0 , 0 ) ;
}
# endif // defined(HAVE_STB)