Experiment with output callbacks instead of handlers.
This commit is contained in:
parent
d38b89edc7
commit
a32d780347
@ -364,7 +364,7 @@ bool Compressor::Private::compress(const InputOptions::Private & inputOptions, c
|
||||
// Get output handler.
|
||||
if (!outputOptions.hasValidOutputHandler())
|
||||
{
|
||||
if (outputOptions.errorHandler) outputOptions.errorHandler->error(Error_FileOpen);
|
||||
outputOptions.error(Error_FileOpen);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -587,11 +587,7 @@ bool Compressor::Private::outputHeader(const InputOptions::Private & inputOption
|
||||
if (!supported)
|
||||
{
|
||||
// This container does not support the requested format.
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_UnsupportedOutputFormat);
|
||||
}
|
||||
|
||||
outputOptions.error(Error_UnsupportedOutputFormat);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -617,9 +613,9 @@ bool Compressor::Private::outputHeader(const InputOptions::Private & inputOption
|
||||
}
|
||||
|
||||
bool writeSucceed = outputOptions.outputHandler->writeData(&header, headerSize);
|
||||
if (!writeSucceed && outputOptions.errorHandler != NULL)
|
||||
if (!writeSucceed)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_FileWrite);
|
||||
outputOptions.error(Error_FileWrite);
|
||||
}
|
||||
|
||||
return writeSucceed;
|
||||
@ -632,14 +628,11 @@ bool Compressor::Private::outputHeader(const TexImage & tex, int mipmapCount, co
|
||||
{
|
||||
if (tex.width() <= 0 || tex.height() <= 0 || tex.depth() <= 0 || mipmapCount <= 0)
|
||||
{
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_InvalidInput);
|
||||
}
|
||||
outputOptions.error(Error_InvalidInput);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (outputOptions.outputHandler == NULL || !outputOptions.outputHeader)
|
||||
if (!outputOptions.outputHeader)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -831,11 +824,7 @@ bool Compressor::Private::outputHeader(const TexImage & tex, int mipmapCount, co
|
||||
if (!supported)
|
||||
{
|
||||
// This container does not support the requested format.
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_UnsupportedOutputFormat);
|
||||
}
|
||||
|
||||
outputOptions.error(Error_UnsupportedOutputFormat);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -861,9 +850,9 @@ bool Compressor::Private::outputHeader(const TexImage & tex, int mipmapCount, co
|
||||
}
|
||||
|
||||
bool writeSucceed = outputOptions.outputHandler->writeData(&header, headerSize);
|
||||
if (!writeSucceed && outputOptions.errorHandler != NULL)
|
||||
if (!writeSucceed)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_FileWrite);
|
||||
outputOptions.error(Error_FileWrite);
|
||||
}
|
||||
|
||||
return writeSucceed;
|
||||
@ -885,21 +874,15 @@ bool Compressor::Private::compressMipmaps(uint f, const InputOptions::Private &
|
||||
nvDebugCheck(mipmapCount > 0);
|
||||
|
||||
for (uint m = 0; m < mipmapCount; m++)
|
||||
{
|
||||
if (outputOptions.outputHandler)
|
||||
{
|
||||
int size = computeImageSize(w, h, d, compressionOptions.getBitCount(), compressionOptions.format);
|
||||
outputOptions.outputHandler->beginImage(size, w, h, d, f, m);
|
||||
}
|
||||
outputOptions.beginImage(size, w, h, d, f, m);
|
||||
|
||||
if (!initMipmap(mipmap, inputOptions, w, h, d, f, m))
|
||||
{
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_InvalidInput);
|
||||
outputOptions.error(Error_InvalidInput);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (compressionOptions.pixelType == PixelType_Float)
|
||||
{
|
||||
@ -1515,7 +1498,7 @@ bool Compressor::Private::compress2D(InputFormat inputFormat, AlphaMode alphaMod
|
||||
|
||||
if (compressor == NULL)
|
||||
{
|
||||
if (outputOptions.errorHandler) outputOptions.errorHandler->error(Error_UnsupportedFeature);
|
||||
outputOptions.error(Error_UnsupportedFeature);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -26,6 +26,22 @@
|
||||
using namespace nvtt;
|
||||
|
||||
|
||||
static void defaultBeginImageCallback(int size, int width, int height, int depth, int face, int miplevel, void * userData) {
|
||||
nvtt::OutputHandler * outputHandler = (nvtt::OutputHandler *)userData;
|
||||
if (outputHandler != NULL) outputHandler->beginImage(size, width, height, depth, face, miplevel);
|
||||
}
|
||||
|
||||
static bool defaultOutputCallback(const void * data, int size, void * userData) {
|
||||
nvtt::OutputHandler * outputHandler = (nvtt::OutputHandler *)userData;
|
||||
if (outputHandler != NULL) return outputHandler->writeData(data, size);
|
||||
}
|
||||
|
||||
static void defaultErrorCallback(Error e, void * userData) {
|
||||
nvtt::ErrorHandler * errorHandler = (nvtt::ErrorHandler *)userData;
|
||||
if (errorHandler != NULL) errorHandler->error(e);
|
||||
}
|
||||
|
||||
|
||||
OutputOptions::OutputOptions() : m(*new OutputOptions::Private())
|
||||
{
|
||||
reset();
|
||||
@ -43,8 +59,14 @@ OutputOptions::~OutputOptions()
|
||||
void OutputOptions::reset()
|
||||
{
|
||||
m.fileName.reset();
|
||||
|
||||
m.outputHandler = NULL;
|
||||
m.errorHandler = NULL;
|
||||
|
||||
m.beginImageCallback = defaultBeginImageCallback;
|
||||
m.outputCallback = defaultOutputCallback;
|
||||
m.errorCallback = defaultErrorCallback;
|
||||
|
||||
m.outputHeader = true;
|
||||
m.container = Container_DDS;
|
||||
}
|
||||
@ -61,6 +83,7 @@ void OutputOptions::setFileName(const char * fileName)
|
||||
{
|
||||
m.outputHandler = oh;
|
||||
}
|
||||
m.outputCallback = defaultOutputCallback;
|
||||
}
|
||||
|
||||
/// Set output handler.
|
||||
@ -72,6 +95,7 @@ void OutputOptions::setOutputHandler(OutputHandler * outputHandler)
|
||||
m.fileName.reset();
|
||||
}
|
||||
m.outputHandler = outputHandler;
|
||||
m.outputCallback = defaultOutputCallback;
|
||||
}
|
||||
|
||||
/// Set error handler.
|
||||
@ -103,3 +127,17 @@ bool OutputOptions::Private::hasValidOutputHandler() const
|
||||
return true;
|
||||
}
|
||||
|
||||
void OutputOptions::Private::beginImage(int size, int width, int height, int depth, int face, int miplevel) const
|
||||
{
|
||||
beginImageCallback(size, width, height, depth, face, miplevel, outputHandler);
|
||||
}
|
||||
|
||||
bool OutputOptions::Private::writeData(const void * data, int size) const
|
||||
{
|
||||
return outputCallback(data, size, outputHandler);
|
||||
}
|
||||
|
||||
void OutputOptions::Private::error(Error e) const
|
||||
{
|
||||
errorCallback(e, errorHandler);
|
||||
}
|
||||
|
@ -63,10 +63,19 @@ namespace nvtt
|
||||
|
||||
OutputHandler * outputHandler;
|
||||
ErrorHandler * errorHandler;
|
||||
|
||||
BeginImageCallback * beginImageCallback;
|
||||
OutputCallback * outputCallback;
|
||||
ErrorCallback * errorCallback;
|
||||
|
||||
bool outputHeader;
|
||||
Container container;
|
||||
|
||||
bool hasValidOutputHandler() const;
|
||||
|
||||
void beginImage(int size, int width, int height, int depth, int face, int miplevel) const;
|
||||
bool writeData(const void * data, int size) const;
|
||||
void error(Error e) const;
|
||||
};
|
||||
|
||||
|
||||
|
@ -199,17 +199,11 @@ void CudaCompressor::compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alp
|
||||
if (err != cudaSuccess)
|
||||
{
|
||||
//nvDebug("CUDA Error: %s\n", cudaGetErrorString(err));
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_CudaError);
|
||||
}
|
||||
outputOptions.error(Error_CudaError);
|
||||
}
|
||||
|
||||
// Output result.
|
||||
if (outputOptions.outputHandler != NULL)
|
||||
{
|
||||
outputOptions.outputHandler->writeData(h_result, count * bs);
|
||||
}
|
||||
outputOptions.writeData(h_result, count * bs);
|
||||
|
||||
bn += count;
|
||||
}
|
||||
@ -221,10 +215,7 @@ void CudaCompressor::compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alp
|
||||
cudaFreeArray(d_image);
|
||||
|
||||
#else
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_CudaError);
|
||||
}
|
||||
outputOptions.error(Error_CudaError);
|
||||
#endif
|
||||
|
||||
}
|
||||
@ -362,24 +353,17 @@ void CudaCompressor::compressDXT3(const CompressionOptions::Private & compressio
|
||||
if (err != cudaSuccess)
|
||||
{
|
||||
nvDebug("CUDA Error: %s\n", cudaGetErrorString(err));
|
||||
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_CudaError);
|
||||
}
|
||||
outputOptions.error(Error_CudaError);
|
||||
}
|
||||
|
||||
// Copy result to host, overwrite swizzled image.
|
||||
cudaMemcpy(blockLinearImage, m_ctx.result, count * 8, cudaMemcpyDeviceToHost);
|
||||
|
||||
// Output result.
|
||||
if (outputOptions.outputHandler != NULL)
|
||||
{
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
outputOptions.outputHandler->writeData(alphaBlocks + i, 8);
|
||||
outputOptions.outputHandler->writeData(blockLinearImage + i * 2, 8);
|
||||
}
|
||||
outputOptions.writeData(alphaBlocks + i, 8);
|
||||
outputOptions.writeData(blockLinearImage + i * 2, 8);
|
||||
}
|
||||
|
||||
bn += count;
|
||||
@ -392,10 +376,7 @@ void CudaCompressor::compressDXT3(const CompressionOptions::Private & compressio
|
||||
free(blockLinearImage);
|
||||
|
||||
#else
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_CudaError);
|
||||
}
|
||||
outputOptions.error(Error_CudaError);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -453,24 +434,17 @@ void CudaCompressor::compressDXT5(const CompressionOptions::Private & compressio
|
||||
if (err != cudaSuccess)
|
||||
{
|
||||
nvDebug("CUDA Error: %s\n", cudaGetErrorString(err));
|
||||
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_CudaError);
|
||||
}
|
||||
outputOptions.error(Error_CudaError);
|
||||
}
|
||||
|
||||
// Copy result to host, overwrite swizzled image.
|
||||
cudaMemcpy(blockLinearImage, m_ctx.result, count * 8, cudaMemcpyDeviceToHost);
|
||||
|
||||
// Output result.
|
||||
if (outputOptions.outputHandler != NULL)
|
||||
{
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
outputOptions.outputHandler->writeData(alphaBlocks + i, 8);
|
||||
outputOptions.outputHandler->writeData(blockLinearImage + i * 2, 8);
|
||||
}
|
||||
outputOptions.writeData(alphaBlocks + i, 8);
|
||||
outputOptions.writeData(blockLinearImage + i * 2, 8);
|
||||
}
|
||||
|
||||
bn += count;
|
||||
@ -483,10 +457,7 @@ void CudaCompressor::compressDXT5(const CompressionOptions::Private & compressio
|
||||
free(blockLinearImage);
|
||||
|
||||
#else
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_CudaError);
|
||||
}
|
||||
outputOptions.error(Error_CudaError);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -527,21 +498,14 @@ void CudaCompressor::compressDXT1n(const nvtt::CompressionOptions::Private & com
|
||||
if (err != cudaSuccess)
|
||||
{
|
||||
nvDebug("CUDA Error: %s\n", cudaGetErrorString(err));
|
||||
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_CudaError);
|
||||
}
|
||||
outputOptions.error(Error_CudaError);
|
||||
}
|
||||
|
||||
// Copy result to host, overwrite swizzled image.
|
||||
cudaMemcpy(blockLinearImage, m_ctx.result, count * 8, cudaMemcpyDeviceToHost);
|
||||
|
||||
// Output result.
|
||||
if (outputOptions.outputHandler != NULL)
|
||||
{
|
||||
outputOptions.outputHandler->writeData(blockLinearImage, count * 8);
|
||||
}
|
||||
outputOptions.writeData(blockLinearImage, count * 8);
|
||||
|
||||
bn += count;
|
||||
}
|
||||
@ -552,10 +516,7 @@ void CudaCompressor::compressDXT1n(const nvtt::CompressionOptions::Private & com
|
||||
free(blockLinearImage);
|
||||
|
||||
#else
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_CudaError);
|
||||
}
|
||||
outputOptions.error(Error_CudaError);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -597,20 +558,14 @@ void CudaCompressor::compressCTX1(const nvtt::CompressionOptions::Private & comp
|
||||
{
|
||||
nvDebug("CUDA Error: %s\n", cudaGetErrorString(err));
|
||||
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_CudaError);
|
||||
}
|
||||
outputOptions.error(Error_CudaError);
|
||||
}
|
||||
|
||||
// Copy result to host, overwrite swizzled image.
|
||||
cudaMemcpy(blockLinearImage, m_ctx.result, count * 8, cudaMemcpyDeviceToHost);
|
||||
|
||||
// Output result.
|
||||
if (outputOptions.outputHandler != NULL)
|
||||
{
|
||||
outputOptions.outputHandler->writeData(blockLinearImage, count * 8);
|
||||
}
|
||||
outputOptions.writeData(blockLinearImage, count * 8);
|
||||
|
||||
bn += count;
|
||||
}
|
||||
@ -621,10 +576,7 @@ void CudaCompressor::compressCTX1(const nvtt::CompressionOptions::Private & comp
|
||||
free(blockLinearImage);
|
||||
|
||||
#else
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_CudaError);
|
||||
}
|
||||
outputOptions.error(Error_CudaError);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -637,10 +589,7 @@ void CudaCompressor::compressDXT5n(const nvtt::CompressionOptions::Private & com
|
||||
// @@ TODO
|
||||
|
||||
#else
|
||||
if (outputOptions.errorHandler != NULL)
|
||||
{
|
||||
outputOptions.errorHandler->error(Error_CudaError);
|
||||
}
|
||||
outputOptions.error(Error_CudaError);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -316,6 +316,10 @@ namespace nvtt
|
||||
virtual void error(Error e) = 0;
|
||||
};
|
||||
|
||||
typedef void (BeginImageCallback)(int size, int width, int height, int depth, int face, int miplevel, void *);
|
||||
typedef bool (OutputCallback)(const void * data, int size, void *);
|
||||
typedef void (ErrorCallback)(Error e, void *);
|
||||
|
||||
/// Container.
|
||||
enum Container
|
||||
{
|
||||
@ -341,6 +345,11 @@ namespace nvtt
|
||||
|
||||
NVTT_API void setOutputHandler(OutputHandler * outputHandler);
|
||||
NVTT_API void setErrorHandler(ErrorHandler * errorHandler);
|
||||
|
||||
NVTT_API void setBeginImageCallback(BeginImageCallback * beginImageCallback);
|
||||
NVTT_API void setOutputCallback(OutputCallback * outputCallback);
|
||||
NVTT_API void setErrorCallback(ErrorCallback * errorCallback);
|
||||
|
||||
NVTT_API void setOutputHeader(bool outputHeader);
|
||||
NVTT_API void setContainer(Container container);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user