TextureFactory.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Ryujinx.Graphics.Gal;
  2. using Ryujinx.HLE.Gpu.Memory;
  3. using System;
  4. namespace Ryujinx.HLE.Gpu.Texture
  5. {
  6. static class TextureFactory
  7. {
  8. public static GalTexture MakeTexture(NvGpuVmm Vmm, long TicPosition)
  9. {
  10. int[] Tic = ReadWords(Vmm, TicPosition, 8);
  11. GalTextureFormat Format = (GalTextureFormat)(Tic[0] & 0x7f);
  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 GalTexture(
  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. GalTextureFormat Format = (GalTextureFormat)(Tic[0] & 0x7f);
  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 BlockHeight = 1 << BlockHeightLog2;
  47. int Width = (Tic[4] & 0xffff) + 1;
  48. int Height = (Tic[5] & 0xffff) + 1;
  49. TextureInfo Texture = new TextureInfo(
  50. TextureAddress,
  51. Width,
  52. Height,
  53. Pitch,
  54. BlockHeight,
  55. Swizzle,
  56. Format);
  57. return TextureReader.Read(Vmm, Texture);
  58. }
  59. public static GalTextureSampler MakeSampler(NvGpu Gpu, NvGpuVmm Vmm, long TscPosition)
  60. {
  61. int[] Tsc = ReadWords(Vmm, TscPosition, 8);
  62. GalTextureWrap AddressU = (GalTextureWrap)((Tsc[0] >> 0) & 7);
  63. GalTextureWrap AddressV = (GalTextureWrap)((Tsc[0] >> 3) & 7);
  64. GalTextureWrap AddressP = (GalTextureWrap)((Tsc[0] >> 6) & 7);
  65. GalTextureFilter MagFilter = (GalTextureFilter) ((Tsc[1] >> 0) & 3);
  66. GalTextureFilter MinFilter = (GalTextureFilter) ((Tsc[1] >> 4) & 3);
  67. GalTextureMipFilter MipFilter = (GalTextureMipFilter)((Tsc[1] >> 6) & 3);
  68. GalColorF BorderColor = new GalColorF(
  69. BitConverter.Int32BitsToSingle(Tsc[4]),
  70. BitConverter.Int32BitsToSingle(Tsc[5]),
  71. BitConverter.Int32BitsToSingle(Tsc[6]),
  72. BitConverter.Int32BitsToSingle(Tsc[7]));
  73. return new GalTextureSampler(
  74. AddressU,
  75. AddressV,
  76. AddressP,
  77. MinFilter,
  78. MagFilter,
  79. MipFilter,
  80. BorderColor);
  81. }
  82. private static int[] ReadWords(NvGpuVmm Vmm, long Position, int Count)
  83. {
  84. int[] Words = new int[Count];
  85. for (int Index = 0; Index < Count; Index++, Position += 4)
  86. {
  87. Words[Index] = Vmm.ReadInt32(Position);
  88. }
  89. return Words;
  90. }
  91. }
  92. }