PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/XBMCTex/xbox.cpp

https://github.com/dtmetz/xbmc-fork
C++ | 141 lines | 120 code | 12 blank | 9 comment | 16 complexity | 3e333c55311ca3279d6d00ff8515c052 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0
  1. #include "xbox.h"
  2. #include "Surface.h"
  3. #ifndef min
  4. #define min(a,b) (((a) < (b)) ? (a) : (b))
  5. #endif
  6. bool IsPowerOf2(UINT number)
  7. {
  8. return (number & (~number+1)) == number;
  9. }
  10. int GetLog2(UINT Number)
  11. {
  12. int power = 0;
  13. while (Number)
  14. {
  15. Number >>= 1;
  16. power++;
  17. }
  18. return power-1;
  19. }
  20. BOOL IsPaletted(XB_D3DFORMAT format)
  21. {
  22. switch (format)
  23. {
  24. case XB_D3DFMT_P8:
  25. return true;
  26. default:
  27. return false;
  28. }
  29. }
  30. void SetTextureHeader(UINT Width, UINT Height, UINT Levels, UINT Usage, XB_D3DFORMAT Format, D3DTexture *pTexture, UINT Data, UINT Pitch)
  31. {
  32. // TODO: No idea what most of this is.
  33. memset(pTexture, 0, sizeof(D3DTexture));
  34. pTexture->Common = D3DCOMMON_TYPE_TEXTURE + 1; // what does the 1 give??
  35. pTexture->Format |= (Format & 0xFF) << 8;
  36. pTexture->Format |= 0x10029; // no idea why
  37. pTexture->Data = Data; // offset of texture data
  38. if (IsPowerOf2(Width) && IsPowerOf2(Height))
  39. {
  40. pTexture->Format |= (GetLog2(Width) & 0xF) << 20;
  41. pTexture->Format |= (GetLog2(Height) & 0xF) << 24;
  42. }
  43. else
  44. {
  45. pTexture->Size |= (Width - 1) & 0xfff;
  46. pTexture->Size |= ((Height - 1) & 0xfff) << 12;
  47. pTexture->Size |= (((Pitch >> 6) & 0xff) - 1) << 24;
  48. }
  49. }
  50. BOOL IsSwizzledFormat(XB_D3DFORMAT format)
  51. {
  52. switch (format)
  53. {
  54. case XB_D3DFMT_A8R8G8B8:
  55. case XB_D3DFMT_P8:
  56. return true;
  57. default:
  58. return false;
  59. }
  60. }
  61. DWORD BytesPerPixelFromFormat(XB_D3DFORMAT format)
  62. {
  63. switch (format)
  64. {
  65. case XB_D3DFMT_A8R8G8B8:
  66. case XB_D3DFMT_LIN_A8R8G8B8:
  67. case XB_D3DFMT_DXT1:
  68. case XB_D3DFMT_DXT4:
  69. return 4;
  70. case XB_D3DFMT_P8:
  71. return 1;
  72. default:
  73. break;
  74. }
  75. return 0;
  76. }
  77. // Swizzle.
  78. // Format is:
  79. // 00 01 04 05
  80. // 02 03 06 07
  81. // 08 09 12 13
  82. // 10 11 14 15 ...
  83. // Currently only works for 32bit and 8bit textures, with power of 2 width and height
  84. void Swizzle(const void *src, unsigned int depth, unsigned int width, unsigned int height, void *dest)
  85. {
  86. for (UINT y = 0; y < height; y++)
  87. {
  88. UINT sy = 0;
  89. if (y < width)
  90. {
  91. for (int bit = 0; bit < 16; bit++)
  92. sy |= ((y >> bit) & 1) << (2*bit);
  93. sy <<= 1; // y counts twice
  94. }
  95. else
  96. {
  97. UINT y_mask = y % width;
  98. for (int bit = 0; bit < 16; bit++)
  99. sy |= ((y_mask >> bit) & 1) << (2*bit);
  100. sy <<= 1; // y counts twice
  101. sy += (y / width) * width * width;
  102. }
  103. BYTE *s = (BYTE *)src + y * width * depth;
  104. for (UINT x = 0; x < width; x++)
  105. {
  106. UINT sx = 0;
  107. if (x < height * 2)
  108. {
  109. for (int bit = 0; bit < 16; bit++)
  110. sx |= ((x >> bit) & 1) << (2*bit);
  111. }
  112. else
  113. {
  114. int x_mask = x % (2*height);
  115. for (int bit = 0; bit < 16; bit++)
  116. sx |= ((x_mask >> bit) & 1) << (2*bit);
  117. sx += (x / (2 * height)) * 2 * height * height;
  118. }
  119. BYTE *d = (BYTE *)dest + (sx + sy)*depth;
  120. for (unsigned int i = 0; i < depth; ++i)
  121. *d++ = *s++;
  122. }
  123. }
  124. }
  125. VOID SwizzleRect(LPCVOID pSource, DWORD Pitch, LPVOID pDest, DWORD Width, DWORD Height, DWORD BytesPerPixel)
  126. {
  127. // knows nothing about Pitch
  128. Swizzle(pSource, BytesPerPixel, Width, Height, pDest);
  129. }