TextureHelper.cs 5.3 KB

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