aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Vulkan/DescriptorSetTemplate.cs
blob: 0c0004b95c73921c71a2f932766cbb271149bcda (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
using Ryujinx.Graphics.GAL;
using Silk.NET.Vulkan;
using System;
using System.Runtime.CompilerServices;

namespace Ryujinx.Graphics.Vulkan
{
    class DescriptorSetTemplate : IDisposable
    {
        private readonly VulkanRenderer _gd;
        private readonly Device _device;

        public readonly DescriptorUpdateTemplate Template;
        public readonly int Size;

        public unsafe DescriptorSetTemplate(VulkanRenderer gd, Device device, ResourceBindingSegment[] segments, PipelineLayoutCacheEntry plce, PipelineBindPoint pbp, int setIndex)
        {
            _gd = gd;
            _device = device;

            // Create a template from the set usages. Assumes the descriptor set is updated in segment order then binding order.

            DescriptorUpdateTemplateEntry* entries = stackalloc DescriptorUpdateTemplateEntry[segments.Length];
            nuint structureOffset = 0;

            for (int seg = 0; seg < segments.Length; seg++)
            {
                ResourceBindingSegment segment = segments[seg];

                int binding = segment.Binding;
                int count = segment.Count;

                if (setIndex == PipelineBase.UniformSetIndex)
                {
                    entries[seg] = new DescriptorUpdateTemplateEntry()
                    {
                        DescriptorType = DescriptorType.UniformBuffer,
                        DstBinding = (uint)binding,
                        DescriptorCount = (uint)count,
                        Offset = structureOffset,
                        Stride = (nuint)Unsafe.SizeOf<DescriptorBufferInfo>()
                    };

                    structureOffset += (nuint)(Unsafe.SizeOf<DescriptorBufferInfo>() * count);
                }
                else if (setIndex == PipelineBase.StorageSetIndex)
                {
                    entries[seg] = new DescriptorUpdateTemplateEntry()
                    {
                        DescriptorType = DescriptorType.StorageBuffer,
                        DstBinding = (uint)binding,
                        DescriptorCount = (uint)count,
                        Offset = structureOffset,
                        Stride = (nuint)Unsafe.SizeOf<DescriptorBufferInfo>()
                    };

                    structureOffset += (nuint)(Unsafe.SizeOf<DescriptorBufferInfo>() * count);
                }
                else if (setIndex == PipelineBase.TextureSetIndex)
                {
                    if (segment.Type != ResourceType.BufferTexture)
                    {
                        entries[seg] = new DescriptorUpdateTemplateEntry()
                        {
                            DescriptorType = DescriptorType.CombinedImageSampler,
                            DstBinding = (uint)binding,
                            DescriptorCount = (uint)count,
                            Offset = structureOffset,
                            Stride = (nuint)Unsafe.SizeOf<DescriptorImageInfo>()
                        };

                        structureOffset += (nuint)(Unsafe.SizeOf<DescriptorImageInfo>() * count);
                    }
                    else
                    {
                        entries[seg] = new DescriptorUpdateTemplateEntry()
                        {
                            DescriptorType = DescriptorType.UniformTexelBuffer,
                            DstBinding = (uint)binding,
                            DescriptorCount = (uint)count,
                            Offset = structureOffset,
                            Stride = (nuint)Unsafe.SizeOf<BufferView>()
                        };

                        structureOffset += (nuint)(Unsafe.SizeOf<BufferView>() * count);
                    }
                }
                else if (setIndex == PipelineBase.ImageSetIndex)
                {
                    if (segment.Type != ResourceType.BufferImage)
                    {
                        entries[seg] = new DescriptorUpdateTemplateEntry()
                        {
                            DescriptorType = DescriptorType.StorageImage,
                            DstBinding = (uint)binding,
                            DescriptorCount = (uint)count,
                            Offset = structureOffset,
                            Stride = (nuint)Unsafe.SizeOf<DescriptorImageInfo>()
                        };

                        structureOffset += (nuint)(Unsafe.SizeOf<DescriptorImageInfo>() * count);
                    }
                    else
                    {
                        entries[seg] = new DescriptorUpdateTemplateEntry()
                        {
                            DescriptorType = DescriptorType.StorageTexelBuffer,
                            DstBinding = (uint)binding,
                            DescriptorCount = (uint)count,
                            Offset = structureOffset,
                            Stride = (nuint)Unsafe.SizeOf<BufferView>()
                        };

                        structureOffset += (nuint)(Unsafe.SizeOf<BufferView>() * count);
                    }
                }
            }

            Size = (int)structureOffset;

            var info = new DescriptorUpdateTemplateCreateInfo()
            {
                SType = StructureType.DescriptorUpdateTemplateCreateInfo,
                DescriptorUpdateEntryCount = (uint)segments.Length,
                PDescriptorUpdateEntries = entries,

                TemplateType = DescriptorUpdateTemplateType.DescriptorSet,
                DescriptorSetLayout = plce.DescriptorSetLayouts[setIndex],
                PipelineBindPoint = pbp,
                PipelineLayout = plce.PipelineLayout,
                Set = (uint)setIndex,
            };

            DescriptorUpdateTemplate result;
            gd.Api.CreateDescriptorUpdateTemplate(device, &info, null, &result).ThrowOnError();

            Template = result;
        }

        public unsafe void Dispose()
        {
            _gd.Api.DestroyDescriptorUpdateTemplate(_device, Template, null);
        }
    }
}