TextureHelper.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Graphics.Gal;
  3. using Ryujinx.HLE.Gpu.Memory;
  4. using System;
  5. namespace Ryujinx.HLE.Gpu.Texture
  6. {
  7. static class TextureHelper
  8. {
  9. public static ISwizzle GetSwizzle(TextureInfo Texture, int BlockWidth, int Bpp)
  10. {
  11. int Width = (Texture.Width + (BlockWidth - 1)) / BlockWidth;
  12. int AlignMask = Texture.TileWidth * (64 / Bpp) - 1;
  13. Width = (Width + AlignMask) & ~AlignMask;
  14. switch (Texture.Swizzle)
  15. {
  16. case TextureSwizzle._1dBuffer:
  17. case TextureSwizzle.Pitch:
  18. case TextureSwizzle.PitchColorKey:
  19. return new LinearSwizzle(Texture.Pitch, Bpp);
  20. case TextureSwizzle.BlockLinear:
  21. case TextureSwizzle.BlockLinearColorKey:
  22. return new BlockLinearSwizzle(Width, Bpp, Texture.BlockHeight);
  23. }
  24. throw new NotImplementedException(Texture.Swizzle.ToString());
  25. }
  26. public static int GetTextureSize(GalTexture Texture)
  27. {
  28. switch (Texture.Format)
  29. {
  30. case GalTextureFormat.R32G32B32A32:
  31. return Texture.Width * Texture.Height * 16;
  32. case GalTextureFormat.R16G16B16A16:
  33. return Texture.Width * Texture.Height * 8;
  34. case GalTextureFormat.A8B8G8R8:
  35. case GalTextureFormat.A2B10G10R10:
  36. case GalTextureFormat.R32:
  37. case GalTextureFormat.ZF32:
  38. case GalTextureFormat.BF10GF11RF11:
  39. case GalTextureFormat.Z24S8:
  40. return Texture.Width * Texture.Height * 4;
  41. case GalTextureFormat.A1B5G5R5:
  42. case GalTextureFormat.B5G6R5:
  43. case GalTextureFormat.G8R8:
  44. case GalTextureFormat.R16:
  45. return Texture.Width * Texture.Height * 2;
  46. case GalTextureFormat.R8:
  47. return Texture.Width * Texture.Height;
  48. case GalTextureFormat.BC1:
  49. case GalTextureFormat.BC4:
  50. {
  51. return CompressedTextureSize(Texture.Width, Texture.Height, 4, 4, 8);
  52. }
  53. case GalTextureFormat.BC6H_SF16:
  54. case GalTextureFormat.BC6H_UF16:
  55. case GalTextureFormat.BC7U:
  56. case GalTextureFormat.BC2:
  57. case GalTextureFormat.BC3:
  58. case GalTextureFormat.BC5:
  59. case GalTextureFormat.Astc2D4x4:
  60. {
  61. return CompressedTextureSize(Texture.Width, Texture.Height, 4, 4, 16);
  62. }
  63. case GalTextureFormat.Astc2D5x5:
  64. {
  65. return CompressedTextureSize(Texture.Width, Texture.Height, 5, 5, 16);
  66. }
  67. case GalTextureFormat.Astc2D6x6:
  68. {
  69. return CompressedTextureSize(Texture.Width, Texture.Height, 6, 6, 16);
  70. }
  71. case GalTextureFormat.Astc2D8x8:
  72. {
  73. return CompressedTextureSize(Texture.Width, Texture.Height, 8, 8, 16);
  74. }
  75. case GalTextureFormat.Astc2D10x10:
  76. {
  77. return CompressedTextureSize(Texture.Width, Texture.Height, 10, 10, 16);
  78. }
  79. case GalTextureFormat.Astc2D12x12:
  80. {
  81. return CompressedTextureSize(Texture.Width, Texture.Height, 12, 12, 16);
  82. }
  83. case GalTextureFormat.Astc2D5x4:
  84. {
  85. return CompressedTextureSize(Texture.Width, Texture.Height, 5, 4, 16);
  86. }
  87. case GalTextureFormat.Astc2D6x5:
  88. {
  89. return CompressedTextureSize(Texture.Width, Texture.Height, 6, 5, 16);
  90. }
  91. case GalTextureFormat.Astc2D8x6:
  92. {
  93. return CompressedTextureSize(Texture.Width, Texture.Height, 8, 6, 16);
  94. }
  95. case GalTextureFormat.Astc2D10x8:
  96. {
  97. return CompressedTextureSize(Texture.Width, Texture.Height, 10, 8, 16);
  98. }
  99. case GalTextureFormat.Astc2D12x10:
  100. {
  101. return CompressedTextureSize(Texture.Width, Texture.Height, 12, 10, 16);
  102. }
  103. case GalTextureFormat.Astc2D8x5:
  104. {
  105. return CompressedTextureSize(Texture.Width, Texture.Height, 8, 5, 16);
  106. }
  107. case GalTextureFormat.Astc2D10x5:
  108. {
  109. return CompressedTextureSize(Texture.Width, Texture.Height, 10, 5, 16);
  110. }
  111. case GalTextureFormat.Astc2D10x6:
  112. {
  113. return CompressedTextureSize(Texture.Width, Texture.Height, 10, 6, 16);
  114. }
  115. }
  116. throw new NotImplementedException(Texture.Format.ToString());
  117. }
  118. public static int CompressedTextureSize(int TextureWidth, int TextureHeight, int BlockWidth, int BlockHeight, int Bpb)
  119. {
  120. int W = (TextureWidth + (BlockWidth - 1)) / BlockWidth;
  121. int H = (TextureHeight + (BlockHeight - 1)) / BlockHeight;
  122. return W * H * Bpb;
  123. }
  124. public static (AMemory Memory, long Position) GetMemoryAndPosition(
  125. IAMemory Memory,
  126. long Position)
  127. {
  128. if (Memory is NvGpuVmm Vmm)
  129. {
  130. return (Vmm.Memory, Vmm.GetPhysicalAddress(Position));
  131. }
  132. return ((AMemory)Memory, Position);
  133. }
  134. }
  135. }