TextureReader.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Graphics.Gal;
  3. using System;
  4. namespace Ryujinx.Graphics.Gpu
  5. {
  6. public static class TextureReader
  7. {
  8. public static byte[] Read(AMemory Memory, Texture Texture)
  9. {
  10. switch (Texture.Format)
  11. {
  12. case GalTextureFormat.A8B8G8R8: return Read4Bpp (Memory, Texture);
  13. case GalTextureFormat.A1B5G5R5: return Read2Bpp (Memory, Texture);
  14. case GalTextureFormat.B5G6R5: return Read2Bpp (Memory, Texture);
  15. case GalTextureFormat.BC1: return Read8Bpt4x4 (Memory, Texture);
  16. case GalTextureFormat.BC2: return Read16Bpt4x4(Memory, Texture);
  17. case GalTextureFormat.BC3: return Read16Bpt4x4(Memory, Texture);
  18. case GalTextureFormat.BC4: return Read8Bpt4x4 (Memory, Texture);
  19. case GalTextureFormat.BC5: return Read16Bpt4x4(Memory, Texture);
  20. }
  21. throw new NotImplementedException(Texture.Format.ToString());
  22. }
  23. private unsafe static byte[] Read2Bpp(AMemory Memory, Texture Texture)
  24. {
  25. int Width = Texture.Width;
  26. int Height = Texture.Height;
  27. byte[] Output = new byte[Width * Height * 2];
  28. ISwizzle Swizzle = GetSwizzle(Texture, Width, 2);
  29. fixed (byte* BuffPtr = Output)
  30. {
  31. long OutOffs = 0;
  32. for (int Y = 0; Y < Height; Y++)
  33. for (int X = 0; X < Width; X++)
  34. {
  35. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  36. short Pixel = Memory.ReadInt16Unchecked(Texture.Position + Offset);
  37. *(short*)(BuffPtr + OutOffs) = Pixel;
  38. OutOffs += 2;
  39. }
  40. }
  41. return Output;
  42. }
  43. private unsafe static byte[] Read4Bpp(AMemory Memory, Texture Texture)
  44. {
  45. int Width = Texture.Width;
  46. int Height = Texture.Height;
  47. byte[] Output = new byte[Width * Height * 4];
  48. ISwizzle Swizzle = GetSwizzle(Texture, Width, 4);
  49. fixed (byte* BuffPtr = Output)
  50. {
  51. long OutOffs = 0;
  52. for (int Y = 0; Y < Height; Y++)
  53. for (int X = 0; X < Width; X++)
  54. {
  55. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  56. int Pixel = Memory.ReadInt32Unchecked(Texture.Position + Offset);
  57. *(int*)(BuffPtr + OutOffs) = Pixel;
  58. OutOffs += 4;
  59. }
  60. }
  61. return Output;
  62. }
  63. private unsafe static byte[] Read8Bpt4x4(AMemory Memory, Texture Texture)
  64. {
  65. int Width = (Texture.Width + 3) / 4;
  66. int Height = (Texture.Height + 3) / 4;
  67. byte[] Output = new byte[Width * Height * 8];
  68. ISwizzle Swizzle = GetSwizzle(Texture, Width, 8);
  69. fixed (byte* BuffPtr = Output)
  70. {
  71. long OutOffs = 0;
  72. for (int Y = 0; Y < Height; Y++)
  73. for (int X = 0; X < Width; X++)
  74. {
  75. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  76. long Tile = Memory.ReadInt64Unchecked(Texture.Position + Offset);
  77. *(long*)(BuffPtr + OutOffs) = Tile;
  78. OutOffs += 8;
  79. }
  80. }
  81. return Output;
  82. }
  83. private unsafe static byte[] Read16Bpt4x4(AMemory Memory, Texture Texture)
  84. {
  85. int Width = (Texture.Width + 3) / 4;
  86. int Height = (Texture.Height + 3) / 4;
  87. byte[] Output = new byte[Width * Height * 16];
  88. ISwizzle Swizzle = GetSwizzle(Texture, Width, 16);
  89. fixed (byte* BuffPtr = Output)
  90. {
  91. long OutOffs = 0;
  92. for (int Y = 0; Y < Height; Y++)
  93. for (int X = 0; X < Width; X++)
  94. {
  95. long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
  96. long Tile0 = Memory.ReadInt64Unchecked(Texture.Position + Offset + 0);
  97. long Tile1 = Memory.ReadInt64Unchecked(Texture.Position + Offset + 8);
  98. *(long*)(BuffPtr + OutOffs + 0) = Tile0;
  99. *(long*)(BuffPtr + OutOffs + 8) = Tile1;
  100. OutOffs += 16;
  101. }
  102. }
  103. return Output;
  104. }
  105. private static ISwizzle GetSwizzle(Texture Texture, int Width, int Bpp)
  106. {
  107. switch (Texture.Swizzle)
  108. {
  109. case TextureSwizzle.Pitch:
  110. case TextureSwizzle.PitchColorKey:
  111. return new LinearSwizzle(Texture.Pitch, Bpp);
  112. case TextureSwizzle.BlockLinear:
  113. case TextureSwizzle.BlockLinearColorKey:
  114. return new BlockLinearSwizzle(Width, Bpp, Texture.BlockHeight);
  115. }
  116. throw new NotImplementedException(Texture.Swizzle.ToString());
  117. }
  118. }
  119. }