TextureFactory.cs 2.7 KB

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