TextureHandle.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace Ryujinx.Graphics.Shader
  4. {
  5. public enum TextureHandleType
  6. {
  7. CombinedSampler = 0, // Must be 0.
  8. SeparateSamplerHandle = 1,
  9. SeparateSamplerId = 2,
  10. SeparateConstantSamplerHandle = 3
  11. }
  12. public static class TextureHandle
  13. {
  14. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  15. public static int PackSlots(int cbufSlot0, int cbufSlot1)
  16. {
  17. return cbufSlot0 | ((cbufSlot1 + 1) << 16);
  18. }
  19. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  20. public static (int, int) UnpackSlots(int slots, int defaultTextureBufferIndex)
  21. {
  22. int textureBufferIndex;
  23. int samplerBufferIndex;
  24. if (slots < 0)
  25. {
  26. textureBufferIndex = defaultTextureBufferIndex;
  27. samplerBufferIndex = textureBufferIndex;
  28. }
  29. else
  30. {
  31. uint high = (uint)slots >> 16;
  32. textureBufferIndex = (ushort)slots;
  33. samplerBufferIndex = high != 0 ? (int)high - 1 : textureBufferIndex;
  34. }
  35. return (textureBufferIndex, samplerBufferIndex);
  36. }
  37. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  38. public static int PackOffsets(int cbufOffset0, int cbufOffset1, TextureHandleType type)
  39. {
  40. return cbufOffset0 | (cbufOffset1 << 14) | ((int)type << 28);
  41. }
  42. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  43. public static (int, int, TextureHandleType) UnpackOffsets(int handle)
  44. {
  45. return (handle & 0x3fff, (handle >> 14) & 0x3fff, (TextureHandleType)((uint)handle >> 28));
  46. }
  47. /// <summary>
  48. /// Unpacks the texture ID from the real texture handle.
  49. /// </summary>
  50. /// <param name="packedId">The real texture handle</param>
  51. /// <returns>The texture ID</returns>
  52. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  53. public static int UnpackTextureId(int packedId)
  54. {
  55. return (packedId >> 0) & 0xfffff;
  56. }
  57. /// <summary>
  58. /// Unpacks the sampler ID from the real texture handle.
  59. /// </summary>
  60. /// <param name="packedId">The real texture handle</param>
  61. /// <returns>The sampler ID</returns>
  62. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  63. public static int UnpackSamplerId(int packedId)
  64. {
  65. return (packedId >> 20) & 0xfff;
  66. }
  67. /// <summary>
  68. /// Reads a packed texture and sampler ID (basically, the real texture handle)
  69. /// from a given texture/sampler constant buffer.
  70. /// </summary>
  71. /// <param name="wordOffset">A word offset of the handle on the buffer (the "fake" shader handle)</param>
  72. /// <param name="cachedTextureBuffer">The constant buffer to fetch texture IDs from</param>
  73. /// <param name="cachedSamplerBuffer">The constant buffer to fetch sampler IDs from</param>
  74. /// <returns>The packed texture and sampler ID (the real texture handle)</returns>
  75. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  76. public static int ReadPackedId(int wordOffset, ReadOnlySpan<int> cachedTextureBuffer, ReadOnlySpan<int> cachedSamplerBuffer)
  77. {
  78. (int textureWordOffset, int samplerWordOffset, TextureHandleType handleType) = UnpackOffsets(wordOffset);
  79. int handle = cachedTextureBuffer.Length != 0 ? cachedTextureBuffer[textureWordOffset] : 0;
  80. // The "wordOffset" (which is really the immediate value used on texture instructions on the shader)
  81. // is a 13-bit value. However, in order to also support separate samplers and textures (which uses
  82. // bindless textures on the shader), we extend it with another value on the higher 16 bits with
  83. // another offset for the sampler.
  84. // The shader translator has code to detect separate texture and sampler uses with a bindless texture,
  85. // turn that into a regular texture access and produce those special handles with values on the higher 16 bits.
  86. if (handleType != TextureHandleType.CombinedSampler)
  87. {
  88. int samplerHandle;
  89. if (handleType != TextureHandleType.SeparateConstantSamplerHandle)
  90. {
  91. samplerHandle = cachedSamplerBuffer.Length != 0 ? cachedSamplerBuffer[samplerWordOffset] : 0;
  92. }
  93. else
  94. {
  95. samplerHandle = samplerWordOffset;
  96. }
  97. if (handleType == TextureHandleType.SeparateSamplerId ||
  98. handleType == TextureHandleType.SeparateConstantSamplerHandle)
  99. {
  100. samplerHandle <<= 20;
  101. }
  102. handle |= samplerHandle;
  103. }
  104. return handle;
  105. }
  106. }
  107. }