TextureFactory.cs 3.7 KB

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