TexturePool.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.GAL.Texture;
  3. using Ryujinx.Graphics.Gpu.Memory;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. namespace Ryujinx.Graphics.Gpu.Image
  8. {
  9. class TexturePool : Pool<Texture>
  10. {
  11. public LinkedListNode<TexturePool> CacheNode { get; set; }
  12. private struct TextureContainer
  13. {
  14. public Texture Texture0 { get; set; }
  15. public Texture Texture1 { get; set; }
  16. }
  17. public TexturePool(
  18. GpuContext context,
  19. ulong address,
  20. int maximumId) : base(context, address, maximumId) { }
  21. public override Texture Get(int id)
  22. {
  23. if ((uint)id >= Items.Length)
  24. {
  25. return null;
  26. }
  27. SynchronizeMemory();
  28. Texture texture = Items[id];
  29. if (texture == null)
  30. {
  31. ulong address = Address + (ulong)(uint)id * DescriptorSize;
  32. Span<byte> data = Context.PhysicalMemory.Read(address, DescriptorSize);
  33. TextureDescriptor descriptor = MemoryMarshal.Cast<byte, TextureDescriptor>(data)[0];
  34. TextureInfo info = GetInfo(descriptor);
  35. // Bad address. We can't add a texture with a invalid address
  36. // to the cache.
  37. if (info.Address == MemoryManager.BadAddress)
  38. {
  39. return null;
  40. }
  41. texture = Context.Methods.TextureManager.FindOrCreateTexture(info, TextureSearchFlags.Sampler);
  42. texture.IncrementReferenceCount();
  43. Items[id] = texture;
  44. }
  45. else
  46. {
  47. // Memory is automatically synchronized on texture creation.
  48. texture.SynchronizeMemory();
  49. }
  50. return texture;
  51. }
  52. protected override void InvalidateRangeImpl(ulong address, ulong size)
  53. {
  54. ulong endAddress = address + size;
  55. for (; address < endAddress; address += DescriptorSize)
  56. {
  57. int id = (int)((address - Address) / DescriptorSize);
  58. Texture texture = Items[id];
  59. if (texture != null)
  60. {
  61. Span<byte> data = Context.PhysicalMemory.Read(address, DescriptorSize);
  62. TextureDescriptor descriptor = MemoryMarshal.Cast<byte, TextureDescriptor>(data)[0];
  63. // If the descriptors are the same, the texture is the same,
  64. // we don't need to remove as it was not modified. Just continue.
  65. if (texture.IsPerfectMatch(GetInfo(descriptor), TextureSearchFlags.Strict))
  66. {
  67. continue;
  68. }
  69. texture.DecrementReferenceCount();
  70. Items[id] = null;
  71. }
  72. }
  73. }
  74. private TextureInfo GetInfo(TextureDescriptor descriptor)
  75. {
  76. ulong address = Context.MemoryManager.Translate(descriptor.UnpackAddress());
  77. int width = descriptor.UnpackWidth();
  78. int height = descriptor.UnpackHeight();
  79. int depthOrLayers = descriptor.UnpackDepth();
  80. int levels = descriptor.UnpackLevels();
  81. TextureMsaaMode msaaMode = descriptor.UnpackTextureMsaaMode();
  82. int samplesInX = msaaMode.SamplesInX();
  83. int samplesInY = msaaMode.SamplesInY();
  84. int stride = descriptor.UnpackStride();
  85. TextureDescriptorType descriptorType = descriptor.UnpackTextureDescriptorType();
  86. bool isLinear = descriptorType == TextureDescriptorType.Linear;
  87. Target target = descriptor.UnpackTextureTarget().Convert((samplesInX | samplesInY) != 1);
  88. uint format = descriptor.UnpackFormat();
  89. bool srgb = descriptor.UnpackSrgb();
  90. if (!FormatTable.TryGetTextureFormat(format, srgb, out FormatInfo formatInfo))
  91. {
  92. // TODO: Warning.
  93. formatInfo = FormatInfo.Default;
  94. }
  95. int gobBlocksInY = descriptor.UnpackGobBlocksInY();
  96. int gobBlocksInZ = descriptor.UnpackGobBlocksInZ();
  97. int gobBlocksInTileX = descriptor.UnpackGobBlocksInTileX();
  98. SwizzleComponent swizzleR = descriptor.UnpackSwizzleR().Convert();
  99. SwizzleComponent swizzleG = descriptor.UnpackSwizzleG().Convert();
  100. SwizzleComponent swizzleB = descriptor.UnpackSwizzleB().Convert();
  101. SwizzleComponent swizzleA = descriptor.UnpackSwizzleA().Convert();
  102. DepthStencilMode depthStencilMode = GetDepthStencilMode(
  103. formatInfo.Format,
  104. swizzleR,
  105. swizzleG,
  106. swizzleB,
  107. swizzleA);
  108. return new TextureInfo(
  109. address,
  110. width,
  111. height,
  112. depthOrLayers,
  113. levels,
  114. samplesInX,
  115. samplesInY,
  116. stride,
  117. isLinear,
  118. gobBlocksInY,
  119. gobBlocksInZ,
  120. gobBlocksInTileX,
  121. target,
  122. formatInfo,
  123. depthStencilMode,
  124. swizzleR,
  125. swizzleG,
  126. swizzleB,
  127. swizzleA);
  128. }
  129. private static DepthStencilMode GetDepthStencilMode(Format format, params SwizzleComponent[] components)
  130. {
  131. // R = Depth, G = Stencil.
  132. // On 24-bits depth formats, this is inverted (Stencil is R etc).
  133. // NVN setup:
  134. // For depth, A is set to 1.0f, the other components are set to Depth.
  135. // For stencil, all components are set to Stencil.
  136. SwizzleComponent component = components[0];
  137. for (int index = 1; index < 4 && !IsRG(component); index++)
  138. {
  139. component = components[index];
  140. }
  141. if (!IsRG(component))
  142. {
  143. return DepthStencilMode.Depth;
  144. }
  145. if (format == Format.D24X8Unorm || format == Format.D24UnormS8Uint)
  146. {
  147. return component == SwizzleComponent.Red
  148. ? DepthStencilMode.Stencil
  149. : DepthStencilMode.Depth;
  150. }
  151. else
  152. {
  153. return component == SwizzleComponent.Red
  154. ? DepthStencilMode.Depth
  155. : DepthStencilMode.Stencil;
  156. }
  157. }
  158. private static bool IsRG(SwizzleComponent component)
  159. {
  160. return component == SwizzleComponent.Red ||
  161. component == SwizzleComponent.Green;
  162. }
  163. protected override void Delete(Texture item)
  164. {
  165. item?.DecrementReferenceCount();
  166. }
  167. }
  168. }