aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Gpu/Engine/Threed/ComputeDraw/VtgAsComputeContext.cs
blob: 6de50fb2e7a9b986f845d694a0d8a1386ee190a5 (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
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
using Ryujinx.Common;
using Ryujinx.Graphics.GAL;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Ryujinx.Graphics.Gpu.Engine.Threed.ComputeDraw
{
    /// <summary>
    /// Vertex, tessellation and geometry as compute shader context.
    /// </summary>
    class VtgAsComputeContext : IDisposable
    {
        private const int DummyBufferSize = 16;

        private readonly GpuContext _context;

        /// <summary>
        /// Cache of buffer textures used for vertex and index buffers.
        /// </summary>
        private class BufferTextureCache : IDisposable
        {
            private readonly Dictionary<Format, ITexture> _cache;

            /// <summary>
            /// Creates a new instance of the buffer texture cache.
            /// </summary>
            public BufferTextureCache()
            {
                _cache = new();
            }

            /// <summary>
            /// Gets a cached or creates and caches a buffer texture with the specified format.
            /// </summary>
            /// <param name="renderer">Renderer where the texture will be used</param>
            /// <param name="format">Format of the buffer texture</param>
            /// <returns>Buffer texture</returns>
            public ITexture Get(IRenderer renderer, Format format)
            {
                if (!_cache.TryGetValue(format, out ITexture bufferTexture))
                {
                    bufferTexture = renderer.CreateTexture(new TextureCreateInfo(
                        1,
                        1,
                        1,
                        1,
                        1,
                        1,
                        1,
                        1,
                        format,
                        DepthStencilMode.Depth,
                        Target.TextureBuffer,
                        SwizzleComponent.Red,
                        SwizzleComponent.Green,
                        SwizzleComponent.Blue,
                        SwizzleComponent.Alpha));

                    _cache.Add(format, bufferTexture);
                }

                return bufferTexture;
            }

            protected virtual void Dispose(bool disposing)
            {
                if (disposing)
                {
                    foreach (var texture in _cache.Values)
                    {
                        texture.Release();
                    }

                    _cache.Clear();
                }
            }

            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
        }

        /// <summary>
        /// Buffer state.
        /// </summary>
        private struct Buffer
        {
            /// <summary>
            /// Buffer handle.
            /// </summary>
            public BufferHandle Handle;

            /// <summary>
            /// Current free buffer offset.
            /// </summary>
            public int Offset;

            /// <summary>
            /// Total buffer size in bytes.
            /// </summary>
            public int Size;
        }

        /// <summary>
        /// Index buffer state.
        /// </summary>
        private readonly struct IndexBuffer
        {
            /// <summary>
            /// Buffer handle.
            /// </summary>
            public BufferHandle Handle { get; }

            /// <summary>
            /// Index count.
            /// </summary>
            public int Count { get; }

            /// <summary>
            /// Size in bytes.
            /// </summary>
            public int Size { get; }

            /// <summary>
            /// Creates a new index buffer state.
            /// </summary>
            /// <param name="handle">Buffer handle</param>
            /// <param name="count">Index count</param>
            /// <param name="size">Size in bytes</param>
            public IndexBuffer(BufferHandle handle, int count, int size)
            {
                Handle = handle;
                Count = count;
                Size = size;
            }

            /// <summary>
            /// Creates a full range starting from the beggining of the buffer.
            /// </summary>
            /// <returns>Range</returns>
            public readonly BufferRange ToRange()
            {
                return new BufferRange(Handle, 0, Size);
            }

            /// <summary>
            /// Creates a range starting from the beggining of the buffer, with the specified size.
            /// </summary>
            /// <param name="size">Size in bytes of the range</param>
            /// <returns>Range</returns>
            public readonly BufferRange ToRange(int size)
            {
                return new BufferRange(Handle, 0, size);
            }
        }

        private readonly BufferTextureCache[] _bufferTextures;
        private BufferHandle _dummyBuffer;
        private Buffer _vertexDataBuffer;
        private Buffer _geometryVertexDataBuffer;
        private Buffer _geometryIndexDataBuffer;
        private BufferHandle _sequentialIndexBuffer;
        private int _sequentialIndexBufferCount;

        private readonly Dictionary<PrimitiveTopology, IndexBuffer> _topologyRemapBuffers;

        /// <summary>
        /// Vertex information buffer updater.
        /// </summary>
        public VertexInfoBufferUpdater VertexInfoBufferUpdater { get; }

        /// <summary>
        /// Creates a new instance of the vertex, tessellation and geometry as compute shader context.
        /// </summary>
        /// <param name="context"></param>
        public VtgAsComputeContext(GpuContext context)
        {
            _context = context;
            _bufferTextures = new BufferTextureCache[Constants.TotalVertexBuffers + 2];
            _topologyRemapBuffers = new();
            VertexInfoBufferUpdater = new(context.Renderer);
        }

        /// <summary>
        /// Gets the number of complete primitives that can be formed with a given vertex count, for a given topology.
        /// </summary>
        /// <param name="primitiveType">Topology</param>
        /// <param name="count">Vertex count</param>
        /// <returns>Total of complete primitives</returns>
        public static int GetPrimitivesCount(PrimitiveTopology primitiveType, int count)
        {
            return primitiveType switch
            {
                PrimitiveTopology.Lines => count / 2,
                PrimitiveTopology.LinesAdjacency => count / 4,
                PrimitiveTopology.LineLoop => count > 1 ? count : 0,
                PrimitiveTopology.LineStrip => Math.Max(count - 1, 0),
                PrimitiveTopology.LineStripAdjacency => Math.Max(count - 3, 0),
                PrimitiveTopology.Triangles => count / 3,
                PrimitiveTopology.TrianglesAdjacency => count / 6,
                PrimitiveTopology.TriangleStrip or
                PrimitiveTopology.TriangleFan or
                PrimitiveTopology.Polygon => Math.Max(count - 2, 0),
                PrimitiveTopology.TriangleStripAdjacency => Math.Max(count - 2, 0) / 2,
                PrimitiveTopology.Quads => (count / 4) * 2, // In triangles.
                PrimitiveTopology.QuadStrip => Math.Max((count - 2) / 2, 0) * 2, // In triangles.
                _ => count,
            };
        }

        /// <summary>
        /// Gets the total of vertices that a single primitive has, for the specified topology.
        /// </summary>
        /// <param name="primitiveType">Topology</param>
        /// <returns>Vertex count</returns>
        private static int GetVerticesPerPrimitive(PrimitiveTopology primitiveType)
        {
            return primitiveType switch
            {
                PrimitiveTopology.Lines or
                PrimitiveTopology.LineLoop or
                PrimitiveTopology.LineStrip => 2,
                PrimitiveTopology.LinesAdjacency or
                PrimitiveTopology.LineStripAdjacency => 4,
                PrimitiveTopology.Triangles or
                PrimitiveTopology.TriangleStrip or
                PrimitiveTopology.TriangleFan or
                PrimitiveTopology.Polygon => 3,
                PrimitiveTopology.TrianglesAdjacency or
                PrimitiveTopology.TriangleStripAdjacency => 6,
                PrimitiveTopology.Quads or
                PrimitiveTopology.QuadStrip => 3, // 2 triangles.
                _ => 1,
            };
        }

        /// <summary>
        /// Gets a cached or creates a new buffer that can be used to map linear indices to ones
        /// of a specified topology, and build complete primitives.
        /// </summary>
        /// <param name="topology">Topology</param>
        /// <param name="count">Number of input vertices that needs to be mapped using that buffer</param>
        /// <returns>Remap buffer range</returns>
        public BufferRange GetOrCreateTopologyRemapBuffer(PrimitiveTopology topology, int count)
        {
            if (!_topologyRemapBuffers.TryGetValue(topology, out IndexBuffer buffer) || buffer.Count < count)
            {
                if (buffer.Handle != BufferHandle.Null)
                {
                    _context.Renderer.DeleteBuffer(buffer.Handle);
                }

                buffer = CreateTopologyRemapBuffer(topology, count);
                _topologyRemapBuffers[topology] = buffer;

                return buffer.ToRange();
            }

            return buffer.ToRange(Math.Max(GetPrimitivesCount(topology, count) * GetVerticesPerPrimitive(topology), 1) * sizeof(uint));
        }

        /// <summary>
        /// Creates a new topology remap buffer.
        /// </summary>
        /// <param name="topology">Topology</param>
        /// <param name="count">Maximum of vertices that will be accessed</param>
        /// <returns>Remap buffer range</returns>
        private IndexBuffer CreateTopologyRemapBuffer(PrimitiveTopology topology, int count)
        {
            // Size can't be zero as creating zero sized buffers is invalid.
            Span<int> data = new int[Math.Max(GetPrimitivesCount(topology, count) * GetVerticesPerPrimitive(topology), 1)];

            switch (topology)
            {
                case PrimitiveTopology.Points:
                case PrimitiveTopology.Lines:
                case PrimitiveTopology.LinesAdjacency:
                case PrimitiveTopology.Triangles:
                case PrimitiveTopology.TrianglesAdjacency:
                case PrimitiveTopology.Patches:
                    for (int index = 0; index < data.Length; index++)
                    {
                        data[index] = index;
                    }
                    break;
                case PrimitiveTopology.LineLoop:
                    data[^1] = 0;

                    for (int index = 0; index < ((data.Length - 1) & ~1); index += 2)
                    {
                        data[index] = index >> 1;
                        data[index + 1] = (index >> 1) + 1;
                    }
                    break;
                case PrimitiveTopology.LineStrip:
                    for (int index = 0; index < ((data.Length - 1) & ~1); index += 2)
                    {
                        data[index] = index >> 1;
                        data[index + 1] = (index >> 1) + 1;
                    }
                    break;
                case PrimitiveTopology.TriangleStrip:
                    int tsTrianglesCount = data.Length / 3;
                    int tsOutIndex = 3;

                    if (tsTrianglesCount > 0)
                    {
                        data[0] = 0;
                        data[1] = 1;
                        data[2] = 2;
                    }

                    for (int tri = 1; tri < tsTrianglesCount; tri++)
                    {
                        int baseIndex = tri * 3;

                        if ((tri & 1) != 0)
                        {
                            data[baseIndex] = tsOutIndex - 1;
                            data[baseIndex + 1] = tsOutIndex - 2;
                            data[baseIndex + 2] = tsOutIndex++;
                        }
                        else
                        {
                            data[baseIndex] = tsOutIndex - 2;
                            data[baseIndex + 1] = tsOutIndex - 1;
                            data[baseIndex + 2] = tsOutIndex++;
                        }
                    }
                    break;
                case PrimitiveTopology.TriangleFan:
                case PrimitiveTopology.Polygon:
                    int tfTrianglesCount = data.Length / 3;
                    int tfOutIndex = 1;

                    for (int index = 0; index < tfTrianglesCount * 3; index += 3)
                    {
                        data[index] = 0;
                        data[index + 1] = tfOutIndex;
                        data[index + 2] = ++tfOutIndex;
                    }
                    break;
                case PrimitiveTopology.Quads:
                    int qQuadsCount = data.Length / 6;

                    for (int quad = 0; quad < qQuadsCount; quad++)
                    {
                        int index = quad * 6;
                        int qIndex = quad * 4;

                        data[index] = qIndex;
                        data[index + 1] = qIndex + 1;
                        data[index + 2] = qIndex + 2;
                        data[index + 3] = qIndex;
                        data[index + 4] = qIndex + 2;
                        data[index + 5] = qIndex + 3;
                    }
                    break;
                case PrimitiveTopology.QuadStrip:
                    int qsQuadsCount = data.Length / 6;

                    if (qsQuadsCount > 0)
                    {
                        data[0] = 0;
                        data[1] = 1;
                        data[2] = 2;
                        data[3] = 0;
                        data[4] = 2;
                        data[5] = 3;
                    }

                    for (int quad = 1; quad < qsQuadsCount; quad++)
                    {
                        int index = quad * 6;
                        int qIndex = quad * 2;

                        data[index] = qIndex + 1;
                        data[index + 1] = qIndex;
                        data[index + 2] = qIndex + 2;
                        data[index + 3] = qIndex + 1;
                        data[index + 4] = qIndex + 2;
                        data[index + 5] = qIndex + 3;
                    }
                    break;
                case PrimitiveTopology.LineStripAdjacency:
                    for (int index = 0; index < ((data.Length - 3) & ~3); index += 4)
                    {
                        int lIndex = index >> 2;

                        data[index] = lIndex;
                        data[index + 1] = lIndex + 1;
                        data[index + 2] = lIndex + 2;
                        data[index + 3] = lIndex + 3;
                    }
                    break;
                case PrimitiveTopology.TriangleStripAdjacency:
                    int tsaTrianglesCount = data.Length / 6;
                    int tsaOutIndex = 6;

                    if (tsaTrianglesCount > 0)
                    {
                        data[0] = 0;
                        data[1] = 1;
                        data[2] = 2;
                        data[3] = 3;
                        data[4] = 4;
                        data[5] = 5;
                    }

                    for (int tri = 1; tri < tsaTrianglesCount; tri++)
                    {
                        int baseIndex = tri * 6;

                        if ((tri & 1) != 0)
                        {
                            data[baseIndex] = tsaOutIndex - 2;
                            data[baseIndex + 1] = tsaOutIndex - 1;
                            data[baseIndex + 2] = tsaOutIndex - 4;
                            data[baseIndex + 3] = tsaOutIndex - 3;
                            data[baseIndex + 4] = tsaOutIndex++;
                            data[baseIndex + 5] = tsaOutIndex++;
                        }
                        else
                        {
                            data[baseIndex] = tsaOutIndex - 4;
                            data[baseIndex + 1] = tsaOutIndex - 3;
                            data[baseIndex + 2] = tsaOutIndex - 2;
                            data[baseIndex + 3] = tsaOutIndex - 1;
                            data[baseIndex + 4] = tsaOutIndex++;
                            data[baseIndex + 5] = tsaOutIndex++;
                        }
                    }
                    break;
            }

            ReadOnlySpan<byte> dataBytes = MemoryMarshal.Cast<int, byte>(data);

            BufferHandle buffer = _context.Renderer.CreateBuffer(dataBytes.Length, BufferAccess.DeviceMemory);
            _context.Renderer.SetBufferData(buffer, 0, dataBytes);

            return new IndexBuffer(buffer, count, dataBytes.Length);
        }

        /// <summary>
        /// Gets a buffer texture with a given format, for the given index.
        /// </summary>
        /// <param name="index">Index of the buffer texture</param>
        /// <param name="format">Format of the buffer texture</param>
        /// <returns>Buffer texture</returns>
        public ITexture EnsureBufferTexture(int index, Format format)
        {
            return (_bufferTextures[index] ??= new()).Get(_context.Renderer, format);
        }

        /// <summary>
        /// Gets the offset and size of usable storage on the output vertex buffer.
        /// </summary>
        /// <param name="size">Size in bytes that will be used</param>
        /// <returns>Usable offset and size on the buffer</returns>
        public (int, int) GetVertexDataBuffer(int size)
        {
            return EnsureBuffer(ref _vertexDataBuffer, size);
        }

        /// <summary>
        /// Gets the offset and size of usable storage on the output geometry shader vertex buffer.
        /// </summary>
        /// <param name="size">Size in bytes that will be used</param>
        /// <returns>Usable offset and size on the buffer</returns>
        public (int, int) GetGeometryVertexDataBuffer(int size)
        {
            return EnsureBuffer(ref _geometryVertexDataBuffer, size);
        }

        /// <summary>
        /// Gets the offset and size of usable storage on the output geometry shader index buffer.
        /// </summary>
        /// <param name="size">Size in bytes that will be used</param>
        /// <returns>Usable offset and size on the buffer</returns>
        public (int, int) GetGeometryIndexDataBuffer(int size)
        {
            return EnsureBuffer(ref _geometryIndexDataBuffer, size);
        }

        /// <summary>
        /// Gets a range of the output vertex buffer for binding.
        /// </summary>
        /// <param name="offset">Offset of the range</param>
        /// <param name="size">Size of the range in bytes</param>
        /// <param name="write">Indicates if the buffer contents will be modified</param>
        /// <returns>Range</returns>
        public BufferRange GetVertexDataBufferRange(int offset, int size, bool write)
        {
            return new BufferRange(_vertexDataBuffer.Handle, offset, size, write);
        }

        /// <summary>
        /// Gets a range of the output geometry shader vertex buffer for binding.
        /// </summary>
        /// <param name="offset">Offset of the range</param>
        /// <param name="size">Size of the range in bytes</param>
        /// <param name="write">Indicates if the buffer contents will be modified</param>
        /// <returns>Range</returns>
        public BufferRange GetGeometryVertexDataBufferRange(int offset, int size, bool write)
        {
            return new BufferRange(_geometryVertexDataBuffer.Handle, offset, size, write);
        }

        /// <summary>
        /// Gets a range of the output geometry shader index buffer for binding.
        /// </summary>
        /// <param name="offset">Offset of the range</param>
        /// <param name="size">Size of the range in bytes</param>
        /// <param name="write">Indicates if the buffer contents will be modified</param>
        /// <returns>Range</returns>
        public BufferRange GetGeometryIndexDataBufferRange(int offset, int size, bool write)
        {
            return new BufferRange(_geometryIndexDataBuffer.Handle, offset, size, write);
        }

        /// <summary>
        /// Gets the range for a dummy 16 bytes buffer, filled with zeros.
        /// </summary>
        /// <returns>Dummy buffer range</returns>
        public BufferRange GetDummyBufferRange()
        {
            if (_dummyBuffer == BufferHandle.Null)
            {
                _dummyBuffer = _context.Renderer.CreateBuffer(DummyBufferSize, BufferAccess.DeviceMemory);
                _context.Renderer.Pipeline.ClearBuffer(_dummyBuffer, 0, DummyBufferSize, 0);
            }

            return new BufferRange(_dummyBuffer, 0, DummyBufferSize);
        }

        /// <summary>
        /// Gets the range for a sequential index buffer, with ever incrementing index values.
        /// </summary>
        /// <param name="count">Minimum number of indices that the buffer should have</param>
        /// <returns>Buffer handle</returns>
        public BufferHandle GetSequentialIndexBuffer(int count)
        {
            if (_sequentialIndexBufferCount < count)
            {
                if (_sequentialIndexBuffer != BufferHandle.Null)
                {
                    _context.Renderer.DeleteBuffer(_sequentialIndexBuffer);
                }

                _sequentialIndexBuffer = _context.Renderer.CreateBuffer(count * sizeof(uint), BufferAccess.DeviceMemory);
                _sequentialIndexBufferCount = count;

                Span<int> data = new int[count];

                for (int index = 0; index < count; index++)
                {
                    data[index] = index;
                }

                _context.Renderer.SetBufferData(_sequentialIndexBuffer, 0, MemoryMarshal.Cast<int, byte>(data));
            }

            return _sequentialIndexBuffer;
        }

        /// <summary>
        /// Ensure that a buffer exists, is large enough, and allocates a sub-region of the specified size inside the buffer.
        /// </summary>
        /// <param name="buffer">Buffer state</param>
        /// <param name="size">Required size in bytes</param>
        /// <returns>Allocated offset and size</returns>
        private (int, int) EnsureBuffer(ref Buffer buffer, int size)
        {
            int newSize = buffer.Offset + size;

            if (buffer.Size < newSize)
            {
                if (buffer.Handle != BufferHandle.Null)
                {
                    _context.Renderer.DeleteBuffer(buffer.Handle);
                }

                buffer.Handle = _context.Renderer.CreateBuffer(newSize, BufferAccess.DeviceMemory);
                buffer.Size = newSize;
            }

            int offset = buffer.Offset;

            buffer.Offset = BitUtils.AlignUp(newSize, _context.Capabilities.StorageBufferOffsetAlignment);

            return (offset, size);
        }

        /// <summary>
        /// Frees all buffer sub-regions that were previously allocated.
        /// </summary>
        public void FreeBuffers()
        {
            _vertexDataBuffer.Offset = 0;
            _geometryVertexDataBuffer.Offset = 0;
            _geometryIndexDataBuffer.Offset = 0;
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                for (int index = 0; index < _bufferTextures.Length; index++)
                {
                    _bufferTextures[index]?.Dispose();
                    _bufferTextures[index] = null;
                }

                DestroyIfNotNull(ref _dummyBuffer);
                DestroyIfNotNull(ref _vertexDataBuffer.Handle);
                DestroyIfNotNull(ref _geometryVertexDataBuffer.Handle);
                DestroyIfNotNull(ref _geometryIndexDataBuffer.Handle);
                DestroyIfNotNull(ref _sequentialIndexBuffer);

                foreach (var indexBuffer in _topologyRemapBuffers.Values)
                {
                    _context.Renderer.DeleteBuffer(indexBuffer.Handle);
                }

                _topologyRemapBuffers.Clear();
            }
        }

        /// <summary>
        /// Deletes a buffer if the handle is valid (not null), then sets the handle to null.
        /// </summary>
        /// <param name="handle">Buffer handle</param>
        private void DestroyIfNotNull(ref BufferHandle handle)
        {
            if (handle != BufferHandle.Null)
            {
                _context.Renderer.DeleteBuffer(handle);
                handle = BufferHandle.Null;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}