From ae2324153dc17bf920225dc0d905472bc2b6ec9b Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Sat, 9 Mar 2013 13:36:39 -0500 Subject: [PATCH] Repurpose the rest of our scaffolding to use Compression Jobs --- BPTCEncoder/include/BC7Compressor.h | 20 +++++----------- BPTCEncoder/src/BC7Compressor.cpp | 36 +++++++++++++---------------- Core/include/TexComp.h | 15 ++---------- Core/src/CompressedImage.cpp | 5 +++- Core/src/TexComp.cpp | 9 ++++---- Core/src/ThreadGroup.cpp | 13 ++++++----- Core/src/WorkerQueue.cpp | 6 +++-- 7 files changed, 44 insertions(+), 60 deletions(-) diff --git a/BPTCEncoder/include/BC7Compressor.h b/BPTCEncoder/include/BC7Compressor.h index 656979b..1bd6bb8 100755 --- a/BPTCEncoder/include/BC7Compressor.h +++ b/BPTCEncoder/include/BC7Compressor.h @@ -103,20 +103,12 @@ namespace BC7C // Compress the image given as RGBA data to BC7 format. Width and Height are the dimensions of // the image in pixels. - void CompressImageBC7( - const unsigned char *inBuf, - unsigned char *outBuf, - unsigned int width, - unsigned int height - ); + void Compress(const CompressionJob &); - void CompressImageBC7Stats( - const unsigned char *inBuf, - unsigned char *outBuf, - unsigned int width, - unsigned int height, - BlockStatManager &statManager - ); + // Perform a compression while recording all of the choices the compressor made into a + // list of statistics. We can use this to see whether or not certain heuristics are working, such as + // whether or not certain modes are being chosen more often than others, etc. + void CompressWithStats(const CompressionJob &, BlockStatManager &statManager); #ifdef HAS_SSE_41 // Compress the image given as RGBA data to BC7 format using an algorithm optimized for SIMD @@ -132,5 +124,5 @@ namespace BC7C #endif // Decompress the image given as BC7 data to R8G8B8A8 format. Width and Height are the dimensions of the image in pixels. - void DecompressImageBC7(const unsigned char* inBuf, unsigned char* outBuf, unsigned int width, unsigned int height); + void Decompress(const DecompressionJob &); } diff --git a/BPTCEncoder/src/BC7Compressor.cpp b/BPTCEncoder/src/BC7Compressor.cpp index b7ff093..df1da0c 100755 --- a/BPTCEncoder/src/BC7Compressor.cpp +++ b/BPTCEncoder/src/BC7Compressor.cpp @@ -1484,14 +1484,13 @@ namespace BC7C // 4-byte RGBA format. The width and height parameters specify the size of the image in pixels. // The buffer pointed to by outBuf should be large enough to store the compressed image. This // implementation has an 4:1 compression ratio. - void CompressImageBC7(const unsigned char *inBuf, unsigned char *outBuf, unsigned int width, unsigned int height) + void Compress(const CompressionJob &cj) { - const int kMaxIters = BC7CompressionMode::kMaxAnnealingIterations; - BC7CompressionMode::MaxAnnealingIterations = std::min(kMaxIters, GetQualityLevel()); - - for(uint32 j = 0; j < height; j += 4) + const unsigned char *inBuf = cj.inBuf; + unsigned char *outBuf = cj.outBuf; + for(uint32 j = 0; j < cj.height; j += 4) { - for(uint32 i = 0; i < width; i += 4) + for(uint32 i = 0; i < cj.width; i += 4) { // ExtractBlock(inBuf + i * 4, width, block); CompressBC7Block((const uint32 *)inBuf, outBuf); @@ -1582,19 +1581,15 @@ namespace BC7C } #endif // HAS_ATOMICS - void CompressImageBC7Stats( - const unsigned char *inBuf, - unsigned char *outBuf, - unsigned int width, - unsigned int height, + void CompressWithStats( + const CompressionJob &cj, BlockStatManager &statManager ) { - const int kMaxIters = BC7CompressionMode::kMaxAnnealingIterations; - BC7CompressionMode::MaxAnnealingIterations = std::min(kMaxIters, GetQualityLevel()); - - for(uint32 j = 0; j < height; j += 4) + const unsigned char *inBuf = cj.inBuf; + unsigned char *outBuf = cj.outBuf; + for(uint32 j = 0; j < cj.height; j += 4) { - for(uint32 i = 0; i < width; i += 4) + for(uint32 i = 0; i < cj.width; i += 4) { // ExtractBlock(inBuf + i * 4, width, block); CompressBC7Block((const uint32 *)inBuf, outBuf, statManager); @@ -2567,16 +2562,17 @@ namespace BC7C } // Convert the image from a BC7 buffer to a RGBA8 buffer - void DecompressImageBC7(const uint8 *inBuf, uint8* outBuf, unsigned int width, unsigned int height) { + void Decompress(const DecompressionJob &dj) { + unsigned char *outBuf = dj.outBuf; unsigned int blockIdx = 0; // for(unsigned int j = 0; j < height; j += 4, outBuf += width * 3 * 4) - for(unsigned int j = 0; j < height; j += 4) + for(unsigned int j = 0; j < dj.height; j += 4) { - for(unsigned int i = 0; i < width; i += 4) + for(unsigned int i = 0; i < dj.width; i += 4) { uint32 pixels[16]; - DecompressBC7Block(inBuf + (16*(blockIdx++)), pixels); + DecompressBC7Block(dj.inBuf + (16*(blockIdx++)), pixels); memcpy(outBuf, pixels, 16 * sizeof(uint32)); //memcpy(outBuf + (width * 4), pixels + 4, 4 * sizeof(uint32)); diff --git a/Core/include/TexComp.h b/Core/include/TexComp.h index 5784f4a..1ad8c84 100644 --- a/Core/include/TexComp.h +++ b/Core/include/TexComp.h @@ -103,24 +103,13 @@ extern bool CompressImageData( // returns the compressed image data into outData. It is assumed that there is // enough space allocated for outData to store the compressed data. Allocation // is dependent on the compression format. -typedef void (* CompressionFunc)( - const unsigned char *inData, // Raw image data - unsigned char *outData, // Buffer to store compressed data. - unsigned int width, // Image width - unsigned int height // Image height -); +typedef void (* CompressionFunc)(const CompressionJob &); // A compression function format. It takes the raw data and image dimensions and // returns the compressed image data into outData. It is assumed that there is // enough space allocated for outData to store the compressed data. Allocation // is dependent on the compression format. -typedef void (* CompressionFuncWithStats)( - const unsigned char *inData, // Raw image data - unsigned char *outData, // Buffer to store compressed data. - unsigned int width, // Image width - unsigned int height, // Image height - BlockStatManager &statManager// Stat manager -); +typedef void (* CompressionFuncWithStats)(const CompressionJob &, BlockStatManager &statManager); // This function computes the Peak Signal to Noise Ratio between a // compressed image and a raw image. diff --git a/Core/src/CompressedImage.cpp b/Core/src/CompressedImage.cpp index 0b6e949..7250840 100644 --- a/Core/src/CompressedImage.cpp +++ b/Core/src/CompressedImage.cpp @@ -127,7 +127,10 @@ bool CompressedImage::DecompressImage(unsigned char *outBuf, unsigned int outBuf switch(m_Format) { case eCompressionFormat_BPTC: - BC7C::DecompressImageBC7(m_Data, outBuf, m_Width, m_Height); + { + DecompressionJob dj (m_Data, outBuf, m_Width, m_Height); + BC7C::Decompress(dj); + } break; default: diff --git a/Core/src/TexComp.cpp b/Core/src/TexComp.cpp index 5da1a9c..1621fb5 100644 --- a/Core/src/TexComp.cpp +++ b/Core/src/TexComp.cpp @@ -82,7 +82,7 @@ static CompressionFuncWithStats ChooseFuncFromSettingsWithStats(const SCompress switch(s.format) { case eCompressionFormat_BPTC: { - return BC7C::CompressImageBC7Stats; + return BC7C::CompressWithStats; } break; @@ -105,7 +105,7 @@ static CompressionFunc ChooseFuncFromSettings(const SCompressionSettings &s) { return BC7C::CompressImageBC7SIMD; } #endif - return BC7C::CompressImageBC7; + return BC7C::Compress; } break; @@ -140,11 +140,12 @@ static double CompressImageInSerial( stopWatch.Start(); // !FIXME! We're assuming that we have 4x4 blocks here... + CompressionJob cj (imgData, outBuf, imgDataSz / 16, 4); if(fStats && settings.pStatManager) { - (*fStats)(imgData, outBuf, imgDataSz / 16, 4, *(settings.pStatManager)); + (*fStats)(cj, *(settings.pStatManager)); } else { - (*f)(imgData, outBuf, imgDataSz / 16, 4); + (*f)(cj); } stopWatch.Stop(); diff --git a/Core/src/ThreadGroup.cpp b/Core/src/ThreadGroup.cpp index 1587082..33a58eb 100644 --- a/Core/src/ThreadGroup.cpp +++ b/Core/src/ThreadGroup.cpp @@ -86,10 +86,11 @@ void CmpThread::operator()() { return; } + CompressionJob cj (m_InBuf, m_OutBuf, m_Width, m_Height); if(m_CmpFunc) - (*m_CmpFunc)(m_InBuf, m_OutBuf, m_Width, m_Height); + (*m_CmpFunc)(cj); else - (*m_CmpFuncWithStats)(m_InBuf, m_OutBuf, m_Width, m_Height, *m_StatManager); + (*m_CmpFuncWithStats)(cj, *m_StatManager); { TCLock lock(*m_ParentCounterLock); @@ -112,7 +113,7 @@ ThreadGroup::ThreadGroup( int numThreads, const unsigned char *inBuf, unsigned i , m_ThreadState(eThreadState_Done) , m_ExitFlag(false) , m_CompressedBlockSize( - (func == BC7C::CompressImageBC7 + (func == BC7C::Compress #ifdef HAS_SSE_41 || func == BC7C::CompressImageBC7SIMD #endif @@ -122,7 +123,7 @@ ThreadGroup::ThreadGroup( int numThreads, const unsigned char *inBuf, unsigned i 0 ) , m_UncompressedBlockSize( - (func == BC7C::CompressImageBC7 + (func == BC7C::Compress #ifdef HAS_SSE_41 || func == BC7C::CompressImageBC7SIMD #endif @@ -162,13 +163,13 @@ ThreadGroup::ThreadGroup( , m_ThreadState(eThreadState_Done) , m_ExitFlag(false) , m_CompressedBlockSize( - (func == BC7C::CompressImageBC7Stats)? + (func == BC7C::CompressWithStats)? 16 : 0 ) , m_UncompressedBlockSize( - (func == BC7C::CompressImageBC7Stats)? + (func == BC7C::CompressWithStats)? 64 : 0 diff --git a/Core/src/WorkerQueue.cpp b/Core/src/WorkerQueue.cpp index 2a0bfd3..7f74e56 100644 --- a/Core/src/WorkerQueue.cpp +++ b/Core/src/WorkerQueue.cpp @@ -100,10 +100,12 @@ void WorkerThread::operator()() { { const uint8 *src = m_Parent->GetSrcForThread(m_ThreadIdx); uint8 *dst = m_Parent->GetDstForThread(m_ThreadIdx); + + CompressionJob cj (src, dst, 4 * m_Parent->GetNumBlocksForThread(m_ThreadIdx), 4); if(f) - (*f)(src, dst, 4 * m_Parent->GetNumBlocksForThread(m_ThreadIdx), 4); + (*f)(cj); else - (*fStat)(src, dst, 4 * m_Parent->GetNumBlocksForThread(m_ThreadIdx), 4, *statManager); + (*fStat)(cj, *statManager); break; }