TextureReader.cs 4.8 KB

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