TextureHelper.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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(GalImage Image)
  27. {
  28. switch (Image.Format)
  29. {
  30. case GalImageFormat.R32G32B32A32_SFLOAT:
  31. case GalImageFormat.R32G32B32A32_SINT:
  32. case GalImageFormat.R32G32B32A32_UINT:
  33. return Image.Width * Image.Height * 16;
  34. case GalImageFormat.R16G16B16A16_SFLOAT:
  35. case GalImageFormat.R16G16B16A16_SINT:
  36. case GalImageFormat.R16G16B16A16_SNORM:
  37. case GalImageFormat.R16G16B16A16_UINT:
  38. case GalImageFormat.R16G16B16A16_UNORM:
  39. case GalImageFormat.R32G32_SFLOAT:
  40. case GalImageFormat.R32G32_SINT:
  41. case GalImageFormat.R32G32_UINT:
  42. return Image.Width * Image.Height * 8;
  43. case GalImageFormat.A8B8G8R8_SINT_PACK32:
  44. case GalImageFormat.A8B8G8R8_SNORM_PACK32:
  45. case GalImageFormat.A8B8G8R8_UINT_PACK32:
  46. case GalImageFormat.A8B8G8R8_UNORM_PACK32:
  47. case GalImageFormat.A8B8G8R8_SRGB_PACK32:
  48. case GalImageFormat.A2B10G10R10_SINT_PACK32:
  49. case GalImageFormat.A2B10G10R10_SNORM_PACK32:
  50. case GalImageFormat.A2B10G10R10_UINT_PACK32:
  51. case GalImageFormat.A2B10G10R10_UNORM_PACK32:
  52. case GalImageFormat.R16G16_SFLOAT:
  53. case GalImageFormat.R16G16_SINT:
  54. case GalImageFormat.R16G16_SNORM:
  55. case GalImageFormat.R16G16_UINT:
  56. case GalImageFormat.R16G16_UNORM:
  57. case GalImageFormat.R32_SFLOAT:
  58. case GalImageFormat.R32_SINT:
  59. case GalImageFormat.R32_UINT:
  60. case GalImageFormat.D32_SFLOAT:
  61. case GalImageFormat.B10G11R11_UFLOAT_PACK32:
  62. case GalImageFormat.D24_UNORM_S8_UINT:
  63. return Image.Width * Image.Height * 4;
  64. case GalImageFormat.B4G4R4A4_UNORM_PACK16:
  65. case GalImageFormat.A1R5G5B5_UNORM_PACK16:
  66. case GalImageFormat.B5G6R5_UNORM_PACK16:
  67. case GalImageFormat.R8G8_SINT:
  68. case GalImageFormat.R8G8_SNORM:
  69. case GalImageFormat.R8G8_UINT:
  70. case GalImageFormat.R8G8_UNORM:
  71. case GalImageFormat.R16_SFLOAT:
  72. case GalImageFormat.R16_SINT:
  73. case GalImageFormat.R16_SNORM:
  74. case GalImageFormat.R16_UINT:
  75. case GalImageFormat.R16_UNORM:
  76. case GalImageFormat.D16_UNORM:
  77. return Image.Width * Image.Height * 2;
  78. case GalImageFormat.R8_SINT:
  79. case GalImageFormat.R8_SNORM:
  80. case GalImageFormat.R8_UINT:
  81. case GalImageFormat.R8_UNORM:
  82. return Image.Width * Image.Height;
  83. case GalImageFormat.BC1_RGBA_UNORM_BLOCK:
  84. case GalImageFormat.BC4_SNORM_BLOCK:
  85. case GalImageFormat.BC4_UNORM_BLOCK:
  86. {
  87. return CompressedTextureSize(Image.Width, Image.Height, 4, 4, 8);
  88. }
  89. case GalImageFormat.BC6H_SFLOAT_BLOCK:
  90. case GalImageFormat.BC6H_UFLOAT_BLOCK:
  91. case GalImageFormat.BC7_UNORM_BLOCK:
  92. case GalImageFormat.BC2_UNORM_BLOCK:
  93. case GalImageFormat.BC3_UNORM_BLOCK:
  94. case GalImageFormat.BC5_SNORM_BLOCK:
  95. case GalImageFormat.BC5_UNORM_BLOCK:
  96. case GalImageFormat.ASTC_4x4_UNORM_BLOCK:
  97. {
  98. return CompressedTextureSize(Image.Width, Image.Height, 4, 4, 16);
  99. }
  100. case GalImageFormat.ASTC_5x5_UNORM_BLOCK:
  101. {
  102. return CompressedTextureSize(Image.Width, Image.Height, 5, 5, 16);
  103. }
  104. case GalImageFormat.ASTC_6x6_UNORM_BLOCK:
  105. {
  106. return CompressedTextureSize(Image.Width, Image.Height, 6, 6, 16);
  107. }
  108. case GalImageFormat.ASTC_8x8_UNORM_BLOCK:
  109. {
  110. return CompressedTextureSize(Image.Width, Image.Height, 8, 8, 16);
  111. }
  112. case GalImageFormat.ASTC_10x10_UNORM_BLOCK:
  113. {
  114. return CompressedTextureSize(Image.Width, Image.Height, 10, 10, 16);
  115. }
  116. case GalImageFormat.ASTC_12x12_UNORM_BLOCK:
  117. {
  118. return CompressedTextureSize(Image.Width, Image.Height, 12, 12, 16);
  119. }
  120. case GalImageFormat.ASTC_5x4_UNORM_BLOCK:
  121. {
  122. return CompressedTextureSize(Image.Width, Image.Height, 5, 4, 16);
  123. }
  124. case GalImageFormat.ASTC_6x5_UNORM_BLOCK:
  125. {
  126. return CompressedTextureSize(Image.Width, Image.Height, 6, 5, 16);
  127. }
  128. case GalImageFormat.ASTC_8x6_UNORM_BLOCK:
  129. {
  130. return CompressedTextureSize(Image.Width, Image.Height, 8, 6, 16);
  131. }
  132. case GalImageFormat.ASTC_10x8_UNORM_BLOCK:
  133. {
  134. return CompressedTextureSize(Image.Width, Image.Height, 10, 8, 16);
  135. }
  136. case GalImageFormat.ASTC_12x10_UNORM_BLOCK:
  137. {
  138. return CompressedTextureSize(Image.Width, Image.Height, 12, 10, 16);
  139. }
  140. case GalImageFormat.ASTC_8x5_UNORM_BLOCK:
  141. {
  142. return CompressedTextureSize(Image.Width, Image.Height, 8, 5, 16);
  143. }
  144. case GalImageFormat.ASTC_10x5_UNORM_BLOCK:
  145. {
  146. return CompressedTextureSize(Image.Width, Image.Height, 10, 5, 16);
  147. }
  148. case GalImageFormat.ASTC_10x6_UNORM_BLOCK:
  149. {
  150. return CompressedTextureSize(Image.Width, Image.Height, 10, 6, 16);
  151. }
  152. }
  153. throw new NotImplementedException("0x" + Image.Format.ToString("x2"));
  154. }
  155. public static int CompressedTextureSize(int TextureWidth, int TextureHeight, int BlockWidth, int BlockHeight, int Bpb)
  156. {
  157. int W = (TextureWidth + (BlockWidth - 1)) / BlockWidth;
  158. int H = (TextureHeight + (BlockHeight - 1)) / BlockHeight;
  159. return W * H * Bpb;
  160. }
  161. public static (AMemory Memory, long Position) GetMemoryAndPosition(
  162. IAMemory Memory,
  163. long Position)
  164. {
  165. if (Memory is NvGpuVmm Vmm)
  166. {
  167. return (Vmm.Memory, Vmm.GetPhysicalAddress(Position));
  168. }
  169. return ((AMemory)Memory, Position);
  170. }
  171. }
  172. }