TextureHandle.cs 4.4 KB

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