TextureFactory.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. TextureSwizzle Swizzle = (TextureSwizzle)((Tic[2] >> 21) & 7);
  17. GalMemoryLayout Layout;
  18. if (Swizzle == TextureSwizzle.BlockLinear ||
  19. Swizzle == TextureSwizzle.BlockLinearColorKey)
  20. {
  21. Layout = GalMemoryLayout.BlockLinear;
  22. }
  23. else
  24. {
  25. Layout = GalMemoryLayout.Pitch;
  26. }
  27. int BlockHeightLog2 = (Tic[3] >> 3) & 7;
  28. int TileWidthLog2 = (Tic[3] >> 10) & 7;
  29. int BlockHeight = 1 << BlockHeightLog2;
  30. int TileWidth = 1 << TileWidthLog2;
  31. int Width = (Tic[4] & 0xffff) + 1;
  32. int Height = (Tic[5] & 0xffff) + 1;
  33. return new GalImage(
  34. Width,
  35. Height,
  36. TileWidth,
  37. BlockHeight,
  38. Layout,
  39. Format,
  40. XSource,
  41. YSource,
  42. ZSource,
  43. WSource);
  44. }
  45. public static GalTextureSampler MakeSampler(NvGpu Gpu, NvGpuVmm Vmm, long TscPosition)
  46. {
  47. int[] Tsc = ReadWords(Vmm, TscPosition, 8);
  48. GalTextureWrap AddressU = (GalTextureWrap)((Tsc[0] >> 0) & 7);
  49. GalTextureWrap AddressV = (GalTextureWrap)((Tsc[0] >> 3) & 7);
  50. GalTextureWrap AddressP = (GalTextureWrap)((Tsc[0] >> 6) & 7);
  51. GalTextureFilter MagFilter = (GalTextureFilter) ((Tsc[1] >> 0) & 3);
  52. GalTextureFilter MinFilter = (GalTextureFilter) ((Tsc[1] >> 4) & 3);
  53. GalTextureMipFilter MipFilter = (GalTextureMipFilter)((Tsc[1] >> 6) & 3);
  54. GalColorF BorderColor = new GalColorF(
  55. BitConverter.Int32BitsToSingle(Tsc[4]),
  56. BitConverter.Int32BitsToSingle(Tsc[5]),
  57. BitConverter.Int32BitsToSingle(Tsc[6]),
  58. BitConverter.Int32BitsToSingle(Tsc[7]));
  59. return new GalTextureSampler(
  60. AddressU,
  61. AddressV,
  62. AddressP,
  63. MinFilter,
  64. MagFilter,
  65. MipFilter,
  66. BorderColor);
  67. }
  68. private static GalImageFormat GetImageFormat(int[] Tic)
  69. {
  70. GalTextureType RType = (GalTextureType)((Tic[0] >> 7) & 7);
  71. GalTextureType GType = (GalTextureType)((Tic[0] >> 10) & 7);
  72. GalTextureType BType = (GalTextureType)((Tic[0] >> 13) & 7);
  73. GalTextureType AType = (GalTextureType)((Tic[0] >> 16) & 7);
  74. GalTextureFormat Format = (GalTextureFormat)(Tic[0] & 0x7f);
  75. return ImageUtils.ConvertTexture(Format, RType, GType, BType, AType);
  76. }
  77. private static int[] ReadWords(NvGpuVmm Vmm, long Position, int Count)
  78. {
  79. int[] Words = new int[Count];
  80. for (int Index = 0; Index < Count; Index++, Position += 4)
  81. {
  82. Words[Index] = Vmm.ReadInt32(Position);
  83. }
  84. return Words;
  85. }
  86. }
  87. }