5 * Copyright (C) 2007 Hiroshi Morii All Rights Reserved.
6 * Email koolsmoky(at)users.sourceforge.net
7 * Web http://www.3dfxzone.it/koolsmoky
9 * this is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * this is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with GNU Make; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 #define WIN32_LEAN_AND_MEAN
37 ******************************************************************************/
38 TxLoadLib::TxLoadLib()
42 _dxtnlib = LoadLibrary("dxtn");
45 if (!_tx_compress_dxtn_rgba)
46 _tx_compress_dxtn_rgba = (dxtCompressTexFuncExt)DLSYM(_dxtnlib, "tx_compress_dxtn_rgba");
48 if (!_tx_compress_fxt1)
49 _tx_compress_fxt1 = (fxtCompressTexFuncExt)DLSYM(_dxtnlib, "fxt1_encode");
52 _tx_compress_dxtn_rgba = tx_compress_dxtn_rgba;
53 _tx_compress_fxt1 = fxt1_encode;
58 TxLoadLib::~TxLoadLib()
61 /* free dynamic library */
63 FreeLibrary(_dxtnlib);
69 TxLoadLib::getfxtCompressTexFuncExt()
71 return _tx_compress_fxt1;
75 TxLoadLib::getdxtCompressTexFuncExt()
77 return _tx_compress_dxtn_rgba;
83 ******************************************************************************/
85 TxUtil::checksumTx(uint8 *src, int width, int height, uint16 format)
87 int dataSize = sizeofTx(width, height, format);
89 /* for now we use adler32 if something else is better
90 * we can simply swtich later
92 /* return (dataSize ? Adler32(src, dataSize, 1) : 0); */
95 return (dataSize ? crc32(crc32(0L, Z_NULL, 0), src, dataSize) : 0);
99 TxUtil::sizeofTx(int width, int height, uint16 format)
103 /* a lookup table for the shifts would be better */
105 case GR_TEXFMT_ARGB_CMP_FXT1:
106 dataSize = (((width + 0x7) & ~0x7) * ((height + 0x3) & ~0x3)) >> 1;
108 case GR_TEXFMT_ARGB_CMP_DXT1:
109 dataSize = (((width + 0x3) & ~0x3) * ((height + 0x3) & ~0x3)) >> 1;
111 case GR_TEXFMT_ARGB_CMP_DXT3:
112 case GR_TEXFMT_ARGB_CMP_DXT5:
113 dataSize = ((width + 0x3) & ~0x3) * ((height + 0x3) & ~0x3);
115 case GR_TEXFMT_ALPHA_INTENSITY_44:
116 case GR_TEXFMT_ALPHA_8:
117 case GR_TEXFMT_INTENSITY_8:
119 dataSize = width * height;
121 case GR_TEXFMT_ARGB_4444:
122 case GR_TEXFMT_ARGB_1555:
123 case GR_TEXFMT_RGB_565:
124 case GR_TEXFMT_ALPHA_INTENSITY_88:
125 dataSize = (width * height) << 1;
127 case GR_TEXFMT_ARGB_8888:
128 dataSize = (width * height) << 2;
131 /* unsupported format */
132 DBG_INFO(80, L"Error: cannot get size. unsupported gfmt:%x\n", format);
141 TxUtil::chkAlpha(uint32* src, int width, int height)
143 /* NOTE: _src must be ARGB8888
145 * 0x00000000: 8bit alpha
146 * 0x00000001: 1bit alpha
147 * 0xff000001: no alpha
150 int _size = width * height;
154 mov esi, dword ptr [src];
155 mov ecx, dword ptr [_size];
159 mov eax, dword ptr [esi];
174 mov dword ptr [alpha], ebx;
184 TxUtil::checksum(uint8 *src, int width, int height, int size, int rowStride)
186 /* Rice CRC32 for now. We can switch this to Jabo MD5 or
187 * any other custom checksum.
188 * TODO: use *_HIRESTEXTURE option. */
192 return RiceCRC32(src, width, height, size, rowStride);
196 TxUtil::checksum64(uint8 *src, int width, int height, int size, int rowStride, uint8 *palette)
198 /* Rice CRC32 for now. We can switch this to Jabo MD5 or
199 * any other custom checksum.
200 * TODO: use *_HIRESTEXTURE option. */
201 /* Returned value is 64bits: hi=palette crc32 low=texture crc32 */
208 uint32 crc32 = 0, cimax = 0;
209 switch (size & 0xff) {
211 if (RiceCRC32_CI8(src, width, height, size, rowStride, &crc32, &cimax)) {
212 crc64Ret = (uint64)RiceCRC32(palette, cimax + 1, 1, 2, 512);
214 crc64Ret |= (uint64)crc32;
218 if (RiceCRC32_CI4(src, width, height, size, rowStride, &crc32, &cimax)) {
219 crc64Ret = (uint64)RiceCRC32(palette, cimax + 1, 1, 2, 32);
221 crc64Ret |= (uint64)crc32;
226 crc64Ret = (uint64)RiceCRC32(src, width, height, size, rowStride);
233 ** Computes Adler32 checksum for a stream of data.
235 ** From the specification found in RFC 1950: (ZLIB Compressed Data Format
236 ** Specification version 3.3)
238 ** ADLER32 (Adler-32 checksum) This contains a checksum value of the
239 ** uncompressed data (excluding any dictionary data) computed according to
240 ** Adler-32 algorithm. This algorithm is a 32-bit extension and improvement
241 ** of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073 standard.
243 ** Adler-32 is composed of two sums accumulated per byte: s1 is the sum of
244 ** all bytes, s2 is the sum of all s1 values. Both sums are done modulo
245 ** 65521. s1 is initialized to 1, s2 to zero. The Adler-32 checksum is stored
246 ** as s2*65536 + s1 in most-significant-byte first (network) order.
248 ** 8.2. The Adler-32 algorithm
250 ** The Adler-32 algorithm is much faster than the CRC32 algorithm yet still
251 ** provides an extremely low probability of undetected errors.
253 ** The modulo on unsigned long accumulators can be delayed for 5552 bytes,
254 ** so the modulo operation time is negligible. If the bytes are a, b, c,
255 ** the second sum is 3a + 2b + c + 3, and so is position and order sensitive,
256 ** unlike the first sum, which is just a checksum. That 65521 is prime is
257 ** important to avoid a possible large class of two-byte errors that leave
258 ** the check unchanged. (The Fletcher checksum uses 255, which is not prime
259 ** and which also makes the Fletcher check insensitive to single byte
260 ** changes 0 <-> 255.)
262 ** The sum s1 is initialized to 1 instead of zero to make the length of
263 ** the sequence part of s2, so that the length does not have to be checked
264 ** separately. (Any sequence of zeroes has a Fletcher checksum of zero.)
268 TxUtil::Adler32(const uint8* data, int Len, uint32 dwAdler32)
272 return adler32(dwAdler32, data, Len);
274 register uint32 s1 = dwAdler32 & 0xFFFF;
275 register uint32 s2 = (dwAdler32 >> 16) & 0xFFFF;
279 /* 5552 is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
280 k = (Len < 5552 ? Len : 5552);
286 /* 65521 is the largest prime smaller than 65536 */
291 return (s2 << 16) | s1;
296 TxUtil::Adler32(const uint8* src, int width, int height, int size, int rowStride)
300 uint32 width_in_bytes = width * size;
302 for (i = 0; i < height; i++) {
303 ret = Adler32(src, width_in_bytes, ret);
311 template<class T> static T __ROL__(T value, unsigned int count)
313 const unsigned int nbits = sizeof(T) * 8;
316 T high = value >> (nbits - count);
322 /* Rice CRC32 for hires texture packs */
323 /* NOTE: The following is used in Glide64 to calculate the CRC32
324 * for Rice hires texture packs.
326 * BYTE* addr = (BYTE*)(gfx.RDRAM +
327 * rdp.addr[rdp.tiles[tile].t_mem] +
328 * (rdp.tiles[tile].ul_t * bpl) +
329 * (((rdp.tiles[tile].ul_s<<rdp.tiles[tile].size)+1)>>1));
331 * rdp.tiles[tile].width,
332 * rdp.tiles[tile].height,
333 * (unsigned short)(rdp.tiles[tile].format << 8 | rdp.tiles[tile].size),
337 TxUtil::RiceCRC32(const uint8* src, int width, int height, int size, int rowStride)
344 uint32_t word_hash = 0;
346 const uint32_t bytes_per_width = ((width << size) + 1) >> 1;
351 for (cur_height = height - 1; cur_height >= 0; cur_height--) {
352 for (pos = bytes_per_width - 4; pos < 0x80000000u; pos -= 4) {
353 word = *(uint32_t *)&row[pos];
354 word_hash = pos ^ word;
355 tmp = __ROL__(crc32Ret, 4);
356 crc32Ret = word_hash + tmp;
358 crc32Ret += cur_height ^ word_hash;
365 TxUtil::RiceCRC32_CI4(const uint8* src, int width, int height, int size, int rowStride,
366 uint32* crc32, uint32* cimax)
374 uint32_t word_hash = 0;
376 const uint32_t bytes_per_width = ((width << size) + 1) >> 1;
382 for (cur_height = height - 1; cur_height >= 0; cur_height--) {
383 for (pos = bytes_per_width - 4; pos < 0x80000000u; pos -= 4) {
384 word = *(uint32_t *)&row[pos];
385 if (cimaxRet != 15) {
386 if ((word & 0xF) >= cimaxRet)
387 cimaxRet = word & 0xF;
388 if ((uint32_t)((uint8_t)word >> 4) >= cimaxRet)
389 cimaxRet = (uint8_t)word >> 4;
390 if (((word >> 8) & 0xF) >= cimaxRet)
391 cimaxRet = (word >> 8) & 0xF;
392 if ((uint32_t)((uint16_t)word >> 12) >= cimaxRet)
393 cimaxRet = (uint16_t)word >> 12;
394 if (((word >> 16) & 0xF) >= cimaxRet)
395 cimaxRet = (word >> 16) & 0xF;
396 if (((word >> 20) & 0xF) >= cimaxRet)
397 cimaxRet = (word >> 20) & 0xF;
398 if (((word >> 24) & 0xF) >= cimaxRet)
399 cimaxRet = (word >> 24) & 0xF;
400 if (word >> 28 >= cimaxRet )
401 cimaxRet = word >> 28;
403 word_hash = pos ^ word;
404 tmp = __ROL__(crc32Ret, 4);
405 crc32Ret = word_hash + tmp;
407 crc32Ret += cur_height ^ word_hash;
416 TxUtil::RiceCRC32_CI8(const uint8* src, int width, int height, int size, int rowStride,
417 uint32* crc32, uint32* cimax)
425 uint32_t word_hash = 0;
427 const uint32_t bytes_per_width = ((width << size) + 1) >> 1;
433 for (cur_height = height - 1; cur_height >= 0; cur_height--) {
434 for (pos = bytes_per_width - 4; pos < 0x80000000u; pos -= 4) {
435 word = *(uint32_t *)&row[pos];
436 if (cimaxRet != 255) {
437 if ((uint8_t)word >= cimaxRet)
438 cimaxRet = (uint8_t)word;
439 if ((uint32_t)((uint16_t)word >> 8) >= cimaxRet)
440 cimaxRet = (uint16_t)word >> 8;
441 if (((word >> 16) & 0xFF) >= cimaxRet)
442 cimaxRet = (word >> 16) & 0xFF;
443 if (word >> 24 >= cimaxRet)
444 cimaxRet = word >> 24;
446 word_hash = pos ^ word;
447 tmp = __ROL__(crc32Ret, 4);
448 crc32Ret = word_hash + tmp;
450 crc32Ret += cur_height ^ word_hash;
459 TxUtil::log2(int num)
461 #if defined(__GNUC__)
462 return __builtin_ctz(num);
463 #elif defined(_MSC_VER) && _MSC_VER >= 1400
465 _BitScanForward((DWORD *)&i, num);
467 #elif defined(__MSC__)
469 mov eax, dword ptr [num];
471 mov dword ptr [i], eax;
485 case 1024: return 10;
486 case 2048: return 11;
492 TxUtil::grLodLog2(int w, int h)
494 return (w >= h ? log2(w) : log2(h));
498 TxUtil::grAspectRatioLog2(int w, int h)
500 return (w >= h ? log2(w/h) : -log2(h/w));
504 TxUtil::getNumberofProcessors()
506 int numcore = 1, ret;
509 #ifndef _SC_NPROCESSORS_ONLN
511 GetSystemInfo(&info);
512 #define sysconf(a) info.dwNumberOfProcessors
513 #define _SC_NPROCESSORS_ONLN
516 #ifdef _SC_NPROCESSORS_ONLN
517 ret = sysconf(_SC_NPROCESSORS_CONF);
521 ret = sysconf(_SC_NPROCESSORS_ONLN);
532 * Memory buffers for texture manipulations
533 ******************************************************************************/
537 for (i = 0; i < 2; i++) {
543 TxMemBuf::~TxMemBuf()
549 TxMemBuf::init(int maxwidth, int maxheight)
552 for (i = 0; i < 2; i++) {
554 _tex[i] = (uint8 *)malloc(maxwidth * maxheight * 4);
555 _size[i] = maxwidth * maxheight * 4;
570 for (i = 0; i < 2; i++) {
571 if (_tex[i]) free(_tex[i]);
578 TxMemBuf::get(unsigned int num)
580 return ((num < 2) ? _tex[num] : NULL);
584 TxMemBuf::size_of(unsigned int num)
586 return ((num < 2) ? _size[num] : 0);