TextureFactory.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Ryujinx.Graphics.Gal;
  2. using Ryujinx.Graphics.Memory;
  3. using System;
  4. namespace Ryujinx.Graphics.Texture
  5. {
  6. static class TextureFactory
  7. {
  8. public static GalImage MakeTexture(NvGpuVmm Vmm, long TicPosition)
  9. {
  10. int[] Tic = ReadWords(Vmm, TicPosition, 8);
  11. GalImageFormat Format = GetImageFormat(Tic);
  12. GalTextureSource XSource = (GalTextureSource)((Tic[0] >> 19) & 7);
  13. GalTextureSource YSource = (GalTextureSource)((Tic[0] >> 22) & 7);
  14. GalTextureSource ZSource = (GalTextureSource)((Tic[0] >> 25) & 7);
  15. GalTextureSource WSource = (GalTextureSource)((Tic[0] >> 28) & 7);
  16. int Width = (Tic[4] & 0xffff) + 1;
  17. int Height = (Tic[5] & 0xffff) + 1;
  18. return new GalImage(
  19. Width,
  20. Height,
  21. Format,
  22. XSource,
  23. YSource,
  24. ZSource,
  25. WSource);
  26. }
  27. public static byte[] GetTextureData(NvGpuVmm Vmm, long TicPosition)
  28. {
  29. int[] Tic = ReadWords(Vmm, TicPosition, 8);
  30. GalImageFormat Format = GetImageFormat(Tic);
  31. long TextureAddress = (uint)Tic[1];
  32. TextureAddress |= (long)((ushort)Tic[2]) << 32;
  33. TextureSwizzle Swizzle = (TextureSwizzle)((Tic[2] >> 21) & 7);
  34. if (Swizzle == TextureSwizzle.BlockLinear ||
  35. Swizzle == TextureSwizzle.BlockLinearColorKey)
  36. {
  37. TextureAddress &= ~0x1ffL;
  38. }
  39. else if (Swizzle == TextureSwizzle.Pitch ||
  40. Swizzle == TextureSwizzle.PitchColorKey)
  41. {
  42. TextureAddress &= ~0x1fL;
  43. }
  44. int Pitch = (Tic[3] & 0xffff) << 5;
  45. int BlockHeightLog2 = (Tic[3] >> 3) & 7;
  46. int TileWidthLog2 = (Tic[3] >> 10) & 7;
  47. int BlockHeight = 1 << BlockHeightLog2;
  48. int TileWidth = 1 << TileWidthLog2;
  49. int Width = (Tic[4] & 0xffff) + 1;
  50. int Height = (Tic[5] & 0xffff) + 1;
  51. TextureInfo Texture = new TextureInfo(
  52. TextureAddress,
  53. Width,
  54. Height,
  55. Pitch,
  56. BlockHeight,
  57. TileWidth,
  58. Swizzle,
  59. Format);
  60. return TextureReader.Read(Vmm, Texture);
  61. }
  62. public static GalTextureSampler MakeSampler(NvGpu Gpu, NvGpuVmm Vmm, long TscPosition)
  63. {
  64. int[] Tsc = ReadWords(Vmm, TscPosition, 8);
  65. GalTextureWrap AddressU = (GalTextureWrap)((Tsc[0] >> 0) & 7);
  66. GalTextureWrap AddressV = (GalTextureWrap)((Tsc[0] >> 3) & 7);
  67. GalTextureWrap AddressP = (GalTextureWrap)((Tsc[0] >> 6) & 7);
  68. GalTextureFilter MagFilter = (GalTextureFilter) ((Tsc[1] >> 0) & 3);
  69. GalTextureFilter MinFilter = (GalTextureFilter) ((Tsc[1] >> 4) & 3);
  70. GalTextureMipFilter MipFilter = (GalTextureMipFilter)((Tsc[1] >> 6) & 3);
  71. GalColorF BorderColor = new GalColorF(
  72. BitConverter.Int32BitsToSingle(Tsc[4]),
  73. BitConverter.Int32BitsToSingle(Tsc[5]),
  74. BitConverter.Int32BitsToSingle(Tsc[6]),
  75. BitConverter.Int32BitsToSingle(Tsc[7]));
  76. return new GalTextureSampler(
  77. AddressU,
  78. AddressV,
  79. AddressP,
  80. MinFilter,
  81. MagFilter,
  82. MipFilter,
  83. BorderColor);
  84. }
  85. private static GalImageFormat GetImageFormat(int[] Tic)
  86. {
  87. GalTextureType RType = (GalTextureType)((Tic[0] >> 7) & 7);
  88. GalTextureType GType = (GalTextureType)((Tic[0] >> 10) & 7);
  89. GalTextureType BType = (GalTextureType)((Tic[0] >> 13) & 7);
  90. GalTextureType AType = (GalTextureType)((Tic[0] >> 16) & 7);
  91. GalTextureFormat Format = (GalTextureFormat)(Tic[0] & 0x7f);
  92. return ImageUtils.ConvertTexture(Format, RType, GType, BType, AType);
  93. }
  94. private static int[] ReadWords(NvGpuVmm Vmm, long Position, int Count)
  95. {
  96. int[] Words = new int[Count];
  97. for (int Index = 0; Index < Count; Index++, Position += 4)
  98. {
  99. Words[Index] = Vmm.ReadInt32(Position);
  100. }
  101. return Words;
  102. }
  103. }
  104. }