pull/216/head
castano 14 years ago
parent d8ed531e4c
commit a19e25228f

@ -9,6 +9,7 @@ SET(NVTT_SRCS
Context.cpp
nvtt_wrapper.h
nvtt_wrapper.cpp
Compressor.h
CompressDXT.h
CompressDXT.cpp
CompressRGB.h

@ -0,0 +1,40 @@
// Copyright Ignacio Castano <icastano@nvidia.com> 2009
//
// 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.
#ifndef NV_TT_COMPRESSOR_H
#define NV_TT_COMPRESSOR_H
#include <nvcore/nvcore.h> // uint
#include "nvtt.h"
namespace nv
{
struct CompressorInterface
{
virtual ~CompressorInterface() {}
virtual void compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alphaMode, uint w, uint h, const void * data, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions) = 0;
};
} // nv namespace
#endif // NV_TT_COMPRESSOR_H

@ -21,7 +21,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#include "CompressDXT.h"
#include "CompressorDXT.h"
#include "QuickCompressDXT.h"
#include "OptimalCompressDXT.h"
#include "CompressionOptions.h"
@ -32,13 +32,13 @@
#include "squish/fastclusterfit.h"
#include "squish/weightedclusterfit.h"
#include <nvtt/nvtt.h>
#include "nvtt.h"
#include <nvcore/Memory.h>
#include "nvcore/Memory.h"
#include <nvimage/Image.h>
#include <nvimage/ColorBlock.h>
#include <nvimage/BlockDXT.h>
#include "nvimage/Image.h"
#include "nvimage/ColorBlock.h"
#include "nvimage/BlockDXT.h"
// s3_quant
@ -71,16 +71,15 @@ typedef ULONG_PTR DWORD_PTR;
#include "stb/stb_dxt.h"
#endif
// OpenMP
#if defined (HAVE_OPENMP)
#pragma message(NV_FILE_LINE "FIXME: Define HAVE_OPENMP from cmake.")
#define HAVE_OPENMP
#include <omp.h>
#endif
using namespace nv;
using namespace nvtt;
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)
void FixedBlockCompressor::compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alphaMode, uint w, uint h, const void * data, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions)
{
const uint bs = blockSize();
const uint bw = (w + 3) / 4;

@ -21,26 +21,20 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#ifndef NV_TT_COMPRESSDXT_H
#define NV_TT_COMPRESSDXT_H
#ifndef NV_TT_COMPRESSORDXT_H
#define NV_TT_COMPRESSORDXT_H
#include <nvimage/nvimage.h>
#include <nvcore/nvcore.h>
#include "nvtt.h"
#include "Compressor.h"
namespace nv
{
class Image;
struct ColorBlock;
struct CompressorInterface
{
virtual ~CompressorInterface() {}
virtual void compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alphaMode, uint w, uint h, void * data, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions) = 0;
};
struct FixedBlockCompressor : public CompressorInterface
{
virtual void compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alphaMode, uint w, uint h, void * data, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions);
virtual void compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alphaMode, uint w, uint h, const void * data, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions);
virtual void compressBlock(ColorBlock & rgba, nvtt::AlphaMode alphaMode, const nvtt::CompressionOptions::Private & compressionOptions, void * output) = 0;
virtual uint blockSize() const = 0;
@ -182,4 +176,4 @@ namespace nv
} // nv namespace
#endif // NV_TT_COMPRESSDXT_H
#endif // NV_TT_COMPRESSORDXT_H

