aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Shader/SamplerType.cs
blob: a693495fa5f072b0105d5d4d81279b66c129e95d (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using Ryujinx.Graphics.Shader.Translation;
using System;

namespace Ryujinx.Graphics.Shader
{
    [Flags]
    public enum SamplerType
    {
        None = 0,
        Texture1D,
        TextureBuffer,
        Texture2D,
        Texture3D,
        TextureCube,

        Mask = 0xff,

        Array = 1 << 8,
        Multisample = 1 << 9,
        Shadow = 1 << 10,
    }

    static class SamplerTypeExtensions
    {
        public static int GetDimensions(this SamplerType type)
        {
            return (type & SamplerType.Mask) switch
            {
                SamplerType.Texture1D => 1,
                SamplerType.TextureBuffer => 1,
                SamplerType.Texture2D => 2,
                SamplerType.Texture3D => 3,
                SamplerType.TextureCube => 3,
                _ => throw new ArgumentException($"Invalid sampler type \"{type}\"."),
            };
        }

        public static string ToShortSamplerType(this SamplerType type)
        {
            string typeName = (type & SamplerType.Mask) switch
            {
                SamplerType.Texture1D => "1d",
                SamplerType.TextureBuffer => "b",
                SamplerType.Texture2D => "2d",
                SamplerType.Texture3D => "3d",
                SamplerType.TextureCube => "cube",
                _ => throw new ArgumentException($"Invalid sampler type \"{type}\"."),
            };

            if ((type & SamplerType.Multisample) != 0)
            {
                typeName += "ms";
            }

            if ((type & SamplerType.Array) != 0)
            {
                typeName += "a";
            }

            if ((type & SamplerType.Shadow) != 0)
            {
                typeName += "s";
            }

            return typeName;
        }

        public static string ToGlslSamplerType(this SamplerType type)
        {
            string typeName = (type & SamplerType.Mask) switch
            {
                SamplerType.None => "sampler",
                SamplerType.Texture1D => "sampler1D",
                SamplerType.TextureBuffer => "samplerBuffer",
                SamplerType.Texture2D => "sampler2D",
                SamplerType.Texture3D => "sampler3D",
                SamplerType.TextureCube => "samplerCube",
                _ => throw new ArgumentException($"Invalid sampler type \"{type}\"."),
            };

            if ((type & SamplerType.Multisample) != 0)
            {
                typeName += "MS";
            }

            if ((type & SamplerType.Array) != 0)
            {
                typeName += "Array";
            }

            if ((type & SamplerType.Shadow) != 0)
            {
                typeName += "Shadow";
            }

            return typeName;
        }

        public static string ToGlslTextureType(this SamplerType type)
        {
            string typeName = (type & SamplerType.Mask) switch
            {
                SamplerType.Texture1D => "texture1D",
                SamplerType.TextureBuffer => "textureBuffer",
                SamplerType.Texture2D => "texture2D",
                SamplerType.Texture3D => "texture3D",
                SamplerType.TextureCube => "textureCube",
                _ => throw new ArgumentException($"Invalid texture type \"{type}\"."),
            };

            if ((type & SamplerType.Multisample) != 0)
            {
                typeName += "MS";
            }

            if ((type & SamplerType.Array) != 0)
            {
                typeName += "Array";
            }

            return typeName;
        }

        public static string ToGlslImageType(this SamplerType type, AggregateType componentType)
        {
            string typeName = (type & SamplerType.Mask) switch
            {
                SamplerType.Texture1D => "image1D",
                SamplerType.TextureBuffer => "imageBuffer",
                SamplerType.Texture2D => "image2D",
                SamplerType.Texture3D => "image3D",
                SamplerType.TextureCube => "imageCube",
                _ => throw new ArgumentException($"Invalid sampler type \"{type}\"."),
            };

            if ((type & SamplerType.Multisample) != 0)
            {
                typeName += "MS";
            }

            if ((type & SamplerType.Array) != 0)
            {
                typeName += "Array";
            }

            switch (componentType)
            {
                case AggregateType.U32:
                    typeName = 'u' + typeName;
                    break;
                case AggregateType.S32:
                    typeName = 'i' + typeName;
                    break;
            }

            return typeName;
        }
    }
}