Skip to content

Commit

Permalink
use SDL threads instead of C++11 threads, which are not supported on …
Browse files Browse the repository at this point in the history
…clang
  • Loading branch information
richard42 committed Dec 25, 2014
1 parent 6a46ad1 commit 4bbe95d
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 117 deletions.
52 changes: 34 additions & 18 deletions src/GlideHQ/TxFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,28 @@
#include "TextureFilters.h"
#include "TxDbg.h"
#ifndef NO_FILTER_THREAD
#include <functional>
#include <thread>
#include <SDL.h>
#include <SDL_Thread.h>
#endif
#if defined(__MINGW32__)
#define swprintf _snwprintf
#endif

typedef struct {
uint32 *src;
uint32 srcwidth;
uint32 srcheight;
uint32 *dest;
uint32 filter;
} FilterParams;

int FilterThreadFunc(void *pFilterParams)
{
FilterParams *pParams = (FilterParams *) pFilterParams;
filter_8888(pParams->src, pParams->srcwidth, pParams->srcheight, pParams->dest, pParams->filter);
return 0;
}

void TxFilter::clear()
{
/* clear hires texture cache */
Expand Down Expand Up @@ -275,30 +290,31 @@ TxFilter::filter(uint8 *src, int srcwidth, int srcheight, uint16 srcformat, uint
numcore--;
}
if (blkrow > 0 && numcore > 1) {
std::thread *thrd[MAX_NUMCORE];
SDL_Thread *thrd[MAX_NUMCORE];
FilterParams params[MAX_NUMCORE];
unsigned int i;
int blkheight = blkrow << 2;
unsigned int srcStride = (srcwidth * blkheight) << 2;
unsigned int destStride = srcStride << scale_shift << scale_shift;
for (i = 0; i < numcore - 1; i++) {
thrd[i] = new std::thread(std::bind(filter_8888,
(uint32*)_texture,
srcwidth,
blkheight,
(uint32*)_tmptex,
filter));
for (i = 0; i < numcore; i++) {
params[i].src = (uint32*) _texture;
params[i].srcwidth = srcwidth;
if (i == numcore - 1)
params[i].srcheight = srcheight - blkheight * i;
else
params[i].srcheight = blkheight;
params[i].dest = (uint32*) _tmptex;
params[i].filter = filter;
#if SDL_VERSION_ATLEAST(2,0,0)
thrd[i] = SDL_CreateThread(FilterThreadFunc, "filter8888", &params[i]);
#else
thrd[i] = SDL_CreateThread(FilterThreadFunc, &params[i]);
#endif
_texture += srcStride;
_tmptex += destStride;
}
thrd[i] = new std::thread(std::bind(filter_8888,
(uint32*)_texture,
srcwidth,
srcheight - blkheight * i,
(uint32*)_tmptex,
filter));
for (i = 0; i < numcore; i++) {
thrd[i]->join();
delete thrd[i];
SDL_WaitThread(thrd[i], NULL);
}
} else {
filter_8888((uint32*)_texture, srcwidth, srcheight, (uint32*)_tmptex, filter);
Expand Down
217 changes: 137 additions & 80 deletions src/GlideHQ/TxQuantize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,70 @@
#endif

#ifndef NO_FILTER_THREAD
#include <functional>
#include <thread>
#include <SDL.h>
#include <SDL_Thread.h>
#endif

/* NOTE: The codes are not optimized. They can be made faster. */

#include "TxQuantize.h"

typedef void (*quantizerFunc)(uint32* src, uint32* dest, int width, int height);

typedef struct {
quantizerFunc func;
uint32 *src;
uint32 *dest;
uint32 width;
uint32 height;
} QuantizeParams;

int QuantizeThreadFunc(void *pQuantParams)
{
QuantizeParams *pParams = (QuantizeParams *) pQuantParams;
pParams->func(pParams->src, pParams->dest, pParams->width, pParams->height);
return 0;
}

typedef struct {
TxQuantize *pThis;
int comps;
int width;
int height;
const void *src;
int srcRowStride;
int destformat;
void *dest;
int dstRowStride;
} CompressParams;

int TxQuantize::CompressThreadFuncFXT1(void *pCompressParams)
{
CompressParams *pParams = (CompressParams *) pCompressParams;

return pParams->pThis->_tx_compress_fxt1(pParams->width,
pParams->height,
pParams->comps,
pParams->src,
pParams->srcRowStride,
pParams->dest,
pParams->dstRowStride);
}

int TxQuantize::CompressThreadFuncDXT(void *pCompressParams)
{
CompressParams *pParams = (CompressParams *) pCompressParams;

pParams->pThis->_tx_compress_dxtn_rgba(pParams->comps,
pParams->width,
pParams->height,
pParams->src,
pParams->destformat,
pParams->dest,
pParams->dstRowStride);
return 0;
}

/* NOTE: The codes are not optimized. They can be made faster. */

TxQuantize::TxQuantize()
{
_txUtil = new TxUtil();
Expand Down Expand Up @@ -1735,7 +1791,6 @@ TxQuantize::P8_16BPP(uint32* src, uint32* dest, int width, int height, uint32* p
boolean
TxQuantize::quantize(uint8* src, uint8* dest, int width, int height, uint16 srcformat, uint16 destformat, boolean fastQuantizer)
{
typedef void (TxQuantize::*quantizerFunc)(uint32* src, uint32* dest, int width, int height);
quantizerFunc quantizer;
int bpp_shift = 0;

Expand Down Expand Up @@ -1777,36 +1832,37 @@ TxQuantize::quantize(uint8* src, uint8* dest, int width, int height, uint16 srcf
numcore--;
}
if (blkrow > 0 && numcore > 1) {
std::thread *thrd[MAX_NUMCORE];
SDL_Thread *thrd[MAX_NUMCORE];
QuantizeParams params[MAX_NUMCORE];
unsigned int i;
int blkheight = blkrow << 2;
unsigned int srcStride = (width * blkheight) << (2 - bpp_shift);
unsigned int destStride = srcStride << bpp_shift;
for (i = 0; i < numcore - 1; i++) {
thrd[i] = new std::thread(std::bind(quantizer,
this,
(uint32*)src,
(uint32*)dest,
width,
blkheight));
params[i].func = quantizer;
params[i].src = (uint32*) src;
params[i].dest = (uint32*) dest;
params[i].width = width;
if (i == numcore-1)
params[i].height = height - blkheight * i;
else
params[i].height = blkheight;
#if SDL_VERSION_ATLEAST(2,0,0)
thrd[i] = SDL_CreateThread(QuantizeThreadFunc, "quantizer", &params[i]);
#else
thrd[i] = SDL_CreateThread(QuantizeThreadFunc, &params[i]);
#endif
src += srcStride;
dest += destStride;
}
thrd[i] = new std::thread(std::bind(quantizer,
this,
(uint32*)src,
(uint32*)dest,
width,
height - blkheight * i));
for (i = 0; i < numcore; i++) {
thrd[i]->join();
delete thrd[i];
SDL_WaitThread(thrd[i], NULL);
}
} else {
(*this.*quantizer)((uint32*)src, (uint32*)dest, width, height);
quantizer((uint32*)src, (uint32*)dest, width, height);
}
#else
(*this.*quantizer)((uint32*)src, (uint32*)dest, width, height);
quantizer((uint32*)src, (uint32*)dest, width, height);
#endif

} else if (srcformat == GR_TEXFMT_ARGB_8888) {
Expand Down Expand Up @@ -1848,36 +1904,37 @@ TxQuantize::quantize(uint8* src, uint8* dest, int width, int height, uint16 srcf
numcore--;
}
if (blkrow > 0 && numcore > 1) {
std::thread *thrd[MAX_NUMCORE];
SDL_Thread *thrd[MAX_NUMCORE];
QuantizeParams params[MAX_NUMCORE];
unsigned int i;
int blkheight = blkrow << 2;
unsigned int srcStride = (width * blkheight) << 2;
unsigned int destStride = srcStride >> bpp_shift;
for (i = 0; i < numcore - 1; i++) {
thrd[i] = new std::thread(std::bind(quantizer,
this,
(uint32*)src,
(uint32*)dest,
width,
blkheight));
for (i = 0; i < numcore; i++) {
params[i].func = quantizer;
params[i].src = (uint32*) src;
params[i].dest = (uint32*) dest;
params[i].width = width;
if (i == numcore-1)
params[i].height = height - blkheight*i;
else
params[i].height = blkheight;
#if SDL_VERSION_ATLEAST(2,0,0)
thrd[i] = SDL_CreateThread(QuantizeThreadFunc, "quantizer", &params[i]);
#else
thrd[i] = SDL_CreateThread(QuantizeThreadFunc, &params[i]);
#endif
src += srcStride;
dest += destStride;
}
thrd[i] = new std::thread(std::bind(quantizer,
this,
(uint32*)src,
(uint32*)dest,
width,
height - blkheight * i));
for (i = 0; i < numcore; i++) {
thrd[i]->join();
delete thrd[i];
SDL_WaitThread(thrd[i], NULL);
}
} else {
(*this.*quantizer)((uint32*)src, (uint32*)dest, width, height);
quantizer((uint32*)src, (uint32*)dest, width, height);
}
#else
(*this.*quantizer)((uint32*)src, (uint32*)dest, width, height);
quantizer((uint32*)src, (uint32*)dest, width, height);
#endif

} else {
Expand Down Expand Up @@ -1918,34 +1975,34 @@ TxQuantize::FXT1(uint8 *src, uint8 *dest,
numcore--;
}
if (blkrow > 0 && numcore > 1) {
std::thread *thrd[MAX_NUMCORE];
SDL_Thread *thrd[MAX_NUMCORE];
CompressParams params[MAX_NUMCORE];
unsigned int i;
int blkheight = blkrow << 2;
unsigned int srcStride = (srcwidth * blkheight) << 2;
unsigned int destStride = dstRowStride * blkrow;
for (i = 0; i < numcore - 1; i++) {
thrd[i] = new std::thread(std::bind(_tx_compress_fxt1,
srcwidth,
blkheight,
4,
src,
srcRowStride,
dest,
dstRowStride));
for (i = 0; i < numcore; i++) {
params[i].pThis = this;
params[i].comps = 4;
params[i].width = srcwidth;
if (i == numcore-1)
params[i].height = srcheight - blkheight * i;
else
params[i].height = blkheight;
params[i].src = src;
params[i].srcRowStride = srcRowStride;
params[i].dest = dest;
params[i].dstRowStride = dstRowStride;
#if SDL_VERSION_ATLEAST(2,0,0)
thrd[i] = SDL_CreateThread(TxQuantize::CompressThreadFuncFXT1, "compressor", &params[i]);
#else
thrd[i] = SDL_CreateThread(TxQuantize::CompressThreadFuncFXT1, &params[i]);
#endif
src += srcStride;
dest += destStride;
}
thrd[i] = new std::thread(std::bind(_tx_compress_fxt1,
srcwidth,
srcheight - blkheight * i,
4,
src,
srcRowStride,
dest,
dstRowStride));
for (i = 0; i < numcore; i++) {
thrd[i]->join();
delete thrd[i];
SDL_WaitThread(thrd[i], NULL);
}
} else {
(*_tx_compress_fxt1)(srcwidth, /* width */
Expand Down Expand Up @@ -2034,34 +2091,34 @@ TxQuantize::DXTn(uint8 *src, uint8 *dest,
numcore--;
}
if (blkrow > 0 && numcore > 1) {
std::thread *thrd[MAX_NUMCORE];
SDL_Thread *thrd[MAX_NUMCORE];
CompressParams params[MAX_NUMCORE];
unsigned int i;
int blkheight = blkrow << 2;
unsigned int srcStride = (srcwidth * blkheight) << 2;
unsigned int destStride = dstRowStride * blkrow;
for (i = 0; i < numcore - 1; i++) {
thrd[i] = new std::thread(std::bind(_tx_compress_dxtn_rgba,
4,
srcwidth,
blkheight,
src,
compression,
dest,
dstRowStride));
for (i = 0; i < numcore; i++) {
params[i].pThis = this;
params[i].comps = 4;
params[i].width = srcwidth;
if (i == numcore-1)
params[i].height = srcheight - blkheight * i;
else
params[i].height = blkheight;
params[i].src = src;
params[i].destformat = compression;
params[i].dest = dest;
params[i].dstRowStride = dstRowStride;
#if SDL_VERSION_ATLEAST(2,0,0)
thrd[i] = SDL_CreateThread(TxQuantize::CompressThreadFuncDXT, "compressor", &params[i]);
#else
thrd[i] = SDL_CreateThread(TxQuantize::CompressThreadFuncDXT, &params[i]);
#endif
src += srcStride;
dest += destStride;
}
thrd[i] = new std::thread(std::bind(_tx_compress_dxtn_rgba,
4,
srcwidth,
srcheight - blkheight * i,
src,
compression,
dest,
dstRowStride));
for (i = 0; i < numcore; i++) {
thrd[i]->join();
delete thrd[i];
SDL_WaitThread(thrd[i], NULL);
}
} else {
(*_tx_compress_dxtn_rgba)(4, /* comps: ARGB8888=4, RGB888=3 */
Expand Down
Loading

0 comments on commit 4bbe95d

Please sign in to comment.