Merge changes from the-witness.

This commit is contained in:
castano
2011-01-08 04:54:06 +00:00
parent 993e853a5f
commit 5f8cd22cdb
22 changed files with 401 additions and 194 deletions

View File

@ -24,6 +24,7 @@
#include "nvtt.h"
#include "CompressionOptions.h"
#include "nvimage/DirectDrawSurface.h"
using namespace nv;
using namespace nvtt;
@ -92,10 +93,10 @@ void CompressionOptions::setQuality(Quality quality)
/// perception more than a 7%. A better choice in my opinion is (3, 4, 2).
void CompressionOptions::setColorWeights(float red, float green, float blue, float alpha/*=1.0f*/)
{
// float total = red + green + blue;
// float x = red / total;
// float y = green / total;
// m.colorWeight.set(x, y, 1.0f - x - y);
// float total = red + green + blue;
// float x = red / total;
// float y = green / total;
// m.colorWeight.set(x, y, 1.0f - x - y);
m.colorWeight.set(red, green, blue, alpha);
}
@ -185,4 +186,64 @@ void CompressionOptions::setQuantization(bool colorDithering, bool alphaDitherin
}
// Translate to and from D3D formats.
unsigned int CompressionOptions::d3d9Format() const
{
if (m.format == Format_RGB) {
if (m.pixelType == PixelType_UnsignedNorm) {
uint bitcount = m.bitcount;
uint rmask = m.rmask;
uint gmask = m.gmask;
uint bmask = m.bmask;
uint amask = m.amask;
if (bitcount == 0) {
bitcount = m.rsize + m.gsize + m.bsize + m.asize;
rmask = ((1 << m.rsize) - 1) << (m.asize + m.bsize + m.gsize);
gmask = ((1 << m.gsize) - 1) << (m.asize + m.bsize);
bmask = ((1 << m.bsize) - 1) << m.asize;
amask = ((1 << m.asize) - 1) << 0;
}
if (bitcount <= 32) {
return nv::findD3D9Format(bitcount, rmask, gmask, bmask, amask);
}
}
return 0;
}
else {
uint d3d9_formats[] = {
0, // Format_RGB,
FOURCC_DXT1, // Format_DXT1
FOURCC_DXT1, // Format_DXT1a
FOURCC_DXT3, // Format_DXT3
FOURCC_DXT5, // Format_DXT5
FOURCC_DXT5, // Format_DXT5n
FOURCC_ATI1, // Format_BC4
FOURCC_ATI2, // Format_BC5
FOURCC_DXT1, // Format_DXT1n
0, // Format_CTX1
0, // Format_BC6
0, // Format_BC7
0, // Format_RGBE
};
return d3d9_formats[m.format];
}
}
/*
bool CompressionOptions::setDirect3D9Format(unsigned int format)
{
}
unsigned int CompressionOptions::dxgiFormat() const
{
}
bool CompressionOptions::setDXGIFormat(unsigned int format)
{
}
*/

View File

@ -144,23 +144,23 @@ void NormalCompressorDXT1::compressBlock(ColorSet & set, nvtt::AlphaMode alphaMo
}
}
#else
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())
{
BlockDXT1 * block = new(output) BlockDXT1;
OptimalCompress::compressDXT1(rgba.color(0), block);
}
else
{
nvsquish::ColourSet colours((uint8 *)rgba.colors(), 0);
fit.SetColourSet(&colours, nvsquish::kDxt1);
fit.Compress(output);
}
}
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())
{
BlockDXT1 * block = new(output) BlockDXT1;
OptimalCompress::compressDXT1(rgba.color(0), block);
}
else
{
nvsquish::ColourSet colours((uint8 *)rgba.colors(), 0);
fit.SetColourSet(&colours, nvsquish::kDxt1);
fit.Compress(output);
}
}
#endif
void NormalCompressorDXT1a::compressBlock(ColorBlock & rgba, nvtt::AlphaMode alphaMode, const nvtt::CompressionOptions::Private & compressionOptions, void * output)

View File

@ -110,7 +110,10 @@ namespace
{
nvDebugCheck(alignment >= 1);
flush();
putBits(0, ((size_t)ptr % alignment) * 8);
int remainder = (size_t)ptr % alignment;
if (remainder != 0) {
putBits(0, (alignment - remainder) * 8);
}
}
uint8 * ptr;

View File

