aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Gpu/Image/Texture.cs
blob: 32db8688295cdf3883391688341bc55ee5bcc766 (plain)
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
using Ryujinx.Common;
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.GAL.Texture;
using Ryujinx.Graphics.Gpu.Memory;
using Ryujinx.Graphics.Texture;
using Ryujinx.Graphics.Texture.Astc;
using System;
using System.Collections.Generic;

namespace Ryujinx.Graphics.Gpu.Image
{
    class Texture : IRange<Texture>
    {
        private GpuContext _context;

        private TextureInfo _info;

        private SizeInfo _sizeInfo;

        public Format Format => _info.FormatInfo.Format;

        public TextureInfo Info => _info;

        private int _depth;
        private int _layers;
        private int _firstLayer;
        private int _firstLevel;

        private bool _hasData;

        private ITexture _arrayViewTexture;
        private Target   _arrayViewTarget;

        private Texture _viewStorage;

        private List<Texture> _views;

        public ITexture HostTexture { get; private set; }

        public LinkedListNode<Texture> CacheNode { get; set; }

        public bool Modified { get; set; }

        public ulong Address    => _info.Address;
        public ulong EndAddress => _info.Address + Size;

        public ulong Size => (ulong)_sizeInfo.TotalSize;

        private int _referenceCount;

        private int _sequenceNumber;

        private Texture(
            GpuContext  context,
            TextureInfo info,
            SizeInfo    sizeInfo,
            int         firstLayer,
            int         firstLevel)
        {
            InitializeTexture(context, info, sizeInfo);

            _firstLayer = firstLayer;
            _firstLevel = firstLevel;

            _hasData = true;
        }

        public Texture(GpuContext context, TextureInfo info, SizeInfo sizeInfo)
        {
            InitializeTexture(context, info, sizeInfo);

            TextureCreateInfo createInfo = TextureManager.GetCreateInfo(info, context.Capabilities);

            HostTexture = _context.Renderer.CreateTexture(createInfo);
        }

        private void InitializeTexture(GpuContext context, TextureInfo info, SizeInfo sizeInfo)
        {
            _context  = context;
            _sizeInfo = sizeInfo;

            SetInfo(info);

            _viewStorage = this;

            _views = new List<Texture>();
        }

        public Texture CreateView(TextureInfo info, SizeInfo sizeInfo, int firstLayer, int firstLevel)
        {
            Texture texture = new Texture(
                _context,
                info,
                sizeInfo,
                _firstLayer + firstLayer,
                _firstLevel + firstLevel);

            TextureCreateInfo createInfo = TextureManager.GetCreateInfo(info, _context.Capabilities);

            texture.HostTexture = HostTexture.CreateView(createInfo, firstLayer, firstLevel);

            _viewStorage.AddView(texture);

            return texture;
        }

        private void AddView(Texture texture)
        {
            _views.Add(texture);

            texture._viewStorage = this;
        }

        private void RemoveView(Texture texture)
        {
            _views.Remove(texture);

            texture._viewStorage = null;
        }

        public void ChangeSize(int width, int height, int depthOrLayers)
        {
            width  <<= _firstLevel;
            height <<= _firstLevel;

            if (_info.Target == Target.Texture3D)
            {
                depthOrLayers <<= _firstLevel;
            }
            else
            {
                depthOrLayers = _viewStorage._info.DepthOrLayers;
            }

            _viewStorage.RecreateStorageOrView(width, height, depthOrLayers);

            foreach (Texture view in _viewStorage._views)
            {
                int viewWidth  = Math.Max(1, width  >> view._firstLevel);
                int viewHeight = Math.Max(1, height >> view._firstLevel);

                int viewDepthOrLayers;

                if (view._info.Target == Target.Texture3D)
                {
                    viewDepthOrLayers = Math.Max(1, depthOrLayers >> view._firstLevel);
                }
                else
                {
                    viewDepthOrLayers = view._info.DepthOrLayers;
                }

                view.RecreateStorageOrView(viewWidth, viewHeight, viewDepthOrLayers);
            }
        }

        private void RecreateStorageOrView(int width, int height, int depthOrLayers)
        {
            SetInfo(new TextureInfo(
                _info.Address,
                width,
                height,
                depthOrLayers,
                _info.Levels,
                _info.SamplesInX,
                _info.SamplesInY,
                _info.Stride,
                _info.IsLinear,
                _info.GobBlocksInY,
                _info.GobBlocksInZ,
                _info.GobBlocksInTileX,
                _info.Target,
                _info.FormatInfo,
                _info.DepthStencilMode,
                _info.SwizzleR,
                _info.SwizzleG,
                _info.SwizzleB,
                _info.SwizzleA));

            TextureCreateInfo createInfo = TextureManager.GetCreateInfo(_info, _context.Capabilities);

            if (_viewStorage != this)
            {
                ReplaceStorage(_viewStorage.HostTexture.CreateView(createInfo, _firstLayer, _firstLevel));
            }
            else
            {
                ITexture newStorage = _context.Renderer.CreateTexture(createInfo);

                HostTexture.CopyTo(newStorage);

                ReplaceStorage(newStorage);
            }
        }

