aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyMS.cs
blob: 9963dc661f9dceea70abbd0eadc41b651c71cb0d (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using OpenTK.Graphics.OpenGL;
using Ryujinx.Graphics.GAL;
using System;
using System.Numerics;

namespace Ryujinx.Graphics.OpenGL.Image
{
    class TextureCopyMS
    {
        private const string ComputeShaderMSToNonMS = @"#version 450 core

layout (binding = 0, $FORMAT$) uniform uimage2DMS imgIn;
layout (binding = 1, $FORMAT$) uniform uimage2D imgOut;

layout (local_size_x = 32, local_size_y = 32, local_size_z = 1) in;

void main()
{
    uvec2 coords = gl_GlobalInvocationID.xy;
    ivec2 imageSz = imageSize(imgOut);
    if (int(coords.x) >= imageSz.x || int(coords.y) >= imageSz.y)
    {
        return;
    }
    int inSamples = imageSamples(imgIn);
    int samplesInXLog2 = 0;
    int samplesInYLog2 = 0;
    switch (inSamples)
    {
        case 2:
            samplesInXLog2 = 1;
            break;
        case 4:
            samplesInXLog2 = 1;
            samplesInYLog2 = 1;
            break;
        case 8:
            samplesInXLog2 = 2;
            samplesInYLog2 = 1;
            break;
        case 16:
            samplesInXLog2 = 2;
            samplesInYLog2 = 2;
            break;
    }
    int samplesInX = 1 << samplesInXLog2;
    int samplesInY = 1 << samplesInYLog2;
    int sampleIdx = (int(coords.x) & (samplesInX - 1)) | ((int(coords.y) & (samplesInY - 1)) << samplesInXLog2);
    uvec4 value = imageLoad(imgIn, ivec2(int(coords.x) >> samplesInXLog2, int(coords.y) >> samplesInYLog2), sampleIdx);
    imageStore(imgOut, ivec2(coords), value);
}";

        private const string ComputeShaderNonMSToMS = @"#version 450 core

layout (binding = 0, $FORMAT$) uniform uimage2D imgIn;
layout (binding = 1, $FORMAT$) uniform uimage2DMS imgOut;

layout (local_size_x = 32, local_size_y = 32, local_size_z = 1) in;

void main()
{
    uvec2 coords = gl_GlobalInvocationID.xy;
    ivec2 imageSz = imageSize(imgIn);
    if (int(coords.x) >= imageSz.x || int(coords.y) >= imageSz.y)
    {
        return;
    }
    int outSamples = imageSamples(imgOut);
    int samplesInXLog2 = 0;
    int samplesInYLog2 = 0;
    switch (outSamples)
    {
        case 2:
            samplesInXLog2 = 1;
            break;
        case 4:
            samplesInXLog2 = 1;
            samplesInYLog2 = 1;
            break;
        case 8:
            samplesInXLog2 = 2;
            samplesInYLog2 = 1;
            break;
        case 16:
            samplesInXLog2 = 2;
            samplesInYLog2 = 2;
            break;
    }
    int samplesInX = 1 << samplesInXLog2;
    int samplesInY = 1 << samplesInYLog2;
    int sampleIdx = (int(coords.x) & (samplesInX - 1)) | ((int(coords.y) & (samplesInY - 1)) << samplesInXLog2);
    uvec4 value = imageLoad(imgIn, ivec2(coords));
    imageStore(imgOut, ivec2(int(coords.x) >> samplesInXLog2, int(coords.y) >> samplesInYLog2), sampleIdx, value);
}";

        private readonly OpenGLRenderer _renderer;
        private int[] _msToNonMSProgramHandles;
        private int[] _nonMSToMSProgramHandles;

        public TextureCopyMS(OpenGLRenderer renderer)
        {
            _renderer = renderer;
            _msToNonMSProgramHandles = new int[5];
            _nonMSToMSProgramHandles = new int[5];
        }

        public void CopyMSToNonMS(ITextureInfo src, ITextureInfo dst, int srcLayer, int dstLayer, int depth)
        {
            TextureCreateInfo srcInfo = src.Info;
            TextureCreateInfo dstInfo = dst.Info;

            int srcHandle = CreateViewIfNeeded(src);
            int dstHandle = CreateViewIfNeeded(dst);

            int dstWidth = dstInfo.Width;
            int dstHeight = dstInfo.Height;

            GL.UseProgram(GetMSToNonMSShader(srcInfo.BytesPerPixel));

            for (int z = 0; z < depth; z++)
            {
                GL.BindImageTexture(0, srcHandle, 0, false, srcLayer + z, TextureAccess.ReadOnly, GetFormat(srcInfo.BytesPerPixel));
                GL.BindImageTexture(1, dstHandle, 0, false, dstLayer + z, TextureAccess.WriteOnly, GetFormat(dstInfo.BytesPerPixel));

                GL.DispatchCompute((dstWidth + 31) / 32, (dstHeight + 31) / 32, 1);
            }

            Pipeline pipeline = (Pipeline)_renderer.Pipeline;

            pipeline.RestoreProgram();
            pipeline.RestoreImages1And2();

            DestroyViewIfNeeded(src, srcHandle);
            DestroyViewIfNeeded(dst, dstHandle);
        }

        public void CopyNonMSToMS(ITextureInfo src, ITextureInfo dst, int srcLayer, int dstLayer, int depth)
        {
            TextureCreateInfo srcInfo = src.Info;
            TextureCreateInfo dstInfo = dst.Info;

            int srcHandle = CreateViewIfNeeded(src);
            int dstHandle = CreateViewIfNeeded(dst);

            int srcWidth = srcInfo.Width;
            int srcHeight = srcInfo.Height;

            GL.UseProgram(GetNonMSToMSShader(srcInfo.BytesPerPixel));

            for (int z = 0; z < depth; z++)
            {
                GL.BindImageTexture(0, srcHandle, 0, false, srcLayer + z, TextureAccess.ReadOnly, GetFormat(srcInfo.BytesPerPixel));
                GL.BindImageTexture(1, dstHandle, 0, false, dstLayer + z, TextureAccess.WriteOnly, GetFormat(dstInfo.BytesPerPixel));

                GL.DispatchCompute((srcWidth + 31) / 32, (srcHeight + 31) / 32, 1);
            }

            Pipeline pipeline = (Pipeline)_renderer.Pipeline;

            pipeline.RestoreProgram();
            pipeline.RestoreImages1And2();

            DestroyViewIfNeeded(src, srcHandle);
            DestroyViewIfNeeded(dst, dstHandle);
        }

        private static SizedInternalFormat GetFormat(int bytesPerPixel)
        {
            return bytesPerPixel switch
            {
                1 => SizedInternalFormat.R8ui,
                2 => SizedInternalFormat.R16ui,
                4 => SizedInternalFormat.R32ui,
                8 => SizedInternalFormat.Rg32ui,
                16 => SizedInternalFormat.Rgba32ui,
                _ => throw new ArgumentException($"Invalid bytes per pixel {bytesPerPixel}.")
            };
        }

        private static int CreateViewIfNeeded(ITextureInfo texture)
        {
            // Binding sRGB textures as images doesn't work on NVIDIA,
            // we need to create and bind a RGBA view for it to work.
            if (texture.Info.Format == Format.R8G8B8A8Srgb)
            {
                int handle = GL.GenTexture();

                GL.TextureView(
                    handle,
                    texture.Info.Target.Convert(),
                    texture.Storage.Handle,
                    PixelInternalFormat.Rgba8,
                    texture.FirstLevel,
                    1,
                    texture.FirstLayer,
                    texture.Info.GetLayers());

                return handle;
            }

            return texture.Handle;
        }

        private static void DestroyViewIfNeeded(ITextureInfo info, int handle)
        {
            if (info.Handle != handle)
            {
                GL.DeleteTexture(handle);
            }
        }

        private int GetMSToNonMSShader(int bytesPerPixel)
        {
            return GetShader(ComputeShaderMSToNonMS, _msToNonMSProgramHandles, bytesPerPixel);
        }

        private int GetNonMSToMSShader(int bytesPerPixel)
        {
            return GetShader(ComputeShaderNonMSToMS, _nonMSToMSProgramHandles, bytesPerPixel);
        }

        private int GetShader(string code, int[] programHandles, int bytesPerPixel)
        {
            int index = BitOperations.Log2((uint)bytesPerPixel);

            if (programHandles[index] == 0)
            {
                int csHandle = GL.CreateShader(ShaderType.ComputeShader);

                string format = new[] { "r8ui", "r16ui", "r32ui", "rg32ui", "rgba32ui" }[index];

                GL.ShaderSource(csHandle, code.Replace("$FORMAT$", format));
                GL.CompileShader(csHandle);

                int programHandle = GL.CreateProgram();

                GL.AttachShader(programHandle, csHandle);
                GL.LinkProgram(programHandle);
                GL.DetachShader(programHandle, csHandle);
                GL.DeleteShader(csHandle);

                GL.GetProgram(programHandle, GetProgramParameterName.LinkStatus, out int status);

                if (status == 0)
                {
                    throw new Exception(GL.GetProgramInfoLog(programHandle));
                }

                programHandles[index] = programHandle;
            }

            return programHandles[index];
        }

        public void Dispose()
        {
            for (int i = 0; i < _msToNonMSProgramHandles.Length; i++)
            {
                if (_msToNonMSProgramHandles[i] != 0)
                {
                    GL.DeleteProgram(_msToNonMSProgramHandles[i]);
                    _msToNonMSProgramHandles[i] = 0;
                }
            }

            for (int i = 0; i < _nonMSToMSProgramHandles.Length; i++)
            {
                if (_nonMSToMSProgramHandles[i] != 0)
                {
                    GL.DeleteProgram(_nonMSToMSProgramHandles[i]);
                    _nonMSToMSProgramHandles[i] = 0;
                }
            }
        }
    }
}