TextureReader.cs 3.9 KB

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