TextureDescriptorCapableGpuAccessor.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.Image;
  3. using Ryujinx.Graphics.Shader;
  4. namespace Ryujinx.Graphics.Gpu.Shader
  5. {
  6. abstract class TextureDescriptorCapableGpuAccessor : GpuAccessorBase, IGpuAccessor
  7. {
  8. public TextureDescriptorCapableGpuAccessor(GpuContext context) : base(context)
  9. {
  10. }
  11. public abstract T MemoryRead<T>(ulong address) where T : unmanaged;
  12. public abstract ITextureDescriptor GetTextureDescriptor(int handle, int cbufSlot);
  13. /// <summary>
  14. /// Queries texture format information, for shaders using image load or store.
  15. /// </summary>
  16. /// <remarks>
  17. /// This only returns non-compressed color formats.
  18. /// If the format of the texture is a compressed, depth or unsupported format, then a default value is returned.
  19. /// </remarks>
  20. /// <param name="handle">Texture handle</param>
  21. /// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
  22. /// <returns>Color format of the non-compressed texture</returns>
  23. public TextureFormat QueryTextureFormat(int handle, int cbufSlot = -1)
  24. {
  25. var descriptor = GetTextureDescriptor(handle, cbufSlot);
  26. if (!FormatTable.TryGetTextureFormat(descriptor.UnpackFormat(), descriptor.UnpackSrgb(), out FormatInfo formatInfo))
  27. {
  28. return TextureFormat.Unknown;
  29. }
  30. return formatInfo.Format switch
  31. {
  32. Format.R8Unorm => TextureFormat.R8Unorm,
  33. Format.R8Snorm => TextureFormat.R8Snorm,
  34. Format.R8Uint => TextureFormat.R8Uint,
  35. Format.R8Sint => TextureFormat.R8Sint,
  36. Format.R16Float => TextureFormat.R16Float,
  37. Format.R16Unorm => TextureFormat.R16Unorm,
  38. Format.R16Snorm => TextureFormat.R16Snorm,
  39. Format.R16Uint => TextureFormat.R16Uint,
  40. Format.R16Sint => TextureFormat.R16Sint,
  41. Format.R32Float => TextureFormat.R32Float,
  42. Format.R32Uint => TextureFormat.R32Uint,
  43. Format.R32Sint => TextureFormat.R32Sint,
  44. Format.R8G8Unorm => TextureFormat.R8G8Unorm,
  45. Format.R8G8Snorm => TextureFormat.R8G8Snorm,
  46. Format.R8G8Uint => TextureFormat.R8G8Uint,
  47. Format.R8G8Sint => TextureFormat.R8G8Sint,
  48. Format.R16G16Float => TextureFormat.R16G16Float,
  49. Format.R16G16Unorm => TextureFormat.R16G16Unorm,
  50. Format.R16G16Snorm => TextureFormat.R16G16Snorm,
  51. Format.R16G16Uint => TextureFormat.R16G16Uint,
  52. Format.R16G16Sint => TextureFormat.R16G16Sint,
  53. Format.R32G32Float => TextureFormat.R32G32Float,
  54. Format.R32G32Uint => TextureFormat.R32G32Uint,
  55. Format.R32G32Sint => TextureFormat.R32G32Sint,
  56. Format.R8G8B8A8Unorm => TextureFormat.R8G8B8A8Unorm,
  57. Format.R8G8B8A8Snorm => TextureFormat.R8G8B8A8Snorm,
  58. Format.R8G8B8A8Uint => TextureFormat.R8G8B8A8Uint,
  59. Format.R8G8B8A8Sint => TextureFormat.R8G8B8A8Sint,
  60. Format.R8G8B8A8Srgb => TextureFormat.R8G8B8A8Unorm,
  61. Format.R16G16B16A16Float => TextureFormat.R16G16B16A16Float,
  62. Format.R16G16B16A16Unorm => TextureFormat.R16G16B16A16Unorm,
  63. Format.R16G16B16A16Snorm => TextureFormat.R16G16B16A16Snorm,
  64. Format.R16G16B16A16Uint => TextureFormat.R16G16B16A16Uint,
  65. Format.R16G16B16A16Sint => TextureFormat.R16G16B16A16Sint,
  66. Format.R32G32B32A32Float => TextureFormat.R32G32B32A32Float,
  67. Format.R32G32B32A32Uint => TextureFormat.R32G32B32A32Uint,
  68. Format.R32G32B32A32Sint => TextureFormat.R32G32B32A32Sint,
  69. Format.R10G10B10A2Unorm => TextureFormat.R10G10B10A2Unorm,
  70. Format.R10G10B10A2Uint => TextureFormat.R10G10B10A2Uint,
  71. Format.R11G11B10Float => TextureFormat.R11G11B10Float,
  72. _ => TextureFormat.Unknown
  73. };
  74. }
  75. /// <summary>
  76. /// Queries texture target information.
  77. /// </summary>
  78. /// <param name="handle">Texture handle</param>
  79. /// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
  80. /// <returns>True if the texture is a buffer texture, false otherwise</returns>
  81. public bool QueryIsTextureBuffer(int handle, int cbufSlot = -1)
  82. {
  83. return GetTextureDescriptor(handle, cbufSlot).UnpackTextureTarget() == TextureTarget.TextureBuffer;
  84. }
  85. /// <summary>
  86. /// Queries texture target information.
  87. /// </summary>
  88. /// <param name="handle">Texture handle</param>
  89. /// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
  90. /// <returns>True if the texture is a rectangle texture, false otherwise</returns>
  91. public bool QueryIsTextureRectangle(int handle, int cbufSlot = -1)
  92. {
  93. var descriptor = GetTextureDescriptor(handle, cbufSlot);
  94. TextureTarget target = descriptor.UnpackTextureTarget();
  95. bool is2DTexture = target == TextureTarget.Texture2D ||
  96. target == TextureTarget.Texture2DRect;
  97. return !descriptor.UnpackTextureCoordNormalized() && is2DTexture;
  98. }
  99. }
  100. }