TextureFactory.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Ryujinx.Graphics.Gal;
  2. using System;
  3. namespace Ryujinx.Core.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. int Pitch = (Tic[3] & 0xffff) << 5;
  34. int BlockHeightLog2 = (Tic[3] >> 3) & 7;
  35. int BlockHeight = 1 << BlockHeightLog2;
  36. int Width = (Tic[4] & 0xffff) + 1;
  37. int Height = (Tic[5] & 0xffff) + 1;
  38. Texture Texture = new Texture(
  39. TextureAddress,
  40. Width,
  41. Height,
  42. Pitch,
  43. BlockHeight,
  44. Swizzle,
  45. Format);
  46. return TextureReader.Read(Vmm, Texture);
  47. }
  48. public static GalTextureSampler MakeSampler(NvGpu Gpu, NvGpuVmm Vmm, long TscPosition)
  49. {
  50. int[] Tsc = ReadWords(Vmm, TscPosition, 8);
  51. GalTextureWrap AddressU = (GalTextureWrap)((Tsc[0] >> 0) & 7);
  52. GalTextureWrap AddressV = (GalTextureWrap)((Tsc[0] >> 3) & 7);
  53. GalTextureWrap AddressP = (GalTextureWrap)((Tsc[0] >> 6) & 7);
  54. GalTextureFilter MagFilter = (GalTextureFilter) ((Tsc[1] >> 0) & 3);
  55. GalTextureFilter MinFilter = (GalTextureFilter) ((Tsc[1] >> 4) & 3);
  56. GalTextureMipFilter MipFilter = (GalTextureMipFilter)((Tsc[1] >> 6) & 3);
  57. GalColorF BorderColor = new GalColorF(
  58. BitConverter.Int32BitsToSingle(Tsc[4]),
  59. BitConverter.Int32BitsToSingle(Tsc[5]),
  60. BitConverter.Int32BitsToSingle(Tsc[6]),
  61. BitConverter.Int32BitsToSingle(Tsc[7]));
  62. return new GalTextureSampler(
  63. AddressU,
  64. AddressV,
  65. AddressP,
  66. MinFilter,
  67. MagFilter,
  68. MipFilter,
  69. BorderColor);
  70. }
  71. private static int[] ReadWords(NvGpuVmm Vmm, long Position, int Count)
  72. {
  73. int[] Words = new int[Count];
  74. for (int Index = 0; Index < Count; Index++, Position += 4)
  75. {
  76. Words[Index] = Vmm.ReadInt32(Position);
  77. }
  78. return Words;
  79. }
  80. }
  81. }