TextureReader.cs 5.0 KB

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