TextureDescriptorCapableGpuAccessor.cs 5.5 KB

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