@ -21,7 +21,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#include "CompressRGB.h"
#include "CompressorRGB.h"
#include "CompressionOptions.h"
#include "OutputOptions.h"
@ -61,8 +61,119 @@ namespace
} // namespace
void PixelFormatConverter::compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alphaMode, uint w, uint h, const void * data, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions)
{
uint bitCount;
uint rmask, rshift, rsize;
uint gmask, gshift, gsize;
uint bmask, bshift, bsize;
uint amask, ashift, asize;
if (compressionOptions.pixelType == nvtt::PixelType_Float)
{
rsize = compressionOptions.rsize;
gsize = compressionOptions.gsize;
bsize = compressionOptions.bsize;
asize = compressionOptions.asize;
nvCheck(rsize == 0 || rsize == 16 || rsize == 32);
nvCheck(gsize == 0 || gsize == 16 || gsize == 32);
nvCheck(bsize == 0 || bsize == 16 || bsize == 32);
nvCheck(asize == 0 || asize == 16 || asize == 32);
bitCount = rsize + gsize + bsize + asize;
}
else
{
if (compressionOptions.bitcount != 0)
{
bitCount = compressionOptions.bitcount;
nvCheck(bitCount == 8 || bitCount == 16 || bitCount == 24 || bitCount == 32);
rmask = compressionOptions.rmask;
gmask = compressionOptions.gmask;
bmask = compressionOptions.bmask;
amask = compressionOptions.amask;
PixelFormat::maskShiftAndSize(rmask, &rshift, &rsize);
PixelFormat::maskShiftAndSize(gmask, &gshift, &gsize);
PixelFormat::maskShiftAndSize(bmask, &bshift, &bsize);
PixelFormat::maskShiftAndSize(amask, &ashift, &asize);
}
else
{
rsize = compressionOptions.rsize;
gsize = compressionOptions.gsize;
bsize = compressionOptions.bsize;
asize = compressionOptions.asize;
bitCount = rsize + gsize + bsize + asize;
nvCheck(bitCount <= 32);
ashift = 0;
bshift = ashift + asize;
gshift = bshift + bsize;
rshift = gshift + gsize;
rmask = ((1 << rsize) - 1) << rshift;
gmask = ((1 << gsize) - 1) << gshift;
bmask = ((1 << bsize) - 1) << bshift;
amask = ((1 << asize) - 1) << ashift;
}
}
uint byteCount = (bitCount + 7) / 8;
uint pitch = computePitch(w, bitCount);
uint srcPitchDiv4 = w;
if (inputFormat == nvtt::InputFormat_RGBA_32F) srcPitchDiv4 = w * 4;
// Allocate output scanline.
uint8 * dst = (uint8 *)mem::malloc(pitch + 4);
for (uint y = 0; y < h; y++)
{
const uint * src = (const uint *)data + srcPitchDiv4;
for (uint x = 0; x < w; x++)
{
float r, g, b, a;
if (inputFormat == nvtt::InputFormat_BGRA_8UB) {
Color32 c = Color32(src[x]);
r = float(c.r) / 255.0f;
g = float(c.g) / 255.0f;
b = float(c.b) / 255.0f;
a = float(c.a) / 255.0f;
}
else /*if (inputFormat == nvtt::InputFormat_RGBA_32F)*/ {
r = src[4 * x + 0];
g = src[4 * x + 1];
b = src[4 * x + 2];
a = src[4 * x + 3];
}
if (compressionOptions.pixelType == nvtt::PixelType_Float)
{
}
}
if (outputOptions.outputHandler != NULL)
{
outputOptions.outputHandler->writeData(dst, pitch);
}
}
mem::free(dst);
}
// Pixel format converter.
void nv::compressRGB(const Image * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions)
void compressRGB(const Image * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions)
{
nvCheck(image != NULL);
@ -166,7 +277,7 @@ void nv::compressRGB(const Image * image, const OutputOptions::Private & outputO
}
void nv::compressRGB(const FloatImage * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions)
void compressRGB(const FloatImage * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions)
{
nvCheck(image != NULL);

@ -21,21 +21,20 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#ifndef NV_TT_COMPRESSRGB_H
#define NV_TT_COMPRESSRGB_H
#ifndef NV_TT_COMPRESSORRGB_H
#define NV_TT_COMPRESSORRGB_H
#include "nvtt.h"
#include "Compressor.h"
namespace nv
{
class Image;
class FloatImage;
struct PixelFormatConverter : public CompressorInterface
{
virtual void compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alphaMode, uint w, uint h, const void * data, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions);
};
// Pixel format converter.
void compressRGB(const Image * image, const nvtt::OutputOptions::Private & outputOptions, const nvtt::CompressionOptions::Private & compressionOptions);
void compressRGB(const FloatImage * image, const nvtt::OutputOptions::Private & outputOptions, const nvtt::CompressionOptions::Private & compressionOptions);
} // nv namespace
#endif // NV_TT_COMPRESSDXT_H
#endif // NV_TT_COMPRESSORRGB_H

@ -43,10 +43,10 @@
#include "CompressionOptions.h"
#include "OutputOptions.h"
#include "CompressDXT.h"
#include "CompressRGB.h"
#include "CompressorDXT.h"
#include "CompressorRGB.h"
#include "cuda/CudaUtils.h"
#include "cuda/CudaCompressDXT.h"
#include "cuda/CudaCompressorDXT.h"
using namespace nv;
@ -303,8 +303,7 @@ int Compressor::estimateSize(const InputOptions & inputOptions, const Compressio
// RAW api.
bool Compressor::compress2D(InputFormat format, int w, int h, void * data, const CompressionOptions & compressionOptions, const OutputOptions & outputOptions) const
{
#pragma message(NV_FILE_LINE "TODO: Implement raw compress api")
return false;
return m.compress2D(format, AlphaMode_None, w, h, data, compressionOptions.m, outputOptions.m);
}
int Compressor::estimateSize(int w, int h, int d, const CompressionOptions & compressionOptions) const
@ -955,7 +954,11 @@ bool Compressor::Private::compressMipmaps(uint f, const InputOptions::Private &
quantizeMipmap(mipmap, compressionOptions);
}
compressMipmap(mipmap, inputOptions, compressionOptions, outputOptions);
const Image * image = mipmap.asFixedImage();
nvDebugCheck(image != NULL);
// @@ Ignore return value?
compress2D(InputFormat_BGRA_8UB, inputOptions.alphaMode, image->width(), image->height(), image->pixels(), compressionOptions, outputOptions);
// Compute extents of next mipmap:
w = max(1U, w / 2);
@ -1290,7 +1293,11 @@ void Compressor::Private::quantizeMipmap(Mipmap & mipmap, const CompressionOptio
CompressorInterface * Compressor::Private::chooseCpuCompressor(const CompressionOptions::Private & compressionOptions) const
{
if (compressionOptions.format == Format_DXT1)
if (compressionOptions.format == Format_RGB)
{
return new PixelFormatConverter;
}
else if (compressionOptions.format == Format_DXT1)
{
#if defined(HAVE_S3QUANT)
if (compressionOptions.externalCompressor == "s3") return new S3CompressorDXT1;
@ -1414,6 +1421,7 @@ CompressorInterface * Compressor::Private::chooseGpuCompressor(const Compression
return NULL;
}
#if defined HAVE_CUDA
if (compressionOptions.format == Format_DXT1)
{
return new CudaCompressorDXT1(*cuda);
@ -1428,11 +1436,11 @@ CompressorInterface * Compressor::Private::chooseGpuCompressor(const Compression
}
else if (compressionOptions.format == Format_DXT3)
{
return new CudaCompressorDXT3(*cuda);
//return new CudaCompressorDXT3(*cuda);
}
else if (compressionOptions.format == Format_DXT5)
{
return new CudaCompressorDXT5(*cuda);
//return new CudaCompressorDXT5(*cuda);
}
else if (compressionOptions.format == Format_DXT5n)
{
@ -1458,6 +1466,7 @@ CompressorInterface * Compressor::Private::chooseGpuCompressor(const Compression
{
// Not supported.
}
#endif // defined HAVE_CUDA
return NULL;
}
@ -1465,52 +1474,44 @@ CompressorInterface * Compressor::Private::chooseGpuCompressor(const Compression
// Compress the given mipmap.
bool Compressor::Private::compressMipmap(const Mipmap & mipmap, const InputOptions::Private & inputOptions, const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) const
/*bool Compressor::Private::compressMipmap(const Mipmap & mipmap, const InputOptions::Private & inputOptions, const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) const
{
// @@ This is broken, we should not assume the fixed image is the valid one. @@ Compressor should do conversion if necessary.
const Image * image = mipmap.asFixedImage();
nvDebugCheck(image != NULL);
return compress2D(InputFormat_BGRA_8UB, inputOptions.alphaMode, image->width(), image->height(), image->pixels(), compressionOptions, outputOptions);
}*/
bool Compressor::Private::compress2D(InputFormat inputFormat, AlphaMode alphaMode, int w, int h, const void * data, const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) const
{
if (compressionOptions.format == Format_RGBA)
// Decide what compressor to use.
CompressorInterface * compressor = NULL;
#if defined HAVE_CUDA
if (cudaEnabled && w * h >= 512)
{
// Pixel format conversion.
if (compressionOptions.pixelType == PixelType_Float)
{
compressRGB(mipmap.asFloatImage(), outputOptions, compressionOptions);
}
else
{
compressRGB(mipmap.asFixedImage(), outputOptions, compressionOptions);
}
compressor = chooseGpuCompressor(compressionOptions);
}
else
#endif
if (compressor == NULL)
{
const Image * image = mipmap.asFixedImage();
nvDebugCheck(image != NULL);
// Decide what compressor to use.
CompressorInterface * compressor = NULL;
if (cudaEnabled && image->width() * image->height() >= 512)
{
compressor = chooseGpuCompressor(compressionOptions);
}
if (compressor == NULL)
{
compressor = chooseCpuCompressor(compressionOptions);
}
compressor = chooseCpuCompressor(compressionOptions);
}
if (compressor == NULL)
{
if (outputOptions.errorHandler) outputOptions.errorHandler->error(Error_UnsupportedFeature);
}
else
{
compressor->compress(InputFormat_BGRA_8UB, inputOptions.alphaMode, image->width(), image->height(), (void *)image->pixels(), compressionOptions, outputOptions);
if (compressor == NULL)
{
if (outputOptions.errorHandler) outputOptions.errorHandler->error(Error_UnsupportedFeature);
}
else
{
compressor->compress(inputFormat, alphaMode, w, h, data, compressionOptions, outputOptions);
delete compressor;
}
delete compressor;
}
return true;
}
int Compressor::Private::estimateSize(const InputOptions::Private & inputOptions, const CompressionOptions::Private & compressionOptions) const
{
const Format format = compressionOptions.format;

@ -21,14 +21,13 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#ifndef NV_TT_COMPRESSOR_H
#define NV_TT_COMPRESSOR_H
#ifndef NV_TT_CONTEXT_H
#define NV_TT_CONTEXT_H
#include <nvcore/Ptr.h>
#include <nvtt/cuda/CudaCompressDXT.h>
#include <nvtt/CompressDXT.h>
#include "nvcore/Ptr.h"
#include "nvtt/Compressor.h"
#include "nvtt/cuda/CudaCompressorDXT.h"
#include "nvtt.h"
namespace nv
@ -46,7 +45,7 @@ namespace nvtt
bool compress(const InputOptions::Private & inputOptions, const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) const;
bool compress(const void * data, int width, int height, const CompressionOptions & compressionOptions, const OutputOptions & outputOptions) const;
bool compress2D(InputFormat inputFormat, AlphaMode alphaMode, int w, int h, const void * data, const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) const;
int estimateSize(const InputOptions::Private & inputOptions, const CompressionOptions::Private & compressionOptions) const;
@ -71,7 +70,6 @@ namespace nvtt
void premultiplyAlphaMipmap(Mipmap & mipmap, const InputOptions::Private & inputOptions) const;
void processInputImage(Mipmap & mipmap, const InputOptions::Private & inputOptions) const;
void quantizeMipmap(Mipmap & mipmap, const CompressionOptions::Private & compressionOptions) const;
bool compressMipmap(const Mipmap & mipmap, const InputOptions::Private & inputOptions, const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) const;
public:
@ -86,4 +84,4 @@ namespace nvtt
} // nvtt namespace
#endif // NV_TT_COMPRESSOR_H
#endif // NV_TT_CONTEXT_H

@ -32,7 +32,7 @@
#include <nvtt/QuickCompressDXT.h>
#include <nvtt/OptimalCompressDXT.h>
#include "CudaCompressDXT.h"
#include "CudaCompressorDXT.h"
#include "CudaUtils.h"
@ -62,8 +62,7 @@ extern "C" void compressKernelDXT3(uint firstBlock, uint blockNum, uint w, uint
//extern "C" void compressKernelCTX1(uint blockNum, uint * d_data, uint * d_result, uint * d_bitmaps);
#pragma message(NV_FILE_LINE "TODO: Rename Bitmaps.h to BitmapTable.h")
#include "Bitmaps.h"
#include "BitmapTable.h"
/*
// Convert linear image to block linear.
@ -141,13 +140,14 @@ bool CudaContext::isValid() const
}
#if defined HAVE_CUDA
CudaCompressor::CudaCompressor(CudaContext & ctx) : m_ctx(ctx)
{
}
void CudaCompressor::compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alphaMode, uint w, uint h, void * data, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions)
void CudaCompressor::compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alphaMode, uint w, uint h, const void * data, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions)
{
nvDebugCheck(cuda::isHardwarePresent());
@ -645,3 +645,5 @@ void CudaCompressor::compressDXT5n(const nvtt::CompressionOptions::Private & com
}
#endif // 0
#endif // defined HAVE_CUDA

@ -21,20 +21,16 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#ifndef NV_TT_CUDACOMPRESSDXT_H
#define NV_TT_CUDACOMPRESSDXT_H
#ifndef NV_TT_CUDACOMPRESSORDXT_H
#define NV_TT_CUDACOMPRESSORDXT_H
#include <nvimage/nvimage.h>
#include <nvtt/nvtt.h>
#include "nvtt/CompressDXT.h"
#include "nvtt/nvtt.h"
#include "../Compressor.h" // CompressorInterface
struct cudaArray;
namespace nv
{
class Image;
class CudaContext
{
public:
@ -51,12 +47,13 @@ namespace nv
uint * result;
};
#if defined HAVE_CUDA
struct CudaCompressor : public CompressorInterface
{
CudaCompressor(CudaContext & ctx);
virtual void compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alphaMode, uint w, uint h, void * data, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions);
virtual void compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alphaMode, uint w, uint h, const void * data, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions);
virtual void setup(cudaArray * image, const nvtt::CompressionOptions::Private & compressionOptions) = 0;
virtual void compressBlocks(uint first, uint count, uint w, uint h, nvtt::AlphaMode alphaMode, const nvtt::CompressionOptions::Private & compressionOptions, void * output) = 0;
@ -107,6 +104,8 @@ namespace nv
virtual uint blockSize() const { return 8; };
};*/
#endif // defined HAVE_CUDA
} // nv namespace

@ -21,6 +21,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#pragma once
#ifndef NV_TT_H
#define NV_TT_H

Loading…
Cancel
Save