TextureFactory.cs 2.6 KB

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