Guest User

Untitled

a guest
Jun 22nd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. // Reconstructed for Xenia by Triang3l.
  2.  
  3. DWORD D3D::AlignTextureDimensions(DWORD * pWidth, DWORD * pHeight, DWORD * pDepth,
  4. DWORD BitsPerPixel, DWORD GpuFormat, DWORD GpuDimension, BOOL Tiled) {
  5. DWORD widthAlignmentBlocks = 32;
  6. DWORD heightAlignmentBlocks = (GpuDimension == GPU_DIMENSION_1D ? 1 : 32);
  7. DWORD depthAlignment = (GpuDimension == GPU_DIMENSION_3D ? 4 : 1);
  8.  
  9. DWORD blockWidth, blockHeight;
  10. D3D::BlockSizeOfGpuFormat(GpuFormat, &blockWidth, &blockHeight);
  11. if (!Tiled) {
  12. widthAlignmentBlocks = std::max(256 / (blockWidth * blockHeight * BitsPerPixel / 8), (DWORD) 32);
  13. }
  14. DWORD widthAlignment = widthAlignmentBlocks * blockWidth;
  15. DWORD heightAlignment = heightAlignmentBlocks * blockHeight;
  16.  
  17. DWORD widthAligned = (*pWidth + widthAlignment - 1) & (widthAlignment - 1);
  18. DWORD heightAligned = (*pHeight + heightAlignment - 1) & (heightAlignment - 1);
  19. DWORD depthAligned = (*pDepth + depthAlignment - 1) & (depthAlignment - 1);
  20. *pWidth = widthAligned;
  21. *pHeight = heightAligned;
  22. *pDepth = depthAligned;
  23.  
  24. DWORD textureSize = widthAligned * BitsPerPixel / 8 * heightAligned;
  25. if (GpuDimension == GPU_DIMENSION_3D) {
  26. // Page-align the whole 3D texture.
  27. textureSize = ((textureSize * depthAligned) + 4095) & ~((DWORD) 4095);
  28. } else {
  29. // Page-align each slice.
  30. textureSize = ((textureSize + 4095) & ~((DWORD) 4095)) * depthAligned;
  31. }
  32. return textureSize;
  33. }
  34.  
  35. DWORD D3D::GetMipTailLevelOffsetCoords(DWORD Level, DWORD Width, DWORD Height, DWORD Depth,
  36. DWORD TailRowPitch, DWORD TailSlicePitch, DWORD GpuFormat,
  37. UINT * pOffsetX, UINT * pOffsetY, UINT * pOffsetZ) {
  38. DWORD offsets[3] = { 0 };
  39.  
  40. DWORD blockWidth, blockHeight;
  41. D3D::BlockSizeOfGpuFormat(GpuFormat, &blockWidth, &blockHeight);
  42. DWORD bitsPerPixel = (BYTE) D3D::g_FormatTable[GpuFormat];
  43.  
  44. int logWidth = 32 - __lzcnt(Width - 1);
  45. int logHeight = 32 - __lzcnt(Height - 1);
  46. int logDepth = 32 - __lzcnt(Depth - 1);
  47. DWORD shortestAxis = logWidth > logHeight;
  48. DWORD packSize[2] = { (DWORD) 1 << logWidth, (DWORD) 1 << logHeight };
  49.  
  50. if (Level >= 3) {
  51. DWORD longestAxis = !shortestAxis;
  52. offsets[longestAxis] = packSize[longestAxis] >> (Level - 2);
  53. if (offsets[longestAxis] < 4) {
  54. // Never reached for 2D textures - 16x16, the pack size, has the max mip level of 4.
  55. // At level 3, packSize[longestAxis] >> (Level - 2) will be 8, at level 4, it will be 4.
  56. offsets[2] = std::max(logDepth - Level, 1) * 4;
  57. }
  58. // Z offset is 0 otherwise.
  59. } else {
  60. offsets[shortestAxis] = 16 >> Level;
  61. // Z offset is 0.
  62. }
  63.  
  64. *pOffsetX = offsets[0] / blockWidth;
  65. *pOffsetY = offsets[1] / blockHeight;
  66. *pOffsetZ = offsets[2];
  67. return (offsets[2] * TailSlicePitch) + (offsets[1] * TailRowPitch) + (offsets[0] * blockWidth * bitsPerPixel / 8);
  68. }
  69.  
  70. DWORD XGRAPHICS::GetMipTailLevelOffsetCoords(UINT Width, UINT Height, UINT Depth,
  71. UINT Level, DWORD GpuFormat, BOOL Tiled, BOOL Border, XGPOINT3D * pOffset) {
  72. int borderSize = Border ? 1 : 0;
  73.  
  74. int logWidth = (borderSize - __lzcnt(Width - borderSize * 2 - 1)) + 32;
  75. int logHeight = (borderSize - __lzcnt(Height - borderSize * 2 - 1)) + 32
  76. int tailLevel = std::max(std::max(logWidth, logHeight) - 4, 0);
  77. if (Level < tailLevel) {
  78. pOffset->X = pOffset->Y = pOffset->Z = 0;
  79. return 0;
  80. }
  81. int logDepth = (Depth > 1 ? ((borderSize - __lzcnt(Depth - borderSize * 2 - 1)) + 32) : 0);
  82.  
  83. DWORD tailWidth = (DWORD) 1 << (logWidth - tailLevel);
  84. DWORD tailHeight = (DWORD) 1 << (logHeight - tailLevel);
  85. DWORD tailDepth = (DWORD) 1 << std::max(logDepth - tailLevel, 0);
  86. DWORD bitsPerPixel = (BYTE) D3D::g_FormatTable[GpuFormat];
  87. D3D::AlignTextureDimensions(&tailWidth, &tailHeight, &tailDepth,
  88. bitsPerPixel, GpuFormat, GPU_DIMENSION_2D, Tiled);
  89. DWORD tailRowPitch = tailWidth * bitsPerPixel / 8;
  90. DWORD tailSlicePitch = tailHeight * tailRowPitch;
  91. if (Depth <= 1) {
  92. tailSlicePitch = (tailSlicePitch + 4095) & ~((DWORD) 4095);
  93. }
  94.  
  95. UINT offsetX, offsetY, offsetZ;
  96. DWORD offset = D3D::GetMipTailLevelOffsetCoords(Level - tailLevel, tailWidth, tailHeight, tailDepth,
  97. tailRowPitch, tailSlicePitch, GpuFormat, &offsetX, &offsetY, &offsetZ);
  98. pOffset->X = offsetX;
  99. pOffset->Y = offsetY;
  100. pOffset->Z = offsetZ;
  101. return offset;
  102. }
Add Comment
Please, Sign In to add comment