diff --git a/src/nvimage/DirectDrawSurface.cpp b/src/nvimage/DirectDrawSurface.cpp index e94e7a8..e176853 100644 --- a/src/nvimage/DirectDrawSurface.cpp +++ b/src/nvimage/DirectDrawSurface.cpp @@ -1419,7 +1419,7 @@ uint DirectDrawSurface::mipmapSize(uint mipmap) const { nvDebugCheck((header.pf.flags & DDPF_RGB) || (header.pf.flags & DDPF_LUMINANCE)); - uint pitch = computePitch(w, header.pf.bitcount, 8); // Asuming 8 bit alignment, which is the same D3DX expects. + uint pitch = computeBytePitch(w, header.pf.bitcount, 8); // Asuming 8 bit alignment, which is the same D3DX expects. return pitch * h * d; } diff --git a/src/nvimage/ErrorMetric.cpp b/src/nvimage/ErrorMetric.cpp index 3a7b02f..c259a57 100644 --- a/src/nvimage/ErrorMetric.cpp +++ b/src/nvimage/ErrorMetric.cpp @@ -321,6 +321,7 @@ float nv::spatialCieLabError(const FloatImage * img0, const FloatImage * img1) */ // @@ Measure Delta E between lab0 and lab1. + return 0.0f; } diff --git a/src/nvimage/nvimage.h b/src/nvimage/nvimage.h index 4eff444..8ddb931 100644 --- a/src/nvimage/nvimage.h +++ b/src/nvimage/nvimage.h @@ -4,7 +4,9 @@ #ifndef NV_IMAGE_H #define NV_IMAGE_H -#include +#include "nvcore/nvcore.h" +#include "nvcore/Debug.h" // nvDebugCheck +#include "nvcore/Utils.h" // isPowerOfTwo // Function linkage #if NVIMAGE_SHARED @@ -21,13 +23,27 @@ #endif -// Some utility functions: +namespace nv { -inline uint computePitch(uint w, uint bitsize, uint alignment) -{ - return ((w * bitsize + 8 * alignment - 1) / (8 * alignment)) * alignment; -} + // Some utility functions: + inline uint computeBitPitch(uint w, uint bitsize, uint alignmentInBits) + { + nvDebugCheck(isPowerOfTwo(alignmentInBits)); + return ((w * bitsize + alignmentInBits - 1) / alignmentInBits) * alignmentInBits; + } + + inline uint computeBytePitch(uint w, uint bitsize, uint alignmentInBits) + { + nvDebugCheck(alignmentInBits >= 8); + + uint pitch = computeBitPitch(w, bitsize, alignmentInBits); + + return (pitch + 7) / 8; + } + + +} // nv namespace #endif // NV_IMAGE_H diff --git a/src/nvtt/CompressionOptions.cpp b/src/nvtt/CompressionOptions.cpp index 187ea4e..138f06a 100644 --- a/src/nvtt/CompressionOptions.cpp +++ b/src/nvtt/CompressionOptions.cpp @@ -63,7 +63,7 @@ void CompressionOptions::reset() m.asize = 8; m.pixelType = PixelType_UnsignedNorm; - m.pitchAlignment = 1; + m.pitchAlignment = 8; m.enableColorDithering = false; m.enableAlphaDithering = false; diff --git a/src/nvtt/CompressorRGB.cpp b/src/nvtt/CompressorRGB.cpp index 6433872..47907b1 100644 --- a/src/nvtt/CompressorRGB.cpp +++ b/src/nvtt/CompressorRGB.cpp @@ -188,11 +188,12 @@ void PixelFormatConverter::compress(nvtt::AlphaMode /*alphaMode*/, uint w, uint } } - const uint pitch = computePitch(w, bitCount, compressionOptions.pitchAlignment); + const uint pitch = computeBitPitch(w, bitCount, compressionOptions.pitchAlignment); + const uint pitchInBytes = (pitch + 7) / 8; const uint wh = w * h; // Allocate output scanline. - uint8 * const dst = malloc(pitch); + uint8 * const dst = malloc(pitchInBytes); for (uint y = 0; y < h; y++) { @@ -251,14 +252,15 @@ void PixelFormatConverter::compress(nvtt::AlphaMode /*alphaMode*/, uint w, uint // Zero padding. stream.align(compressionOptions.pitchAlignment); - nvDebugCheck(stream.ptr == dst + pitch); + nvDebugCheck(stream.ptr == dst + pitchInBytes); - /*for (uint x = w * byteCount; x < pitch; x++) + /*for (uint x = w * byteCount; x < pitchInBytes; x++) { *(dst + x) = 0; }*/ - outputOptions.writeData(dst, pitch); + // @@ This code does not truly support less than byte-aligned textures. + outputOptions.writeData(dst, pitchInBytes); } free(dst); diff --git a/src/nvtt/Context.cpp b/src/nvtt/Context.cpp index 7811555..2c991d7 100644 --- a/src/nvtt/Context.cpp +++ b/src/nvtt/Context.cpp @@ -479,7 +479,7 @@ bool Compressor::Private::outputHeader(nvtt::TextureType textureType, int w, int if (compressionOptions.format == Format_RGBA) { // Get output bit count. - header.setPitch(computePitch(w, compressionOptions.getBitCount(), compressionOptions.pitchAlignment)); + header.setPitch(computeBytePitch(w, compressionOptions.getBitCount(), compressionOptions.pitchAlignment)); if (compressionOptions.pixelType == PixelType_Float) { diff --git a/src/nvtt/TexImage.cpp b/src/nvtt/TexImage.cpp index a92f4a2..063b0d3 100644 --- a/src/nvtt/TexImage.cpp +++ b/src/nvtt/TexImage.cpp @@ -109,10 +109,10 @@ uint nv::countMipmaps(uint w, uint h, uint d) return mipmap + 1; } -uint nv::computeImageSize(uint w, uint h, uint d, uint bitCount, uint alignment, Format format) +uint nv::computeImageSize(uint w, uint h, uint d, uint bitCount, uint pitchAlignmentInBits, Format format) { if (format == Format_RGBA) { - return d * h * computePitch(w, bitCount, alignment); + return d * h * computeBytePitch(w, bitCount, pitchAlignmentInBits); } else { // @@ Handle 3D textures. DXT and VTC have different behaviors.