PageRenderTime 53ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/XBMCTex/xbox.cpp

http://github.com/elan/plex
C++ | 166 lines | 123 code | 13 blank | 30 comment | 16 complexity | 3ec429be87fa620ffabd7fb6e21420d2 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-3.0, CC-BY-SA-3.0, 0BSD, LGPL-2.1, GPL-3.0, BSD-3-Clause, CC0-1.0, Unlicense, GPL-2.0, AGPL-1.0
  1. /*
  2. * Copyright (C) 2004-2009 Team XBMC
  3. * http://www.xbmc.org
  4. *
  5. * This Program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This Program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with XBMC; see the file COPYING. If not, write to
  17. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. */
  21. #include "xbox.h"
  22. #include "Surface.h"
  23. #include "EndianSwap.h"
  24. #ifndef min
  25. #define min(a,b) (((a) < (b)) ? (a) : (b))
  26. #endif
  27. bool IsPowerOf2(UINT number)
  28. {
  29. return (number & (~number+1)) == number;
  30. }
  31. int GetLog2(UINT Number)
  32. {
  33. int power = 0;
  34. while (Number)
  35. {
  36. Number >>= 1;
  37. power++;
  38. }
  39. return power-1;
  40. }
  41. BOOL IsPaletted(XB_D3DFORMAT format)
  42. {
  43. switch (format)
  44. {
  45. case XB_D3DFMT_P8:
  46. return true;
  47. default:
  48. return false;
  49. }
  50. }
  51. void SetTextureHeader(UINT Width, UINT Height, UINT Levels, UINT Usage, XB_D3DFORMAT Format, D3DTexture *pTexture, UINT Data, UINT Pitch)
  52. {
  53. // TODO: No idea what most of this is.
  54. // Byte swapping to convert the header on big-endian system
  55. memset(pTexture, 0, sizeof(D3DTexture));
  56. pTexture->Common = Endian_SwapLE32(D3DCOMMON_TYPE_TEXTURE + 1); // what does the 1 give??
  57. pTexture->Format |= (Format & 0xFF) << 8;
  58. pTexture->Format |= 0x10029; // no idea why
  59. pTexture->Data = Endian_SwapLE32(Data); // offset of texture data
  60. if (IsPowerOf2(Width) && IsPowerOf2(Height))
  61. {
  62. pTexture->Format |= (GetLog2(Width) & 0xF) << 20;
  63. pTexture->Format |= (GetLog2(Height) & 0xF) << 24;
  64. }
  65. else
  66. {
  67. pTexture->Size |= (Width - 1) & 0xfff;
  68. pTexture->Size |= ((Height - 1) & 0xfff) << 12;
  69. pTexture->Size |= (((Pitch >> 6) & 0xff) - 1) << 24;
  70. }
  71. pTexture->Format = Endian_SwapLE32(pTexture->Format);
  72. pTexture->Size = Endian_SwapLE32(pTexture->Size);
  73. }
  74. BOOL IsSwizzledFormat(XB_D3DFORMAT format)
  75. {
  76. switch (format)
  77. {
  78. case XB_D3DFMT_A8R8G8B8:
  79. case XB_D3DFMT_P8:
  80. return true;
  81. default:
  82. return false;
  83. }
  84. }
  85. DWORD BytesPerPixelFromFormat(XB_D3DFORMAT format)
  86. {
  87. switch (format)
  88. {
  89. case XB_D3DFMT_A8R8G8B8:
  90. case XB_D3DFMT_LIN_A8R8G8B8:
  91. case XB_D3DFMT_DXT1:
  92. case XB_D3DFMT_DXT4:
  93. return 4;
  94. case XB_D3DFMT_P8:
  95. return 1;
  96. default:
  97. break;
  98. }
  99. return 0;
  100. }
  101. // Swizzle.
  102. // Format is:
  103. // 00 01 04 05
  104. // 02 03 06 07
  105. // 08 09 12 13
  106. // 10 11 14 15 ...
  107. // Currently only works for 32bit and 8bit textures, with power of 2 width and height
  108. void Swizzle(const void *src, unsigned int depth, unsigned int width, unsigned int height, void *dest)
  109. {
  110. for (UINT y = 0; y < height; y++)
  111. {
  112. UINT sy = 0;
  113. if (y < width)
  114. {
  115. for (int bit = 0; bit < 16; bit++)
  116. sy |= ((y >> bit) & 1) << (2*bit);
  117. sy <<= 1; // y counts twice
  118. }
  119. else
  120. {
  121. UINT y_mask = y % width;
  122. for (int bit = 0; bit < 16; bit++)
  123. sy |= ((y_mask >> bit) & 1) << (2*bit);
  124. sy <<= 1; // y counts twice
  125. sy += (y / width) * width * width;
  126. }
  127. BYTE *s = (BYTE *)src + y * width * depth;
  128. for (UINT x = 0; x < width; x++)
  129. {
  130. UINT sx = 0;
  131. if (x < height * 2)
  132. {
  133. for (int bit = 0; bit < 16; bit++)
  134. sx |= ((x >> bit) & 1) << (2*bit);
  135. }
  136. else
  137. {
  138. int x_mask = x % (2*height);
  139. for (int bit = 0; bit < 16; bit++)
  140. sx |= ((x_mask >> bit) & 1) << (2*bit);
  141. sx += (x / (2 * height)) * 2 * height * height;
  142. }
  143. BYTE *d = (BYTE *)dest + (sx + sy)*depth;
  144. for (unsigned int i = 0; i < depth; ++i)
  145. *d++ = *s++;
  146. }
  147. }
  148. }
  149. VOID SwizzleRect(LPCVOID pSource, DWORD Pitch, LPVOID pDest, DWORD Width, DWORD Height, DWORD BytesPerPixel)
  150. {
  151. // knows nothing about Pitch
  152. Swizzle(pSource, BytesPerPixel, Width, Height, pDest);
  153. }