diff --git a/src/nvtt/CMakeLists.txt b/src/nvtt/CMakeLists.txt index a1dd09d..ea5cc94 100644 --- a/src/nvtt/CMakeLists.txt +++ b/src/nvtt/CMakeLists.txt @@ -24,6 +24,7 @@ SET(NVTT_SRCS InputOptions.cpp OutputOptions.h OutputOptions.cpp + Texture.h Texture.cpp cuda/CudaUtils.h cuda/CudaUtils.cpp cuda/CudaMath.h diff --git a/src/nvtt/Texture.cpp b/src/nvtt/Texture.cpp new file mode 100644 index 0000000..86b6b59 --- /dev/null +++ b/src/nvtt/Texture.cpp @@ -0,0 +1,131 @@ +// Copyright NVIDIA Corporation 2007 -- Ignacio Castano +// +// 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. + +#include "Texture.h" + +using namespace nvtt; + + +Texture::Texture() : m(*new Texture::Private()) +{ +} + +Texture::~Texture() +{ + delete &m; +} + +Texture::Texture(const Texture & tex) : m(*new Texture::Private()) +{ + // @@ Not implemented. +} + +void Texture::operator=(const Texture & tex) +{ + // @@ Not implemented. +} + +bool Texture::load(const char * fileName) +{ + // @@ Not implemented. + return false; +} + +void Texture::setType(TextureType type) +{ + m.type = type; +} + +void Texture::setTexture2D(InputFormat format, int w, int h, int idx, void * data) +{ + // @@ Not implemented. +} + +void Texture::resize(int w, int h, ResizeFilter filter) +{ + // if cubemap, make sure w==h.s + // @@ Not implemented. +} + +bool Texture::buildMipmap(MipmapFilter filter) +{ + // @@ Not implemented. +} + +// Color transforms. +void Texture::toLinear(float gamma) +{ + foreach(i, m.imageArray) + { + m.imageArray[i].toLinear(0, 3, gamma); + } +} + +void Texture::toGamma(float gamma) +{ + foreach(i, m.imageArray) + { + m.imageArray[i].toGamma(0, 3, gamma); + } +} + +void Texture::transform(const float w0[4], const float w1[4], const float w2[4], const float w3[4], const float offset[4]) +{ + // @@ Not implemented. +} + +void Texture::swizzle(int r, int g, int b, int a) +{ + foreach(i, m.imageArray) + { + m.imageArray[i].swizzle(0, r, g, b, a); + } +} + +void Texture::scaleBias(int channel, float scale, float bias) +{ + foreach(i, m.imageArray) + { + m.imageArray[i].scaleBias(channel, 1, scale, bias); + } +} + +void Texture::normalize() +{ + foreach(i, m.imageArray) + { + m.imageArray[i].normalize(0); + } +} + +void Texture::blend(float r, float g, float b, float a) +{ + // @@ Not implemented. +} + +void Texture::premultiplyAlpha() +{ + // @@ Not implemented. +} + + diff --git a/src/nvtt/Texture.h b/src/nvtt/Texture.h new file mode 100644 index 0000000..5b129f3 --- /dev/null +++ b/src/nvtt/Texture.h @@ -0,0 +1,47 @@ +// Copyright NVIDIA Corporation 2007 -- Ignacio Castano +// +// 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_TEXTURE_H +#define NV_TT_TEXTURE_H + +#include "nvtt.h" + +#include + +#include +#include + +namespace nvtt +{ + + struct Texture::Private + { + TextureType type; + nv::Array imageArray; + }; + + +} // nvtt namespace + + +#endif // NV_TT_TEXTURE_H diff --git a/src/nvtt/experimental/nvtt_experimental.cpp b/src/nvtt/experimental/nvtt_experimental.cpp index 2291459..7d612aa 100644 --- a/src/nvtt/experimental/nvtt_experimental.cpp +++ b/src/nvtt/experimental/nvtt_experimental.cpp @@ -1,16 +1,16 @@ #include "nvtt_experimental.h" -struct NvttImage +struct NvttTexture { - NvttImage() : + NvttTexture() : m_constant(false), m_image(NULL), m_floatImage(NULL) { } - ~NvttImage() + ~NvttTexture() { if (m_constant && m_image) m_image->unwrap(); delete m_image; @@ -22,14 +22,14 @@ struct NvttImage FloatImage * m_floatImage; }; -NvttImage * nvttCreateImage() +NvttTexture * nvttCreateTexture() { - return new NvttImage(); + return new NvttTexture(); } -void nvttDestroyImage(NvttImage * img) +void nvttDestroyTexture(NvttTexture * tex) { - delete img; + delete tex; } void nvttSetImageData(NvttImage * img, NvttInputFormat format, uint w, uint h, void * data) diff --git a/src/nvtt/experimental/nvtt_experimental.h b/src/nvtt/experimental/nvtt_experimental.h index 6870ae9..22991f7 100644 --- a/src/nvtt/experimental/nvtt_experimental.h +++ b/src/nvtt/experimental/nvtt_experimental.h @@ -4,14 +4,30 @@ #include -typedef struct NvttImage NvttImage; +typedef struct NvttTexture NvttTexture; +typedef struct NvttOutputOptions NvttOutputOptions; + + +// Global functions +void nvttInitialize(...); +unsigned int nvttGetVersion(); +const char * nvttGetErrorString(unsigned int error); + + +// Texture functions +NvttTexture * nvttCreateTexture(); +void nvttDestroyTexture(NvttTexture * tex); + +void nvttSetTexture2D(NvttTexture * tex, NvttInputFormat format, uint w, uint h, uint idx, void * data); + +void nvttResize(NvttTexture * img, uint w, uint h); +unsigned int nvttDownsample(NvttTexture * img); + +void nvttOutputCompressed(NvttTexture * img, NvttOutputFormat format); +void nvttOutputPixelFormat(NvttTexture * img, NvttOutputFormat format); -NvttImage * nvttCreateImage(); -void nvttDestroyImage(NvttImage * img); -void nvttSetImageData(NvttImage * img, NvttInputFormat format, uint w, uint h, void * data); -void nvttCompressImage(NvttImage * img, NvttFormat format); // How to control the compression parameters? @@ -50,6 +66,7 @@ void nvttCompressImage(NvttImage * img, NvttFormat format); // - Return it explicitely from nvttCompressImage. // - Store it along the image, retrieve later explicitely with 'nvttGetCompressedData(img, ...)' +/* // Global functions void nvttInitialize(...); @@ -80,7 +97,7 @@ void nvttResizeImage(NvttImage * image, unsigned int w, unsigned int h); void nvttQuantizeImage(NvttImage * image, bool dither, unsigned int rbits, unsigned int gbits, unsigned int bbits, unsigned int abits); void nvttCompressImage(NvttImage * image, void * buffer, int size); - +*/ #endif // NVTT_EXPERIMENTAL_H diff --git a/src/nvtt/experimental/test.cpp b/src/nvtt/experimental/test.cpp new file mode 100644 index 0000000..a60ee61 --- /dev/null +++ b/src/nvtt/experimental/test.cpp @@ -0,0 +1,61 @@ + +#include "nvtt_experimental.h" + +/* +Errors in the original API: +- Too many memory copies. +- Implementation too complicated. +- Error output should not be in output options. +- Data driven interface. Follows the dialog model. Provide all the data upfront. +*/ + + +// Output texture with mipmaps +void example0() +{ + CompressionOptions compressionOptions; + OutputOptions outputOptions; + + Texture img; + img.setTexture2D(format, w, h, 0, data); + + Compressor context; + context.outputHeader(outputOptions); + context.outputCompressed(img, compressionOptions, outputOptions); + + img.toLinear(2.2); + while (img.downsample(NVTT_FILTER_BOX)) + { + img.toGamma(2.2); + outputCompressed(img, compressionOptions, outputOptions); + } +} + + +// Output texture with colored mipmaps +void example1() +{ + CompressionOptions compressionOptions; + OutputOptions outputOptions; + + Texture img; + img.setTexture2D(format, w, h, 0, data); + + Compressor context; + context.outputHeader(outputOptions); + context.outputCompressed(img, compressionOptions, outputOptions); + + img.toLinear(2.2); + while (img.downsample(NVTT_FILTER_BOX)) + { + img.toGamma(2.2); + + Texture mipmap = img; + mipmap.blend(color[i].r, color[i].g, color[i].b, 0.5f); + + context.outputCompressed(mipmap, compressionOptions, outputOptions); + } +} + + + diff --git a/src/nvtt/nvtt.h b/src/nvtt/nvtt.h index 0e76369..5b7d1f2 100644 --- a/src/nvtt/nvtt.h +++ b/src/nvtt/nvtt.h @@ -49,11 +49,14 @@ #define NVTT_VERSION 201 -#define NVTT_DECLARE_PIMPL(Class) \ +#define NVTT_FORBID_COPY(Class) \ private: \ Class(const Class &); \ void operator=(const Class &); \ public: \ + +#define NVTT_DECLARE_PIMPL(Class) \ + public: \ struct Private; \ Private & m @@ -61,6 +64,9 @@ // Public interface. namespace nvtt { + // Forward declarations. + struct Texture; + /// Supported compression formats. enum Format { @@ -110,6 +116,7 @@ namespace nvtt /// Compression options. This class describes the desired compression format and other compression settings. struct CompressionOptions { + NVTT_FORBID_COPY(CompressionOptions); NVTT_DECLARE_PIMPL(CompressionOptions); NVTT_API CompressionOptions(); @@ -174,6 +181,15 @@ namespace nvtt MipmapFilter_Kaiser, ///< Kaiser-windowed Sinc filter is the best downsampling filter. }; + /// Texture resize filters. + enum ResizeFilter + { + ResizeFilter_Box, + ResizeFilter_Triangle, + ResizeFilter_Kaiser, + ResizeFilter_Mitchell, + }; + /// Color transformation. enum ColorTransform { @@ -204,6 +220,7 @@ namespace nvtt /// Input options. Specify format and layout of the input texture. struct InputOptions { + NVTT_FORBID_COPY(InputOptions); NVTT_DECLARE_PIMPL(InputOptions); NVTT_API InputOptions(); @@ -304,6 +321,7 @@ namespace nvtt /// the compressor to the user. struct OutputOptions { + NVTT_FORBID_COPY(OutputOptions); NVTT_DECLARE_PIMPL(OutputOptions); NVTT_API OutputOptions(); @@ -324,6 +342,7 @@ namespace nvtt /// Texture compressor. struct Compressor { + NVTT_FORBID_COPY(Compressor); NVTT_DECLARE_PIMPL(Compressor); NVTT_API Compressor(); @@ -337,15 +356,53 @@ namespace nvtt // Estimate the size of compressing the input with the given options. NVTT_API int estimateSize(const InputOptions & inputOptions, const CompressionOptions & compressionOptions) const; + + NVTT_API void outputCompressed(const Texture & tex, const OutputOptions & outputOptions); }; + - + /// Texture data. + struct Texture + { + NVTT_DECLARE_PIMPL(Texture); + + NVTT_API Texture(); + NVTT_API ~Texture(); + + Texture(const Texture & tex); + void operator=(const Texture & tex); + + NVTT_API bool load(const char * fileName); // @@ Input callbacks? + + NVTT_API void setType(TextureType type); + NVTT_API void setTexture2D(InputFormat format, int w, int h, int idx, void * data); + + // Resizing + NVTT_API void resize(int w, int h, ResizeFilter filter); + NVTT_API bool buildMipmap(MipmapFilter filter); + + // Color transforms. + NVTT_API void toLinear(float gamma); + NVTT_API void toGamma(float gamma); + NVTT_API void transform(const float w0[4], const float w1[4], const float w2[4], const float w3[4], const float offset[4]); + NVTT_API void swizzle(int r, int g, int b, int a); + NVTT_API void scaleBias(int channel, float scale, float bias); + NVTT_API void normalize(); + NVTT_API void blend(float r, float g, float b, float a); + NVTT_API void premultiplyAlpha(); + }; + + // Return string for the given error code. NVTT_API const char * errorString(Error e); // Return NVTT version. NVTT_API unsigned int version(); + // Set callbacks. + //NVTT_API void setErrorCallback(ErrorCallback callback); + //NVTT_API void setMemoryCallbacks(...); + } // nvtt namespace #endif // NV_TT_H