TextureFactory.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Ryujinx.Graphics.Gal;
  2. using System;
  3. namespace Ryujinx.Core.Gpu
  4. {
  5. static class TextureFactory
  6. {
  7. public static GalTexture MakeTexture(NvGpu Gpu, 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. long TextureAddress = (uint)Tic[1];
  16. TextureAddress |= (long)((ushort)Tic[2]) << 32;
  17. TextureSwizzle Swizzle = (TextureSwizzle)((Tic[2] >> 21) & 7);
  18. int Pitch = (Tic[3] & 0xffff) << 5;
  19. int BlockHeightLog2 = (Tic[3] >> 3) & 7;
  20. int BlockHeight = 1 << BlockHeightLog2;
  21. int Width = (Tic[4] & 0xffff) + 1;
  22. int Height = (Tic[5] & 0xffff) + 1;
  23. Texture Texture = new Texture(
  24. TextureAddress,
  25. Width,
  26. Height,
  27. Pitch,
  28. BlockHeight,
  29. Swizzle,
  30. Format);
  31. byte[] Data = TextureReader.Read(Vmm, Texture);
  32. return new GalTexture(
  33. Data,
  34. Width,
  35. Height,
  36. Format,
  37. XSource,
  38. YSource,
  39. ZSource,
  40. WSource);
  41. }
  42. public static GalTextureSampler MakeSampler(NvGpu Gpu, NvGpuVmm Vmm, long TscPosition)
  43. {
  44. int[] Tsc = ReadWords(Vmm, TscPosition, 8);
  45. GalTextureWrap AddressU = (GalTextureWrap)((Tsc[0] >> 0) & 7);
  46. GalTextureWrap AddressV = (GalTextureWrap)((Tsc[0] >> 3) & 7);
  47. GalTextureWrap AddressP = (GalTextureWrap)((Tsc[0] >> 6) & 7);
  48. GalTextureFilter MagFilter = (GalTextureFilter) ((Tsc[1] >> 0) & 3);
  49. GalTextureFilter MinFilter = (GalTextureFilter) ((Tsc[1] >> 4) & 3);
  50. GalTextureMipFilter MipFilter = (GalTextureMipFilter)((Tsc[1] >> 6) & 3);
  51. GalColorF BorderColor = new GalColorF(
  52. BitConverter.Int32BitsToSingle(Tsc[4]),
  53. BitConverter.Int32BitsToSingle(Tsc[5]),
  54. BitConverter.Int32BitsToSingle(Tsc[6]),
  55. BitConverter.Int32BitsToSingle(Tsc[7]));
  56. return new GalTextureSampler(
  57. AddressU,
  58. AddressV,
  59. AddressP,
  60. MinFilter,
  61. MagFilter,
  62. MipFilter,
  63. BorderColor);
  64. }
  65. private static int[] ReadWords(NvGpuVmm Vmm, long Position, int Count)
  66. {
  67. int[] Words = new int[Count];
  68. for (int Index = 0; Index < Count; Index++, Position += 4)
  69. {
  70. Words[Index] = Vmm.ReadInt32(Position);
  71. }
  72. return Words;
  73. }
  74. }
  75. }