        public void SynchronizeMemory()
        {
            if (_sequenceNumber == _context.SequenceNumber && _hasData)
            {
                return;
            }

            _sequenceNumber = _context.SequenceNumber;

            bool modified = _context.PhysicalMemory.GetModifiedRanges(Address, Size).Length != 0;

            if (!modified && _hasData)
            {
                return;
            }

            ulong pageSize = (uint)_context.PhysicalMemory.GetPageSize();

            ulong pageMask = pageSize - 1;

            ulong rangeAddress = Address & ~pageMask;

            ulong rangeSize = (EndAddress - Address + pageMask) & ~pageMask;

            _context.Methods.InvalidateRange(rangeAddress, rangeSize);

            Span<byte> data = _context.PhysicalMemory.Read(Address, Size);

            if (_info.IsLinear)
            {
                data = LayoutConverter.ConvertLinearStridedToLinear(
                    _info.Width,
                    _info.Height,
                    _info.FormatInfo.BlockWidth,
                    _info.FormatInfo.BlockHeight,
                    _info.Stride,
                    _info.FormatInfo.BytesPerPixel,
                    data);
            }
            else
            {
                data = LayoutConverter.ConvertBlockLinearToLinear(
                    _info.Width,
                    _info.Height,
                    _depth,
                    _info.Levels,
                    _layers,
                    _info.FormatInfo.BlockWidth,
                    _info.FormatInfo.BlockHeight,
                    _info.FormatInfo.BytesPerPixel,
                    _info.GobBlocksInY,
                    _info.GobBlocksInZ,
                    _info.GobBlocksInTileX,
                    _sizeInfo,
                    data);
            }

            if (!_context.Capabilities.SupportsAstcCompression && _info.FormatInfo.Format.IsAstc())
            {
                int blockWidth  = _info.FormatInfo.BlockWidth;
                int blockHeight = _info.FormatInfo.BlockHeight;

                data = AstcDecoder.DecodeToRgba8(
                    data,
                    blockWidth,
                    blockHeight,
                    1,
                    _info.Width,
                    _info.Height,
                    _depth);
            }

            HostTexture.SetData(data);

            _hasData = true;
        }

        public void Flush()
        {
            byte[] data = HostTexture.GetData(0);

            _context.PhysicalMemory.Write(Address, data);
        }

        public bool IsPerfectMatch(TextureInfo info, TextureSearchFlags flags)
        {
            if (!FormatMatches(info, (flags & TextureSearchFlags.Strict) != 0))
            {
                return false;
            }

            if (!LayoutMatches(info))
            {
                return false;
            }

            if (!SizeMatches(info, (flags & TextureSearchFlags.Strict) == 0))
            {
                return false;
            }

            if ((flags & TextureSearchFlags.Sampler) != 0)
            {
                if (!SamplerParamsMatches(info))
                {
                    return false;
                }
            }

            if ((flags & TextureSearchFlags.IgnoreMs) != 0)
            {
                bool msTargetCompatible = _info.Target == Target.Texture2DMultisample &&
                                           info.Target == Target.Texture2D;

                if (!msTargetCompatible && !TargetAndSamplesCompatible(info))
                {
                    return false;
                }
            }
            else if (!TargetAndSamplesCompatible(info))
            {
                return false;
            }

            return _info.Address == info.Address && _info.Levels == info.Levels;
        }

        private bool FormatMatches(TextureInfo info, bool strict)
        {
            // D32F and R32F texture have the same representation internally,
            // however the R32F format is used to sample from depth textures.
            if (_info.FormatInfo.Format == Format.D32Float &&
                 info.FormatInfo.Format == Format.R32Float && !strict)
            {
                return true;
            }

            if (_info.FormatInfo.Format == Format.R8G8B8A8Srgb &&
                 info.FormatInfo.Format == Format.R8G8B8A8Unorm && !strict)
            {
                return true;
            }

            if (_info.FormatInfo.Format == Format.R8G8B8A8Unorm &&
                 info.FormatInfo.Format == Format.R8G8B8A8Srgb && !strict)
            {
                return true;
            }

            return _info.FormatInfo.Format == info.FormatInfo.Format;
        }

