TextureReader.cs 4.9 KB

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