2010-11-06 02:34:34 +00:00
|
|
|
// Copyright (c) 2009-2011 Ignacio Castano <castano@gmail.com>
|
|
|
|
// Copyright (c) 2007-2009 NVIDIA Corporation -- Ignacio Castano <icastano@nvidia.com>
|
2007-05-17 00:11:38 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2010-03-16 22:37:25 +00:00
|
|
|
#include "CompressorRGB.h"
|
2008-07-31 09:55:22 +00:00
|
|
|
#include "CompressionOptions.h"
|
|
|
|
#include "OutputOptions.h"
|
2007-07-17 09:16:28 +00:00
|
|
|
|
2011-09-27 05:17:01 +00:00
|
|
|
#include "nvimage/Image.h"
|
|
|
|
#include "nvimage/FloatImage.h"
|
|
|
|
#include "nvimage/PixelFormat.h"
|
2008-07-31 09:55:22 +00:00
|
|
|
|
2011-09-27 05:17:01 +00:00
|
|
|
#include "nvmath/Color.h"
|
|
|
|
#include "nvmath/Half.h"
|
2007-05-17 00:11:38 +00:00
|
|
|
|
2011-09-27 05:17:01 +00:00
|
|
|
#include "nvcore/Debug.h"
|
2007-05-17 00:11:38 +00:00
|
|
|
|
|
|
|
using namespace nv;
|
|
|
|
using namespace nvtt;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2010-11-03 18:31:16 +00:00
|
|
|
inline void convert_to_a8r8g8b8(const void * src, void * dst, uint w)
|
|
|
|
{
|
|
|
|
memcpy(dst, src, 4 * w);
|
|
|
|
}
|
2007-05-17 00:11:38 +00:00
|
|
|
|
2010-11-03 18:31:16 +00:00
|
|
|
inline void convert_to_x8r8g8b8(const void * src, void * dst, uint w)
|
|
|
|
{
|
|
|
|
memcpy(dst, src, 4 * w);
|
|
|
|
}
|
2007-05-17 00:11:38 +00:00
|
|
|
|
2010-03-17 02:25:06 +00:00
|
|
|
static uint16 to_half(float f)
|
|
|
|
{
|
2010-11-03 18:31:16 +00:00
|
|
|
union { float f; uint32 u; } c;
|
2010-03-17 02:25:06 +00:00
|
|
|
c.f = f;
|
|
|
|
return half_from_float(c.u);
|
|
|
|
}
|
|
|
|
|
2010-08-31 01:39:08 +00:00
|
|
|
struct BitStream
|
|
|
|
{
|
|
|
|
BitStream(uint8 * ptr) : ptr(ptr), buffer(0), bits(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void putBits(uint p, int bitCount)
|
|
|
|
{
|
|
|
|
nvDebugCheck(bits < 8);
|
|
|
|
nvDebugCheck(bitCount <= 32);
|
|
|
|
|
|
|
|
uint64 buffer = (this->buffer << bitCount) | p;
|
|
|
|
uint bits = this->bits + bitCount;
|
|
|
|
|
|
|
|
while (bits >= 8)
|
|
|
|
{
|
|
|
|
*ptr++ = (buffer & 0xFF);
|
|
|
|
|
|
|
|
buffer >>= 8;
|
|
|
|
bits -= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->buffer = (uint8)buffer;
|
|
|
|
this->bits = bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
void putFloat(float f)
|
|
|
|
{
|
|
|
|
nvDebugCheck(bits == 0);
|
|
|
|
*((float *)ptr) = f;
|
|
|
|
ptr += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
void putHalf(float f)
|
|
|
|
{
|
|
|
|
nvDebugCheck(bits == 0);
|
|
|
|
*((uint16 *)ptr) = to_half(f);
|
|
|
|
ptr += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush()
|
|
|
|
{
|
|
|
|
nvDebugCheck(bits < 8);
|
|
|
|
if (bits) {
|
|
|
|
*ptr++ = buffer;
|
|
|
|
buffer = 0;
|
|
|
|
bits = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void align(int alignment)
|
|
|
|
{
|
|
|
|
nvDebugCheck(alignment >= 1);
|
|
|
|
flush();
|
2011-09-27 17:48:46 +00:00
|
|
|
int remainder = (int)((uintptr_t)ptr % alignment);
|
2011-01-08 04:54:06 +00:00
|
|
|
if (remainder != 0) {
|
|
|
|
putBits(0, (alignment - remainder) * 8);
|
|
|
|
}
|
2010-08-31 01:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8 * ptr;
|
|
|
|
uint8 buffer;
|
|
|
|
uint8 bits;
|
|
|
|
};
|
|
|
|
|
2007-05-17 00:11:38 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2010-03-16 22:37:25 +00:00
|
|
|
|
2011-09-27 05:17:01 +00:00
|
|
|
void PixelFormatConverter::compress(nvtt::AlphaMode /*alphaMode*/, uint w, uint h, uint d, const float * data, nvtt::TaskDispatcher * dispatcher, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions)
|
2010-03-16 22:37:25 +00:00
|
|
|
{
|
2010-11-03 18:31:16 +00:00
|
|
|
nvDebugCheck (compressionOptions.format == nvtt::Format_RGBA);
|
|
|
|
|
2010-10-06 02:56:35 +00:00
|
|
|
uint bitCount;
|
|
|
|
uint rmask, rshift, rsize;
|
|
|
|
uint gmask, gshift, gsize;
|
|
|
|
uint bmask, bshift, bsize;
|
|
|
|
uint amask, ashift, asize;
|
2010-03-16 22:37:25 +00:00
|
|
|
|
|
|
|
if (compressionOptions.pixelType == nvtt::PixelType_Float)
|
|
|
|
{
|
2010-10-06 02:56:35 +00:00
|
|
|
rsize = compressionOptions.rsize;
|
|
|
|
gsize = compressionOptions.gsize;
|
|
|
|
bsize = compressionOptions.bsize;
|
|
|
|
asize = compressionOptions.asize;
|
2010-03-16 22:37:25 +00:00
|
|
|
|
2010-10-06 02:56:35 +00:00
|
|
|
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);
|
2010-03-16 22:37:25 +00:00
|
|
|
|
2010-10-06 02:56:35 +00:00
|
|
|
bitCount = rsize + gsize + bsize + asize;
|
2010-03-16 22:37:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-10-06 02:56:35 +00:00
|
|
|
if (compressionOptions.bitcount != 0)
|
|
|
|
{
|
|
|
|
bitCount = compressionOptions.bitcount;
|
|
|
|
nvCheck(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;
|
|
|
|
}
|
2010-03-16 22:37:25 +00:00
|
|
|
}
|
|
|
|
|
2011-09-13 17:08:09 +00:00
|
|
|
const uint pitch = computeBytePitch(w, bitCount, compressionOptions.pitchAlignment);
|
2011-09-27 05:17:01 +00:00
|
|
|
const uint whd = w * h * d;
|
2010-03-16 22:37:25 +00:00
|
|
|
|
|
|
|
// Allocate output scanline.
|
2011-09-13 17:08:09 +00:00
|
|
|
uint8 * const dst = malloc<uint8>(pitch);
|
2010-03-16 22:37:25 +00:00
|
|
|
|
2011-09-27 05:17:01 +00:00
|
|
|
for (uint z = 0; z < d; z++)
|
2010-10-06 02:56:35 +00:00
|
|
|
{
|
2011-09-27 05:17:01 +00:00
|
|
|
for (uint y = 0; y < h; y++)
|
2010-05-28 23:16:06 +00:00
|
|
|
{
|
2011-09-27 05:17:01 +00:00
|
|
|
const float * src = (const float *)data + y * w;
|
2010-03-16 22:37:25 +00:00
|
|
|
|
2011-09-27 05:17:01 +00:00
|
|
|
BitStream stream(dst);
|
2010-03-16 22:37:25 +00:00
|
|
|
|
2011-09-27 05:17:01 +00:00
|
|
|
for (uint x = 0; x < w; x++)
|
2010-11-03 18:31:16 +00:00
|
|
|
{
|
2011-09-27 05:17:01 +00:00
|
|
|
float r = src[x + 0 * whd];
|
|
|
|
float g = src[x + 1 * whd];
|
|
|
|
float b = src[x + 2 * whd];
|
|
|
|
float a = src[x + 3 * whd];
|
2010-11-03 18:31:16 +00:00
|
|
|
|
2011-09-27 05:17:01 +00:00
|
|
|
if (compressionOptions.pixelType == nvtt::PixelType_Float)
|
|
|
|
{
|
|
|
|
if (rsize == 32) stream.putFloat(r);
|
|
|
|
else if (rsize == 16) stream.putHalf(r);
|
2010-11-03 18:31:16 +00:00
|
|
|
|
2011-09-27 05:17:01 +00:00
|
|
|
if (gsize == 32) stream.putFloat(g);
|
|
|
|
else if (gsize == 16) stream.putHalf(g);
|
2010-11-03 18:31:16 +00:00
|
|
|
|
2011-09-27 05:17:01 +00:00
|
|
|
if (bsize == 32) stream.putFloat(b);
|
|
|
|
else if (bsize == 16) stream.putHalf(b);
|
|
|
|
|
|
|
|
if (asize == 32) stream.putFloat(a);
|
|
|
|
else if (asize == 16) stream.putHalf(a);
|
|
|
|
}
|
|
|
|
else
|
2010-05-28 23:16:06 +00:00
|
|
|
{
|
2011-09-27 05:17:01 +00:00
|
|
|
Color32 c;
|
|
|
|
if (compressionOptions.pixelType == nvtt::PixelType_UnsignedNorm) {
|
|
|
|
c.r = uint8(clamp(r * 255, 0.0f, 255.0f));
|
|
|
|
c.g = uint8(clamp(g * 255, 0.0f, 255.0f));
|
|
|
|
c.b = uint8(clamp(b * 255, 0.0f, 255.0f));
|
|
|
|
c.a = uint8(clamp(a * 255, 0.0f, 255.0f));
|
|
|
|
}
|
|
|
|
// @@ Add support for nvtt::PixelType_SignedInt, nvtt::PixelType_SignedNorm, nvtt::PixelType_UnsignedInt
|
|
|
|
|
|
|
|
uint p = 0;
|
|
|
|
p |= PixelFormat::convert(c.r, 8, rsize) << rshift;
|
|
|
|
p |= PixelFormat::convert(c.g, 8, gsize) << gshift;
|
|
|
|
p |= PixelFormat::convert(c.b, 8, bsize) << bshift;
|
|
|
|
p |= PixelFormat::convert(c.a, 8, asize) << ashift;
|
|
|
|
|
|
|
|
stream.putBits(p, bitCount);
|
|
|
|
}
|
2010-03-17 02:25:06 +00:00
|
|
|
}
|
2008-07-31 09:55:22 +00:00
|
|
|
|
2011-09-27 05:17:01 +00:00
|
|
|
// Zero padding.
|
|
|
|
stream.align(compressionOptions.pitchAlignment);
|
|
|
|
nvDebugCheck(stream.ptr == dst + pitch);
|
2010-08-31 01:39:08 +00:00
|
|
|
|
2011-09-27 05:17:01 +00:00
|
|
|
// Scanlines are always byte-aligned.
|
|
|
|
outputOptions.writeData(dst, pitch);
|
|
|
|
}
|
2010-03-17 02:25:06 +00:00
|
|
|
}
|
2008-07-31 02:04:44 +00:00
|
|
|
|
2010-10-06 02:56:35 +00:00
|
|
|
free(dst);
|
2008-07-31 02:04:44 +00:00
|
|
|
}
|