aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Shader/TextureHandle.cs
blob: 3aaceac48067d11d48ab6219636146689b6f5b6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Runtime.CompilerServices;

namespace Ryujinx.Graphics.Shader
{
    public enum TextureHandleType
    {
        CombinedSampler = 0, // Must be 0.
        SeparateSamplerHandle = 1,
        SeparateSamplerId = 2,
        SeparateConstantSamplerHandle = 3,
        Direct = 4,
    }

    public static class TextureHandle
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int PackSlots(int cbufSlot0, int cbufSlot1)
        {
            return cbufSlot0 | ((cbufSlot1 + 1) << 16);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static (int, int) UnpackSlots(int slots, int defaultTextureBufferIndex)
        {
            int textureBufferIndex;
            int samplerBufferIndex;

            if (slots < 0)
            {
                textureBufferIndex = defaultTextureBufferIndex;
                samplerBufferIndex = textureBufferIndex;
            }
            else
            {
                uint high = (uint)slots >> 16;

                textureBufferIndex = (ushort)slots;
                samplerBufferIndex = high != 0 ? (int)high - 1 : textureBufferIndex;
            }

            return (textureBufferIndex, samplerBufferIndex);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int PackOffsets(int cbufOffset0, int cbufOffset1, TextureHandleType type)
        {
            return cbufOffset0 | (cbufOffset1 << 14) | ((int)type << 28);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static (int, int, TextureHandleType) UnpackOffsets(int handle)
        {
            return (handle & 0x3fff, (handle >> 14) & 0x3fff, (TextureHandleType)((uint)handle >> 28));
        }

        /// <summary>
        /// Unpacks the texture ID from the real texture handle.
        /// </summary>
        /// <param name="packedId">The real texture handle</param>
        /// <returns>The texture ID</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int UnpackTextureId(int packedId)
        {
            return packedId & 0xfffff;
        }

        /// <summary>
        /// Unpacks the sampler ID from the real texture handle.
        /// </summary>
        /// <param name="packedId">The real texture handle</param>
        /// <returns>The sampler ID</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int UnpackSamplerId(int packedId)
        {
            return (packedId >> 20) & 0xfff;
        }

        /// <summary>
        /// Reads a packed texture and sampler ID (basically, the real texture handle)
        /// from a given texture/sampler constant buffer.
        /// </summary>
        /// <param name="wordOffset">A word offset of the handle on the buffer (the "fake" shader handle)</param>
        /// <param name="cachedTextureBuffer">The constant buffer to fetch texture IDs from</param>
        /// <param name="cachedSamplerBuffer">The constant buffer to fetch sampler IDs from</param>
        /// <returns>The packed texture and sampler ID (the real texture handle)</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int ReadPackedId(int wordOffset, ReadOnlySpan<int> cachedTextureBuffer, ReadOnlySpan<int> cachedSamplerBuffer)
        {
            (int textureWordOffset, int samplerWordOffset, TextureHandleType handleType) = UnpackOffsets(wordOffset);

            int handle = textureWordOffset < cachedTextureBuffer.Length ? cachedTextureBuffer[textureWordOffset] : 0;

            // The "wordOffset" (which is really the immediate value used on texture instructions on the shader)
            // is a 13-bit value. However, in order to also support separate samplers and textures (which uses
            // bindless textures on the shader), we extend it with another value on the higher 16 bits with
            // another offset for the sampler.
            // The shader translator has code to detect separate texture and sampler uses with a bindless texture,
            // turn that into a regular texture access and produce those special handles with values on the higher 16 bits.
            if (handleType != TextureHandleType.CombinedSampler)
            {
                int samplerHandle;

                if (handleType != TextureHandleType.SeparateConstantSamplerHandle)
                {
                    samplerHandle = samplerWordOffset < cachedSamplerBuffer.Length ? cachedSamplerBuffer[samplerWordOffset] : 0;
                }
                else
                {
                    samplerHandle = samplerWordOffset;
                }

                if (handleType == TextureHandleType.SeparateSamplerId ||
                    handleType == TextureHandleType.SeparateConstantSamplerHandle)
                {
                    samplerHandle <<= 20;
                }

                handle |= samplerHandle;
            }

            return handle;
        }
    }
}