        private bool LayoutMatches(TextureInfo info)
        {
            if (_info.IsLinear != info.IsLinear)
            {
                return false;
            }

            // For linear textures, gob block sizes are ignored.
            // For block linear textures, the stride is ignored.
            if (info.IsLinear)
            {
                return _info.Stride == info.Stride;
            }
            else
            {
                return _info.GobBlocksInY == info.GobBlocksInY &&
                       _info.GobBlocksInZ == info.GobBlocksInZ;
            }
        }

        public bool SizeMatches(TextureInfo info)
        {
            return SizeMatches(info, alignSizes: false);
        }

        public bool SizeMatches(TextureInfo info, int level)
        {
            return Math.Max(1, _info.Width      >> level) == info.Width  &&
                   Math.Max(1, _info.Height     >> level) == info.Height &&
                   Math.Max(1, _info.GetDepth() >> level) == info.GetDepth();
        }

        private bool SizeMatches(TextureInfo info, bool alignSizes)
        {
            if (_info.GetLayers() != info.GetLayers())
            {
                return false;
            }

            if (alignSizes)
            {
                Size size0 = GetAlignedSize(_info);
                Size size1 = GetAlignedSize(info);

                return size0.Width  == size1.Width  &&
                       size0.Height == size1.Height &&
                       size0.Depth  == size1.Depth;
            }
            else
            {
                return _info.Width      == info.Width  &&
                       _info.Height     == info.Height &&
                       _info.GetDepth() == info.GetDepth();
            }
        }

        private bool SamplerParamsMatches(TextureInfo info)
        {
            return _info.DepthStencilMode == info.DepthStencilMode &&
                   _info.SwizzleR         == info.SwizzleR         &&
                   _info.SwizzleG         == info.SwizzleG         &&
                   _info.SwizzleB         == info.SwizzleB         &&
                   _info.SwizzleA         == info.SwizzleA;
        }

        private bool TargetAndSamplesCompatible(TextureInfo info)
        {
            return _info.Target     == info.Target     &&
                   _info.SamplesInX == info.SamplesInX &&
                   _info.SamplesInY == info.SamplesInY;
        }

        public bool IsViewCompatible(TextureInfo info, ulong size, out int firstLayer, out int firstLevel)
        {
            // Out of range.
            if (info.Address < Address || info.Address + size > EndAddress)
            {
                firstLayer = 0;
                firstLevel = 0;

                return false;
            }

            int offset = (int)(info.Address - Address);

            if (!_sizeInfo.FindView(offset, (int)size, out firstLayer, out firstLevel))
            {
                return false;
            }

            if (!ViewLayoutCompatible(info, firstLevel))
            {
                return false;
            }

            if (!ViewFormatCompatible(info))
            {
                return false;
            }

            if (!ViewSizeMatches(info, firstLevel))
            {
                return false;
            }

            if (!ViewTargetCompatible(info))
            {
                return false;
            }

            return _info.SamplesInX == info.SamplesInX &&
                   _info.SamplesInY == info.SamplesInY;
        }

        private bool ViewLayoutCompatible(TextureInfo info, int level)
        {
            if (_info.IsLinear != info.IsLinear)
            {
                return false;
            }

            // For linear textures, gob block sizes are ignored.
            // For block linear textures, the stride is ignored.
            if (info.IsLinear)
            {
                int width = Math.Max(1, _info.Width >> level);

                int stride = width * _info.FormatInfo.BytesPerPixel;

                stride = BitUtils.AlignUp(stride, 32);

                return stride == info.Stride;
            }
            else
            {
                int height = Math.Max(1, _info.Height     >> level);
                int depth  = Math.Max(1, _info.GetDepth() >> level);

                (int gobBlocksInY, int gobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
                    height,
                    depth,
                    _info.FormatInfo.BlockHeight,
                    _info.GobBlocksInY,
                    _info.GobBlocksInZ);

                return gobBlocksInY == info.GobBlocksInY &&
                       gobBlocksInZ == info.GobBlocksInZ;
            }
        }

        private bool ViewFormatCompatible(TextureInfo info)
        {
            return TextureCompatibility.FormatCompatible(_info.FormatInfo, info.FormatInfo);
        }

        private bool ViewSizeMatches(TextureInfo info, int level)
        {
            Size size = GetAlignedSize(_info, level);

            Size otherSize = GetAlignedSize(info);

            return size.Width  == otherSize.Width  &&
                   size.Height == otherSize.Height &&
                   size.Depth  == otherSize.Depth;
        }

