TextureHelper.cs 7.6 KB

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