2012-09-21 20:57:45 +00:00
|
|
|
#include "WorkerQueue.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2012-09-29 19:36:42 +00:00
|
|
|
#include <assert.h>
|
2012-11-01 22:56:13 +00:00
|
|
|
#include <algorithm>
|
2012-09-21 22:14:38 +00:00
|
|
|
|
2012-11-01 22:56:13 +00:00
|
|
|
#include "BC7Compressor.h"
|
2012-09-21 22:14:38 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline void clamp(T &x, const T &min, const T &max) {
|
|
|
|
if(x < min) x = min;
|
|
|
|
else if(x > max) x = max;
|
|
|
|
}
|
2012-09-21 20:57:45 +00:00
|
|
|
|
|
|
|
WorkerThread::WorkerThread(WorkerQueue * parent, uint32 idx)
|
2012-09-29 19:36:42 +00:00
|
|
|
: TCCallable()
|
|
|
|
, m_ThreadIdx(idx)
|
2012-09-21 20:57:45 +00:00
|
|
|
, m_Parent(parent)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void WorkerThread::operator()() {
|
|
|
|
|
|
|
|
if(!m_Parent) {
|
|
|
|
fprintf(stderr, "%s\n", "Illegal worker thread initialization -- parent is NULL.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompressionFunc f = m_Parent->GetCompressionFunc();
|
2012-11-01 22:56:13 +00:00
|
|
|
CompressionFuncWithStats fStat = m_Parent->GetCompressionFuncWithStats();
|
|
|
|
BlockStatManager *statManager = m_Parent->GetBlockStatManager();
|
|
|
|
|
|
|
|
if(!(f || (fStat && statManager))) {
|
2012-09-21 20:57:45 +00:00
|
|
|
fprintf(stderr, "%s\n", "Illegal worker queue initialization -- compression func is NULL.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-25 21:05:52 +00:00
|
|
|
bool quitFlag = false;
|
|
|
|
while(!quitFlag) {
|
2012-09-21 22:14:38 +00:00
|
|
|
|
2012-09-25 21:05:52 +00:00
|
|
|
switch(m_Parent->AcceptThreadData(m_ThreadIdx))
|
|
|
|
{
|
|
|
|
|
|
|
|
case eAction_Quit:
|
|
|
|
{
|
|
|
|
quitFlag = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case eAction_Wait:
|
|
|
|
{
|
2012-09-29 19:36:42 +00:00
|
|
|
TCThread::Yield();
|
2012-09-25 21:05:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case eAction_DoWork:
|
|
|
|
{
|
|
|
|
const uint8 *src = m_Parent->GetSrcForThread(m_ThreadIdx);
|
|
|
|
uint8 *dst = m_Parent->GetDstForThread(m_ThreadIdx);
|
2012-11-01 22:56:13 +00:00
|
|
|
if(f)
|
|
|
|
(*f)(src, dst, 4 * m_Parent->GetNumBlocksForThread(m_ThreadIdx), 4);
|
|
|
|
else
|
|
|
|
(*fStat)(src, dst, 4 * m_Parent->GetNumBlocksForThread(m_ThreadIdx), 4, *statManager);
|
|
|
|
|
2012-09-25 21:05:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unrecognized thread command!\n");
|
|
|
|
quitFlag = true;
|
|
|
|
break;
|
|
|
|
}
|
2012-09-21 22:14:38 +00:00
|
|
|
}
|
2012-09-21 20:57:45 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
m_Parent->NotifyWorkerFinished();
|
2012-09-21 20:57:45 +00:00
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
return;
|
2012-09-21 20:57:45 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
WorkerQueue::WorkerQueue(
|
2012-09-21 22:43:35 +00:00
|
|
|
uint32 numCompressions,
|
2012-09-21 22:14:38 +00:00
|
|
|
uint32 numThreads,
|
|
|
|
uint32 jobSize,
|
|
|
|
const uint8 *inBuf,
|
|
|
|
uint32 inBufSz,
|
|
|
|
CompressionFunc func,
|
|
|
|
uint8 *outBuf
|
|
|
|
)
|
2012-09-21 22:43:35 +00:00
|
|
|
: m_NumCompressions(0)
|
2012-11-01 22:56:13 +00:00
|
|
|
, m_TotalNumCompressions(std::max(uint32(1), numCompressions))
|
2012-09-21 22:43:35 +00:00
|
|
|
, m_NumThreads(numThreads)
|
2012-09-25 21:05:52 +00:00
|
|
|
, m_WaitingThreads(0)
|
2012-09-21 22:14:38 +00:00
|
|
|
, m_ActiveThreads(0)
|
2012-11-01 22:56:13 +00:00
|
|
|
, m_JobSize(std::max(uint32(1), jobSize))
|
2012-09-21 22:14:38 +00:00
|
|
|
, m_InBufSz(inBufSz)
|
|
|
|
, m_InBuf(inBuf)
|
|
|
|
, m_OutBuf(outBuf)
|
2012-09-26 17:31:39 +00:00
|
|
|
, m_NextBlock(0)
|
2012-09-21 22:14:38 +00:00
|
|
|
, m_CompressionFunc(func)
|
2012-11-01 22:56:13 +00:00
|
|
|
, m_CompressionFuncWithStats(NULL)
|
|
|
|
, m_BlockStatManager(NULL)
|
|
|
|
{
|
|
|
|
clamp(m_NumThreads, uint32(1), uint32(kMaxNumWorkerThreads));
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if(m_InBufSz % 64) {
|
|
|
|
fprintf(stderr, "WorkerQueue.cpp -- WARNING: InBufSz not a multiple of 64. Are you sure that your image dimensions are correct?\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerQueue::WorkerQueue(
|
|
|
|
uint32 numCompressions,
|
|
|
|
uint32 numThreads,
|
|
|
|
uint32 jobSize,
|
|
|
|
const uint8 *inBuf,
|
|
|
|
uint32 inBufSz,
|
|
|
|
CompressionFuncWithStats func,
|
|
|
|
BlockStatManager &blockStatManager,
|
|
|
|
uint8 *outBuf
|
|
|
|
)
|
|
|
|
: m_NumCompressions(0)
|
|
|
|
, m_TotalNumCompressions(std::max(uint32(1), numCompressions))
|
|
|
|
, m_NumThreads(numThreads)
|
|
|
|
, m_WaitingThreads(0)
|
|
|
|
, m_ActiveThreads(0)
|
|
|
|
, m_JobSize(std::max(uint32(1), jobSize))
|
|
|
|
, m_InBufSz(inBufSz)
|
|
|
|
, m_InBuf(inBuf)
|
|
|
|
, m_OutBuf(outBuf)
|
|
|
|
, m_NextBlock(0)
|
|
|
|
, m_CompressionFunc(NULL)
|
|
|
|
, m_CompressionFuncWithStats(func)
|
|
|
|
, m_BlockStatManager(&blockStatManager)
|
2012-09-21 22:14:38 +00:00
|
|
|
{
|
2012-09-21 22:43:35 +00:00
|
|
|
clamp(m_NumThreads, uint32(1), uint32(kMaxNumWorkerThreads));
|
2012-09-21 22:14:38 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if(m_InBufSz % 64) {
|
2012-11-01 22:56:13 +00:00
|
|
|
fprintf(stderr, "WorkerQueue.cpp -- WARNING: InBufSz not a multiple of 64. Are you sure that your image dimensions are correct?\n");
|
2012-09-21 22:14:38 +00:00
|
|
|
}
|
2012-09-21 20:57:45 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
void WorkerQueue::Run() {
|
|
|
|
|
|
|
|
// Spawn a bunch of threads...
|
2012-09-29 19:36:42 +00:00
|
|
|
TCLock lock(m_Mutex);
|
2012-11-07 22:10:26 +00:00
|
|
|
for(uint32 i = 0; i < m_NumThreads; i++) {
|
2012-09-29 19:36:42 +00:00
|
|
|
m_Workers[i] = new WorkerThread(this, i);
|
|
|
|
m_ThreadHandles[m_ActiveThreads] = new TCThread(*m_Workers[i]);
|
2012-09-21 22:14:38 +00:00
|
|
|
m_ActiveThreads++;
|
2012-09-21 20:57:45 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 22:43:35 +00:00
|
|
|
m_StopWatch.Reset();
|
|
|
|
m_StopWatch.Start();
|
|
|
|
|
2012-09-26 17:31:39 +00:00
|
|
|
m_NextBlock = 0;
|
2012-09-25 21:05:52 +00:00
|
|
|
m_WaitingThreads = 0;
|
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
// Wait for them to finish...
|
|
|
|
while(m_ActiveThreads > 0) {
|
2012-09-29 19:36:42 +00:00
|
|
|
m_CV.Wait(lock);
|
2012-09-21 20:57:45 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 22:43:35 +00:00
|
|
|
m_StopWatch.Stop();
|
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
// Join them all together..
|
2012-11-07 22:10:26 +00:00
|
|
|
for(uint32 i = 0; i < m_NumThreads; i++) {
|
2012-09-29 19:36:42 +00:00
|
|
|
m_ThreadHandles[i]->Join();
|
2012-09-21 22:14:38 +00:00
|
|
|
delete m_ThreadHandles[i];
|
2012-09-29 19:36:42 +00:00
|
|
|
delete m_Workers[i];
|
2012-09-21 20:57:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
void WorkerQueue::NotifyWorkerFinished() {
|
|
|
|
{
|
2012-09-29 19:36:42 +00:00
|
|
|
TCLock lock(m_Mutex);
|
2012-09-21 22:14:38 +00:00
|
|
|
m_ActiveThreads--;
|
|
|
|
}
|
2012-09-29 19:36:42 +00:00
|
|
|
m_CV.NotifyOne();
|
2012-09-21 22:14:38 +00:00
|
|
|
}
|
2012-09-21 20:57:45 +00:00
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
WorkerThread::EAction WorkerQueue::AcceptThreadData(uint32 threadIdx) {
|
|
|
|
if(threadIdx < 0 || threadIdx >= m_ActiveThreads) {
|
|
|
|
return WorkerThread::eAction_Quit;
|
2012-09-21 20:57:45 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
// How many blocks total do we have?
|
|
|
|
const uint32 totalBlocks = m_InBufSz / 64;
|
|
|
|
|
|
|
|
// Make sure we have exclusive access...
|
2012-09-29 19:36:42 +00:00
|
|
|
TCLock lock(m_Mutex);
|
2012-09-21 22:14:38 +00:00
|
|
|
|
|
|
|
// If we've completed all blocks, then mark the thread for
|
|
|
|
// completion.
|
2012-09-25 21:05:52 +00:00
|
|
|
if(m_NextBlock == totalBlocks) {
|
|
|
|
if(m_NumCompressions < m_TotalNumCompressions) {
|
|
|
|
if(++m_WaitingThreads == m_ActiveThreads) {
|
|
|
|
m_NextBlock = 0;
|
|
|
|
m_WaitingThreads = 0;
|
|
|
|
} else {
|
|
|
|
return WorkerThread::eAction_Wait;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return WorkerThread::eAction_Quit;
|
|
|
|
}
|
2012-09-21 20:57:45 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
// Otherwise, this thread's offset is the current block...
|
|
|
|
m_Offsets[threadIdx] = m_NextBlock;
|
|
|
|
|
|
|
|
// The number of blocks to process is either the job size
|
|
|
|
// or the number of blocks remaining.
|
2012-11-01 22:56:13 +00:00
|
|
|
int blocksProcessed = std::min(m_JobSize, totalBlocks - m_NextBlock);
|
2012-09-21 22:14:38 +00:00
|
|
|
m_NumBlocks[threadIdx] = blocksProcessed;
|
2012-09-21 20:57:45 +00:00
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
// Make sure the next block is updated.
|
|
|
|
m_NextBlock += blocksProcessed;
|
2012-09-21 20:57:45 +00:00
|
|
|
|
2012-09-25 21:05:52 +00:00
|
|
|
if(m_NextBlock == totalBlocks) {
|
|
|
|
++m_NumCompressions;
|
2012-09-21 22:43:35 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
return WorkerThread::eAction_DoWork;
|
2012-09-21 20:57:45 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
const uint8 *WorkerQueue::GetSrcForThread(const int threadIdx) const {
|
|
|
|
assert(m_Offsets[threadIdx] >= 0);
|
|
|
|
assert(threadIdx >= 0);
|
|
|
|
assert(threadIdx < m_NumThreads);
|
2012-09-21 20:57:45 +00:00
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
const uint32 inBufBlockSz = 16 * 4;
|
|
|
|
return m_InBuf + m_Offsets[threadIdx] * inBufBlockSz;
|
|
|
|
}
|
2012-09-21 20:57:45 +00:00
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
uint8 *WorkerQueue::GetDstForThread(const int threadIdx) const {
|
|
|
|
assert(m_Offsets[threadIdx] >= 0);
|
|
|
|
assert(threadIdx >= 0);
|
|
|
|
assert(threadIdx < m_NumThreads);
|
2012-09-21 20:57:45 +00:00
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
const uint32 outBufBlockSz = 16;
|
|
|
|
return m_OutBuf + m_Offsets[threadIdx] * outBufBlockSz;
|
2012-09-21 20:57:45 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
uint32 WorkerQueue::GetNumBlocksForThread(const int threadIdx) const {
|
|
|
|
assert(m_Offsets[threadIdx] >= 0);
|
|
|
|
assert(threadIdx >= 0);
|
|
|
|
assert(threadIdx < m_NumThreads);
|
2012-09-21 20:57:45 +00:00
|
|
|
|
2012-09-21 22:14:38 +00:00
|
|
|
return m_NumBlocks[threadIdx];
|
2012-09-21 20:57:45 +00:00
|
|
|
}
|