        private bool ViewTargetCompatible(TextureInfo info)
        {
            switch (_info.Target)
            {
                case Target.Texture1D:
                case Target.Texture1DArray:
                    return info.Target == Target.Texture1D ||
                           info.Target == Target.Texture1DArray;

                case Target.Texture2D:
                    return info.Target == Target.Texture2D ||
                           info.Target == Target.Texture2DArray;

                case Target.Texture2DArray:
                case Target.Cubemap:
                case Target.CubemapArray:
                    return info.Target == Target.Texture2D      ||
                           info.Target == Target.Texture2DArray ||
                           info.Target == Target.Cubemap        ||
                           info.Target == Target.CubemapArray;

                case Target.Texture2DMultisample:
                case Target.Texture2DMultisampleArray:
                    return info.Target == Target.Texture2DMultisample ||
                           info.Target == Target.Texture2DMultisampleArray;

                case Target.Texture3D:
                    return info.Target == Target.Texture3D;
            }

            return false;
        }

        private static Size GetAlignedSize(TextureInfo info, int level = 0)
        {
            int width  = Math.Max(1, info.Width  >> level);
            int height = Math.Max(1, info.Height >> level);

            if (info.IsLinear)
            {
                return SizeCalculator.GetLinearAlignedSize(
                    width,
                    height,
                    info.FormatInfo.BlockWidth,
                    info.FormatInfo.BlockHeight,
                    info.FormatInfo.BytesPerPixel);
            }
            else
            {
                int depth = Math.Max(1, info.GetDepth() >> level);

                (int gobBlocksInY, int gobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
                    height,
                    depth,
                    info.FormatInfo.BlockHeight,
                    info.GobBlocksInY,
                    info.GobBlocksInZ);

                return SizeCalculator.GetBlockLinearAlignedSize(
                    width,
                    height,
                    depth,
                    info.FormatInfo.BlockWidth,
                    info.FormatInfo.BlockHeight,
                    info.FormatInfo.BytesPerPixel,
                    gobBlocksInY,
                    gobBlocksInZ,
                    info.GobBlocksInTileX);
            }
        }

        public ITexture GetTargetTexture(Target target)
        {
            if (target == _info.Target)
            {
                return HostTexture;
            }

            if (_arrayViewTexture == null && IsSameDimensionsTarget(target))
            {
                TextureCreateInfo createInfo = new TextureCreateInfo(
                    _info.Width,
                    _info.Height,
                    target == Target.CubemapArray ? 6 : 1,
                    _info.Levels,
                    _info.Samples,
                    _info.FormatInfo.BlockWidth,
                    _info.FormatInfo.BlockHeight,
                    _info.FormatInfo.BytesPerPixel,
                    _info.FormatInfo.Format,
                    _info.DepthStencilMode,
                    target,
                    _info.SwizzleR,
                    _info.SwizzleG,
                    _info.SwizzleB,
                    _info.SwizzleA);

                ITexture viewTexture = HostTexture.CreateView(createInfo, 0, 0);

                _arrayViewTexture = viewTexture;
                _arrayViewTarget  = target;

                return viewTexture;
            }
            else if (_arrayViewTarget == target)
            {
                return _arrayViewTexture;
            }

            return null;
        }

        private bool IsSameDimensionsTarget(Target target)
        {
            switch (_info.Target)
            {
                case Target.Texture1D:
                case Target.Texture1DArray:
                    return target == Target.Texture1D ||
                           target == Target.Texture1DArray;

                case Target.Texture2D:
                case Target.Texture2DArray:
                    return target == Target.Texture2D ||
                           target == Target.Texture2DArray;

                case Target.Cubemap:
                case Target.CubemapArray:
                    return target == Target.Cubemap ||
                           target == Target.CubemapArray;

                case Target.Texture2DMultisample:
                case Target.Texture2DMultisampleArray:
                    return target == Target.Texture2DMultisample ||
                           target == Target.Texture2DMultisampleArray;

                case Target.Texture3D:
                    return target == Target.Texture3D;
            }

            return false;
        }

        public void ReplaceView(Texture parent, TextureInfo info, ITexture hostTexture)
        {
            ReplaceStorage(hostTexture);

            parent._viewStorage.AddView(this);

            SetInfo(info);
        }

        private void SetInfo(TextureInfo info)
        {
            _info = info;

            _depth  = info.GetDepth();
            _layers = info.GetLayers();
        }

        private void ReplaceStorage(ITexture hostTexture)
        {
            DisposeTextures();

            HostTexture = hostTexture;
        }

        public bool OverlapsWith(ulong address, ulong size)
        {
            return Address < address + size && address < EndAddress;
        }

        public void Invalidate()
        {
            // _hasData = false;
        }

        public void IncrementReferenceCount()
        {
            _referenceCount++;
        }

        public void DecrementReferenceCount()
        {
            if (--_referenceCount == 0)
            {
                if (_viewStorage != this)
                {
                    _viewStorage.RemoveView(this);
                }

                _context.Methods.TextureManager.RemoveTextureFromCache(this);

                DisposeTextures();
            }
        }

        private void DisposeTextures()
        {
            HostTexture.Dispose();

            _arrayViewTexture?.Dispose();
            _arrayViewTexture = null;
        }
    }
}