-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
ResourceGroupAllocator.cs
164 lines (137 loc) · 5.74 KB
/
ResourceGroupAllocator.cs
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
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using Stride.Core;
using Stride.Rendering;
using Stride.Shaders;
namespace Stride.Graphics
{
/// <summary>
/// Allocator for resource groups.
/// </summary>
/// <note>Non thread-safe. You should have one such allocator per thread.</note>
public class ResourceGroupAllocator : ComponentBase
{
private readonly GraphicsDevice graphicsDevice;
private readonly GraphicsResourceAllocator allocator;
private readonly List<DescriptorPool> descriptorPools = new List<DescriptorPool>();
private readonly List<BufferPool> bufferPools = new List<BufferPool>();
private readonly List<ResourceGroup> resourceGroupPool = new List<ResourceGroup>();
private int currentResourceGroupPoolIndex = 0;
private DescriptorPool currentDescriptorPool;
private int currentDescriptorPoolIndex = -1;
private BufferPool currentBufferPool;
private int currentBufferPoolIndex = -1;
private CommandList commandList;
public ResourceGroupAllocator(GraphicsResourceAllocator allocator, CommandList commandList)
{
this.allocator = allocator;
this.commandList = commandList;
this.graphicsDevice = commandList.GraphicsDevice;
SetupNextDescriptorPool();
}
protected override void Destroy()
{
foreach (var descriptorPool in descriptorPools)
descriptorPool.Dispose();
descriptorPools.Clear();
currentDescriptorPool = null;
foreach (var bufferPool in bufferPools)
bufferPool.Dispose();
bufferPools.Clear();
currentBufferPool = null;
base.Destroy();
}
public void Reset(CommandList commandList)
{
this.commandList = commandList;
foreach (var descriptorPool in descriptorPools)
{
descriptorPool.Reset();
}
foreach (var bufferPool in bufferPools)
{
bufferPool.Reset();
}
currentResourceGroupPoolIndex = -1;
currentDescriptorPool = descriptorPools[0];
currentDescriptorPoolIndex = 0;
currentBufferPool = null;
currentBufferPoolIndex = -1;
}
public void Flush()
{
currentBufferPool?.Unmap();
}
public ResourceGroup AllocateResourceGroup()
{
ResourceGroup resourceGroup;
if (++currentResourceGroupPoolIndex >= resourceGroupPool.Count)
{
resourceGroupPool.Add(resourceGroup = new ResourceGroup());
}
else
{
resourceGroup = resourceGroupPool[currentResourceGroupPoolIndex];
// Clear object
resourceGroup.DescriptorSet = default(DescriptorSet);
resourceGroup.ConstantBuffer = default(BufferPoolAllocationResult);
}
return resourceGroup;
}
public void PrepareResourceGroup(ResourceGroupLayout resourceGroupLayout, BufferPoolAllocationType constantBufferAllocationType, ResourceGroup resourceGroup)
{
if (resourceGroup == null)
throw new InvalidOperationException();
resourceGroup.DescriptorSet = DescriptorSet.New(graphicsDevice, currentDescriptorPool, resourceGroupLayout.DescriptorSetLayout);
if (!resourceGroup.DescriptorSet.IsValid)
{
SetupNextDescriptorPool();
resourceGroup.DescriptorSet = DescriptorSet.New(graphicsDevice, currentDescriptorPool, resourceGroupLayout.DescriptorSetLayout);
}
if (resourceGroupLayout.ConstantBufferSize > 0)
{
if (currentBufferPool == null || !currentBufferPool.CanAllocate(resourceGroupLayout.ConstantBufferSize))
{
SetupNextBufferPool();
}
currentBufferPool.Allocate(graphicsDevice, resourceGroupLayout.ConstantBufferSize, constantBufferAllocationType, ref resourceGroup.ConstantBuffer);
}
}
private void SetupNextBufferPool()
{
Flush();
currentBufferPoolIndex++;
if (currentBufferPoolIndex >= bufferPools.Count)
{
bufferPools.Add(currentBufferPool = BufferPool.New(allocator, graphicsDevice, 1024 * 1024));
}
else
{
currentBufferPool = bufferPools[currentBufferPoolIndex];
}
currentBufferPool.Map(commandList);
}
private void SetupNextDescriptorPool()
{
currentDescriptorPoolIndex++;
if (currentDescriptorPoolIndex >= descriptorPools.Count)
{
descriptorPools.Add(currentDescriptorPool = DescriptorPool.New(
graphicsDevice,
new[]
{
new DescriptorTypeCount(EffectParameterClass.ConstantBuffer, 16384),
new DescriptorTypeCount(EffectParameterClass.ShaderResourceView, 65536),
new DescriptorTypeCount(EffectParameterClass.UnorderedAccessView, 4096),
new DescriptorTypeCount(EffectParameterClass.Sampler, 16384),
}));
}
else
{
currentDescriptorPool = descriptorPools[currentDescriptorPoolIndex];
}
}
}
}