@ -571,7 +571,7 @@ bool Compressor::Private::outputHeader(nvtt::TextureType textureType, int w, int
header.setSwizzleCode('A', '2', 'X', 'Y');
}
}
else if (compressionOptions.format == Format_BC6) {
else if (compressionOptions.format == Format_BC6) { // @@ This is not supported by D3DX. Always use DX10 header with BC6-7 formats.
header.setFourCC('Z', 'O', 'H', ' ');
}
else if (compressionOptions.format == Format_BC7) {
@ -586,6 +586,8 @@ bool Compressor::Private::outputHeader(nvtt::TextureType textureType, int w, int
supported = false;
}
}
if (outputOptions.srgb) header.setSrgbFlag(true);
}
if (!supported)
@ -595,9 +597,6 @@ bool Compressor::Private::outputHeader(nvtt::TextureType textureType, int w, int
return false;
}
// Swap bytes if necessary.
header.swapBytes();
uint headerSize = 128;
if (header.hasDX10Header())
{
@ -605,6 +604,9 @@ bool Compressor::Private::outputHeader(nvtt::TextureType textureType, int w, int
headerSize = 128 + 20;
}
// Swap bytes if necessary.
header.swapBytes();
bool writeSucceed = outputOptions.writeData(&header, headerSize);
if (!writeSucceed)
{
@ -796,37 +798,3 @@ CompressorInterface * Compressor::Private::chooseGpuCompressor(const Compression
return NULL;
}
/*
int Compressor::Private::estimateSize(const InputOptions::Private & inputOptions, const CompressionOptions::Private & compressionOptions) const
{
const Format format = compressionOptions.format;
const uint bitCount = compressionOptions.getBitCount();
const uint pitchAlignment = compressionOptions.pitchAlignment;
uint mipmapCount, width, height, depth;
inputOptions.computeExtents(&mipmapCount, &width, &height, &depth);
int size = 0;
for (uint f = 0; f < inputOptions.faceCount; f++)
{
uint w = inputOptions.targetWidth;
uint h = inputOptions.targetHeight;
uint d = inputOptions.targetDepth;
for (uint m = 0; m < mipmapCount; m++)
{
size += computeImageSize(w, h, d, bitCount, pitchAlignment, format);
// Compute extents of next mipmap:
w = max(1U, w / 2);
h = max(1U, h / 2);
d = max(1U, d / 2);
}
}
return size;
}
*/

View File

@ -51,6 +51,7 @@ void OutputOptions::reset()
m.outputHeader = true;
m.container = Container_DDS;
m.version = 0;
m.srgb = false;
}
@ -108,6 +109,12 @@ void OutputOptions::setUserVersion(int version)
m.version = version;
}
/// Set SRGB flag.
void OutputOptions::setSrgbFlag(bool b)
{
m.srgb = b;
}
bool OutputOptions::Private::hasValidOutputHandler() const
{
if (!fileName.isNull())

View File

@ -66,6 +66,7 @@ namespace nvtt
bool outputHeader;
Container container;
int version;
bool srgb;
bool hasValidOutputHandler() const;

View File

@ -234,6 +234,11 @@ void TexImage::setNormalMap(bool isNormalMap)
}
}
bool TexImage::isNull() const
{
return m->image == NULL;
}
int TexImage::width() const
{
if (m->image != NULL) return m->image->width();

View File

@ -147,6 +147,12 @@ namespace nvtt
NVTT_API void setPitchAlignment(int pitchAlignment);
NVTT_API void setQuantization(bool colorDithering, bool alphaDithering, bool binaryAlpha, int alphaThreshold = 127);
// Translate to and from D3D formats.
NVTT_API unsigned int d3d9Format() const;
//NVTT_API bool setD3D9Format(unsigned int format);
//NVTT_API unsigned int dxgiFormat() const;
//NVTT_API bool setDxgiFormat(unsigned int format);
};
/*
@ -329,6 +335,7 @@ namespace nvtt
NVTT_API void setOutputHeader(bool outputHeader);
NVTT_API void setContainer(Container container);
NVTT_API void setUserVersion(int version);
NVTT_API void setSrgbFlag(bool b);
};
typedef void Task(void * context, int id);
@ -392,6 +399,7 @@ namespace nvtt
NVTT_API void setNormalMap(bool isNormalMap);
// Queries.
NVTT_API bool isNull() const;
NVTT_API int width() const;
NVTT_API int height() const;
NVTT_API int depth() const;

View File

@ -451,7 +451,7 @@ int main(int argc, char *argv[])
}
// Block compressed textures with mipmaps must be powers of two.
//if (!noMipmaps && format != nvtt::Format_RGB)
if (!noMipmaps && format != nvtt::Format_RGB)
{
inputOptions.setRoundMode(nvtt::RoundMode_ToNearestPowerOfTwo);
}