aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.OpenGL/Image/TextureCopy.cs
blob: 75c4d8708ed6e28e08a65172887f39fa9c91bc1e (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
using OpenTK.Graphics.OpenGL;
using Ryujinx.Common;
using Ryujinx.Graphics.GAL;
using System;

namespace Ryujinx.Graphics.OpenGL.Image
{
    class TextureCopy : IDisposable
    {
        private readonly OpenGLRenderer _renderer;

        private int _srcFramebuffer;
        private int _dstFramebuffer;

        private int _copyPboHandle;
        private int _copyPboSize;

        public TextureCopy(OpenGLRenderer renderer)
        {
            _renderer = renderer;
        }

        public void Copy(
            TextureView src,
            TextureView dst,
            Extents2D   srcRegion,
            Extents2D   dstRegion,
            bool        linearFilter,
            int         srcLayer = 0,
            int         dstLayer = 0,
            int         srcLevel = 0,
            int         dstLevel = 0)
        {
            int levels = Math.Min(src.Info.Levels - srcLevel, dst.Info.Levels - dstLevel);
            int layers = Math.Min(src.Info.GetLayers() - srcLayer, dst.Info.GetLayers() - dstLayer);

            Copy(src, dst, srcRegion, dstRegion, linearFilter, srcLayer, dstLayer, srcLevel, dstLevel, layers, levels);
        }

        public void Copy(
            TextureView src,
            TextureView dst,
            Extents2D   srcRegion,
            Extents2D   dstRegion,
            bool        linearFilter,
            int         srcLayer,
            int         dstLayer,
            int         srcLevel,
            int         dstLevel,
            int         layers,
            int         levels)
        {
            TextureView srcConverted = src.Format.IsBgr() != dst.Format.IsBgr() ? BgraSwap(src) : src;

            (int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();

            GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, GetSrcFramebufferLazy());
            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, GetDstFramebufferLazy());

            if (srcLevel != 0)
            {
                srcRegion = srcRegion.Reduce(srcLevel);
            }

            if (dstLevel != 0)
            {
                dstRegion = dstRegion.Reduce(dstLevel);
            }

            for (int level = 0; level < levels; level++)
            {
                for (int layer = 0; layer < layers; layer++)
                {
                    if ((srcLayer | dstLayer) != 0 || layers > 1)
                    {
                        Attach(FramebufferTarget.ReadFramebuffer, src.Format, srcConverted.Handle, srcLevel + level, srcLayer + layer);
                        Attach(FramebufferTarget.DrawFramebuffer, dst.Format, dst.Handle, dstLevel + level, dstLayer + layer);
                    }
                    else
                    {
                        Attach(FramebufferTarget.ReadFramebuffer, src.Format, srcConverted.Handle, srcLevel + level);
                        Attach(FramebufferTarget.DrawFramebuffer, dst.Format, dst.Handle, dstLevel + level);
                    }

                    ClearBufferMask mask = GetMask(src.Format);

                    if ((mask & (ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit)) != 0 || src.Format.IsInteger())
                    {
                        linearFilter = false;
                    }

                    BlitFramebufferFilter filter = linearFilter
                        ? BlitFramebufferFilter.Linear
                        : BlitFramebufferFilter.Nearest;

                    GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
                    GL.DrawBuffer(DrawBufferMode.ColorAttachment0);

                    GL.Disable(EnableCap.RasterizerDiscard);
                    GL.Disable(IndexedEnableCap.ScissorTest, 0);

                    GL.BlitFramebuffer(
                        srcRegion.X1,
                        srcRegion.Y1,
                        srcRegion.X2,
                        srcRegion.Y2,
                        dstRegion.X1,
                        dstRegion.Y1,
                        dstRegion.X2,
                        dstRegion.Y2,
                        mask,
                        filter);
                }

                if (level < levels - 1)
                {
                    srcRegion = srcRegion.Reduce(1);
                    dstRegion = dstRegion.Reduce(1);
                }
            }

            Attach(FramebufferTarget.ReadFramebuffer, src.Format, 0);
            Attach(FramebufferTarget.DrawFramebuffer, dst.Format, 0);

            GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);

            ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
            ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();

            if (srcConverted != src)
            {
                srcConverted.Dispose();
            }
        }

        public void CopyUnscaled(
            ITextureInfo src,
            ITextureInfo dst,
            int srcLayer,
            int dstLayer,
            int srcLevel,
            int dstLevel)
        {
            TextureCreateInfo srcInfo = src.Info;
            TextureCreateInfo dstInfo = dst.Info;

            int srcDepth = srcInfo.GetDepthOrLayers();
            int srcLevels = srcInfo.Levels;

            int dstDepth = dstInfo.GetDepthOrLayers();
            int dstLevels = dstInfo.Levels;

            if (dstInfo.Target == Target.Texture3D)
            {
                dstDepth = Math.Max(1, dstDepth >> dstLevel);
            }

            int depth = Math.Min(srcDepth, dstDepth);
            int levels = Math.Min(srcLevels, dstLevels);

            CopyUnscaled(src, dst, srcLayer, dstLayer, srcLevel, dstLevel, depth, levels);
        }

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

            int srcHandle = src.Handle;
            int dstHandle = dst.Handle;

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

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

            srcWidth = Math.Max(1, srcWidth >> srcLevel);
            srcHeight = Math.Max(1, srcHeight >> srcLevel);

            dstWidth = Math.Max(1, dstWidth >> dstLevel);
            dstHeight = Math.Max(1, dstHeight >> dstLevel);

            int blockWidth = 1;
            int blockHeight = 1;
            bool sizeInBlocks = false;

            // When copying from a compressed to a non-compressed format,
            // the non-compressed texture will have the size of the texture
            // in blocks (not in texels), so we must adjust that size to
            // match the size in texels of the compressed texture.
            if (!srcInfo.IsCompressed && dstInfo.IsCompressed)
            {
                srcWidth *= dstInfo.BlockWidth;
                srcHeight *= dstInfo.BlockHeight;
                blockWidth = dstInfo.BlockWidth;
                blockHeight = dstInfo.BlockHeight;

                sizeInBlocks = true;
            }
            else if (srcInfo.IsCompressed && !dstInfo.IsCompressed)
            {
                dstWidth *= srcInfo.BlockWidth;
                dstHeight *= srcInfo.BlockHeight;
                blockWidth = srcInfo.BlockWidth;
                blockHeight = srcInfo.BlockHeight;
            }

            int width = Math.Min(srcWidth, dstWidth);
            int height = Math.Min(srcHeight, dstHeight);

            for (int level = 0; level < levels; level++)
            {
                // Stop copy if we are already out of the levels range.
                if (level >= srcInfo.Levels || dstLevel + level >= dstInfo.Levels)
                {
                    break;
                }

                if ((width % blockWidth != 0 || height % blockHeight != 0) && src is TextureView srcView && dst is TextureView dstView)
                {
                    PboCopy(srcView, dstView, srcLayer, dstLayer, srcLevel + level, dstLevel + level, width, height);
                }
                else
                {
                    int copyWidth = sizeInBlocks ? BitUtils.DivRoundUp(width, blockWidth) : width;
                    int copyHeight = sizeInBlocks ? BitUtils.DivRoundUp(height, blockHeight) : height;

                    if (HwCapabilities.Vendor == HwCapabilities.GpuVendor.IntelWindows)
                    {
                        GL.CopyImageSubData(
                            src.Storage.Handle,
                            src.Storage.Info.Target.ConvertToImageTarget(),
                            src.FirstLevel + srcLevel + level,
                            0,
                            0,
                            src.FirstLayer + srcLayer,
                            dst.Storage.Handle,
                            dst.Storage.Info.Target.ConvertToImageTarget(),
                            dst.FirstLevel + dstLevel + level,
                            0,
                            0,
                            dst.FirstLayer + dstLayer,
                            copyWidth,
                            copyHeight,
                            depth);
                    }
                    else
                    {
                        GL.CopyImageSubData(
                            srcHandle,
                            srcInfo.Target.ConvertToImageTarget(),
                            srcLevel + level,
                            0,
                            0,
                            srcLayer,
                            dstHandle,
                            dstInfo.Target.ConvertToImageTarget(),
                            dstLevel + level,
                            0,
                            0,
                            dstLayer,
                            copyWidth,
                            copyHeight,
                            depth);
                    }
                }

                width = Math.Max(1, width >> 1);
                height = Math.Max(1, height >> 1);

                if (srcInfo.Target == Target.Texture3D)
                {
                    depth = Math.Max(1, depth >> 1);
                }
            }
        }

        private static FramebufferAttachment AttachmentForFormat(Format format)
        {
            if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
            {
                return FramebufferAttachment.DepthStencilAttachment;
            }
            else if (IsDepthOnly(format))
            {
                return FramebufferAttachment.DepthAttachment;
            }
            else if (format == Format.S8Uint)
            {
                return FramebufferAttachment.StencilAttachment;
            }
            else
            {
                return FramebufferAttachment.ColorAttachment0;
            }
        }

        private static void Attach(FramebufferTarget target, Format format, int handle, int level = 0)
        {
            FramebufferAttachment attachment = AttachmentForFormat(format);

            GL.FramebufferTexture(target, attachment, handle, level);
        }

        private static void Attach(FramebufferTarget target, Format format, int handle, int level, int layer)
        {
            FramebufferAttachment attachment = AttachmentForFormat(format);

            GL.FramebufferTextureLayer(target, attachment, handle, level, layer);
        }

        private static ClearBufferMask GetMask(Format format)
        {
            if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint || format == Format.S8UintD24Unorm)
            {
                return ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit;
            }
            else if (IsDepthOnly(format))
            {
                return ClearBufferMask.DepthBufferBit;
            }
            else if (format == Format.S8Uint)
            {
                return ClearBufferMask.StencilBufferBit;
            }
            else
            {
                return ClearBufferMask.ColorBufferBit;
            }
        }

        private static bool IsDepthOnly(Format format)
        {
            return format == Format.D16Unorm || format == Format.D32Float;
        }

        public TextureView BgraSwap(TextureView from)
        {
            TextureView to = (TextureView)_renderer.CreateTexture(from.Info, from.ScaleFactor);

            EnsurePbo(from);

            GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);

            from.WriteToPbo(0, forceBgra: true);

            GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
            GL.BindBuffer(BufferTarget.PixelUnpackBuffer, _copyPboHandle);

            to.ReadFromPbo(0, _copyPboSize);

            GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);

            return to;
        }

        private TextureView PboCopy(TextureView from, TextureView to, int srcLayer, int dstLayer, int srcLevel, int dstLevel, int width, int height)
        {
            int dstWidth = width;
            int dstHeight = height;

            // The size of the source texture.
            int unpackWidth = from.Width;
            int unpackHeight = from.Height;

            if (from.Info.IsCompressed != to.Info.IsCompressed)
            {
                if (from.Info.IsCompressed)
                {
                    // Dest size is in pixels, but should be in blocks
                    dstWidth = BitUtils.DivRoundUp(width, from.Info.BlockWidth);
                    dstHeight = BitUtils.DivRoundUp(height, from.Info.BlockHeight);

                    // When copying from a compressed texture, the source size must be taken in blocks for unpacking to the uncompressed block texture.
                    unpackWidth = BitUtils.DivRoundUp(from.Info.Width, from.Info.BlockWidth);
                    unpackHeight = BitUtils.DivRoundUp(from.Info.Height, from.Info.BlockHeight);
                }
                else
                {
                    // When copying to a compressed texture, the source size must be scaled by the block width for unpacking on the compressed target.
                    unpackWidth = from.Info.Width * to.Info.BlockWidth;
                    unpackHeight = from.Info.Height * to.Info.BlockHeight;
                }
            }

            EnsurePbo(from);

            GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);

            // The source texture is written out in full, then the destination is taken as a slice from the data using unpack params.
            // The offset points to the base at which the requested layer is at.

            int offset = from.WriteToPbo2D(0, srcLayer, srcLevel);

            // If the destination size is not an exact match for the source unpack parameters, we need to set them to slice the data correctly.

            bool slice = (unpackWidth != dstWidth || unpackHeight != dstHeight);

            if (slice)
            {
                // Set unpack parameters to take a slice of width/height:
                GL.PixelStore(PixelStoreParameter.UnpackRowLength, unpackWidth);
                GL.PixelStore(PixelStoreParameter.UnpackImageHeight, unpackHeight);

                if (to.Info.IsCompressed)
                {
                    GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockWidth, to.Info.BlockWidth);
                    GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockHeight, to.Info.BlockHeight);
                    GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockDepth, 1);
                    GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockSize, to.Info.BytesPerPixel);
                }
            }

            GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
            GL.BindBuffer(BufferTarget.PixelUnpackBuffer, _copyPboHandle);

            to.ReadFromPbo2D(offset, dstLayer, dstLevel, dstWidth, dstHeight);

            if (slice)
            {
                // Reset unpack parameters
                GL.PixelStore(PixelStoreParameter.UnpackRowLength, 0);
                GL.PixelStore(PixelStoreParameter.UnpackImageHeight, 0);

                if (to.Info.IsCompressed)
                {
                    GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockWidth, 0);
                    GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockHeight, 0);
                    GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockDepth, 0);
                    GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockSize, 0);
                }
            }

            GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);

            return to;
        }

        private void EnsurePbo(TextureView view)
        {
            int requiredSize = 0;

            for (int level = 0; level < view.Info.Levels; level++)
            {
                requiredSize += view.Info.GetMipSize(level);
            }

            if (_copyPboSize < requiredSize && _copyPboHandle != 0)
            {
                GL.DeleteBuffer(_copyPboHandle);

                _copyPboHandle = 0;
            }

            if (_copyPboHandle == 0)
            {
                _copyPboHandle = GL.GenBuffer();
                _copyPboSize = requiredSize;

                GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);
                GL.BufferData(BufferTarget.PixelPackBuffer, requiredSize, IntPtr.Zero, BufferUsageHint.DynamicCopy);
            }
        }

        private int GetSrcFramebufferLazy()
        {
            if (_srcFramebuffer == 0)
            {
                _srcFramebuffer = GL.GenFramebuffer();
            }

            return _srcFramebuffer;
        }

        private int GetDstFramebufferLazy()
        {
            if (_dstFramebuffer == 0)
            {
                _dstFramebuffer = GL.GenFramebuffer();
            }

            return _dstFramebuffer;
        }

        public void Dispose()
        {
            if (_srcFramebuffer != 0)
            {
                GL.DeleteFramebuffer(_srcFramebuffer);

                _srcFramebuffer = 0;
            }

            if (_dstFramebuffer != 0)
            {
                GL.DeleteFramebuffer(_dstFramebuffer);

                _dstFramebuffer = 0;
            }

            if (_copyPboHandle != 0)
            {
                GL.DeleteBuffer(_copyPboHandle);

                _copyPboHandle = 0;
            }
        }
    }
}